blob: a869d85f732231635747e15641aa9a71f84e47f9 [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) {
4889 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor(p));
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004890 contentView.setProgressTintList(R.id.progress, colorStateList);
4891 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004892 }
Selim Cinek29603462015-11-17 19:04:39 -08004893 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004894 } else {
4895 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004896 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004897 }
Joe Onorato561d3852010-11-20 18:09:34 -08004898 }
4899
Selim Cinek384804b2018-04-18 14:31:07 +08004900 private void bindLargeIconAndReply(RemoteViews contentView, StandardTemplateParams p,
4901 TemplateBindResult result) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004902 boolean largeIconShown = bindLargeIcon(contentView, p);
4903 boolean replyIconShown = bindReplyIcon(contentView, p);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004904 boolean iconContainerVisible = largeIconShown || replyIconShown;
Selim Cinek384804b2018-04-18 14:31:07 +08004905 contentView.setViewVisibility(R.id.right_icon_container,
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004906 iconContainerVisible ? View.VISIBLE : View.GONE);
Selim Cinek1c72fa02018-04-23 18:00:54 +08004907 int marginEnd = calculateMarginEnd(largeIconShown, replyIconShown);
4908 contentView.setViewLayoutMarginEnd(R.id.line1, marginEnd);
4909 contentView.setViewLayoutMarginEnd(R.id.text, marginEnd);
4910 contentView.setViewLayoutMarginEnd(R.id.progress, marginEnd);
Selim Cinek384804b2018-04-18 14:31:07 +08004911 if (result != null) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08004912 result.setIconMarginEnd(marginEnd);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004913 result.setRightIconContainerVisible(iconContainerVisible);
Selim Cinek384804b2018-04-18 14:31:07 +08004914 }
4915 }
4916
Selim Cinek1c72fa02018-04-23 18:00:54 +08004917 private int calculateMarginEnd(boolean largeIconShown, boolean replyIconShown) {
4918 int marginEnd = 0;
4919 int contentMargin = mContext.getResources().getDimensionPixelSize(
4920 R.dimen.notification_content_margin_end);
4921 int iconSize = mContext.getResources().getDimensionPixelSize(
4922 R.dimen.notification_right_icon_size);
4923 if (replyIconShown) {
4924 // The size of the reply icon
4925 marginEnd += iconSize;
4926
4927 int replyInset = mContext.getResources().getDimensionPixelSize(
4928 R.dimen.notification_reply_inset);
4929 // We're subtracting the inset of the reply icon to make sure it's
4930 // aligned nicely on the right, and remove it from the following padding
4931 marginEnd -= replyInset * 2;
4932 }
4933 if (largeIconShown) {
4934 // adding size of the right icon
4935 marginEnd += iconSize;
4936
4937 if (replyIconShown) {
4938 // We also add some padding to the reply icon if it's around
4939 marginEnd += contentMargin;
4940 }
4941 }
4942 if (replyIconShown || largeIconShown) {
4943 // The padding to the content
4944 marginEnd += contentMargin;
4945 }
4946 return marginEnd;
4947 }
4948
Selim Cinek384804b2018-04-18 14:31:07 +08004949 /**
4950 * Bind the large icon.
4951 * @return if the largeIcon is visible
4952 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004953 private boolean bindLargeIcon(RemoteViews contentView, StandardTemplateParams p) {
Selim Cinek279fa862016-06-14 10:57:25 -07004954 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4955 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4956 }
Lucas Dupin00be88f2019-01-03 17:50:52 -08004957 boolean showLargeIcon = mN.mLargeIcon != null && !p.hideLargeIcon;
Selim Cinek88188f22017-09-19 16:46:56 -07004958 if (showLargeIcon) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004959 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4960 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004961 processLargeLegacyIcon(mN.mLargeIcon, contentView, p);
Selim Cinek88188f22017-09-19 16:46:56 -07004962 }
Selim Cinek384804b2018-04-18 14:31:07 +08004963 return showLargeIcon;
4964 }
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004965
Selim Cinek384804b2018-04-18 14:31:07 +08004966 /**
4967 * Bind the reply icon.
4968 * @return if the reply icon is visible
4969 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004970 private boolean bindReplyIcon(RemoteViews contentView, StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08004971 boolean actionVisible = !p.hideReplyIcon;
Selim Cinek384804b2018-04-18 14:31:07 +08004972 Action action = null;
Selim Cinek88188f22017-09-19 16:46:56 -07004973 if (actionVisible) {
Selim Cinek384804b2018-04-18 14:31:07 +08004974 action = findReplyAction();
4975 actionVisible = action != null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004976 }
Selim Cinek384804b2018-04-18 14:31:07 +08004977 if (actionVisible) {
4978 contentView.setViewVisibility(R.id.reply_icon_action, View.VISIBLE);
4979 contentView.setDrawableTint(R.id.reply_icon_action,
4980 false /* targetBackground */,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004981 getNeutralColor(p),
Selim Cinek384804b2018-04-18 14:31:07 +08004982 PorterDuff.Mode.SRC_ATOP);
4983 contentView.setOnClickPendingIntent(R.id.reply_icon_action, action.actionIntent);
4984 contentView.setRemoteInputs(R.id.reply_icon_action, action.mRemoteInputs);
4985 } else {
4986 contentView.setRemoteInputs(R.id.reply_icon_action, null);
4987 }
4988 contentView.setViewVisibility(R.id.reply_icon_action,
4989 actionVisible ? View.VISIBLE : View.GONE);
4990 return actionVisible;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004991 }
4992
4993 private Action findReplyAction() {
4994 ArrayList<Action> actions = mActions;
4995 if (mOriginalActions != null) {
4996 actions = mOriginalActions;
4997 }
4998 int numActions = actions.size();
4999 for (int i = 0; i < numActions; i++) {
5000 Action action = actions.get(i);
5001 if (hasValidRemoteInput(action)) {
5002 return action;
5003 }
5004 }
5005 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005006 }
5007
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005008 private void bindNotificationHeader(RemoteViews contentView, StandardTemplateParams p) {
5009 bindSmallIcon(contentView, p);
5010 bindHeaderAppName(contentView, p);
Lucas Dupin00be88f2019-01-03 17:50:52 -08005011 bindHeaderText(contentView, p);
5012 bindHeaderTextSecondary(contentView, p);
5013 bindHeaderChronometerAndTime(contentView, p);
5014 bindProfileBadge(contentView, p);
5015 bindAlertedIcon(contentView, p);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005016 bindActivePermissions(contentView, p);
5017 bindExpandButton(contentView, p);
Julia Reynoldsfc640012018-02-21 12:25:27 -05005018 mN.mUsesStandardHeader = true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005019 }
5020
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005021 private void bindActivePermissions(RemoteViews contentView, StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005022 int color = getNeutralColor(p);
Julia Reynoldsb887b232018-04-10 16:38:08 -04005023 contentView.setDrawableTint(R.id.camera, false, color, PorterDuff.Mode.SRC_ATOP);
5024 contentView.setDrawableTint(R.id.mic, false, color, PorterDuff.Mode.SRC_ATOP);
5025 contentView.setDrawableTint(R.id.overlay, false, color, PorterDuff.Mode.SRC_ATOP);
5026 }
5027
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005028 private void bindExpandButton(RemoteViews contentView, StandardTemplateParams p) {
5029 int color = isColorized(p) ? getPrimaryTextColor(p) : getSecondaryTextColor(p);
Sunny Goyal5b153922017-09-21 21:00:36 -07005030 contentView.setDrawableTint(R.id.expand_button, false, color,
5031 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005032 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08005033 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005034 }
5035
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005036 private void bindHeaderChronometerAndTime(RemoteViews contentView,
5037 StandardTemplateParams p) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005038 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08005039 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005040 setTextViewColorSecondary(contentView, R.id.time_divider, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005041 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
5042 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
5043 contentView.setLong(R.id.chronometer, "setBase",
5044 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
5045 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07005046 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07005047 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005048 setTextViewColorSecondary(contentView, R.id.chronometer, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005049 } else {
5050 contentView.setViewVisibility(R.id.time, View.VISIBLE);
5051 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005052 setTextViewColorSecondary(contentView, R.id.time, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005053 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005054 } else {
5055 // We still want a time to be set but gone, such that we can show and hide it
5056 // on demand in case it's a child notification without anything in the header
5057 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005058 }
5059 }
5060
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005061 private void bindHeaderText(RemoteViews contentView, StandardTemplateParams p) {
5062 CharSequence summaryText = p.summaryText;
5063 if (summaryText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05005064 && mStyle.hasSummaryInHeader()) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005065 summaryText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05005066 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005067 if (summaryText == null
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07005068 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
5069 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005070 summaryText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07005071 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005072 if (summaryText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005073 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek48f66b72017-08-18 16:17:51 -07005074 contentView.setTextViewText(R.id.header_text, processTextSpans(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005075 processLegacyText(summaryText)));
5076 setTextViewColorSecondary(contentView, R.id.header_text, p);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07005077 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
5078 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005079 setTextViewColorSecondary(contentView, R.id.header_text_divider, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005080 }
5081 }
5082
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005083 private void bindHeaderTextSecondary(RemoteViews contentView, StandardTemplateParams p) {
5084 if (!TextUtils.isEmpty(p.headerTextSecondary)) {
Selim Cinekafeed292017-12-12 17:32:44 -08005085 contentView.setTextViewText(R.id.header_text_secondary, processTextSpans(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005086 processLegacyText(p.headerTextSecondary)));
5087 setTextViewColorSecondary(contentView, R.id.header_text_secondary, p);
Selim Cinekafeed292017-12-12 17:32:44 -08005088 contentView.setViewVisibility(R.id.header_text_secondary, View.VISIBLE);
5089 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005090 setTextViewColorSecondary(contentView, R.id.header_text_secondary_divider, p);
Selim Cinekafeed292017-12-12 17:32:44 -08005091 }
5092 }
5093
Adrian Rooseba05822016-04-22 17:09:27 -07005094 /**
5095 * @hide
5096 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005097 @UnsupportedAppUsage
Adrian Rooseba05822016-04-22 17:09:27 -07005098 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04005099 CharSequence name = null;
5100 final PackageManager pm = mContext.getPackageManager();
5101 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
5102 // only system packages which lump together a bunch of unrelated stuff
5103 // may substitute a different name to make the purpose of the
5104 // notification more clear. the correct package label should always
5105 // be accessible via SystemUI.
5106 final String pkg = mContext.getPackageName();
5107 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
5108 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
5109 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
5110 name = subName;
5111 } else {
5112 Log.w(TAG, "warning: pkg "
5113 + pkg + " attempting to substitute app name '" + subName
5114 + "' without holding perm "
5115 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
5116 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005117 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04005118 if (TextUtils.isEmpty(name)) {
5119 name = pm.getApplicationLabel(mContext.getApplicationInfo());
5120 }
5121 if (TextUtils.isEmpty(name)) {
5122 // still nothing?
5123 return null;
5124 }
5125
5126 return String.valueOf(name);
5127 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005128 private void bindHeaderAppName(RemoteViews contentView, StandardTemplateParams p) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04005129 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005130 if (isColorized(p)) {
5131 setTextViewColorPrimary(contentView, R.id.app_name_text, p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005132 } else {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005133 contentView.setTextColor(R.id.app_name_text, getSecondaryTextColor(p));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005134 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005135 }
5136
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005137 private boolean isColorized(StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005138 return p.allowColorization && mN.isColorized();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005139 }
5140
5141 private void bindSmallIcon(RemoteViews contentView, StandardTemplateParams p) {
Selim Cinek279fa862016-06-14 10:57:25 -07005142 if (mN.mSmallIcon == null && mN.icon != 0) {
5143 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
5144 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005145 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Sunny Goyal5b153922017-09-21 21:00:36 -07005146 contentView.setInt(R.id.icon, "setImageLevel", mN.iconLevel);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005147 processSmallIconColor(mN.mSmallIcon, contentView, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005148 }
5149
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005150 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005151 * @return true if the built notification will show the time or the chronometer; false
5152 * otherwise
5153 */
5154 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07005155 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005156 }
5157
Christoph Studerfe718432014-09-01 18:21:18 +02005158 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07005159 // actions_container is only reset when there are no actions to avoid focus issues with
5160 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02005161 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02005162 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08005163
5164 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
5165 big.setTextViewText(R.id.notification_material_reply_text_1, null);
Kenny Guya0f6de82018-04-06 16:20:16 +01005166 big.setViewVisibility(R.id.notification_material_reply_text_1_container, View.GONE);
5167 big.setViewVisibility(R.id.notification_material_reply_progress, View.GONE);
Adrian Roose458aa82015-12-08 16:17:19 -08005168
5169 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
5170 big.setTextViewText(R.id.notification_material_reply_text_2, null);
5171 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
5172 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07005173
Selim Cineked64a142018-02-06 18:06:01 -08005174 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
5175 R.dimen.notification_content_margin);
Christoph Studerfe718432014-09-01 18:21:18 +02005176 }
5177
Selim Cinek384804b2018-04-18 14:31:07 +08005178 private RemoteViews applyStandardTemplateWithActions(int layoutId,
5179 TemplateBindResult result) {
5180 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this),
5181 result);
Adrian Roos48d746a2016-04-12 14:57:28 -07005182 }
5183
Gustav Sennton1463d832018-11-06 16:12:48 +00005184 private static List<Notification.Action> filterOutContextualActions(
5185 List<Notification.Action> actions) {
5186 List<Notification.Action> nonContextualActions = new ArrayList<>();
5187 for (Notification.Action action : actions) {
Gustav Sennton005d7a02019-01-04 13:41:32 +00005188 if (!action.isContextual()) {
Gustav Sennton1463d832018-11-06 16:12:48 +00005189 nonContextualActions.add(action);
5190 }
5191 }
5192 return nonContextualActions;
5193 }
5194
Adrian Roos70d7aa32017-01-11 15:39:06 -08005195 private RemoteViews applyStandardTemplateWithActions(int layoutId,
Selim Cinek384804b2018-04-18 14:31:07 +08005196 StandardTemplateParams p, TemplateBindResult result) {
5197 RemoteViews big = applyStandardTemplate(layoutId, p, result);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005198
Christoph Studerfe718432014-09-01 18:21:18 +02005199 resetStandardTemplateWithActions(big);
5200
Adrian Roose458aa82015-12-08 16:17:19 -08005201 boolean validRemoteInput = false;
5202
Gustav Sennton1463d832018-11-06 16:12:48 +00005203 // In the UI contextual actions appear separately from the standard actions, so we
5204 // filter them out here.
5205 List<Notification.Action> nonContextualActions = filterOutContextualActions(mActions);
5206
5207 int N = nonContextualActions.size();
Lucas Dupin00be88f2019-01-03 17:50:52 -08005208 boolean emphazisedMode = mN.fullScreenIntent != null;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005209 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005210 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08005211 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005212 big.setViewVisibility(R.id.actions, View.VISIBLE);
Selim Cineked64a142018-02-06 18:06:01 -08005213 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Daniel Sandler8680bf82012-05-15 16:52:52 -04005214 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005215 for (int i=0; i<N; i++) {
Gustav Sennton1463d832018-11-06 16:12:48 +00005216 Action action = nonContextualActions.get(i);
5217
Selim Cinekffcc7cf2018-02-06 17:43:51 -08005218 boolean actionHasValidInput = hasValidRemoteInput(action);
5219 validRemoteInput |= actionHasValidInput;
Adrian Roose458aa82015-12-08 16:17:19 -08005220
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005221 final RemoteViews button = generateActionButton(action, emphazisedMode, p);
Selim Cinek396caca2018-04-10 17:46:46 -07005222 if (actionHasValidInput && !emphazisedMode) {
Selim Cinekffcc7cf2018-02-06 17:43:51 -08005223 // Clear the drawable
5224 button.setInt(R.id.action0, "setBackgroundResource", 0);
5225 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005226 big.addView(R.id.actions, button);
5227 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07005228 } else {
5229 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005230 }
Adrian Roose458aa82015-12-08 16:17:19 -08005231
5232 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Lucas Dupin00be88f2019-01-03 17:50:52 -08005233 if (validRemoteInput && replyText != null
Selim Cinekbee4e072018-05-21 22:06:43 -07005234 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])
5235 && p.maxRemoteInputHistory > 0) {
Kenny Guya0f6de82018-04-06 16:20:16 +01005236 boolean showSpinner = mN.extras.getBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER);
Adrian Roose458aa82015-12-08 16:17:19 -08005237 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Kenny Guya0f6de82018-04-06 16:20:16 +01005238 big.setViewVisibility(R.id.notification_material_reply_text_1_container,
5239 View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005240 big.setTextViewText(R.id.notification_material_reply_text_1,
5241 processTextSpans(replyText[0]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005242 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1, p);
Kenny Guya0f6de82018-04-06 16:20:16 +01005243 big.setViewVisibility(R.id.notification_material_reply_progress,
5244 showSpinner ? View.VISIBLE : View.GONE);
5245 big.setProgressIndeterminateTintList(
5246 R.id.notification_material_reply_progress,
5247 ColorStateList.valueOf(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005248 isColorized(p) ? getPrimaryTextColor(p) : resolveContrastColor(p)));
Adrian Roose458aa82015-12-08 16:17:19 -08005249
Selim Cinekbee4e072018-05-21 22:06:43 -07005250 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])
5251 && p.maxRemoteInputHistory > 1) {
Adrian Roose458aa82015-12-08 16:17:19 -08005252 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005253 big.setTextViewText(R.id.notification_material_reply_text_2,
5254 processTextSpans(replyText[1]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005255 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2, p);
Adrian Roose458aa82015-12-08 16:17:19 -08005256
Selim Cinekbee4e072018-05-21 22:06:43 -07005257 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])
5258 && p.maxRemoteInputHistory > 2) {
Adrian Roose458aa82015-12-08 16:17:19 -08005259 big.setViewVisibility(
5260 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005261 big.setTextViewText(R.id.notification_material_reply_text_3,
5262 processTextSpans(replyText[2]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005263 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3, p);
Adrian Roose458aa82015-12-08 16:17:19 -08005264 }
5265 }
5266 }
5267
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005268 return big;
5269 }
5270
Adrian Roose458aa82015-12-08 16:17:19 -08005271 private boolean hasValidRemoteInput(Action action) {
5272 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
5273 // Weird actions
5274 return false;
5275 }
5276
5277 RemoteInput[] remoteInputs = action.getRemoteInputs();
5278 if (remoteInputs == null) {
5279 return false;
5280 }
5281
5282 for (RemoteInput r : remoteInputs) {
5283 CharSequence[] choices = r.getChoices();
5284 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
5285 return true;
5286 }
5287 }
5288 return false;
5289 }
5290
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005291 /**
5292 * Construct a RemoteViews for the final 1U notification layout. In order:
5293 * 1. Custom contentView from the caller
5294 * 2. Style's proposed content view
5295 * 3. Standard template view
5296 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005297 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005298 return createContentView(false /* increasedheight */ );
5299 }
5300
5301 /**
5302 * Construct a RemoteViews for the smaller content view.
5303 *
5304 * @param increasedHeight true if this layout be created with an increased height. Some
5305 * styles may support showing more then just that basic 1U size
5306 * and the system may decide to render important notifications
5307 * slightly bigger even when collapsed.
5308 *
5309 * @hide
5310 */
5311 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005312 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005313 return mN.contentView;
5314 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005315 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005316 if (styleView != null) {
5317 return styleView;
5318 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005319 }
Selim Cinek384804b2018-04-18 14:31:07 +08005320 return applyStandardTemplate(getBaseLayoutResource(), null /* result */);
Joe Onorato46439ce2010-11-19 13:56:21 -08005321 }
5322
Selim Cineka7679b62017-05-10 16:33:25 -07005323 private boolean useExistingRemoteView() {
5324 return mStyle == null || (!mStyle.displayCustomViewInline()
5325 && !mRebuildStyledRemoteViews);
5326 }
5327
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005328 /**
5329 * Construct a RemoteViews for the final big notification layout.
5330 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005331 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05005332 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07005333 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005334 return mN.bigContentView;
5335 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05005336 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08005337 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005338 } else if (mActions.size() != 0) {
Selim Cinek384804b2018-04-18 14:31:07 +08005339 result = applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5340 null /* result */);
Selim Cinek850a8542015-11-11 11:48:36 -05005341 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08005342 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05005343 return result;
5344 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005345
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005346 /**
Selim Cinek414ad332017-02-24 19:06:12 -08005347 * Construct a RemoteViews for the final notification header only. This will not be
5348 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005349 *
5350 * @hide
5351 */
Lucas Dupin00be88f2019-01-03 17:50:52 -08005352 public RemoteViews makeNotificationHeader() {
5353 return makeNotificationHeader(mParams.reset().fillTextsFrom(this));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005354 }
5355
5356 /**
5357 * Construct a RemoteViews for the final notification header only. This will not be
5358 * colorized.
5359 *
5360 * @param p the template params to inflate this with
5361 */
5362 private RemoteViews makeNotificationHeader(StandardTemplateParams p) {
5363 // Headers on their own are never colorized
5364 p.disallowColorization();
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005365 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Lucas Dupin00be88f2019-01-03 17:50:52 -08005366 R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005367 resetNotificationHeader(header);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005368 bindNotificationHeader(header, p);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005369 return header;
5370 }
5371
Adrian Roos487374f2017-01-11 15:48:14 -08005372 /**
5373 * Construct a RemoteViews for the ambient version of the notification.
5374 *
5375 * @hide
5376 */
5377 public RemoteViews makeAmbientNotification() {
Selim Cinek0e069942019-01-24 16:09:41 -08005378 RemoteViews headsUpContentView = createHeadsUpContentView(false /* increasedHeight */);
5379 if (headsUpContentView != null) {
5380 return headsUpContentView;
5381 }
5382 return createContentView();
Adrian Roos487374f2017-01-11 15:48:14 -08005383 }
5384
Selim Cinek29603462015-11-17 19:04:39 -08005385 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005386 if (result != null) {
5387 result.setViewVisibility(R.id.text_line_1, View.GONE);
5388 }
Selim Cinek29603462015-11-17 19:04:39 -08005389 }
5390
Selim Cinek6743c0b2017-01-18 18:24:01 -08005391 /**
5392 * Adapt the Notification header if this view is used as an expanded view.
5393 *
5394 * @hide
5395 */
5396 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005397 if (result != null) {
5398 result.setBoolean(R.id.notification_header, "setExpanded", true);
5399 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005400 }
5401
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005402 /**
5403 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005404 *
5405 * @param increasedHeight true if this layout be created with an increased height. Some
5406 * styles may support showing more then just that basic 1U size
5407 * and the system may decide to render important notifications
5408 * slightly bigger even when collapsed.
5409 *
5410 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005411 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005412 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005413 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005414 return mN.headsUpContentView;
5415 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08005416 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
5417 if (styleView != null) {
5418 return styleView;
5419 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05005420 } else if (mActions.size() == 0) {
5421 return null;
5422 }
5423
Selim Cinekbee4e072018-05-21 22:06:43 -07005424 // We only want at most a single remote input history to be shown here, otherwise
5425 // the content would become squished.
5426 StandardTemplateParams p = mParams.reset().fillTextsFrom(this)
5427 .setMaxRemoteInputHistory(1);
5428 return applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5429 p,
5430 null /* result */);
Chris Wren8fd39ec2014-02-27 17:43:26 -05005431 }
5432
Selim Cinek624c02db2015-12-14 21:00:02 -08005433 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08005434 * Construct a RemoteViews for the final heads-up notification layout.
5435 */
5436 public RemoteViews createHeadsUpContentView() {
5437 return createHeadsUpContentView(false /* useIncreasedHeight */);
5438 }
5439
5440 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08005441 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5442 *
5443 * @hide
5444 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005445 @UnsupportedAppUsage
Selim Cinek624c02db2015-12-14 21:00:02 -08005446 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005447 return makePublicView(false /* ambient */);
5448 }
5449
5450 /**
5451 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5452 *
5453 * @hide
5454 */
5455 public RemoteViews makePublicAmbientNotification() {
5456 return makePublicView(true /* ambient */);
5457 }
5458
5459 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08005460 if (mN.publicVersion != null) {
5461 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005462 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08005463 }
5464 Bundle savedBundle = mN.extras;
5465 Style style = mStyle;
5466 mStyle = null;
5467 Icon largeIcon = mN.mLargeIcon;
5468 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07005469 Bitmap largeIconLegacy = mN.largeIcon;
5470 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005471 ArrayList<Action> actions = mActions;
5472 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08005473 Bundle publicExtras = new Bundle();
5474 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
5475 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
5476 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
5477 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07005478 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
5479 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cineked07b962018-04-30 14:39:35 -07005480 String appName = savedBundle.getString(EXTRA_SUBSTITUTE_APP_NAME);
5481 if (appName != null) {
5482 publicExtras.putString(EXTRA_SUBSTITUTE_APP_NAME, appName);
5483 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005484 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07005485 RemoteViews view;
Lucas Dupin00be88f2019-01-03 17:50:52 -08005486 view = makeNotificationHeader();
5487 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
Selim Cinek624c02db2015-12-14 21:00:02 -08005488 mN.extras = savedBundle;
5489 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07005490 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005491 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08005492 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005493 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08005494 }
5495
Selim Cinek6743c0b2017-01-18 18:24:01 -08005496 /**
5497 * Construct a content view for the display when low - priority
5498 *
5499 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
5500 * a new subtext is created consisting of the content of the
5501 * notification.
5502 * @hide
5503 */
5504 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005505 StandardTemplateParams p = mParams.reset()
5506 .forceDefaultColor()
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005507 .fillTextsFrom(this);
5508 if (!useRegularSubtext || TextUtils.isEmpty(mParams.summaryText)) {
5509 p.summaryText(createSummaryText());
Selim Cinek6743c0b2017-01-18 18:24:01 -08005510 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005511 RemoteViews header = makeNotificationHeader(p);
Selim Cinek1b554392017-02-28 17:22:49 -08005512 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08005513 return header;
5514 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005515
Selim Cinek6743c0b2017-01-18 18:24:01 -08005516 private CharSequence createSummaryText() {
5517 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
5518 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
5519 return titleText;
5520 }
5521 SpannableStringBuilder summary = new SpannableStringBuilder();
5522 if (titleText == null) {
5523 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
5524 }
5525 BidiFormatter bidi = BidiFormatter.getInstance();
5526 if (titleText != null) {
5527 summary.append(bidi.unicodeWrap(titleText));
5528 }
5529 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
5530 if (titleText != null && contentText != null) {
5531 summary.append(bidi.unicodeWrap(mContext.getText(
5532 R.string.notification_header_divider_symbol_with_spaces)));
5533 }
5534 if (contentText != null) {
5535 summary.append(bidi.unicodeWrap(contentText));
5536 }
5537 return summary;
5538 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05005539
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005540 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005541 StandardTemplateParams p) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04005542 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07005543 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005544 emphazisedMode ? getEmphasizedActionLayoutResource()
5545 : tombstone ? getActionTombstoneLayoutResource()
5546 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04005547 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04005548 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04005549 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005550 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005551 if (action.mRemoteInputs != null) {
5552 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
5553 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005554 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07005555 // change the background bgColor
Selim Cinek981962e2016-07-20 20:41:58 -07005556 CharSequence title = action.title;
5557 ColorStateList[] outResultColor = null;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005558 int background = resolveBackgroundColor(p);
Selim Cinek981962e2016-07-20 20:41:58 -07005559 if (isLegacy()) {
Lucas Dupina291d192018-06-07 13:59:42 -07005560 title = ContrastColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07005561 } else {
5562 outResultColor = new ColorStateList[1];
Selim Cinek396caca2018-04-10 17:46:46 -07005563 title = ensureColorSpanContrast(title, background, outResultColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005564 }
Selim Cinek48f66b72017-08-18 16:17:51 -07005565 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005566 setTextViewColorPrimary(button, R.id.action0, p);
Selim Cinek396caca2018-04-10 17:46:46 -07005567 int rippleColor;
5568 boolean hasColorOverride = outResultColor != null && outResultColor[0] != null;
5569 if (hasColorOverride) {
5570 // There's a span spanning the full text, let's take it and use it as the
5571 // background color
5572 background = outResultColor[0].getDefaultColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005573 int textColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07005574 background, mInNightMode);
Selim Cinek396caca2018-04-10 17:46:46 -07005575 button.setTextColor(R.id.action0, textColor);
5576 rippleColor = textColor;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005577 } else if (getRawColor(p) != COLOR_DEFAULT && !isColorized(p)
Lucas Dupinf15c5e52019-04-01 19:00:12 -07005578 && mTintActionButtons && !mInNightMode) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005579 rippleColor = resolveContrastColor(p);
Selim Cinek396caca2018-04-10 17:46:46 -07005580 button.setTextColor(R.id.action0, rippleColor);
5581 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005582 rippleColor = getPrimaryTextColor(p);
Selim Cinek981962e2016-07-20 20:41:58 -07005583 }
Selim Cinek396caca2018-04-10 17:46:46 -07005584 // We only want about 20% alpha for the ripple
5585 rippleColor = (rippleColor & 0x00ffffff) | 0x33000000;
5586 button.setColorStateList(R.id.action0, "setRippleColor",
5587 ColorStateList.valueOf(rippleColor));
5588 button.setColorStateList(R.id.action0, "setButtonBackground",
5589 ColorStateList.valueOf(background));
5590 button.setBoolean(R.id.action0, "setHasStroke", !hasColorOverride);
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005591 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07005592 button.setTextViewText(R.id.action0, processTextSpans(
5593 processLegacyText(action.title)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005594 if (isColorized(p)) {
5595 setTextViewColorPrimary(button, R.id.action0, p);
5596 } else if (getRawColor(p) != COLOR_DEFAULT && mTintActionButtons) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005597 button.setTextColor(R.id.action0, resolveContrastColor(p));
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005598 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005599 }
Tony Mak7d4b3a52018-11-27 17:29:36 +00005600 button.setIntTag(R.id.action0, R.id.notification_action_index_tag,
5601 mActions.indexOf(action));
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005602 return button;
5603 }
5604
Joe Onoratocb109a02011-01-18 17:57:41 -08005605 /**
Selim Cinek981962e2016-07-20 20:41:58 -07005606 * Ensures contrast on color spans against a background color. also returns the color of the
5607 * text if a span was found that spans over the whole text.
5608 *
5609 * @param charSequence the charSequence on which the spans are
5610 * @param background the background color to ensure the contrast against
5611 * @param outResultColor an array in which a color will be returned as the first element if
5612 * there exists a full length color span.
5613 * @return the contrasted charSequence
5614 */
5615 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
5616 ColorStateList[] outResultColor) {
5617 if (charSequence instanceof Spanned) {
5618 Spanned ss = (Spanned) charSequence;
5619 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
5620 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
5621 for (Object span : spans) {
5622 Object resultSpan = span;
5623 int spanStart = ss.getSpanStart(span);
5624 int spanEnd = ss.getSpanEnd(span);
5625 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
5626 if (resultSpan instanceof CharacterStyle) {
5627 resultSpan = ((CharacterStyle) span).getUnderlying();
5628 }
5629 if (resultSpan instanceof TextAppearanceSpan) {
5630 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
5631 ColorStateList textColor = originalSpan.getTextColor();
5632 if (textColor != null) {
5633 int[] colors = textColor.getColors();
5634 int[] newColors = new int[colors.length];
5635 for (int i = 0; i < newColors.length; i++) {
Lucas Dupina291d192018-06-07 13:59:42 -07005636 newColors[i] = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005637 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005638 }
5639 textColor = new ColorStateList(textColor.getStates().clone(),
5640 newColors);
Selim Cinek396caca2018-04-10 17:46:46 -07005641 if (fullLength) {
5642 outResultColor[0] = textColor;
5643 // Let's drop the color from the span
5644 textColor = null;
5645 }
Selim Cinek981962e2016-07-20 20:41:58 -07005646 resultSpan = new TextAppearanceSpan(
5647 originalSpan.getFamily(),
5648 originalSpan.getTextStyle(),
5649 originalSpan.getTextSize(),
5650 textColor,
5651 originalSpan.getLinkTextColor());
Selim Cinek981962e2016-07-20 20:41:58 -07005652 }
5653 } else if (resultSpan instanceof ForegroundColorSpan) {
5654 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
5655 int foregroundColor = originalSpan.getForegroundColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005656 foregroundColor = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005657 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005658 if (fullLength) {
5659 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
Selim Cinek396caca2018-04-10 17:46:46 -07005660 resultSpan = null;
5661 } else {
5662 resultSpan = new ForegroundColorSpan(foregroundColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005663 }
5664 } else {
5665 resultSpan = span;
5666 }
Selim Cinek396caca2018-04-10 17:46:46 -07005667 if (resultSpan != null) {
5668 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
5669 }
Selim Cinek981962e2016-07-20 20:41:58 -07005670 }
5671 return builder;
5672 }
5673 return charSequence;
5674 }
5675
5676 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005677 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07005678 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005679 */
5680 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005681 if (!mIsLegacyInitialized) {
5682 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
5683 < Build.VERSION_CODES.LOLLIPOP;
5684 mIsLegacyInitialized = true;
5685 }
5686 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005687 }
5688
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005689 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08005690 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
Lucas Dupin00be88f2019-01-03 17:50:52 -08005691 if (isAlreadyLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005692 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005693 } else {
5694 return charSequence;
5695 }
5696 }
5697
Dan Sandler26e81cf2014-05-06 10:01:27 -04005698 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005699 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04005700 */
Adrian Roos487374f2017-01-11 15:48:14 -08005701 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005702 StandardTemplateParams p) {
Selim Cinekea4bef72015-12-02 15:51:10 -08005703 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005704 int color;
Lucas Dupin00be88f2019-01-03 17:50:52 -08005705 if (isColorized(p)) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005706 color = getPrimaryTextColor(p);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005707 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005708 color = resolveContrastColor(p);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005709 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005710 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07005711 contentView.setDrawableTint(R.id.icon, false, color,
5712 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005713
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005714 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005715 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08005716 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005717 }
5718
Dan Sandler26e81cf2014-05-06 10:01:27 -04005719 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005720 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04005721 * if it's grayscale).
5722 */
5723 // TODO: also check bounds, transparency, that sort of thing.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005724 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView,
5725 StandardTemplateParams p) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04005726 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005727 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005728 // resolve color will fall back to the default when legacy
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005729 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(p),
Sunny Goyal5b153922017-09-21 21:00:36 -07005730 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01005731 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005732 }
5733
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05005734 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005735 if (mN.color != COLOR_DEFAULT) {
5736 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02005737 }
Jorim Jaggi74419312014-06-10 20:57:21 +02005738 }
5739
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005740 int resolveContrastColor(StandardTemplateParams p) {
5741 int rawColor = getRawColor(p);
5742 if (mCachedContrastColorIsFor == rawColor && mCachedContrastColor != COLOR_INVALID) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08005743 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005744 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005745
Selim Cinekac5f0272017-05-02 16:05:41 -07005746 int color;
Selim Cinekc7f5a822018-03-20 19:32:06 -07005747 int background = mContext.getColor(
5748 com.android.internal.R.color.notification_material_background_color);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005749 if (rawColor == COLOR_DEFAULT) {
5750 ensureColors(p);
Lucas Dupinf03e7522018-06-25 16:21:13 -07005751 color = ContrastColorUtil.resolveDefaultColor(mContext, background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005752 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005753 color = ContrastColorUtil.resolveContrastColor(mContext, rawColor,
Anthony Chenad4d1582017-04-10 16:07:58 -07005754 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005755 }
5756 if (Color.alpha(color) < 255) {
5757 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005758 color = ContrastColorUtil.compositeColors(color, background);
Selim Cinekac5f0272017-05-02 16:05:41 -07005759 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005760 mCachedContrastColorIsFor = rawColor;
Selim Cinekac5f0272017-05-02 16:05:41 -07005761 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005762 }
5763
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005764 /**
5765 * Return the raw color of this Notification, which doesn't necessarily satisfy contrast.
5766 *
5767 * @see #resolveContrastColor(StandardTemplateParams) for the contrasted color
5768 * @param p the template params to inflate this with
5769 */
5770 private int getRawColor(StandardTemplateParams p) {
5771 if (p.forceDefaultColor) {
5772 return COLOR_DEFAULT;
5773 }
5774 return mN.color;
5775 }
5776
Selim Cinek4717d862018-04-19 09:19:15 +08005777 int resolveNeutralColor() {
5778 if (mNeutralColor != COLOR_INVALID) {
5779 return mNeutralColor;
5780 }
5781 int background = mContext.getColor(
5782 com.android.internal.R.color.notification_material_background_color);
Lucas Dupinf03e7522018-06-25 16:21:13 -07005783 mNeutralColor = ContrastColorUtil.resolveDefaultColor(mContext, background,
5784 mInNightMode);
Selim Cinek4717d862018-04-19 09:19:15 +08005785 if (Color.alpha(mNeutralColor) < 255) {
5786 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005787 mNeutralColor = ContrastColorUtil.compositeColors(mNeutralColor, background);
Selim Cinek4717d862018-04-19 09:19:15 +08005788 }
5789 return mNeutralColor;
5790 }
5791
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005792 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005793 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005794 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08005795 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005796 @NonNull
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005797 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005798 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005799 mN.actions = new Action[mActions.size()];
5800 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005801 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005802 if (!mPersonList.isEmpty()) {
Selim Cineke7238dd2017-12-14 17:48:32 -08005803 mN.extras.putParcelableArrayList(EXTRA_PEOPLE_LIST, mPersonList);
Dan Sandler0bf2ed82013-12-21 23:33:41 -06005804 }
Selim Cinek247fa012016-02-18 09:50:48 -08005805 if (mN.bigContentView != null || mN.contentView != null
5806 || mN.headsUpContentView != null) {
5807 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
5808 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005809 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08005810 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005811
Julia Reynolds3b848122016-02-26 10:45:32 -05005812 /**
5813 * Creates a Builder from an existing notification so further changes can be made.
5814 * @param context The context for your application / activity.
5815 * @param n The notification to create a Builder from.
5816 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005817 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005818 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005819 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005820 ApplicationInfo applicationInfo = n.extras.getParcelable(
5821 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005822 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05005823 if (applicationInfo != null) {
5824 try {
5825 builderContext = context.createApplicationContext(applicationInfo,
5826 Context.CONTEXT_RESTRICTED);
5827 } catch (NameNotFoundException e) {
5828 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
5829 builderContext = context; // try with our context
5830 }
5831 } else {
5832 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02005833 }
5834
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005835 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005836 }
5837
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005838 /**
Gustav Sennton761884c2018-11-19 17:40:19 +00005839 * Determines whether the platform can generate contextual actions for a notification.
5840 * By default this is true.
5841 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005842 @NonNull
Gustav Sennton761884c2018-11-19 17:40:19 +00005843 public Builder setAllowSystemGeneratedContextualActions(boolean allowed) {
5844 mN.mAllowSystemGeneratedContextualActions = allowed;
5845 return this;
5846 }
5847
5848 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005849 * @deprecated Use {@link #build()} instead.
5850 */
5851 @Deprecated
5852 public Notification getNotification() {
5853 return build();
5854 }
5855
5856 /**
5857 * Combine all of the options that have been set and return a new {@link Notification}
5858 * object.
5859 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005860 @NonNull
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005861 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005862 // first, add any extras from the calling code
5863 if (mUserExtras != null) {
5864 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005865 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005866
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005867 mN.creationTime = System.currentTimeMillis();
5868
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005869 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05005870 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02005871
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005872 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005873
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005874 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02005875 mStyle.reduceImageSizes(mContext);
5876 mStyle.purgeResources();
Selim Cinek90343862018-02-01 11:07:11 -08005877 mStyle.validate(mContext);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005878 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005879 }
Selim Cinekd0426622017-07-11 13:19:59 +02005880 mN.reduceImageSizes(mContext);
5881
Adrian Roos5081c0d2016-02-26 16:04:19 -08005882 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07005883 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005884 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005885 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005886 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
5887 mN.contentView.getSequenceNumber());
5888 }
5889 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005890 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005891 if (mN.bigContentView != null) {
5892 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
5893 mN.bigContentView.getSequenceNumber());
5894 }
5895 }
5896 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005897 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005898 if (mN.headsUpContentView != null) {
5899 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
5900 mN.headsUpContentView.getSequenceNumber());
5901 }
5902 }
5903 }
5904
Julia Reynolds4c0c2022016-02-02 15:11:59 -05005905 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
5906 mN.flags |= FLAG_SHOW_LIGHTS;
5907 }
5908
Adrian Roosfb921842017-10-26 14:49:56 +02005909 mN.allPendingIntents = null;
5910
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005911 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005912 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005913
5914 /**
5915 * Apply this Builder to an existing {@link Notification} object.
5916 *
5917 * @hide
5918 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005919 @NonNull
5920 public Notification buildInto(@NonNull Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005921 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005922 return n;
5923 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005924
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005925 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005926 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005927 * change. Also removes extenders on low ram devices, as
5928 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005929 *
5930 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5931 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005932 * @hide
5933 */
Kristian Monsen05f34792018-04-09 10:27:16 +02005934 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam,
5935 Context context) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005936 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005937
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005938 // Only strip views for known Styles because we won't know how to
5939 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005940 if (!isLowRam
5941 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005942 && getNotificationStyleClass(templateClass) == null) {
5943 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005944 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005945
5946 // Only strip unmodified BuilderRemoteViews.
5947 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005948 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005949 n.contentView.getSequenceNumber();
5950 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005951 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005952 n.bigContentView.getSequenceNumber();
5953 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005954 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005955 n.headsUpContentView.getSequenceNumber();
5956
5957 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005958 if (!isLowRam
5959 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005960 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005961 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005962
5963 Notification clone = n.clone();
5964 if (stripContentView) {
5965 clone.contentView = null;
5966 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5967 }
5968 if (stripBigContentView) {
5969 clone.bigContentView = null;
5970 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5971 }
5972 if (stripHeadsUpContentView) {
5973 clone.headsUpContentView = null;
5974 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5975 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005976 if (isLowRam) {
Kristian Monsen05f34792018-04-09 10:27:16 +02005977 String[] allowedServices = context.getResources().getStringArray(
5978 R.array.config_allowedManagedServicesOnLowRamDevices);
5979 if (allowedServices.length == 0) {
5980 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5981 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5982 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5983 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005984 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005985 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005986 }
5987
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005988 @UnsupportedAppUsage
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005989 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005990 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005991 }
5992
5993 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005994 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005995 }
5996
5997 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005998 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005999 }
6000
6001 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02006002 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006003 }
6004
6005 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07006006 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006007 }
6008
Adrian Roosc1a80b02016-04-05 14:54:55 -07006009 private int getMessagingLayoutResource() {
6010 return R.layout.notification_template_material_messaging;
6011 }
6012
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006013 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07006014 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006015 }
6016
Selim Cinek06e9e1f2016-07-08 17:14:16 -07006017 private int getEmphasizedActionLayoutResource() {
6018 return R.layout.notification_material_action_emphasized;
6019 }
6020
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006021 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07006022 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006023 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08006024
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006025 private int getBackgroundColor(StandardTemplateParams p) {
6026 if (isColorized(p)) {
6027 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : getRawColor(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08006028 } else {
Selim Cinekc7f5a822018-03-20 19:32:06 -07006029 return COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08006030 }
6031 }
6032
Selim Cinek396caca2018-04-10 17:46:46 -07006033 /**
Selim Cinek4717d862018-04-19 09:19:15 +08006034 * Gets a neutral color that can be used for icons or similar that should not stand out.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006035 * @param p the template params to inflate this with
Selim Cinek4717d862018-04-19 09:19:15 +08006036 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006037 private int getNeutralColor(StandardTemplateParams p) {
6038 if (isColorized(p)) {
6039 return getSecondaryTextColor(p);
Selim Cinek4717d862018-04-19 09:19:15 +08006040 } else {
6041 return resolveNeutralColor();
6042 }
6043 }
6044
6045 /**
Selim Cinek396caca2018-04-10 17:46:46 -07006046 * Same as getBackgroundColor but also resolved the default color to the background.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006047 * @param p the template params to inflate this with
Selim Cinek396caca2018-04-10 17:46:46 -07006048 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006049 private int resolveBackgroundColor(StandardTemplateParams p) {
6050 int backgroundColor = getBackgroundColor(p);
Selim Cinek396caca2018-04-10 17:46:46 -07006051 if (backgroundColor == COLOR_DEFAULT) {
6052 backgroundColor = mContext.getColor(
6053 com.android.internal.R.color.notification_material_background_color);
6054 }
6055 return backgroundColor;
6056 }
6057
Anthony Chenad4d1582017-04-10 16:07:58 -07006058 private boolean shouldTintActionButtons() {
6059 return mTintActionButtons;
6060 }
6061
Selim Cinek99104832017-01-25 14:47:33 -08006062 private boolean textColorsNeedInversion() {
6063 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
6064 return false;
6065 }
6066 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
6067 return targetSdkVersion > Build.VERSION_CODES.M
6068 && targetSdkVersion < Build.VERSION_CODES.O;
6069 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07006070
6071 /**
6072 * Set a color palette to be used as the background and textColors
6073 *
6074 * @param backgroundColor the color to be used as the background
6075 * @param foregroundColor the color to be used as the foreground
6076 *
6077 * @hide
6078 */
6079 public void setColorPalette(int backgroundColor, int foregroundColor) {
6080 mBackgroundColor = backgroundColor;
6081 mForegroundColor = foregroundColor;
6082 mTextColorsAreForBackground = COLOR_INVALID;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006083 ensureColors(mParams.reset().fillTextsFrom(this));
Selim Cinek5fb73f82017-04-20 16:55:38 -07006084 }
Selim Cinekac5f0272017-05-02 16:05:41 -07006085
6086 /**
Selim Cineka7679b62017-05-10 16:33:25 -07006087 * Forces all styled remoteViews to be built from scratch and not use any cached
6088 * RemoteViews.
6089 * This is needed for legacy apps that are baking in their remoteviews into the
6090 * notification.
6091 *
6092 * @hide
6093 */
6094 public void setRebuildStyledRemoteViews(boolean rebuild) {
6095 mRebuildStyledRemoteViews = rebuild;
6096 }
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006097
6098 /**
6099 * Get the text that should be displayed in the statusBar when heads upped. This is
6100 * usually just the app name, but may be different depending on the style.
6101 *
6102 * @param publicMode If true, return a text that is safe to display in public.
6103 *
6104 * @hide
6105 */
6106 public CharSequence getHeadsUpStatusBarText(boolean publicMode) {
6107 if (mStyle != null && !publicMode) {
6108 CharSequence text = mStyle.getHeadsUpStatusBarText();
6109 if (!TextUtils.isEmpty(text)) {
6110 return text;
6111 }
6112 }
6113 return loadHeaderAppName();
6114 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08006115 }
6116
6117 /**
Selim Cinekd0426622017-07-11 13:19:59 +02006118 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
6119 * remote views.
6120 *
6121 * @hide
6122 */
6123 void reduceImageSizes(Context context) {
6124 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
6125 return;
6126 }
Selim Cineka8cb1262017-08-15 16:53:44 -07006127 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006128 if (mLargeIcon != null || largeIcon != null) {
6129 Resources resources = context.getResources();
6130 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07006131 int maxWidth = resources.getDimensionPixelSize(isLowRam
6132 ? R.dimen.notification_right_icon_size_low_ram
6133 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006134 int maxHeight = maxWidth;
6135 if (MediaStyle.class.equals(style)
6136 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006137 maxHeight = resources.getDimensionPixelSize(isLowRam
6138 ? R.dimen.notification_media_image_max_height_low_ram
6139 : R.dimen.notification_media_image_max_height);
6140 maxWidth = resources.getDimensionPixelSize(isLowRam
6141 ? R.dimen.notification_media_image_max_width_low_ram
6142 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006143 }
6144 if (mLargeIcon != null) {
6145 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
6146 }
6147 if (largeIcon != null) {
6148 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
6149 }
6150 }
Selim Cineka8cb1262017-08-15 16:53:44 -07006151 reduceImageSizesForRemoteView(contentView, context, isLowRam);
6152 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
6153 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02006154 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
6155 }
6156
Selim Cineka8cb1262017-08-15 16:53:44 -07006157 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
6158 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02006159 if (remoteView != null) {
6160 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006161 int maxWidth = resources.getDimensionPixelSize(isLowRam
6162 ? R.dimen.notification_custom_view_max_image_width_low_ram
6163 : R.dimen.notification_custom_view_max_image_width);
6164 int maxHeight = resources.getDimensionPixelSize(isLowRam
6165 ? R.dimen.notification_custom_view_max_image_height_low_ram
6166 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02006167 remoteView.reduceImageSizes(maxWidth, maxHeight);
6168 }
6169 }
6170
6171 /**
Selim Cinek22714f12017-04-13 16:23:53 -07006172 * @return whether this notification is a foreground service notification
Gus Prevasb0c1a462018-11-05 11:06:15 -05006173 * @hide
Selim Cinek7b9605b2017-01-19 17:36:00 -08006174 */
Gus Prevasb0c1a462018-11-05 11:06:15 -05006175 public boolean isForegroundService() {
Selim Cinek22714f12017-04-13 16:23:53 -07006176 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08006177 }
6178
6179 /**
Selim Cinek99104832017-01-25 14:47:33 -08006180 * @return whether this notification has a media session attached
6181 * @hide
6182 */
6183 public boolean hasMediaSession() {
6184 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
6185 }
6186
6187 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006188 * @return the style class of this notification
6189 * @hide
6190 */
6191 public Class<? extends Notification.Style> getNotificationStyle() {
6192 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
6193
6194 if (!TextUtils.isEmpty(templateClass)) {
6195 return Notification.getNotificationStyleClass(templateClass);
6196 }
6197 return null;
6198 }
6199
6200 /**
Selim Cinek22714f12017-04-13 16:23:53 -07006201 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08006202 *
6203 * @hide
6204 */
6205 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07006206 if (isColorizedMedia()) {
6207 return true;
6208 }
Julia Reynolds4db59552017-06-30 13:34:01 -04006209 return extras.getBoolean(EXTRA_COLORIZED)
6210 && (hasColorizedPermission() || isForegroundService());
6211 }
6212
6213 /**
6214 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
6215 * permission. The permission is checked when a notification is enqueued.
6216 */
6217 private boolean hasColorizedPermission() {
6218 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07006219 }
6220
6221 /**
6222 * @return true if this notification is colorized and it is a media notification
6223 *
6224 * @hide
6225 */
6226 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08006227 Class<? extends Style> style = getNotificationStyle();
6228 if (MediaStyle.class.equals(style)) {
6229 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
6230 if ((colorized == null || colorized) && hasMediaSession()) {
6231 return true;
6232 }
6233 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6234 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
6235 return true;
6236 }
6237 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07006238 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006239 }
6240
Selim Cinek0847acd2017-04-24 19:48:29 -07006241
6242 /**
6243 * @return true if this is a media notification
6244 *
6245 * @hide
6246 */
6247 public boolean isMediaNotification() {
6248 Class<? extends Style> style = getNotificationStyle();
6249 if (MediaStyle.class.equals(style)) {
6250 return true;
6251 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6252 return true;
6253 }
6254 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006255 }
6256
Mady Mellor7eb18ef2019-03-27 14:03:46 -07006257 /**
Julia Tuttle28fb1a92019-05-03 18:07:45 -04006258 * @return true if this notification is showing as a bubble
Mady Mellor7eb18ef2019-03-27 14:03:46 -07006259 *
6260 * @hide
6261 */
6262 public boolean isBubbleNotification() {
6263 return (flags & Notification.FLAG_BUBBLE) != 0;
6264 }
6265
Selim Cinek279fa862016-06-14 10:57:25 -07006266 private boolean hasLargeIcon() {
6267 return mLargeIcon != null || largeIcon != null;
6268 }
6269
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006270 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006271 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006272 * @hide
6273 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07006274 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006275 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
6276 }
6277
6278 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006279 * @return true if the notification will show a chronometer; false otherwise
6280 * @hide
6281 */
6282 public boolean showsChronometer() {
6283 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
6284 }
6285
6286 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04006287 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05006288 */
6289 @SystemApi
6290 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
6291 Class<? extends Style>[] classes = new Class[] {
6292 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
6293 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
6294 MessagingStyle.class };
6295 for (Class<? extends Style> innerClass : classes) {
6296 if (templateClass.equals(innerClass.getName())) {
6297 return innerClass;
6298 }
6299 }
6300 return null;
6301 }
6302
6303 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006304 * An object that can apply a rich notification style to a {@link Notification.Builder}
6305 * object.
6306 */
Griff Hazendfcb0802014-02-11 12:00:00 -08006307 public static abstract class Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07006308
6309 /**
6310 * The number of items allowed simulatanously in the remote input history.
6311 * @hide
6312 */
6313 static final int MAX_REMOTE_INPUT_HISTORY_LINES = 3;
Chris Wrend6297db2012-05-03 16:20:13 -04006314 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006315
6316 /**
6317 * @hide
6318 */
6319 protected CharSequence mSummaryText = null;
6320
6321 /**
6322 * @hide
6323 */
6324 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04006325
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006326 protected Builder mBuilder;
6327
Chris Wrend6297db2012-05-03 16:20:13 -04006328 /**
6329 * Overrides ContentTitle in the big form of the template.
6330 * This defaults to the value passed to setContentTitle().
6331 */
6332 protected void internalSetBigContentTitle(CharSequence title) {
6333 mBigContentTitle = title;
6334 }
6335
6336 /**
6337 * Set the first line of text after the detail section in the big form of the template.
6338 */
6339 protected void internalSetSummaryText(CharSequence cs) {
6340 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04006341 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04006342 }
6343
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006344 public void setBuilder(Builder builder) {
6345 if (mBuilder != builder) {
6346 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07006347 if (mBuilder != null) {
6348 mBuilder.setStyle(this);
6349 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006350 }
6351 }
6352
Chris Wrend6297db2012-05-03 16:20:13 -04006353 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006354 if (mBuilder == null) {
6355 throw new IllegalArgumentException("Style requires a valid Builder object");
6356 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006357 }
Chris Wrend6297db2012-05-03 16:20:13 -04006358
6359 protected RemoteViews getStandardView(int layoutId) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006360 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder);
6361 return getStandardView(layoutId, p, null);
Selim Cinek384804b2018-04-18 14:31:07 +08006362 }
6363
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006364
Selim Cinek384804b2018-04-18 14:31:07 +08006365 /**
6366 * Get the standard view for this style.
6367 *
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006368 * @param layoutId The layout id to use.
6369 * @param p the params for this inflation.
Selim Cinek384804b2018-04-18 14:31:07 +08006370 * @param result The result where template bind information is saved.
6371 * @return A remoteView for this style.
6372 * @hide
6373 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006374 protected RemoteViews getStandardView(int layoutId, StandardTemplateParams p,
6375 TemplateBindResult result) {
Chris Wrend6297db2012-05-03 16:20:13 -04006376 checkBuilder();
6377
6378 if (mBigContentTitle != null) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006379 p.title = mBigContentTitle;
Chris Wrend6297db2012-05-03 16:20:13 -04006380 }
6381
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006382 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId, p,
6383 result);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006384
Chris Wrend6297db2012-05-03 16:20:13 -04006385 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
6386 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04006387 } else {
6388 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04006389 }
6390
Chris Wrend6297db2012-05-03 16:20:13 -04006391 return contentView;
6392 }
6393
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006394 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006395 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006396 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08006397 *
6398 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006399 * @hide
6400 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08006401 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006402 return null;
6403 }
6404
6405 /**
6406 * Construct a Style-specific RemoteViews for the final big notification layout.
6407 * @hide
6408 */
6409 public RemoteViews makeBigContentView() {
6410 return null;
6411 }
6412
6413 /**
6414 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08006415 *
6416 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006417 * @hide
6418 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006419 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006420 return null;
6421 }
6422
6423 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006424 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006425 * @hide
6426 */
6427 public void addExtras(Bundle extras) {
6428 if (mSummaryTextSet) {
6429 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
6430 }
6431 if (mBigContentTitle != null) {
6432 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
6433 }
Chris Wren91ad5632013-06-05 15:05:57 -04006434 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006435 }
6436
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006437 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006438 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006439 * @hide
6440 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02006441 protected void restoreFromExtras(Bundle extras) {
6442 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
6443 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
6444 mSummaryTextSet = true;
6445 }
6446 if (extras.containsKey(EXTRA_TITLE_BIG)) {
6447 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
6448 }
6449 }
6450
6451
6452 /**
6453 * @hide
6454 */
6455 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006456 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006457 return wip;
6458 }
6459
Daniel Sandler0ec46202015-06-24 01:27:05 -04006460 /**
6461 * @hide
6462 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006463 public void purgeResources() {}
6464
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006465 /**
6466 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
6467 * attached to.
6468 *
6469 * @return the fully constructed Notification.
6470 */
6471 public Notification build() {
6472 checkBuilder();
6473 return mBuilder.build();
6474 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006475
6476 /**
6477 * @hide
6478 * @return true if the style positions the progress bar on the second line; false if the
6479 * style hides the progress bar
6480 */
6481 protected boolean hasProgress() {
6482 return true;
6483 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006484
6485 /**
6486 * @hide
6487 * @return Whether we should put the summary be put into the notification header
6488 */
6489 public boolean hasSummaryInHeader() {
6490 return true;
6491 }
Selim Cinek593610c2016-02-16 18:42:57 -08006492
6493 /**
6494 * @hide
6495 * @return Whether custom content views are displayed inline in the style
6496 */
6497 public boolean displayCustomViewInline() {
6498 return false;
6499 }
Selim Cinekd0426622017-07-11 13:19:59 +02006500
6501 /**
6502 * Reduces the image sizes contained in this style.
6503 *
6504 * @hide
6505 */
6506 public void reduceImageSizes(Context context) {
6507 }
Selim Cinek90343862018-02-01 11:07:11 -08006508
6509 /**
6510 * Validate that this style was properly composed. This is called at build time.
6511 * @hide
6512 */
6513 public void validate(Context context) {
6514 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006515
6516 /**
6517 * @hide
6518 */
6519 public abstract boolean areNotificationsVisiblyDifferent(Style other);
Selim Cinekc7f5a822018-03-20 19:32:06 -07006520
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006521 /**
koprivaa1a78482018-10-09 10:09:23 -07006522 * @return the text that should be displayed in the statusBar when heads-upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006523 * If {@code null} is returned, the default implementation will be used.
6524 *
6525 * @hide
6526 */
6527 public CharSequence getHeadsUpStatusBarText() {
6528 return null;
6529 }
Joe Onorato46439ce2010-11-19 13:56:21 -08006530 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006531
6532 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006533 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08006534 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006535 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006536 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006537 * Notification notif = new Notification.Builder(mContext)
6538 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
6539 * .setContentText(subject)
6540 * .setSmallIcon(R.drawable.new_post)
6541 * .setLargeIcon(aBitmap)
6542 * .setStyle(new Notification.BigPictureStyle()
6543 * .bigPicture(aBigBitmap))
6544 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006545 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006546 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006547 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006548 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006549 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006550 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006551 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006552 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006553
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006554 public BigPictureStyle() {
6555 }
6556
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006557 /**
6558 * @deprecated use {@code BigPictureStyle()}.
6559 */
6560 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006561 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006562 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006563 }
6564
Chris Wrend6297db2012-05-03 16:20:13 -04006565 /**
6566 * Overrides ContentTitle in the big form of the template.
6567 * This defaults to the value passed to setContentTitle().
6568 */
6569 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006570 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006571 return this;
6572 }
6573
6574 /**
6575 * Set the first line of text after the detail section in the big form of the template.
6576 */
6577 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006578 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006579 return this;
6580 }
6581
Chris Wren0bd664d2012-08-01 13:56:56 -04006582 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05006583 * @hide
6584 */
6585 public Bitmap getBigPicture() {
6586 return mPicture;
6587 }
6588
6589 /**
Chris Wren0bd664d2012-08-01 13:56:56 -04006590 * Provide the bitmap to be used as the payload for the BigPicture notification.
6591 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006592 public BigPictureStyle bigPicture(Bitmap b) {
6593 mPicture = b;
6594 return this;
6595 }
6596
Chris Wren3745a3d2012-05-22 15:11:52 -04006597 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04006598 * Override the large icon when the big notification is shown.
6599 */
6600 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04006601 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
6602 }
6603
6604 /**
6605 * Override the large icon when the big notification is shown.
6606 */
6607 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04006608 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006609 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006610 return this;
6611 }
6612
Riley Andrews0394a0c2015-11-03 23:36:52 -08006613 /** @hide */
6614 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
6615
Daniel Sandler0ec46202015-06-24 01:27:05 -04006616 /**
6617 * @hide
6618 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006619 @Override
6620 public void purgeResources() {
6621 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08006622 if (mPicture != null &&
6623 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08006624 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006625 mPicture = mPicture.createAshmemBitmap();
6626 }
6627 if (mBigLargeIcon != null) {
6628 mBigLargeIcon.convertToAshmem();
6629 }
6630 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01006631
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006632 /**
6633 * @hide
6634 */
Selim Cinekd0426622017-07-11 13:19:59 +02006635 @Override
6636 public void reduceImageSizes(Context context) {
6637 super.reduceImageSizes(context);
6638 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006639 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006640 if (mPicture != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006641 int maxPictureWidth = resources.getDimensionPixelSize(isLowRam
6642 ? R.dimen.notification_big_picture_max_height_low_ram
6643 : R.dimen.notification_big_picture_max_height);
6644 int maxPictureHeight = resources.getDimensionPixelSize(isLowRam
6645 ? R.dimen.notification_big_picture_max_width_low_ram
6646 : R.dimen.notification_big_picture_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006647 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
6648 }
6649 if (mBigLargeIcon != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006650 int rightIconSize = resources.getDimensionPixelSize(isLowRam
6651 ? R.dimen.notification_right_icon_size_low_ram
6652 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006653 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
6654 }
6655 }
6656
6657 /**
6658 * @hide
6659 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006660 public RemoteViews makeBigContentView() {
6661 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01006662 // This covers the following cases:
6663 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006664 // mN.mLargeIcon
6665 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04006666 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07006667 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006668 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006669 oldLargeIcon = mBuilder.mN.mLargeIcon;
6670 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006671 // The legacy largeIcon might not allow us to clear the image, as it's taken in
6672 // replacement if the other one is null. Because we're restoring these legacy icons
6673 // for old listeners, this is in general non-null.
6674 largeIconLegacy = mBuilder.mN.largeIcon;
6675 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006676 }
6677
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006678 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder);
Selim Cinek384804b2018-04-18 14:31:07 +08006679 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource(),
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006680 p, null /* result */);
Selim Cinek03d0d652015-11-13 13:18:09 -05006681 if (mSummaryTextSet) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006682 contentView.setTextViewText(R.id.text, mBuilder.processTextSpans(
6683 mBuilder.processLegacyText(mSummaryText)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006684 mBuilder.setTextViewColorSecondary(contentView, R.id.text, p);
Selim Cinekc848c3a2016-01-13 15:27:30 -08006685 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05006686 }
Selim Cinek279fa862016-06-14 10:57:25 -07006687 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08006688
Christoph Studer5c510ee2014-12-15 16:32:27 +01006689 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006690 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006691 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006692 }
6693
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006694 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006695 return contentView;
6696 }
6697
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006698 /**
6699 * @hide
6700 */
6701 public void addExtras(Bundle extras) {
6702 super.addExtras(extras);
6703
6704 if (mBigLargeIconSet) {
6705 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
6706 }
6707 extras.putParcelable(EXTRA_PICTURE, mPicture);
6708 }
6709
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006710 /**
6711 * @hide
6712 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006713 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02006714 protected void restoreFromExtras(Bundle extras) {
6715 super.restoreFromExtras(extras);
6716
6717 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01006718 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02006719 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04006720 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006721 mPicture = extras.getParcelable(EXTRA_PICTURE);
6722 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006723
6724 /**
6725 * @hide
6726 */
6727 @Override
6728 public boolean hasSummaryInHeader() {
6729 return false;
6730 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006731
6732 /**
6733 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006734 * Note that we aren't actually comparing the contents of the bitmaps here, so this
6735 * is only doing a cursory inspection. Bitmaps of equal size will appear the same.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006736 */
6737 @Override
6738 public boolean areNotificationsVisiblyDifferent(Style other) {
6739 if (other == null || getClass() != other.getClass()) {
6740 return true;
6741 }
6742 BigPictureStyle otherS = (BigPictureStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006743 return areBitmapsObviouslyDifferent(getBigPicture(), otherS.getBigPicture());
6744 }
6745
6746 private static boolean areBitmapsObviouslyDifferent(Bitmap a, Bitmap b) {
6747 if (a == b) {
6748 return false;
6749 }
6750 if (a == null || b == null) {
6751 return true;
6752 }
6753 return a.getWidth() != b.getWidth()
6754 || a.getHeight() != b.getHeight()
6755 || a.getConfig() != b.getConfig()
6756 || a.getGenerationId() != b.getGenerationId();
Julia Reynolds7217dc92018-03-07 12:12:09 -05006757 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006758 }
6759
6760 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006761 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08006762 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006763 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006764 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006765 * Notification notif = new Notification.Builder(mContext)
6766 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
6767 * .setContentText(subject)
6768 * .setSmallIcon(R.drawable.new_mail)
6769 * .setLargeIcon(aBitmap)
6770 * .setStyle(new Notification.BigTextStyle()
6771 * .bigText(aVeryLongString))
6772 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006773 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006774 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006775 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006776 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006777 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006778
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006779 private CharSequence mBigText;
6780
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006781 public BigTextStyle() {
6782 }
6783
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006784 /**
6785 * @deprecated use {@code BigTextStyle()}.
6786 */
6787 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006788 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006789 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006790 }
6791
Chris Wrend6297db2012-05-03 16:20:13 -04006792 /**
6793 * Overrides ContentTitle in the big form of the template.
6794 * This defaults to the value passed to setContentTitle().
6795 */
6796 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006797 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006798 return this;
6799 }
6800
6801 /**
6802 * Set the first line of text after the detail section in the big form of the template.
6803 */
6804 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006805 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006806 return this;
6807 }
6808
Chris Wren0bd664d2012-08-01 13:56:56 -04006809 /**
6810 * Provide the longer text to be displayed in the big form of the
6811 * template in place of the content text.
6812 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006813 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006814 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006815 return this;
6816 }
6817
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006818 /**
6819 * @hide
6820 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05006821 public CharSequence getBigText() {
6822 return mBigText;
6823 }
6824
6825 /**
6826 * @hide
6827 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006828 public void addExtras(Bundle extras) {
6829 super.addExtras(extras);
6830
Christoph Studer4600f9b2014-07-22 22:44:43 +02006831 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
6832 }
6833
6834 /**
6835 * @hide
6836 */
6837 @Override
6838 protected void restoreFromExtras(Bundle extras) {
6839 super.restoreFromExtras(extras);
6840
6841 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006842 }
6843
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006844 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006845 * @param increasedHeight true if this layout be created with an increased height.
6846 *
6847 * @hide
6848 */
6849 @Override
6850 public RemoteViews makeContentView(boolean increasedHeight) {
6851 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006852 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006853 mBuilder.mActions = new ArrayList<>();
6854 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006855 mBuilder.mActions = mBuilder.mOriginalActions;
6856 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006857 return remoteViews;
6858 }
6859 return super.makeContentView(increasedHeight);
6860 }
6861
6862 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006863 * @hide
6864 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006865 @Override
6866 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
6867 if (increasedHeight && mBuilder.mActions.size() > 0) {
6868 return makeBigContentView();
6869 }
6870 return super.makeHeadsUpContentView(increasedHeight);
6871 }
6872
6873 /**
6874 * @hide
6875 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006876 public RemoteViews makeBigContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006877 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder).text(null);
Selim Cinek384804b2018-04-18 14:31:07 +08006878 TemplateBindResult result = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006879 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource(), p,
6880 result);
Selim Cinek1c72fa02018-04-23 18:00:54 +08006881 contentView.setInt(R.id.big_text, "setImageEndMargin", result.getIconMarginEnd());
Joe Malin8d40d042012-11-05 11:36:40 -08006882
Selim Cinek3a2c4b92015-12-17 17:01:17 -08006883 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07006884 if (TextUtils.isEmpty(bigTextText)) {
6885 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
6886 // experience
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006887 bigTextText = mBuilder.processLegacyText(
6888 mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT));
Selim Cinek75998782016-04-26 10:39:17 -07006889 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006890 contentView.setTextViewText(R.id.big_text, mBuilder.processTextSpans(bigTextText));
6891 mBuilder.setTextViewColorSecondary(contentView, R.id.big_text, p);
6892 contentView.setViewVisibility(R.id.big_text,
6893 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08006894 contentView.setBoolean(R.id.big_text, "setHasImage",
6895 result.isRightIconContainerVisible());
Selim Cinek4fb12d32015-11-19 18:10:48 -08006896
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006897 return contentView;
6898 }
6899
Julia Reynolds7217dc92018-03-07 12:12:09 -05006900 /**
6901 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006902 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006903 */
6904 @Override
6905 public boolean areNotificationsVisiblyDifferent(Style other) {
6906 if (other == null || getClass() != other.getClass()) {
6907 return true;
6908 }
6909 BigTextStyle newS = (BigTextStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006910 return !Objects.equals(String.valueOf(getBigText()), String.valueOf(newS.getBigText()));
Julia Reynolds7217dc92018-03-07 12:12:09 -05006911 }
6912
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006913 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006914
6915 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006916 * Helper class for generating large-format notifications that include multiple back-and-forth
6917 * messages of varying types between any number of people.
6918 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006919 * <p>
Alex Hillsfc737de2016-03-23 17:33:02 -04006920 * If the platform does not provide large-format notifications, this method has no effect. The
6921 * user will always see the normal notification view.
Selim Cinekce8794f2018-05-23 16:46:05 -07006922 *
6923 * <p>
6924 * If the app is targeting Android P and above, it is required to use the {@link Person}
6925 * class in order to get an optimal rendering of the notification and its avatars. For
6926 * conversations involving multiple people, the app should also make sure that it marks the
6927 * conversation as a group with {@link #setGroupConversation(boolean)}.
6928 *
6929 * <p>
6930 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior.
6931 * Here's an example of how this may be used:
Alex Hillsfc737de2016-03-23 17:33:02 -04006932 * <pre class="prettyprint">
6933 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006934 * Person user = new Person.Builder().setIcon(userIcon).setName(userName).build();
6935 * MessagingStyle style = new MessagingStyle(user)
6936 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getPerson())
6937 * .addMessage(messages[2].getText(), messages[2].getTime(), messages[2].getPerson())
6938 * .setGroupConversation(hasMultiplePeople());
6939 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006940 * Notification noti = new Notification.Builder()
Selim Cinekce8794f2018-05-23 16:46:05 -07006941 * .setContentTitle(&quot;2 new messages with &quot; + sender.toString())
Alex Hillsfc737de2016-03-23 17:33:02 -04006942 * .setContentText(subject)
6943 * .setSmallIcon(R.drawable.new_message)
6944 * .setLargeIcon(aBitmap)
Selim Cinekce8794f2018-05-23 16:46:05 -07006945 * .setStyle(style)
Alex Hillsfc737de2016-03-23 17:33:02 -04006946 * .build();
6947 * </pre>
6948 */
6949 public static class MessagingStyle extends Style {
6950
6951 /**
6952 * The maximum number of messages that will be retained in the Notification itself (the
6953 * number displayed is up to the platform).
6954 */
6955 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
6956
Selim Cinekcb8b9852017-12-15 18:01:52 -08006957 @NonNull Person mUser;
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006958 @Nullable CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04006959 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08006960 List<Message> mHistoricMessages = new ArrayList<>();
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006961 boolean mIsGroupConversation;
Alex Hillsfc737de2016-03-23 17:33:02 -04006962
6963 MessagingStyle() {
6964 }
6965
6966 /**
Alex Hillsfd590442016-10-07 09:52:44 -04006967 * @param userDisplayName Required - the name to be displayed for any replies sent by the
6968 * user before the posting app reposts the notification with those messages after they've
6969 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04006970 * {@link #addMessage(Notification.MessagingStyle.Message)}
Selim Cinekcb8b9852017-12-15 18:01:52 -08006971 *
6972 * @deprecated use {@code MessagingStyle(Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006973 */
Alex Hillsfd590442016-10-07 09:52:44 -04006974 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Selim Cinek9acd6732018-03-23 16:39:02 -07006975 this(new Person.Builder().setName(userDisplayName).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006976 }
6977
6978 /**
6979 * @param user Required - The person displayed for any messages that are sent by the
6980 * user. Any messages added with {@link #addMessage(Notification.MessagingStyle.Message)}
6981 * who don't have a Person associated with it will be displayed as if they were sent
Selim Cinek90343862018-02-01 11:07:11 -08006982 * by this user. The user also needs to have a valid name associated with it, which will
6983 * be enforced starting in Android P.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006984 */
6985 public MessagingStyle(@NonNull Person user) {
6986 mUser = user;
Selim Cinek90343862018-02-01 11:07:11 -08006987 }
6988
6989 /**
6990 * Validate that this style was properly composed. This is called at build time.
6991 * @hide
6992 */
6993 @Override
6994 public void validate(Context context) {
6995 super.validate(context);
6996 if (context.getApplicationInfo().targetSdkVersion
6997 >= Build.VERSION_CODES.P && (mUser == null || mUser.getName() == null)) {
6998 throw new RuntimeException("User must be valid and have a name.");
Selim Cinekcb8b9852017-12-15 18:01:52 -08006999 }
7000 }
7001
7002 /**
koprivaa1a78482018-10-09 10:09:23 -07007003 * @return the text that should be displayed in the statusBar when heads upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08007004 * If {@code null} is returned, the default implementation will be used.
7005 *
7006 * @hide
7007 */
7008 @Override
7009 public CharSequence getHeadsUpStatusBarText() {
7010 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
7011 ? super.mBigContentTitle
7012 : mConversationTitle;
7013 if (!TextUtils.isEmpty(conversationTitle) && !hasOnlyWhiteSpaceSenders()) {
7014 return conversationTitle;
7015 }
7016 return null;
7017 }
7018
7019 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08007020 * @return the user to be displayed for any replies sent by the user
7021 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007022 @NonNull
Selim Cinekcb8b9852017-12-15 18:01:52 -08007023 public Person getUser() {
7024 return mUser;
Alex Hillsfc737de2016-03-23 17:33:02 -04007025 }
7026
7027 /**
7028 * Returns the name to be displayed for any replies sent by the user
Selim Cinekcb8b9852017-12-15 18:01:52 -08007029 *
7030 * @deprecated use {@link #getUser()} instead
Alex Hillsfc737de2016-03-23 17:33:02 -04007031 */
7032 public CharSequence getUserDisplayName() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007033 return mUser.getName();
Alex Hillsfc737de2016-03-23 17:33:02 -04007034 }
7035
7036 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007037 * Sets the title to be displayed on this conversation. May be set to {@code null}.
7038 *
Kodlee Yin14656422017-12-22 17:00:46 -08007039 * <p>This API's behavior was changed in SDK version {@link Build.VERSION_CODES#P}. If your
7040 * application's target version is less than {@link Build.VERSION_CODES#P}, setting a
7041 * conversation title to a non-null value will make {@link #isGroupConversation()} return
7042 * {@code true} and passing {@code null} will make it return {@code false}. In
7043 * {@link Build.VERSION_CODES#P} and beyond, use {@link #setGroupConversation(boolean)}
7044 * to set group conversation status.
7045 *
7046 * @param conversationTitle Title displayed for this conversation
7047 * @return this object for method chaining
Alex Hillsfc737de2016-03-23 17:33:02 -04007048 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007049 public MessagingStyle setConversationTitle(@Nullable CharSequence conversationTitle) {
Alex Hillsfc737de2016-03-23 17:33:02 -04007050 mConversationTitle = conversationTitle;
7051 return this;
7052 }
7053
7054 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007055 * Return the title to be displayed on this conversation. May return {@code null}.
Alex Hillsfc737de2016-03-23 17:33:02 -04007056 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007057 @Nullable
Alex Hillsfc737de2016-03-23 17:33:02 -04007058 public CharSequence getConversationTitle() {
7059 return mConversationTitle;
7060 }
7061
7062 /**
7063 * Adds a message for display by this notification. Convenience call for a simple
7064 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
7065 * @param text A {@link CharSequence} to be displayed as the message content
7066 * @param timestamp Time at which the message arrived
7067 * @param sender A {@link CharSequence} to be used for displaying the name of the
7068 * sender. Should be <code>null</code> for messages by the current user, in which case
7069 * the platform will insert {@link #getUserDisplayName()}.
7070 * Should be unique amongst all individuals in the conversation, and should be
7071 * consistent during re-posts of the notification.
7072 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07007073 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Alex Hillsfc737de2016-03-23 17:33:02 -04007074 *
7075 * @return this object for method chaining
Selim Cinekcb8b9852017-12-15 18:01:52 -08007076 *
7077 * @deprecated use {@link #addMessage(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04007078 */
7079 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007080 return addMessage(text, timestamp,
Selim Cinek9acd6732018-03-23 16:39:02 -07007081 sender == null ? null : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08007082 }
7083
7084 /**
7085 * Adds a message for display by this notification. Convenience call for a simple
7086 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
7087 * @param text A {@link CharSequence} to be displayed as the message content
7088 * @param timestamp Time at which the message arrived
7089 * @param sender The {@link Person} who sent the message.
7090 * Should be <code>null</code> for messages by the current user, in which case
7091 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7092 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07007093 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Selim Cinekcb8b9852017-12-15 18:01:52 -08007094 *
7095 * @return this object for method chaining
7096 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007097 public MessagingStyle addMessage(@NonNull CharSequence text, long timestamp,
7098 @Nullable Person sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08007099 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04007100 }
7101
7102 /**
7103 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08007104 *
7105 * <p>The messages should be added in chronologic order, i.e. the oldest first,
7106 * the newest last.
7107 *
Alex Hillsfc737de2016-03-23 17:33:02 -04007108 * @param message The {@link Message} to be displayed
7109 * @return this object for method chaining
7110 */
7111 public MessagingStyle addMessage(Message message) {
7112 mMessages.add(message);
7113 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
7114 mMessages.remove(0);
7115 }
7116 return this;
7117 }
7118
7119 /**
Adrian Roos437cd562017-01-18 15:47:03 -08007120 * Adds a {@link Message} for historic context in this notification.
7121 *
7122 * <p>Messages should be added as historic if they are not the main subject of the
7123 * notification but may give context to a conversation. The system may choose to present
7124 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
7125 *
7126 * <p>The messages should be added in chronologic order, i.e. the oldest first,
7127 * the newest last.
7128 *
7129 * @param message The historic {@link Message} to be added
7130 * @return this object for method chaining
7131 */
7132 public MessagingStyle addHistoricMessage(Message message) {
7133 mHistoricMessages.add(message);
7134 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
7135 mHistoricMessages.remove(0);
7136 }
7137 return this;
7138 }
7139
7140 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007141 * Gets the list of {@code Message} objects that represent the notification
7142 */
7143 public List<Message> getMessages() {
7144 return mMessages;
7145 }
7146
7147 /**
Adrian Roos437cd562017-01-18 15:47:03 -08007148 * Gets the list of historic {@code Message}s in the notification.
7149 */
7150 public List<Message> getHistoricMessages() {
7151 return mHistoricMessages;
7152 }
7153
7154 /**
Selim Cinekce8794f2018-05-23 16:46:05 -07007155 * Sets whether this conversation notification represents a group. If the app is targeting
7156 * Android P, this is required if the app wants to display the largeIcon set with
7157 * {@link Notification.Builder#setLargeIcon(Bitmap)}, otherwise it will be hidden.
Kodlee Yin14656422017-12-22 17:00:46 -08007158 *
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007159 * @param isGroupConversation {@code true} if the conversation represents a group,
7160 * {@code false} otherwise.
7161 * @return this object for method chaining
7162 */
7163 public MessagingStyle setGroupConversation(boolean isGroupConversation) {
7164 mIsGroupConversation = isGroupConversation;
7165 return this;
7166 }
7167
7168 /**
Kodlee Yin14656422017-12-22 17:00:46 -08007169 * Returns {@code true} if this notification represents a group conversation, otherwise
7170 * {@code false}.
7171 *
7172 * <p> If the application that generated this {@link MessagingStyle} targets an SDK version
7173 * less than {@link Build.VERSION_CODES#P}, this method becomes dependent on whether or
7174 * not the conversation title is set; returning {@code true} if the conversation title is
7175 * a non-null value, or {@code false} otherwise. From {@link Build.VERSION_CODES#P} forward,
7176 * this method returns what's set by {@link #setGroupConversation(boolean)} allowing for
7177 * named, non-group conversations.
7178 *
7179 * @see #setConversationTitle(CharSequence)
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007180 */
7181 public boolean isGroupConversation() {
Kodlee Yin14656422017-12-22 17:00:46 -08007182 // When target SDK version is < P, a non-null conversation title dictates if this is
7183 // as group conversation.
7184 if (mBuilder != null
7185 && mBuilder.mContext.getApplicationInfo().targetSdkVersion
7186 < Build.VERSION_CODES.P) {
7187 return mConversationTitle != null;
7188 }
7189
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007190 return mIsGroupConversation;
7191 }
7192
7193 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007194 * @hide
7195 */
7196 @Override
7197 public void addExtras(Bundle extras) {
7198 super.addExtras(extras);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007199 if (mUser != null) {
7200 // For legacy usages
7201 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUser.getName());
7202 extras.putParcelable(EXTRA_MESSAGING_PERSON, mUser);
Alex Hillsfc737de2016-03-23 17:33:02 -04007203 }
7204 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007205 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04007206 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007207 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
7208 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04007209 }
Adrian Roos437cd562017-01-18 15:47:03 -08007210 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
7211 Message.getBundleArrayForMessages(mHistoricMessages));
7212 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007213
7214 fixTitleAndTextExtras(extras);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007215 extras.putBoolean(EXTRA_IS_GROUP_CONVERSATION, mIsGroupConversation);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007216 }
7217
7218 private void fixTitleAndTextExtras(Bundle extras) {
7219 Message m = findLatestIncomingMessage();
7220 CharSequence text = (m == null) ? null : m.mText;
7221 CharSequence sender = m == null ? null
Selim Cinekcb8b9852017-12-15 18:01:52 -08007222 : m.mSender == null || TextUtils.isEmpty(m.mSender.getName())
7223 ? mUser.getName() : m.mSender.getName();
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007224 CharSequence title;
7225 if (!TextUtils.isEmpty(mConversationTitle)) {
7226 if (!TextUtils.isEmpty(sender)) {
7227 BidiFormatter bidi = BidiFormatter.getInstance();
7228 title = mBuilder.mContext.getString(
7229 com.android.internal.R.string.notification_messaging_title_template,
Selim Cinekcb8b9852017-12-15 18:01:52 -08007230 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(sender));
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007231 } else {
7232 title = mConversationTitle;
7233 }
7234 } else {
7235 title = sender;
7236 }
7237
7238 if (title != null) {
7239 extras.putCharSequence(EXTRA_TITLE, title);
7240 }
7241 if (text != null) {
7242 extras.putCharSequence(EXTRA_TEXT, text);
7243 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007244 }
7245
7246 /**
7247 * @hide
7248 */
7249 @Override
7250 protected void restoreFromExtras(Bundle extras) {
7251 super.restoreFromExtras(extras);
7252
Selim Cinekcb8b9852017-12-15 18:01:52 -08007253 mUser = extras.getParcelable(EXTRA_MESSAGING_PERSON);
7254 if (mUser == null) {
7255 CharSequence displayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
Selim Cinek9acd6732018-03-23 16:39:02 -07007256 mUser = new Person.Builder().setName(displayName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007257 }
Adrian Roos96b7e202016-05-17 13:50:38 -07007258 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08007259 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007260 mMessages = Message.getMessagesFromBundleArray(messages);
Adrian Roos437cd562017-01-18 15:47:03 -08007261 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007262 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007263 mIsGroupConversation = extras.getBoolean(EXTRA_IS_GROUP_CONVERSATION);
Alex Hillsfc737de2016-03-23 17:33:02 -04007264 }
7265
7266 /**
7267 * @hide
7268 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07007269 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007270 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07007271 mBuilder.mOriginalActions = mBuilder.mActions;
7272 mBuilder.mActions = new ArrayList<>();
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007273 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007274 false /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007275 mBuilder.mActions = mBuilder.mOriginalActions;
7276 mBuilder.mOriginalActions = null;
7277 return remoteViews;
Adrian Roosc1a80b02016-04-05 14:54:55 -07007278 }
7279
Julia Reynolds7217dc92018-03-07 12:12:09 -05007280 /**
7281 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04007282 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05007283 */
7284 @Override
7285 public boolean areNotificationsVisiblyDifferent(Style other) {
7286 if (other == null || getClass() != other.getClass()) {
7287 return true;
7288 }
7289 MessagingStyle newS = (MessagingStyle) other;
7290 List<MessagingStyle.Message> oldMs = getMessages();
7291 List<MessagingStyle.Message> newMs = newS.getMessages();
7292
Dan Sandler7d67bd42018-05-15 14:06:38 -04007293 if (oldMs == null || newMs == null) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007294 newMs = new ArrayList<>();
7295 }
7296
7297 int n = oldMs.size();
7298 if (n != newMs.size()) {
7299 return true;
7300 }
7301 for (int i = 0; i < n; i++) {
7302 MessagingStyle.Message oldM = oldMs.get(i);
7303 MessagingStyle.Message newM = newMs.get(i);
Dan Sandler7d67bd42018-05-15 14:06:38 -04007304 if (!Objects.equals(
7305 String.valueOf(oldM.getText()),
7306 String.valueOf(newM.getText()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007307 return true;
7308 }
7309 if (!Objects.equals(oldM.getDataUri(), newM.getDataUri())) {
7310 return true;
7311 }
Dan Sandler7d67bd42018-05-15 14:06:38 -04007312 String oldSender = String.valueOf(oldM.getSenderPerson() == null
7313 ? oldM.getSender()
7314 : oldM.getSenderPerson().getName());
7315 String newSender = String.valueOf(newM.getSenderPerson() == null
7316 ? newM.getSender()
7317 : newM.getSenderPerson().getName());
Julia Reynolds7217dc92018-03-07 12:12:09 -05007318 if (!Objects.equals(oldSender, newSender)) {
7319 return true;
7320 }
7321
7322 String oldKey = oldM.getSenderPerson() == null
7323 ? null : oldM.getSenderPerson().getKey();
7324 String newKey = newM.getSenderPerson() == null
7325 ? null : newM.getSenderPerson().getKey();
7326 if (!Objects.equals(oldKey, newKey)) {
7327 return true;
7328 }
7329 // Other fields (like timestamp) intentionally excluded
7330 }
7331 return false;
7332 }
7333
Adrian Roosc1a80b02016-04-05 14:54:55 -07007334 private Message findLatestIncomingMessage() {
Selim Cinek88188f22017-09-19 16:46:56 -07007335 return findLatestIncomingMessage(mMessages);
7336 }
7337
7338 /**
7339 * @hide
7340 */
7341 @Nullable
7342 public static Message findLatestIncomingMessage(
7343 List<Message> messages) {
7344 for (int i = messages.size() - 1; i >= 0; i--) {
7345 Message m = messages.get(i);
Adrian Roosc1a80b02016-04-05 14:54:55 -07007346 // Incoming messages have a non-empty sender.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007347 if (m.mSender != null && !TextUtils.isEmpty(m.mSender.getName())) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07007348 return m;
7349 }
7350 }
Selim Cinek88188f22017-09-19 16:46:56 -07007351 if (!messages.isEmpty()) {
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007352 // No incoming messages, fall back to outgoing message
Selim Cinek88188f22017-09-19 16:46:56 -07007353 return messages.get(messages.size() - 1);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007354 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07007355 return null;
7356 }
7357
7358 /**
7359 * @hide
7360 */
7361 @Override
7362 public RemoteViews makeBigContentView() {
Selim Cinek384804b2018-04-18 14:31:07 +08007363 return makeMessagingView(false /* displayImagesAtEnd */, true /* hideLargeIcon */);
Selim Cinekafeed292017-12-12 17:32:44 -08007364 }
7365
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007366 /**
7367 * Create a messaging layout.
7368 *
7369 * @param displayImagesAtEnd should images be displayed at the end of the content instead
7370 * of inline.
Selim Cinek384804b2018-04-18 14:31:07 +08007371 * @param hideRightIcons Should the reply affordance be shown at the end of the notification
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007372 * @return the created remoteView.
7373 */
Selim Cinekafeed292017-12-12 17:32:44 -08007374 @NonNull
Selim Cinek384804b2018-04-18 14:31:07 +08007375 private RemoteViews makeMessagingView(boolean displayImagesAtEnd, boolean hideRightIcons) {
Selim Cinek88188f22017-09-19 16:46:56 -07007376 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
Adrian Roosc1a80b02016-04-05 14:54:55 -07007377 ? super.mBigContentTitle
7378 : mConversationTitle;
Selim Cinekce8794f2018-05-23 16:46:05 -07007379 boolean atLeastP = mBuilder.mContext.getApplicationInfo().targetSdkVersion
7380 >= Build.VERSION_CODES.P;
7381 boolean isOneToOne;
Selim Cinek2dd3e722018-01-19 11:06:06 -08007382 CharSequence nameReplacement = null;
Selim Cinekce8794f2018-05-23 16:46:05 -07007383 Icon avatarReplacement = null;
7384 if (!atLeastP) {
7385 isOneToOne = TextUtils.isEmpty(conversationTitle);
7386 avatarReplacement = mBuilder.mN.mLargeIcon;
7387 if (hasOnlyWhiteSpaceSenders()) {
7388 isOneToOne = true;
7389 nameReplacement = conversationTitle;
7390 conversationTitle = null;
7391 }
7392 } else {
7393 isOneToOne = !isGroupConversation();
Adrian Roosb1f427c2016-05-26 12:27:15 -07007394 }
Selim Cinek384804b2018-04-18 14:31:07 +08007395 TemplateBindResult bindResult = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007396 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).title(
7397 conversationTitle).text(null)
7398 .hideLargeIcon(hideRightIcons || isOneToOne)
7399 .hideReplyIcon(hideRightIcons)
7400 .headerTextSecondary(conversationTitle);
Adrian Roos48d746a2016-04-12 14:57:28 -07007401 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07007402 mBuilder.getMessagingLayoutResource(),
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007403 p,
Selim Cinek384804b2018-04-18 14:31:07 +08007404 bindResult);
Selim Cinek88188f22017-09-19 16:46:56 -07007405 addExtras(mBuilder.mN.extras);
Selim Cinekafeed292017-12-12 17:32:44 -08007406 // also update the end margin if there is an image
Selim Cinek1c72fa02018-04-23 18:00:54 +08007407 contentView.setViewLayoutMarginEnd(R.id.notification_messaging,
7408 bindResult.getIconMarginEnd());
Selim Cinek88188f22017-09-19 16:46:56 -07007409 contentView.setInt(R.id.status_bar_latest_event_content, "setLayoutColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007410 mBuilder.isColorized(p) ? mBuilder.getPrimaryTextColor(p)
7411 : mBuilder.resolveContrastColor(p));
Kenny Guy14d035c2018-05-02 19:10:36 +01007412 contentView.setInt(R.id.status_bar_latest_event_content, "setSenderTextColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007413 mBuilder.getPrimaryTextColor(p));
Kenny Guy14d035c2018-05-02 19:10:36 +01007414 contentView.setInt(R.id.status_bar_latest_event_content, "setMessageTextColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007415 mBuilder.getSecondaryTextColor(p));
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007416 contentView.setBoolean(R.id.status_bar_latest_event_content, "setDisplayImagesAtEnd",
7417 displayImagesAtEnd);
Selim Cinekce8794f2018-05-23 16:46:05 -07007418 contentView.setIcon(R.id.status_bar_latest_event_content, "setAvatarReplacement",
7419 avatarReplacement);
Selim Cinek2dd3e722018-01-19 11:06:06 -08007420 contentView.setCharSequence(R.id.status_bar_latest_event_content, "setNameReplacement",
7421 nameReplacement);
Selim Cinek88188f22017-09-19 16:46:56 -07007422 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsOneToOne",
7423 isOneToOne);
7424 contentView.setBundle(R.id.status_bar_latest_event_content, "setData",
7425 mBuilder.mN.extras);
Alex Hillsfc737de2016-03-23 17:33:02 -04007426 return contentView;
7427 }
7428
Selim Cinekf7409db2017-10-24 16:17:14 -07007429 private boolean hasOnlyWhiteSpaceSenders() {
7430 for (int i = 0; i < mMessages.size(); i++) {
7431 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007432 Person sender = m.getSenderPerson();
7433 if (sender != null && !isWhiteSpace(sender.getName())) {
Selim Cinekf7409db2017-10-24 16:17:14 -07007434 return false;
7435 }
7436 }
7437 return true;
7438 }
7439
7440 private boolean isWhiteSpace(CharSequence sender) {
7441 if (TextUtils.isEmpty(sender)) {
7442 return true;
7443 }
7444 if (sender.toString().matches("^\\s*$")) {
7445 return true;
7446 }
7447 // Let's check if we only have 0 whitespace chars. Some apps did this as a workaround
7448 // For the presentation that we had.
7449 for (int i = 0; i < sender.length(); i++) {
7450 char c = sender.charAt(i);
7451 if (c != '\u200B') {
7452 return false;
7453 }
7454 }
7455 return true;
7456 }
7457
Selim Cinek88188f22017-09-19 16:46:56 -07007458 private CharSequence createConversationTitleFromMessages() {
7459 ArraySet<CharSequence> names = new ArraySet<>();
7460 for (int i = 0; i < mMessages.size(); i++) {
7461 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007462 Person sender = m.getSenderPerson();
Selim Cinek88188f22017-09-19 16:46:56 -07007463 if (sender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007464 names.add(sender.getName());
Selim Cinek88188f22017-09-19 16:46:56 -07007465 }
7466 }
7467 SpannableStringBuilder title = new SpannableStringBuilder();
7468 int size = names.size();
7469 for (int i = 0; i < size; i++) {
7470 CharSequence name = names.valueAt(i);
7471 if (!TextUtils.isEmpty(title)) {
7472 title.append(", ");
7473 }
7474 title.append(BidiFormatter.getInstance().unicodeWrap(name));
7475 }
7476 return title;
7477 }
7478
Adrian Roosdedd1df2016-04-26 16:38:47 -07007479 /**
7480 * @hide
7481 */
7482 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007483 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007484 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007485 true /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007486 remoteViews.setInt(R.id.notification_messaging, "setMaxDisplayedLines", 1);
7487 return remoteViews;
Adrian Roosdedd1df2016-04-26 16:38:47 -07007488 }
7489
Adrian Roosc1a80b02016-04-05 14:54:55 -07007490 private static TextAppearanceSpan makeFontColorSpan(int color) {
7491 return new TextAppearanceSpan(null, 0, 0,
7492 ColorStateList.valueOf(color), null);
7493 }
7494
Alex Hillsd9b04d92016-04-11 16:38:16 -04007495 public static final class Message {
Tony Mak09db2ea2018-06-27 18:12:48 +01007496 /** @hide */
7497 public static final String KEY_TEXT = "text";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007498 static final String KEY_TIMESTAMP = "time";
7499 static final String KEY_SENDER = "sender";
Selim Cinekcb8b9852017-12-15 18:01:52 -08007500 static final String KEY_SENDER_PERSON = "sender_person";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007501 static final String KEY_DATA_MIME_TYPE = "type";
7502 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08007503 static final String KEY_EXTRAS_BUNDLE = "extras";
Kenny Guya0f6de82018-04-06 16:20:16 +01007504 static final String KEY_REMOTE_INPUT_HISTORY = "remote_input_history";
Alex Hillsfc737de2016-03-23 17:33:02 -04007505
7506 private final CharSequence mText;
7507 private final long mTimestamp;
Selim Cinekcb8b9852017-12-15 18:01:52 -08007508 @Nullable
7509 private final Person mSender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007510 /** True if this message was generated from the extra
7511 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}
7512 */
7513 private final boolean mRemoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007514
Shane Brennan5a871862017-03-11 13:14:17 -08007515 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007516 private String mDataMimeType;
7517 private Uri mDataUri;
7518
7519 /**
7520 * Constructor
7521 * @param text A {@link CharSequence} to be displayed as the message content
7522 * @param timestamp Time at which the message arrived
7523 * @param sender A {@link CharSequence} to be used for displaying the name of the
7524 * sender. Should be <code>null</code> for messages by the current user, in which case
7525 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
7526 * Should be unique amongst all individuals in the conversation, and should be
7527 * consistent during re-posts of the notification.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007528 *
7529 * @deprecated use {@code Message(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04007530 */
7531 public Message(CharSequence text, long timestamp, CharSequence sender){
Selim Cinek9acd6732018-03-23 16:39:02 -07007532 this(text, timestamp, sender == null ? null
7533 : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08007534 }
7535
7536 /**
7537 * Constructor
7538 * @param text A {@link CharSequence} to be displayed as the message content
7539 * @param timestamp Time at which the message arrived
7540 * @param sender The {@link Person} who sent the message.
7541 * Should be <code>null</code> for messages by the current user, in which case
7542 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7543 * <p>
Selim Cinek9acd6732018-03-23 16:39:02 -07007544 * The person provided should contain an Icon, set with
7545 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7546 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7547 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7548 * to differentiate between the different users.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007549 * </p>
7550 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007551 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender) {
Kenny Guya0f6de82018-04-06 16:20:16 +01007552 this(text, timestamp, sender, false /* remoteHistory */);
7553 }
7554
7555 /**
7556 * Constructor
7557 * @param text A {@link CharSequence} to be displayed as the message content
7558 * @param timestamp Time at which the message arrived
7559 * @param sender The {@link Person} who sent the message.
7560 * Should be <code>null</code> for messages by the current user, in which case
7561 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7562 * @param remoteInputHistory True if the messages was generated from the extra
7563 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7564 * <p>
7565 * The person provided should contain an Icon, set with
7566 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7567 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7568 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7569 * to differentiate between the different users.
7570 * </p>
7571 * @hide
7572 */
7573 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender,
7574 boolean remoteInputHistory) {
Alex Hillsfc737de2016-03-23 17:33:02 -04007575 mText = text;
7576 mTimestamp = timestamp;
7577 mSender = sender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007578 mRemoteInputHistory = remoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007579 }
7580
7581 /**
7582 * Sets a binary blob of data and an associated MIME type for a message. In the case
7583 * where the platform doesn't support the MIME type, the original text provided in the
7584 * constructor will be used.
7585 * @param dataMimeType The MIME type of the content. See
7586 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
7587 * types on Android and Android Wear.
7588 * @param dataUri The uri containing the content whose type is given by the MIME type.
7589 * <p class="note">
7590 * <ol>
7591 * <li>Notification Listeners including the System UI need permission to access the
7592 * data the Uri points to. The recommended ways to do this are:</li>
7593 * <li>Store the data in your own ContentProvider, making sure that other apps have
7594 * the correct permission to access your provider. The preferred mechanism for
7595 * providing access is to use per-URI permissions which are temporary and only
7596 * grant access to the receiving application. An easy way to create a
7597 * ContentProvider like this is to use the FileProvider helper class.</li>
7598 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
7599 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
7600 * also store non-media types (see MediaStore.Files for more info). Files can be
7601 * inserted into the MediaStore using scanFile() after which a content:// style
7602 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
7603 * Note that once added to the system MediaStore the content is accessible to any
7604 * app on the device.</li>
7605 * </ol>
7606 * @return this object for method chaining
7607 */
7608 public Message setData(String dataMimeType, Uri dataUri) {
7609 mDataMimeType = dataMimeType;
7610 mDataUri = dataUri;
7611 return this;
7612 }
7613
Alex Hillsfc737de2016-03-23 17:33:02 -04007614 /**
7615 * Get the text to be used for this message, or the fallback text if a type and content
7616 * Uri have been set
7617 */
7618 public CharSequence getText() {
7619 return mText;
7620 }
7621
7622 /**
7623 * Get the time at which this message arrived
7624 */
7625 public long getTimestamp() {
7626 return mTimestamp;
7627 }
7628
7629 /**
Shane Brennan5a871862017-03-11 13:14:17 -08007630 * Get the extras Bundle for this message.
7631 */
7632 public Bundle getExtras() {
7633 return mExtras;
7634 }
7635
7636 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007637 * Get the text used to display the contact's name in the messaging experience
Selim Cinekcb8b9852017-12-15 18:01:52 -08007638 *
7639 * @deprecated use {@link #getSenderPerson()}
Alex Hillsfc737de2016-03-23 17:33:02 -04007640 */
7641 public CharSequence getSender() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007642 return mSender == null ? null : mSender.getName();
7643 }
7644
7645 /**
7646 * Get the sender associated with this message.
7647 */
7648 @Nullable
7649 public Person getSenderPerson() {
Alex Hillsfc737de2016-03-23 17:33:02 -04007650 return mSender;
7651 }
7652
7653 /**
7654 * Get the MIME type of the data pointed to by the Uri
7655 */
7656 public String getDataMimeType() {
7657 return mDataMimeType;
7658 }
7659
7660 /**
koprivaa1a78482018-10-09 10:09:23 -07007661 * Get the Uri pointing to the content of the message. Can be null, in which case
Alex Hillsfc737de2016-03-23 17:33:02 -04007662 * {@see #getText()} is used.
7663 */
7664 public Uri getDataUri() {
7665 return mDataUri;
7666 }
7667
Kenny Guya0f6de82018-04-06 16:20:16 +01007668 /**
7669 * @return True if the message was generated from
7670 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7671 * @hide
7672 */
7673 public boolean isRemoteInputHistory() {
7674 return mRemoteInputHistory;
7675 }
7676
Beverlye98937a2018-11-15 10:18:57 -05007677 /**
7678 * @hide
7679 */
7680 @VisibleForTesting
7681 public Bundle toBundle() {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007682 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007683 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007684 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04007685 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007686 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04007687 if (mSender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007688 // Legacy listeners need this
7689 bundle.putCharSequence(KEY_SENDER, mSender.getName());
7690 bundle.putParcelable(KEY_SENDER_PERSON, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04007691 }
7692 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007693 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04007694 }
7695 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007696 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04007697 }
Shane Brennan5a871862017-03-11 13:14:17 -08007698 if (mExtras != null) {
7699 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
7700 }
Kenny Guya0f6de82018-04-06 16:20:16 +01007701 if (mRemoteInputHistory) {
7702 bundle.putBoolean(KEY_REMOTE_INPUT_HISTORY, mRemoteInputHistory);
7703 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007704 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04007705 }
7706
Alex Hillsd9b04d92016-04-11 16:38:16 -04007707 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
7708 Bundle[] bundles = new Bundle[messages.size()];
7709 final int N = messages.size();
7710 for (int i = 0; i < N; i++) {
7711 bundles[i] = messages.get(i).toBundle();
7712 }
7713 return bundles;
7714 }
7715
Selim Cinek88188f22017-09-19 16:46:56 -07007716 /**
7717 * @return A list of messages read from the bundles.
7718 *
7719 * @hide
7720 */
7721 public static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
7722 if (bundles == null) {
7723 return new ArrayList<>();
7724 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007725 List<Message> messages = new ArrayList<>(bundles.length);
7726 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07007727 if (bundles[i] instanceof Bundle) {
7728 Message message = getMessageFromBundle((Bundle)bundles[i]);
7729 if (message != null) {
7730 messages.add(message);
7731 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007732 }
7733 }
7734 return messages;
7735 }
7736
Selim Cinekb0dc61b2018-05-22 18:49:36 -07007737 /**
7738 * @return The message that is stored in the bundle or null if the message couldn't be
7739 * resolved.
7740 *
7741 * @hide
7742 */
7743 @Nullable
7744 public static Message getMessageFromBundle(Bundle bundle) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007745 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07007746 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007747 return null;
7748 } else {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007749
7750 Person senderPerson = bundle.getParcelable(KEY_SENDER_PERSON);
7751 if (senderPerson == null) {
7752 // Legacy apps that use compat don't actually provide the sender objects
7753 // We need to fix the compat version to provide people / use
7754 // the native api instead
7755 CharSequence senderName = bundle.getCharSequence(KEY_SENDER);
7756 if (senderName != null) {
Selim Cinek9acd6732018-03-23 16:39:02 -07007757 senderPerson = new Person.Builder().setName(senderName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007758 }
7759 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007760 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
Selim Cinekcb8b9852017-12-15 18:01:52 -08007761 bundle.getLong(KEY_TIMESTAMP),
Kenny Guya0f6de82018-04-06 16:20:16 +01007762 senderPerson,
7763 bundle.getBoolean(KEY_REMOTE_INPUT_HISTORY, false));
Alex Hillsd9b04d92016-04-11 16:38:16 -04007764 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
7765 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007766 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
7767 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04007768 }
Shane Brennan5a871862017-03-11 13:14:17 -08007769 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
7770 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
7771 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007772 return message;
7773 }
7774 } catch (ClassCastException e) {
7775 return null;
7776 }
7777 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007778 }
7779 }
7780
7781 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04007782 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08007783 *
Robert Ly91c5ce32014-06-08 15:37:00 -07007784 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04007785 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07007786 * Notification notif = new Notification.Builder(mContext)
7787 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
7788 * .setContentText(subject)
7789 * .setSmallIcon(R.drawable.new_mail)
7790 * .setLargeIcon(aBitmap)
7791 * .setStyle(new Notification.InboxStyle()
7792 * .addLine(str1)
7793 * .addLine(str2)
7794 * .setContentTitle(&quot;&quot;)
7795 * .setSummaryText(&quot;+3 more&quot;))
7796 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04007797 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08007798 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04007799 * @see Notification#bigContentView
7800 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007801 public static class InboxStyle extends Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07007802
7803 /**
7804 * The number of lines of remote input history allowed until we start reducing lines.
7805 */
7806 private static final int NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION = 1;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007807 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
7808
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007809 public InboxStyle() {
7810 }
7811
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007812 /**
7813 * @deprecated use {@code InboxStyle()}.
7814 */
7815 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04007816 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007817 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04007818 }
7819
Chris Wrend6297db2012-05-03 16:20:13 -04007820 /**
7821 * Overrides ContentTitle in the big form of the template.
7822 * This defaults to the value passed to setContentTitle().
7823 */
7824 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007825 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04007826 return this;
7827 }
7828
7829 /**
7830 * Set the first line of text after the detail section in the big form of the template.
7831 */
7832 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007833 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04007834 return this;
7835 }
7836
Chris Wren0bd664d2012-08-01 13:56:56 -04007837 /**
7838 * Append a line to the digest section of the Inbox notification.
7839 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04007840 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007841 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04007842 return this;
7843 }
7844
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007845 /**
7846 * @hide
7847 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05007848 public ArrayList<CharSequence> getLines() {
7849 return mTexts;
7850 }
7851
7852 /**
7853 * @hide
7854 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007855 public void addExtras(Bundle extras) {
7856 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007857
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007858 CharSequence[] a = new CharSequence[mTexts.size()];
7859 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
7860 }
7861
Christoph Studer4600f9b2014-07-22 22:44:43 +02007862 /**
7863 * @hide
7864 */
7865 @Override
7866 protected void restoreFromExtras(Bundle extras) {
7867 super.restoreFromExtras(extras);
7868
7869 mTexts.clear();
7870 if (extras.containsKey(EXTRA_TEXT_LINES)) {
7871 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
7872 }
7873 }
7874
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007875 /**
7876 * @hide
7877 */
7878 public RemoteViews makeBigContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007879 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder).text(null);
Selim Cinek384804b2018-04-18 14:31:07 +08007880 TemplateBindResult result = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007881 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource(), p, result);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007882
Chris Wrend6297db2012-05-03 16:20:13 -04007883 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 -04007884 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04007885
Chris Wren4ed80d52012-05-17 09:30:03 -04007886 // Make sure all rows are gone in case we reuse a view.
7887 for (int rowId : rowIds) {
7888 contentView.setViewVisibility(rowId, View.GONE);
7889 }
7890
Daniel Sandler879c5e02012-04-17 16:46:51 -04007891 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07007892 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7893 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08007894 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07007895 int onlyViewId = 0;
7896 int maxRows = rowIds.length;
7897 if (mBuilder.mActions.size() > 0) {
7898 maxRows--;
7899 }
Selim Cinekbee4e072018-05-21 22:06:43 -07007900 CharSequence[] remoteInputHistory = mBuilder.mN.extras.getCharSequenceArray(
7901 EXTRA_REMOTE_INPUT_HISTORY);
7902 if (remoteInputHistory != null
7903 && remoteInputHistory.length > NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION) {
7904 // Let's remove some messages to make room for the remote input history.
7905 // 1 is always able to fit, but let's remove them if they are 2 or 3
7906 int numRemoteInputs = Math.min(remoteInputHistory.length,
7907 MAX_REMOTE_INPUT_HISTORY_LINES);
7908 int totalNumRows = mTexts.size() + numRemoteInputs
7909 - NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION;
7910 if (totalNumRows > maxRows) {
7911 int overflow = totalNumRows - maxRows;
7912 if (mTexts.size() > maxRows) {
7913 // Heuristic: if the Texts don't fit anyway, we'll rather drop the last
7914 // few messages, even with the remote input
7915 maxRows -= overflow;
7916 } else {
7917 // otherwise we drop the first messages
7918 i = overflow;
7919 }
7920 }
7921 }
Selim Cinek07c80172016-04-21 16:40:47 -07007922 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007923 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07007924 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007925 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07007926 contentView.setTextViewText(rowIds[i],
7927 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007928 mBuilder.setTextViewColorSecondary(contentView, rowIds[i], p);
Selim Cinek07c80172016-04-21 16:40:47 -07007929 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek384804b2018-04-18 14:31:07 +08007930 handleInboxImageMargin(contentView, rowIds[i], first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007931 result.getIconMarginEnd());
Selim Cinek07c80172016-04-21 16:40:47 -07007932 if (first) {
7933 onlyViewId = rowIds[i];
7934 } else {
7935 onlyViewId = 0;
7936 }
Selim Cinek247fa012016-02-18 09:50:48 -08007937 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007938 }
7939 i++;
7940 }
Selim Cinek07c80172016-04-21 16:40:47 -07007941 if (onlyViewId != 0) {
7942 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
7943 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7944 R.dimen.notification_text_margin_top);
7945 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
7946 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007947
Daniel Sandler879c5e02012-04-17 16:46:51 -04007948 return contentView;
7949 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007950
Julia Reynolds7217dc92018-03-07 12:12:09 -05007951 /**
7952 * @hide
7953 */
7954 @Override
7955 public boolean areNotificationsVisiblyDifferent(Style other) {
7956 if (other == null || getClass() != other.getClass()) {
7957 return true;
7958 }
7959 InboxStyle newS = (InboxStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04007960
7961 final ArrayList<CharSequence> myLines = getLines();
7962 final ArrayList<CharSequence> newLines = newS.getLines();
7963 final int n = myLines.size();
7964 if (n != newLines.size()) {
7965 return true;
7966 }
7967
7968 for (int i = 0; i < n; i++) {
7969 if (!Objects.equals(
7970 String.valueOf(myLines.get(i)),
7971 String.valueOf(newLines.get(i)))) {
7972 return true;
7973 }
7974 }
7975 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05007976 }
7977
Selim Cinek384804b2018-04-18 14:31:07 +08007978 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007979 int marginEndValue) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08007980 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08007981 if (first) {
7982 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
7983 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
7984 boolean hasProgress = max != 0 || ind;
Selim Cinek384804b2018-04-18 14:31:07 +08007985 if (!hasProgress) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08007986 endMargin = marginEndValue;
Selim Cinek247fa012016-02-18 09:50:48 -08007987 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007988 }
Selim Cinek1c72fa02018-04-23 18:00:54 +08007989 contentView.setViewLayoutMarginEnd(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08007990 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04007991 }
Dan Sandler842dd772014-05-15 09:36:47 -04007992
7993 /**
7994 * Notification style for media playback notifications.
7995 *
7996 * In the expanded form, {@link Notification#bigContentView}, up to 5
7997 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04007998 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04007999 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
8000 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
8001 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08008002 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04008003 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
8004 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01008005 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04008006 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08008007 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01008008 * Notifications created with MediaStyle will have their category set to
8009 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
8010 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08008011 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07008012 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
8013 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04008014 * the System UI can identify this as a notification representing an active media session
8015 * and respond accordingly (by showing album artwork in the lockscreen, for example).
8016 *
Selim Cinek99104832017-01-25 14:47:33 -08008017 * <p>
8018 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
8019 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
8020 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
8021 * <p>
8022 *
Dan Sandler842dd772014-05-15 09:36:47 -04008023 * To use this style with your Notification, feed it to
8024 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8025 * <pre class="prettyprint">
8026 * Notification noti = new Notification.Builder()
8027 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01008028 * .setContentTitle(&quot;Track title&quot;)
8029 * .setContentText(&quot;Artist - Album&quot;)
8030 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01008031 * .setStyle(<b>new Notification.MediaStyle()</b>
8032 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04008033 * .build();
8034 * </pre>
8035 *
8036 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08008037 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04008038 */
8039 public static class MediaStyle extends Style {
Gus Prevas9cc96602018-10-11 11:24:23 -04008040 // Changing max media buttons requires also changing templates
8041 // (notification_template_material_media and notification_template_material_big_media).
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008042 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04008043 static final int MAX_MEDIA_BUTTONS = 5;
Gus Prevas9cc96602018-10-11 11:24:23 -04008044 @IdRes private static final int[] MEDIA_BUTTON_IDS = {
8045 R.id.action0,
8046 R.id.action1,
8047 R.id.action2,
8048 R.id.action3,
8049 R.id.action4,
8050 };
Dan Sandler842dd772014-05-15 09:36:47 -04008051
8052 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07008053 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04008054
8055 public MediaStyle() {
8056 }
8057
Adrian Roosf5faf9d2016-05-23 13:56:15 -07008058 /**
8059 * @deprecated use {@code MediaStyle()}.
8060 */
8061 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04008062 public MediaStyle(Builder builder) {
8063 setBuilder(builder);
8064 }
8065
8066 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008067 * 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 -04008068 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008069 *
8070 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04008071 */
8072 public MediaStyle setShowActionsInCompactView(int...actions) {
8073 mActionsToShowInCompact = actions;
8074 return this;
8075 }
8076
8077 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07008078 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
8079 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04008080 */
Jeff Browndba34ba2014-06-24 20:46:03 -07008081 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04008082 mToken = token;
8083 return this;
8084 }
8085
Christoph Studer4600f9b2014-07-22 22:44:43 +02008086 /**
8087 * @hide
8088 */
Dan Sandler842dd772014-05-15 09:36:47 -04008089 @Override
Mathew Inwood61e8ae62018-08-14 14:17:44 +01008090 @UnsupportedAppUsage
Dan Sandler842dd772014-05-15 09:36:47 -04008091 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02008092 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01008093 if (wip.category == null) {
8094 wip.category = Notification.CATEGORY_TRANSPORT;
8095 }
Dan Sandler842dd772014-05-15 09:36:47 -04008096 return wip;
8097 }
8098
Christoph Studer4600f9b2014-07-22 22:44:43 +02008099 /**
8100 * @hide
8101 */
8102 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008103 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04008104 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02008105 }
8106
8107 /**
8108 * @hide
8109 */
8110 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04008111 public RemoteViews makeBigContentView() {
8112 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02008113 }
8114
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008115 /**
8116 * @hide
8117 */
8118 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008119 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008120 RemoteViews expanded = makeMediaBigContentView();
8121 return expanded != null ? expanded : makeMediaContentView();
8122 }
8123
Dan Sandler842dd772014-05-15 09:36:47 -04008124 /** @hide */
8125 @Override
8126 public void addExtras(Bundle extras) {
8127 super.addExtras(extras);
8128
8129 if (mToken != null) {
8130 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
8131 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01008132 if (mActionsToShowInCompact != null) {
8133 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
8134 }
Dan Sandler842dd772014-05-15 09:36:47 -04008135 }
8136
Christoph Studer4600f9b2014-07-22 22:44:43 +02008137 /**
8138 * @hide
8139 */
8140 @Override
8141 protected void restoreFromExtras(Bundle extras) {
8142 super.restoreFromExtras(extras);
8143
8144 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
8145 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
8146 }
8147 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
8148 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
8149 }
8150 }
8151
Julia Reynolds7217dc92018-03-07 12:12:09 -05008152 /**
8153 * @hide
8154 */
8155 @Override
8156 public boolean areNotificationsVisiblyDifferent(Style other) {
8157 if (other == null || getClass() != other.getClass()) {
8158 return true;
8159 }
8160 // All fields to compare are on the Notification object
8161 return false;
8162 }
8163
Gus Prevas9cc96602018-10-11 11:24:23 -04008164 private void bindMediaActionButton(RemoteViews container, @IdRes int buttonId,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008165 Action action, StandardTemplateParams p) {
Dan Sandler842dd772014-05-15 09:36:47 -04008166 final boolean tombstone = (action.actionIntent == null);
Gus Prevas9cc96602018-10-11 11:24:23 -04008167 container.setViewVisibility(buttonId, View.VISIBLE);
8168 container.setImageViewIcon(buttonId, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07008169
8170 // If the action buttons should not be tinted, then just use the default
8171 // notification color. Otherwise, just use the passed-in color.
Gus Prevas9cc96602018-10-11 11:24:23 -04008172 Resources resources = mBuilder.mContext.getResources();
8173 Configuration currentConfig = resources.getConfiguration();
Lucas Dupinf03e7522018-06-25 16:21:13 -07008174 boolean inNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
8175 == Configuration.UI_MODE_NIGHT_YES;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008176 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized(p)
8177 ? getActionColor(p)
Lucas Dupina291d192018-06-07 13:59:42 -07008178 : ContrastColorUtil.resolveColor(mBuilder.mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07008179 Notification.COLOR_DEFAULT, inNightMode);
Anthony Chenad4d1582017-04-10 16:07:58 -07008180
Gus Prevas9cc96602018-10-11 11:24:23 -04008181 container.setDrawableTint(buttonId, false, tintColor,
Sunny Goyal5b153922017-09-21 21:00:36 -07008182 PorterDuff.Mode.SRC_ATOP);
Gus Prevas9cc96602018-10-11 11:24:23 -04008183
8184 final TypedArray typedArray = mBuilder.mContext.obtainStyledAttributes(
8185 new int[]{ android.R.attr.colorControlHighlight });
8186 int rippleAlpha = Color.alpha(typedArray.getColor(0, 0));
8187 typedArray.recycle();
8188 int rippleColor = Color.argb(rippleAlpha, Color.red(tintColor), Color.green(tintColor),
8189 Color.blue(tintColor));
8190 container.setRippleDrawableColor(buttonId, ColorStateList.valueOf(rippleColor));
8191
Dan Sandler842dd772014-05-15 09:36:47 -04008192 if (!tombstone) {
Gus Prevas9cc96602018-10-11 11:24:23 -04008193 container.setOnClickPendingIntent(buttonId, action.actionIntent);
Dan Sandler842dd772014-05-15 09:36:47 -04008194 }
Gus Prevas9cc96602018-10-11 11:24:23 -04008195 container.setContentDescription(buttonId, action.title);
Dan Sandler842dd772014-05-15 09:36:47 -04008196 }
8197
8198 private RemoteViews makeMediaContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008199 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).fillTextsFrom(
8200 mBuilder);
Dan Sandler842dd772014-05-15 09:36:47 -04008201 RemoteViews view = mBuilder.applyStandardTemplate(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008202 R.layout.notification_template_material_media, p,
Selim Cinek384804b2018-04-18 14:31:07 +08008203 null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04008204
8205 final int numActions = mBuilder.mActions.size();
Gus Prevas9cc96602018-10-11 11:24:23 -04008206 final int numActionsToShow = mActionsToShowInCompact == null
Dan Sandler842dd772014-05-15 09:36:47 -04008207 ? 0
8208 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Gus Prevas9cc96602018-10-11 11:24:23 -04008209 if (numActionsToShow > numActions) {
8210 throw new IllegalArgumentException(String.format(
8211 "setShowActionsInCompactView: action %d out of bounds (max %d)",
8212 numActions, numActions - 1));
8213 }
8214 for (int i = 0; i < MAX_MEDIA_BUTTONS_IN_COMPACT; i++) {
8215 if (i < numActionsToShow) {
Dan Sandler842dd772014-05-15 09:36:47 -04008216 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008217 bindMediaActionButton(view, MEDIA_BUTTON_IDS[i], action, p);
Gus Prevas9cc96602018-10-11 11:24:23 -04008218 } else {
8219 view.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04008220 }
8221 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08008222 handleImage(view);
8223 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008224 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07008225 if (mBuilder.mN.hasLargeIcon()) {
Selim Cinek384804b2018-04-18 14:31:07 +08008226 endMargin = R.dimen.notification_media_image_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08008227 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008228 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04008229 return view;
8230 }
8231
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008232 private int getActionColor(StandardTemplateParams p) {
8233 return mBuilder.isColorized(p) ? mBuilder.getPrimaryTextColor(p)
8234 : mBuilder.resolveContrastColor(p);
Selim Cinek99104832017-01-25 14:47:33 -08008235 }
8236
Dan Sandler842dd772014-05-15 09:36:47 -04008237 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008238 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008239 // Dont add an expanded view if there is no more content to be revealed
8240 int actionsInCompact = mActionsToShowInCompact == null
8241 ? 0
8242 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07008243 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008244 return null;
8245 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008246 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).fillTextsFrom(
8247 mBuilder);
Selim Cinek5bf069a2015-11-10 19:14:27 -05008248 RemoteViews big = mBuilder.applyStandardTemplate(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008249 R.layout.notification_template_material_big_media, p , null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04008250
Gus Prevas9cc96602018-10-11 11:24:23 -04008251 for (int i = 0; i < MAX_MEDIA_BUTTONS; i++) {
8252 if (i < actionCount) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008253 bindMediaActionButton(big, MEDIA_BUTTON_IDS[i], mBuilder.mActions.get(i), p);
Gus Prevas9cc96602018-10-11 11:24:23 -04008254 } else {
8255 big.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04008256 }
8257 }
linanson2bcd4032019-01-14 15:22:04 +08008258 bindMediaActionButton(big, R.id.media_seamless, new Action(R.drawable.ic_media_seamless,
8259 mBuilder.mContext.getString(
8260 com.android.internal.R.string.ext_media_seamless_action), null), p);
8261 big.setViewVisibility(R.id.media_seamless, View.GONE);
Selim Cinek5bf069a2015-11-10 19:14:27 -05008262 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04008263 return big;
8264 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008265
Selim Cinek5bf069a2015-11-10 19:14:27 -05008266 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07008267 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008268 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
8269 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008270 }
8271 }
8272
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008273 /**
8274 * @hide
8275 */
8276 @Override
8277 protected boolean hasProgress() {
8278 return false;
8279 }
Dan Sandler842dd772014-05-15 09:36:47 -04008280 }
Griff Hazen61a9e862014-05-22 16:05:19 -07008281
Selim Cinek593610c2016-02-16 18:42:57 -08008282 /**
8283 * Notification style for custom views that are decorated by the system
8284 *
8285 * <p>Instead of providing a notification that is completely custom, a developer can set this
8286 * style and still obtain system decorations like the notification header with the expand
8287 * affordance and actions.
8288 *
8289 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8290 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8291 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8292 * corresponding custom views to display.
8293 *
8294 * To use this style with your Notification, feed it to
8295 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8296 * <pre class="prettyprint">
8297 * Notification noti = new Notification.Builder()
8298 * .setSmallIcon(R.drawable.ic_stat_player)
8299 * .setLargeIcon(albumArtBitmap))
8300 * .setCustomContentView(contentView);
8301 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
8302 * .build();
8303 * </pre>
8304 */
8305 public static class DecoratedCustomViewStyle extends Style {
8306
8307 public DecoratedCustomViewStyle() {
8308 }
8309
Selim Cinek593610c2016-02-16 18:42:57 -08008310 /**
8311 * @hide
8312 */
8313 public boolean displayCustomViewInline() {
8314 return true;
8315 }
8316
8317 /**
8318 * @hide
8319 */
8320 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008321 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008322 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
8323 }
8324
8325 /**
8326 * @hide
8327 */
8328 @Override
8329 public RemoteViews makeBigContentView() {
8330 return makeDecoratedBigContentView();
8331 }
8332
8333 /**
8334 * @hide
8335 */
8336 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008337 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008338 return makeDecoratedHeadsUpContentView();
8339 }
8340
Selim Cinek593610c2016-02-16 18:42:57 -08008341 private RemoteViews makeDecoratedHeadsUpContentView() {
8342 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
8343 ? mBuilder.mN.contentView
8344 : mBuilder.mN.headsUpContentView;
8345 if (mBuilder.mActions.size() == 0) {
8346 return makeStandardTemplateWithCustomContent(headsUpContentView);
8347 }
Selim Cinek384804b2018-04-18 14:31:07 +08008348 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008349 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008350 mBuilder.getBigBaseLayoutResource(), result);
8351 buildIntoRemoteViewContent(remoteViews, headsUpContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008352 return remoteViews;
8353 }
8354
Selim Cinek593610c2016-02-16 18:42:57 -08008355 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
Selim Cinek384804b2018-04-18 14:31:07 +08008356 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008357 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08008358 mBuilder.getBaseLayoutResource(), result);
8359 buildIntoRemoteViewContent(remoteViews, customContent, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008360 return remoteViews;
8361 }
8362
Selim Cinek593610c2016-02-16 18:42:57 -08008363 private RemoteViews makeDecoratedBigContentView() {
8364 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
8365 ? mBuilder.mN.contentView
8366 : mBuilder.mN.bigContentView;
8367 if (mBuilder.mActions.size() == 0) {
8368 return makeStandardTemplateWithCustomContent(bigContentView);
8369 }
Selim Cinek384804b2018-04-18 14:31:07 +08008370 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008371 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008372 mBuilder.getBigBaseLayoutResource(), result);
8373 buildIntoRemoteViewContent(remoteViews, bigContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008374 return remoteViews;
8375 }
Selim Cinek247fa012016-02-18 09:50:48 -08008376
8377 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
Selim Cinek384804b2018-04-18 14:31:07 +08008378 RemoteViews customContent, TemplateBindResult result) {
Selim Cinek6fe4a102019-01-25 14:38:24 -08008379 int childIndex = -1;
Adrian Roos5081c0d2016-02-26 16:04:19 -08008380 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008381 // Need to clone customContent before adding, because otherwise it can no longer be
8382 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008383 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07008384 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
8385 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Sunny Goyalc12d31c2018-11-12 16:29:18 -08008386 remoteViews.addFlags(RemoteViews.FLAG_REAPPLY_DISALLOWED);
Selim Cinek6fe4a102019-01-25 14:38:24 -08008387 childIndex = 0;
Adrian Roos5081c0d2016-02-26 16:04:19 -08008388 }
Selim Cinek6fe4a102019-01-25 14:38:24 -08008389 remoteViews.setIntTag(R.id.notification_main_column,
8390 com.android.internal.R.id.notification_custom_view_index_tag,
8391 childIndex);
Selim Cinek247fa012016-02-18 09:50:48 -08008392 // also update the end margin if there is an image
Selim Cinek384804b2018-04-18 14:31:07 +08008393 Resources resources = mBuilder.mContext.getResources();
8394 int endMargin = resources.getDimensionPixelSize(
Selim Cinek1c72fa02018-04-23 18:00:54 +08008395 R.dimen.notification_content_margin_end) + result.getIconMarginEnd();
Selim Cinek384804b2018-04-18 14:31:07 +08008396 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08008397 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05008398
8399 /**
8400 * @hide
8401 */
8402 @Override
8403 public boolean areNotificationsVisiblyDifferent(Style other) {
8404 if (other == null || getClass() != other.getClass()) {
8405 return true;
8406 }
8407 // Comparison done for all custom RemoteViews, independent of style
8408 return false;
8409 }
Selim Cinek593610c2016-02-16 18:42:57 -08008410 }
8411
Selim Cinek03eb3b72016-02-18 10:39:45 -08008412 /**
8413 * Notification style for media custom views that are decorated by the system
8414 *
8415 * <p>Instead of providing a media notification that is completely custom, a developer can set
8416 * this style and still obtain system decorations like the notification header with the expand
8417 * affordance and actions.
8418 *
8419 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8420 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8421 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8422 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08008423 * <p>
8424 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
8425 * notification by using {@link Notification.Builder#setColorized(boolean)}.
8426 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08008427 * To use this style with your Notification, feed it to
8428 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8429 * <pre class="prettyprint">
8430 * Notification noti = new Notification.Builder()
8431 * .setSmallIcon(R.drawable.ic_stat_player)
8432 * .setLargeIcon(albumArtBitmap))
8433 * .setCustomContentView(contentView);
8434 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
8435 * .setMediaSession(mySession))
8436 * .build();
8437 * </pre>
8438 *
8439 * @see android.app.Notification.DecoratedCustomViewStyle
8440 * @see android.app.Notification.MediaStyle
8441 */
8442 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
8443
8444 public DecoratedMediaCustomViewStyle() {
8445 }
8446
Selim Cinek03eb3b72016-02-18 10:39:45 -08008447 /**
8448 * @hide
8449 */
8450 public boolean displayCustomViewInline() {
8451 return true;
8452 }
8453
8454 /**
8455 * @hide
8456 */
8457 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008458 public RemoteViews makeContentView(boolean increasedHeight) {
8459 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008460 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8461 mBuilder.mN.contentView);
8462 }
8463
8464 /**
8465 * @hide
8466 */
8467 @Override
8468 public RemoteViews makeBigContentView() {
8469 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
8470 ? mBuilder.mN.bigContentView
8471 : mBuilder.mN.contentView;
8472 return makeBigContentViewWithCustomContent(customRemoteView);
8473 }
8474
8475 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
8476 RemoteViews remoteViews = super.makeBigContentView();
8477 if (remoteViews != null) {
8478 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
8479 customRemoteView);
8480 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08008481 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008482 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8483 customRemoteView);
8484 } else {
8485 return null;
8486 }
8487 }
8488
8489 /**
8490 * @hide
8491 */
8492 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008493 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08008494 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
8495 ? mBuilder.mN.headsUpContentView
8496 : mBuilder.mN.contentView;
8497 return makeBigContentViewWithCustomContent(customRemoteView);
8498 }
8499
Julia Reynolds7217dc92018-03-07 12:12:09 -05008500 /**
8501 * @hide
8502 */
8503 @Override
8504 public boolean areNotificationsVisiblyDifferent(Style other) {
8505 if (other == null || getClass() != other.getClass()) {
8506 return true;
8507 }
8508 // Comparison done for all custom RemoteViews, independent of style
8509 return false;
8510 }
8511
Selim Cinek03eb3b72016-02-18 10:39:45 -08008512 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
8513 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08008514 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008515 // Need to clone customContent before adding, because otherwise it can no longer be
8516 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008517 customContent = customContent.clone();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008518 customContent.overrideTextColors(mBuilder.getPrimaryTextColor(mBuilder.mParams));
Selim Cinekf91017e2016-03-14 12:25:09 -07008519 remoteViews.removeAllViews(id);
8520 remoteViews.addView(id, customContent);
Sunny Goyalc12d31c2018-11-12 16:29:18 -08008521 remoteViews.addFlags(RemoteViews.FLAG_REAPPLY_DISALLOWED);
Adrian Roos5081c0d2016-02-26 16:04:19 -08008522 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08008523 return remoteViews;
8524 }
8525 }
8526
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008527 /**
8528 * Encapsulates the information needed to display a notification as a bubble.
8529 *
8530 * <p>A bubble is used to display app content in a floating window over the existing
8531 * foreground activity. A bubble has a collapsed state represented by an icon,
8532 * {@link BubbleMetadata.Builder#setIcon(Icon)} and an expanded state which is populated
8533 * via {@link BubbleMetadata.Builder#setIntent(PendingIntent)}.</p>
8534 *
8535 * <b>Notifications with a valid and allowed bubble will display in collapsed state
8536 * outside of the notification shade on unlocked devices. When a user interacts with the
8537 * collapsed bubble, the bubble intent will be invoked and displayed.</b>
8538 *
8539 * @see Notification.Builder#setBubbleMetadata(BubbleMetadata)
8540 */
8541 public static final class BubbleMetadata implements Parcelable {
8542
8543 private PendingIntent mPendingIntent;
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008544 private PendingIntent mDeleteIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008545 private Icon mIcon;
8546 private int mDesiredHeight;
Mady Mellor7af771a2019-03-07 15:04:54 -08008547 @DimenRes private int mDesiredHeightResId;
Mady Mellor0157ff22019-01-24 15:31:48 -08008548 private int mFlags;
8549
8550 /**
8551 * If set and the app creating the bubble is in the foreground, the bubble will be posted
8552 * in its expanded state, with the contents of {@link #getIntent()} in a floating window.
8553 *
8554 * <p>If the app creating the bubble is not in the foreground this flag has no effect.</p>
8555 *
8556 * <p>Generally this flag should only be set if the user has performed an action to request
8557 * or create a bubble.</p>
8558 */
8559 private static final int FLAG_AUTO_EXPAND_BUBBLE = 0x00000001;
8560
8561 /**
Mady Mellor8a1f0252019-04-01 11:31:34 -07008562 * If set and the app posting the bubble is in the foreground, the bubble will
8563 * be posted <b>without</b> the associated notification in the notification shade.
Mady Mellor0157ff22019-01-24 15:31:48 -08008564 *
Mady Mellor8a1f0252019-04-01 11:31:34 -07008565 * <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 -08008566 *
8567 * <p>Generally this flag should only be set if the user has performed an action to request
Mady Mellor8a1f0252019-04-01 11:31:34 -07008568 * or create a bubble, or if the user has seen the content in the notification and the
8569 * notification is no longer relevant.</p>
Mady Mellor0157ff22019-01-24 15:31:48 -08008570 */
Mady Mellor8a1f0252019-04-01 11:31:34 -07008571 private static final int FLAG_SUPPRESS_NOTIFICATION = 0x00000002;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008572
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008573 private BubbleMetadata(PendingIntent expandIntent, PendingIntent deleteIntent,
Mady Mellor7af771a2019-03-07 15:04:54 -08008574 Icon icon, int height, @DimenRes int heightResId) {
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008575 mPendingIntent = expandIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008576 mIcon = icon;
8577 mDesiredHeight = height;
Mady Mellor7af771a2019-03-07 15:04:54 -08008578 mDesiredHeightResId = heightResId;
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008579 mDeleteIntent = deleteIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008580 }
8581
8582 private BubbleMetadata(Parcel in) {
8583 mPendingIntent = PendingIntent.CREATOR.createFromParcel(in);
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008584 mIcon = Icon.CREATOR.createFromParcel(in);
8585 mDesiredHeight = in.readInt();
Mady Mellor0157ff22019-01-24 15:31:48 -08008586 mFlags = in.readInt();
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008587 if (in.readInt() != 0) {
8588 mDeleteIntent = PendingIntent.CREATOR.createFromParcel(in);
8589 }
Mady Mellor7af771a2019-03-07 15:04:54 -08008590 mDesiredHeightResId = in.readInt();
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008591 }
8592
8593 /**
8594 * @return the pending intent used to populate the floating window for this bubble.
8595 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008596 @NonNull
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008597 public PendingIntent getIntent() {
8598 return mPendingIntent;
8599 }
8600
8601 /**
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008602 * @return the pending intent to send when the bubble is dismissed by a user, if one exists.
8603 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008604 @Nullable
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008605 public PendingIntent getDeleteIntent() {
8606 return mDeleteIntent;
8607 }
8608
8609 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008610 * @return the icon that will be displayed for this bubble when it is collapsed.
8611 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008612 @NonNull
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008613 public Icon getIcon() {
8614 return mIcon;
8615 }
8616
8617 /**
Mady Mellor7af771a2019-03-07 15:04:54 -08008618 * @return the ideal height, in DPs, for the floating window that app content defined by
Mady Mellor8a529e22019-03-25 17:37:55 -07008619 * {@link #getIntent()} for this bubble. A value of 0 indicates a desired height has not
8620 * been set.
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008621 */
Mady Mellor8a529e22019-03-25 17:37:55 -07008622 @Dimension(unit = DP)
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008623 public int getDesiredHeight() {
8624 return mDesiredHeight;
8625 }
8626
Mady Mellor0157ff22019-01-24 15:31:48 -08008627 /**
Mady Mellor7af771a2019-03-07 15:04:54 -08008628 * @return the resId of ideal height for the floating window that app content defined by
Mady Mellor8a529e22019-03-25 17:37:55 -07008629 * {@link #getIntent()} for this bubble. A value of 0 indicates a res value has not
8630 * been provided for the desired height.
Mady Mellor7af771a2019-03-07 15:04:54 -08008631 */
8632 @DimenRes
8633 public int getDesiredHeightResId() {
8634 return mDesiredHeightResId;
8635 }
8636
8637 /**
Mady Mellor0157ff22019-01-24 15:31:48 -08008638 * @return whether this bubble should auto expand when it is posted.
8639 *
8640 * @see BubbleMetadata.Builder#setAutoExpandBubble(boolean)
8641 */
8642 public boolean getAutoExpandBubble() {
8643 return (mFlags & FLAG_AUTO_EXPAND_BUBBLE) != 0;
8644 }
8645
8646 /**
Mady Mellorc529d6d2019-04-16 14:22:52 -07008647 * @return whether this bubble should suppress the notification when it is posted.
8648 *
8649 * @see BubbleMetadata.Builder#setSuppressNotification(boolean)
8650 */
8651 public boolean isNotificationSuppressed() {
Mady Mellor8a1f0252019-04-01 11:31:34 -07008652 return (mFlags & FLAG_SUPPRESS_NOTIFICATION) != 0;
Mady Mellor0157ff22019-01-24 15:31:48 -08008653 }
8654
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07008655 public static final @android.annotation.NonNull Parcelable.Creator<BubbleMetadata> CREATOR =
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008656 new Parcelable.Creator<BubbleMetadata>() {
8657
8658 @Override
8659 public BubbleMetadata createFromParcel(Parcel source) {
8660 return new BubbleMetadata(source);
8661 }
8662
8663 @Override
8664 public BubbleMetadata[] newArray(int size) {
8665 return new BubbleMetadata[size];
8666 }
8667 };
8668
8669 @Override
8670 public int describeContents() {
8671 return 0;
8672 }
8673
8674 @Override
8675 public void writeToParcel(Parcel out, int flags) {
8676 mPendingIntent.writeToParcel(out, 0);
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008677 mIcon.writeToParcel(out, 0);
8678 out.writeInt(mDesiredHeight);
Mady Mellor0157ff22019-01-24 15:31:48 -08008679 out.writeInt(mFlags);
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008680 out.writeInt(mDeleteIntent != null ? 1 : 0);
8681 if (mDeleteIntent != null) {
8682 mDeleteIntent.writeToParcel(out, 0);
8683 }
Mady Mellor7af771a2019-03-07 15:04:54 -08008684 out.writeInt(mDesiredHeightResId);
Mady Mellor0157ff22019-01-24 15:31:48 -08008685 }
8686
8687 private void setFlags(int flags) {
8688 mFlags = flags;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008689 }
8690
8691 /**
8692 * Builder to construct a {@link BubbleMetadata} object.
8693 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008694 public static final class Builder {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008695
8696 private PendingIntent mPendingIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008697 private Icon mIcon;
8698 private int mDesiredHeight;
Mady Mellor7af771a2019-03-07 15:04:54 -08008699 @DimenRes private int mDesiredHeightResId;
Mady Mellor0157ff22019-01-24 15:31:48 -08008700 private int mFlags;
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008701 private PendingIntent mDeleteIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008702
8703 /**
8704 * Constructs a new builder object.
8705 */
8706 public Builder() {
8707 }
8708
8709 /**
8710 * Sets the intent that will be used when the bubble is expanded. This will display the
8711 * app content in a floating window over the existing foreground activity.
8712 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008713 @NonNull
8714 public BubbleMetadata.Builder setIntent(@NonNull PendingIntent intent) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008715 if (intent == null) {
8716 throw new IllegalArgumentException("Bubble requires non-null pending intent");
8717 }
8718 mPendingIntent = intent;
8719 return this;
8720 }
8721
8722 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008723 * Sets the icon that will represent the bubble when it is collapsed.
8724 *
8725 * <p>An icon is required and should be representative of the content within the bubble.
8726 * If your app produces multiple bubbles, the image should be unique for each of them.
8727 * </p>
Mady Mellor9848a6c2019-03-19 15:29:05 -07008728 *
8729 * <p>The shape of a bubble icon is adaptive and can match the device theme.
8730 *
8731 * If your icon is bitmap-based, you should create it using
8732 * {@link Icon#createWithAdaptiveBitmap(Bitmap)}, otherwise this method will throw.
8733 *
8734 * If your icon is not bitmap-based, you should expect that the icon will be tinted.
8735 * </p>
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008736 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008737 @NonNull
8738 public BubbleMetadata.Builder setIcon(@NonNull Icon icon) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008739 if (icon == null) {
8740 throw new IllegalArgumentException("Bubbles require non-null icon");
8741 }
Mady Mellor9848a6c2019-03-19 15:29:05 -07008742 if (icon.getType() == TYPE_BITMAP) {
8743 throw new IllegalArgumentException("When using bitmap based icons, Bubbles "
8744 + "require TYPE_ADAPTIVE_BITMAP, please use"
8745 + " Icon#createWithAdaptiveBitmap instead");
8746 }
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008747 mIcon = icon;
8748 return this;
8749 }
8750
8751 /**
Mady Mellor7af771a2019-03-07 15:04:54 -08008752 * Sets the desired height in DPs for the app content defined by
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008753 * {@link #setIntent(PendingIntent)}, this height may not be respected if there is not
8754 * enough space on the screen or if the provided height is too small to be useful.
Mady Mellor7af771a2019-03-07 15:04:54 -08008755 * <p>
8756 * If {@link #setDesiredHeightResId(int)} was previously called on this builder, the
8757 * previous value set will be cleared after calling this method, and this value will
8758 * be used instead.
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008759 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008760 @NonNull
Mady Mellor8a529e22019-03-25 17:37:55 -07008761 public BubbleMetadata.Builder setDesiredHeight(@Dimension(unit = DP) int height) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008762 mDesiredHeight = Math.max(height, 0);
Mady Mellor7af771a2019-03-07 15:04:54 -08008763 mDesiredHeightResId = 0;
8764 return this;
8765 }
8766
8767
8768 /**
8769 * Sets the desired height via resId for the app content defined by
8770 * {@link #setIntent(PendingIntent)}, this height may not be respected if there is not
8771 * enough space on the screen or if the provided height is too small to be useful.
8772 * <p>
8773 * If {@link #setDesiredHeight(int)} was previously called on this builder, the
8774 * previous value set will be cleared after calling this method, and this value will
8775 * be used instead.
8776 */
8777 @NonNull
8778 public BubbleMetadata.Builder setDesiredHeightResId(@DimenRes int heightResId) {
8779 mDesiredHeightResId = heightResId;
8780 mDesiredHeight = 0;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008781 return this;
8782 }
8783
8784 /**
Mady Mellor0157ff22019-01-24 15:31:48 -08008785 * If set and the app creating the bubble is in the foreground, the bubble will be
8786 * posted in its expanded state, with the contents of {@link #getIntent()} in a
8787 * floating window.
8788 *
8789 * <p>If the app creating the bubble is not in the foreground this flag has no effect.
8790 * </p>
8791 *
8792 * <p>Generally this flag should only be set if the user has performed an action to
8793 * request or create a bubble.</p>
8794 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008795 @NonNull
Mady Mellor0157ff22019-01-24 15:31:48 -08008796 public BubbleMetadata.Builder setAutoExpandBubble(boolean shouldExpand) {
8797 setFlag(FLAG_AUTO_EXPAND_BUBBLE, shouldExpand);
8798 return this;
8799 }
8800
8801 /**
Mady Mellor8a1f0252019-04-01 11:31:34 -07008802 * If set and the app posting the bubble is in the foreground, the bubble will be
8803 * posted <b>without</b> the associated notification in the notification shade.
8804 *
8805 * <p>If the app posting the bubble is not in the foreground this flag has no effect.
8806 * </p>
8807 *
8808 * <p>Generally this flag should only be set if the user has performed an action to
8809 * request or create a bubble, or if the user has seen the content in the notification
8810 * and the notification is no longer relevant.</p>
8811 */
8812 @NonNull
8813 public BubbleMetadata.Builder setSuppressNotification(boolean shouldSupressNotif) {
8814 setFlag(FLAG_SUPPRESS_NOTIFICATION, shouldSupressNotif);
Mady Mellor0157ff22019-01-24 15:31:48 -08008815 return this;
8816 }
8817
8818 /**
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008819 * Sets an optional intent to send when this bubble is explicitly removed by the user.
8820 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008821 @NonNull
8822 public BubbleMetadata.Builder setDeleteIntent(@Nullable PendingIntent deleteIntent) {
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008823 mDeleteIntent = deleteIntent;
8824 return this;
8825 }
8826
8827 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008828 * Creates the {@link BubbleMetadata} defined by this builder.
8829 * <p>Will throw {@link IllegalStateException} if required fields have not been set
8830 * on this builder.</p>
8831 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008832 @NonNull
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008833 public BubbleMetadata build() {
8834 if (mPendingIntent == null) {
8835 throw new IllegalStateException("Must supply pending intent to bubble");
8836 }
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008837 if (mIcon == null) {
8838 throw new IllegalStateException("Must supply an icon for the bubble");
8839 }
Mady Mellor138f4c92019-02-20 16:12:17 -08008840 BubbleMetadata data = new BubbleMetadata(mPendingIntent, mDeleteIntent,
Mady Mellor7af771a2019-03-07 15:04:54 -08008841 mIcon, mDesiredHeight, mDesiredHeightResId);
Mady Mellor0157ff22019-01-24 15:31:48 -08008842 data.setFlags(mFlags);
8843 return data;
8844 }
8845
8846 /**
8847 * @hide
8848 */
8849 public BubbleMetadata.Builder setFlag(int mask, boolean value) {
8850 if (value) {
8851 mFlags |= mask;
8852 } else {
8853 mFlags &= ~mask;
8854 }
8855 return this;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008856 }
8857 }
8858 }
8859
8860
Christoph Studer4600f9b2014-07-22 22:44:43 +02008861 // When adding a new Style subclass here, don't forget to update
8862 // Builder.getNotificationStyleClass.
8863
Griff Hazen61a9e862014-05-22 16:05:19 -07008864 /**
8865 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
8866 * metadata or change options on a notification builder.
8867 */
8868 public interface Extender {
8869 /**
8870 * Apply this extender to a notification builder.
8871 * @param builder the builder to be modified.
8872 * @return the build object for chaining.
8873 */
8874 public Builder extend(Builder builder);
8875 }
8876
8877 /**
8878 * Helper class to add wearable extensions to notifications.
8879 * <p class="note"> See
8880 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
8881 * for Android Wear</a> for more information on how to use this class.
8882 * <p>
8883 * To create a notification with wearable extensions:
8884 * <ol>
8885 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
8886 * properties.
8887 * <li>Create a {@link android.app.Notification.WearableExtender}.
8888 * <li>Set wearable-specific properties using the
8889 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
8890 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
8891 * notification.
8892 * <li>Post the notification to the notification system with the
8893 * {@code NotificationManager.notify(...)} methods.
8894 * </ol>
8895 *
8896 * <pre class="prettyprint">
8897 * Notification notif = new Notification.Builder(mContext)
8898 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
8899 * .setContentText(subject)
8900 * .setSmallIcon(R.drawable.new_mail)
8901 * .extend(new Notification.WearableExtender()
8902 * .setContentIcon(R.drawable.new_mail))
8903 * .build();
8904 * NotificationManager notificationManger =
8905 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
8906 * notificationManger.notify(0, notif);</pre>
8907 *
8908 * <p>Wearable extensions can be accessed on an existing notification by using the
8909 * {@code WearableExtender(Notification)} constructor,
8910 * and then using the {@code get} methods to access values.
8911 *
8912 * <pre class="prettyprint">
8913 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
8914 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07008915 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008916 */
8917 public static final class WearableExtender implements Extender {
8918 /**
8919 * Sentinel value for an action index that is unset.
8920 */
8921 public static final int UNSET_ACTION_INDEX = -1;
8922
8923 /**
8924 * Size value for use with {@link #setCustomSizePreset} to show this notification with
8925 * default sizing.
8926 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07008927 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07008928 * on their content.
Gus Prevasd7363752018-09-18 14:35:15 -04008929 *
8930 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008931 */
Gus Prevasd7363752018-09-18 14:35:15 -04008932 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008933 public static final int SIZE_DEFAULT = 0;
8934
8935 /**
8936 * Size value for use with {@link #setCustomSizePreset} to show this notification
8937 * with an extra small size.
8938 * <p>This value is only applicable for custom display notifications created using
8939 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008940 *
8941 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008942 */
Gus Prevasd7363752018-09-18 14:35:15 -04008943 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008944 public static final int SIZE_XSMALL = 1;
8945
8946 /**
8947 * Size value for use with {@link #setCustomSizePreset} to show this notification
8948 * with a small size.
8949 * <p>This value is only applicable for custom display notifications created using
8950 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008951 *
8952 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008953 */
Gus Prevasd7363752018-09-18 14:35:15 -04008954 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008955 public static final int SIZE_SMALL = 2;
8956
8957 /**
8958 * Size value for use with {@link #setCustomSizePreset} to show this notification
8959 * with a medium size.
8960 * <p>This value is only applicable for custom display notifications created using
8961 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008962 *
8963 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008964 */
Gus Prevasd7363752018-09-18 14:35:15 -04008965 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008966 public static final int SIZE_MEDIUM = 3;
8967
8968 /**
8969 * Size value for use with {@link #setCustomSizePreset} to show this notification
8970 * with a large size.
8971 * <p>This value is only applicable for custom display notifications created using
8972 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008973 *
8974 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008975 */
Gus Prevasd7363752018-09-18 14:35:15 -04008976 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008977 public static final int SIZE_LARGE = 4;
8978
Griff Hazend5f11f92014-05-27 15:40:09 -07008979 /**
8980 * Size value for use with {@link #setCustomSizePreset} to show this notification
8981 * full screen.
8982 * <p>This value is only applicable for custom display notifications created using
8983 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008984 *
8985 * @deprecated Display intents are no longer supported.
Griff Hazend5f11f92014-05-27 15:40:09 -07008986 */
Gus Prevasd7363752018-09-18 14:35:15 -04008987 @Deprecated
Griff Hazend5f11f92014-05-27 15:40:09 -07008988 public static final int SIZE_FULL_SCREEN = 5;
8989
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008990 /**
8991 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
8992 * short amount of time when this notification is displayed on the screen. This
8993 * is the default value.
Gus Prevasd7363752018-09-18 14:35:15 -04008994 *
8995 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008996 */
Gus Prevasd7363752018-09-18 14:35:15 -04008997 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008998 public static final int SCREEN_TIMEOUT_SHORT = 0;
8999
9000 /**
9001 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
9002 * for a longer amount of time when this notification is displayed on the screen.
Gus Prevasd7363752018-09-18 14:35:15 -04009003 *
9004 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009005 */
Gus Prevasd7363752018-09-18 14:35:15 -04009006 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009007 public static final int SCREEN_TIMEOUT_LONG = -1;
9008
Griff Hazen61a9e862014-05-22 16:05:19 -07009009 /** Notification extra which contains wearable extensions */
9010 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
9011
Pete Gastaf6781d2014-10-07 15:17:05 -04009012 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07009013 private static final String KEY_ACTIONS = "actions";
9014 private static final String KEY_FLAGS = "flags";
9015 private static final String KEY_DISPLAY_INTENT = "displayIntent";
9016 private static final String KEY_PAGES = "pages";
9017 private static final String KEY_BACKGROUND = "background";
9018 private static final String KEY_CONTENT_ICON = "contentIcon";
9019 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
9020 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
9021 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
9022 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
9023 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009024 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04009025 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009026 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07009027
9028 // Flags bitwise-ored to mFlags
9029 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
9030 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
9031 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
9032 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009033 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04009034 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04009035 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07009036
9037 // Default value for flags integer
9038 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
9039
9040 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
9041 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
9042
9043 private ArrayList<Action> mActions = new ArrayList<Action>();
9044 private int mFlags = DEFAULT_FLAGS;
9045 private PendingIntent mDisplayIntent;
9046 private ArrayList<Notification> mPages = new ArrayList<Notification>();
9047 private Bitmap mBackground;
9048 private int mContentIcon;
9049 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
9050 private int mContentActionIndex = UNSET_ACTION_INDEX;
9051 private int mCustomSizePreset = SIZE_DEFAULT;
9052 private int mCustomContentHeight;
9053 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009054 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04009055 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009056 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07009057
9058 /**
9059 * Create a {@link android.app.Notification.WearableExtender} with default
9060 * options.
9061 */
9062 public WearableExtender() {
9063 }
9064
9065 public WearableExtender(Notification notif) {
9066 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
9067 if (wearableBundle != null) {
9068 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
9069 if (actions != null) {
9070 mActions.addAll(actions);
9071 }
9072
9073 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
9074 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
9075
9076 Notification[] pages = getNotificationArrayFromBundle(
9077 wearableBundle, KEY_PAGES);
9078 if (pages != null) {
9079 Collections.addAll(mPages, pages);
9080 }
9081
9082 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
9083 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
9084 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
9085 DEFAULT_CONTENT_ICON_GRAVITY);
9086 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
9087 UNSET_ACTION_INDEX);
9088 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
9089 SIZE_DEFAULT);
9090 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
9091 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009092 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04009093 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009094 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07009095 }
9096 }
9097
9098 /**
9099 * Apply wearable extensions to a notification that is being built. This is typically
9100 * called by the {@link android.app.Notification.Builder#extend} method of
9101 * {@link android.app.Notification.Builder}.
9102 */
9103 @Override
9104 public Notification.Builder extend(Notification.Builder builder) {
9105 Bundle wearableBundle = new Bundle();
9106
9107 if (!mActions.isEmpty()) {
9108 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
9109 }
9110 if (mFlags != DEFAULT_FLAGS) {
9111 wearableBundle.putInt(KEY_FLAGS, mFlags);
9112 }
9113 if (mDisplayIntent != null) {
9114 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
9115 }
9116 if (!mPages.isEmpty()) {
9117 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
9118 new Notification[mPages.size()]));
9119 }
9120 if (mBackground != null) {
9121 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
9122 }
9123 if (mContentIcon != 0) {
9124 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
9125 }
9126 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
9127 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
9128 }
9129 if (mContentActionIndex != UNSET_ACTION_INDEX) {
9130 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
9131 mContentActionIndex);
9132 }
9133 if (mCustomSizePreset != SIZE_DEFAULT) {
9134 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
9135 }
9136 if (mCustomContentHeight != 0) {
9137 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
9138 }
9139 if (mGravity != DEFAULT_GRAVITY) {
9140 wearableBundle.putInt(KEY_GRAVITY, mGravity);
9141 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009142 if (mHintScreenTimeout != 0) {
9143 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
9144 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04009145 if (mDismissalId != null) {
9146 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
9147 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009148 if (mBridgeTag != null) {
9149 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
9150 }
Griff Hazen61a9e862014-05-22 16:05:19 -07009151
9152 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
9153 return builder;
9154 }
9155
9156 @Override
9157 public WearableExtender clone() {
9158 WearableExtender that = new WearableExtender();
9159 that.mActions = new ArrayList<Action>(this.mActions);
9160 that.mFlags = this.mFlags;
9161 that.mDisplayIntent = this.mDisplayIntent;
9162 that.mPages = new ArrayList<Notification>(this.mPages);
9163 that.mBackground = this.mBackground;
9164 that.mContentIcon = this.mContentIcon;
9165 that.mContentIconGravity = this.mContentIconGravity;
9166 that.mContentActionIndex = this.mContentActionIndex;
9167 that.mCustomSizePreset = this.mCustomSizePreset;
9168 that.mCustomContentHeight = this.mCustomContentHeight;
9169 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009170 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04009171 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009172 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07009173 return that;
9174 }
9175
9176 /**
9177 * Add a wearable action to this notification.
9178 *
9179 * <p>When wearable actions are added using this method, the set of actions that
9180 * show on a wearable device splits from devices that only show actions added
9181 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
9182 * of which actions display on different devices.
9183 *
9184 * @param action the action to add to this notification
9185 * @return this object for method chaining
9186 * @see android.app.Notification.Action
9187 */
9188 public WearableExtender addAction(Action action) {
9189 mActions.add(action);
9190 return this;
9191 }
9192
9193 /**
9194 * Adds wearable actions to this notification.
9195 *
9196 * <p>When wearable actions are added using this method, the set of actions that
9197 * show on a wearable device splits from devices that only show actions added
9198 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
9199 * of which actions display on different devices.
9200 *
9201 * @param actions the actions to add to this notification
9202 * @return this object for method chaining
9203 * @see android.app.Notification.Action
9204 */
9205 public WearableExtender addActions(List<Action> actions) {
9206 mActions.addAll(actions);
9207 return this;
9208 }
9209
9210 /**
9211 * Clear all wearable actions present on this builder.
9212 * @return this object for method chaining.
9213 * @see #addAction
9214 */
9215 public WearableExtender clearActions() {
9216 mActions.clear();
9217 return this;
9218 }
9219
9220 /**
9221 * Get the wearable actions present on this notification.
9222 */
9223 public List<Action> getActions() {
9224 return mActions;
9225 }
9226
9227 /**
9228 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07009229 * this notification. The {@link PendingIntent} provided should be for an activity.
9230 *
9231 * <pre class="prettyprint">
9232 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
9233 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
9234 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
9235 * Notification notif = new Notification.Builder(context)
9236 * .extend(new Notification.WearableExtender()
9237 * .setDisplayIntent(displayPendingIntent)
9238 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
9239 * .build();</pre>
9240 *
9241 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07009242 * should have an empty task affinity. It is also recommended to use the device
9243 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07009244 *
9245 * <p>Example AndroidManifest.xml entry:
9246 * <pre class="prettyprint">
9247 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
9248 * android:exported=&quot;true&quot;
9249 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07009250 * android:taskAffinity=&quot;&quot;
9251 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07009252 *
9253 * @param intent the {@link PendingIntent} for an activity
9254 * @return this object for method chaining
9255 * @see android.app.Notification.WearableExtender#getDisplayIntent
Gus Prevasd7363752018-09-18 14:35:15 -04009256 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009257 */
Gus Prevasd7363752018-09-18 14:35:15 -04009258 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009259 public WearableExtender setDisplayIntent(PendingIntent intent) {
9260 mDisplayIntent = intent;
9261 return this;
9262 }
9263
9264 /**
9265 * Get the intent to launch inside of an activity view when displaying this
9266 * notification. This {@code PendingIntent} should be for an activity.
Gus Prevasd7363752018-09-18 14:35:15 -04009267 *
9268 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009269 */
Gus Prevasd7363752018-09-18 14:35:15 -04009270 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009271 public PendingIntent getDisplayIntent() {
9272 return mDisplayIntent;
9273 }
9274
9275 /**
9276 * Add an additional page of content to display with this notification. The current
9277 * notification forms the first page, and pages added using this function form
9278 * subsequent pages. This field can be used to separate a notification into multiple
9279 * sections.
9280 *
9281 * @param page the notification to add as another page
9282 * @return this object for method chaining
9283 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04009284 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009285 */
Gus Prevasd7363752018-09-18 14:35:15 -04009286 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009287 public WearableExtender addPage(Notification page) {
9288 mPages.add(page);
9289 return this;
9290 }
9291
9292 /**
9293 * Add additional pages of content to display with this notification. The current
9294 * notification forms the first page, and pages added using this function form
9295 * subsequent pages. This field can be used to separate a notification into multiple
9296 * sections.
9297 *
9298 * @param pages a list of notifications
9299 * @return this object for method chaining
9300 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04009301 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009302 */
Gus Prevasd7363752018-09-18 14:35:15 -04009303 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009304 public WearableExtender addPages(List<Notification> pages) {
9305 mPages.addAll(pages);
9306 return this;
9307 }
9308
9309 /**
9310 * Clear all additional pages present on this builder.
9311 * @return this object for method chaining.
9312 * @see #addPage
Gus Prevasd7363752018-09-18 14:35:15 -04009313 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009314 */
Gus Prevasd7363752018-09-18 14:35:15 -04009315 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009316 public WearableExtender clearPages() {
9317 mPages.clear();
9318 return this;
9319 }
9320
9321 /**
9322 * Get the array of additional pages of content for displaying this notification. The
9323 * current notification forms the first page, and elements within this array form
9324 * subsequent pages. This field can be used to separate a notification into multiple
9325 * sections.
9326 * @return the pages for this notification
Gus Prevasd7363752018-09-18 14:35:15 -04009327 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009328 */
Gus Prevasd7363752018-09-18 14:35:15 -04009329 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009330 public List<Notification> getPages() {
9331 return mPages;
9332 }
9333
9334 /**
9335 * Set a background image to be displayed behind the notification content.
9336 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
9337 * will work with any notification style.
9338 *
9339 * @param background the background bitmap
9340 * @return this object for method chaining
9341 * @see android.app.Notification.WearableExtender#getBackground
Gus Prevasd7363752018-09-18 14:35:15 -04009342 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009343 */
Gus Prevasd7363752018-09-18 14:35:15 -04009344 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009345 public WearableExtender setBackground(Bitmap background) {
9346 mBackground = background;
9347 return this;
9348 }
9349
9350 /**
9351 * Get a background image to be displayed behind the notification content.
9352 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
9353 * will work with any notification style.
9354 *
9355 * @return the background image
9356 * @see android.app.Notification.WearableExtender#setBackground
Gus Prevasd7363752018-09-18 14:35:15 -04009357 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009358 */
Gus Prevasd7363752018-09-18 14:35:15 -04009359 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009360 public Bitmap getBackground() {
9361 return mBackground;
9362 }
9363
9364 /**
9365 * Set an icon that goes with the content of this notification.
9366 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009367 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009368 public WearableExtender setContentIcon(int icon) {
9369 mContentIcon = icon;
9370 return this;
9371 }
9372
9373 /**
9374 * Get an icon that goes with the content of this notification.
9375 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009376 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009377 public int getContentIcon() {
9378 return mContentIcon;
9379 }
9380
9381 /**
9382 * Set the gravity that the content icon should have within the notification display.
9383 * Supported values include {@link android.view.Gravity#START} and
9384 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
9385 * @see #setContentIcon
9386 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009387 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009388 public WearableExtender setContentIconGravity(int contentIconGravity) {
9389 mContentIconGravity = contentIconGravity;
9390 return this;
9391 }
9392
9393 /**
9394 * Get the gravity that the content icon should have within the notification display.
9395 * Supported values include {@link android.view.Gravity#START} and
9396 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
9397 * @see #getContentIcon
9398 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009399 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009400 public int getContentIconGravity() {
9401 return mContentIconGravity;
9402 }
9403
9404 /**
Gus Prevasd7363752018-09-18 14:35:15 -04009405 * Set an action from this notification's actions as the primary action. If the action has a
9406 * {@link RemoteInput} associated with it, shortcuts to the options for that input are shown
9407 * directly on the notification.
Griff Hazen14f57992014-05-26 09:07:14 -07009408 *
Gus Prevasd7363752018-09-18 14:35:15 -04009409 * @param actionIndex The index of the primary action.
Griff Hazen14f57992014-05-26 09:07:14 -07009410 * If wearable actions were added to the main notification, this index
9411 * will apply to that list, otherwise it will apply to the regular
9412 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07009413 */
9414 public WearableExtender setContentAction(int actionIndex) {
9415 mContentActionIndex = actionIndex;
9416 return this;
9417 }
9418
9419 /**
Gus Prevasd7363752018-09-18 14:35:15 -04009420 * Get the index of the notification action, if any, that was specified as the primary
9421 * action.
Griff Hazen14f57992014-05-26 09:07:14 -07009422 *
9423 * <p>If wearable specific actions were added to the main notification, this index will
9424 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07009425 *
9426 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07009427 */
9428 public int getContentAction() {
9429 return mContentActionIndex;
9430 }
9431
9432 /**
9433 * Set the gravity that this notification should have within the available viewport space.
9434 * Supported values include {@link android.view.Gravity#TOP},
9435 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
9436 * The default value is {@link android.view.Gravity#BOTTOM}.
9437 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009438 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009439 public WearableExtender setGravity(int gravity) {
9440 mGravity = gravity;
9441 return this;
9442 }
9443
9444 /**
9445 * Get the gravity that this notification should have within the available viewport space.
9446 * Supported values include {@link android.view.Gravity#TOP},
9447 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
9448 * The default value is {@link android.view.Gravity#BOTTOM}.
9449 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009450 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009451 public int getGravity() {
9452 return mGravity;
9453 }
9454
9455 /**
9456 * Set the custom size preset for the display of this notification out of the available
9457 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
9458 * {@link #SIZE_LARGE}.
9459 * <p>Some custom size presets are only applicable for custom display notifications created
9460 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
9461 * documentation for the preset in question. See also
9462 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
9463 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009464 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009465 public WearableExtender setCustomSizePreset(int sizePreset) {
9466 mCustomSizePreset = sizePreset;
9467 return this;
9468 }
9469
9470 /**
9471 * Get the custom size preset for the display of this notification out of the available
9472 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
9473 * {@link #SIZE_LARGE}.
9474 * <p>Some custom size presets are only applicable for custom display notifications created
9475 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
9476 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
9477 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009478 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009479 public int getCustomSizePreset() {
9480 return mCustomSizePreset;
9481 }
9482
9483 /**
9484 * Set the custom height in pixels for the display of this notification's content.
9485 * <p>This option is only available for custom display notifications created
9486 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
9487 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
9488 * {@link #getCustomContentHeight}.
9489 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009490 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009491 public WearableExtender setCustomContentHeight(int height) {
9492 mCustomContentHeight = height;
9493 return this;
9494 }
9495
9496 /**
9497 * Get the custom height in pixels for the display of this notification's content.
9498 * <p>This option is only available for custom display notifications created
9499 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
9500 * {@link #setCustomContentHeight}.
9501 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009502 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009503 public int getCustomContentHeight() {
9504 return mCustomContentHeight;
9505 }
9506
9507 /**
9508 * Set whether the scrolling position for the contents of this notification should start
9509 * at the bottom of the contents instead of the top when the contents are too long to
9510 * display within the screen. Default is false (start scroll at the top).
9511 */
9512 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
9513 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
9514 return this;
9515 }
9516
9517 /**
9518 * Get whether the scrolling position for the contents of this notification should start
9519 * at the bottom of the contents instead of the top when the contents are too long to
9520 * display within the screen. Default is false (start scroll at the top).
9521 */
9522 public boolean getStartScrollBottom() {
9523 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
9524 }
9525
9526 /**
9527 * Set whether the content intent is available when the wearable device is not connected
9528 * to a companion device. The user can still trigger this intent when the wearable device
9529 * is offline, but a visual hint will indicate that the content intent may not be available.
9530 * Defaults to true.
9531 */
9532 public WearableExtender setContentIntentAvailableOffline(
9533 boolean contentIntentAvailableOffline) {
9534 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
9535 return this;
9536 }
9537
9538 /**
9539 * Get whether the content intent is available when the wearable device is not connected
9540 * to a companion device. The user can still trigger this intent when the wearable device
9541 * is offline, but a visual hint will indicate that the content intent may not be available.
9542 * Defaults to true.
9543 */
9544 public boolean getContentIntentAvailableOffline() {
9545 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
9546 }
9547
9548 /**
9549 * Set a hint that this notification's icon should not be displayed.
9550 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
9551 * @return this object for method chaining
9552 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009553 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009554 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
9555 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
9556 return this;
9557 }
9558
9559 /**
9560 * Get a hint that this notification's icon should not be displayed.
9561 * @return {@code true} if this icon should not be displayed, false otherwise.
9562 * The default value is {@code false} if this was never set.
9563 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009564 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009565 public boolean getHintHideIcon() {
9566 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
9567 }
9568
9569 /**
9570 * Set a visual hint that only the background image of this notification should be
9571 * displayed, and other semantic content should be hidden. This hint is only applicable
9572 * to sub-pages added using {@link #addPage}.
9573 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009574 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009575 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
9576 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
9577 return this;
9578 }
9579
9580 /**
9581 * Get a visual hint that only the background image of this notification should be
9582 * displayed, and other semantic content should be hidden. This hint is only applicable
9583 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
9584 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009585 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009586 public boolean getHintShowBackgroundOnly() {
9587 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
9588 }
9589
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009590 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009591 * Set a hint that this notification's background should not be clipped if possible,
9592 * and should instead be resized to fully display on the screen, retaining the aspect
9593 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009594 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
9595 * @return this object for method chaining
9596 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009597 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009598 public WearableExtender setHintAvoidBackgroundClipping(
9599 boolean hintAvoidBackgroundClipping) {
9600 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
9601 return this;
9602 }
9603
9604 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009605 * Get a hint that this notification's background should not be clipped if possible,
9606 * and should instead be resized to fully display on the screen, retaining the aspect
9607 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009608 * @return {@code true} if it's ok if the background is clipped on the screen, false
9609 * otherwise. The default value is {@code false} if this was never set.
9610 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009611 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009612 public boolean getHintAvoidBackgroundClipping() {
9613 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
9614 }
9615
9616 /**
9617 * Set a hint that the screen should remain on for at least this duration when
9618 * this notification is displayed on the screen.
9619 * @param timeout The requested screen timeout in milliseconds. Can also be either
9620 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9621 * @return this object for method chaining
9622 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009623 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009624 public WearableExtender setHintScreenTimeout(int timeout) {
9625 mHintScreenTimeout = timeout;
9626 return this;
9627 }
9628
9629 /**
9630 * Get the duration, in milliseconds, that the screen should remain on for
9631 * when this notification is displayed.
9632 * @return the duration in milliseconds if > 0, or either one of the sentinel values
9633 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9634 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009635 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009636 public int getHintScreenTimeout() {
9637 return mHintScreenTimeout;
9638 }
9639
Alex Hills9ab3a232016-04-05 14:54:56 -04009640 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04009641 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
9642 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9643 * qr codes, as well as other simple black-and-white tickets.
9644 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
9645 * @return this object for method chaining
Gus Prevasd7363752018-09-18 14:35:15 -04009646 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009647 */
Gus Prevasd7363752018-09-18 14:35:15 -04009648 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009649 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
9650 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
9651 return this;
9652 }
9653
9654 /**
9655 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
9656 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9657 * qr codes, as well as other simple black-and-white tickets.
9658 * @return {@code true} if it should be displayed in ambient, false otherwise
9659 * otherwise. The default value is {@code false} if this was never set.
Gus Prevasd7363752018-09-18 14:35:15 -04009660 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009661 */
Gus Prevasd7363752018-09-18 14:35:15 -04009662 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009663 public boolean getHintAmbientBigPicture() {
9664 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
9665 }
9666
9667 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04009668 * Set a hint that this notification's content intent will launch an {@link Activity}
9669 * directly, telling the platform that it can generate the appropriate transitions.
9670 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
9671 * an activity and transitions should be generated, false otherwise.
9672 * @return this object for method chaining
9673 */
9674 public WearableExtender setHintContentIntentLaunchesActivity(
9675 boolean hintContentIntentLaunchesActivity) {
9676 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
9677 return this;
9678 }
9679
9680 /**
9681 * Get a hint that this notification's content intent will launch an {@link Activity}
9682 * directly, telling the platform that it can generate the appropriate transitions
9683 * @return {@code true} if the content intent will launch an activity and transitions should
9684 * be generated, false otherwise. The default value is {@code false} if this was never set.
9685 */
9686 public boolean getHintContentIntentLaunchesActivity() {
9687 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
9688 }
9689
Nadia Benbernou948627e2016-04-14 14:41:08 -04009690 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009691 * Sets the dismissal id for this notification. If a notification is posted with a
9692 * dismissal id, then when that notification is canceled, notifications on other wearables
9693 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04009694 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009695 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04009696 * @param dismissalId the dismissal id of the notification.
9697 * @return this object for method chaining
9698 */
9699 public WearableExtender setDismissalId(String dismissalId) {
9700 mDismissalId = dismissalId;
9701 return this;
9702 }
9703
9704 /**
9705 * Returns the dismissal id of the notification.
9706 * @return the dismissal id of the notification or null if it has not been set.
9707 */
9708 public String getDismissalId() {
9709 return mDismissalId;
9710 }
9711
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009712 /**
9713 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
9714 * posted from a phone to provide finer-grained control on what notifications are bridged
9715 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
9716 * Features to Notifications</a> for more information.
9717 * @param bridgeTag the bridge tag of the notification.
9718 * @return this object for method chaining
9719 */
9720 public WearableExtender setBridgeTag(String bridgeTag) {
9721 mBridgeTag = bridgeTag;
9722 return this;
9723 }
9724
9725 /**
9726 * Returns the bridge tag of the notification.
9727 * @return the bridge tag or null if not present.
9728 */
9729 public String getBridgeTag() {
9730 return mBridgeTag;
9731 }
9732
Griff Hazen61a9e862014-05-22 16:05:19 -07009733 private void setFlag(int mask, boolean value) {
9734 if (value) {
9735 mFlags |= mask;
9736 } else {
9737 mFlags &= ~mask;
9738 }
9739 }
9740 }
9741
9742 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009743 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
9744 * with car extensions:
9745 *
9746 * <ol>
9747 * <li>Create an {@link Notification.Builder}, setting any desired
9748 * properties.
9749 * <li>Create a {@link CarExtender}.
9750 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
9751 * {@link CarExtender}.
9752 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9753 * to apply the extensions to a notification.
9754 * </ol>
9755 *
9756 * <pre class="prettyprint">
9757 * Notification notification = new Notification.Builder(context)
9758 * ...
9759 * .extend(new CarExtender()
9760 * .set*(...))
9761 * .build();
9762 * </pre>
9763 *
9764 * <p>Car extensions can be accessed on an existing notification by using the
9765 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
9766 * to access values.
9767 */
9768 public static final class CarExtender implements Extender {
9769 private static final String TAG = "CarExtender";
9770
9771 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
9772 private static final String EXTRA_LARGE_ICON = "large_icon";
9773 private static final String EXTRA_CONVERSATION = "car_conversation";
9774 private static final String EXTRA_COLOR = "app_color";
9775
9776 private Bitmap mLargeIcon;
9777 private UnreadConversation mUnreadConversation;
9778 private int mColor = Notification.COLOR_DEFAULT;
9779
9780 /**
9781 * Create a {@link CarExtender} with default options.
9782 */
9783 public CarExtender() {
9784 }
9785
9786 /**
9787 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
9788 *
9789 * @param notif The notification from which to copy options.
9790 */
9791 public CarExtender(Notification notif) {
9792 Bundle carBundle = notif.extras == null ?
9793 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
9794 if (carBundle != null) {
9795 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
9796 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
9797
9798 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
9799 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
9800 }
9801 }
9802
9803 /**
9804 * Apply car extensions to a notification that is being built. This is typically called by
9805 * the {@link Notification.Builder#extend(Notification.Extender)}
9806 * method of {@link Notification.Builder}.
9807 */
9808 @Override
9809 public Notification.Builder extend(Notification.Builder builder) {
9810 Bundle carExtensions = new Bundle();
9811
9812 if (mLargeIcon != null) {
9813 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
9814 }
9815 if (mColor != Notification.COLOR_DEFAULT) {
9816 carExtensions.putInt(EXTRA_COLOR, mColor);
9817 }
9818
9819 if (mUnreadConversation != null) {
9820 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
9821 carExtensions.putBundle(EXTRA_CONVERSATION, b);
9822 }
9823
9824 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
9825 return builder;
9826 }
9827
9828 /**
9829 * Sets the accent color to use when Android Auto presents the notification.
9830 *
9831 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
9832 * to accent the displayed notification. However, not all colors are acceptable in an
9833 * automotive setting. This method can be used to override the color provided in the
9834 * notification in such a situation.
9835 */
Tor Norbye80756e32015-03-02 09:39:27 -08009836 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009837 mColor = color;
9838 return this;
9839 }
9840
9841 /**
9842 * Gets the accent color.
9843 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04009844 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009845 */
Tor Norbye80756e32015-03-02 09:39:27 -08009846 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009847 public int getColor() {
9848 return mColor;
9849 }
9850
9851 /**
9852 * Sets the large icon of the car notification.
9853 *
9854 * If no large icon is set in the extender, Android Auto will display the icon
9855 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
9856 *
9857 * @param largeIcon The large icon to use in the car notification.
9858 * @return This object for method chaining.
9859 */
9860 public CarExtender setLargeIcon(Bitmap largeIcon) {
9861 mLargeIcon = largeIcon;
9862 return this;
9863 }
9864
9865 /**
9866 * Gets the large icon used in this car notification, or null if no icon has been set.
9867 *
9868 * @return The large icon for the car notification.
9869 * @see CarExtender#setLargeIcon
9870 */
9871 public Bitmap getLargeIcon() {
9872 return mLargeIcon;
9873 }
9874
9875 /**
9876 * Sets the unread conversation in a message notification.
9877 *
9878 * @param unreadConversation The unread part of the conversation this notification conveys.
9879 * @return This object for method chaining.
9880 */
9881 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
9882 mUnreadConversation = unreadConversation;
9883 return this;
9884 }
9885
9886 /**
9887 * Returns the unread conversation conveyed by this notification.
9888 * @see #setUnreadConversation(UnreadConversation)
9889 */
9890 public UnreadConversation getUnreadConversation() {
9891 return mUnreadConversation;
9892 }
9893
9894 /**
9895 * A class which holds the unread messages from a conversation.
9896 */
9897 public static class UnreadConversation {
9898 private static final String KEY_AUTHOR = "author";
9899 private static final String KEY_TEXT = "text";
9900 private static final String KEY_MESSAGES = "messages";
9901 private static final String KEY_REMOTE_INPUT = "remote_input";
9902 private static final String KEY_ON_REPLY = "on_reply";
9903 private static final String KEY_ON_READ = "on_read";
9904 private static final String KEY_PARTICIPANTS = "participants";
9905 private static final String KEY_TIMESTAMP = "timestamp";
9906
9907 private final String[] mMessages;
9908 private final RemoteInput mRemoteInput;
9909 private final PendingIntent mReplyPendingIntent;
9910 private final PendingIntent mReadPendingIntent;
9911 private final String[] mParticipants;
9912 private final long mLatestTimestamp;
9913
9914 UnreadConversation(String[] messages, RemoteInput remoteInput,
9915 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
9916 String[] participants, long latestTimestamp) {
9917 mMessages = messages;
9918 mRemoteInput = remoteInput;
9919 mReadPendingIntent = readPendingIntent;
9920 mReplyPendingIntent = replyPendingIntent;
9921 mParticipants = participants;
9922 mLatestTimestamp = latestTimestamp;
9923 }
9924
9925 /**
9926 * Gets the list of messages conveyed by this notification.
9927 */
9928 public String[] getMessages() {
9929 return mMessages;
9930 }
9931
9932 /**
9933 * Gets the remote input that will be used to convey the response to a message list, or
9934 * null if no such remote input exists.
9935 */
9936 public RemoteInput getRemoteInput() {
9937 return mRemoteInput;
9938 }
9939
9940 /**
9941 * Gets the pending intent that will be triggered when the user replies to this
9942 * notification.
9943 */
9944 public PendingIntent getReplyPendingIntent() {
9945 return mReplyPendingIntent;
9946 }
9947
9948 /**
9949 * Gets the pending intent that Android Auto will send after it reads aloud all messages
9950 * in this object's message list.
9951 */
9952 public PendingIntent getReadPendingIntent() {
9953 return mReadPendingIntent;
9954 }
9955
9956 /**
9957 * Gets the participants in the conversation.
9958 */
9959 public String[] getParticipants() {
9960 return mParticipants;
9961 }
9962
9963 /**
9964 * Gets the firs participant in the conversation.
9965 */
9966 public String getParticipant() {
9967 return mParticipants.length > 0 ? mParticipants[0] : null;
9968 }
9969
9970 /**
9971 * Gets the timestamp of the conversation.
9972 */
9973 public long getLatestTimestamp() {
9974 return mLatestTimestamp;
9975 }
9976
9977 Bundle getBundleForUnreadConversation() {
9978 Bundle b = new Bundle();
9979 String author = null;
9980 if (mParticipants != null && mParticipants.length > 1) {
9981 author = mParticipants[0];
9982 }
9983 Parcelable[] messages = new Parcelable[mMessages.length];
9984 for (int i = 0; i < messages.length; i++) {
9985 Bundle m = new Bundle();
9986 m.putString(KEY_TEXT, mMessages[i]);
9987 m.putString(KEY_AUTHOR, author);
9988 messages[i] = m;
9989 }
9990 b.putParcelableArray(KEY_MESSAGES, messages);
9991 if (mRemoteInput != null) {
9992 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
9993 }
9994 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
9995 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
9996 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
9997 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
9998 return b;
9999 }
10000
10001 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
10002 if (b == null) {
10003 return null;
10004 }
10005 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
10006 String[] messages = null;
10007 if (parcelableMessages != null) {
10008 String[] tmp = new String[parcelableMessages.length];
10009 boolean success = true;
10010 for (int i = 0; i < tmp.length; i++) {
10011 if (!(parcelableMessages[i] instanceof Bundle)) {
10012 success = false;
10013 break;
10014 }
10015 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
10016 if (tmp[i] == null) {
10017 success = false;
10018 break;
10019 }
10020 }
10021 if (success) {
10022 messages = tmp;
10023 } else {
10024 return null;
10025 }
10026 }
10027
10028 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
10029 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
10030
10031 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
10032
10033 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
10034 if (participants == null || participants.length != 1) {
10035 return null;
10036 }
10037
10038 return new UnreadConversation(messages,
10039 remoteInput,
10040 onReply,
10041 onRead,
10042 participants, b.getLong(KEY_TIMESTAMP));
10043 }
10044 };
10045
10046 /**
10047 * Builder class for {@link CarExtender.UnreadConversation} objects.
10048 */
10049 public static class Builder {
10050 private final List<String> mMessages = new ArrayList<String>();
10051 private final String mParticipant;
10052 private RemoteInput mRemoteInput;
10053 private PendingIntent mReadPendingIntent;
10054 private PendingIntent mReplyPendingIntent;
10055 private long mLatestTimestamp;
10056
10057 /**
10058 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
10059 *
10060 * @param name The name of the other participant in the conversation.
10061 */
10062 public Builder(String name) {
10063 mParticipant = name;
10064 }
10065
10066 /**
10067 * Appends a new unread message to the list of messages for this conversation.
10068 *
10069 * The messages should be added from oldest to newest.
10070 *
10071 * @param message The text of the new unread message.
10072 * @return This object for method chaining.
10073 */
10074 public Builder addMessage(String message) {
10075 mMessages.add(message);
10076 return this;
10077 }
10078
10079 /**
10080 * Sets the pending intent and remote input which will convey the reply to this
10081 * notification.
10082 *
10083 * @param pendingIntent The pending intent which will be triggered on a reply.
10084 * @param remoteInput The remote input parcelable which will carry the reply.
10085 * @return This object for method chaining.
10086 *
10087 * @see CarExtender.UnreadConversation#getRemoteInput
10088 * @see CarExtender.UnreadConversation#getReplyPendingIntent
10089 */
10090 public Builder setReplyAction(
10091 PendingIntent pendingIntent, RemoteInput remoteInput) {
10092 mRemoteInput = remoteInput;
10093 mReplyPendingIntent = pendingIntent;
10094
10095 return this;
10096 }
10097
10098 /**
10099 * Sets the pending intent that will be sent once the messages in this notification
10100 * are read.
10101 *
10102 * @param pendingIntent The pending intent to use.
10103 * @return This object for method chaining.
10104 */
10105 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
10106 mReadPendingIntent = pendingIntent;
10107 return this;
10108 }
10109
10110 /**
10111 * Sets the timestamp of the most recent message in an unread conversation.
10112 *
10113 * If a messaging notification has been posted by your application and has not
10114 * yet been cancelled, posting a later notification with the same id and tag
10115 * but without a newer timestamp may result in Android Auto not displaying a
10116 * heads up notification for the later notification.
10117 *
10118 * @param timestamp The timestamp of the most recent message in the conversation.
10119 * @return This object for method chaining.
10120 */
10121 public Builder setLatestTimestamp(long timestamp) {
10122 mLatestTimestamp = timestamp;
10123 return this;
10124 }
10125
10126 /**
10127 * Builds a new unread conversation object.
10128 *
10129 * @return The new unread conversation object.
10130 */
10131 public UnreadConversation build() {
10132 String[] messages = mMessages.toArray(new String[mMessages.size()]);
10133 String[] participants = { mParticipant };
10134 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
10135 mReadPendingIntent, participants, mLatestTimestamp);
10136 }
10137 }
10138 }
10139
10140 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010141 * <p>Helper class to add Android TV extensions to notifications. To create a notification
10142 * with a TV extension:
10143 *
10144 * <ol>
10145 * <li>Create an {@link Notification.Builder}, setting any desired properties.
10146 * <li>Create a {@link TvExtender}.
10147 * <li>Set TV-specific properties using the {@code set} methods of
10148 * {@link TvExtender}.
10149 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
10150 * to apply the extension to a notification.
10151 * </ol>
10152 *
10153 * <pre class="prettyprint">
10154 * Notification notification = new Notification.Builder(context)
10155 * ...
10156 * .extend(new TvExtender()
10157 * .set*(...))
10158 * .build();
10159 * </pre>
10160 *
10161 * <p>TV extensions can be accessed on an existing notification by using the
10162 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
10163 * to access values.
10164 *
10165 * @hide
10166 */
10167 @SystemApi
10168 public static final class TvExtender implements Extender {
10169 private static final String TAG = "TvExtender";
10170
10171 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
10172 private static final String EXTRA_FLAGS = "flags";
10173 private static final String EXTRA_CONTENT_INTENT = "content_intent";
10174 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010175 private static final String EXTRA_CHANNEL_ID = "channel_id";
Rhiannon Malia1a083932018-01-24 15:02:30 -080010176 private static final String EXTRA_SUPPRESS_SHOW_OVER_APPS = "suppressShowOverApps";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010177
10178 // Flags bitwise-ored to mFlags
10179 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
10180
10181 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010182 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010183 private PendingIntent mContentIntent;
10184 private PendingIntent mDeleteIntent;
Rhiannon Malia1a083932018-01-24 15:02:30 -080010185 private boolean mSuppressShowOverApps;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010186
10187 /**
10188 * Create a {@link TvExtender} with default options.
10189 */
10190 public TvExtender() {
10191 mFlags = FLAG_AVAILABLE_ON_TV;
10192 }
10193
10194 /**
10195 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
10196 *
10197 * @param notif The notification from which to copy options.
10198 */
10199 public TvExtender(Notification notif) {
10200 Bundle bundle = notif.extras == null ?
10201 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
10202 if (bundle != null) {
10203 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010204 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Rhiannon Malia1a083932018-01-24 15:02:30 -080010205 mSuppressShowOverApps = bundle.getBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010206 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
10207 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
10208 }
10209 }
10210
10211 /**
10212 * Apply a TV extension to a notification that is being built. This is typically called by
10213 * the {@link Notification.Builder#extend(Notification.Extender)}
10214 * method of {@link Notification.Builder}.
10215 */
10216 @Override
10217 public Notification.Builder extend(Notification.Builder builder) {
10218 Bundle bundle = new Bundle();
10219
10220 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010221 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Rhiannon Malia1a083932018-01-24 15:02:30 -080010222 bundle.putBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS, mSuppressShowOverApps);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010223 if (mContentIntent != null) {
10224 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
10225 }
10226
10227 if (mDeleteIntent != null) {
10228 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
10229 }
10230
10231 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
10232 return builder;
10233 }
10234
10235 /**
10236 * Returns true if this notification should be shown on TV. This method return true
10237 * if the notification was extended with a TvExtender.
10238 */
10239 public boolean isAvailableOnTv() {
10240 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
10241 }
10242
10243 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010244 * Specifies the channel the notification should be delivered on when shown on TV.
10245 * It can be different from the channel that the notification is delivered to when
10246 * posting on a non-TV device.
10247 */
10248 public TvExtender setChannel(String channelId) {
10249 mChannelId = channelId;
10250 return this;
10251 }
10252
10253 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -040010254 * Specifies the channel the notification should be delivered on when shown on TV.
10255 * It can be different from the channel that the notification is delivered to when
10256 * posting on a non-TV device.
10257 */
10258 public TvExtender setChannelId(String channelId) {
10259 mChannelId = channelId;
10260 return this;
10261 }
10262
Jeff Sharkey000ce802017-04-29 13:13:27 -060010263 /** @removed */
10264 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010265 public String getChannel() {
10266 return mChannelId;
10267 }
10268
10269 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -040010270 * Returns the id of the channel this notification posts to on TV.
10271 */
10272 public String getChannelId() {
10273 return mChannelId;
10274 }
10275
10276 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010277 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
10278 * If provided, it is used instead of the content intent specified
10279 * at the level of Notification.
10280 */
10281 public TvExtender setContentIntent(PendingIntent intent) {
10282 mContentIntent = intent;
10283 return this;
10284 }
10285
10286 /**
10287 * Returns the TV-specific content intent. If this method returns null, the
10288 * main content intent on the notification should be used.
10289 *
10290 * @see {@link Notification#contentIntent}
10291 */
10292 public PendingIntent getContentIntent() {
10293 return mContentIntent;
10294 }
10295
10296 /**
10297 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
10298 * by the user on TV. If provided, it is used instead of the delete intent specified
10299 * at the level of Notification.
10300 */
10301 public TvExtender setDeleteIntent(PendingIntent intent) {
10302 mDeleteIntent = intent;
10303 return this;
10304 }
10305
10306 /**
10307 * Returns the TV-specific delete intent. If this method returns null, the
10308 * main delete intent on the notification should be used.
10309 *
10310 * @see {@link Notification#deleteIntent}
10311 */
10312 public PendingIntent getDeleteIntent() {
10313 return mDeleteIntent;
10314 }
Rhiannon Malia1a083932018-01-24 15:02:30 -080010315
10316 /**
10317 * Specifies whether this notification should suppress showing a message over top of apps
10318 * outside of the launcher.
10319 */
10320 public TvExtender setSuppressShowOverApps(boolean suppress) {
10321 mSuppressShowOverApps = suppress;
10322 return this;
10323 }
10324
10325 /**
10326 * Returns true if this notification should not show messages over top of apps
10327 * outside of the launcher.
10328 */
10329 public boolean getSuppressShowOverApps() {
10330 return mSuppressShowOverApps;
10331 }
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010332 }
10333
10334 /**
Griff Hazen61a9e862014-05-22 16:05:19 -070010335 * Get an array of Notification objects from a parcelable array bundle field.
10336 * Update the bundle to have a typed array so fetches in the future don't need
10337 * to do an array copy.
10338 */
10339 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
10340 Parcelable[] array = bundle.getParcelableArray(key);
10341 if (array instanceof Notification[] || array == null) {
10342 return (Notification[]) array;
10343 }
10344 Notification[] typedArray = Arrays.copyOf(array, array.length,
10345 Notification[].class);
10346 bundle.putParcelableArray(key, typedArray);
10347 return typedArray;
10348 }
Christoph Studer4600f9b2014-07-22 22:44:43 +020010349
10350 private static class BuilderRemoteViews extends RemoteViews {
10351 public BuilderRemoteViews(Parcel parcel) {
10352 super(parcel);
10353 }
10354
Kenny Guy77320062014-08-27 21:37:15 +010010355 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
10356 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +020010357 }
10358
10359 @Override
10360 public BuilderRemoteViews clone() {
10361 Parcel p = Parcel.obtain();
10362 writeToParcel(p, 0);
10363 p.setDataPosition(0);
10364 BuilderRemoteViews brv = new BuilderRemoteViews(p);
10365 p.recycle();
10366 return brv;
10367 }
10368 }
Adrian Roos70d7aa32017-01-11 15:39:06 -080010369
Selim Cinek384804b2018-04-18 14:31:07 +080010370 /**
10371 * A result object where information about the template that was created is saved.
10372 */
10373 private static class TemplateBindResult {
Selim Cinek1c72fa02018-04-23 18:00:54 +080010374 int mIconMarginEnd;
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010375 boolean mRightIconContainerVisible;
Selim Cinek384804b2018-04-18 14:31:07 +080010376
10377 /**
Selim Cinek1c72fa02018-04-23 18:00:54 +080010378 * Get the margin end that needs to be added to any fields that may overlap
Selim Cinek384804b2018-04-18 14:31:07 +080010379 * with the right actions.
10380 */
Selim Cinek1c72fa02018-04-23 18:00:54 +080010381 public int getIconMarginEnd() {
10382 return mIconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +080010383 }
10384
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010385 /**
10386 * Is the icon container visible on the right size because of the reply button or the
10387 * right icon.
10388 */
10389 public boolean isRightIconContainerVisible() {
10390 return mRightIconContainerVisible;
10391 }
10392
Selim Cinek1c72fa02018-04-23 18:00:54 +080010393 public void setIconMarginEnd(int iconMarginEnd) {
10394 this.mIconMarginEnd = iconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +080010395 }
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010396
10397 public void setRightIconContainerVisible(boolean iconContainerVisible) {
10398 mRightIconContainerVisible = iconContainerVisible;
10399 }
Selim Cinek384804b2018-04-18 14:31:07 +080010400 }
10401
Adrian Roos70d7aa32017-01-11 15:39:06 -080010402 private static class StandardTemplateParams {
10403 boolean hasProgress = true;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010404 CharSequence title;
10405 CharSequence text;
Selim Cinekafeed292017-12-12 17:32:44 -080010406 CharSequence headerTextSecondary;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010407 CharSequence summaryText;
Selim Cinekbee4e072018-05-21 22:06:43 -070010408 int maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek88188f22017-09-19 16:46:56 -070010409 boolean hideLargeIcon;
Selim Cinek384804b2018-04-18 14:31:07 +080010410 boolean hideReplyIcon;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010411 boolean allowColorization = true;
10412 boolean forceDefaultColor = false;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010413
10414 final StandardTemplateParams reset() {
10415 hasProgress = true;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010416 title = null;
10417 text = null;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010418 summaryText = null;
Selim Cinekafeed292017-12-12 17:32:44 -080010419 headerTextSecondary = null;
Selim Cinekbee4e072018-05-21 22:06:43 -070010420 maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010421 allowColorization = true;
10422 forceDefaultColor = false;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010423 return this;
10424 }
10425
10426 final StandardTemplateParams hasProgress(boolean hasProgress) {
10427 this.hasProgress = hasProgress;
10428 return this;
10429 }
10430
10431 final StandardTemplateParams title(CharSequence title) {
10432 this.title = title;
10433 return this;
10434 }
10435
10436 final StandardTemplateParams text(CharSequence text) {
10437 this.text = text;
10438 return this;
10439 }
10440
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010441 final StandardTemplateParams summaryText(CharSequence text) {
10442 this.summaryText = text;
10443 return this;
10444 }
10445
Selim Cinekafeed292017-12-12 17:32:44 -080010446 final StandardTemplateParams headerTextSecondary(CharSequence text) {
10447 this.headerTextSecondary = text;
10448 return this;
10449 }
10450
Selim Cinek384804b2018-04-18 14:31:07 +080010451 final StandardTemplateParams hideLargeIcon(boolean hideLargeIcon) {
10452 this.hideLargeIcon = hideLargeIcon;
Selim Cinek88188f22017-09-19 16:46:56 -070010453 return this;
10454 }
10455
Selim Cinek384804b2018-04-18 14:31:07 +080010456 final StandardTemplateParams hideReplyIcon(boolean hideReplyIcon) {
10457 this.hideReplyIcon = hideReplyIcon;
Selim Cinek88188f22017-09-19 16:46:56 -070010458 return this;
10459 }
10460
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010461 final StandardTemplateParams disallowColorization() {
10462 this.allowColorization = false;
10463 return this;
10464 }
10465
10466 final StandardTemplateParams forceDefaultColor() {
10467 this.forceDefaultColor = true;
10468 return this;
10469 }
10470
Adrian Roos70d7aa32017-01-11 15:39:06 -080010471 final StandardTemplateParams fillTextsFrom(Builder b) {
10472 Bundle extras = b.mN.extras;
Lucas Dupin00be88f2019-01-03 17:50:52 -080010473 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE));
Lucas Dupin06c5e642017-09-13 16:34:58 -070010474
Lucas Dupin06c5e642017-09-13 16:34:58 -070010475 CharSequence text = extras.getCharSequence(EXTRA_BIG_TEXT);
Lucas Dupin00be88f2019-01-03 17:50:52 -080010476 if (TextUtils.isEmpty(text)) {
Lucas Dupin06c5e642017-09-13 16:34:58 -070010477 text = extras.getCharSequence(EXTRA_TEXT);
10478 }
Lucas Dupin00be88f2019-01-03 17:50:52 -080010479 this.text = b.processLegacyText(text);
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010480 this.summaryText = extras.getCharSequence(EXTRA_SUB_TEXT);
Adrian Roos70d7aa32017-01-11 15:39:06 -080010481 return this;
10482 }
Selim Cinekbee4e072018-05-21 22:06:43 -070010483
10484 /**
10485 * Set the maximum lines of remote input history lines allowed.
10486 * @param maxRemoteInputHistory The number of lines.
10487 * @return The builder for method chaining.
10488 */
10489 public StandardTemplateParams setMaxRemoteInputHistory(int maxRemoteInputHistory) {
10490 this.maxRemoteInputHistory = maxRemoteInputHistory;
10491 return this;
10492 }
Adrian Roos70d7aa32017-01-11 15:39:06 -080010493 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010494}