blob: 9470ab7f5ac277f2deb38398b4e79ed36a7a88d8 [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;
Adrian Roos487374f2017-01-11 15:48:14 -08003389 /**
Selim Cinek4717d862018-04-19 09:19:15 +08003390 * Caches a ambient version of {@link #mCachedAmbientColorIsFor}.
Adrian Roos487374f2017-01-11 15:48:14 -08003391 */
3392 private int mCachedAmbientColor = COLOR_INVALID;
3393 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Selim Cinek4717d862018-04-19 09:19:15 +08003394 /**
3395 * A neutral color color that can be used for icons.
3396 */
3397 private int mNeutralColor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08003398
3399 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08003400 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
3401 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
3402 */
3403 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08003404 private int mTextColorsAreForBackground = COLOR_INVALID;
3405 private int mPrimaryTextColor = COLOR_INVALID;
3406 private int mSecondaryTextColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07003407 private int mBackgroundColor = COLOR_INVALID;
3408 private int mForegroundColor = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07003409 /**
3410 * A temporary location where actions are stored. If != null the view originally has action
3411 * but doesn't have any for this inflation.
3412 */
3413 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07003414 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003415
Anthony Chenad4d1582017-04-10 16:07:58 -07003416 private boolean mTintActionButtons;
3417 private boolean mInNightMode;
3418
Adrian Roos70d7aa32017-01-11 15:39:06 -08003419 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003420 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08003421 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003422 * @param context
3423 * A {@link Context} that will be used by the Builder to construct the
3424 * RemoteViews. The Context will not be held past the lifetime of this Builder
3425 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003426 * @param channelId
3427 * The constructed Notification will be posted on this
3428 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
3429 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003430 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003431 public Builder(Context context, String channelId) {
3432 this(context, (Notification) null);
3433 mN.mChannelId = channelId;
3434 }
3435
3436 /**
3437 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
3438 * instead. All posted Notifications must specify a NotificationChannel Id.
3439 */
3440 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003441 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003442 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003443 }
3444
Joe Onoratocb109a02011-01-18 17:57:41 -08003445 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003446 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02003447 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003448 public Builder(Context context, Notification toAdopt) {
3449 mContext = context;
Anthony Chenad4d1582017-04-10 16:07:58 -07003450 Resources res = mContext.getResources();
3451 mTintActionButtons = res.getBoolean(R.bool.config_tintNotificationActionButtons);
3452
3453 if (res.getBoolean(R.bool.config_enableNightMode)) {
3454 Configuration currentConfig = res.getConfiguration();
3455 mInNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
3456 == Configuration.UI_MODE_NIGHT_YES;
3457 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003458
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003459 if (toAdopt == null) {
3460 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003461 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
3462 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
3463 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003464 mN.priority = PRIORITY_DEFAULT;
3465 mN.visibility = VISIBILITY_PRIVATE;
3466 } else {
3467 mN = toAdopt;
3468 if (mN.actions != null) {
3469 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003470 }
3471
Selim Cineke7238dd2017-12-14 17:48:32 -08003472 if (mN.extras.containsKey(EXTRA_PEOPLE_LIST)) {
3473 ArrayList<Person> people = mN.extras.getParcelableArrayList(EXTRA_PEOPLE_LIST);
3474 mPersonList.addAll(people);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003475 }
3476
Selim Cinek4ac6f602016-06-13 15:47:03 -07003477 if (mN.getSmallIcon() == null && mN.icon != 0) {
3478 setSmallIcon(mN.icon);
3479 }
3480
3481 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
3482 setLargeIcon(mN.largeIcon);
3483 }
3484
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003485 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
3486 if (!TextUtils.isEmpty(templateClass)) {
3487 final Class<? extends Style> styleClass
3488 = getNotificationStyleClass(templateClass);
3489 if (styleClass == null) {
3490 Log.d(TAG, "Unknown style class: " + templateClass);
3491 } else {
3492 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003493 final Constructor<? extends Style> ctor =
3494 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003495 ctor.setAccessible(true);
3496 final Style style = ctor.newInstance();
3497 style.restoreFromExtras(mN.extras);
3498
3499 if (style != null) {
3500 setStyle(style);
3501 }
3502 } catch (Throwable t) {
3503 Log.e(TAG, "Could not create Style", t);
3504 }
3505 }
3506 }
3507
3508 }
3509 }
3510
Lucas Dupina291d192018-06-07 13:59:42 -07003511 private ContrastColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08003512 if (mColorUtil == null) {
Lucas Dupina291d192018-06-07 13:59:42 -07003513 mColorUtil = ContrastColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003514 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003515 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003516 }
3517
3518 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003519 * If this notification is duplicative of a Launcher shortcut, sets the
3520 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
3521 * the shortcut.
3522 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04003523 * This field will be ignored by Launchers that don't support badging, don't show
3524 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003525 *
3526 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
3527 * supersedes
3528 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003529 @NonNull
Julia Reynolds13d898c2017-02-02 12:22:05 -05003530 public Builder setShortcutId(String shortcutId) {
3531 mN.mShortcutId = shortcutId;
3532 return this;
3533 }
3534
3535 /**
Felipe Leme90205ef2019-03-05 09:59:52 -08003536 * Sets the {@link LocusId} associated with this notification.
3537 *
3538 * <p>This method should be called when the {@link LocusId} is used in other places (such
Felipe Leme8c2360b2019-04-17 11:16:17 -07003539 * as {@link ShortcutInfo} and {@link ContentCaptureContext}) so the Android system can
3540 * correlate them.
Felipe Leme90205ef2019-03-05 09:59:52 -08003541 */
3542 @NonNull
3543 public Builder setLocusId(@Nullable LocusId locusId) {
3544 mN.mLocusId = locusId;
3545 return this;
3546 }
3547
3548 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003549 * Sets which icon to display as a badge for this notification.
3550 *
3551 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
3552 * {@link #BADGE_ICON_LARGE}.
3553 *
3554 * Note: This value might be ignored, for launchers that don't support badge icons.
3555 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003556 @NonNull
Julia Reynolds612beb22017-03-30 10:48:30 -04003557 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04003558 mN.mBadgeIcon = icon;
3559 return this;
3560 }
3561
3562 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003563 * Sets the group alert behavior for this notification. Use this method to mute this
3564 * notification if alerts for this notification's group should be handled by a different
3565 * notification. This is only applicable for notifications that belong to a
Julia Reynolds399d9bf2017-08-11 12:52:14 -04003566 * {@link #setGroup(String) group}. This must be called on all notifications you want to
3567 * mute. For example, if you want only the summary of your group to make noise, all
3568 * children in the group should have the group alert behavior {@link #GROUP_ALERT_SUMMARY}.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003569 *
3570 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
3571 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003572 @NonNull
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003573 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
3574 mN.mGroupAlertBehavior = groupAlertBehavior;
3575 return this;
3576 }
3577
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003578 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003579 * Sets the {@link BubbleMetadata} that will be used to display app content in a floating
3580 * window over the existing foreground activity.
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003581 *
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003582 * <p>This data will be ignored unless the notification is posted to a channel that
3583 * allows {@link NotificationChannel#canBubble() bubbles}.</p>
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003584 *
Mady Mellor65dcaa92019-04-03 12:21:44 -07003585 * <p>Notifications allowed to bubble that have valid bubble metadata will display in
3586 * collapsed state outside of the notification shade on unlocked devices. When a user
3587 * interacts with the collapsed state, the bubble intent will be invoked and displayed.</p>
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003588 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003589 @NonNull
Mady Mellorcf8f1b22019-03-07 14:08:21 -08003590 public Builder setBubbleMetadata(@Nullable BubbleMetadata data) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003591 mN.mBubbleMetadata = data;
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003592 return this;
3593 }
3594
Jeff Sharkey000ce802017-04-29 13:13:27 -06003595 /** @removed */
3596 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003597 public Builder setChannel(String channelId) {
3598 mN.mChannelId = channelId;
3599 return this;
3600 }
3601
3602 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003603 * Specifies the channel the notification should be delivered on.
3604 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003605 @NonNull
Julia Reynoldsbad42972017-04-25 13:52:49 -04003606 public Builder setChannelId(String channelId) {
3607 mN.mChannelId = channelId;
3608 return this;
3609 }
3610
Jeff Sharkey000ce802017-04-29 13:13:27 -06003611 /** @removed */
3612 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05003613 public Builder setTimeout(long durationMs) {
3614 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05003615 return this;
3616 }
3617
3618 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003619 * Specifies a duration in milliseconds after which this notification should be canceled,
3620 * if it is not already canceled.
3621 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003622 @NonNull
Julia Reynoldsbad42972017-04-25 13:52:49 -04003623 public Builder setTimeoutAfter(long durationMs) {
3624 mN.mTimeout = durationMs;
3625 return this;
3626 }
3627
3628 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003629 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003630 *
3631 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
3632 * shown anymore by default and must be opted into by using
3633 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003634 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003635 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08003636 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003637 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003638 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003639 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08003640 return this;
3641 }
3642
Joe Onoratocb109a02011-01-18 17:57:41 -08003643 /**
Griff Hazen50c11652014-05-16 09:46:31 -07003644 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07003645 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003646 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
3647 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07003648 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003649 @NonNull
Daniel Sandler0c890492012-09-12 17:23:10 -07003650 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003651 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07003652 return this;
3653 }
3654
3655 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003656 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08003657 *
3658 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003659 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003660 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003661 * Useful when showing an elapsed time (like an ongoing phone call).
3662 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08003663 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07003664 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08003665 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003666 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003667 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07003668 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003669 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003670 @NonNull
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003671 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003672 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003673 return this;
3674 }
3675
3676 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08003677 * Sets the Chronometer to count down instead of counting up.
3678 *
3679 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
3680 * If it isn't set the chronometer will count up.
3681 *
3682 * @see #setUsesChronometer(boolean)
3683 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003684 @NonNull
Adrian Roos96b7e202016-05-17 13:50:38 -07003685 public Builder setChronometerCountDown(boolean countDown) {
3686 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003687 return this;
3688 }
3689
3690 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003691 * Set the small icon resource, which will be used to represent the notification in the
3692 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003693 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003694
3695 * The platform template for the expanded view will draw this icon in the left, unless a
3696 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3697 * icon will be moved to the right-hand side.
3698 *
3699
3700 * @param icon
3701 * A resource ID in the application's package of the drawable to use.
3702 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003703 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003704 @NonNull
Tor Norbye7b9c9122013-05-30 16:48:33 -07003705 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003706 return setSmallIcon(icon != 0
3707 ? Icon.createWithResource(mContext, icon)
3708 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003709 }
3710
Joe Onoratocb109a02011-01-18 17:57:41 -08003711 /**
3712 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3713 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3714 * LevelListDrawable}.
3715 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003716 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003717 * @param level The level to use for the icon.
3718 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003719 * @see Notification#icon
3720 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003721 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003722 @NonNull
Tor Norbye7b9c9122013-05-30 16:48:33 -07003723 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003724 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003725 return setSmallIcon(icon);
3726 }
3727
3728 /**
3729 * Set the small icon, which will be used to represent the notification in the
koprivadebd4ee2018-09-13 10:59:46 -07003730 * status bar and content view (unless overridden there by a
Dan Sandlerd63f9322015-05-06 15:18:49 -04003731 * {@link #setLargeIcon(Bitmap) large icon}).
3732 *
3733 * @param icon An Icon object to use.
3734 * @see Notification#icon
3735 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003736 @NonNull
Dan Sandlerd63f9322015-05-06 15:18:49 -04003737 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003738 mN.setSmallIcon(icon);
3739 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3740 mN.icon = icon.getResId();
3741 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003742 return this;
3743 }
3744
Joe Onoratocb109a02011-01-18 17:57:41 -08003745 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003746 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003747 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003748 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003749 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003750 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003751 return this;
3752 }
3753
Joe Onoratocb109a02011-01-18 17:57:41 -08003754 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003755 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003756 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003757 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003758 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003759 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003760 return this;
3761 }
3762
Joe Onoratocb109a02011-01-18 17:57:41 -08003763 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003764 * This provides some additional information that is displayed in the notification. No
3765 * guarantees are given where exactly it is displayed.
3766 *
3767 * <p>This information should only be provided if it provides an essential
3768 * benefit to the understanding of the notification. The more text you provide the
3769 * less readable it becomes. For example, an email client should only provide the account
3770 * name here if more than one email account has been added.</p>
3771 *
3772 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3773 * notification header area.
3774 *
3775 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3776 * this will be shown in the third line of text in the platform notification template.
3777 * You should not be using {@link #setProgress(int, int, boolean)} at the
3778 * same time on those versions; they occupy the same place.
3779 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003780 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003781 @NonNull
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003782 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003783 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003784 return this;
3785 }
3786
3787 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003788 * Provides text that will appear as a link to your application's settings.
3789 *
3790 * <p>This text does not appear within notification {@link Style templates} but may
3791 * appear when the user uses an affordance to learn more about the notification.
3792 * Additionally, this text will not appear unless you provide a valid link target by
3793 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3794 *
3795 * <p>This text is meant to be concise description about what the user can customize
3796 * when they click on this link. The recommended maximum length is 40 characters.
3797 * @param text
3798 * @return
3799 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003800 @NonNull
Julia Reynolds3aedded2017-03-31 14:42:09 -04003801 public Builder setSettingsText(CharSequence text) {
3802 mN.mSettingsText = safeCharSequence(text);
3803 return this;
3804 }
3805
3806 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003807 * Set the remote input history.
3808 *
3809 * This should be set to the most recent inputs that have been sent
3810 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3811 * longer relevant (e.g. for chat notifications once the other party has responded).
3812 *
3813 * The most recent input must be stored at the 0 index, the second most recent at the
3814 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3815 * and how much of each individual input is shown.
3816 *
3817 * <p>Note: The reply text will only be shown on notifications that have least one action
3818 * with a {@code RemoteInput}.</p>
3819 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003820 @NonNull
Adrian Roose458aa82015-12-08 16:17:19 -08003821 public Builder setRemoteInputHistory(CharSequence[] text) {
3822 if (text == null) {
3823 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3824 } else {
3825 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3826 CharSequence[] safe = new CharSequence[N];
3827 for (int i = 0; i < N; i++) {
3828 safe[i] = safeCharSequence(text[i]);
3829 }
3830 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3831 }
3832 return this;
3833 }
3834
3835 /**
Kenny Guya0f6de82018-04-06 16:20:16 +01003836 * Sets whether remote history entries view should have a spinner.
3837 * @hide
3838 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003839 @NonNull
Kenny Guya0f6de82018-04-06 16:20:16 +01003840 public Builder setShowRemoteInputSpinner(boolean showSpinner) {
3841 mN.extras.putBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER, showSpinner);
3842 return this;
3843 }
3844
3845 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +01003846 * Sets whether smart reply buttons should be hidden.
3847 * @hide
3848 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003849 @NonNull
Kenny Guy8cc15d22018-05-09 09:50:55 +01003850 public Builder setHideSmartReplies(boolean hideSmartReplies) {
3851 mN.extras.putBoolean(EXTRA_HIDE_SMART_REPLIES, hideSmartReplies);
3852 return this;
3853 }
3854
3855 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003856 * Sets the number of items this notification represents. May be displayed as a badge count
3857 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003858 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003859 @NonNull
Joe Onorato8595a3d2010-11-19 18:12:07 -08003860 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003861 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003862 return this;
3863 }
3864
Joe Onoratocb109a02011-01-18 17:57:41 -08003865 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003866 * A small piece of additional information pertaining to this notification.
3867 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003868 * The platform template will draw this on the last line of the notification, at the far
3869 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003870 *
3871 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3872 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3873 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003874 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003875 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003876 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003877 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003878 return this;
3879 }
3880
Joe Onoratocb109a02011-01-18 17:57:41 -08003881 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003882 * Set the progress this notification represents.
3883 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003884 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003885 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003886 @NonNull
Jeff Sharkey1c400132011-08-05 14:50:13 -07003887 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003888 mN.extras.putInt(EXTRA_PROGRESS, progress);
3889 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3890 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003891 return this;
3892 }
3893
3894 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003895 * Supply a custom RemoteViews to use instead of the platform template.
3896 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003897 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003898 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003899 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003900 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003901 return setCustomContentView(views);
3902 }
3903
3904 /**
3905 * Supply custom RemoteViews to use instead of the platform template.
3906 *
3907 * This will override the layout that would otherwise be constructed by this Builder
3908 * object.
3909 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003910 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003911 public Builder setCustomContentView(RemoteViews contentView) {
3912 mN.contentView = contentView;
3913 return this;
3914 }
3915
3916 /**
3917 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3918 *
3919 * This will override the expanded layout that would otherwise be constructed by this
3920 * Builder object.
3921 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003922 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003923 public Builder setCustomBigContentView(RemoteViews contentView) {
3924 mN.bigContentView = contentView;
3925 return this;
3926 }
3927
3928 /**
3929 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3930 *
3931 * This will override the heads-up layout that would otherwise be constructed by this
3932 * Builder object.
3933 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003934 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003935 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3936 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003937 return this;
3938 }
3939
Joe Onoratocb109a02011-01-18 17:57:41 -08003940 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003941 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3942 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003943 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3944 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3945 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003946 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003947 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003948 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003949 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003950 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003951 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003952 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003953 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003954 return this;
3955 }
3956
Joe Onoratocb109a02011-01-18 17:57:41 -08003957 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003958 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3959 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003960 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003961 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003962 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003963 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003964 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003965 return this;
3966 }
3967
Joe Onoratocb109a02011-01-18 17:57:41 -08003968 /**
3969 * An intent to launch instead of posting the notification to the status bar.
3970 * Only for use with extremely high-priority notifications demanding the user's
3971 * <strong>immediate</strong> attention, such as an incoming phone call or
3972 * alarm clock that the user has explicitly set to a particular time.
3973 * If this facility is used for something else, please give the user an option
3974 * to turn it off and use a normal notification, as this can be extremely
3975 * disruptive.
3976 *
Chris Wren47c20a12014-06-18 17:27:29 -04003977 * <p>
3978 * The system UI may choose to display a heads-up notification, instead of
3979 * launching this intent, while the user is using the device.
3980 * </p>
Julia Reynolds47fd15f2018-11-28 10:16:00 -05003981 * <p>Apps targeting {@link Build.VERSION_CODES#Q} and above will have to request
3982 * a permission ({@link android.Manifest.permission#USE_FULL_SCREEN_INTENT}) in order to
3983 * use full screen intents.</p>
Chris Wren47c20a12014-06-18 17:27:29 -04003984 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003985 * @param intent The pending intent to launch.
3986 * @param highPriority Passing true will cause this notification to be sent
3987 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003988 *
3989 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003990 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003991 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003992 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003993 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003994 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3995 return this;
3996 }
3997
Joe Onoratocb109a02011-01-18 17:57:41 -08003998 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003999 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004000 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004001 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08004002 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004003 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08004004 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004005 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08004006 return this;
4007 }
4008
Joe Onoratocb109a02011-01-18 17:57:41 -08004009 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04004010 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004011 *
Joe Onoratocb109a02011-01-18 17:57:41 -08004012 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04004013 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004014 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004015 setTicker(tickerText);
4016 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08004017 return this;
4018 }
4019
Joe Onoratocb109a02011-01-18 17:57:41 -08004020 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04004021 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004022 *
4023 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04004024 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
4025 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04004026 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004027 @NonNull
Dan Sandlerd63f9322015-05-06 15:18:49 -04004028 public Builder setLargeIcon(Bitmap b) {
4029 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
4030 }
4031
4032 /**
4033 * Add a large icon to the notification content view.
4034 *
4035 * In the platform template, this image will be shown on the left of the notification view
4036 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
4037 * badge atop the large icon).
4038 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004039 @NonNull
Dan Sandlerd63f9322015-05-06 15:18:49 -04004040 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004041 mN.mLargeIcon = icon;
4042 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08004043 return this;
4044 }
4045
Joe Onoratocb109a02011-01-18 17:57:41 -08004046 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004047 * Set the sound to play.
4048 *
John Spurlockc0650f022014-07-19 13:22:39 -04004049 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
4050 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004051 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004052 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08004053 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004054 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08004055 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004056 mN.sound = sound;
4057 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08004058 return this;
4059 }
4060
Joe Onoratocb109a02011-01-18 17:57:41 -08004061 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004062 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08004063 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004064 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
4065 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004066 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08004067 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07004068 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004069 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08004070 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004071 mN.sound = sound;
4072 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08004073 return this;
4074 }
4075
Joe Onoratocb109a02011-01-18 17:57:41 -08004076 /**
John Spurlockc0650f022014-07-19 13:22:39 -04004077 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
4078 * use during playback.
4079 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004080 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04004081 * @see Notification#sound
4082 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004083 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04004084 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004085 mN.sound = sound;
4086 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04004087 return this;
4088 }
4089
4090 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08004091 * Set the vibration pattern to use.
4092 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004093 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
4094 * <code>pattern</code> parameter.
4095 *
Chris Wren47c20a12014-06-18 17:27:29 -04004096 * <p>
4097 * A notification that vibrates is more likely to be presented as a heads-up notification.
4098 * </p>
4099 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004100 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004101 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08004102 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004103 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004104 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004105 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08004106 return this;
4107 }
4108
Joe Onoratocb109a02011-01-18 17:57:41 -08004109 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004110 * Set the desired color for the indicator LED on the device, as well as the
4111 * blink duty cycle (specified in milliseconds).
4112 *
4113
4114 * Not all devices will honor all (or even any) of these values.
4115 *
Julia Reynolds529e3322017-02-06 08:33:01 -05004116 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004117 * @see Notification#ledARGB
4118 * @see Notification#ledOnMS
4119 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08004120 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004121 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08004122 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004123 mN.ledARGB = argb;
4124 mN.ledOnMS = onMs;
4125 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004126 if (onMs != 0 || offMs != 0) {
4127 mN.flags |= FLAG_SHOW_LIGHTS;
4128 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004129 return this;
4130 }
4131
Joe Onoratocb109a02011-01-18 17:57:41 -08004132 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004133 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08004134 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004135
4136 * Ongoing notifications cannot be dismissed by the user, so your application or service
4137 * must take care of canceling them.
4138 *
4139
4140 * They are typically used to indicate a background task that the user is actively engaged
4141 * with (e.g., playing music) or is pending in some way and therefore occupying the device
4142 * (e.g., a file download, sync operation, active network connection).
4143 *
4144
4145 * @see Notification#FLAG_ONGOING_EVENT
Joe Onoratocb109a02011-01-18 17:57:41 -08004146 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004147 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08004148 public Builder setOngoing(boolean ongoing) {
4149 setFlag(FLAG_ONGOING_EVENT, ongoing);
4150 return this;
4151 }
4152
Joe Onoratocb109a02011-01-18 17:57:41 -08004153 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08004154 * Set whether this notification should be colorized. When set, the color set with
4155 * {@link #setColor(int)} will be used as the background color of this notification.
4156 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08004157 * This should only be used for high priority ongoing tasks like navigation, an ongoing
4158 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08004159 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07004160 * For most styles, the coloring will only be applied if the notification is for a
4161 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08004162 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07004163 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08004164 *
Aurimas Liutikas7f695332018-05-31 21:07:32 -07004165 * @see #setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08004166 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08004167 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004168 @NonNull
Selim Cinek7b9605b2017-01-19 17:36:00 -08004169 public Builder setColorized(boolean colorize) {
4170 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
4171 return this;
4172 }
4173
4174 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08004175 * Set this flag if you would only like the sound, vibrate
4176 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004177 *
4178 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08004179 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004180 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08004181 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
4182 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
4183 return this;
4184 }
4185
Joe Onoratocb109a02011-01-18 17:57:41 -08004186 /**
Julia Reynolds04499532016-09-13 14:04:53 -04004187 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004188 *
4189 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08004190 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004191 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08004192 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08004193 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08004194 return this;
4195 }
4196
Joe Onoratocb109a02011-01-18 17:57:41 -08004197 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08004198 * Set whether or not this notification should not bridge to other devices.
4199 *
4200 * <p>Some notifications can be bridged to other devices for remote display.
4201 * This hint can be set to recommend this notification not be bridged.
4202 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004203 @NonNull
Griff Hazendfcb0802014-02-11 12:00:00 -08004204 public Builder setLocalOnly(boolean localOnly) {
4205 setFlag(FLAG_LOCAL_ONLY, localOnly);
4206 return this;
4207 }
4208
4209 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004210 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08004211 * <p>
4212 * The value should be one or more of the following fields combined with
4213 * bitwise-or:
4214 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
4215 * <p>
4216 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004217 *
4218 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05004219 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004220 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08004221 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004222 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004223 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004224 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08004225 return this;
4226 }
4227
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004228 /**
4229 * Set the priority of this notification.
4230 *
4231 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004232 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004233 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004234 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07004235 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004236 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004237 return this;
4238 }
Joe Malin8d40d042012-11-05 11:36:40 -08004239
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004240 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04004241 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08004242 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04004243 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004244 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004245 @NonNull
John Spurlockfd7f1e02014-03-18 16:41:57 -04004246 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004247 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004248 return this;
4249 }
4250
4251 /**
Chris Wrendde75302014-03-26 17:24:15 -04004252 * Add a person that is relevant to this notification.
4253 *
Chris Wrene6c48932014-09-29 17:19:27 -04004254 * <P>
4255 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04004256 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4257 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4258 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04004259 * </P>
4260 *
4261 * <P>
4262 * The person should be specified by the {@code String} representation of a
4263 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
4264 * </P>
4265 *
4266 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
4267 * URIs. The path part of these URIs must exist in the contacts database, in the
4268 * appropriate column, or the reference will be discarded as invalid. Telephone schema
4269 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
Selim Cineke7238dd2017-12-14 17:48:32 -08004270 * It is also possible to provide a URI with the schema {@code name:} in order to uniquely
4271 * identify a person without an entry in the contacts database.
Chris Wrene6c48932014-09-29 17:19:27 -04004272 * </P>
4273 *
4274 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04004275 * @see Notification#EXTRA_PEOPLE
Selim Cineke7238dd2017-12-14 17:48:32 -08004276 * @deprecated use {@link #addPerson(Person)}
Chris Wrendde75302014-03-26 17:24:15 -04004277 */
Chris Wrene6c48932014-09-29 17:19:27 -04004278 public Builder addPerson(String uri) {
Selim Cinek9acd6732018-03-23 16:39:02 -07004279 addPerson(new Person.Builder().setUri(uri).build());
Selim Cineke7238dd2017-12-14 17:48:32 -08004280 return this;
4281 }
4282
4283 /**
4284 * Add a person that is relevant to this notification.
4285 *
4286 * <P>
4287 * Depending on user preferences, this annotation may allow the notification to pass
4288 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4289 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4290 * appear more prominently in the user interface.
4291 * </P>
4292 *
4293 * <P>
4294 * A person should usually contain a uri in order to benefit from the ranking boost.
4295 * However, even if no uri is provided, it's beneficial to provide other people in the
4296 * notification, such that listeners and voice only devices can announce and handle them
4297 * properly.
4298 * </P>
4299 *
4300 * @param person the person to add.
4301 * @see Notification#EXTRA_PEOPLE_LIST
4302 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004303 @NonNull
Selim Cineke7238dd2017-12-14 17:48:32 -08004304 public Builder addPerson(Person person) {
4305 mPersonList.add(person);
Chris Wrendde75302014-03-26 17:24:15 -04004306 return this;
4307 }
4308
4309 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004310 * Set this notification to be part of a group of notifications sharing the same key.
4311 * Grouped notifications may display in a cluster or stack on devices which
4312 * support such rendering.
4313 *
4314 * <p>To make this notification the summary for its group, also call
4315 * {@link #setGroupSummary}. A sort order can be specified for group members by using
4316 * {@link #setSortKey}.
4317 * @param groupKey The group key of the group.
4318 * @return this object for method chaining
4319 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004320 @NonNull
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004321 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004322 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004323 return this;
4324 }
4325
4326 /**
4327 * Set this notification to be the group summary for a group of notifications.
4328 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04004329 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
4330 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004331 * @param isGroupSummary Whether this notification should be a group summary.
4332 * @return this object for method chaining
4333 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004334 @NonNull
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004335 public Builder setGroupSummary(boolean isGroupSummary) {
4336 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
4337 return this;
4338 }
4339
4340 /**
4341 * Set a sort key that orders this notification among other notifications from the
4342 * same package. This can be useful if an external sort was already applied and an app
4343 * would like to preserve this. Notifications will be sorted lexicographically using this
4344 * value, although providing different priorities in addition to providing sort key may
4345 * cause this value to be ignored.
4346 *
4347 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07004348 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004349 *
4350 * @see String#compareTo(String)
4351 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004352 @NonNull
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004353 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004354 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004355 return this;
4356 }
4357
4358 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004359 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004360 *
Griff Hazen720042b2014-02-24 15:46:56 -08004361 * <p>Values within the Bundle will replace existing extras values in this Builder.
4362 *
4363 * @see Notification#extras
4364 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004365 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07004366 public Builder addExtras(Bundle extras) {
4367 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004368 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08004369 }
4370 return this;
4371 }
4372
4373 /**
4374 * Set metadata for this notification.
4375 *
4376 * <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 -04004377 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004378 * called.
4379 *
Griff Hazen720042b2014-02-24 15:46:56 -08004380 * <p>Replaces any existing extras values with those from the provided Bundle.
4381 * Use {@link #addExtras} to merge in metadata instead.
4382 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004383 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004384 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004385 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07004386 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004387 if (extras != null) {
4388 mUserExtras = extras;
4389 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004390 return this;
4391 }
4392
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004393 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004394 * Get the current metadata Bundle used by this notification Builder.
4395 *
4396 * <p>The returned Bundle is shared with this Builder.
4397 *
4398 * <p>The current contents of this Bundle are copied into the Notification each time
4399 * {@link #build()} is called.
4400 *
4401 * @see Notification#extras
4402 */
4403 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004404 return mUserExtras;
4405 }
4406
4407 private Bundle getAllExtras() {
4408 final Bundle saveExtras = (Bundle) mUserExtras.clone();
4409 saveExtras.putAll(mN.extras);
4410 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08004411 }
4412
4413 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004414 * Add an action to this notification. Actions are typically displayed by
4415 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004416 * <p>
4417 * Every action must have an icon (32dp square and matching the
4418 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4419 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4420 * <p>
4421 * A notification in its expanded form can display up to 3 actions, from left to right in
4422 * the order they were added. Actions will not be displayed when the notification is
4423 * collapsed, however, so be sure that any essential functions may be accessed by the user
4424 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004425 *
4426 * @param icon Resource ID of a drawable that represents the action.
4427 * @param title Text describing the action.
4428 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04004429 *
4430 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004431 */
Dan Sandler86647982015-05-13 23:41:13 -04004432 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004433 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004434 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004435 return this;
4436 }
4437
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004438 /**
Griff Hazen959591e2014-05-15 22:26:18 -07004439 * Add an action to this notification. Actions are typically displayed by
4440 * the system as a button adjacent to the notification content.
4441 * <p>
4442 * Every action must have an icon (32dp square and matching the
4443 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4444 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4445 * <p>
4446 * A notification in its expanded form can display up to 3 actions, from left to right in
4447 * the order they were added. Actions will not be displayed when the notification is
4448 * collapsed, however, so be sure that any essential functions may be accessed by the user
4449 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
4450 *
4451 * @param action The action to add.
4452 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004453 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07004454 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08004455 if (action != null) {
4456 mActions.add(action);
4457 }
Griff Hazen959591e2014-05-15 22:26:18 -07004458 return this;
4459 }
4460
4461 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004462 * Alter the complete list of actions attached to this notification.
4463 * @see #addAction(Action).
4464 *
4465 * @param actions
4466 * @return
4467 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004468 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004469 public Builder setActions(Action... actions) {
4470 mActions.clear();
4471 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08004472 if (actions[i] != null) {
4473 mActions.add(actions[i]);
4474 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004475 }
4476 return this;
4477 }
4478
4479 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004480 * Add a rich notification style to be applied at build time.
4481 *
4482 * @param style Object responsible for modifying the notification style.
4483 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004484 @NonNull
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004485 public Builder setStyle(Style style) {
4486 if (mStyle != style) {
4487 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004488 if (mStyle != null) {
4489 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004490 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
4491 } else {
4492 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004493 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004494 }
4495 return this;
4496 }
4497
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004498 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05004499 * Returns the style set by {@link #setStyle(Style)}.
4500 */
4501 public Style getStyle() {
4502 return mStyle;
4503 }
4504
4505 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004506 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07004507 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004508 * @return The same Builder.
4509 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004510 @NonNull
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06004511 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004512 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004513 return this;
4514 }
4515
4516 /**
4517 * Supply a replacement Notification whose contents should be shown in insecure contexts
4518 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
4519 * @param n A replacement notification, presumably with some or all info redacted.
4520 * @return The same Builder.
4521 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004522 @NonNull
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004523 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004524 if (n != null) {
4525 mN.publicVersion = new Notification();
4526 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
4527 } else {
4528 mN.publicVersion = null;
4529 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004530 return this;
4531 }
4532
Griff Hazenb720abe2014-05-20 13:15:30 -07004533 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004534 * Apply an extender to this notification builder. Extenders may be used to add
4535 * metadata or change options on this builder.
4536 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004537 @NonNull
Griff Hazen61a9e862014-05-22 16:05:19 -07004538 public Builder extend(Extender extender) {
4539 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004540 return this;
4541 }
4542
Dan Sandler4e787062015-06-17 15:09:48 -04004543 /**
4544 * @hide
4545 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004546 @NonNull
Julia Reynoldse46bb372016-03-17 11:05:58 -04004547 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08004548 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004549 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004550 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004551 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004552 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04004553 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08004554 }
4555
Dan Sandler26e81cf2014-05-06 10:01:27 -04004556 /**
4557 * Sets {@link Notification#color}.
4558 *
4559 * @param argb The accent color to use
4560 *
4561 * @return The same Builder.
4562 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004563 @NonNull
Tor Norbye80756e32015-03-02 09:39:27 -08004564 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004565 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004566 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04004567 return this;
4568 }
4569
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004570 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04004571 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
4572 // This user can never be a badged profile,
4573 // and also includes USER_ALL system notifications.
4574 return null;
4575 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02004576 // Note: This assumes that the current user can read the profile badge of the
4577 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08004578 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05004579 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004580 }
4581
4582 private Bitmap getProfileBadge() {
4583 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01004584 if (badge == null) {
4585 return null;
4586 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004587 final int size = mContext.getResources().getDimensionPixelSize(
4588 R.dimen.notification_badge_size);
4589 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004590 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004591 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004592 badge.draw(canvas);
4593 return bitmap;
4594 }
4595
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004596 private void bindProfileBadge(RemoteViews contentView, StandardTemplateParams p) {
Kenny Guy98193ea2014-07-24 19:54:37 +01004597 Bitmap profileBadge = getProfileBadge();
4598
Kenny Guy98193ea2014-07-24 19:54:37 +01004599 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004600 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
4601 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004602 if (isColorized(p)) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004603 contentView.setDrawableTint(R.id.profile_badge, false,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004604 getPrimaryTextColor(p), PorterDuff.Mode.SRC_ATOP);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004605 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004606 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004607 }
4608
Gus Prevasbae5ba32018-11-28 15:45:13 -05004609 private void bindAlertedIcon(RemoteViews contentView, StandardTemplateParams p) {
4610 contentView.setDrawableTint(
4611 R.id.alerted_icon,
4612 false /* targetBackground */,
4613 getNeutralColor(p),
4614 PorterDuff.Mode.SRC_ATOP);
4615 }
4616
Julia Reynoldsfc640012018-02-21 12:25:27 -05004617 /**
4618 * @hide
4619 */
4620 public boolean usesStandardHeader() {
4621 if (mN.mUsesStandardHeader) {
4622 return true;
4623 }
4624 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.N) {
4625 if (mN.contentView == null && mN.bigContentView == null) {
4626 return true;
4627 }
4628 }
4629 boolean contentViewUsesHeader = mN.contentView == null
4630 || STANDARD_LAYOUTS.contains(mN.contentView.getLayoutId());
4631 boolean bigContentViewUsesHeader = mN.bigContentView == null
4632 || STANDARD_LAYOUTS.contains(mN.bigContentView.getLayoutId());
4633 return contentViewUsesHeader && bigContentViewUsesHeader;
4634 }
4635
Christoph Studerfe718432014-09-01 18:21:18 +02004636 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004637 resetNotificationHeader(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02004638 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08004639 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004640 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08004641 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004642 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08004643 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08004644 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004645 }
4646
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004647 /**
4648 * Resets the notification header to its original state
4649 */
4650 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07004651 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
4652 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08004653 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004654 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02004655 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004656 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07004657 contentView.setTextViewText(R.id.header_text, null);
Selim Cinekafeed292017-12-12 17:32:44 -08004658 contentView.setViewVisibility(R.id.header_text_secondary, View.GONE);
4659 contentView.setTextViewText(R.id.header_text_secondary, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004660 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinekafeed292017-12-12 17:32:44 -08004661 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004662 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004663 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004664 contentView.setImageViewIcon(R.id.profile_badge, null);
4665 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Gus Prevasa3226492018-10-23 11:10:09 -04004666 contentView.setViewVisibility(R.id.alerted_icon, View.GONE);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004667 mN.mUsesStandardHeader = false;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004668 }
4669
Selim Cinek384804b2018-04-18 14:31:07 +08004670 private RemoteViews applyStandardTemplate(int resId, TemplateBindResult result) {
4671 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this),
4672 result);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004673 }
4674
Selim Cinek384804b2018-04-18 14:31:07 +08004675 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p,
4676 TemplateBindResult result) {
Kenny Guy77320062014-08-27 21:37:15 +01004677 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04004678
Christoph Studerfe718432014-09-01 18:21:18 +02004679 resetStandardTemplate(contentView);
4680
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004681 final Bundle ex = mN.extras;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004682 updateBackgroundColor(contentView, p);
4683 bindNotificationHeader(contentView, p);
Selim Cinek384804b2018-04-18 14:31:07 +08004684 bindLargeIconAndReply(contentView, p, result);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004685 boolean showProgress = handleProgressBar(contentView, ex, p);
Adrian Roos70d7aa32017-01-11 15:39:06 -08004686 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08004687 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004688 contentView.setTextViewText(R.id.title, processTextSpans(p.title));
Lucas Dupin00be88f2019-01-03 17:50:52 -08004689 setTextViewColorPrimary(contentView, R.id.title, p);
Selim Cinek954cc232016-05-20 13:29:23 -07004690 contentView.setViewLayoutWidth(R.id.title, showProgress
4691 ? ViewGroup.LayoutParams.WRAP_CONTENT
4692 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08004693 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08004694 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08004695 int textId = showProgress ? com.android.internal.R.id.text_line_1
4696 : com.android.internal.R.id.text;
Selim Cinek48f66b72017-08-18 16:17:51 -07004697 contentView.setTextViewText(textId, processTextSpans(p.text));
Lucas Dupin00be88f2019-01-03 17:50:52 -08004698 setTextViewColorSecondary(contentView, textId, p);
Selim Cinek41598732016-01-11 16:58:37 -08004699 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08004700 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08004701
Selim Cinek279fa862016-06-14 10:57:25 -07004702 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004703
Selim Cinek29603462015-11-17 19:04:39 -08004704 return contentView;
4705 }
4706
Selim Cinek48f66b72017-08-18 16:17:51 -07004707 private CharSequence processTextSpans(CharSequence text) {
Lucas Dupind3c99322018-09-27 10:22:29 -07004708 if (hasForegroundColor() || mInNightMode) {
Lucas Dupina291d192018-06-07 13:59:42 -07004709 return ContrastColorUtil.clearColorSpans(text);
Selim Cinek48f66b72017-08-18 16:17:51 -07004710 }
4711 return text;
4712 }
4713
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004714 private void setTextViewColorPrimary(RemoteViews contentView, int id,
4715 StandardTemplateParams p) {
4716 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004717 contentView.setTextColor(id, mPrimaryTextColor);
4718 }
4719
Selim Cinek48f66b72017-08-18 16:17:51 -07004720 private boolean hasForegroundColor() {
4721 return mForegroundColor != COLOR_INVALID;
4722 }
4723
Selim Cinek389edcd2017-05-11 19:16:44 -07004724 /**
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004725 * Return the primary text color using the existing template params
Selim Cinek389edcd2017-05-11 19:16:44 -07004726 * @hide
4727 */
4728 @VisibleForTesting
4729 public int getPrimaryTextColor() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004730 return getPrimaryTextColor(mParams);
4731 }
4732
4733 /**
4734 * @param p the template params to inflate this with
4735 * @return the primary text color
4736 * @hide
4737 */
4738 @VisibleForTesting
4739 public int getPrimaryTextColor(StandardTemplateParams p) {
4740 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004741 return mPrimaryTextColor;
4742 }
4743
Selim Cinek389edcd2017-05-11 19:16:44 -07004744 /**
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004745 * Return the secondary text color using the existing template params
Selim Cinek389edcd2017-05-11 19:16:44 -07004746 * @hide
4747 */
4748 @VisibleForTesting
4749 public int getSecondaryTextColor() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004750 return getSecondaryTextColor(mParams);
4751 }
4752
4753 /**
4754 * @param p the template params to inflate this with
4755 * @return the secondary text color
4756 * @hide
4757 */
4758 @VisibleForTesting
4759 public int getSecondaryTextColor(StandardTemplateParams p) {
4760 ensureColors(p);
Selim Cinek389edcd2017-05-11 19:16:44 -07004761 return mSecondaryTextColor;
4762 }
4763
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004764 private void setTextViewColorSecondary(RemoteViews contentView, int id,
4765 StandardTemplateParams p) {
4766 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004767 contentView.setTextColor(id, mSecondaryTextColor);
4768 }
4769
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004770 private void ensureColors(StandardTemplateParams p) {
4771 int backgroundColor = getBackgroundColor(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004772 if (mPrimaryTextColor == COLOR_INVALID
4773 || mSecondaryTextColor == COLOR_INVALID
Selim Cinek7b9605b2017-01-19 17:36:00 -08004774 || mTextColorsAreForBackground != backgroundColor) {
4775 mTextColorsAreForBackground = backgroundColor;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004776 if (!hasForegroundColor() || !isColorized(p)) {
Lucas Dupina291d192018-06-07 13:59:42 -07004777 mPrimaryTextColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004778 backgroundColor, mInNightMode);
Lucas Dupina291d192018-06-07 13:59:42 -07004779 mSecondaryTextColor = ContrastColorUtil.resolveSecondaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004780 backgroundColor, mInNightMode);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004781 if (backgroundColor != COLOR_DEFAULT && isColorized(p)) {
Lucas Dupina291d192018-06-07 13:59:42 -07004782 mPrimaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004783 mPrimaryTextColor, backgroundColor, 4.5);
Lucas Dupina291d192018-06-07 13:59:42 -07004784 mSecondaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004785 mSecondaryTextColor, backgroundColor, 4.5);
4786 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07004787 } else {
Lucas Dupina291d192018-06-07 13:59:42 -07004788 double backLum = ContrastColorUtil.calculateLuminance(backgroundColor);
4789 double textLum = ContrastColorUtil.calculateLuminance(mForegroundColor);
4790 double contrast = ContrastColorUtil.calculateContrast(mForegroundColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004791 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07004792 // We only respect the given colors if worst case Black or White still has
4793 // contrast
4794 boolean backgroundLight = backLum > textLum
4795 && satisfiesTextContrast(backgroundColor, Color.BLACK)
4796 || backLum <= textLum
4797 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004798 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07004799 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004800 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004801 mForegroundColor,
4802 backgroundColor,
4803 true /* findFG */,
4804 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004805 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004806 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004807 } else {
4808 mSecondaryTextColor =
Lucas Dupina291d192018-06-07 13:59:42 -07004809 ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004810 mForegroundColor,
4811 backgroundColor,
4812 true /* findFG */,
4813 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004814 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004815 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004816 }
4817 } else {
4818 mPrimaryTextColor = mForegroundColor;
Lucas Dupina291d192018-06-07 13:59:42 -07004819 mSecondaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004820 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4821 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Lucas Dupina291d192018-06-07 13:59:42 -07004822 if (ContrastColorUtil.calculateContrast(mSecondaryTextColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004823 backgroundColor) < 4.5f) {
4824 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07004825 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004826 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004827 mSecondaryTextColor,
4828 backgroundColor,
4829 true /* findFG */,
4830 4.5f);
4831 } else {
4832 mSecondaryTextColor
Lucas Dupina291d192018-06-07 13:59:42 -07004833 = ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004834 mSecondaryTextColor,
4835 backgroundColor,
4836 true /* findFG */,
4837 4.5f);
4838 }
Lucas Dupina291d192018-06-07 13:59:42 -07004839 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004840 mSecondaryTextColor, backgroundLight
4841 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4842 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004843 }
4844 }
4845 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004846 }
4847 }
4848
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004849 private void updateBackgroundColor(RemoteViews contentView,
4850 StandardTemplateParams p) {
4851 if (isColorized(p)) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004852 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004853 getBackgroundColor(p));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004854 } else {
4855 // Clear it!
4856 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4857 0);
4858 }
4859 }
4860
Selim Cinek860b6da2015-12-16 19:02:19 -08004861 /**
4862 * @param remoteView the remote view to update the minheight in
4863 * @param hasMinHeight does it have a mimHeight
4864 * @hide
4865 */
4866 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4867 int minHeight = 0;
4868 if (hasMinHeight) {
4869 // we need to set the minHeight of the notification
4870 minHeight = mContext.getResources().getDimensionPixelSize(
4871 com.android.internal.R.dimen.notification_min_content_height);
4872 }
4873 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4874 }
4875
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004876 private boolean handleProgressBar(RemoteViews contentView, Bundle ex,
4877 StandardTemplateParams p) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004878 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4879 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4880 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004881 if (p.hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004882 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004883 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004884 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004885 contentView.setProgressBackgroundTintList(
4886 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4887 R.color.notification_progress_background_color)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004888 if (getRawColor(p) != COLOR_DEFAULT) {
Beth Thibodeau38275c32019-05-08 16:36:59 -04004889 int color = isColorized(p) ? getPrimaryTextColor(p) : resolveContrastColor(p);
4890 ColorStateList colorStateList = ColorStateList.valueOf(color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004891 contentView.setProgressTintList(R.id.progress, colorStateList);
4892 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004893 }
Selim Cinek29603462015-11-17 19:04:39 -08004894 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004895 } else {
4896 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004897 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004898 }
Joe Onorato561d3852010-11-20 18:09:34 -08004899 }
4900
Selim Cinek384804b2018-04-18 14:31:07 +08004901 private void bindLargeIconAndReply(RemoteViews contentView, StandardTemplateParams p,
4902 TemplateBindResult result) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004903 boolean largeIconShown = bindLargeIcon(contentView, p);
4904 boolean replyIconShown = bindReplyIcon(contentView, p);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004905 boolean iconContainerVisible = largeIconShown || replyIconShown;
Selim Cinek384804b2018-04-18 14:31:07 +08004906 contentView.setViewVisibility(R.id.right_icon_container,
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004907 iconContainerVisible ? View.VISIBLE : View.GONE);
Selim Cinek1c72fa02018-04-23 18:00:54 +08004908 int marginEnd = calculateMarginEnd(largeIconShown, replyIconShown);
4909 contentView.setViewLayoutMarginEnd(R.id.line1, marginEnd);
4910 contentView.setViewLayoutMarginEnd(R.id.text, marginEnd);
4911 contentView.setViewLayoutMarginEnd(R.id.progress, marginEnd);
Selim Cinek384804b2018-04-18 14:31:07 +08004912 if (result != null) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08004913 result.setIconMarginEnd(marginEnd);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004914 result.setRightIconContainerVisible(iconContainerVisible);
Selim Cinek384804b2018-04-18 14:31:07 +08004915 }
4916 }
4917
Selim Cinek1c72fa02018-04-23 18:00:54 +08004918 private int calculateMarginEnd(boolean largeIconShown, boolean replyIconShown) {
4919 int marginEnd = 0;
4920 int contentMargin = mContext.getResources().getDimensionPixelSize(
4921 R.dimen.notification_content_margin_end);
4922 int iconSize = mContext.getResources().getDimensionPixelSize(
4923 R.dimen.notification_right_icon_size);
4924 if (replyIconShown) {
4925 // The size of the reply icon
4926 marginEnd += iconSize;
4927
4928 int replyInset = mContext.getResources().getDimensionPixelSize(
4929 R.dimen.notification_reply_inset);
4930 // We're subtracting the inset of the reply icon to make sure it's
4931 // aligned nicely on the right, and remove it from the following padding
4932 marginEnd -= replyInset * 2;
4933 }
4934 if (largeIconShown) {
4935 // adding size of the right icon
4936 marginEnd += iconSize;
4937
4938 if (replyIconShown) {
4939 // We also add some padding to the reply icon if it's around
4940 marginEnd += contentMargin;
4941 }
4942 }
4943 if (replyIconShown || largeIconShown) {
4944 // The padding to the content
4945 marginEnd += contentMargin;
4946 }
4947 return marginEnd;
4948 }
4949
Selim Cinek384804b2018-04-18 14:31:07 +08004950 /**
4951 * Bind the large icon.
4952 * @return if the largeIcon is visible
4953 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004954 private boolean bindLargeIcon(RemoteViews contentView, StandardTemplateParams p) {
Selim Cinek279fa862016-06-14 10:57:25 -07004955 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4956 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4957 }
Lucas Dupin00be88f2019-01-03 17:50:52 -08004958 boolean showLargeIcon = mN.mLargeIcon != null && !p.hideLargeIcon;
Selim Cinek88188f22017-09-19 16:46:56 -07004959 if (showLargeIcon) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004960 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4961 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004962 processLargeLegacyIcon(mN.mLargeIcon, contentView, p);
Selim Cinek88188f22017-09-19 16:46:56 -07004963 }
Selim Cinek384804b2018-04-18 14:31:07 +08004964 return showLargeIcon;
4965 }
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004966
Selim Cinek384804b2018-04-18 14:31:07 +08004967 /**
4968 * Bind the reply icon.
4969 * @return if the reply icon is visible
4970 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004971 private boolean bindReplyIcon(RemoteViews contentView, StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08004972 boolean actionVisible = !p.hideReplyIcon;
Selim Cinek384804b2018-04-18 14:31:07 +08004973 Action action = null;
Selim Cinek88188f22017-09-19 16:46:56 -07004974 if (actionVisible) {
Selim Cinek384804b2018-04-18 14:31:07 +08004975 action = findReplyAction();
4976 actionVisible = action != null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004977 }
Selim Cinek384804b2018-04-18 14:31:07 +08004978 if (actionVisible) {
4979 contentView.setViewVisibility(R.id.reply_icon_action, View.VISIBLE);
4980 contentView.setDrawableTint(R.id.reply_icon_action,
4981 false /* targetBackground */,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004982 getNeutralColor(p),
Selim Cinek384804b2018-04-18 14:31:07 +08004983 PorterDuff.Mode.SRC_ATOP);
4984 contentView.setOnClickPendingIntent(R.id.reply_icon_action, action.actionIntent);
4985 contentView.setRemoteInputs(R.id.reply_icon_action, action.mRemoteInputs);
4986 } else {
4987 contentView.setRemoteInputs(R.id.reply_icon_action, null);
4988 }
4989 contentView.setViewVisibility(R.id.reply_icon_action,
4990 actionVisible ? View.VISIBLE : View.GONE);
4991 return actionVisible;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004992 }
4993
4994 private Action findReplyAction() {
4995 ArrayList<Action> actions = mActions;
4996 if (mOriginalActions != null) {
4997 actions = mOriginalActions;
4998 }
4999 int numActions = actions.size();
5000 for (int i = 0; i < numActions; i++) {
5001 Action action = actions.get(i);
5002 if (hasValidRemoteInput(action)) {
5003 return action;
5004 }
5005 }
5006 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005007 }
5008
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005009 private void bindNotificationHeader(RemoteViews contentView, StandardTemplateParams p) {
5010 bindSmallIcon(contentView, p);
5011 bindHeaderAppName(contentView, p);
Lucas Dupin00be88f2019-01-03 17:50:52 -08005012 bindHeaderText(contentView, p);
5013 bindHeaderTextSecondary(contentView, p);
5014 bindHeaderChronometerAndTime(contentView, p);
5015 bindProfileBadge(contentView, p);
5016 bindAlertedIcon(contentView, p);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005017 bindActivePermissions(contentView, p);
5018 bindExpandButton(contentView, p);
Julia Reynoldsfc640012018-02-21 12:25:27 -05005019 mN.mUsesStandardHeader = true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005020 }
5021
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005022 private void bindActivePermissions(RemoteViews contentView, StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005023 int color = getNeutralColor(p);
Julia Reynoldsb887b232018-04-10 16:38:08 -04005024 contentView.setDrawableTint(R.id.camera, false, color, PorterDuff.Mode.SRC_ATOP);
5025 contentView.setDrawableTint(R.id.mic, false, color, PorterDuff.Mode.SRC_ATOP);
5026 contentView.setDrawableTint(R.id.overlay, false, color, PorterDuff.Mode.SRC_ATOP);
5027 }
5028
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005029 private void bindExpandButton(RemoteViews contentView, StandardTemplateParams p) {
5030 int color = isColorized(p) ? getPrimaryTextColor(p) : getSecondaryTextColor(p);
Sunny Goyal5b153922017-09-21 21:00:36 -07005031 contentView.setDrawableTint(R.id.expand_button, false, color,
5032 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005033 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08005034 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005035 }
5036
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005037 private void bindHeaderChronometerAndTime(RemoteViews contentView,
5038 StandardTemplateParams p) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005039 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08005040 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005041 setTextViewColorSecondary(contentView, R.id.time_divider, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005042 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
5043 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
5044 contentView.setLong(R.id.chronometer, "setBase",
5045 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
5046 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07005047 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07005048 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005049 setTextViewColorSecondary(contentView, R.id.chronometer, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005050 } else {
5051 contentView.setViewVisibility(R.id.time, View.VISIBLE);
5052 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005053 setTextViewColorSecondary(contentView, R.id.time, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005054 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005055 } else {
5056 // We still want a time to be set but gone, such that we can show and hide it
5057 // on demand in case it's a child notification without anything in the header
5058 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005059 }
5060 }
5061
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005062 private void bindHeaderText(RemoteViews contentView, StandardTemplateParams p) {
5063 CharSequence summaryText = p.summaryText;
5064 if (summaryText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05005065 && mStyle.hasSummaryInHeader()) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005066 summaryText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05005067 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005068 if (summaryText == null
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07005069 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
5070 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005071 summaryText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07005072 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005073 if (summaryText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005074 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek48f66b72017-08-18 16:17:51 -07005075 contentView.setTextViewText(R.id.header_text, processTextSpans(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005076 processLegacyText(summaryText)));
5077 setTextViewColorSecondary(contentView, R.id.header_text, p);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07005078 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
5079 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005080 setTextViewColorSecondary(contentView, R.id.header_text_divider, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005081 }
5082 }
5083
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005084 private void bindHeaderTextSecondary(RemoteViews contentView, StandardTemplateParams p) {
5085 if (!TextUtils.isEmpty(p.headerTextSecondary)) {
Selim Cinekafeed292017-12-12 17:32:44 -08005086 contentView.setTextViewText(R.id.header_text_secondary, processTextSpans(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005087 processLegacyText(p.headerTextSecondary)));
5088 setTextViewColorSecondary(contentView, R.id.header_text_secondary, p);
Selim Cinekafeed292017-12-12 17:32:44 -08005089 contentView.setViewVisibility(R.id.header_text_secondary, View.VISIBLE);
5090 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005091 setTextViewColorSecondary(contentView, R.id.header_text_secondary_divider, p);
Selim Cinekafeed292017-12-12 17:32:44 -08005092 }
5093 }
5094
Adrian Rooseba05822016-04-22 17:09:27 -07005095 /**
5096 * @hide
5097 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005098 @UnsupportedAppUsage
Adrian Rooseba05822016-04-22 17:09:27 -07005099 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04005100 CharSequence name = null;
5101 final PackageManager pm = mContext.getPackageManager();
5102 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
5103 // only system packages which lump together a bunch of unrelated stuff
5104 // may substitute a different name to make the purpose of the
5105 // notification more clear. the correct package label should always
5106 // be accessible via SystemUI.
5107 final String pkg = mContext.getPackageName();
5108 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
5109 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
5110 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
5111 name = subName;
5112 } else {
5113 Log.w(TAG, "warning: pkg "
5114 + pkg + " attempting to substitute app name '" + subName
5115 + "' without holding perm "
5116 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
5117 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005118 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04005119 if (TextUtils.isEmpty(name)) {
5120 name = pm.getApplicationLabel(mContext.getApplicationInfo());
5121 }
5122 if (TextUtils.isEmpty(name)) {
5123 // still nothing?
5124 return null;
5125 }
5126
5127 return String.valueOf(name);
5128 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005129 private void bindHeaderAppName(RemoteViews contentView, StandardTemplateParams p) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04005130 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005131 if (isColorized(p)) {
5132 setTextViewColorPrimary(contentView, R.id.app_name_text, p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005133 } else {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005134 contentView.setTextColor(R.id.app_name_text, getSecondaryTextColor(p));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005135 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005136 }
5137
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005138 private boolean isColorized(StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005139 return p.allowColorization && mN.isColorized();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005140 }
5141
5142 private void bindSmallIcon(RemoteViews contentView, StandardTemplateParams p) {
Selim Cinek279fa862016-06-14 10:57:25 -07005143 if (mN.mSmallIcon == null && mN.icon != 0) {
5144 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
5145 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005146 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Sunny Goyal5b153922017-09-21 21:00:36 -07005147 contentView.setInt(R.id.icon, "setImageLevel", mN.iconLevel);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005148 processSmallIconColor(mN.mSmallIcon, contentView, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005149 }
5150
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005151 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005152 * @return true if the built notification will show the time or the chronometer; false
5153 * otherwise
5154 */
5155 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07005156 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005157 }
5158
Christoph Studerfe718432014-09-01 18:21:18 +02005159 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07005160 // actions_container is only reset when there are no actions to avoid focus issues with
5161 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02005162 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02005163 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08005164
5165 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
5166 big.setTextViewText(R.id.notification_material_reply_text_1, null);
Kenny Guya0f6de82018-04-06 16:20:16 +01005167 big.setViewVisibility(R.id.notification_material_reply_text_1_container, View.GONE);
5168 big.setViewVisibility(R.id.notification_material_reply_progress, View.GONE);
Adrian Roose458aa82015-12-08 16:17:19 -08005169
5170 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
5171 big.setTextViewText(R.id.notification_material_reply_text_2, null);
5172 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
5173 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07005174
Selim Cineked64a142018-02-06 18:06:01 -08005175 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
5176 R.dimen.notification_content_margin);
Christoph Studerfe718432014-09-01 18:21:18 +02005177 }
5178
Selim Cinek384804b2018-04-18 14:31:07 +08005179 private RemoteViews applyStandardTemplateWithActions(int layoutId,
5180 TemplateBindResult result) {
5181 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this),
5182 result);
Adrian Roos48d746a2016-04-12 14:57:28 -07005183 }
5184
Gustav Sennton1463d832018-11-06 16:12:48 +00005185 private static List<Notification.Action> filterOutContextualActions(
5186 List<Notification.Action> actions) {
5187 List<Notification.Action> nonContextualActions = new ArrayList<>();
5188 for (Notification.Action action : actions) {
Gustav Sennton005d7a02019-01-04 13:41:32 +00005189 if (!action.isContextual()) {
Gustav Sennton1463d832018-11-06 16:12:48 +00005190 nonContextualActions.add(action);
5191 }
5192 }
5193 return nonContextualActions;
5194 }
5195
Adrian Roos70d7aa32017-01-11 15:39:06 -08005196 private RemoteViews applyStandardTemplateWithActions(int layoutId,
Selim Cinek384804b2018-04-18 14:31:07 +08005197 StandardTemplateParams p, TemplateBindResult result) {
5198 RemoteViews big = applyStandardTemplate(layoutId, p, result);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005199
Christoph Studerfe718432014-09-01 18:21:18 +02005200 resetStandardTemplateWithActions(big);
5201
Adrian Roose458aa82015-12-08 16:17:19 -08005202 boolean validRemoteInput = false;
5203
Gustav Sennton1463d832018-11-06 16:12:48 +00005204 // In the UI contextual actions appear separately from the standard actions, so we
5205 // filter them out here.
5206 List<Notification.Action> nonContextualActions = filterOutContextualActions(mActions);
5207
5208 int N = nonContextualActions.size();
Lucas Dupin00be88f2019-01-03 17:50:52 -08005209 boolean emphazisedMode = mN.fullScreenIntent != null;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005210 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005211 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08005212 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005213 big.setViewVisibility(R.id.actions, View.VISIBLE);
Selim Cineked64a142018-02-06 18:06:01 -08005214 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Daniel Sandler8680bf82012-05-15 16:52:52 -04005215 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005216 for (int i=0; i<N; i++) {
Gustav Sennton1463d832018-11-06 16:12:48 +00005217 Action action = nonContextualActions.get(i);
5218
Selim Cinekffcc7cf2018-02-06 17:43:51 -08005219 boolean actionHasValidInput = hasValidRemoteInput(action);
5220 validRemoteInput |= actionHasValidInput;
Adrian Roose458aa82015-12-08 16:17:19 -08005221
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005222 final RemoteViews button = generateActionButton(action, emphazisedMode, p);
Selim Cinek396caca2018-04-10 17:46:46 -07005223 if (actionHasValidInput && !emphazisedMode) {
Selim Cinekffcc7cf2018-02-06 17:43:51 -08005224 // Clear the drawable
5225 button.setInt(R.id.action0, "setBackgroundResource", 0);
5226 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005227 big.addView(R.id.actions, button);
5228 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07005229 } else {
5230 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005231 }
Adrian Roose458aa82015-12-08 16:17:19 -08005232
5233 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Lucas Dupin00be88f2019-01-03 17:50:52 -08005234 if (validRemoteInput && replyText != null
Selim Cinekbee4e072018-05-21 22:06:43 -07005235 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])
5236 && p.maxRemoteInputHistory > 0) {
Kenny Guya0f6de82018-04-06 16:20:16 +01005237 boolean showSpinner = mN.extras.getBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER);
Adrian Roose458aa82015-12-08 16:17:19 -08005238 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Kenny Guya0f6de82018-04-06 16:20:16 +01005239 big.setViewVisibility(R.id.notification_material_reply_text_1_container,
5240 View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005241 big.setTextViewText(R.id.notification_material_reply_text_1,
5242 processTextSpans(replyText[0]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005243 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1, p);
Kenny Guya0f6de82018-04-06 16:20:16 +01005244 big.setViewVisibility(R.id.notification_material_reply_progress,
5245 showSpinner ? View.VISIBLE : View.GONE);
5246 big.setProgressIndeterminateTintList(
5247 R.id.notification_material_reply_progress,
5248 ColorStateList.valueOf(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005249 isColorized(p) ? getPrimaryTextColor(p) : resolveContrastColor(p)));
Adrian Roose458aa82015-12-08 16:17:19 -08005250
Selim Cinekbee4e072018-05-21 22:06:43 -07005251 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])
5252 && p.maxRemoteInputHistory > 1) {
Adrian Roose458aa82015-12-08 16:17:19 -08005253 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005254 big.setTextViewText(R.id.notification_material_reply_text_2,
5255 processTextSpans(replyText[1]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005256 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2, p);
Adrian Roose458aa82015-12-08 16:17:19 -08005257
Selim Cinekbee4e072018-05-21 22:06:43 -07005258 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])
5259 && p.maxRemoteInputHistory > 2) {
Adrian Roose458aa82015-12-08 16:17:19 -08005260 big.setViewVisibility(
5261 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005262 big.setTextViewText(R.id.notification_material_reply_text_3,
5263 processTextSpans(replyText[2]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005264 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3, p);
Adrian Roose458aa82015-12-08 16:17:19 -08005265 }
5266 }
5267 }
5268
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005269 return big;
5270 }
5271
Adrian Roose458aa82015-12-08 16:17:19 -08005272 private boolean hasValidRemoteInput(Action action) {
5273 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
5274 // Weird actions
5275 return false;
5276 }
5277
5278 RemoteInput[] remoteInputs = action.getRemoteInputs();
5279 if (remoteInputs == null) {
5280 return false;
5281 }
5282
5283 for (RemoteInput r : remoteInputs) {
5284 CharSequence[] choices = r.getChoices();
5285 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
5286 return true;
5287 }
5288 }
5289 return false;
5290 }
5291
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005292 /**
5293 * Construct a RemoteViews for the final 1U notification layout. In order:
5294 * 1. Custom contentView from the caller
5295 * 2. Style's proposed content view
5296 * 3. Standard template view
5297 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005298 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005299 return createContentView(false /* increasedheight */ );
5300 }
5301
5302 /**
5303 * Construct a RemoteViews for the smaller content view.
5304 *
5305 * @param increasedHeight true if this layout be created with an increased height. Some
5306 * styles may support showing more then just that basic 1U size
5307 * and the system may decide to render important notifications
5308 * slightly bigger even when collapsed.
5309 *
5310 * @hide
5311 */
5312 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005313 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005314 return mN.contentView;
5315 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005316 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005317 if (styleView != null) {
5318 return styleView;
5319 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005320 }
Selim Cinek384804b2018-04-18 14:31:07 +08005321 return applyStandardTemplate(getBaseLayoutResource(), null /* result */);
Joe Onorato46439ce2010-11-19 13:56:21 -08005322 }
5323
Selim Cineka7679b62017-05-10 16:33:25 -07005324 private boolean useExistingRemoteView() {
5325 return mStyle == null || (!mStyle.displayCustomViewInline()
5326 && !mRebuildStyledRemoteViews);
5327 }
5328
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005329 /**
5330 * Construct a RemoteViews for the final big notification layout.
5331 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005332 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05005333 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07005334 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005335 return mN.bigContentView;
5336 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05005337 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08005338 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005339 } else if (mActions.size() != 0) {
Selim Cinek384804b2018-04-18 14:31:07 +08005340 result = applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5341 null /* result */);
Selim Cinek850a8542015-11-11 11:48:36 -05005342 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08005343 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05005344 return result;
5345 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005346
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005347 /**
Selim Cinek414ad332017-02-24 19:06:12 -08005348 * Construct a RemoteViews for the final notification header only. This will not be
5349 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005350 *
5351 * @hide
5352 */
Lucas Dupin00be88f2019-01-03 17:50:52 -08005353 public RemoteViews makeNotificationHeader() {
5354 return makeNotificationHeader(mParams.reset().fillTextsFrom(this));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005355 }
5356
5357 /**
5358 * Construct a RemoteViews for the final notification header only. This will not be
5359 * colorized.
5360 *
5361 * @param p the template params to inflate this with
5362 */
5363 private RemoteViews makeNotificationHeader(StandardTemplateParams p) {
5364 // Headers on their own are never colorized
5365 p.disallowColorization();
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005366 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Lucas Dupin00be88f2019-01-03 17:50:52 -08005367 R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005368 resetNotificationHeader(header);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005369 bindNotificationHeader(header, p);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005370 return header;
5371 }
5372
Adrian Roos487374f2017-01-11 15:48:14 -08005373 /**
5374 * Construct a RemoteViews for the ambient version of the notification.
5375 *
5376 * @hide
5377 */
5378 public RemoteViews makeAmbientNotification() {
Selim Cinek0e069942019-01-24 16:09:41 -08005379 RemoteViews headsUpContentView = createHeadsUpContentView(false /* increasedHeight */);
5380 if (headsUpContentView != null) {
5381 return headsUpContentView;
5382 }
5383 return createContentView();
Adrian Roos487374f2017-01-11 15:48:14 -08005384 }
5385
Selim Cinek29603462015-11-17 19:04:39 -08005386 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005387 if (result != null) {
5388 result.setViewVisibility(R.id.text_line_1, View.GONE);
5389 }
Selim Cinek29603462015-11-17 19:04:39 -08005390 }
5391
Selim Cinek6743c0b2017-01-18 18:24:01 -08005392 /**
5393 * Adapt the Notification header if this view is used as an expanded view.
5394 *
5395 * @hide
5396 */
5397 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005398 if (result != null) {
5399 result.setBoolean(R.id.notification_header, "setExpanded", true);
5400 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005401 }
5402
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005403 /**
5404 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005405 *
5406 * @param increasedHeight true if this layout be created with an increased height. Some
5407 * styles may support showing more then just that basic 1U size
5408 * and the system may decide to render important notifications
5409 * slightly bigger even when collapsed.
5410 *
5411 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005412 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005413 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005414 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005415 return mN.headsUpContentView;
5416 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08005417 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
5418 if (styleView != null) {
5419 return styleView;
5420 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05005421 } else if (mActions.size() == 0) {
5422 return null;
5423 }
5424
Selim Cinekbee4e072018-05-21 22:06:43 -07005425 // We only want at most a single remote input history to be shown here, otherwise
5426 // the content would become squished.
5427 StandardTemplateParams p = mParams.reset().fillTextsFrom(this)
5428 .setMaxRemoteInputHistory(1);
5429 return applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5430 p,
5431 null /* result */);
Chris Wren8fd39ec2014-02-27 17:43:26 -05005432 }
5433
Selim Cinek624c02db2015-12-14 21:00:02 -08005434 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08005435 * Construct a RemoteViews for the final heads-up notification layout.
5436 */
5437 public RemoteViews createHeadsUpContentView() {
5438 return createHeadsUpContentView(false /* useIncreasedHeight */);
5439 }
5440
5441 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08005442 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5443 *
5444 * @hide
5445 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005446 @UnsupportedAppUsage
Selim Cinek624c02db2015-12-14 21:00:02 -08005447 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005448 return makePublicView(false /* ambient */);
5449 }
5450
5451 /**
5452 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5453 *
5454 * @hide
5455 */
5456 public RemoteViews makePublicAmbientNotification() {
5457 return makePublicView(true /* ambient */);
5458 }
5459
5460 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08005461 if (mN.publicVersion != null) {
5462 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005463 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08005464 }
5465 Bundle savedBundle = mN.extras;
5466 Style style = mStyle;
5467 mStyle = null;
5468 Icon largeIcon = mN.mLargeIcon;
5469 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07005470 Bitmap largeIconLegacy = mN.largeIcon;
5471 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005472 ArrayList<Action> actions = mActions;
5473 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08005474 Bundle publicExtras = new Bundle();
5475 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
5476 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
5477 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
5478 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07005479 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
5480 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cineked07b962018-04-30 14:39:35 -07005481 String appName = savedBundle.getString(EXTRA_SUBSTITUTE_APP_NAME);
5482 if (appName != null) {
5483 publicExtras.putString(EXTRA_SUBSTITUTE_APP_NAME, appName);
5484 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005485 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07005486 RemoteViews view;
Lucas Dupin00be88f2019-01-03 17:50:52 -08005487 view = makeNotificationHeader();
5488 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
Selim Cinek624c02db2015-12-14 21:00:02 -08005489 mN.extras = savedBundle;
5490 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07005491 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005492 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08005493 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005494 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08005495 }
5496
Selim Cinek6743c0b2017-01-18 18:24:01 -08005497 /**
5498 * Construct a content view for the display when low - priority
5499 *
5500 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
5501 * a new subtext is created consisting of the content of the
5502 * notification.
5503 * @hide
5504 */
5505 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005506 StandardTemplateParams p = mParams.reset()
5507 .forceDefaultColor()
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005508 .fillTextsFrom(this);
5509 if (!useRegularSubtext || TextUtils.isEmpty(mParams.summaryText)) {
5510 p.summaryText(createSummaryText());
Selim Cinek6743c0b2017-01-18 18:24:01 -08005511 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005512 RemoteViews header = makeNotificationHeader(p);
Selim Cinek1b554392017-02-28 17:22:49 -08005513 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08005514 return header;
5515 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005516
Selim Cinek6743c0b2017-01-18 18:24:01 -08005517 private CharSequence createSummaryText() {
5518 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
5519 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
5520 return titleText;
5521 }
5522 SpannableStringBuilder summary = new SpannableStringBuilder();
5523 if (titleText == null) {
5524 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
5525 }
5526 BidiFormatter bidi = BidiFormatter.getInstance();
5527 if (titleText != null) {
5528 summary.append(bidi.unicodeWrap(titleText));
5529 }
5530 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
5531 if (titleText != null && contentText != null) {
5532 summary.append(bidi.unicodeWrap(mContext.getText(
5533 R.string.notification_header_divider_symbol_with_spaces)));
5534 }
5535 if (contentText != null) {
5536 summary.append(bidi.unicodeWrap(contentText));
5537 }
5538 return summary;
5539 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05005540
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005541 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005542 StandardTemplateParams p) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04005543 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07005544 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005545 emphazisedMode ? getEmphasizedActionLayoutResource()
5546 : tombstone ? getActionTombstoneLayoutResource()
5547 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04005548 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04005549 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04005550 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005551 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005552 if (action.mRemoteInputs != null) {
5553 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
5554 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005555 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07005556 // change the background bgColor
Selim Cinek981962e2016-07-20 20:41:58 -07005557 CharSequence title = action.title;
5558 ColorStateList[] outResultColor = null;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005559 int background = resolveBackgroundColor(p);
Selim Cinek981962e2016-07-20 20:41:58 -07005560 if (isLegacy()) {
Lucas Dupina291d192018-06-07 13:59:42 -07005561 title = ContrastColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07005562 } else {
5563 outResultColor = new ColorStateList[1];
Selim Cinek396caca2018-04-10 17:46:46 -07005564 title = ensureColorSpanContrast(title, background, outResultColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005565 }
Selim Cinek48f66b72017-08-18 16:17:51 -07005566 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005567 setTextViewColorPrimary(button, R.id.action0, p);
Selim Cinek396caca2018-04-10 17:46:46 -07005568 int rippleColor;
5569 boolean hasColorOverride = outResultColor != null && outResultColor[0] != null;
5570 if (hasColorOverride) {
5571 // There's a span spanning the full text, let's take it and use it as the
5572 // background color
5573 background = outResultColor[0].getDefaultColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005574 int textColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07005575 background, mInNightMode);
Selim Cinek396caca2018-04-10 17:46:46 -07005576 button.setTextColor(R.id.action0, textColor);
5577 rippleColor = textColor;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005578 } else if (getRawColor(p) != COLOR_DEFAULT && !isColorized(p)
Lucas Dupinf15c5e52019-04-01 19:00:12 -07005579 && mTintActionButtons && !mInNightMode) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005580 rippleColor = resolveContrastColor(p);
Selim Cinek396caca2018-04-10 17:46:46 -07005581 button.setTextColor(R.id.action0, rippleColor);
5582 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005583 rippleColor = getPrimaryTextColor(p);
Selim Cinek981962e2016-07-20 20:41:58 -07005584 }
Selim Cinek396caca2018-04-10 17:46:46 -07005585 // We only want about 20% alpha for the ripple
5586 rippleColor = (rippleColor & 0x00ffffff) | 0x33000000;
5587 button.setColorStateList(R.id.action0, "setRippleColor",
5588 ColorStateList.valueOf(rippleColor));
5589 button.setColorStateList(R.id.action0, "setButtonBackground",
5590 ColorStateList.valueOf(background));
5591 button.setBoolean(R.id.action0, "setHasStroke", !hasColorOverride);
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005592 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07005593 button.setTextViewText(R.id.action0, processTextSpans(
5594 processLegacyText(action.title)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005595 if (isColorized(p)) {
5596 setTextViewColorPrimary(button, R.id.action0, p);
5597 } else if (getRawColor(p) != COLOR_DEFAULT && mTintActionButtons) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005598 button.setTextColor(R.id.action0, resolveContrastColor(p));
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005599 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005600 }
Tony Mak7d4b3a52018-11-27 17:29:36 +00005601 button.setIntTag(R.id.action0, R.id.notification_action_index_tag,
5602 mActions.indexOf(action));
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005603 return button;
5604 }
5605
Joe Onoratocb109a02011-01-18 17:57:41 -08005606 /**
Selim Cinek981962e2016-07-20 20:41:58 -07005607 * Ensures contrast on color spans against a background color. also returns the color of the
5608 * text if a span was found that spans over the whole text.
5609 *
5610 * @param charSequence the charSequence on which the spans are
5611 * @param background the background color to ensure the contrast against
5612 * @param outResultColor an array in which a color will be returned as the first element if
5613 * there exists a full length color span.
5614 * @return the contrasted charSequence
5615 */
5616 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
5617 ColorStateList[] outResultColor) {
5618 if (charSequence instanceof Spanned) {
5619 Spanned ss = (Spanned) charSequence;
5620 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
5621 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
5622 for (Object span : spans) {
5623 Object resultSpan = span;
5624 int spanStart = ss.getSpanStart(span);
5625 int spanEnd = ss.getSpanEnd(span);
5626 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
5627 if (resultSpan instanceof CharacterStyle) {
5628 resultSpan = ((CharacterStyle) span).getUnderlying();
5629 }
5630 if (resultSpan instanceof TextAppearanceSpan) {
5631 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
5632 ColorStateList textColor = originalSpan.getTextColor();
5633 if (textColor != null) {
5634 int[] colors = textColor.getColors();
5635 int[] newColors = new int[colors.length];
5636 for (int i = 0; i < newColors.length; i++) {
Lucas Dupina291d192018-06-07 13:59:42 -07005637 newColors[i] = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005638 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005639 }
5640 textColor = new ColorStateList(textColor.getStates().clone(),
5641 newColors);
Selim Cinek396caca2018-04-10 17:46:46 -07005642 if (fullLength) {
5643 outResultColor[0] = textColor;
5644 // Let's drop the color from the span
5645 textColor = null;
5646 }
Selim Cinek981962e2016-07-20 20:41:58 -07005647 resultSpan = new TextAppearanceSpan(
5648 originalSpan.getFamily(),
5649 originalSpan.getTextStyle(),
5650 originalSpan.getTextSize(),
5651 textColor,
5652 originalSpan.getLinkTextColor());
Selim Cinek981962e2016-07-20 20:41:58 -07005653 }
5654 } else if (resultSpan instanceof ForegroundColorSpan) {
5655 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
5656 int foregroundColor = originalSpan.getForegroundColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005657 foregroundColor = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005658 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005659 if (fullLength) {
5660 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
Selim Cinek396caca2018-04-10 17:46:46 -07005661 resultSpan = null;
5662 } else {
5663 resultSpan = new ForegroundColorSpan(foregroundColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005664 }
5665 } else {
5666 resultSpan = span;
5667 }
Selim Cinek396caca2018-04-10 17:46:46 -07005668 if (resultSpan != null) {
5669 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
5670 }
Selim Cinek981962e2016-07-20 20:41:58 -07005671 }
5672 return builder;
5673 }
5674 return charSequence;
5675 }
5676
5677 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005678 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07005679 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005680 */
5681 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005682 if (!mIsLegacyInitialized) {
5683 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
5684 < Build.VERSION_CODES.LOLLIPOP;
5685 mIsLegacyInitialized = true;
5686 }
5687 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005688 }
5689
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005690 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08005691 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
Lucas Dupin00be88f2019-01-03 17:50:52 -08005692 if (isAlreadyLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005693 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005694 } else {
5695 return charSequence;
5696 }
5697 }
5698
Dan Sandler26e81cf2014-05-06 10:01:27 -04005699 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005700 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04005701 */
Adrian Roos487374f2017-01-11 15:48:14 -08005702 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005703 StandardTemplateParams p) {
Selim Cinekea4bef72015-12-02 15:51:10 -08005704 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005705 int color;
Lucas Dupin00be88f2019-01-03 17:50:52 -08005706 if (isColorized(p)) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005707 color = getPrimaryTextColor(p);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005708 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005709 color = resolveContrastColor(p);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005710 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005711 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07005712 contentView.setDrawableTint(R.id.icon, false, color,
5713 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005714
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005715 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005716 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08005717 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005718 }
5719
Dan Sandler26e81cf2014-05-06 10:01:27 -04005720 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005721 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04005722 * if it's grayscale).
5723 */
5724 // TODO: also check bounds, transparency, that sort of thing.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005725 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView,
5726 StandardTemplateParams p) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04005727 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005728 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005729 // resolve color will fall back to the default when legacy
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005730 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(p),
Sunny Goyal5b153922017-09-21 21:00:36 -07005731 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01005732 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005733 }
5734
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05005735 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005736 if (mN.color != COLOR_DEFAULT) {
5737 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02005738 }
Jorim Jaggi74419312014-06-10 20:57:21 +02005739 }
5740
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005741 int resolveContrastColor(StandardTemplateParams p) {
5742 int rawColor = getRawColor(p);
5743 if (mCachedContrastColorIsFor == rawColor && mCachedContrastColor != COLOR_INVALID) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08005744 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005745 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005746
Selim Cinekac5f0272017-05-02 16:05:41 -07005747 int color;
Selim Cinekc7f5a822018-03-20 19:32:06 -07005748 int background = mContext.getColor(
5749 com.android.internal.R.color.notification_material_background_color);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005750 if (rawColor == COLOR_DEFAULT) {
5751 ensureColors(p);
Lucas Dupinf03e7522018-06-25 16:21:13 -07005752 color = ContrastColorUtil.resolveDefaultColor(mContext, background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005753 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005754 color = ContrastColorUtil.resolveContrastColor(mContext, rawColor,
Anthony Chenad4d1582017-04-10 16:07:58 -07005755 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005756 }
5757 if (Color.alpha(color) < 255) {
5758 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005759 color = ContrastColorUtil.compositeColors(color, background);
Selim Cinekac5f0272017-05-02 16:05:41 -07005760 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005761 mCachedContrastColorIsFor = rawColor;
Selim Cinekac5f0272017-05-02 16:05:41 -07005762 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005763 }
5764
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005765 /**
5766 * Return the raw color of this Notification, which doesn't necessarily satisfy contrast.
5767 *
5768 * @see #resolveContrastColor(StandardTemplateParams) for the contrasted color
5769 * @param p the template params to inflate this with
5770 */
5771 private int getRawColor(StandardTemplateParams p) {
5772 if (p.forceDefaultColor) {
5773 return COLOR_DEFAULT;
5774 }
5775 return mN.color;
5776 }
5777
Selim Cinek4717d862018-04-19 09:19:15 +08005778 int resolveNeutralColor() {
5779 if (mNeutralColor != COLOR_INVALID) {
5780 return mNeutralColor;
5781 }
5782 int background = mContext.getColor(
5783 com.android.internal.R.color.notification_material_background_color);
Lucas Dupinf03e7522018-06-25 16:21:13 -07005784 mNeutralColor = ContrastColorUtil.resolveDefaultColor(mContext, background,
5785 mInNightMode);
Selim Cinek4717d862018-04-19 09:19:15 +08005786 if (Color.alpha(mNeutralColor) < 255) {
5787 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005788 mNeutralColor = ContrastColorUtil.compositeColors(mNeutralColor, background);
Selim Cinek4717d862018-04-19 09:19:15 +08005789 }
5790 return mNeutralColor;
5791 }
5792
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005793 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005794 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005795 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08005796 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005797 @NonNull
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005798 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005799 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005800 mN.actions = new Action[mActions.size()];
5801 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005802 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005803 if (!mPersonList.isEmpty()) {
Selim Cineke7238dd2017-12-14 17:48:32 -08005804 mN.extras.putParcelableArrayList(EXTRA_PEOPLE_LIST, mPersonList);
Dan Sandler0bf2ed82013-12-21 23:33:41 -06005805 }
Selim Cinek247fa012016-02-18 09:50:48 -08005806 if (mN.bigContentView != null || mN.contentView != null
5807 || mN.headsUpContentView != null) {
5808 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
5809 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005810 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08005811 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005812
Julia Reynolds3b848122016-02-26 10:45:32 -05005813 /**
5814 * Creates a Builder from an existing notification so further changes can be made.
5815 * @param context The context for your application / activity.
5816 * @param n The notification to create a Builder from.
5817 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005818 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005819 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005820 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005821 ApplicationInfo applicationInfo = n.extras.getParcelable(
5822 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005823 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05005824 if (applicationInfo != null) {
5825 try {
5826 builderContext = context.createApplicationContext(applicationInfo,
5827 Context.CONTEXT_RESTRICTED);
5828 } catch (NameNotFoundException e) {
5829 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
5830 builderContext = context; // try with our context
5831 }
5832 } else {
5833 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02005834 }
5835
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005836 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005837 }
5838
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005839 /**
Gustav Sennton761884c2018-11-19 17:40:19 +00005840 * Determines whether the platform can generate contextual actions for a notification.
5841 * By default this is true.
5842 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005843 @NonNull
Gustav Sennton761884c2018-11-19 17:40:19 +00005844 public Builder setAllowSystemGeneratedContextualActions(boolean allowed) {
5845 mN.mAllowSystemGeneratedContextualActions = allowed;
5846 return this;
5847 }
5848
5849 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005850 * @deprecated Use {@link #build()} instead.
5851 */
5852 @Deprecated
5853 public Notification getNotification() {
5854 return build();
5855 }
5856
5857 /**
5858 * Combine all of the options that have been set and return a new {@link Notification}
5859 * object.
5860 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005861 @NonNull
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005862 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005863 // first, add any extras from the calling code
5864 if (mUserExtras != null) {
5865 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005866 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005867
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005868 mN.creationTime = System.currentTimeMillis();
5869
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005870 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05005871 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02005872
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005873 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005874
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005875 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02005876 mStyle.reduceImageSizes(mContext);
5877 mStyle.purgeResources();
Selim Cinek90343862018-02-01 11:07:11 -08005878 mStyle.validate(mContext);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005879 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005880 }
Selim Cinekd0426622017-07-11 13:19:59 +02005881 mN.reduceImageSizes(mContext);
5882
Adrian Roos5081c0d2016-02-26 16:04:19 -08005883 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07005884 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005885 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005886 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005887 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
5888 mN.contentView.getSequenceNumber());
5889 }
5890 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005891 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005892 if (mN.bigContentView != null) {
5893 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
5894 mN.bigContentView.getSequenceNumber());
5895 }
5896 }
5897 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005898 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005899 if (mN.headsUpContentView != null) {
5900 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
5901 mN.headsUpContentView.getSequenceNumber());
5902 }
5903 }
5904 }
5905
Julia Reynolds4c0c2022016-02-02 15:11:59 -05005906 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
5907 mN.flags |= FLAG_SHOW_LIGHTS;
5908 }
5909
Adrian Roosfb921842017-10-26 14:49:56 +02005910 mN.allPendingIntents = null;
5911
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005912 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005913 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005914
5915 /**
5916 * Apply this Builder to an existing {@link Notification} object.
5917 *
5918 * @hide
5919 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005920 @NonNull
5921 public Notification buildInto(@NonNull Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005922 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005923 return n;
5924 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005925
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005926 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005927 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005928 * change. Also removes extenders on low ram devices, as
5929 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005930 *
5931 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5932 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005933 * @hide
5934 */
Kristian Monsen05f34792018-04-09 10:27:16 +02005935 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam,
5936 Context context) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005937 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005938
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005939 // Only strip views for known Styles because we won't know how to
5940 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005941 if (!isLowRam
5942 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005943 && getNotificationStyleClass(templateClass) == null) {
5944 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005945 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005946
5947 // Only strip unmodified BuilderRemoteViews.
5948 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005949 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005950 n.contentView.getSequenceNumber();
5951 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005952 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005953 n.bigContentView.getSequenceNumber();
5954 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005955 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005956 n.headsUpContentView.getSequenceNumber();
5957
5958 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005959 if (!isLowRam
5960 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005961 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005962 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005963
5964 Notification clone = n.clone();
5965 if (stripContentView) {
5966 clone.contentView = null;
5967 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5968 }
5969 if (stripBigContentView) {
5970 clone.bigContentView = null;
5971 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5972 }
5973 if (stripHeadsUpContentView) {
5974 clone.headsUpContentView = null;
5975 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5976 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005977 if (isLowRam) {
Kristian Monsen05f34792018-04-09 10:27:16 +02005978 String[] allowedServices = context.getResources().getStringArray(
5979 R.array.config_allowedManagedServicesOnLowRamDevices);
5980 if (allowedServices.length == 0) {
5981 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5982 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5983 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5984 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005985 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005986 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005987 }
5988
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005989 @UnsupportedAppUsage
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005990 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005991 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005992 }
5993
5994 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005995 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005996 }
5997
5998 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005999 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006000 }
6001
6002 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02006003 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006004 }
6005
6006 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07006007 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006008 }
6009
Adrian Roosc1a80b02016-04-05 14:54:55 -07006010 private int getMessagingLayoutResource() {
6011 return R.layout.notification_template_material_messaging;
6012 }
6013
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006014 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07006015 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006016 }
6017
Selim Cinek06e9e1f2016-07-08 17:14:16 -07006018 private int getEmphasizedActionLayoutResource() {
6019 return R.layout.notification_material_action_emphasized;
6020 }
6021
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006022 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07006023 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006024 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08006025
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006026 private int getBackgroundColor(StandardTemplateParams p) {
6027 if (isColorized(p)) {
6028 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : getRawColor(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08006029 } else {
Selim Cinekc7f5a822018-03-20 19:32:06 -07006030 return COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08006031 }
6032 }
6033
Selim Cinek396caca2018-04-10 17:46:46 -07006034 /**
Selim Cinek4717d862018-04-19 09:19:15 +08006035 * Gets a neutral color that can be used for icons or similar that should not stand out.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006036 * @param p the template params to inflate this with
Selim Cinek4717d862018-04-19 09:19:15 +08006037 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006038 private int getNeutralColor(StandardTemplateParams p) {
6039 if (isColorized(p)) {
6040 return getSecondaryTextColor(p);
Selim Cinek4717d862018-04-19 09:19:15 +08006041 } else {
6042 return resolveNeutralColor();
6043 }
6044 }
6045
6046 /**
Selim Cinek396caca2018-04-10 17:46:46 -07006047 * Same as getBackgroundColor but also resolved the default color to the background.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006048 * @param p the template params to inflate this with
Selim Cinek396caca2018-04-10 17:46:46 -07006049 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006050 private int resolveBackgroundColor(StandardTemplateParams p) {
6051 int backgroundColor = getBackgroundColor(p);
Selim Cinek396caca2018-04-10 17:46:46 -07006052 if (backgroundColor == COLOR_DEFAULT) {
6053 backgroundColor = mContext.getColor(
6054 com.android.internal.R.color.notification_material_background_color);
6055 }
6056 return backgroundColor;
6057 }
6058
Anthony Chenad4d1582017-04-10 16:07:58 -07006059 private boolean shouldTintActionButtons() {
6060 return mTintActionButtons;
6061 }
6062
Selim Cinek99104832017-01-25 14:47:33 -08006063 private boolean textColorsNeedInversion() {
6064 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
6065 return false;
6066 }
6067 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
6068 return targetSdkVersion > Build.VERSION_CODES.M
6069 && targetSdkVersion < Build.VERSION_CODES.O;
6070 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07006071
6072 /**
6073 * Set a color palette to be used as the background and textColors
6074 *
6075 * @param backgroundColor the color to be used as the background
6076 * @param foregroundColor the color to be used as the foreground
6077 *
6078 * @hide
6079 */
6080 public void setColorPalette(int backgroundColor, int foregroundColor) {
6081 mBackgroundColor = backgroundColor;
6082 mForegroundColor = foregroundColor;
6083 mTextColorsAreForBackground = COLOR_INVALID;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006084 ensureColors(mParams.reset().fillTextsFrom(this));
Selim Cinek5fb73f82017-04-20 16:55:38 -07006085 }
Selim Cinekac5f0272017-05-02 16:05:41 -07006086
6087 /**
Selim Cineka7679b62017-05-10 16:33:25 -07006088 * Forces all styled remoteViews to be built from scratch and not use any cached
6089 * RemoteViews.
6090 * This is needed for legacy apps that are baking in their remoteviews into the
6091 * notification.
6092 *
6093 * @hide
6094 */
6095 public void setRebuildStyledRemoteViews(boolean rebuild) {
6096 mRebuildStyledRemoteViews = rebuild;
6097 }
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006098
6099 /**
6100 * Get the text that should be displayed in the statusBar when heads upped. This is
6101 * usually just the app name, but may be different depending on the style.
6102 *
6103 * @param publicMode If true, return a text that is safe to display in public.
6104 *
6105 * @hide
6106 */
6107 public CharSequence getHeadsUpStatusBarText(boolean publicMode) {
6108 if (mStyle != null && !publicMode) {
6109 CharSequence text = mStyle.getHeadsUpStatusBarText();
6110 if (!TextUtils.isEmpty(text)) {
6111 return text;
6112 }
6113 }
6114 return loadHeaderAppName();
6115 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08006116 }
6117
6118 /**
Selim Cinekd0426622017-07-11 13:19:59 +02006119 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
6120 * remote views.
6121 *
6122 * @hide
6123 */
6124 void reduceImageSizes(Context context) {
6125 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
6126 return;
6127 }
Selim Cineka8cb1262017-08-15 16:53:44 -07006128 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006129 if (mLargeIcon != null || largeIcon != null) {
6130 Resources resources = context.getResources();
6131 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07006132 int maxWidth = resources.getDimensionPixelSize(isLowRam
6133 ? R.dimen.notification_right_icon_size_low_ram
6134 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006135 int maxHeight = maxWidth;
6136 if (MediaStyle.class.equals(style)
6137 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006138 maxHeight = resources.getDimensionPixelSize(isLowRam
6139 ? R.dimen.notification_media_image_max_height_low_ram
6140 : R.dimen.notification_media_image_max_height);
6141 maxWidth = resources.getDimensionPixelSize(isLowRam
6142 ? R.dimen.notification_media_image_max_width_low_ram
6143 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006144 }
6145 if (mLargeIcon != null) {
6146 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
6147 }
6148 if (largeIcon != null) {
6149 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
6150 }
6151 }
Selim Cineka8cb1262017-08-15 16:53:44 -07006152 reduceImageSizesForRemoteView(contentView, context, isLowRam);
6153 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
6154 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02006155 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
6156 }
6157
Selim Cineka8cb1262017-08-15 16:53:44 -07006158 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
6159 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02006160 if (remoteView != null) {
6161 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006162 int maxWidth = resources.getDimensionPixelSize(isLowRam
6163 ? R.dimen.notification_custom_view_max_image_width_low_ram
6164 : R.dimen.notification_custom_view_max_image_width);
6165 int maxHeight = resources.getDimensionPixelSize(isLowRam
6166 ? R.dimen.notification_custom_view_max_image_height_low_ram
6167 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02006168 remoteView.reduceImageSizes(maxWidth, maxHeight);
6169 }
6170 }
6171
6172 /**
Selim Cinek22714f12017-04-13 16:23:53 -07006173 * @return whether this notification is a foreground service notification
Gus Prevasb0c1a462018-11-05 11:06:15 -05006174 * @hide
Selim Cinek7b9605b2017-01-19 17:36:00 -08006175 */
Gus Prevasb0c1a462018-11-05 11:06:15 -05006176 public boolean isForegroundService() {
Selim Cinek22714f12017-04-13 16:23:53 -07006177 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08006178 }
6179
6180 /**
Selim Cinek99104832017-01-25 14:47:33 -08006181 * @return whether this notification has a media session attached
6182 * @hide
6183 */
6184 public boolean hasMediaSession() {
6185 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
6186 }
6187
6188 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006189 * @return the style class of this notification
6190 * @hide
6191 */
6192 public Class<? extends Notification.Style> getNotificationStyle() {
6193 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
6194
6195 if (!TextUtils.isEmpty(templateClass)) {
6196 return Notification.getNotificationStyleClass(templateClass);
6197 }
6198 return null;
6199 }
6200
6201 /**
Selim Cinek22714f12017-04-13 16:23:53 -07006202 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08006203 *
6204 * @hide
6205 */
6206 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07006207 if (isColorizedMedia()) {
6208 return true;
6209 }
Julia Reynolds4db59552017-06-30 13:34:01 -04006210 return extras.getBoolean(EXTRA_COLORIZED)
6211 && (hasColorizedPermission() || isForegroundService());
6212 }
6213
6214 /**
6215 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
6216 * permission. The permission is checked when a notification is enqueued.
6217 */
6218 private boolean hasColorizedPermission() {
6219 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07006220 }
6221
6222 /**
6223 * @return true if this notification is colorized and it is a media notification
6224 *
6225 * @hide
6226 */
6227 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08006228 Class<? extends Style> style = getNotificationStyle();
6229 if (MediaStyle.class.equals(style)) {
6230 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
6231 if ((colorized == null || colorized) && hasMediaSession()) {
6232 return true;
6233 }
6234 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6235 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
6236 return true;
6237 }
6238 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07006239 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006240 }
6241
Selim Cinek0847acd2017-04-24 19:48:29 -07006242
6243 /**
6244 * @return true if this is a media notification
6245 *
6246 * @hide
6247 */
6248 public boolean isMediaNotification() {
6249 Class<? extends Style> style = getNotificationStyle();
6250 if (MediaStyle.class.equals(style)) {
6251 return true;
6252 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6253 return true;
6254 }
6255 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006256 }
6257
Mady Mellor7eb18ef2019-03-27 14:03:46 -07006258 /**
Julia Tuttle28fb1a92019-05-03 18:07:45 -04006259 * @return true if this notification is showing as a bubble
Mady Mellor7eb18ef2019-03-27 14:03:46 -07006260 *
6261 * @hide
6262 */
6263 public boolean isBubbleNotification() {
6264 return (flags & Notification.FLAG_BUBBLE) != 0;
6265 }
6266
Selim Cinek279fa862016-06-14 10:57:25 -07006267 private boolean hasLargeIcon() {
6268 return mLargeIcon != null || largeIcon != null;
6269 }
6270
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006271 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006272 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006273 * @hide
6274 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07006275 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006276 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
6277 }
6278
6279 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006280 * @return true if the notification will show a chronometer; false otherwise
6281 * @hide
6282 */
6283 public boolean showsChronometer() {
6284 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
6285 }
6286
6287 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04006288 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05006289 */
6290 @SystemApi
6291 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
6292 Class<? extends Style>[] classes = new Class[] {
6293 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
6294 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
6295 MessagingStyle.class };
6296 for (Class<? extends Style> innerClass : classes) {
6297 if (templateClass.equals(innerClass.getName())) {
6298 return innerClass;
6299 }
6300 }
6301 return null;
6302 }
6303
6304 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006305 * An object that can apply a rich notification style to a {@link Notification.Builder}
6306 * object.
6307 */
Griff Hazendfcb0802014-02-11 12:00:00 -08006308 public static abstract class Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07006309
6310 /**
6311 * The number of items allowed simulatanously in the remote input history.
6312 * @hide
6313 */
6314 static final int MAX_REMOTE_INPUT_HISTORY_LINES = 3;
Chris Wrend6297db2012-05-03 16:20:13 -04006315 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006316
6317 /**
6318 * @hide
6319 */
6320 protected CharSequence mSummaryText = null;
6321
6322 /**
6323 * @hide
6324 */
6325 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04006326
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006327 protected Builder mBuilder;
6328
Chris Wrend6297db2012-05-03 16:20:13 -04006329 /**
6330 * Overrides ContentTitle in the big form of the template.
6331 * This defaults to the value passed to setContentTitle().
6332 */
6333 protected void internalSetBigContentTitle(CharSequence title) {
6334 mBigContentTitle = title;
6335 }
6336
6337 /**
6338 * Set the first line of text after the detail section in the big form of the template.
6339 */
6340 protected void internalSetSummaryText(CharSequence cs) {
6341 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04006342 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04006343 }
6344
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006345 public void setBuilder(Builder builder) {
6346 if (mBuilder != builder) {
6347 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07006348 if (mBuilder != null) {
6349 mBuilder.setStyle(this);
6350 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006351 }
6352 }
6353
Chris Wrend6297db2012-05-03 16:20:13 -04006354 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006355 if (mBuilder == null) {
6356 throw new IllegalArgumentException("Style requires a valid Builder object");
6357 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006358 }
Chris Wrend6297db2012-05-03 16:20:13 -04006359
6360 protected RemoteViews getStandardView(int layoutId) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006361 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder);
6362 return getStandardView(layoutId, p, null);
Selim Cinek384804b2018-04-18 14:31:07 +08006363 }
6364
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006365
Selim Cinek384804b2018-04-18 14:31:07 +08006366 /**
6367 * Get the standard view for this style.
6368 *
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006369 * @param layoutId The layout id to use.
6370 * @param p the params for this inflation.
Selim Cinek384804b2018-04-18 14:31:07 +08006371 * @param result The result where template bind information is saved.
6372 * @return A remoteView for this style.
6373 * @hide
6374 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006375 protected RemoteViews getStandardView(int layoutId, StandardTemplateParams p,
6376 TemplateBindResult result) {
Chris Wrend6297db2012-05-03 16:20:13 -04006377 checkBuilder();
6378
6379 if (mBigContentTitle != null) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006380 p.title = mBigContentTitle;
Chris Wrend6297db2012-05-03 16:20:13 -04006381 }
6382
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006383 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId, p,
6384 result);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006385
Chris Wrend6297db2012-05-03 16:20:13 -04006386 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
6387 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04006388 } else {
6389 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04006390 }
6391
Chris Wrend6297db2012-05-03 16:20:13 -04006392 return contentView;
6393 }
6394
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006395 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006396 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006397 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08006398 *
6399 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006400 * @hide
6401 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08006402 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006403 return null;
6404 }
6405
6406 /**
6407 * Construct a Style-specific RemoteViews for the final big notification layout.
6408 * @hide
6409 */
6410 public RemoteViews makeBigContentView() {
6411 return null;
6412 }
6413
6414 /**
6415 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08006416 *
6417 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006418 * @hide
6419 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006420 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006421 return null;
6422 }
6423
6424 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006425 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006426 * @hide
6427 */
6428 public void addExtras(Bundle extras) {
6429 if (mSummaryTextSet) {
6430 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
6431 }
6432 if (mBigContentTitle != null) {
6433 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
6434 }
Chris Wren91ad5632013-06-05 15:05:57 -04006435 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006436 }
6437
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006438 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006439 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006440 * @hide
6441 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02006442 protected void restoreFromExtras(Bundle extras) {
6443 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
6444 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
6445 mSummaryTextSet = true;
6446 }
6447 if (extras.containsKey(EXTRA_TITLE_BIG)) {
6448 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
6449 }
6450 }
6451
6452
6453 /**
6454 * @hide
6455 */
6456 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006457 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006458 return wip;
6459 }
6460
Daniel Sandler0ec46202015-06-24 01:27:05 -04006461 /**
6462 * @hide
6463 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006464 public void purgeResources() {}
6465
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006466 /**
6467 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
6468 * attached to.
6469 *
6470 * @return the fully constructed Notification.
6471 */
6472 public Notification build() {
6473 checkBuilder();
6474 return mBuilder.build();
6475 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006476
6477 /**
6478 * @hide
6479 * @return true if the style positions the progress bar on the second line; false if the
6480 * style hides the progress bar
6481 */
6482 protected boolean hasProgress() {
6483 return true;
6484 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006485
6486 /**
6487 * @hide
6488 * @return Whether we should put the summary be put into the notification header
6489 */
6490 public boolean hasSummaryInHeader() {
6491 return true;
6492 }
Selim Cinek593610c2016-02-16 18:42:57 -08006493
6494 /**
6495 * @hide
6496 * @return Whether custom content views are displayed inline in the style
6497 */
6498 public boolean displayCustomViewInline() {
6499 return false;
6500 }
Selim Cinekd0426622017-07-11 13:19:59 +02006501
6502 /**
6503 * Reduces the image sizes contained in this style.
6504 *
6505 * @hide
6506 */
6507 public void reduceImageSizes(Context context) {
6508 }
Selim Cinek90343862018-02-01 11:07:11 -08006509
6510 /**
6511 * Validate that this style was properly composed. This is called at build time.
6512 * @hide
6513 */
6514 public void validate(Context context) {
6515 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006516
6517 /**
6518 * @hide
6519 */
6520 public abstract boolean areNotificationsVisiblyDifferent(Style other);
Selim Cinekc7f5a822018-03-20 19:32:06 -07006521
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006522 /**
koprivaa1a78482018-10-09 10:09:23 -07006523 * @return the text that should be displayed in the statusBar when heads-upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006524 * If {@code null} is returned, the default implementation will be used.
6525 *
6526 * @hide
6527 */
6528 public CharSequence getHeadsUpStatusBarText() {
6529 return null;
6530 }
Joe Onorato46439ce2010-11-19 13:56:21 -08006531 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006532
6533 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006534 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08006535 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006536 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006537 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006538 * Notification notif = new Notification.Builder(mContext)
6539 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
6540 * .setContentText(subject)
6541 * .setSmallIcon(R.drawable.new_post)
6542 * .setLargeIcon(aBitmap)
6543 * .setStyle(new Notification.BigPictureStyle()
6544 * .bigPicture(aBigBitmap))
6545 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006546 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006547 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006548 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006549 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006550 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006551 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006552 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006553 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006554
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006555 public BigPictureStyle() {
6556 }
6557
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006558 /**
6559 * @deprecated use {@code BigPictureStyle()}.
6560 */
6561 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006562 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006563 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006564 }
6565
Chris Wrend6297db2012-05-03 16:20:13 -04006566 /**
6567 * Overrides ContentTitle in the big form of the template.
6568 * This defaults to the value passed to setContentTitle().
6569 */
6570 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006571 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006572 return this;
6573 }
6574
6575 /**
6576 * Set the first line of text after the detail section in the big form of the template.
6577 */
6578 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006579 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006580 return this;
6581 }
6582
Chris Wren0bd664d2012-08-01 13:56:56 -04006583 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05006584 * @hide
6585 */
6586 public Bitmap getBigPicture() {
6587 return mPicture;
6588 }
6589
6590 /**
Chris Wren0bd664d2012-08-01 13:56:56 -04006591 * Provide the bitmap to be used as the payload for the BigPicture notification.
6592 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006593 public BigPictureStyle bigPicture(Bitmap b) {
6594 mPicture = b;
6595 return this;
6596 }
6597
Chris Wren3745a3d2012-05-22 15:11:52 -04006598 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04006599 * Override the large icon when the big notification is shown.
6600 */
6601 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04006602 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
6603 }
6604
6605 /**
6606 * Override the large icon when the big notification is shown.
6607 */
6608 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04006609 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006610 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006611 return this;
6612 }
6613
Riley Andrews0394a0c2015-11-03 23:36:52 -08006614 /** @hide */
6615 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
6616
Daniel Sandler0ec46202015-06-24 01:27:05 -04006617 /**
6618 * @hide
6619 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006620 @Override
6621 public void purgeResources() {
6622 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08006623 if (mPicture != null &&
6624 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08006625 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006626 mPicture = mPicture.createAshmemBitmap();
6627 }
6628 if (mBigLargeIcon != null) {
6629 mBigLargeIcon.convertToAshmem();
6630 }
6631 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01006632
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006633 /**
6634 * @hide
6635 */
Selim Cinekd0426622017-07-11 13:19:59 +02006636 @Override
6637 public void reduceImageSizes(Context context) {
6638 super.reduceImageSizes(context);
6639 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006640 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006641 if (mPicture != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006642 int maxPictureWidth = resources.getDimensionPixelSize(isLowRam
6643 ? R.dimen.notification_big_picture_max_height_low_ram
6644 : R.dimen.notification_big_picture_max_height);
6645 int maxPictureHeight = resources.getDimensionPixelSize(isLowRam
6646 ? R.dimen.notification_big_picture_max_width_low_ram
6647 : R.dimen.notification_big_picture_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006648 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
6649 }
6650 if (mBigLargeIcon != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006651 int rightIconSize = resources.getDimensionPixelSize(isLowRam
6652 ? R.dimen.notification_right_icon_size_low_ram
6653 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006654 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
6655 }
6656 }
6657
6658 /**
6659 * @hide
6660 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006661 public RemoteViews makeBigContentView() {
6662 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01006663 // This covers the following cases:
6664 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006665 // mN.mLargeIcon
6666 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04006667 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07006668 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006669 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006670 oldLargeIcon = mBuilder.mN.mLargeIcon;
6671 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006672 // The legacy largeIcon might not allow us to clear the image, as it's taken in
6673 // replacement if the other one is null. Because we're restoring these legacy icons
6674 // for old listeners, this is in general non-null.
6675 largeIconLegacy = mBuilder.mN.largeIcon;
6676 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006677 }
6678
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006679 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder);
Selim Cinek384804b2018-04-18 14:31:07 +08006680 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource(),
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006681 p, null /* result */);
Selim Cinek03d0d652015-11-13 13:18:09 -05006682 if (mSummaryTextSet) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006683 contentView.setTextViewText(R.id.text, mBuilder.processTextSpans(
6684 mBuilder.processLegacyText(mSummaryText)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006685 mBuilder.setTextViewColorSecondary(contentView, R.id.text, p);
Selim Cinekc848c3a2016-01-13 15:27:30 -08006686 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05006687 }
Selim Cinek279fa862016-06-14 10:57:25 -07006688 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08006689
Christoph Studer5c510ee2014-12-15 16:32:27 +01006690 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006691 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006692 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006693 }
6694
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006695 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006696 return contentView;
6697 }
6698
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006699 /**
6700 * @hide
6701 */
6702 public void addExtras(Bundle extras) {
6703 super.addExtras(extras);
6704
6705 if (mBigLargeIconSet) {
6706 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
6707 }
6708 extras.putParcelable(EXTRA_PICTURE, mPicture);
6709 }
6710
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006711 /**
6712 * @hide
6713 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006714 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02006715 protected void restoreFromExtras(Bundle extras) {
6716 super.restoreFromExtras(extras);
6717
6718 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01006719 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02006720 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04006721 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006722 mPicture = extras.getParcelable(EXTRA_PICTURE);
6723 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006724
6725 /**
6726 * @hide
6727 */
6728 @Override
6729 public boolean hasSummaryInHeader() {
6730 return false;
6731 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006732
6733 /**
6734 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006735 * Note that we aren't actually comparing the contents of the bitmaps here, so this
6736 * is only doing a cursory inspection. Bitmaps of equal size will appear the same.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006737 */
6738 @Override
6739 public boolean areNotificationsVisiblyDifferent(Style other) {
6740 if (other == null || getClass() != other.getClass()) {
6741 return true;
6742 }
6743 BigPictureStyle otherS = (BigPictureStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006744 return areBitmapsObviouslyDifferent(getBigPicture(), otherS.getBigPicture());
6745 }
6746
6747 private static boolean areBitmapsObviouslyDifferent(Bitmap a, Bitmap b) {
6748 if (a == b) {
6749 return false;
6750 }
6751 if (a == null || b == null) {
6752 return true;
6753 }
6754 return a.getWidth() != b.getWidth()
6755 || a.getHeight() != b.getHeight()
6756 || a.getConfig() != b.getConfig()
6757 || a.getGenerationId() != b.getGenerationId();
Julia Reynolds7217dc92018-03-07 12:12:09 -05006758 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006759 }
6760
6761 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006762 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08006763 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006764 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006765 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006766 * Notification notif = new Notification.Builder(mContext)
6767 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
6768 * .setContentText(subject)
6769 * .setSmallIcon(R.drawable.new_mail)
6770 * .setLargeIcon(aBitmap)
6771 * .setStyle(new Notification.BigTextStyle()
6772 * .bigText(aVeryLongString))
6773 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006774 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006775 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006776 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006777 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006778 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006779
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006780 private CharSequence mBigText;
6781
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006782 public BigTextStyle() {
6783 }
6784
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006785 /**
6786 * @deprecated use {@code BigTextStyle()}.
6787 */
6788 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006789 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006790 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006791 }
6792
Chris Wrend6297db2012-05-03 16:20:13 -04006793 /**
6794 * Overrides ContentTitle in the big form of the template.
6795 * This defaults to the value passed to setContentTitle().
6796 */
6797 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006798 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006799 return this;
6800 }
6801
6802 /**
6803 * Set the first line of text after the detail section in the big form of the template.
6804 */
6805 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006806 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006807 return this;
6808 }
6809
Chris Wren0bd664d2012-08-01 13:56:56 -04006810 /**
6811 * Provide the longer text to be displayed in the big form of the
6812 * template in place of the content text.
6813 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006814 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006815 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006816 return this;
6817 }
6818
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006819 /**
6820 * @hide
6821 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05006822 public CharSequence getBigText() {
6823 return mBigText;
6824 }
6825
6826 /**
6827 * @hide
6828 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006829 public void addExtras(Bundle extras) {
6830 super.addExtras(extras);
6831
Christoph Studer4600f9b2014-07-22 22:44:43 +02006832 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
6833 }
6834
6835 /**
6836 * @hide
6837 */
6838 @Override
6839 protected void restoreFromExtras(Bundle extras) {
6840 super.restoreFromExtras(extras);
6841
6842 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006843 }
6844
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006845 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006846 * @param increasedHeight true if this layout be created with an increased height.
6847 *
6848 * @hide
6849 */
6850 @Override
6851 public RemoteViews makeContentView(boolean increasedHeight) {
6852 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006853 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006854 mBuilder.mActions = new ArrayList<>();
6855 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006856 mBuilder.mActions = mBuilder.mOriginalActions;
6857 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006858 return remoteViews;
6859 }
6860 return super.makeContentView(increasedHeight);
6861 }
6862
6863 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006864 * @hide
6865 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006866 @Override
6867 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
6868 if (increasedHeight && mBuilder.mActions.size() > 0) {
6869 return makeBigContentView();
6870 }
6871 return super.makeHeadsUpContentView(increasedHeight);
6872 }
6873
6874 /**
6875 * @hide
6876 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006877 public RemoteViews makeBigContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006878 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder).text(null);
Selim Cinek384804b2018-04-18 14:31:07 +08006879 TemplateBindResult result = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006880 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource(), p,
6881 result);
Selim Cinek1c72fa02018-04-23 18:00:54 +08006882 contentView.setInt(R.id.big_text, "setImageEndMargin", result.getIconMarginEnd());
Joe Malin8d40d042012-11-05 11:36:40 -08006883
Selim Cinek3a2c4b92015-12-17 17:01:17 -08006884 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07006885 if (TextUtils.isEmpty(bigTextText)) {
6886 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
6887 // experience
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006888 bigTextText = mBuilder.processLegacyText(
6889 mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT));
Selim Cinek75998782016-04-26 10:39:17 -07006890 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006891 contentView.setTextViewText(R.id.big_text, mBuilder.processTextSpans(bigTextText));
6892 mBuilder.setTextViewColorSecondary(contentView, R.id.big_text, p);
6893 contentView.setViewVisibility(R.id.big_text,
6894 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08006895 contentView.setBoolean(R.id.big_text, "setHasImage",
6896 result.isRightIconContainerVisible());
Selim Cinek4fb12d32015-11-19 18:10:48 -08006897
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006898 return contentView;
6899 }
6900
Julia Reynolds7217dc92018-03-07 12:12:09 -05006901 /**
6902 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006903 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006904 */
6905 @Override
6906 public boolean areNotificationsVisiblyDifferent(Style other) {
6907 if (other == null || getClass() != other.getClass()) {
6908 return true;
6909 }
6910 BigTextStyle newS = (BigTextStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006911 return !Objects.equals(String.valueOf(getBigText()), String.valueOf(newS.getBigText()));
Julia Reynolds7217dc92018-03-07 12:12:09 -05006912 }
6913
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006914 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006915
6916 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006917 * Helper class for generating large-format notifications that include multiple back-and-forth
6918 * messages of varying types between any number of people.
6919 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006920 * <p>
Alex Hillsfc737de2016-03-23 17:33:02 -04006921 * If the platform does not provide large-format notifications, this method has no effect. The
6922 * user will always see the normal notification view.
Selim Cinekce8794f2018-05-23 16:46:05 -07006923 *
6924 * <p>
6925 * If the app is targeting Android P and above, it is required to use the {@link Person}
6926 * class in order to get an optimal rendering of the notification and its avatars. For
6927 * conversations involving multiple people, the app should also make sure that it marks the
6928 * conversation as a group with {@link #setGroupConversation(boolean)}.
6929 *
6930 * <p>
6931 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior.
6932 * Here's an example of how this may be used:
Alex Hillsfc737de2016-03-23 17:33:02 -04006933 * <pre class="prettyprint">
6934 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006935 * Person user = new Person.Builder().setIcon(userIcon).setName(userName).build();
6936 * MessagingStyle style = new MessagingStyle(user)
6937 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getPerson())
6938 * .addMessage(messages[2].getText(), messages[2].getTime(), messages[2].getPerson())
6939 * .setGroupConversation(hasMultiplePeople());
6940 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006941 * Notification noti = new Notification.Builder()
Selim Cinekce8794f2018-05-23 16:46:05 -07006942 * .setContentTitle(&quot;2 new messages with &quot; + sender.toString())
Alex Hillsfc737de2016-03-23 17:33:02 -04006943 * .setContentText(subject)
6944 * .setSmallIcon(R.drawable.new_message)
6945 * .setLargeIcon(aBitmap)
Selim Cinekce8794f2018-05-23 16:46:05 -07006946 * .setStyle(style)
Alex Hillsfc737de2016-03-23 17:33:02 -04006947 * .build();
6948 * </pre>
6949 */
6950 public static class MessagingStyle extends Style {
6951
6952 /**
6953 * The maximum number of messages that will be retained in the Notification itself (the
6954 * number displayed is up to the platform).
6955 */
6956 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
6957
Selim Cinekcb8b9852017-12-15 18:01:52 -08006958 @NonNull Person mUser;
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006959 @Nullable CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04006960 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08006961 List<Message> mHistoricMessages = new ArrayList<>();
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006962 boolean mIsGroupConversation;
Alex Hillsfc737de2016-03-23 17:33:02 -04006963
6964 MessagingStyle() {
6965 }
6966
6967 /**
Alex Hillsfd590442016-10-07 09:52:44 -04006968 * @param userDisplayName Required - the name to be displayed for any replies sent by the
6969 * user before the posting app reposts the notification with those messages after they've
6970 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04006971 * {@link #addMessage(Notification.MessagingStyle.Message)}
Selim Cinekcb8b9852017-12-15 18:01:52 -08006972 *
6973 * @deprecated use {@code MessagingStyle(Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006974 */
Alex Hillsfd590442016-10-07 09:52:44 -04006975 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Selim Cinek9acd6732018-03-23 16:39:02 -07006976 this(new Person.Builder().setName(userDisplayName).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006977 }
6978
6979 /**
6980 * @param user Required - The person displayed for any messages that are sent by the
6981 * user. Any messages added with {@link #addMessage(Notification.MessagingStyle.Message)}
6982 * who don't have a Person associated with it will be displayed as if they were sent
Selim Cinek90343862018-02-01 11:07:11 -08006983 * by this user. The user also needs to have a valid name associated with it, which will
6984 * be enforced starting in Android P.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006985 */
6986 public MessagingStyle(@NonNull Person user) {
6987 mUser = user;
Selim Cinek90343862018-02-01 11:07:11 -08006988 }
6989
6990 /**
6991 * Validate that this style was properly composed. This is called at build time.
6992 * @hide
6993 */
6994 @Override
6995 public void validate(Context context) {
6996 super.validate(context);
6997 if (context.getApplicationInfo().targetSdkVersion
6998 >= Build.VERSION_CODES.P && (mUser == null || mUser.getName() == null)) {
6999 throw new RuntimeException("User must be valid and have a name.");
Selim Cinekcb8b9852017-12-15 18:01:52 -08007000 }
7001 }
7002
7003 /**
koprivaa1a78482018-10-09 10:09:23 -07007004 * @return the text that should be displayed in the statusBar when heads upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08007005 * If {@code null} is returned, the default implementation will be used.
7006 *
7007 * @hide
7008 */
7009 @Override
7010 public CharSequence getHeadsUpStatusBarText() {
7011 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
7012 ? super.mBigContentTitle
7013 : mConversationTitle;
7014 if (!TextUtils.isEmpty(conversationTitle) && !hasOnlyWhiteSpaceSenders()) {
7015 return conversationTitle;
7016 }
7017 return null;
7018 }
7019
7020 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08007021 * @return the user to be displayed for any replies sent by the user
7022 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007023 @NonNull
Selim Cinekcb8b9852017-12-15 18:01:52 -08007024 public Person getUser() {
7025 return mUser;
Alex Hillsfc737de2016-03-23 17:33:02 -04007026 }
7027
7028 /**
7029 * Returns the name to be displayed for any replies sent by the user
Selim Cinekcb8b9852017-12-15 18:01:52 -08007030 *
7031 * @deprecated use {@link #getUser()} instead
Alex Hillsfc737de2016-03-23 17:33:02 -04007032 */
7033 public CharSequence getUserDisplayName() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007034 return mUser.getName();
Alex Hillsfc737de2016-03-23 17:33:02 -04007035 }
7036
7037 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007038 * Sets the title to be displayed on this conversation. May be set to {@code null}.
7039 *
Kodlee Yin14656422017-12-22 17:00:46 -08007040 * <p>This API's behavior was changed in SDK version {@link Build.VERSION_CODES#P}. If your
7041 * application's target version is less than {@link Build.VERSION_CODES#P}, setting a
7042 * conversation title to a non-null value will make {@link #isGroupConversation()} return
7043 * {@code true} and passing {@code null} will make it return {@code false}. In
7044 * {@link Build.VERSION_CODES#P} and beyond, use {@link #setGroupConversation(boolean)}
7045 * to set group conversation status.
7046 *
7047 * @param conversationTitle Title displayed for this conversation
7048 * @return this object for method chaining
Alex Hillsfc737de2016-03-23 17:33:02 -04007049 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007050 public MessagingStyle setConversationTitle(@Nullable CharSequence conversationTitle) {
Alex Hillsfc737de2016-03-23 17:33:02 -04007051 mConversationTitle = conversationTitle;
7052 return this;
7053 }
7054
7055 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007056 * Return the title to be displayed on this conversation. May return {@code null}.
Alex Hillsfc737de2016-03-23 17:33:02 -04007057 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007058 @Nullable
Alex Hillsfc737de2016-03-23 17:33:02 -04007059 public CharSequence getConversationTitle() {
7060 return mConversationTitle;
7061 }
7062
7063 /**
7064 * Adds a message for display by this notification. Convenience call for a simple
7065 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
7066 * @param text A {@link CharSequence} to be displayed as the message content
7067 * @param timestamp Time at which the message arrived
7068 * @param sender A {@link CharSequence} to be used for displaying the name of the
7069 * sender. Should be <code>null</code> for messages by the current user, in which case
7070 * the platform will insert {@link #getUserDisplayName()}.
7071 * Should be unique amongst all individuals in the conversation, and should be
7072 * consistent during re-posts of the notification.
7073 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07007074 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Alex Hillsfc737de2016-03-23 17:33:02 -04007075 *
7076 * @return this object for method chaining
Selim Cinekcb8b9852017-12-15 18:01:52 -08007077 *
7078 * @deprecated use {@link #addMessage(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04007079 */
7080 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007081 return addMessage(text, timestamp,
Selim Cinek9acd6732018-03-23 16:39:02 -07007082 sender == null ? null : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08007083 }
7084
7085 /**
7086 * Adds a message for display by this notification. Convenience call for a simple
7087 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
7088 * @param text A {@link CharSequence} to be displayed as the message content
7089 * @param timestamp Time at which the message arrived
7090 * @param sender The {@link Person} who sent the message.
7091 * Should be <code>null</code> for messages by the current user, in which case
7092 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7093 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07007094 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Selim Cinekcb8b9852017-12-15 18:01:52 -08007095 *
7096 * @return this object for method chaining
7097 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007098 public MessagingStyle addMessage(@NonNull CharSequence text, long timestamp,
7099 @Nullable Person sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08007100 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04007101 }
7102
7103 /**
7104 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08007105 *
7106 * <p>The messages should be added in chronologic order, i.e. the oldest first,
7107 * the newest last.
7108 *
Alex Hillsfc737de2016-03-23 17:33:02 -04007109 * @param message The {@link Message} to be displayed
7110 * @return this object for method chaining
7111 */
7112 public MessagingStyle addMessage(Message message) {
7113 mMessages.add(message);
7114 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
7115 mMessages.remove(0);
7116 }
7117 return this;
7118 }
7119
7120 /**
Adrian Roos437cd562017-01-18 15:47:03 -08007121 * Adds a {@link Message} for historic context in this notification.
7122 *
7123 * <p>Messages should be added as historic if they are not the main subject of the
7124 * notification but may give context to a conversation. The system may choose to present
7125 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
7126 *
7127 * <p>The messages should be added in chronologic order, i.e. the oldest first,
7128 * the newest last.
7129 *
7130 * @param message The historic {@link Message} to be added
7131 * @return this object for method chaining
7132 */
7133 public MessagingStyle addHistoricMessage(Message message) {
7134 mHistoricMessages.add(message);
7135 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
7136 mHistoricMessages.remove(0);
7137 }
7138 return this;
7139 }
7140
7141 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007142 * Gets the list of {@code Message} objects that represent the notification
7143 */
7144 public List<Message> getMessages() {
7145 return mMessages;
7146 }
7147
7148 /**
Adrian Roos437cd562017-01-18 15:47:03 -08007149 * Gets the list of historic {@code Message}s in the notification.
7150 */
7151 public List<Message> getHistoricMessages() {
7152 return mHistoricMessages;
7153 }
7154
7155 /**
Selim Cinekce8794f2018-05-23 16:46:05 -07007156 * Sets whether this conversation notification represents a group. If the app is targeting
7157 * Android P, this is required if the app wants to display the largeIcon set with
7158 * {@link Notification.Builder#setLargeIcon(Bitmap)}, otherwise it will be hidden.
Kodlee Yin14656422017-12-22 17:00:46 -08007159 *
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007160 * @param isGroupConversation {@code true} if the conversation represents a group,
7161 * {@code false} otherwise.
7162 * @return this object for method chaining
7163 */
7164 public MessagingStyle setGroupConversation(boolean isGroupConversation) {
7165 mIsGroupConversation = isGroupConversation;
7166 return this;
7167 }
7168
7169 /**
Kodlee Yin14656422017-12-22 17:00:46 -08007170 * Returns {@code true} if this notification represents a group conversation, otherwise
7171 * {@code false}.
7172 *
7173 * <p> If the application that generated this {@link MessagingStyle} targets an SDK version
7174 * less than {@link Build.VERSION_CODES#P}, this method becomes dependent on whether or
7175 * not the conversation title is set; returning {@code true} if the conversation title is
7176 * a non-null value, or {@code false} otherwise. From {@link Build.VERSION_CODES#P} forward,
7177 * this method returns what's set by {@link #setGroupConversation(boolean)} allowing for
7178 * named, non-group conversations.
7179 *
7180 * @see #setConversationTitle(CharSequence)
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007181 */
7182 public boolean isGroupConversation() {
Kodlee Yin14656422017-12-22 17:00:46 -08007183 // When target SDK version is < P, a non-null conversation title dictates if this is
7184 // as group conversation.
7185 if (mBuilder != null
7186 && mBuilder.mContext.getApplicationInfo().targetSdkVersion
7187 < Build.VERSION_CODES.P) {
7188 return mConversationTitle != null;
7189 }
7190
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007191 return mIsGroupConversation;
7192 }
7193
7194 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007195 * @hide
7196 */
7197 @Override
7198 public void addExtras(Bundle extras) {
7199 super.addExtras(extras);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007200 if (mUser != null) {
7201 // For legacy usages
7202 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUser.getName());
7203 extras.putParcelable(EXTRA_MESSAGING_PERSON, mUser);
Alex Hillsfc737de2016-03-23 17:33:02 -04007204 }
7205 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007206 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04007207 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007208 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
7209 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04007210 }
Adrian Roos437cd562017-01-18 15:47:03 -08007211 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
7212 Message.getBundleArrayForMessages(mHistoricMessages));
7213 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007214
7215 fixTitleAndTextExtras(extras);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007216 extras.putBoolean(EXTRA_IS_GROUP_CONVERSATION, mIsGroupConversation);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007217 }
7218
7219 private void fixTitleAndTextExtras(Bundle extras) {
7220 Message m = findLatestIncomingMessage();
7221 CharSequence text = (m == null) ? null : m.mText;
7222 CharSequence sender = m == null ? null
Selim Cinekcb8b9852017-12-15 18:01:52 -08007223 : m.mSender == null || TextUtils.isEmpty(m.mSender.getName())
7224 ? mUser.getName() : m.mSender.getName();
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007225 CharSequence title;
7226 if (!TextUtils.isEmpty(mConversationTitle)) {
7227 if (!TextUtils.isEmpty(sender)) {
7228 BidiFormatter bidi = BidiFormatter.getInstance();
7229 title = mBuilder.mContext.getString(
7230 com.android.internal.R.string.notification_messaging_title_template,
Selim Cinekcb8b9852017-12-15 18:01:52 -08007231 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(sender));
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007232 } else {
7233 title = mConversationTitle;
7234 }
7235 } else {
7236 title = sender;
7237 }
7238
7239 if (title != null) {
7240 extras.putCharSequence(EXTRA_TITLE, title);
7241 }
7242 if (text != null) {
7243 extras.putCharSequence(EXTRA_TEXT, text);
7244 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007245 }
7246
7247 /**
7248 * @hide
7249 */
7250 @Override
7251 protected void restoreFromExtras(Bundle extras) {
7252 super.restoreFromExtras(extras);
7253
Selim Cinekcb8b9852017-12-15 18:01:52 -08007254 mUser = extras.getParcelable(EXTRA_MESSAGING_PERSON);
7255 if (mUser == null) {
7256 CharSequence displayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
Selim Cinek9acd6732018-03-23 16:39:02 -07007257 mUser = new Person.Builder().setName(displayName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007258 }
Adrian Roos96b7e202016-05-17 13:50:38 -07007259 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08007260 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007261 mMessages = Message.getMessagesFromBundleArray(messages);
Adrian Roos437cd562017-01-18 15:47:03 -08007262 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007263 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007264 mIsGroupConversation = extras.getBoolean(EXTRA_IS_GROUP_CONVERSATION);
Alex Hillsfc737de2016-03-23 17:33:02 -04007265 }
7266
7267 /**
7268 * @hide
7269 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07007270 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007271 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07007272 mBuilder.mOriginalActions = mBuilder.mActions;
7273 mBuilder.mActions = new ArrayList<>();
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007274 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007275 false /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007276 mBuilder.mActions = mBuilder.mOriginalActions;
7277 mBuilder.mOriginalActions = null;
7278 return remoteViews;
Adrian Roosc1a80b02016-04-05 14:54:55 -07007279 }
7280
Julia Reynolds7217dc92018-03-07 12:12:09 -05007281 /**
7282 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04007283 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05007284 */
7285 @Override
7286 public boolean areNotificationsVisiblyDifferent(Style other) {
7287 if (other == null || getClass() != other.getClass()) {
7288 return true;
7289 }
7290 MessagingStyle newS = (MessagingStyle) other;
7291 List<MessagingStyle.Message> oldMs = getMessages();
7292 List<MessagingStyle.Message> newMs = newS.getMessages();
7293
Dan Sandler7d67bd42018-05-15 14:06:38 -04007294 if (oldMs == null || newMs == null) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007295 newMs = new ArrayList<>();
7296 }
7297
7298 int n = oldMs.size();
7299 if (n != newMs.size()) {
7300 return true;
7301 }
7302 for (int i = 0; i < n; i++) {
7303 MessagingStyle.Message oldM = oldMs.get(i);
7304 MessagingStyle.Message newM = newMs.get(i);
Dan Sandler7d67bd42018-05-15 14:06:38 -04007305 if (!Objects.equals(
7306 String.valueOf(oldM.getText()),
7307 String.valueOf(newM.getText()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007308 return true;
7309 }
7310 if (!Objects.equals(oldM.getDataUri(), newM.getDataUri())) {
7311 return true;
7312 }
Dan Sandler7d67bd42018-05-15 14:06:38 -04007313 String oldSender = String.valueOf(oldM.getSenderPerson() == null
7314 ? oldM.getSender()
7315 : oldM.getSenderPerson().getName());
7316 String newSender = String.valueOf(newM.getSenderPerson() == null
7317 ? newM.getSender()
7318 : newM.getSenderPerson().getName());
Julia Reynolds7217dc92018-03-07 12:12:09 -05007319 if (!Objects.equals(oldSender, newSender)) {
7320 return true;
7321 }
7322
7323 String oldKey = oldM.getSenderPerson() == null
7324 ? null : oldM.getSenderPerson().getKey();
7325 String newKey = newM.getSenderPerson() == null
7326 ? null : newM.getSenderPerson().getKey();
7327 if (!Objects.equals(oldKey, newKey)) {
7328 return true;
7329 }
7330 // Other fields (like timestamp) intentionally excluded
7331 }
7332 return false;
7333 }
7334
Adrian Roosc1a80b02016-04-05 14:54:55 -07007335 private Message findLatestIncomingMessage() {
Selim Cinek88188f22017-09-19 16:46:56 -07007336 return findLatestIncomingMessage(mMessages);
7337 }
7338
7339 /**
7340 * @hide
7341 */
7342 @Nullable
7343 public static Message findLatestIncomingMessage(
7344 List<Message> messages) {
7345 for (int i = messages.size() - 1; i >= 0; i--) {
7346 Message m = messages.get(i);
Adrian Roosc1a80b02016-04-05 14:54:55 -07007347 // Incoming messages have a non-empty sender.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007348 if (m.mSender != null && !TextUtils.isEmpty(m.mSender.getName())) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07007349 return m;
7350 }
7351 }
Selim Cinek88188f22017-09-19 16:46:56 -07007352 if (!messages.isEmpty()) {
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007353 // No incoming messages, fall back to outgoing message
Selim Cinek88188f22017-09-19 16:46:56 -07007354 return messages.get(messages.size() - 1);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007355 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07007356 return null;
7357 }
7358
7359 /**
7360 * @hide
7361 */
7362 @Override
7363 public RemoteViews makeBigContentView() {
Selim Cinek384804b2018-04-18 14:31:07 +08007364 return makeMessagingView(false /* displayImagesAtEnd */, true /* hideLargeIcon */);
Selim Cinekafeed292017-12-12 17:32:44 -08007365 }
7366
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007367 /**
7368 * Create a messaging layout.
7369 *
7370 * @param displayImagesAtEnd should images be displayed at the end of the content instead
7371 * of inline.
Selim Cinek384804b2018-04-18 14:31:07 +08007372 * @param hideRightIcons Should the reply affordance be shown at the end of the notification
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007373 * @return the created remoteView.
7374 */
Selim Cinekafeed292017-12-12 17:32:44 -08007375 @NonNull
Selim Cinek384804b2018-04-18 14:31:07 +08007376 private RemoteViews makeMessagingView(boolean displayImagesAtEnd, boolean hideRightIcons) {
Selim Cinek88188f22017-09-19 16:46:56 -07007377 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
Adrian Roosc1a80b02016-04-05 14:54:55 -07007378 ? super.mBigContentTitle
7379 : mConversationTitle;
Selim Cinekce8794f2018-05-23 16:46:05 -07007380 boolean atLeastP = mBuilder.mContext.getApplicationInfo().targetSdkVersion
7381 >= Build.VERSION_CODES.P;
7382 boolean isOneToOne;
Selim Cinek2dd3e722018-01-19 11:06:06 -08007383 CharSequence nameReplacement = null;
Selim Cinekce8794f2018-05-23 16:46:05 -07007384 Icon avatarReplacement = null;
7385 if (!atLeastP) {
7386 isOneToOne = TextUtils.isEmpty(conversationTitle);
7387 avatarReplacement = mBuilder.mN.mLargeIcon;
7388 if (hasOnlyWhiteSpaceSenders()) {
7389 isOneToOne = true;
7390 nameReplacement = conversationTitle;
7391 conversationTitle = null;
7392 }
7393 } else {
7394 isOneToOne = !isGroupConversation();
Adrian Roosb1f427c2016-05-26 12:27:15 -07007395 }
Selim Cinek384804b2018-04-18 14:31:07 +08007396 TemplateBindResult bindResult = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007397 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).title(
7398 conversationTitle).text(null)
7399 .hideLargeIcon(hideRightIcons || isOneToOne)
7400 .hideReplyIcon(hideRightIcons)
7401 .headerTextSecondary(conversationTitle);
Adrian Roos48d746a2016-04-12 14:57:28 -07007402 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07007403 mBuilder.getMessagingLayoutResource(),
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007404 p,
Selim Cinek384804b2018-04-18 14:31:07 +08007405 bindResult);
Selim Cinek88188f22017-09-19 16:46:56 -07007406 addExtras(mBuilder.mN.extras);
Selim Cinekafeed292017-12-12 17:32:44 -08007407 // also update the end margin if there is an image
Selim Cinek1c72fa02018-04-23 18:00:54 +08007408 contentView.setViewLayoutMarginEnd(R.id.notification_messaging,
7409 bindResult.getIconMarginEnd());
Selim Cinek88188f22017-09-19 16:46:56 -07007410 contentView.setInt(R.id.status_bar_latest_event_content, "setLayoutColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007411 mBuilder.isColorized(p) ? mBuilder.getPrimaryTextColor(p)
7412 : mBuilder.resolveContrastColor(p));
Kenny Guy14d035c2018-05-02 19:10:36 +01007413 contentView.setInt(R.id.status_bar_latest_event_content, "setSenderTextColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007414 mBuilder.getPrimaryTextColor(p));
Kenny Guy14d035c2018-05-02 19:10:36 +01007415 contentView.setInt(R.id.status_bar_latest_event_content, "setMessageTextColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007416 mBuilder.getSecondaryTextColor(p));
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007417 contentView.setBoolean(R.id.status_bar_latest_event_content, "setDisplayImagesAtEnd",
7418 displayImagesAtEnd);
Selim Cinekce8794f2018-05-23 16:46:05 -07007419 contentView.setIcon(R.id.status_bar_latest_event_content, "setAvatarReplacement",
7420 avatarReplacement);
Selim Cinek2dd3e722018-01-19 11:06:06 -08007421 contentView.setCharSequence(R.id.status_bar_latest_event_content, "setNameReplacement",
7422 nameReplacement);
Selim Cinek88188f22017-09-19 16:46:56 -07007423 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsOneToOne",
7424 isOneToOne);
7425 contentView.setBundle(R.id.status_bar_latest_event_content, "setData",
7426 mBuilder.mN.extras);
Alex Hillsfc737de2016-03-23 17:33:02 -04007427 return contentView;
7428 }
7429
Selim Cinekf7409db2017-10-24 16:17:14 -07007430 private boolean hasOnlyWhiteSpaceSenders() {
7431 for (int i = 0; i < mMessages.size(); i++) {
7432 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007433 Person sender = m.getSenderPerson();
7434 if (sender != null && !isWhiteSpace(sender.getName())) {
Selim Cinekf7409db2017-10-24 16:17:14 -07007435 return false;
7436 }
7437 }
7438 return true;
7439 }
7440
7441 private boolean isWhiteSpace(CharSequence sender) {
7442 if (TextUtils.isEmpty(sender)) {
7443 return true;
7444 }
7445 if (sender.toString().matches("^\\s*$")) {
7446 return true;
7447 }
7448 // Let's check if we only have 0 whitespace chars. Some apps did this as a workaround
7449 // For the presentation that we had.
7450 for (int i = 0; i < sender.length(); i++) {
7451 char c = sender.charAt(i);
7452 if (c != '\u200B') {
7453 return false;
7454 }
7455 }
7456 return true;
7457 }
7458
Selim Cinek88188f22017-09-19 16:46:56 -07007459 private CharSequence createConversationTitleFromMessages() {
7460 ArraySet<CharSequence> names = new ArraySet<>();
7461 for (int i = 0; i < mMessages.size(); i++) {
7462 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007463 Person sender = m.getSenderPerson();
Selim Cinek88188f22017-09-19 16:46:56 -07007464 if (sender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007465 names.add(sender.getName());
Selim Cinek88188f22017-09-19 16:46:56 -07007466 }
7467 }
7468 SpannableStringBuilder title = new SpannableStringBuilder();
7469 int size = names.size();
7470 for (int i = 0; i < size; i++) {
7471 CharSequence name = names.valueAt(i);
7472 if (!TextUtils.isEmpty(title)) {
7473 title.append(", ");
7474 }
7475 title.append(BidiFormatter.getInstance().unicodeWrap(name));
7476 }
7477 return title;
7478 }
7479
Adrian Roosdedd1df2016-04-26 16:38:47 -07007480 /**
7481 * @hide
7482 */
7483 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007484 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007485 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007486 true /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007487 remoteViews.setInt(R.id.notification_messaging, "setMaxDisplayedLines", 1);
7488 return remoteViews;
Adrian Roosdedd1df2016-04-26 16:38:47 -07007489 }
7490
Adrian Roosc1a80b02016-04-05 14:54:55 -07007491 private static TextAppearanceSpan makeFontColorSpan(int color) {
7492 return new TextAppearanceSpan(null, 0, 0,
7493 ColorStateList.valueOf(color), null);
7494 }
7495
Alex Hillsd9b04d92016-04-11 16:38:16 -04007496 public static final class Message {
Tony Mak09db2ea2018-06-27 18:12:48 +01007497 /** @hide */
7498 public static final String KEY_TEXT = "text";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007499 static final String KEY_TIMESTAMP = "time";
7500 static final String KEY_SENDER = "sender";
Selim Cinekcb8b9852017-12-15 18:01:52 -08007501 static final String KEY_SENDER_PERSON = "sender_person";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007502 static final String KEY_DATA_MIME_TYPE = "type";
7503 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08007504 static final String KEY_EXTRAS_BUNDLE = "extras";
Kenny Guya0f6de82018-04-06 16:20:16 +01007505 static final String KEY_REMOTE_INPUT_HISTORY = "remote_input_history";
Alex Hillsfc737de2016-03-23 17:33:02 -04007506
7507 private final CharSequence mText;
7508 private final long mTimestamp;
Selim Cinekcb8b9852017-12-15 18:01:52 -08007509 @Nullable
7510 private final Person mSender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007511 /** True if this message was generated from the extra
7512 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}
7513 */
7514 private final boolean mRemoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007515
Shane Brennan5a871862017-03-11 13:14:17 -08007516 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007517 private String mDataMimeType;
7518 private Uri mDataUri;
7519
7520 /**
7521 * Constructor
7522 * @param text A {@link CharSequence} to be displayed as the message content
7523 * @param timestamp Time at which the message arrived
7524 * @param sender A {@link CharSequence} to be used for displaying the name of the
7525 * sender. Should be <code>null</code> for messages by the current user, in which case
7526 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
7527 * Should be unique amongst all individuals in the conversation, and should be
7528 * consistent during re-posts of the notification.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007529 *
7530 * @deprecated use {@code Message(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04007531 */
7532 public Message(CharSequence text, long timestamp, CharSequence sender){
Selim Cinek9acd6732018-03-23 16:39:02 -07007533 this(text, timestamp, sender == null ? null
7534 : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08007535 }
7536
7537 /**
7538 * Constructor
7539 * @param text A {@link CharSequence} to be displayed as the message content
7540 * @param timestamp Time at which the message arrived
7541 * @param sender The {@link Person} who sent the message.
7542 * Should be <code>null</code> for messages by the current user, in which case
7543 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7544 * <p>
Selim Cinek9acd6732018-03-23 16:39:02 -07007545 * The person provided should contain an Icon, set with
7546 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7547 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7548 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7549 * to differentiate between the different users.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007550 * </p>
7551 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007552 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender) {
Kenny Guya0f6de82018-04-06 16:20:16 +01007553 this(text, timestamp, sender, false /* remoteHistory */);
7554 }
7555
7556 /**
7557 * Constructor
7558 * @param text A {@link CharSequence} to be displayed as the message content
7559 * @param timestamp Time at which the message arrived
7560 * @param sender The {@link Person} who sent the message.
7561 * Should be <code>null</code> for messages by the current user, in which case
7562 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7563 * @param remoteInputHistory True if the messages was generated from the extra
7564 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7565 * <p>
7566 * The person provided should contain an Icon, set with
7567 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7568 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7569 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7570 * to differentiate between the different users.
7571 * </p>
7572 * @hide
7573 */
7574 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender,
7575 boolean remoteInputHistory) {
Alex Hillsfc737de2016-03-23 17:33:02 -04007576 mText = text;
7577 mTimestamp = timestamp;
7578 mSender = sender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007579 mRemoteInputHistory = remoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007580 }
7581
7582 /**
7583 * Sets a binary blob of data and an associated MIME type for a message. In the case
7584 * where the platform doesn't support the MIME type, the original text provided in the
7585 * constructor will be used.
7586 * @param dataMimeType The MIME type of the content. See
7587 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
7588 * types on Android and Android Wear.
7589 * @param dataUri The uri containing the content whose type is given by the MIME type.
7590 * <p class="note">
7591 * <ol>
7592 * <li>Notification Listeners including the System UI need permission to access the
7593 * data the Uri points to. The recommended ways to do this are:</li>
7594 * <li>Store the data in your own ContentProvider, making sure that other apps have
7595 * the correct permission to access your provider. The preferred mechanism for
7596 * providing access is to use per-URI permissions which are temporary and only
7597 * grant access to the receiving application. An easy way to create a
7598 * ContentProvider like this is to use the FileProvider helper class.</li>
7599 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
7600 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
7601 * also store non-media types (see MediaStore.Files for more info). Files can be
7602 * inserted into the MediaStore using scanFile() after which a content:// style
7603 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
7604 * Note that once added to the system MediaStore the content is accessible to any
7605 * app on the device.</li>
7606 * </ol>
7607 * @return this object for method chaining
7608 */
7609 public Message setData(String dataMimeType, Uri dataUri) {
7610 mDataMimeType = dataMimeType;
7611 mDataUri = dataUri;
7612 return this;
7613 }
7614
Alex Hillsfc737de2016-03-23 17:33:02 -04007615 /**
7616 * Get the text to be used for this message, or the fallback text if a type and content
7617 * Uri have been set
7618 */
7619 public CharSequence getText() {
7620 return mText;
7621 }
7622
7623 /**
7624 * Get the time at which this message arrived
7625 */
7626 public long getTimestamp() {
7627 return mTimestamp;
7628 }
7629
7630 /**
Shane Brennan5a871862017-03-11 13:14:17 -08007631 * Get the extras Bundle for this message.
7632 */
7633 public Bundle getExtras() {
7634 return mExtras;
7635 }
7636
7637 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007638 * Get the text used to display the contact's name in the messaging experience
Selim Cinekcb8b9852017-12-15 18:01:52 -08007639 *
7640 * @deprecated use {@link #getSenderPerson()}
Alex Hillsfc737de2016-03-23 17:33:02 -04007641 */
7642 public CharSequence getSender() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007643 return mSender == null ? null : mSender.getName();
7644 }
7645
7646 /**
7647 * Get the sender associated with this message.
7648 */
7649 @Nullable
7650 public Person getSenderPerson() {
Alex Hillsfc737de2016-03-23 17:33:02 -04007651 return mSender;
7652 }
7653
7654 /**
7655 * Get the MIME type of the data pointed to by the Uri
7656 */
7657 public String getDataMimeType() {
7658 return mDataMimeType;
7659 }
7660
7661 /**
koprivaa1a78482018-10-09 10:09:23 -07007662 * Get the Uri pointing to the content of the message. Can be null, in which case
Alex Hillsfc737de2016-03-23 17:33:02 -04007663 * {@see #getText()} is used.
7664 */
7665 public Uri getDataUri() {
7666 return mDataUri;
7667 }
7668
Kenny Guya0f6de82018-04-06 16:20:16 +01007669 /**
7670 * @return True if the message was generated from
7671 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7672 * @hide
7673 */
7674 public boolean isRemoteInputHistory() {
7675 return mRemoteInputHistory;
7676 }
7677
Beverlye98937a2018-11-15 10:18:57 -05007678 /**
7679 * @hide
7680 */
7681 @VisibleForTesting
7682 public Bundle toBundle() {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007683 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007684 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007685 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04007686 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007687 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04007688 if (mSender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007689 // Legacy listeners need this
7690 bundle.putCharSequence(KEY_SENDER, mSender.getName());
7691 bundle.putParcelable(KEY_SENDER_PERSON, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04007692 }
7693 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007694 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04007695 }
7696 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007697 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04007698 }
Shane Brennan5a871862017-03-11 13:14:17 -08007699 if (mExtras != null) {
7700 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
7701 }
Kenny Guya0f6de82018-04-06 16:20:16 +01007702 if (mRemoteInputHistory) {
7703 bundle.putBoolean(KEY_REMOTE_INPUT_HISTORY, mRemoteInputHistory);
7704 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007705 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04007706 }
7707
Alex Hillsd9b04d92016-04-11 16:38:16 -04007708 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
7709 Bundle[] bundles = new Bundle[messages.size()];
7710 final int N = messages.size();
7711 for (int i = 0; i < N; i++) {
7712 bundles[i] = messages.get(i).toBundle();
7713 }
7714 return bundles;
7715 }
7716
Selim Cinek88188f22017-09-19 16:46:56 -07007717 /**
7718 * @return A list of messages read from the bundles.
7719 *
7720 * @hide
7721 */
7722 public static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
7723 if (bundles == null) {
7724 return new ArrayList<>();
7725 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007726 List<Message> messages = new ArrayList<>(bundles.length);
7727 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07007728 if (bundles[i] instanceof Bundle) {
7729 Message message = getMessageFromBundle((Bundle)bundles[i]);
7730 if (message != null) {
7731 messages.add(message);
7732 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007733 }
7734 }
7735 return messages;
7736 }
7737
Selim Cinekb0dc61b2018-05-22 18:49:36 -07007738 /**
7739 * @return The message that is stored in the bundle or null if the message couldn't be
7740 * resolved.
7741 *
7742 * @hide
7743 */
7744 @Nullable
7745 public static Message getMessageFromBundle(Bundle bundle) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007746 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07007747 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007748 return null;
7749 } else {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007750
7751 Person senderPerson = bundle.getParcelable(KEY_SENDER_PERSON);
7752 if (senderPerson == null) {
7753 // Legacy apps that use compat don't actually provide the sender objects
7754 // We need to fix the compat version to provide people / use
7755 // the native api instead
7756 CharSequence senderName = bundle.getCharSequence(KEY_SENDER);
7757 if (senderName != null) {
Selim Cinek9acd6732018-03-23 16:39:02 -07007758 senderPerson = new Person.Builder().setName(senderName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007759 }
7760 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007761 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
Selim Cinekcb8b9852017-12-15 18:01:52 -08007762 bundle.getLong(KEY_TIMESTAMP),
Kenny Guya0f6de82018-04-06 16:20:16 +01007763 senderPerson,
7764 bundle.getBoolean(KEY_REMOTE_INPUT_HISTORY, false));
Alex Hillsd9b04d92016-04-11 16:38:16 -04007765 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
7766 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007767 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
7768 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04007769 }
Shane Brennan5a871862017-03-11 13:14:17 -08007770 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
7771 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
7772 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007773 return message;
7774 }
7775 } catch (ClassCastException e) {
7776 return null;
7777 }
7778 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007779 }
7780 }
7781
7782 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04007783 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08007784 *
Robert Ly91c5ce32014-06-08 15:37:00 -07007785 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04007786 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07007787 * Notification notif = new Notification.Builder(mContext)
7788 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
7789 * .setContentText(subject)
7790 * .setSmallIcon(R.drawable.new_mail)
7791 * .setLargeIcon(aBitmap)
7792 * .setStyle(new Notification.InboxStyle()
7793 * .addLine(str1)
7794 * .addLine(str2)
7795 * .setContentTitle(&quot;&quot;)
7796 * .setSummaryText(&quot;+3 more&quot;))
7797 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04007798 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08007799 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04007800 * @see Notification#bigContentView
7801 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007802 public static class InboxStyle extends Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07007803
7804 /**
7805 * The number of lines of remote input history allowed until we start reducing lines.
7806 */
7807 private static final int NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION = 1;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007808 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
7809
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007810 public InboxStyle() {
7811 }
7812
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007813 /**
7814 * @deprecated use {@code InboxStyle()}.
7815 */
7816 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04007817 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007818 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04007819 }
7820
Chris Wrend6297db2012-05-03 16:20:13 -04007821 /**
7822 * Overrides ContentTitle in the big form of the template.
7823 * This defaults to the value passed to setContentTitle().
7824 */
7825 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007826 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04007827 return this;
7828 }
7829
7830 /**
7831 * Set the first line of text after the detail section in the big form of the template.
7832 */
7833 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007834 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04007835 return this;
7836 }
7837
Chris Wren0bd664d2012-08-01 13:56:56 -04007838 /**
7839 * Append a line to the digest section of the Inbox notification.
7840 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04007841 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007842 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04007843 return this;
7844 }
7845
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007846 /**
7847 * @hide
7848 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05007849 public ArrayList<CharSequence> getLines() {
7850 return mTexts;
7851 }
7852
7853 /**
7854 * @hide
7855 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007856 public void addExtras(Bundle extras) {
7857 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007858
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007859 CharSequence[] a = new CharSequence[mTexts.size()];
7860 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
7861 }
7862
Christoph Studer4600f9b2014-07-22 22:44:43 +02007863 /**
7864 * @hide
7865 */
7866 @Override
7867 protected void restoreFromExtras(Bundle extras) {
7868 super.restoreFromExtras(extras);
7869
7870 mTexts.clear();
7871 if (extras.containsKey(EXTRA_TEXT_LINES)) {
7872 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
7873 }
7874 }
7875
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007876 /**
7877 * @hide
7878 */
7879 public RemoteViews makeBigContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007880 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder).text(null);
Selim Cinek384804b2018-04-18 14:31:07 +08007881 TemplateBindResult result = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007882 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource(), p, result);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007883
Chris Wrend6297db2012-05-03 16:20:13 -04007884 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 -04007885 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04007886
Chris Wren4ed80d52012-05-17 09:30:03 -04007887 // Make sure all rows are gone in case we reuse a view.
7888 for (int rowId : rowIds) {
7889 contentView.setViewVisibility(rowId, View.GONE);
7890 }
7891
Daniel Sandler879c5e02012-04-17 16:46:51 -04007892 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07007893 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7894 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08007895 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07007896 int onlyViewId = 0;
7897 int maxRows = rowIds.length;
7898 if (mBuilder.mActions.size() > 0) {
7899 maxRows--;
7900 }
Selim Cinekbee4e072018-05-21 22:06:43 -07007901 CharSequence[] remoteInputHistory = mBuilder.mN.extras.getCharSequenceArray(
7902 EXTRA_REMOTE_INPUT_HISTORY);
7903 if (remoteInputHistory != null
7904 && remoteInputHistory.length > NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION) {
7905 // Let's remove some messages to make room for the remote input history.
7906 // 1 is always able to fit, but let's remove them if they are 2 or 3
7907 int numRemoteInputs = Math.min(remoteInputHistory.length,
7908 MAX_REMOTE_INPUT_HISTORY_LINES);
7909 int totalNumRows = mTexts.size() + numRemoteInputs
7910 - NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION;
7911 if (totalNumRows > maxRows) {
7912 int overflow = totalNumRows - maxRows;
7913 if (mTexts.size() > maxRows) {
7914 // Heuristic: if the Texts don't fit anyway, we'll rather drop the last
7915 // few messages, even with the remote input
7916 maxRows -= overflow;
7917 } else {
7918 // otherwise we drop the first messages
7919 i = overflow;
7920 }
7921 }
7922 }
Selim Cinek07c80172016-04-21 16:40:47 -07007923 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007924 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07007925 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007926 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07007927 contentView.setTextViewText(rowIds[i],
7928 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007929 mBuilder.setTextViewColorSecondary(contentView, rowIds[i], p);
Selim Cinek07c80172016-04-21 16:40:47 -07007930 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek384804b2018-04-18 14:31:07 +08007931 handleInboxImageMargin(contentView, rowIds[i], first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007932 result.getIconMarginEnd());
Selim Cinek07c80172016-04-21 16:40:47 -07007933 if (first) {
7934 onlyViewId = rowIds[i];
7935 } else {
7936 onlyViewId = 0;
7937 }
Selim Cinek247fa012016-02-18 09:50:48 -08007938 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007939 }
7940 i++;
7941 }
Selim Cinek07c80172016-04-21 16:40:47 -07007942 if (onlyViewId != 0) {
7943 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
7944 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7945 R.dimen.notification_text_margin_top);
7946 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
7947 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007948
Daniel Sandler879c5e02012-04-17 16:46:51 -04007949 return contentView;
7950 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007951
Julia Reynolds7217dc92018-03-07 12:12:09 -05007952 /**
7953 * @hide
7954 */
7955 @Override
7956 public boolean areNotificationsVisiblyDifferent(Style other) {
7957 if (other == null || getClass() != other.getClass()) {
7958 return true;
7959 }
7960 InboxStyle newS = (InboxStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04007961
7962 final ArrayList<CharSequence> myLines = getLines();
7963 final ArrayList<CharSequence> newLines = newS.getLines();
7964 final int n = myLines.size();
7965 if (n != newLines.size()) {
7966 return true;
7967 }
7968
7969 for (int i = 0; i < n; i++) {
7970 if (!Objects.equals(
7971 String.valueOf(myLines.get(i)),
7972 String.valueOf(newLines.get(i)))) {
7973 return true;
7974 }
7975 }
7976 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05007977 }
7978
Selim Cinek384804b2018-04-18 14:31:07 +08007979 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007980 int marginEndValue) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08007981 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08007982 if (first) {
7983 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
7984 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
7985 boolean hasProgress = max != 0 || ind;
Selim Cinek384804b2018-04-18 14:31:07 +08007986 if (!hasProgress) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08007987 endMargin = marginEndValue;
Selim Cinek247fa012016-02-18 09:50:48 -08007988 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007989 }
Selim Cinek1c72fa02018-04-23 18:00:54 +08007990 contentView.setViewLayoutMarginEnd(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08007991 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04007992 }
Dan Sandler842dd772014-05-15 09:36:47 -04007993
7994 /**
7995 * Notification style for media playback notifications.
7996 *
7997 * In the expanded form, {@link Notification#bigContentView}, up to 5
7998 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04007999 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04008000 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
8001 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
8002 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08008003 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04008004 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
8005 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01008006 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04008007 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08008008 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01008009 * Notifications created with MediaStyle will have their category set to
8010 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
8011 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08008012 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07008013 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
8014 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04008015 * the System UI can identify this as a notification representing an active media session
8016 * and respond accordingly (by showing album artwork in the lockscreen, for example).
8017 *
Selim Cinek99104832017-01-25 14:47:33 -08008018 * <p>
8019 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
8020 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
8021 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
8022 * <p>
8023 *
Dan Sandler842dd772014-05-15 09:36:47 -04008024 * To use this style with your Notification, feed it to
8025 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8026 * <pre class="prettyprint">
8027 * Notification noti = new Notification.Builder()
8028 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01008029 * .setContentTitle(&quot;Track title&quot;)
8030 * .setContentText(&quot;Artist - Album&quot;)
8031 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01008032 * .setStyle(<b>new Notification.MediaStyle()</b>
8033 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04008034 * .build();
8035 * </pre>
8036 *
8037 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08008038 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04008039 */
8040 public static class MediaStyle extends Style {
Gus Prevas9cc96602018-10-11 11:24:23 -04008041 // Changing max media buttons requires also changing templates
8042 // (notification_template_material_media and notification_template_material_big_media).
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008043 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04008044 static final int MAX_MEDIA_BUTTONS = 5;
Gus Prevas9cc96602018-10-11 11:24:23 -04008045 @IdRes private static final int[] MEDIA_BUTTON_IDS = {
8046 R.id.action0,
8047 R.id.action1,
8048 R.id.action2,
8049 R.id.action3,
8050 R.id.action4,
8051 };
Dan Sandler842dd772014-05-15 09:36:47 -04008052
8053 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07008054 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04008055
8056 public MediaStyle() {
8057 }
8058
Adrian Roosf5faf9d2016-05-23 13:56:15 -07008059 /**
8060 * @deprecated use {@code MediaStyle()}.
8061 */
8062 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04008063 public MediaStyle(Builder builder) {
8064 setBuilder(builder);
8065 }
8066
8067 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008068 * 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 -04008069 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008070 *
8071 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04008072 */
8073 public MediaStyle setShowActionsInCompactView(int...actions) {
8074 mActionsToShowInCompact = actions;
8075 return this;
8076 }
8077
8078 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07008079 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
8080 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04008081 */
Jeff Browndba34ba2014-06-24 20:46:03 -07008082 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04008083 mToken = token;
8084 return this;
8085 }
8086
Christoph Studer4600f9b2014-07-22 22:44:43 +02008087 /**
8088 * @hide
8089 */
Dan Sandler842dd772014-05-15 09:36:47 -04008090 @Override
Mathew Inwood61e8ae62018-08-14 14:17:44 +01008091 @UnsupportedAppUsage
Dan Sandler842dd772014-05-15 09:36:47 -04008092 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02008093 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01008094 if (wip.category == null) {
8095 wip.category = Notification.CATEGORY_TRANSPORT;
8096 }
Dan Sandler842dd772014-05-15 09:36:47 -04008097 return wip;
8098 }
8099
Christoph Studer4600f9b2014-07-22 22:44:43 +02008100 /**
8101 * @hide
8102 */
8103 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008104 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04008105 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02008106 }
8107
8108 /**
8109 * @hide
8110 */
8111 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04008112 public RemoteViews makeBigContentView() {
8113 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02008114 }
8115
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008116 /**
8117 * @hide
8118 */
8119 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008120 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008121 RemoteViews expanded = makeMediaBigContentView();
8122 return expanded != null ? expanded : makeMediaContentView();
8123 }
8124
Dan Sandler842dd772014-05-15 09:36:47 -04008125 /** @hide */
8126 @Override
8127 public void addExtras(Bundle extras) {
8128 super.addExtras(extras);
8129
8130 if (mToken != null) {
8131 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
8132 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01008133 if (mActionsToShowInCompact != null) {
8134 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
8135 }
Dan Sandler842dd772014-05-15 09:36:47 -04008136 }
8137
Christoph Studer4600f9b2014-07-22 22:44:43 +02008138 /**
8139 * @hide
8140 */
8141 @Override
8142 protected void restoreFromExtras(Bundle extras) {
8143 super.restoreFromExtras(extras);
8144
8145 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
8146 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
8147 }
8148 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
8149 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
8150 }
8151 }
8152
Julia Reynolds7217dc92018-03-07 12:12:09 -05008153 /**
8154 * @hide
8155 */
8156 @Override
8157 public boolean areNotificationsVisiblyDifferent(Style other) {
8158 if (other == null || getClass() != other.getClass()) {
8159 return true;
8160 }
8161 // All fields to compare are on the Notification object
8162 return false;
8163 }
8164
Gus Prevas9cc96602018-10-11 11:24:23 -04008165 private void bindMediaActionButton(RemoteViews container, @IdRes int buttonId,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008166 Action action, StandardTemplateParams p) {
Dan Sandler842dd772014-05-15 09:36:47 -04008167 final boolean tombstone = (action.actionIntent == null);
Gus Prevas9cc96602018-10-11 11:24:23 -04008168 container.setViewVisibility(buttonId, View.VISIBLE);
8169 container.setImageViewIcon(buttonId, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07008170
8171 // If the action buttons should not be tinted, then just use the default
8172 // notification color. Otherwise, just use the passed-in color.
Gus Prevas9cc96602018-10-11 11:24:23 -04008173 Resources resources = mBuilder.mContext.getResources();
8174 Configuration currentConfig = resources.getConfiguration();
Lucas Dupinf03e7522018-06-25 16:21:13 -07008175 boolean inNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
8176 == Configuration.UI_MODE_NIGHT_YES;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008177 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized(p)
8178 ? getActionColor(p)
Lucas Dupina291d192018-06-07 13:59:42 -07008179 : ContrastColorUtil.resolveColor(mBuilder.mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07008180 Notification.COLOR_DEFAULT, inNightMode);
Anthony Chenad4d1582017-04-10 16:07:58 -07008181
Gus Prevas9cc96602018-10-11 11:24:23 -04008182 container.setDrawableTint(buttonId, false, tintColor,
Sunny Goyal5b153922017-09-21 21:00:36 -07008183 PorterDuff.Mode.SRC_ATOP);
Gus Prevas9cc96602018-10-11 11:24:23 -04008184
8185 final TypedArray typedArray = mBuilder.mContext.obtainStyledAttributes(
8186 new int[]{ android.R.attr.colorControlHighlight });
8187 int rippleAlpha = Color.alpha(typedArray.getColor(0, 0));
8188 typedArray.recycle();
8189 int rippleColor = Color.argb(rippleAlpha, Color.red(tintColor), Color.green(tintColor),
8190 Color.blue(tintColor));
8191 container.setRippleDrawableColor(buttonId, ColorStateList.valueOf(rippleColor));
8192
Dan Sandler842dd772014-05-15 09:36:47 -04008193 if (!tombstone) {
Gus Prevas9cc96602018-10-11 11:24:23 -04008194 container.setOnClickPendingIntent(buttonId, action.actionIntent);
Dan Sandler842dd772014-05-15 09:36:47 -04008195 }
Gus Prevas9cc96602018-10-11 11:24:23 -04008196 container.setContentDescription(buttonId, action.title);
Dan Sandler842dd772014-05-15 09:36:47 -04008197 }
8198
8199 private RemoteViews makeMediaContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008200 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).fillTextsFrom(
8201 mBuilder);
Dan Sandler842dd772014-05-15 09:36:47 -04008202 RemoteViews view = mBuilder.applyStandardTemplate(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008203 R.layout.notification_template_material_media, p,
Selim Cinek384804b2018-04-18 14:31:07 +08008204 null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04008205
8206 final int numActions = mBuilder.mActions.size();
Gus Prevas9cc96602018-10-11 11:24:23 -04008207 final int numActionsToShow = mActionsToShowInCompact == null
Dan Sandler842dd772014-05-15 09:36:47 -04008208 ? 0
8209 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Gus Prevas9cc96602018-10-11 11:24:23 -04008210 if (numActionsToShow > numActions) {
8211 throw new IllegalArgumentException(String.format(
8212 "setShowActionsInCompactView: action %d out of bounds (max %d)",
8213 numActions, numActions - 1));
8214 }
8215 for (int i = 0; i < MAX_MEDIA_BUTTONS_IN_COMPACT; i++) {
8216 if (i < numActionsToShow) {
Dan Sandler842dd772014-05-15 09:36:47 -04008217 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008218 bindMediaActionButton(view, MEDIA_BUTTON_IDS[i], action, p);
Gus Prevas9cc96602018-10-11 11:24:23 -04008219 } else {
8220 view.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04008221 }
8222 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08008223 handleImage(view);
8224 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008225 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07008226 if (mBuilder.mN.hasLargeIcon()) {
Selim Cinek384804b2018-04-18 14:31:07 +08008227 endMargin = R.dimen.notification_media_image_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08008228 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008229 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04008230 return view;
8231 }
8232
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008233 private int getActionColor(StandardTemplateParams p) {
8234 return mBuilder.isColorized(p) ? mBuilder.getPrimaryTextColor(p)
8235 : mBuilder.resolveContrastColor(p);
Selim Cinek99104832017-01-25 14:47:33 -08008236 }
8237
Dan Sandler842dd772014-05-15 09:36:47 -04008238 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008239 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008240 // Dont add an expanded view if there is no more content to be revealed
8241 int actionsInCompact = mActionsToShowInCompact == null
8242 ? 0
8243 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07008244 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008245 return null;
8246 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008247 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).fillTextsFrom(
8248 mBuilder);
Selim Cinek5bf069a2015-11-10 19:14:27 -05008249 RemoteViews big = mBuilder.applyStandardTemplate(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008250 R.layout.notification_template_material_big_media, p , null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04008251
Gus Prevas9cc96602018-10-11 11:24:23 -04008252 for (int i = 0; i < MAX_MEDIA_BUTTONS; i++) {
8253 if (i < actionCount) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008254 bindMediaActionButton(big, MEDIA_BUTTON_IDS[i], mBuilder.mActions.get(i), p);
Gus Prevas9cc96602018-10-11 11:24:23 -04008255 } else {
8256 big.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04008257 }
8258 }
linanson2bcd4032019-01-14 15:22:04 +08008259 bindMediaActionButton(big, R.id.media_seamless, new Action(R.drawable.ic_media_seamless,
8260 mBuilder.mContext.getString(
8261 com.android.internal.R.string.ext_media_seamless_action), null), p);
8262 big.setViewVisibility(R.id.media_seamless, View.GONE);
Selim Cinek5bf069a2015-11-10 19:14:27 -05008263 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04008264 return big;
8265 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008266
Selim Cinek5bf069a2015-11-10 19:14:27 -05008267 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07008268 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008269 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
8270 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008271 }
8272 }
8273
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008274 /**
8275 * @hide
8276 */
8277 @Override
8278 protected boolean hasProgress() {
8279 return false;
8280 }
Dan Sandler842dd772014-05-15 09:36:47 -04008281 }
Griff Hazen61a9e862014-05-22 16:05:19 -07008282
Selim Cinek593610c2016-02-16 18:42:57 -08008283 /**
8284 * Notification style for custom views that are decorated by the system
8285 *
8286 * <p>Instead of providing a notification that is completely custom, a developer can set this
8287 * style and still obtain system decorations like the notification header with the expand
8288 * affordance and actions.
8289 *
8290 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8291 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8292 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8293 * corresponding custom views to display.
8294 *
8295 * To use this style with your Notification, feed it to
8296 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8297 * <pre class="prettyprint">
8298 * Notification noti = new Notification.Builder()
8299 * .setSmallIcon(R.drawable.ic_stat_player)
8300 * .setLargeIcon(albumArtBitmap))
8301 * .setCustomContentView(contentView);
8302 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
8303 * .build();
8304 * </pre>
8305 */
8306 public static class DecoratedCustomViewStyle extends Style {
8307
8308 public DecoratedCustomViewStyle() {
8309 }
8310
Selim Cinek593610c2016-02-16 18:42:57 -08008311 /**
8312 * @hide
8313 */
8314 public boolean displayCustomViewInline() {
8315 return true;
8316 }
8317
8318 /**
8319 * @hide
8320 */
8321 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008322 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008323 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
8324 }
8325
8326 /**
8327 * @hide
8328 */
8329 @Override
8330 public RemoteViews makeBigContentView() {
8331 return makeDecoratedBigContentView();
8332 }
8333
8334 /**
8335 * @hide
8336 */
8337 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008338 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008339 return makeDecoratedHeadsUpContentView();
8340 }
8341
Selim Cinek593610c2016-02-16 18:42:57 -08008342 private RemoteViews makeDecoratedHeadsUpContentView() {
8343 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
8344 ? mBuilder.mN.contentView
8345 : mBuilder.mN.headsUpContentView;
8346 if (mBuilder.mActions.size() == 0) {
8347 return makeStandardTemplateWithCustomContent(headsUpContentView);
8348 }
Selim Cinek384804b2018-04-18 14:31:07 +08008349 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008350 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008351 mBuilder.getBigBaseLayoutResource(), result);
8352 buildIntoRemoteViewContent(remoteViews, headsUpContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008353 return remoteViews;
8354 }
8355
Selim Cinek593610c2016-02-16 18:42:57 -08008356 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
Selim Cinek384804b2018-04-18 14:31:07 +08008357 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008358 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08008359 mBuilder.getBaseLayoutResource(), result);
8360 buildIntoRemoteViewContent(remoteViews, customContent, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008361 return remoteViews;
8362 }
8363
Selim Cinek593610c2016-02-16 18:42:57 -08008364 private RemoteViews makeDecoratedBigContentView() {
8365 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
8366 ? mBuilder.mN.contentView
8367 : mBuilder.mN.bigContentView;
8368 if (mBuilder.mActions.size() == 0) {
8369 return makeStandardTemplateWithCustomContent(bigContentView);
8370 }
Selim Cinek384804b2018-04-18 14:31:07 +08008371 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008372 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008373 mBuilder.getBigBaseLayoutResource(), result);
8374 buildIntoRemoteViewContent(remoteViews, bigContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008375 return remoteViews;
8376 }
Selim Cinek247fa012016-02-18 09:50:48 -08008377
8378 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
Selim Cinek384804b2018-04-18 14:31:07 +08008379 RemoteViews customContent, TemplateBindResult result) {
Selim Cinek6fe4a102019-01-25 14:38:24 -08008380 int childIndex = -1;
Adrian Roos5081c0d2016-02-26 16:04:19 -08008381 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008382 // Need to clone customContent before adding, because otherwise it can no longer be
8383 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008384 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07008385 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
8386 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Sunny Goyalc12d31c2018-11-12 16:29:18 -08008387 remoteViews.addFlags(RemoteViews.FLAG_REAPPLY_DISALLOWED);
Selim Cinek6fe4a102019-01-25 14:38:24 -08008388 childIndex = 0;
Adrian Roos5081c0d2016-02-26 16:04:19 -08008389 }
Selim Cinek6fe4a102019-01-25 14:38:24 -08008390 remoteViews.setIntTag(R.id.notification_main_column,
8391 com.android.internal.R.id.notification_custom_view_index_tag,
8392 childIndex);
Selim Cinek247fa012016-02-18 09:50:48 -08008393 // also update the end margin if there is an image
Selim Cinek384804b2018-04-18 14:31:07 +08008394 Resources resources = mBuilder.mContext.getResources();
8395 int endMargin = resources.getDimensionPixelSize(
Selim Cinek1c72fa02018-04-23 18:00:54 +08008396 R.dimen.notification_content_margin_end) + result.getIconMarginEnd();
Selim Cinek384804b2018-04-18 14:31:07 +08008397 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08008398 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05008399
8400 /**
8401 * @hide
8402 */
8403 @Override
8404 public boolean areNotificationsVisiblyDifferent(Style other) {
8405 if (other == null || getClass() != other.getClass()) {
8406 return true;
8407 }
8408 // Comparison done for all custom RemoteViews, independent of style
8409 return false;
8410 }
Selim Cinek593610c2016-02-16 18:42:57 -08008411 }
8412
Selim Cinek03eb3b72016-02-18 10:39:45 -08008413 /**
8414 * Notification style for media custom views that are decorated by the system
8415 *
8416 * <p>Instead of providing a media notification that is completely custom, a developer can set
8417 * this style and still obtain system decorations like the notification header with the expand
8418 * affordance and actions.
8419 *
8420 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8421 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8422 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8423 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08008424 * <p>
8425 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
8426 * notification by using {@link Notification.Builder#setColorized(boolean)}.
8427 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08008428 * To use this style with your Notification, feed it to
8429 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8430 * <pre class="prettyprint">
8431 * Notification noti = new Notification.Builder()
8432 * .setSmallIcon(R.drawable.ic_stat_player)
8433 * .setLargeIcon(albumArtBitmap))
8434 * .setCustomContentView(contentView);
8435 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
8436 * .setMediaSession(mySession))
8437 * .build();
8438 * </pre>
8439 *
8440 * @see android.app.Notification.DecoratedCustomViewStyle
8441 * @see android.app.Notification.MediaStyle
8442 */
8443 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
8444
8445 public DecoratedMediaCustomViewStyle() {
8446 }
8447
Selim Cinek03eb3b72016-02-18 10:39:45 -08008448 /**
8449 * @hide
8450 */
8451 public boolean displayCustomViewInline() {
8452 return true;
8453 }
8454
8455 /**
8456 * @hide
8457 */
8458 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008459 public RemoteViews makeContentView(boolean increasedHeight) {
8460 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008461 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8462 mBuilder.mN.contentView);
8463 }
8464
8465 /**
8466 * @hide
8467 */
8468 @Override
8469 public RemoteViews makeBigContentView() {
8470 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
8471 ? mBuilder.mN.bigContentView
8472 : mBuilder.mN.contentView;
8473 return makeBigContentViewWithCustomContent(customRemoteView);
8474 }
8475
8476 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
8477 RemoteViews remoteViews = super.makeBigContentView();
8478 if (remoteViews != null) {
8479 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
8480 customRemoteView);
8481 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08008482 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008483 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8484 customRemoteView);
8485 } else {
8486 return null;
8487 }
8488 }
8489
8490 /**
8491 * @hide
8492 */
8493 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008494 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08008495 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
8496 ? mBuilder.mN.headsUpContentView
8497 : mBuilder.mN.contentView;
8498 return makeBigContentViewWithCustomContent(customRemoteView);
8499 }
8500
Julia Reynolds7217dc92018-03-07 12:12:09 -05008501 /**
8502 * @hide
8503 */
8504 @Override
8505 public boolean areNotificationsVisiblyDifferent(Style other) {
8506 if (other == null || getClass() != other.getClass()) {
8507 return true;
8508 }
8509 // Comparison done for all custom RemoteViews, independent of style
8510 return false;
8511 }
8512
Selim Cinek03eb3b72016-02-18 10:39:45 -08008513 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
8514 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08008515 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008516 // Need to clone customContent before adding, because otherwise it can no longer be
8517 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008518 customContent = customContent.clone();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008519 customContent.overrideTextColors(mBuilder.getPrimaryTextColor(mBuilder.mParams));
Selim Cinekf91017e2016-03-14 12:25:09 -07008520 remoteViews.removeAllViews(id);
8521 remoteViews.addView(id, customContent);
Sunny Goyalc12d31c2018-11-12 16:29:18 -08008522 remoteViews.addFlags(RemoteViews.FLAG_REAPPLY_DISALLOWED);
Adrian Roos5081c0d2016-02-26 16:04:19 -08008523 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08008524 return remoteViews;
8525 }
8526 }
8527
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008528 /**
8529 * Encapsulates the information needed to display a notification as a bubble.
8530 *
8531 * <p>A bubble is used to display app content in a floating window over the existing
8532 * foreground activity. A bubble has a collapsed state represented by an icon,
8533 * {@link BubbleMetadata.Builder#setIcon(Icon)} and an expanded state which is populated
8534 * via {@link BubbleMetadata.Builder#setIntent(PendingIntent)}.</p>
8535 *
8536 * <b>Notifications with a valid and allowed bubble will display in collapsed state
8537 * outside of the notification shade on unlocked devices. When a user interacts with the
8538 * collapsed bubble, the bubble intent will be invoked and displayed.</b>
8539 *
8540 * @see Notification.Builder#setBubbleMetadata(BubbleMetadata)
8541 */
8542 public static final class BubbleMetadata implements Parcelable {
8543
8544 private PendingIntent mPendingIntent;
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008545 private PendingIntent mDeleteIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008546 private Icon mIcon;
8547 private int mDesiredHeight;
Mady Mellor7af771a2019-03-07 15:04:54 -08008548 @DimenRes private int mDesiredHeightResId;
Mady Mellor0157ff22019-01-24 15:31:48 -08008549 private int mFlags;
8550
8551 /**
8552 * If set and the app creating the bubble is in the foreground, the bubble will be posted
8553 * in its expanded state, with the contents of {@link #getIntent()} in a floating window.
8554 *
8555 * <p>If the app creating the bubble is not in the foreground this flag has no effect.</p>
8556 *
8557 * <p>Generally this flag should only be set if the user has performed an action to request
8558 * or create a bubble.</p>
8559 */
8560 private static final int FLAG_AUTO_EXPAND_BUBBLE = 0x00000001;
8561
8562 /**
Mady Mellor8a1f0252019-04-01 11:31:34 -07008563 * If set and the app posting the bubble is in the foreground, the bubble will
8564 * be posted <b>without</b> the associated notification in the notification shade.
Mady Mellor0157ff22019-01-24 15:31:48 -08008565 *
Mady Mellor8a1f0252019-04-01 11:31:34 -07008566 * <p>If the app posting the bubble is not in the foreground this flag has no effect.</p>
Mady Mellor0157ff22019-01-24 15:31:48 -08008567 *
8568 * <p>Generally this flag should only be set if the user has performed an action to request
Mady Mellor8a1f0252019-04-01 11:31:34 -07008569 * or create a bubble, or if the user has seen the content in the notification and the
8570 * notification is no longer relevant.</p>
Mady Mellor0157ff22019-01-24 15:31:48 -08008571 */
Mady Mellor8a1f0252019-04-01 11:31:34 -07008572 private static final int FLAG_SUPPRESS_NOTIFICATION = 0x00000002;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008573
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008574 private BubbleMetadata(PendingIntent expandIntent, PendingIntent deleteIntent,
Mady Mellor7af771a2019-03-07 15:04:54 -08008575 Icon icon, int height, @DimenRes int heightResId) {
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008576 mPendingIntent = expandIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008577 mIcon = icon;
8578 mDesiredHeight = height;
Mady Mellor7af771a2019-03-07 15:04:54 -08008579 mDesiredHeightResId = heightResId;
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008580 mDeleteIntent = deleteIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008581 }
8582
8583 private BubbleMetadata(Parcel in) {
8584 mPendingIntent = PendingIntent.CREATOR.createFromParcel(in);
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008585 mIcon = Icon.CREATOR.createFromParcel(in);
8586 mDesiredHeight = in.readInt();
Mady Mellor0157ff22019-01-24 15:31:48 -08008587 mFlags = in.readInt();
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008588 if (in.readInt() != 0) {
8589 mDeleteIntent = PendingIntent.CREATOR.createFromParcel(in);
8590 }
Mady Mellor7af771a2019-03-07 15:04:54 -08008591 mDesiredHeightResId = in.readInt();
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008592 }
8593
8594 /**
8595 * @return the pending intent used to populate the floating window for this bubble.
8596 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008597 @NonNull
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008598 public PendingIntent getIntent() {
8599 return mPendingIntent;
8600 }
8601
8602 /**
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008603 * @return the pending intent to send when the bubble is dismissed by a user, if one exists.
8604 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008605 @Nullable
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008606 public PendingIntent getDeleteIntent() {
8607 return mDeleteIntent;
8608 }
8609
8610 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008611 * @return the icon that will be displayed for this bubble when it is collapsed.
8612 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008613 @NonNull
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008614 public Icon getIcon() {
8615 return mIcon;
8616 }
8617
8618 /**
Mady Mellor7af771a2019-03-07 15:04:54 -08008619 * @return the ideal height, in DPs, for the floating window that app content defined by
Mady Mellor8a529e22019-03-25 17:37:55 -07008620 * {@link #getIntent()} for this bubble. A value of 0 indicates a desired height has not
8621 * been set.
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008622 */
Mady Mellor8a529e22019-03-25 17:37:55 -07008623 @Dimension(unit = DP)
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008624 public int getDesiredHeight() {
8625 return mDesiredHeight;
8626 }
8627
Mady Mellor0157ff22019-01-24 15:31:48 -08008628 /**
Mady Mellor7af771a2019-03-07 15:04:54 -08008629 * @return the resId of ideal height for the floating window that app content defined by
Mady Mellor8a529e22019-03-25 17:37:55 -07008630 * {@link #getIntent()} for this bubble. A value of 0 indicates a res value has not
8631 * been provided for the desired height.
Mady Mellor7af771a2019-03-07 15:04:54 -08008632 */
8633 @DimenRes
8634 public int getDesiredHeightResId() {
8635 return mDesiredHeightResId;
8636 }
8637
8638 /**
Mady Mellor0157ff22019-01-24 15:31:48 -08008639 * @return whether this bubble should auto expand when it is posted.
8640 *
8641 * @see BubbleMetadata.Builder#setAutoExpandBubble(boolean)
8642 */
8643 public boolean getAutoExpandBubble() {
8644 return (mFlags & FLAG_AUTO_EXPAND_BUBBLE) != 0;
8645 }
8646
8647 /**
Mady Mellorc529d6d2019-04-16 14:22:52 -07008648 * @return whether this bubble should suppress the notification when it is posted.
8649 *
8650 * @see BubbleMetadata.Builder#setSuppressNotification(boolean)
8651 */
8652 public boolean isNotificationSuppressed() {
Mady Mellor8a1f0252019-04-01 11:31:34 -07008653 return (mFlags & FLAG_SUPPRESS_NOTIFICATION) != 0;
Mady Mellor0157ff22019-01-24 15:31:48 -08008654 }
8655
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07008656 public static final @android.annotation.NonNull Parcelable.Creator<BubbleMetadata> CREATOR =
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008657 new Parcelable.Creator<BubbleMetadata>() {
8658
8659 @Override
8660 public BubbleMetadata createFromParcel(Parcel source) {
8661 return new BubbleMetadata(source);
8662 }
8663
8664 @Override
8665 public BubbleMetadata[] newArray(int size) {
8666 return new BubbleMetadata[size];
8667 }
8668 };
8669
8670 @Override
8671 public int describeContents() {
8672 return 0;
8673 }
8674
8675 @Override
8676 public void writeToParcel(Parcel out, int flags) {
8677 mPendingIntent.writeToParcel(out, 0);
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008678 mIcon.writeToParcel(out, 0);
8679 out.writeInt(mDesiredHeight);
Mady Mellor0157ff22019-01-24 15:31:48 -08008680 out.writeInt(mFlags);
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008681 out.writeInt(mDeleteIntent != null ? 1 : 0);
8682 if (mDeleteIntent != null) {
8683 mDeleteIntent.writeToParcel(out, 0);
8684 }
Mady Mellor7af771a2019-03-07 15:04:54 -08008685 out.writeInt(mDesiredHeightResId);
Mady Mellor0157ff22019-01-24 15:31:48 -08008686 }
8687
8688 private void setFlags(int flags) {
8689 mFlags = flags;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008690 }
8691
8692 /**
8693 * Builder to construct a {@link BubbleMetadata} object.
8694 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008695 public static final class Builder {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008696
8697 private PendingIntent mPendingIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008698 private Icon mIcon;
8699 private int mDesiredHeight;
Mady Mellor7af771a2019-03-07 15:04:54 -08008700 @DimenRes private int mDesiredHeightResId;
Mady Mellor0157ff22019-01-24 15:31:48 -08008701 private int mFlags;
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008702 private PendingIntent mDeleteIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008703
8704 /**
8705 * Constructs a new builder object.
8706 */
8707 public Builder() {
8708 }
8709
8710 /**
8711 * Sets the intent that will be used when the bubble is expanded. This will display the
8712 * app content in a floating window over the existing foreground activity.
8713 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008714 @NonNull
8715 public BubbleMetadata.Builder setIntent(@NonNull PendingIntent intent) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008716 if (intent == null) {
8717 throw new IllegalArgumentException("Bubble requires non-null pending intent");
8718 }
8719 mPendingIntent = intent;
8720 return this;
8721 }
8722
8723 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008724 * Sets the icon that will represent the bubble when it is collapsed.
8725 *
8726 * <p>An icon is required and should be representative of the content within the bubble.
8727 * If your app produces multiple bubbles, the image should be unique for each of them.
8728 * </p>
Mady Mellor9848a6c2019-03-19 15:29:05 -07008729 *
8730 * <p>The shape of a bubble icon is adaptive and can match the device theme.
8731 *
8732 * If your icon is bitmap-based, you should create it using
8733 * {@link Icon#createWithAdaptiveBitmap(Bitmap)}, otherwise this method will throw.
8734 *
8735 * If your icon is not bitmap-based, you should expect that the icon will be tinted.
8736 * </p>
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008737 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008738 @NonNull
8739 public BubbleMetadata.Builder setIcon(@NonNull Icon icon) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008740 if (icon == null) {
8741 throw new IllegalArgumentException("Bubbles require non-null icon");
8742 }
Mady Mellor9848a6c2019-03-19 15:29:05 -07008743 if (icon.getType() == TYPE_BITMAP) {
8744 throw new IllegalArgumentException("When using bitmap based icons, Bubbles "
8745 + "require TYPE_ADAPTIVE_BITMAP, please use"
8746 + " Icon#createWithAdaptiveBitmap instead");
8747 }
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008748 mIcon = icon;
8749 return this;
8750 }
8751
8752 /**
Mady Mellor7af771a2019-03-07 15:04:54 -08008753 * Sets the desired height in DPs for the app content defined by
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008754 * {@link #setIntent(PendingIntent)}, this height may not be respected if there is not
8755 * enough space on the screen or if the provided height is too small to be useful.
Mady Mellor7af771a2019-03-07 15:04:54 -08008756 * <p>
8757 * If {@link #setDesiredHeightResId(int)} was previously called on this builder, the
8758 * previous value set will be cleared after calling this method, and this value will
8759 * be used instead.
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008760 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008761 @NonNull
Mady Mellor8a529e22019-03-25 17:37:55 -07008762 public BubbleMetadata.Builder setDesiredHeight(@Dimension(unit = DP) int height) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008763 mDesiredHeight = Math.max(height, 0);
Mady Mellor7af771a2019-03-07 15:04:54 -08008764 mDesiredHeightResId = 0;
8765 return this;
8766 }
8767
8768
8769 /**
8770 * Sets the desired height via resId for the app content defined by
8771 * {@link #setIntent(PendingIntent)}, this height may not be respected if there is not
8772 * enough space on the screen or if the provided height is too small to be useful.
8773 * <p>
8774 * If {@link #setDesiredHeight(int)} was previously called on this builder, the
8775 * previous value set will be cleared after calling this method, and this value will
8776 * be used instead.
8777 */
8778 @NonNull
8779 public BubbleMetadata.Builder setDesiredHeightResId(@DimenRes int heightResId) {
8780 mDesiredHeightResId = heightResId;
8781 mDesiredHeight = 0;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008782 return this;
8783 }
8784
8785 /**
Mady Mellor0157ff22019-01-24 15:31:48 -08008786 * If set and the app creating the bubble is in the foreground, the bubble will be
8787 * posted in its expanded state, with the contents of {@link #getIntent()} in a
8788 * floating window.
8789 *
8790 * <p>If the app creating the bubble is not in the foreground this flag has no effect.
8791 * </p>
8792 *
8793 * <p>Generally this flag should only be set if the user has performed an action to
8794 * request or create a bubble.</p>
8795 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008796 @NonNull
Mady Mellor0157ff22019-01-24 15:31:48 -08008797 public BubbleMetadata.Builder setAutoExpandBubble(boolean shouldExpand) {
8798 setFlag(FLAG_AUTO_EXPAND_BUBBLE, shouldExpand);
8799 return this;
8800 }
8801
8802 /**
Mady Mellor8a1f0252019-04-01 11:31:34 -07008803 * If set and the app posting the bubble is in the foreground, the bubble will be
8804 * posted <b>without</b> the associated notification in the notification shade.
8805 *
8806 * <p>If the app posting the bubble is not in the foreground this flag has no effect.
8807 * </p>
8808 *
8809 * <p>Generally this flag should only be set if the user has performed an action to
8810 * request or create a bubble, or if the user has seen the content in the notification
8811 * and the notification is no longer relevant.</p>
8812 */
8813 @NonNull
8814 public BubbleMetadata.Builder setSuppressNotification(boolean shouldSupressNotif) {
8815 setFlag(FLAG_SUPPRESS_NOTIFICATION, shouldSupressNotif);
Mady Mellor0157ff22019-01-24 15:31:48 -08008816 return this;
8817 }
8818
8819 /**
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008820 * Sets an optional intent to send when this bubble is explicitly removed by the user.
8821 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008822 @NonNull
8823 public BubbleMetadata.Builder setDeleteIntent(@Nullable PendingIntent deleteIntent) {
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008824 mDeleteIntent = deleteIntent;
8825 return this;
8826 }
8827
8828 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008829 * Creates the {@link BubbleMetadata} defined by this builder.
8830 * <p>Will throw {@link IllegalStateException} if required fields have not been set
8831 * on this builder.</p>
8832 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008833 @NonNull
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008834 public BubbleMetadata build() {
8835 if (mPendingIntent == null) {
8836 throw new IllegalStateException("Must supply pending intent to bubble");
8837 }
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008838 if (mIcon == null) {
8839 throw new IllegalStateException("Must supply an icon for the bubble");
8840 }
Mady Mellor138f4c92019-02-20 16:12:17 -08008841 BubbleMetadata data = new BubbleMetadata(mPendingIntent, mDeleteIntent,
Mady Mellor7af771a2019-03-07 15:04:54 -08008842 mIcon, mDesiredHeight, mDesiredHeightResId);
Mady Mellor0157ff22019-01-24 15:31:48 -08008843 data.setFlags(mFlags);
8844 return data;
8845 }
8846
8847 /**
8848 * @hide
8849 */
8850 public BubbleMetadata.Builder setFlag(int mask, boolean value) {
8851 if (value) {
8852 mFlags |= mask;
8853 } else {
8854 mFlags &= ~mask;
8855 }
8856 return this;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008857 }
8858 }
8859 }
8860
8861
Christoph Studer4600f9b2014-07-22 22:44:43 +02008862 // When adding a new Style subclass here, don't forget to update
8863 // Builder.getNotificationStyleClass.
8864
Griff Hazen61a9e862014-05-22 16:05:19 -07008865 /**
8866 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
8867 * metadata or change options on a notification builder.
8868 */
8869 public interface Extender {
8870 /**
8871 * Apply this extender to a notification builder.
8872 * @param builder the builder to be modified.
8873 * @return the build object for chaining.
8874 */
8875 public Builder extend(Builder builder);
8876 }
8877
8878 /**
8879 * Helper class to add wearable extensions to notifications.
8880 * <p class="note"> See
8881 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
8882 * for Android Wear</a> for more information on how to use this class.
8883 * <p>
8884 * To create a notification with wearable extensions:
8885 * <ol>
8886 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
8887 * properties.
8888 * <li>Create a {@link android.app.Notification.WearableExtender}.
8889 * <li>Set wearable-specific properties using the
8890 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
8891 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
8892 * notification.
8893 * <li>Post the notification to the notification system with the
8894 * {@code NotificationManager.notify(...)} methods.
8895 * </ol>
8896 *
8897 * <pre class="prettyprint">
8898 * Notification notif = new Notification.Builder(mContext)
8899 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
8900 * .setContentText(subject)
8901 * .setSmallIcon(R.drawable.new_mail)
8902 * .extend(new Notification.WearableExtender()
8903 * .setContentIcon(R.drawable.new_mail))
8904 * .build();
8905 * NotificationManager notificationManger =
8906 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
8907 * notificationManger.notify(0, notif);</pre>
8908 *
8909 * <p>Wearable extensions can be accessed on an existing notification by using the
8910 * {@code WearableExtender(Notification)} constructor,
8911 * and then using the {@code get} methods to access values.
8912 *
8913 * <pre class="prettyprint">
8914 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
8915 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07008916 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008917 */
8918 public static final class WearableExtender implements Extender {
8919 /**
8920 * Sentinel value for an action index that is unset.
8921 */
8922 public static final int UNSET_ACTION_INDEX = -1;
8923
8924 /**
8925 * Size value for use with {@link #setCustomSizePreset} to show this notification with
8926 * default sizing.
8927 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07008928 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07008929 * on their content.
Gus Prevasd7363752018-09-18 14:35:15 -04008930 *
8931 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008932 */
Gus Prevasd7363752018-09-18 14:35:15 -04008933 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008934 public static final int SIZE_DEFAULT = 0;
8935
8936 /**
8937 * Size value for use with {@link #setCustomSizePreset} to show this notification
8938 * with an extra small size.
8939 * <p>This value is only applicable for custom display notifications created using
8940 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008941 *
8942 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008943 */
Gus Prevasd7363752018-09-18 14:35:15 -04008944 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008945 public static final int SIZE_XSMALL = 1;
8946
8947 /**
8948 * Size value for use with {@link #setCustomSizePreset} to show this notification
8949 * with a small size.
8950 * <p>This value is only applicable for custom display notifications created using
8951 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008952 *
8953 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008954 */
Gus Prevasd7363752018-09-18 14:35:15 -04008955 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008956 public static final int SIZE_SMALL = 2;
8957
8958 /**
8959 * Size value for use with {@link #setCustomSizePreset} to show this notification
8960 * with a medium size.
8961 * <p>This value is only applicable for custom display notifications created using
8962 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008963 *
8964 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008965 */
Gus Prevasd7363752018-09-18 14:35:15 -04008966 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008967 public static final int SIZE_MEDIUM = 3;
8968
8969 /**
8970 * Size value for use with {@link #setCustomSizePreset} to show this notification
8971 * with a large size.
8972 * <p>This value is only applicable for custom display notifications created using
8973 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008974 *
8975 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008976 */
Gus Prevasd7363752018-09-18 14:35:15 -04008977 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008978 public static final int SIZE_LARGE = 4;
8979
Griff Hazend5f11f92014-05-27 15:40:09 -07008980 /**
8981 * Size value for use with {@link #setCustomSizePreset} to show this notification
8982 * full screen.
8983 * <p>This value is only applicable for custom display notifications created using
8984 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008985 *
8986 * @deprecated Display intents are no longer supported.
Griff Hazend5f11f92014-05-27 15:40:09 -07008987 */
Gus Prevasd7363752018-09-18 14:35:15 -04008988 @Deprecated
Griff Hazend5f11f92014-05-27 15:40:09 -07008989 public static final int SIZE_FULL_SCREEN = 5;
8990
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008991 /**
8992 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
8993 * short amount of time when this notification is displayed on the screen. This
8994 * is the default value.
Gus Prevasd7363752018-09-18 14:35:15 -04008995 *
8996 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008997 */
Gus Prevasd7363752018-09-18 14:35:15 -04008998 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008999 public static final int SCREEN_TIMEOUT_SHORT = 0;
9000
9001 /**
9002 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
9003 * for a longer amount of time when this notification is displayed on the screen.
Gus Prevasd7363752018-09-18 14:35:15 -04009004 *
9005 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009006 */
Gus Prevasd7363752018-09-18 14:35:15 -04009007 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009008 public static final int SCREEN_TIMEOUT_LONG = -1;
9009
Griff Hazen61a9e862014-05-22 16:05:19 -07009010 /** Notification extra which contains wearable extensions */
9011 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
9012
Pete Gastaf6781d2014-10-07 15:17:05 -04009013 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07009014 private static final String KEY_ACTIONS = "actions";
9015 private static final String KEY_FLAGS = "flags";
9016 private static final String KEY_DISPLAY_INTENT = "displayIntent";
9017 private static final String KEY_PAGES = "pages";
9018 private static final String KEY_BACKGROUND = "background";
9019 private static final String KEY_CONTENT_ICON = "contentIcon";
9020 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
9021 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
9022 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
9023 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
9024 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009025 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04009026 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009027 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07009028
9029 // Flags bitwise-ored to mFlags
9030 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
9031 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
9032 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
9033 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009034 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04009035 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04009036 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07009037
9038 // Default value for flags integer
9039 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
9040
9041 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
9042 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
9043
9044 private ArrayList<Action> mActions = new ArrayList<Action>();
9045 private int mFlags = DEFAULT_FLAGS;
9046 private PendingIntent mDisplayIntent;
9047 private ArrayList<Notification> mPages = new ArrayList<Notification>();
9048 private Bitmap mBackground;
9049 private int mContentIcon;
9050 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
9051 private int mContentActionIndex = UNSET_ACTION_INDEX;
9052 private int mCustomSizePreset = SIZE_DEFAULT;
9053 private int mCustomContentHeight;
9054 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009055 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04009056 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009057 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07009058
9059 /**
9060 * Create a {@link android.app.Notification.WearableExtender} with default
9061 * options.
9062 */
9063 public WearableExtender() {
9064 }
9065
9066 public WearableExtender(Notification notif) {
9067 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
9068 if (wearableBundle != null) {
9069 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
9070 if (actions != null) {
9071 mActions.addAll(actions);
9072 }
9073
9074 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
9075 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
9076
9077 Notification[] pages = getNotificationArrayFromBundle(
9078 wearableBundle, KEY_PAGES);
9079 if (pages != null) {
9080 Collections.addAll(mPages, pages);
9081 }
9082
9083 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
9084 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
9085 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
9086 DEFAULT_CONTENT_ICON_GRAVITY);
9087 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
9088 UNSET_ACTION_INDEX);
9089 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
9090 SIZE_DEFAULT);
9091 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
9092 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009093 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04009094 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009095 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07009096 }
9097 }
9098
9099 /**
9100 * Apply wearable extensions to a notification that is being built. This is typically
9101 * called by the {@link android.app.Notification.Builder#extend} method of
9102 * {@link android.app.Notification.Builder}.
9103 */
9104 @Override
9105 public Notification.Builder extend(Notification.Builder builder) {
9106 Bundle wearableBundle = new Bundle();
9107
9108 if (!mActions.isEmpty()) {
9109 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
9110 }
9111 if (mFlags != DEFAULT_FLAGS) {
9112 wearableBundle.putInt(KEY_FLAGS, mFlags);
9113 }
9114 if (mDisplayIntent != null) {
9115 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
9116 }
9117 if (!mPages.isEmpty()) {
9118 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
9119 new Notification[mPages.size()]));
9120 }
9121 if (mBackground != null) {
9122 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
9123 }
9124 if (mContentIcon != 0) {
9125 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
9126 }
9127 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
9128 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
9129 }
9130 if (mContentActionIndex != UNSET_ACTION_INDEX) {
9131 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
9132 mContentActionIndex);
9133 }
9134 if (mCustomSizePreset != SIZE_DEFAULT) {
9135 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
9136 }
9137 if (mCustomContentHeight != 0) {
9138 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
9139 }
9140 if (mGravity != DEFAULT_GRAVITY) {
9141 wearableBundle.putInt(KEY_GRAVITY, mGravity);
9142 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009143 if (mHintScreenTimeout != 0) {
9144 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
9145 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04009146 if (mDismissalId != null) {
9147 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
9148 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009149 if (mBridgeTag != null) {
9150 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
9151 }
Griff Hazen61a9e862014-05-22 16:05:19 -07009152
9153 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
9154 return builder;
9155 }
9156
9157 @Override
9158 public WearableExtender clone() {
9159 WearableExtender that = new WearableExtender();
9160 that.mActions = new ArrayList<Action>(this.mActions);
9161 that.mFlags = this.mFlags;
9162 that.mDisplayIntent = this.mDisplayIntent;
9163 that.mPages = new ArrayList<Notification>(this.mPages);
9164 that.mBackground = this.mBackground;
9165 that.mContentIcon = this.mContentIcon;
9166 that.mContentIconGravity = this.mContentIconGravity;
9167 that.mContentActionIndex = this.mContentActionIndex;
9168 that.mCustomSizePreset = this.mCustomSizePreset;
9169 that.mCustomContentHeight = this.mCustomContentHeight;
9170 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009171 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04009172 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009173 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07009174 return that;
9175 }
9176
9177 /**
9178 * Add a wearable action to this notification.
9179 *
9180 * <p>When wearable actions are added using this method, the set of actions that
9181 * show on a wearable device splits from devices that only show actions added
9182 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
9183 * of which actions display on different devices.
9184 *
9185 * @param action the action to add to this notification
9186 * @return this object for method chaining
9187 * @see android.app.Notification.Action
9188 */
9189 public WearableExtender addAction(Action action) {
9190 mActions.add(action);
9191 return this;
9192 }
9193
9194 /**
9195 * Adds wearable actions to this notification.
9196 *
9197 * <p>When wearable actions are added using this method, the set of actions that
9198 * show on a wearable device splits from devices that only show actions added
9199 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
9200 * of which actions display on different devices.
9201 *
9202 * @param actions the actions to add to this notification
9203 * @return this object for method chaining
9204 * @see android.app.Notification.Action
9205 */
9206 public WearableExtender addActions(List<Action> actions) {
9207 mActions.addAll(actions);
9208 return this;
9209 }
9210
9211 /**
9212 * Clear all wearable actions present on this builder.
9213 * @return this object for method chaining.
9214 * @see #addAction
9215 */
9216 public WearableExtender clearActions() {
9217 mActions.clear();
9218 return this;
9219 }
9220
9221 /**
9222 * Get the wearable actions present on this notification.
9223 */
9224 public List<Action> getActions() {
9225 return mActions;
9226 }
9227
9228 /**
9229 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07009230 * this notification. The {@link PendingIntent} provided should be for an activity.
9231 *
9232 * <pre class="prettyprint">
9233 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
9234 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
9235 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
9236 * Notification notif = new Notification.Builder(context)
9237 * .extend(new Notification.WearableExtender()
9238 * .setDisplayIntent(displayPendingIntent)
9239 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
9240 * .build();</pre>
9241 *
9242 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07009243 * should have an empty task affinity. It is also recommended to use the device
9244 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07009245 *
9246 * <p>Example AndroidManifest.xml entry:
9247 * <pre class="prettyprint">
9248 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
9249 * android:exported=&quot;true&quot;
9250 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07009251 * android:taskAffinity=&quot;&quot;
9252 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07009253 *
9254 * @param intent the {@link PendingIntent} for an activity
9255 * @return this object for method chaining
9256 * @see android.app.Notification.WearableExtender#getDisplayIntent
Gus Prevasd7363752018-09-18 14:35:15 -04009257 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009258 */
Gus Prevasd7363752018-09-18 14:35:15 -04009259 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009260 public WearableExtender setDisplayIntent(PendingIntent intent) {
9261 mDisplayIntent = intent;
9262 return this;
9263 }
9264
9265 /**
9266 * Get the intent to launch inside of an activity view when displaying this
9267 * notification. This {@code PendingIntent} should be for an activity.
Gus Prevasd7363752018-09-18 14:35:15 -04009268 *
9269 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009270 */
Gus Prevasd7363752018-09-18 14:35:15 -04009271 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009272 public PendingIntent getDisplayIntent() {
9273 return mDisplayIntent;
9274 }
9275
9276 /**
9277 * Add an additional page of content to display with this notification. The current
9278 * notification forms the first page, and pages added using this function form
9279 * subsequent pages. This field can be used to separate a notification into multiple
9280 * sections.
9281 *
9282 * @param page the notification to add as another page
9283 * @return this object for method chaining
9284 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04009285 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009286 */
Gus Prevasd7363752018-09-18 14:35:15 -04009287 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009288 public WearableExtender addPage(Notification page) {
9289 mPages.add(page);
9290 return this;
9291 }
9292
9293 /**
9294 * Add additional pages of content to display with this notification. The current
9295 * notification forms the first page, and pages added using this function form
9296 * subsequent pages. This field can be used to separate a notification into multiple
9297 * sections.
9298 *
9299 * @param pages a list of notifications
9300 * @return this object for method chaining
9301 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04009302 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009303 */
Gus Prevasd7363752018-09-18 14:35:15 -04009304 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009305 public WearableExtender addPages(List<Notification> pages) {
9306 mPages.addAll(pages);
9307 return this;
9308 }
9309
9310 /**
9311 * Clear all additional pages present on this builder.
9312 * @return this object for method chaining.
9313 * @see #addPage
Gus Prevasd7363752018-09-18 14:35:15 -04009314 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009315 */
Gus Prevasd7363752018-09-18 14:35:15 -04009316 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009317 public WearableExtender clearPages() {
9318 mPages.clear();
9319 return this;
9320 }
9321
9322 /**
9323 * Get the array of additional pages of content for displaying this notification. The
9324 * current notification forms the first page, and elements within this array form
9325 * subsequent pages. This field can be used to separate a notification into multiple
9326 * sections.
9327 * @return the pages for this notification
Gus Prevasd7363752018-09-18 14:35:15 -04009328 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009329 */
Gus Prevasd7363752018-09-18 14:35:15 -04009330 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009331 public List<Notification> getPages() {
9332 return mPages;
9333 }
9334
9335 /**
9336 * Set a background image to be displayed behind the notification content.
9337 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
9338 * will work with any notification style.
9339 *
9340 * @param background the background bitmap
9341 * @return this object for method chaining
9342 * @see android.app.Notification.WearableExtender#getBackground
Gus Prevasd7363752018-09-18 14:35:15 -04009343 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009344 */
Gus Prevasd7363752018-09-18 14:35:15 -04009345 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009346 public WearableExtender setBackground(Bitmap background) {
9347 mBackground = background;
9348 return this;
9349 }
9350
9351 /**
9352 * Get a background image to be displayed behind the notification content.
9353 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
9354 * will work with any notification style.
9355 *
9356 * @return the background image
9357 * @see android.app.Notification.WearableExtender#setBackground
Gus Prevasd7363752018-09-18 14:35:15 -04009358 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009359 */
Gus Prevasd7363752018-09-18 14:35:15 -04009360 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009361 public Bitmap getBackground() {
9362 return mBackground;
9363 }
9364
9365 /**
9366 * Set an icon that goes with the content of this notification.
9367 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009368 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009369 public WearableExtender setContentIcon(int icon) {
9370 mContentIcon = icon;
9371 return this;
9372 }
9373
9374 /**
9375 * Get an icon that goes with the content of this notification.
9376 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009377 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009378 public int getContentIcon() {
9379 return mContentIcon;
9380 }
9381
9382 /**
9383 * Set the gravity that the content icon should have within the notification display.
9384 * Supported values include {@link android.view.Gravity#START} and
9385 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
9386 * @see #setContentIcon
9387 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009388 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009389 public WearableExtender setContentIconGravity(int contentIconGravity) {
9390 mContentIconGravity = contentIconGravity;
9391 return this;
9392 }
9393
9394 /**
9395 * Get the gravity that the content icon should have within the notification display.
9396 * Supported values include {@link android.view.Gravity#START} and
9397 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
9398 * @see #getContentIcon
9399 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009400 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009401 public int getContentIconGravity() {
9402 return mContentIconGravity;
9403 }
9404
9405 /**
Gus Prevasd7363752018-09-18 14:35:15 -04009406 * Set an action from this notification's actions as the primary action. If the action has a
9407 * {@link RemoteInput} associated with it, shortcuts to the options for that input are shown
9408 * directly on the notification.
Griff Hazen14f57992014-05-26 09:07:14 -07009409 *
Gus Prevasd7363752018-09-18 14:35:15 -04009410 * @param actionIndex The index of the primary action.
Griff Hazen14f57992014-05-26 09:07:14 -07009411 * If wearable actions were added to the main notification, this index
9412 * will apply to that list, otherwise it will apply to the regular
9413 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07009414 */
9415 public WearableExtender setContentAction(int actionIndex) {
9416 mContentActionIndex = actionIndex;
9417 return this;
9418 }
9419
9420 /**
Gus Prevasd7363752018-09-18 14:35:15 -04009421 * Get the index of the notification action, if any, that was specified as the primary
9422 * action.
Griff Hazen14f57992014-05-26 09:07:14 -07009423 *
9424 * <p>If wearable specific actions were added to the main notification, this index will
9425 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07009426 *
9427 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07009428 */
9429 public int getContentAction() {
9430 return mContentActionIndex;
9431 }
9432
9433 /**
9434 * Set the gravity that this notification should have within the available viewport space.
9435 * Supported values include {@link android.view.Gravity#TOP},
9436 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
9437 * The default value is {@link android.view.Gravity#BOTTOM}.
9438 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009439 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009440 public WearableExtender setGravity(int gravity) {
9441 mGravity = gravity;
9442 return this;
9443 }
9444
9445 /**
9446 * Get the gravity that this notification should have within the available viewport space.
9447 * Supported values include {@link android.view.Gravity#TOP},
9448 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
9449 * The default value is {@link android.view.Gravity#BOTTOM}.
9450 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009451 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009452 public int getGravity() {
9453 return mGravity;
9454 }
9455
9456 /**
9457 * Set the custom size preset for the display of this notification out of the available
9458 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
9459 * {@link #SIZE_LARGE}.
9460 * <p>Some custom size presets are only applicable for custom display notifications created
9461 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
9462 * documentation for the preset in question. See also
9463 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
9464 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009465 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009466 public WearableExtender setCustomSizePreset(int sizePreset) {
9467 mCustomSizePreset = sizePreset;
9468 return this;
9469 }
9470
9471 /**
9472 * Get the custom size preset for the display of this notification out of the available
9473 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
9474 * {@link #SIZE_LARGE}.
9475 * <p>Some custom size presets are only applicable for custom display notifications created
9476 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
9477 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
9478 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009479 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009480 public int getCustomSizePreset() {
9481 return mCustomSizePreset;
9482 }
9483
9484 /**
9485 * Set the custom height in pixels for the display of this notification's content.
9486 * <p>This option is only available for custom display notifications created
9487 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
9488 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
9489 * {@link #getCustomContentHeight}.
9490 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009491 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009492 public WearableExtender setCustomContentHeight(int height) {
9493 mCustomContentHeight = height;
9494 return this;
9495 }
9496
9497 /**
9498 * Get the custom height in pixels for the display of this notification's content.
9499 * <p>This option is only available for custom display notifications created
9500 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
9501 * {@link #setCustomContentHeight}.
9502 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009503 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009504 public int getCustomContentHeight() {
9505 return mCustomContentHeight;
9506 }
9507
9508 /**
9509 * Set whether the scrolling position for the contents of this notification should start
9510 * at the bottom of the contents instead of the top when the contents are too long to
9511 * display within the screen. Default is false (start scroll at the top).
9512 */
9513 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
9514 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
9515 return this;
9516 }
9517
9518 /**
9519 * Get whether the scrolling position for the contents of this notification should start
9520 * at the bottom of the contents instead of the top when the contents are too long to
9521 * display within the screen. Default is false (start scroll at the top).
9522 */
9523 public boolean getStartScrollBottom() {
9524 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
9525 }
9526
9527 /**
9528 * Set whether the content intent is available when the wearable device is not connected
9529 * to a companion device. The user can still trigger this intent when the wearable device
9530 * is offline, but a visual hint will indicate that the content intent may not be available.
9531 * Defaults to true.
9532 */
9533 public WearableExtender setContentIntentAvailableOffline(
9534 boolean contentIntentAvailableOffline) {
9535 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
9536 return this;
9537 }
9538
9539 /**
9540 * Get whether the content intent is available when the wearable device is not connected
9541 * to a companion device. The user can still trigger this intent when the wearable device
9542 * is offline, but a visual hint will indicate that the content intent may not be available.
9543 * Defaults to true.
9544 */
9545 public boolean getContentIntentAvailableOffline() {
9546 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
9547 }
9548
9549 /**
9550 * Set a hint that this notification's icon should not be displayed.
9551 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
9552 * @return this object for method chaining
9553 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009554 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009555 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
9556 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
9557 return this;
9558 }
9559
9560 /**
9561 * Get a hint that this notification's icon should not be displayed.
9562 * @return {@code true} if this icon should not be displayed, false otherwise.
9563 * The default value is {@code false} if this was never set.
9564 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009565 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009566 public boolean getHintHideIcon() {
9567 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
9568 }
9569
9570 /**
9571 * Set a visual hint that only the background image of this notification should be
9572 * displayed, and other semantic content should be hidden. This hint is only applicable
9573 * to sub-pages added using {@link #addPage}.
9574 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009575 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009576 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
9577 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
9578 return this;
9579 }
9580
9581 /**
9582 * Get a visual hint that only the background image of this notification should be
9583 * displayed, and other semantic content should be hidden. This hint is only applicable
9584 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
9585 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009586 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009587 public boolean getHintShowBackgroundOnly() {
9588 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
9589 }
9590
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009591 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009592 * Set a hint that this notification's background should not be clipped if possible,
9593 * and should instead be resized to fully display on the screen, retaining the aspect
9594 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009595 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
9596 * @return this object for method chaining
9597 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009598 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009599 public WearableExtender setHintAvoidBackgroundClipping(
9600 boolean hintAvoidBackgroundClipping) {
9601 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
9602 return this;
9603 }
9604
9605 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009606 * Get a hint that this notification's background should not be clipped if possible,
9607 * and should instead be resized to fully display on the screen, retaining the aspect
9608 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009609 * @return {@code true} if it's ok if the background is clipped on the screen, false
9610 * otherwise. The default value is {@code false} if this was never set.
9611 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009612 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009613 public boolean getHintAvoidBackgroundClipping() {
9614 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
9615 }
9616
9617 /**
9618 * Set a hint that the screen should remain on for at least this duration when
9619 * this notification is displayed on the screen.
9620 * @param timeout The requested screen timeout in milliseconds. Can also be either
9621 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9622 * @return this object for method chaining
9623 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009624 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009625 public WearableExtender setHintScreenTimeout(int timeout) {
9626 mHintScreenTimeout = timeout;
9627 return this;
9628 }
9629
9630 /**
9631 * Get the duration, in milliseconds, that the screen should remain on for
9632 * when this notification is displayed.
9633 * @return the duration in milliseconds if > 0, or either one of the sentinel values
9634 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9635 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009636 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009637 public int getHintScreenTimeout() {
9638 return mHintScreenTimeout;
9639 }
9640
Alex Hills9ab3a232016-04-05 14:54:56 -04009641 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04009642 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
9643 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9644 * qr codes, as well as other simple black-and-white tickets.
9645 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
9646 * @return this object for method chaining
Gus Prevasd7363752018-09-18 14:35:15 -04009647 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009648 */
Gus Prevasd7363752018-09-18 14:35:15 -04009649 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009650 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
9651 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
9652 return this;
9653 }
9654
9655 /**
9656 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
9657 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9658 * qr codes, as well as other simple black-and-white tickets.
9659 * @return {@code true} if it should be displayed in ambient, false otherwise
9660 * otherwise. The default value is {@code false} if this was never set.
Gus Prevasd7363752018-09-18 14:35:15 -04009661 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009662 */
Gus Prevasd7363752018-09-18 14:35:15 -04009663 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009664 public boolean getHintAmbientBigPicture() {
9665 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
9666 }
9667
9668 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04009669 * Set a hint that this notification's content intent will launch an {@link Activity}
9670 * directly, telling the platform that it can generate the appropriate transitions.
9671 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
9672 * an activity and transitions should be generated, false otherwise.
9673 * @return this object for method chaining
9674 */
9675 public WearableExtender setHintContentIntentLaunchesActivity(
9676 boolean hintContentIntentLaunchesActivity) {
9677 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
9678 return this;
9679 }
9680
9681 /**
9682 * Get a hint that this notification's content intent will launch an {@link Activity}
9683 * directly, telling the platform that it can generate the appropriate transitions
9684 * @return {@code true} if the content intent will launch an activity and transitions should
9685 * be generated, false otherwise. The default value is {@code false} if this was never set.
9686 */
9687 public boolean getHintContentIntentLaunchesActivity() {
9688 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
9689 }
9690
Nadia Benbernou948627e2016-04-14 14:41:08 -04009691 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009692 * Sets the dismissal id for this notification. If a notification is posted with a
9693 * dismissal id, then when that notification is canceled, notifications on other wearables
9694 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04009695 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009696 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04009697 * @param dismissalId the dismissal id of the notification.
9698 * @return this object for method chaining
9699 */
9700 public WearableExtender setDismissalId(String dismissalId) {
9701 mDismissalId = dismissalId;
9702 return this;
9703 }
9704
9705 /**
9706 * Returns the dismissal id of the notification.
9707 * @return the dismissal id of the notification or null if it has not been set.
9708 */
9709 public String getDismissalId() {
9710 return mDismissalId;
9711 }
9712
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009713 /**
9714 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
9715 * posted from a phone to provide finer-grained control on what notifications are bridged
9716 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
9717 * Features to Notifications</a> for more information.
9718 * @param bridgeTag the bridge tag of the notification.
9719 * @return this object for method chaining
9720 */
9721 public WearableExtender setBridgeTag(String bridgeTag) {
9722 mBridgeTag = bridgeTag;
9723 return this;
9724 }
9725
9726 /**
9727 * Returns the bridge tag of the notification.
9728 * @return the bridge tag or null if not present.
9729 */
9730 public String getBridgeTag() {
9731 return mBridgeTag;
9732 }
9733
Griff Hazen61a9e862014-05-22 16:05:19 -07009734 private void setFlag(int mask, boolean value) {
9735 if (value) {
9736 mFlags |= mask;
9737 } else {
9738 mFlags &= ~mask;
9739 }
9740 }
9741 }
9742
9743 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009744 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
9745 * with car extensions:
9746 *
9747 * <ol>
9748 * <li>Create an {@link Notification.Builder}, setting any desired
9749 * properties.
9750 * <li>Create a {@link CarExtender}.
9751 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
9752 * {@link CarExtender}.
9753 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9754 * to apply the extensions to a notification.
9755 * </ol>
9756 *
9757 * <pre class="prettyprint">
9758 * Notification notification = new Notification.Builder(context)
9759 * ...
9760 * .extend(new CarExtender()
9761 * .set*(...))
9762 * .build();
9763 * </pre>
9764 *
9765 * <p>Car extensions can be accessed on an existing notification by using the
9766 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
9767 * to access values.
9768 */
9769 public static final class CarExtender implements Extender {
9770 private static final String TAG = "CarExtender";
9771
9772 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
9773 private static final String EXTRA_LARGE_ICON = "large_icon";
9774 private static final String EXTRA_CONVERSATION = "car_conversation";
9775 private static final String EXTRA_COLOR = "app_color";
9776
9777 private Bitmap mLargeIcon;
9778 private UnreadConversation mUnreadConversation;
9779 private int mColor = Notification.COLOR_DEFAULT;
9780
9781 /**
9782 * Create a {@link CarExtender} with default options.
9783 */
9784 public CarExtender() {
9785 }
9786
9787 /**
9788 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
9789 *
9790 * @param notif The notification from which to copy options.
9791 */
9792 public CarExtender(Notification notif) {
9793 Bundle carBundle = notif.extras == null ?
9794 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
9795 if (carBundle != null) {
9796 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
9797 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
9798
9799 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
9800 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
9801 }
9802 }
9803
9804 /**
9805 * Apply car extensions to a notification that is being built. This is typically called by
9806 * the {@link Notification.Builder#extend(Notification.Extender)}
9807 * method of {@link Notification.Builder}.
9808 */
9809 @Override
9810 public Notification.Builder extend(Notification.Builder builder) {
9811 Bundle carExtensions = new Bundle();
9812
9813 if (mLargeIcon != null) {
9814 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
9815 }
9816 if (mColor != Notification.COLOR_DEFAULT) {
9817 carExtensions.putInt(EXTRA_COLOR, mColor);
9818 }
9819
9820 if (mUnreadConversation != null) {
9821 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
9822 carExtensions.putBundle(EXTRA_CONVERSATION, b);
9823 }
9824
9825 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
9826 return builder;
9827 }
9828
9829 /**
9830 * Sets the accent color to use when Android Auto presents the notification.
9831 *
9832 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
9833 * to accent the displayed notification. However, not all colors are acceptable in an
9834 * automotive setting. This method can be used to override the color provided in the
9835 * notification in such a situation.
9836 */
Tor Norbye80756e32015-03-02 09:39:27 -08009837 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009838 mColor = color;
9839 return this;
9840 }
9841
9842 /**
9843 * Gets the accent color.
9844 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04009845 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009846 */
Tor Norbye80756e32015-03-02 09:39:27 -08009847 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009848 public int getColor() {
9849 return mColor;
9850 }
9851
9852 /**
9853 * Sets the large icon of the car notification.
9854 *
9855 * If no large icon is set in the extender, Android Auto will display the icon
9856 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
9857 *
9858 * @param largeIcon The large icon to use in the car notification.
9859 * @return This object for method chaining.
9860 */
9861 public CarExtender setLargeIcon(Bitmap largeIcon) {
9862 mLargeIcon = largeIcon;
9863 return this;
9864 }
9865
9866 /**
9867 * Gets the large icon used in this car notification, or null if no icon has been set.
9868 *
9869 * @return The large icon for the car notification.
9870 * @see CarExtender#setLargeIcon
9871 */
9872 public Bitmap getLargeIcon() {
9873 return mLargeIcon;
9874 }
9875
9876 /**
9877 * Sets the unread conversation in a message notification.
9878 *
9879 * @param unreadConversation The unread part of the conversation this notification conveys.
9880 * @return This object for method chaining.
9881 */
9882 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
9883 mUnreadConversation = unreadConversation;
9884 return this;
9885 }
9886
9887 /**
9888 * Returns the unread conversation conveyed by this notification.
9889 * @see #setUnreadConversation(UnreadConversation)
9890 */
9891 public UnreadConversation getUnreadConversation() {
9892 return mUnreadConversation;
9893 }
9894
9895 /**
9896 * A class which holds the unread messages from a conversation.
9897 */
9898 public static class UnreadConversation {
9899 private static final String KEY_AUTHOR = "author";
9900 private static final String KEY_TEXT = "text";
9901 private static final String KEY_MESSAGES = "messages";
9902 private static final String KEY_REMOTE_INPUT = "remote_input";
9903 private static final String KEY_ON_REPLY = "on_reply";
9904 private static final String KEY_ON_READ = "on_read";
9905 private static final String KEY_PARTICIPANTS = "participants";
9906 private static final String KEY_TIMESTAMP = "timestamp";
9907
9908 private final String[] mMessages;
9909 private final RemoteInput mRemoteInput;
9910 private final PendingIntent mReplyPendingIntent;
9911 private final PendingIntent mReadPendingIntent;
9912 private final String[] mParticipants;
9913 private final long mLatestTimestamp;
9914
9915 UnreadConversation(String[] messages, RemoteInput remoteInput,
9916 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
9917 String[] participants, long latestTimestamp) {
9918 mMessages = messages;
9919 mRemoteInput = remoteInput;
9920 mReadPendingIntent = readPendingIntent;
9921 mReplyPendingIntent = replyPendingIntent;
9922 mParticipants = participants;
9923 mLatestTimestamp = latestTimestamp;
9924 }
9925
9926 /**
9927 * Gets the list of messages conveyed by this notification.
9928 */
9929 public String[] getMessages() {
9930 return mMessages;
9931 }
9932
9933 /**
9934 * Gets the remote input that will be used to convey the response to a message list, or
9935 * null if no such remote input exists.
9936 */
9937 public RemoteInput getRemoteInput() {
9938 return mRemoteInput;
9939 }
9940
9941 /**
9942 * Gets the pending intent that will be triggered when the user replies to this
9943 * notification.
9944 */
9945 public PendingIntent getReplyPendingIntent() {
9946 return mReplyPendingIntent;
9947 }
9948
9949 /**
9950 * Gets the pending intent that Android Auto will send after it reads aloud all messages
9951 * in this object's message list.
9952 */
9953 public PendingIntent getReadPendingIntent() {
9954 return mReadPendingIntent;
9955 }
9956
9957 /**
9958 * Gets the participants in the conversation.
9959 */
9960 public String[] getParticipants() {
9961 return mParticipants;
9962 }
9963
9964 /**
9965 * Gets the firs participant in the conversation.
9966 */
9967 public String getParticipant() {
9968 return mParticipants.length > 0 ? mParticipants[0] : null;
9969 }
9970
9971 /**
9972 * Gets the timestamp of the conversation.
9973 */
9974 public long getLatestTimestamp() {
9975 return mLatestTimestamp;
9976 }
9977
9978 Bundle getBundleForUnreadConversation() {
9979 Bundle b = new Bundle();
9980 String author = null;
9981 if (mParticipants != null && mParticipants.length > 1) {
9982 author = mParticipants[0];
9983 }
9984 Parcelable[] messages = new Parcelable[mMessages.length];
9985 for (int i = 0; i < messages.length; i++) {
9986 Bundle m = new Bundle();
9987 m.putString(KEY_TEXT, mMessages[i]);
9988 m.putString(KEY_AUTHOR, author);
9989 messages[i] = m;
9990 }
9991 b.putParcelableArray(KEY_MESSAGES, messages);
9992 if (mRemoteInput != null) {
9993 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
9994 }
9995 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
9996 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
9997 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
9998 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
9999 return b;
10000 }
10001
10002 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
10003 if (b == null) {
10004 return null;
10005 }
10006 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
10007 String[] messages = null;
10008 if (parcelableMessages != null) {
10009 String[] tmp = new String[parcelableMessages.length];
10010 boolean success = true;
10011 for (int i = 0; i < tmp.length; i++) {
10012 if (!(parcelableMessages[i] instanceof Bundle)) {
10013 success = false;
10014 break;
10015 }
10016 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
10017 if (tmp[i] == null) {
10018 success = false;
10019 break;
10020 }
10021 }
10022 if (success) {
10023 messages = tmp;
10024 } else {
10025 return null;
10026 }
10027 }
10028
10029 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
10030 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
10031
10032 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
10033
10034 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
10035 if (participants == null || participants.length != 1) {
10036 return null;
10037 }
10038
10039 return new UnreadConversation(messages,
10040 remoteInput,
10041 onReply,
10042 onRead,
10043 participants, b.getLong(KEY_TIMESTAMP));
10044 }
10045 };
10046
10047 /**
10048 * Builder class for {@link CarExtender.UnreadConversation} objects.
10049 */
10050 public static class Builder {
10051 private final List<String> mMessages = new ArrayList<String>();
10052 private final String mParticipant;
10053 private RemoteInput mRemoteInput;
10054 private PendingIntent mReadPendingIntent;
10055 private PendingIntent mReplyPendingIntent;
10056 private long mLatestTimestamp;
10057
10058 /**
10059 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
10060 *
10061 * @param name The name of the other participant in the conversation.
10062 */
10063 public Builder(String name) {
10064 mParticipant = name;
10065 }
10066
10067 /**
10068 * Appends a new unread message to the list of messages for this conversation.
10069 *
10070 * The messages should be added from oldest to newest.
10071 *
10072 * @param message The text of the new unread message.
10073 * @return This object for method chaining.
10074 */
10075 public Builder addMessage(String message) {
10076 mMessages.add(message);
10077 return this;
10078 }
10079
10080 /**
10081 * Sets the pending intent and remote input which will convey the reply to this
10082 * notification.
10083 *
10084 * @param pendingIntent The pending intent which will be triggered on a reply.
10085 * @param remoteInput The remote input parcelable which will carry the reply.
10086 * @return This object for method chaining.
10087 *
10088 * @see CarExtender.UnreadConversation#getRemoteInput
10089 * @see CarExtender.UnreadConversation#getReplyPendingIntent
10090 */
10091 public Builder setReplyAction(
10092 PendingIntent pendingIntent, RemoteInput remoteInput) {
10093 mRemoteInput = remoteInput;
10094 mReplyPendingIntent = pendingIntent;
10095
10096 return this;
10097 }
10098
10099 /**
10100 * Sets the pending intent that will be sent once the messages in this notification
10101 * are read.
10102 *
10103 * @param pendingIntent The pending intent to use.
10104 * @return This object for method chaining.
10105 */
10106 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
10107 mReadPendingIntent = pendingIntent;
10108 return this;
10109 }
10110
10111 /**
10112 * Sets the timestamp of the most recent message in an unread conversation.
10113 *
10114 * If a messaging notification has been posted by your application and has not
10115 * yet been cancelled, posting a later notification with the same id and tag
10116 * but without a newer timestamp may result in Android Auto not displaying a
10117 * heads up notification for the later notification.
10118 *
10119 * @param timestamp The timestamp of the most recent message in the conversation.
10120 * @return This object for method chaining.
10121 */
10122 public Builder setLatestTimestamp(long timestamp) {
10123 mLatestTimestamp = timestamp;
10124 return this;
10125 }
10126
10127 /**
10128 * Builds a new unread conversation object.
10129 *
10130 * @return The new unread conversation object.
10131 */
10132 public UnreadConversation build() {
10133 String[] messages = mMessages.toArray(new String[mMessages.size()]);
10134 String[] participants = { mParticipant };
10135 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
10136 mReadPendingIntent, participants, mLatestTimestamp);
10137 }
10138 }
10139 }
10140
10141 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010142 * <p>Helper class to add Android TV extensions to notifications. To create a notification
10143 * with a TV extension:
10144 *
10145 * <ol>
10146 * <li>Create an {@link Notification.Builder}, setting any desired properties.
10147 * <li>Create a {@link TvExtender}.
10148 * <li>Set TV-specific properties using the {@code set} methods of
10149 * {@link TvExtender}.
10150 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
10151 * to apply the extension to a notification.
10152 * </ol>
10153 *
10154 * <pre class="prettyprint">
10155 * Notification notification = new Notification.Builder(context)
10156 * ...
10157 * .extend(new TvExtender()
10158 * .set*(...))
10159 * .build();
10160 * </pre>
10161 *
10162 * <p>TV extensions can be accessed on an existing notification by using the
10163 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
10164 * to access values.
10165 *
10166 * @hide
10167 */
10168 @SystemApi
10169 public static final class TvExtender implements Extender {
10170 private static final String TAG = "TvExtender";
10171
10172 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
10173 private static final String EXTRA_FLAGS = "flags";
10174 private static final String EXTRA_CONTENT_INTENT = "content_intent";
10175 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010176 private static final String EXTRA_CHANNEL_ID = "channel_id";
Rhiannon Malia1a083932018-01-24 15:02:30 -080010177 private static final String EXTRA_SUPPRESS_SHOW_OVER_APPS = "suppressShowOverApps";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010178
10179 // Flags bitwise-ored to mFlags
10180 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
10181
10182 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010183 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010184 private PendingIntent mContentIntent;
10185 private PendingIntent mDeleteIntent;
Rhiannon Malia1a083932018-01-24 15:02:30 -080010186 private boolean mSuppressShowOverApps;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010187
10188 /**
10189 * Create a {@link TvExtender} with default options.
10190 */
10191 public TvExtender() {
10192 mFlags = FLAG_AVAILABLE_ON_TV;
10193 }
10194
10195 /**
10196 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
10197 *
10198 * @param notif The notification from which to copy options.
10199 */
10200 public TvExtender(Notification notif) {
10201 Bundle bundle = notif.extras == null ?
10202 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
10203 if (bundle != null) {
10204 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010205 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Rhiannon Malia1a083932018-01-24 15:02:30 -080010206 mSuppressShowOverApps = bundle.getBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010207 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
10208 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
10209 }
10210 }
10211
10212 /**
10213 * Apply a TV extension to a notification that is being built. This is typically called by
10214 * the {@link Notification.Builder#extend(Notification.Extender)}
10215 * method of {@link Notification.Builder}.
10216 */
10217 @Override
10218 public Notification.Builder extend(Notification.Builder builder) {
10219 Bundle bundle = new Bundle();
10220
10221 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010222 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Rhiannon Malia1a083932018-01-24 15:02:30 -080010223 bundle.putBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS, mSuppressShowOverApps);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010224 if (mContentIntent != null) {
10225 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
10226 }
10227
10228 if (mDeleteIntent != null) {
10229 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
10230 }
10231
10232 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
10233 return builder;
10234 }
10235
10236 /**
10237 * Returns true if this notification should be shown on TV. This method return true
10238 * if the notification was extended with a TvExtender.
10239 */
10240 public boolean isAvailableOnTv() {
10241 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
10242 }
10243
10244 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010245 * Specifies the channel the notification should be delivered on when shown on TV.
10246 * It can be different from the channel that the notification is delivered to when
10247 * posting on a non-TV device.
10248 */
10249 public TvExtender setChannel(String channelId) {
10250 mChannelId = channelId;
10251 return this;
10252 }
10253
10254 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -040010255 * Specifies the channel the notification should be delivered on when shown on TV.
10256 * It can be different from the channel that the notification is delivered to when
10257 * posting on a non-TV device.
10258 */
10259 public TvExtender setChannelId(String channelId) {
10260 mChannelId = channelId;
10261 return this;
10262 }
10263
Jeff Sharkey000ce802017-04-29 13:13:27 -060010264 /** @removed */
10265 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010266 public String getChannel() {
10267 return mChannelId;
10268 }
10269
10270 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -040010271 * Returns the id of the channel this notification posts to on TV.
10272 */
10273 public String getChannelId() {
10274 return mChannelId;
10275 }
10276
10277 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010278 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
10279 * If provided, it is used instead of the content intent specified
10280 * at the level of Notification.
10281 */
10282 public TvExtender setContentIntent(PendingIntent intent) {
10283 mContentIntent = intent;
10284 return this;
10285 }
10286
10287 /**
10288 * Returns the TV-specific content intent. If this method returns null, the
10289 * main content intent on the notification should be used.
10290 *
10291 * @see {@link Notification#contentIntent}
10292 */
10293 public PendingIntent getContentIntent() {
10294 return mContentIntent;
10295 }
10296
10297 /**
10298 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
10299 * by the user on TV. If provided, it is used instead of the delete intent specified
10300 * at the level of Notification.
10301 */
10302 public TvExtender setDeleteIntent(PendingIntent intent) {
10303 mDeleteIntent = intent;
10304 return this;
10305 }
10306
10307 /**
10308 * Returns the TV-specific delete intent. If this method returns null, the
10309 * main delete intent on the notification should be used.
10310 *
10311 * @see {@link Notification#deleteIntent}
10312 */
10313 public PendingIntent getDeleteIntent() {
10314 return mDeleteIntent;
10315 }
Rhiannon Malia1a083932018-01-24 15:02:30 -080010316
10317 /**
10318 * Specifies whether this notification should suppress showing a message over top of apps
10319 * outside of the launcher.
10320 */
10321 public TvExtender setSuppressShowOverApps(boolean suppress) {
10322 mSuppressShowOverApps = suppress;
10323 return this;
10324 }
10325
10326 /**
10327 * Returns true if this notification should not show messages over top of apps
10328 * outside of the launcher.
10329 */
10330 public boolean getSuppressShowOverApps() {
10331 return mSuppressShowOverApps;
10332 }
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010333 }
10334
10335 /**
Griff Hazen61a9e862014-05-22 16:05:19 -070010336 * Get an array of Notification objects from a parcelable array bundle field.
10337 * Update the bundle to have a typed array so fetches in the future don't need
10338 * to do an array copy.
10339 */
10340 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
10341 Parcelable[] array = bundle.getParcelableArray(key);
10342 if (array instanceof Notification[] || array == null) {
10343 return (Notification[]) array;
10344 }
10345 Notification[] typedArray = Arrays.copyOf(array, array.length,
10346 Notification[].class);
10347 bundle.putParcelableArray(key, typedArray);
10348 return typedArray;
10349 }
Christoph Studer4600f9b2014-07-22 22:44:43 +020010350
10351 private static class BuilderRemoteViews extends RemoteViews {
10352 public BuilderRemoteViews(Parcel parcel) {
10353 super(parcel);
10354 }
10355
Kenny Guy77320062014-08-27 21:37:15 +010010356 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
10357 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +020010358 }
10359
10360 @Override
10361 public BuilderRemoteViews clone() {
10362 Parcel p = Parcel.obtain();
10363 writeToParcel(p, 0);
10364 p.setDataPosition(0);
10365 BuilderRemoteViews brv = new BuilderRemoteViews(p);
10366 p.recycle();
10367 return brv;
10368 }
10369 }
Adrian Roos70d7aa32017-01-11 15:39:06 -080010370
Selim Cinek384804b2018-04-18 14:31:07 +080010371 /**
10372 * A result object where information about the template that was created is saved.
10373 */
10374 private static class TemplateBindResult {
Selim Cinek1c72fa02018-04-23 18:00:54 +080010375 int mIconMarginEnd;
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010376 boolean mRightIconContainerVisible;
Selim Cinek384804b2018-04-18 14:31:07 +080010377
10378 /**
Selim Cinek1c72fa02018-04-23 18:00:54 +080010379 * Get the margin end that needs to be added to any fields that may overlap
Selim Cinek384804b2018-04-18 14:31:07 +080010380 * with the right actions.
10381 */
Selim Cinek1c72fa02018-04-23 18:00:54 +080010382 public int getIconMarginEnd() {
10383 return mIconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +080010384 }
10385
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010386 /**
10387 * Is the icon container visible on the right size because of the reply button or the
10388 * right icon.
10389 */
10390 public boolean isRightIconContainerVisible() {
10391 return mRightIconContainerVisible;
10392 }
10393
Selim Cinek1c72fa02018-04-23 18:00:54 +080010394 public void setIconMarginEnd(int iconMarginEnd) {
10395 this.mIconMarginEnd = iconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +080010396 }
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010397
10398 public void setRightIconContainerVisible(boolean iconContainerVisible) {
10399 mRightIconContainerVisible = iconContainerVisible;
10400 }
Selim Cinek384804b2018-04-18 14:31:07 +080010401 }
10402
Adrian Roos70d7aa32017-01-11 15:39:06 -080010403 private static class StandardTemplateParams {
10404 boolean hasProgress = true;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010405 CharSequence title;
10406 CharSequence text;
Selim Cinekafeed292017-12-12 17:32:44 -080010407 CharSequence headerTextSecondary;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010408 CharSequence summaryText;
Selim Cinekbee4e072018-05-21 22:06:43 -070010409 int maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek88188f22017-09-19 16:46:56 -070010410 boolean hideLargeIcon;
Selim Cinek384804b2018-04-18 14:31:07 +080010411 boolean hideReplyIcon;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010412 boolean allowColorization = true;
10413 boolean forceDefaultColor = false;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010414
10415 final StandardTemplateParams reset() {
10416 hasProgress = true;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010417 title = null;
10418 text = null;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010419 summaryText = null;
Selim Cinekafeed292017-12-12 17:32:44 -080010420 headerTextSecondary = null;
Selim Cinekbee4e072018-05-21 22:06:43 -070010421 maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010422 allowColorization = true;
10423 forceDefaultColor = false;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010424 return this;
10425 }
10426
10427 final StandardTemplateParams hasProgress(boolean hasProgress) {
10428 this.hasProgress = hasProgress;
10429 return this;
10430 }
10431
10432 final StandardTemplateParams title(CharSequence title) {
10433 this.title = title;
10434 return this;
10435 }
10436
10437 final StandardTemplateParams text(CharSequence text) {
10438 this.text = text;
10439 return this;
10440 }
10441
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010442 final StandardTemplateParams summaryText(CharSequence text) {
10443 this.summaryText = text;
10444 return this;
10445 }
10446
Selim Cinekafeed292017-12-12 17:32:44 -080010447 final StandardTemplateParams headerTextSecondary(CharSequence text) {
10448 this.headerTextSecondary = text;
10449 return this;
10450 }
10451
Selim Cinek384804b2018-04-18 14:31:07 +080010452 final StandardTemplateParams hideLargeIcon(boolean hideLargeIcon) {
10453 this.hideLargeIcon = hideLargeIcon;
Selim Cinek88188f22017-09-19 16:46:56 -070010454 return this;
10455 }
10456
Selim Cinek384804b2018-04-18 14:31:07 +080010457 final StandardTemplateParams hideReplyIcon(boolean hideReplyIcon) {
10458 this.hideReplyIcon = hideReplyIcon;
Selim Cinek88188f22017-09-19 16:46:56 -070010459 return this;
10460 }
10461
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010462 final StandardTemplateParams disallowColorization() {
10463 this.allowColorization = false;
10464 return this;
10465 }
10466
10467 final StandardTemplateParams forceDefaultColor() {
10468 this.forceDefaultColor = true;
10469 return this;
10470 }
10471
Adrian Roos70d7aa32017-01-11 15:39:06 -080010472 final StandardTemplateParams fillTextsFrom(Builder b) {
10473 Bundle extras = b.mN.extras;
Lucas Dupin00be88f2019-01-03 17:50:52 -080010474 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE));
Lucas Dupin06c5e642017-09-13 16:34:58 -070010475
Lucas Dupin06c5e642017-09-13 16:34:58 -070010476 CharSequence text = extras.getCharSequence(EXTRA_BIG_TEXT);
Lucas Dupin00be88f2019-01-03 17:50:52 -080010477 if (TextUtils.isEmpty(text)) {
Lucas Dupin06c5e642017-09-13 16:34:58 -070010478 text = extras.getCharSequence(EXTRA_TEXT);
10479 }
Lucas Dupin00be88f2019-01-03 17:50:52 -080010480 this.text = b.processLegacyText(text);
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010481 this.summaryText = extras.getCharSequence(EXTRA_SUB_TEXT);
Adrian Roos70d7aa32017-01-11 15:39:06 -080010482 return this;
10483 }
Selim Cinekbee4e072018-05-21 22:06:43 -070010484
10485 /**
10486 * Set the maximum lines of remote input history lines allowed.
10487 * @param maxRemoteInputHistory The number of lines.
10488 * @return The builder for method chaining.
10489 */
10490 public StandardTemplateParams setMaxRemoteInputHistory(int maxRemoteInputHistory) {
10491 this.maxRemoteInputHistory = maxRemoteInputHistory;
10492 return this;
10493 }
Adrian Roos70d7aa32017-01-11 15:39:06 -080010494 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010495}