blob: 47f0cf846c17d88c3cb08d14b53972dbd6cbf74d [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
cncn9d0fd9c2019-11-20 11:05:02 -0800631 /** @hide */
632 @IntDef({FLAG_SHOW_LIGHTS, FLAG_ONGOING_EVENT, FLAG_INSISTENT, FLAG_ONLY_ALERT_ONCE,
633 FLAG_AUTO_CANCEL, FLAG_NO_CLEAR, FLAG_FOREGROUND_SERVICE, FLAG_HIGH_PRIORITY,
634 FLAG_LOCAL_ONLY, FLAG_GROUP_SUMMARY, FLAG_AUTOGROUP_SUMMARY, FLAG_BUBBLE})
635 @Retention(RetentionPolicy.SOURCE)
636 public @interface NotificationFlags{};
637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 public int flags;
639
Tor Norbyed9273d62013-05-30 15:59:53 -0700640 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700641 @IntDef(prefix = { "PRIORITY_" }, value = {
642 PRIORITY_DEFAULT,
643 PRIORITY_LOW,
644 PRIORITY_MIN,
645 PRIORITY_HIGH,
646 PRIORITY_MAX
647 })
Tor Norbyed9273d62013-05-30 15:59:53 -0700648 @Retention(RetentionPolicy.SOURCE)
649 public @interface Priority {}
650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500652 * Default notification {@link #priority}. If your application does not prioritize its own
653 * notifications, use this value for all notifications.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500654 *
655 * @deprecated use {@link NotificationManager#IMPORTANCE_DEFAULT} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500656 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500657 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500658 public static final int PRIORITY_DEFAULT = 0;
659
660 /**
661 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
662 * items smaller, or at a different position in the list, compared with your app's
663 * {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500664 *
665 * @deprecated use {@link NotificationManager#IMPORTANCE_LOW} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500666 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500667 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500668 public static final int PRIORITY_LOW = -1;
669
670 /**
671 * Lowest {@link #priority}; these items might not be shown to the user except under special
672 * circumstances, such as detailed notification logs.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500673 *
674 * @deprecated use {@link NotificationManager#IMPORTANCE_MIN} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500675 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500676 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500677 public static final int PRIORITY_MIN = -2;
678
679 /**
680 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
681 * show these items larger, or at a different position in notification lists, compared with
682 * your app's {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500683 *
684 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500685 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500686 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500687 public static final int PRIORITY_HIGH = 1;
688
689 /**
690 * Highest {@link #priority}, for your application's most important items that require the
691 * user's prompt attention or input.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500692 *
693 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500694 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500695 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500696 public static final int PRIORITY_MAX = 2;
697
698 /**
699 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800700 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500701 * Priority is an indication of how much of the user's valuable attention should be consumed by
702 * this notification. Low-priority notifications may be hidden from the user in certain
703 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500704 * system will make a determination about how to interpret this priority when presenting
705 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400706 *
707 * <p>
708 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
709 * as a heads-up notification.
710 * </p>
711 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500712 * @deprecated use {@link NotificationChannel#getImportance()} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500713 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700714 @Priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500715 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500716 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800717
Dan Sandler26e81cf2014-05-06 10:01:27 -0400718 /**
719 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
720 * to be applied by the standard Style templates when presenting this notification.
721 *
722 * The current template design constructs a colorful header image by overlaying the
723 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
724 * ignored.
725 */
Tor Norbye80756e32015-03-02 09:39:27 -0800726 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400727 public int color = COLOR_DEFAULT;
728
729 /**
730 * Special value of {@link #color} telling the system not to decorate this notification with
731 * any special color but instead use default colors when presenting this notification.
732 */
Tor Norbye80756e32015-03-02 09:39:27 -0800733 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400734 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600735
736 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800737 * Special value of {@link #color} used as a place holder for an invalid color.
Selim Cinek99104832017-01-25 14:47:33 -0800738 * @hide
Adrian Roos4ff3b122016-02-01 12:26:13 -0800739 */
740 @ColorInt
Selim Cinek99104832017-01-25 14:47:33 -0800741 public static final int COLOR_INVALID = 1;
Adrian Roos4ff3b122016-02-01 12:26:13 -0800742
743 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700744 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
745 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600746 * lockscreen).
747 *
748 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
749 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
750 * shown in all situations, but the contents are only available if the device is unlocked for
751 * the appropriate user.
752 *
753 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
754 * can be read even in an "insecure" context (that is, above a secure lockscreen).
755 * To modify the public version of this notification—for example, to redact some portions—see
756 * {@link Builder#setPublicVersion(Notification)}.
757 *
758 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
759 * and ticker until the user has bypassed the lockscreen.
760 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600761 public @Visibility int visibility;
762
763 /** @hide */
764 @IntDef(prefix = { "VISIBILITY_" }, value = {
765 VISIBILITY_PUBLIC,
766 VISIBILITY_PRIVATE,
767 VISIBILITY_SECRET,
768 })
769 @Retention(RetentionPolicy.SOURCE)
770 public @interface Visibility {}
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600771
Griff Hazenfc3922d2014-08-20 11:56:44 -0700772 /**
773 * Notification visibility: Show this notification in its entirety on all lockscreens.
774 *
775 * {@see #visibility}
776 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600777 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700778
779 /**
780 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
781 * private information on secure lockscreens.
782 *
783 * {@see #visibility}
784 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600785 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700786
787 /**
788 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
789 *
790 * {@see #visibility}
791 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600792 public static final int VISIBILITY_SECRET = -1;
793
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500794 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400795 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500796 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400797 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500798
799 /**
Adora Zhangaa90e872018-03-12 14:07:50 -0700800 * Notification category: map turn-by-turn navigation.
801 */
802 public static final String CATEGORY_NAVIGATION = "navigation";
803
804 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400805 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500806 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400807 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500808
809 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400810 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500811 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400812 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500813
814 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400815 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500816 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400817 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500818
819 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400820 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500821 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400822 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500823
824 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400825 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500826 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400827 public static final String CATEGORY_ALARM = "alarm";
828
829 /**
830 * Notification category: progress of a long-running background operation.
831 */
832 public static final String CATEGORY_PROGRESS = "progress";
833
834 /**
835 * Notification category: social network or sharing update.
836 */
837 public static final String CATEGORY_SOCIAL = "social";
838
839 /**
840 * Notification category: error in background operation or authentication status.
841 */
842 public static final String CATEGORY_ERROR = "err";
843
844 /**
845 * Notification category: media transport control for playback.
846 */
847 public static final String CATEGORY_TRANSPORT = "transport";
848
849 /**
850 * Notification category: system or device status update. Reserved for system use.
851 */
852 public static final String CATEGORY_SYSTEM = "sys";
853
854 /**
855 * Notification category: indication of running background service.
856 */
857 public static final String CATEGORY_SERVICE = "service";
858
859 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400860 * Notification category: a specific, timely recommendation for a single thing.
861 * For example, a news app might want to recommend a news story it believes the user will
862 * want to read next.
863 */
864 public static final String CATEGORY_RECOMMENDATION = "recommendation";
865
866 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400867 * Notification category: ongoing information about device or contextual status.
868 */
869 public static final String CATEGORY_STATUS = "status";
870
871 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400872 * Notification category: user-scheduled reminder.
873 */
874 public static final String CATEGORY_REMINDER = "reminder";
875
876 /**
Adora Zhangaa90e872018-03-12 14:07:50 -0700877 * Notification category: extreme car emergencies.
878 * @hide
879 */
880 @SystemApi
881 public static final String CATEGORY_CAR_EMERGENCY = "car_emergency";
882
883 /**
884 * Notification category: car warnings.
885 * @hide
886 */
887 @SystemApi
888 public static final String CATEGORY_CAR_WARNING = "car_warning";
889
890 /**
891 * Notification category: general car system information.
892 * @hide
893 */
894 @SystemApi
895 public static final String CATEGORY_CAR_INFORMATION = "car_information";
896
897 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400898 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
899 * that best describes this Notification. May be used by the system for ranking and filtering.
900 */
901 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500902
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100903 @UnsupportedAppUsage
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700904 private String mGroupKey;
905
906 /**
907 * Get the key used to group this notification into a cluster or stack
908 * with other notifications on devices which support such rendering.
909 */
910 public String getGroup() {
911 return mGroupKey;
912 }
913
914 private String mSortKey;
915
916 /**
917 * Get a sort key that orders this notification among other notifications from the
918 * same package. This can be useful if an external sort was already applied and an app
919 * would like to preserve this. Notifications will be sorted lexicographically using this
920 * value, although providing different priorities in addition to providing sort key may
921 * cause this value to be ignored.
922 *
923 * <p>This sort key can also be used to order members of a notification group. See
924 * {@link Builder#setGroup}.
925 *
926 * @see String#compareTo(String)
927 */
928 public String getSortKey() {
929 return mSortKey;
930 }
931
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500932 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400933 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400934 * <p>
935 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
936 * APIs, and are intended to be used by
937 * {@link android.service.notification.NotificationListenerService} implementations to extract
938 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500939 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400940 public Bundle extras = new Bundle();
941
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400942 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700943 * All pending intents in the notification as the system needs to be able to access them but
944 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700945 * custom parcelable objects.
946 *
947 * @hide
948 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100949 @UnsupportedAppUsage
Felipe Lemedd85da62016-06-28 11:29:54 -0700950 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700951
952 /**
Dianne Hackborn98305522017-05-05 17:53:53 -0700953 * Token identifying the notification that is applying doze/bgcheck whitelisting to the
954 * pending intents inside of it, so only those will get the behavior.
955 *
956 * @hide
957 */
Adrian Roosfb921842017-10-26 14:49:56 +0200958 private IBinder mWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -0700959
960 /**
961 * Must be set by a process to start associating tokens with Notification objects
962 * coming in to it. This is set by NotificationManagerService.
963 *
964 * @hide
965 */
966 static public IBinder processWhitelistToken;
967
968 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400969 * {@link #extras} key: this is the title of the notification,
970 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
971 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500972 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400973
974 /**
975 * {@link #extras} key: this is the title of the notification when shown in expanded form,
976 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
977 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400978 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400979
980 /**
981 * {@link #extras} key: this is the main text payload, as supplied to
982 * {@link Builder#setContentText(CharSequence)}.
983 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500984 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400985
986 /**
987 * {@link #extras} key: this is a third line of text, as supplied to
988 * {@link Builder#setSubText(CharSequence)}.
989 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400990 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400991
992 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800993 * {@link #extras} key: this is the remote input history, as supplied to
994 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700995 *
996 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
997 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
998 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
999 * notifications once the other party has responded).
1000 *
1001 * The extra with this key is of type CharSequence[] and contains the most recent entry at
1002 * the 0 index, the second most recent at the 1 index, etc.
1003 *
1004 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -08001005 */
1006 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
1007
1008 /**
Kenny Guya0f6de82018-04-06 16:20:16 +01001009 * {@link #extras} key: boolean as supplied to
1010 * {@link Builder#setShowRemoteInputSpinner(boolean)}.
1011 *
1012 * If set to true, then the view displaying the remote input history from
1013 * {@link Builder#setRemoteInputHistory(CharSequence[])} will have a progress spinner.
1014 *
1015 * @see Builder#setShowRemoteInputSpinner(boolean)
1016 * @hide
1017 */
1018 public static final String EXTRA_SHOW_REMOTE_INPUT_SPINNER = "android.remoteInputSpinner";
1019
1020 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +01001021 * {@link #extras} key: boolean as supplied to
1022 * {@link Builder#setHideSmartReplies(boolean)}.
1023 *
1024 * If set to true, then any smart reply buttons will be hidden.
1025 *
1026 * @see Builder#setHideSmartReplies(boolean)
1027 * @hide
1028 */
1029 public static final String EXTRA_HIDE_SMART_REPLIES = "android.hideSmartReplies";
1030
1031 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001032 * {@link #extras} key: this is a small piece of additional text as supplied to
1033 * {@link Builder#setContentInfo(CharSequence)}.
1034 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001035 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001036
1037 /**
1038 * {@link #extras} key: this is a line of summary information intended to be shown
1039 * alongside expanded notifications, as supplied to (e.g.)
1040 * {@link BigTextStyle#setSummaryText(CharSequence)}.
1041 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001042 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001043
1044 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02001045 * {@link #extras} key: this is the longer text shown in the big form of a
1046 * {@link BigTextStyle} notification, as supplied to
1047 * {@link BigTextStyle#bigText(CharSequence)}.
1048 */
1049 public static final String EXTRA_BIG_TEXT = "android.bigText";
1050
1051 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001052 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
1053 * supplied to {@link Builder#setSmallIcon(int)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -04001054 *
1055 * @deprecated Use {@link #getSmallIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001056 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04001057 @Deprecated
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001058 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001059
1060 /**
1061 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
1062 * notification payload, as
1063 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -04001064 *
1065 * @deprecated Use {@link #getLargeIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001066 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04001067 @Deprecated
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001068 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001069
1070 /**
1071 * {@link #extras} key: this is a bitmap to be used instead of the one from
1072 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
1073 * shown in its expanded form, as supplied to
1074 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
1075 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001076 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001077
1078 /**
1079 * {@link #extras} key: this is the progress value supplied to
1080 * {@link Builder#setProgress(int, int, boolean)}.
1081 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001082 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001083
1084 /**
1085 * {@link #extras} key: this is the maximum value supplied to
1086 * {@link Builder#setProgress(int, int, boolean)}.
1087 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001088 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001089
1090 /**
1091 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
1092 * {@link Builder#setProgress(int, int, boolean)}.
1093 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001094 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001095
1096 /**
1097 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
1098 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
1099 * {@link Builder#setUsesChronometer(boolean)}.
1100 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001101 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001102
1103 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08001104 * {@link #extras} key: whether the chronometer set on the notification should count down
1105 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -07001106 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -08001107 */
Adrian Roos96b7e202016-05-17 13:50:38 -07001108 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -08001109
1110 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001111 * {@link #extras} key: whether {@link #when} should be shown,
1112 * as supplied to {@link Builder#setShowWhen(boolean)}.
1113 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001114 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001115
1116 /**
1117 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
1118 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
1119 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001120 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001121
1122 /**
1123 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
1124 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
1125 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001126 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -04001127
1128 /**
1129 * {@link #extras} key: A string representing the name of the specific
1130 * {@link android.app.Notification.Style} used to create this notification.
1131 */
Chris Wren91ad5632013-06-05 15:05:57 -04001132 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001133
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001134 /**
Chris Wrene6c48932014-09-29 17:19:27 -04001135 * {@link #extras} key: A String array containing the people that this notification relates to,
1136 * each of which was supplied to {@link Builder#addPerson(String)}.
Selim Cineke7238dd2017-12-14 17:48:32 -08001137 *
1138 * @deprecated the actual objects are now in {@link #EXTRA_PEOPLE_LIST}
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001139 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001140 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001141
1142 /**
Selim Cineke7238dd2017-12-14 17:48:32 -08001143 * {@link #extras} key: An arrayList of {@link Person} objects containing the people that
1144 * this notification relates to.
1145 */
1146 public static final String EXTRA_PEOPLE_LIST = "android.people.list";
1147
1148 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04001149 * Allow certain system-generated notifications to appear before the device is provisioned.
1150 * Only available to notifications coming from the android package.
1151 * @hide
1152 */
Maurice Lam96c10032017-03-29 15:42:38 -07001153 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001154 @RequiresPermission(android.Manifest.permission.NOTIFICATION_DURING_SETUP)
John Spurlockfd7f1e02014-03-18 16:41:57 -04001155 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
1156
1157 /**
Robin Leed107af62018-04-27 13:55:56 +02001158 * {@link #extras} key:
1159 * flat {@link String} representation of a {@link android.content.ContentUris content URI}
1160 * pointing to an image that can be displayed in the background when the notification is
1161 * selected. Used on television platforms. The URI must point to an image stream suitable for
1162 * passing into {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001163 * BitmapFactory.decodeStream}; all other content types will be ignored.
Jose Limae9e3b3b2014-05-18 23:44:50 -07001164 */
1165 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
1166
1167 /**
Dan Sandler842dd772014-05-15 09:36:47 -04001168 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -07001169 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -04001170 * {@link android.app.Notification.MediaStyle} notification.
1171 */
1172 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
1173
1174 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +01001175 * {@link #extras} key: the indices of actions to be shown in the compact view,
1176 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
1177 */
1178 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
1179
Christoph Studer943aa672014-08-03 20:31:16 +02001180 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04001181 * {@link #extras} key: the username to be displayed for all messages sent by the user including
1182 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -07001183 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1184 * {@link CharSequence}
Selim Cinekcb8b9852017-12-15 18:01:52 -08001185 *
1186 * @deprecated use {@link #EXTRA_MESSAGING_PERSON}
Alex Hillsfc737de2016-03-23 17:33:02 -04001187 */
1188 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
1189
1190 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08001191 * {@link #extras} key: the person to be displayed for all messages sent by the user including
1192 * direct replies
1193 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1194 * {@link Person}
1195 */
1196 public static final String EXTRA_MESSAGING_PERSON = "android.messagingUser";
1197
1198 /**
Adrian Roos96b7e202016-05-17 13:50:38 -07001199 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -04001200 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -04001201 */
Alex Hillsd9b04d92016-04-11 16:38:16 -04001202 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -04001203
1204 /**
1205 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
1206 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -07001207 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1208 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -04001209 */
1210 public static final String EXTRA_MESSAGES = "android.messages";
1211
1212 /**
Adrian Roos437cd562017-01-18 15:47:03 -08001213 * {@link #extras} key: an array of
1214 * {@link android.app.Notification.MessagingStyle#addHistoricMessage historic}
1215 * {@link android.app.Notification.MessagingStyle.Message} bundles provided by a
1216 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1217 * array of bundles.
1218 */
1219 public static final String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
1220
1221 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08001222 * {@link #extras} key: whether the {@link android.app.Notification.MessagingStyle} notification
1223 * represents a group conversation.
1224 */
1225 public static final String EXTRA_IS_GROUP_CONVERSATION = "android.isGroupConversation";
1226
1227 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08001228 * {@link #extras} key: whether the notification should be colorized as
Gustav Senntondf4c2442019-03-18 11:41:11 +00001229 * supplied to {@link Builder#setColorized(boolean)}.
Selim Cinek7b9605b2017-01-19 17:36:00 -08001230 */
1231 public static final String EXTRA_COLORIZED = "android.colorized";
1232
1233 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001234 * @hide
1235 */
1236 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
1237
Selim Cinek247fa012016-02-18 09:50:48 -08001238 /**
1239 * @hide
1240 */
1241 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
1242
Shane Brennan472a3b32016-12-12 15:28:10 -08001243 /**
Selim Cinekd0426622017-07-11 13:19:59 +02001244 * @hide
1245 */
1246 public static final String EXTRA_REDUCED_IMAGES = "android.reduced.images";
1247
1248 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001249 * {@link #extras} key: the audio contents of this notification.
1250 *
1251 * This is for use when rendering the notification on an audio-focused interface;
1252 * the audio contents are a complete sound sample that contains the contents/body of the
1253 * notification. This may be used in substitute of a Text-to-Speech reading of the
1254 * notification. For example if the notification represents a voice message this should point
1255 * to the audio of that message.
1256 *
1257 * The data stored under this key should be a String representation of a Uri that contains the
1258 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1259 *
1260 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1261 * has a field for holding data URI. That field can be used for audio.
1262 * See {@code Message#setData}.
1263 *
1264 * Example usage:
1265 * <pre>
1266 * {@code
1267 * Notification.Builder myBuilder = (build your Notification as normal);
1268 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1269 * }
1270 * </pre>
1271 */
1272 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1273
Dan Sandler80eaa592016-04-14 23:34:54 -04001274 /** @hide */
1275 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001276 @RequiresPermission(android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME)
Dan Sandler732bd6c2016-04-12 14:20:32 -04001277 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1278
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001279 /**
Julia Reynolds3c7de112018-03-28 09:33:13 -04001280 * This is set on the notifications shown by system_server about apps running foreground
1281 * services. It indicates that the notification should be shown
1282 * only if any of the given apps do not already have a properly tagged
1283 * {@link #FLAG_FOREGROUND_SERVICE} notification currently visible to the user.
1284 * This is a string array of all package names of the apps.
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001285 * @hide
1286 */
1287 public static final String EXTRA_FOREGROUND_APPS = "android.foregroundApps";
1288
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001289 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04001290 private Icon mSmallIcon;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001291 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04001292 private Icon mLargeIcon;
1293
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001294 @UnsupportedAppUsage
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001295 private String mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001296 private long mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001297
Julia Reynolds13d898c2017-02-02 12:22:05 -05001298 private String mShortcutId;
Felipe Leme90205ef2019-03-05 09:59:52 -08001299 private LocusId mLocusId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04001300 private CharSequence mSettingsText;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001301
Mady Mellorc39b4ae2019-01-09 17:11:37 -08001302 private BubbleMetadata mBubbleMetadata;
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04001303
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001304 /** @hide */
1305 @IntDef(prefix = { "GROUP_ALERT_" }, value = {
1306 GROUP_ALERT_ALL, GROUP_ALERT_CHILDREN, GROUP_ALERT_SUMMARY
1307 })
1308 @Retention(RetentionPolicy.SOURCE)
1309 public @interface GroupAlertBehavior {}
1310
1311 /**
1312 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all notifications in a
1313 * group with sound or vibration ought to make sound or vibrate (respectively), so this
1314 * notification will not be muted when it is in a group.
1315 */
1316 public static final int GROUP_ALERT_ALL = 0;
1317
1318 /**
1319 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all children
1320 * notification in a group should be silenced (no sound or vibration) even if they are posted
1321 * to a {@link NotificationChannel} that has sound and/or vibration. Use this constant to
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001322 * mute this notification if this notification is a group child. This must be applied to all
1323 * children notifications you want to mute.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001324 *
1325 * <p> For example, you might want to use this constant if you post a number of children
1326 * notifications at once (say, after a periodic sync), and only need to notify the user
1327 * audibly once.
1328 */
1329 public static final int GROUP_ALERT_SUMMARY = 1;
1330
1331 /**
1332 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that the summary
1333 * notification in a group should be silenced (no sound or vibration) even if they are
1334 * posted to a {@link NotificationChannel} that has sound and/or vibration. Use this constant
1335 * to mute this notification if this notification is a group summary.
1336 *
1337 * <p>For example, you might want to use this constant if only the children notifications
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001338 * in your group have content and the summary is only used to visually group notifications
1339 * rather than to alert the user that new information is available.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001340 */
1341 public static final int GROUP_ALERT_CHILDREN = 2;
1342
Julia Reynolds2f431e22017-06-07 14:12:09 +00001343 private int mGroupAlertBehavior = GROUP_ALERT_ALL;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001344
Julia Reynolds13d898c2017-02-02 12:22:05 -05001345 /**
1346 * If this notification is being shown as a badge, always show as a number.
1347 */
1348 public static final int BADGE_ICON_NONE = 0;
1349
1350 /**
1351 * If this notification is being shown as a badge, use the {@link #getSmallIcon()} to
1352 * represent this notification.
1353 */
1354 public static final int BADGE_ICON_SMALL = 1;
1355
1356 /**
1357 * If this notification is being shown as a badge, use the {@link #getLargeIcon()} to
1358 * represent this notification.
1359 */
1360 public static final int BADGE_ICON_LARGE = 2;
Julia Reynolds7d5b4da2017-03-20 10:18:49 -04001361 private int mBadgeIcon = BADGE_ICON_NONE;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001362
Chris Wren51c75102013-07-16 20:49:17 -04001363 /**
Gustav Sennton761884c2018-11-19 17:40:19 +00001364 * Determines whether the platform can generate contextual actions for a notification.
1365 */
1366 private boolean mAllowSystemGeneratedContextualActions = true;
1367
1368 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001369 * Structure to encapsulate a named action that can be shown as part of this notification.
1370 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1371 * selected by the user.
1372 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001373 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1374 * or {@link Notification.Builder#addAction(Notification.Action)}
1375 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001376 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001377 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001378 /**
1379 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1380 * {@link RemoteInput}s.
1381 *
1382 * This is intended for {@link RemoteInput}s that only accept data, meaning
1383 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
Gustav Senntondf4c2442019-03-18 11:41:11 +00001384 * is null or empty, and {@link RemoteInput#getAllowedDataTypes} is non-null and not
Shane Brennan472a3b32016-12-12 15:28:10 -08001385 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1386 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1387 *
1388 * You can test if a RemoteInput matches these constraints using
1389 * {@link RemoteInput#isDataOnly}.
1390 */
1391 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1392
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001393 /**
1394 * {@link }: No semantic action defined.
1395 */
1396 public static final int SEMANTIC_ACTION_NONE = 0;
1397
1398 /**
1399 * {@code SemanticAction}: Reply to a conversation, chat, group, or wherever replies
1400 * may be appropriate.
1401 */
1402 public static final int SEMANTIC_ACTION_REPLY = 1;
1403
1404 /**
1405 * {@code SemanticAction}: Mark content as read.
1406 */
1407 public static final int SEMANTIC_ACTION_MARK_AS_READ = 2;
1408
1409 /**
1410 * {@code SemanticAction}: Mark content as unread.
1411 */
1412 public static final int SEMANTIC_ACTION_MARK_AS_UNREAD = 3;
1413
1414 /**
1415 * {@code SemanticAction}: Delete the content associated with the notification. This
1416 * could mean deleting an email, message, etc.
1417 */
1418 public static final int SEMANTIC_ACTION_DELETE = 4;
1419
1420 /**
1421 * {@code SemanticAction}: Archive the content associated with the notification. This
1422 * could mean archiving an email, message, etc.
1423 */
1424 public static final int SEMANTIC_ACTION_ARCHIVE = 5;
1425
1426 /**
1427 * {@code SemanticAction}: Mute the content associated with the notification. This could
1428 * mean silencing a conversation or currently playing media.
1429 */
1430 public static final int SEMANTIC_ACTION_MUTE = 6;
1431
1432 /**
1433 * {@code SemanticAction}: Unmute the content associated with the notification. This could
1434 * mean un-silencing a conversation or currently playing media.
1435 */
1436 public static final int SEMANTIC_ACTION_UNMUTE = 7;
1437
1438 /**
1439 * {@code SemanticAction}: Mark content with a thumbs up.
1440 */
1441 public static final int SEMANTIC_ACTION_THUMBS_UP = 8;
1442
1443 /**
1444 * {@code SemanticAction}: Mark content with a thumbs down.
1445 */
1446 public static final int SEMANTIC_ACTION_THUMBS_DOWN = 9;
1447
Kodlee Yincda5b092018-02-15 15:34:53 -08001448 /**
1449 * {@code SemanticAction}: Call a contact, group, etc.
1450 */
1451 public static final int SEMANTIC_ACTION_CALL = 10;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001452
Griff Hazen959591e2014-05-15 22:26:18 -07001453 private final Bundle mExtras;
Mathew Inwood8c854f82018-09-14 12:35:36 +01001454 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Dan Sandler86647982015-05-13 23:41:13 -04001455 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001456 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001457 private boolean mAllowGeneratedReplies = true;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001458 private final @SemanticAction int mSemanticAction;
Gustav Sennton005d7a02019-01-04 13:41:32 +00001459 private final boolean mIsContextual;
Griff Hazen959591e2014-05-15 22:26:18 -07001460
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001461 /**
1462 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001463 *
1464 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001465 */
Dan Sandler86647982015-05-13 23:41:13 -04001466 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001467 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001468
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001469 /**
1470 * Title of the action.
1471 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001472 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001473
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001474 /**
1475 * Intent to send when the user invokes this action. May be null, in which case the action
1476 * may be rendered in a disabled presentation by the system UI.
1477 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001478 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001479
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001480 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001481 if (in.readInt() != 0) {
1482 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001483 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1484 icon = mIcon.getResId();
1485 }
Dan Sandler86647982015-05-13 23:41:13 -04001486 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001487 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1488 if (in.readInt() == 1) {
1489 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1490 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001491 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001492 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001493 mAllowGeneratedReplies = in.readInt() == 1;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001494 mSemanticAction = in.readInt();
Gustav Sennton005d7a02019-01-04 13:41:32 +00001495 mIsContextual = in.readInt() == 1;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001496 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001497
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001498 /**
Dan Sandler86647982015-05-13 23:41:13 -04001499 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001500 */
Dan Sandler86647982015-05-13 23:41:13 -04001501 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001502 public Action(int icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001503 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true,
Gustav Sennton005d7a02019-01-04 13:41:32 +00001504 SEMANTIC_ACTION_NONE, false /* isContextual */);
Griff Hazen959591e2014-05-15 22:26:18 -07001505 }
1506
Adrian Roos7af53622016-10-12 13:44:05 -07001507 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001508 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001509 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
Gustav Sennton005d7a02019-01-04 13:41:32 +00001510 @SemanticAction int semanticAction, boolean isContextual) {
Dan Sandler86647982015-05-13 23:41:13 -04001511 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001512 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1513 this.icon = icon.getResId();
1514 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001515 this.title = title;
1516 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001517 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001518 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001519 this.mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001520 this.mSemanticAction = semanticAction;
Gustav Sennton005d7a02019-01-04 13:41:32 +00001521 this.mIsContextual = isContextual;
Griff Hazen959591e2014-05-15 22:26:18 -07001522 }
1523
1524 /**
Dan Sandler86647982015-05-13 23:41:13 -04001525 * Return an icon representing the action.
1526 */
1527 public Icon getIcon() {
1528 if (mIcon == null && icon != 0) {
1529 // you snuck an icon in here without using the builder; let's try to keep it
1530 mIcon = Icon.createWithResource("", icon);
1531 }
1532 return mIcon;
1533 }
1534
1535 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001536 * Get additional metadata carried around with this Action.
1537 */
1538 public Bundle getExtras() {
1539 return mExtras;
1540 }
1541
1542 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001543 * Return whether the platform should automatically generate possible replies for this
1544 * {@link Action}
1545 */
1546 public boolean getAllowGeneratedReplies() {
1547 return mAllowGeneratedReplies;
1548 }
1549
1550 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001551 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001552 * May return null if no remote inputs were added. Only returns inputs which accept
1553 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001554 */
1555 public RemoteInput[] getRemoteInputs() {
1556 return mRemoteInputs;
1557 }
1558
1559 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001560 * Returns the {@code SemanticAction} associated with this {@link Action}. A
1561 * {@code SemanticAction} denotes what an {@link Action}'s {@link PendingIntent} will do
1562 * (eg. reply, mark as read, delete, etc).
1563 */
1564 public @SemanticAction int getSemanticAction() {
1565 return mSemanticAction;
1566 }
1567
1568 /**
Gustav Sennton005d7a02019-01-04 13:41:32 +00001569 * Returns whether this is a contextual Action, i.e. whether the action is dependent on the
1570 * notification message body. An example of a contextual action could be an action opening a
1571 * map application with an address shown in the notification.
1572 */
1573 public boolean isContextual() {
1574 return mIsContextual;
1575 }
1576
1577 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001578 * Get the list of inputs to be collected from the user that ONLY accept data when this
1579 * action is sent. These remote inputs are guaranteed to return true on a call to
1580 * {@link RemoteInput#isDataOnly}.
1581 *
Shane Brennanf670d6c2017-04-25 13:05:45 -07001582 * Returns null if there are no data-only remote inputs.
Shane Brennan472a3b32016-12-12 15:28:10 -08001583 *
1584 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1585 * of non-textual RemoteInputs do not access these remote inputs.
1586 */
1587 public RemoteInput[] getDataOnlyRemoteInputs() {
1588 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1589 }
1590
1591 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001592 * Builder class for {@link Action} objects.
1593 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001594 public static final class Builder {
Gustav Senntone1fc87b2019-03-18 14:31:28 +00001595 @Nullable private final Icon mIcon;
1596 @Nullable private final CharSequence mTitle;
1597 @Nullable private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001598 private boolean mAllowGeneratedReplies = true;
Gustav Senntone1fc87b2019-03-18 14:31:28 +00001599 @NonNull private final Bundle mExtras;
1600 @Nullable private ArrayList<RemoteInput> mRemoteInputs;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001601 private @SemanticAction int mSemanticAction;
Gustav Sennton005d7a02019-01-04 13:41:32 +00001602 private boolean mIsContextual;
Griff Hazen959591e2014-05-15 22:26:18 -07001603
1604 /**
1605 * Construct a new builder for {@link Action} object.
1606 * @param icon icon to show for this action
1607 * @param title the title of the action
1608 * @param intent the {@link PendingIntent} to fire when users trigger this action
1609 */
Dan Sandler86647982015-05-13 23:41:13 -04001610 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001611 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001612 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001613 }
1614
1615 /**
1616 * Construct a new builder for {@link Action} object.
1617 * @param icon icon to show for this action
1618 * @param title the title of the action
1619 * @param intent the {@link PendingIntent} to fire when users trigger this action
1620 */
1621 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001622 this(icon, title, intent, new Bundle(), null, true, SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001623 }
1624
1625 /**
1626 * Construct a new builder for {@link Action} object using the fields from an
1627 * {@link Action}.
1628 * @param action the action to read fields from.
1629 */
1630 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001631 this(action.getIcon(), action.title, action.actionIntent,
1632 new Bundle(action.mExtras), action.getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001633 action.getAllowGeneratedReplies(), action.getSemanticAction());
Griff Hazen959591e2014-05-15 22:26:18 -07001634 }
1635
Gustav Senntone1fc87b2019-03-18 14:31:28 +00001636 private Builder(@Nullable Icon icon, @Nullable CharSequence title,
1637 @Nullable PendingIntent intent, @NonNull Bundle extras,
1638 @Nullable RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1639 @SemanticAction int semanticAction) {
Griff Hazen959591e2014-05-15 22:26:18 -07001640 mIcon = icon;
1641 mTitle = title;
1642 mIntent = intent;
1643 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001644 if (remoteInputs != null) {
1645 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1646 Collections.addAll(mRemoteInputs, remoteInputs);
1647 }
Adrian Roos7af53622016-10-12 13:44:05 -07001648 mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001649 mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001650 }
1651
1652 /**
1653 * Merge additional metadata into this builder.
1654 *
1655 * <p>Values within the Bundle will replace existing extras values in this Builder.
1656 *
1657 * @see Notification.Action#extras
1658 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001659 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07001660 public Builder addExtras(Bundle extras) {
1661 if (extras != null) {
1662 mExtras.putAll(extras);
1663 }
1664 return this;
1665 }
1666
1667 /**
1668 * Get the metadata Bundle used by this Builder.
1669 *
1670 * <p>The returned Bundle is shared with this Builder.
1671 */
Gustav Senntone1fc87b2019-03-18 14:31:28 +00001672 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07001673 public Bundle getExtras() {
1674 return mExtras;
1675 }
1676
1677 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001678 * Add an input to be collected from the user when this action is sent.
1679 * Response values can be retrieved from the fired intent by using the
1680 * {@link RemoteInput#getResultsFromIntent} function.
1681 * @param remoteInput a {@link RemoteInput} to add to the action
1682 * @return this object for method chaining
1683 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001684 @NonNull
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001685 public Builder addRemoteInput(RemoteInput remoteInput) {
1686 if (mRemoteInputs == null) {
1687 mRemoteInputs = new ArrayList<RemoteInput>();
1688 }
1689 mRemoteInputs.add(remoteInput);
1690 return this;
1691 }
1692
1693 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001694 * Set whether the platform should automatically generate possible replies to add to
1695 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1696 * {@link RemoteInput}, this has no effect.
1697 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1698 * otherwise
1699 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001700 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001701 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001702 @NonNull
Alex Hills42b0c4d2016-04-26 13:35:36 -04001703 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1704 mAllowGeneratedReplies = allowGeneratedReplies;
1705 return this;
1706 }
1707
1708 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001709 * Sets the {@code SemanticAction} for this {@link Action}. A
1710 * {@code SemanticAction} denotes what an {@link Action}'s
1711 * {@link PendingIntent} will do (eg. reply, mark as read, delete, etc).
1712 * @param semanticAction a SemanticAction defined within {@link Action} with
1713 * {@code SEMANTIC_ACTION_} prefixes
1714 * @return this object for method chaining
1715 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001716 @NonNull
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001717 public Builder setSemanticAction(@SemanticAction int semanticAction) {
1718 mSemanticAction = semanticAction;
1719 return this;
1720 }
1721
1722 /**
Gustav Sennton005d7a02019-01-04 13:41:32 +00001723 * Sets whether this {@link Action} is a contextual action, i.e. whether the action is
1724 * dependent on the notification message body. An example of a contextual action could
1725 * be an action opening a map application with an address shown in the notification.
1726 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001727 @NonNull
Gustav Sennton005d7a02019-01-04 13:41:32 +00001728 public Builder setContextual(boolean isContextual) {
1729 mIsContextual = isContextual;
1730 return this;
1731 }
1732
1733 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001734 * Apply an extender to this action builder. Extenders may be used to add
1735 * metadata or change options on this builder.
1736 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001737 @NonNull
Griff Hazen61a9e862014-05-22 16:05:19 -07001738 public Builder extend(Extender extender) {
1739 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001740 return this;
1741 }
1742
1743 /**
Gustav Senntonc98b1632018-11-23 12:26:15 +00001744 * Throws an NPE if we are building a contextual action missing one of the fields
1745 * necessary to display the action.
1746 */
1747 private void checkContextualActionNullFields() {
Gustav Sennton005d7a02019-01-04 13:41:32 +00001748 if (!mIsContextual) return;
Gustav Senntonc98b1632018-11-23 12:26:15 +00001749
1750 if (mIcon == null) {
1751 throw new NullPointerException("Contextual Actions must contain a valid icon");
1752 }
1753
1754 if (mIntent == null) {
1755 throw new NullPointerException(
1756 "Contextual Actions must contain a valid PendingIntent");
1757 }
1758 }
1759
1760 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001761 * Combine all of the options that have been set and return a new {@link Action}
1762 * object.
1763 * @return the built action
1764 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001765 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07001766 public Action build() {
Gustav Senntonc98b1632018-11-23 12:26:15 +00001767 checkContextualActionNullFields();
1768
Shane Brennan472a3b32016-12-12 15:28:10 -08001769 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1770 RemoteInput[] previousDataInputs =
1771 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001772 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001773 for (RemoteInput input : previousDataInputs) {
1774 dataOnlyInputs.add(input);
1775 }
1776 }
1777 List<RemoteInput> textInputs = new ArrayList<>();
1778 if (mRemoteInputs != null) {
1779 for (RemoteInput input : mRemoteInputs) {
1780 if (input.isDataOnly()) {
1781 dataOnlyInputs.add(input);
1782 } else {
1783 textInputs.add(input);
1784 }
1785 }
1786 }
1787 if (!dataOnlyInputs.isEmpty()) {
1788 RemoteInput[] dataInputsArr =
1789 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1790 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1791 }
1792 RemoteInput[] textInputsArr = textInputs.isEmpty()
1793 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1794 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Gustav Sennton005d7a02019-01-04 13:41:32 +00001795 mAllowGeneratedReplies, mSemanticAction, mIsContextual);
Griff Hazen959591e2014-05-15 22:26:18 -07001796 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001797 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001798
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001799 @Override
1800 public Action clone() {
1801 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001802 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001803 title,
1804 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001805 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001806 getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001807 getAllowGeneratedReplies(),
Gustav Sennton005d7a02019-01-04 13:41:32 +00001808 getSemanticAction(),
1809 isContextual());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001810 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001811
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001812 @Override
1813 public int describeContents() {
1814 return 0;
1815 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001816
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001817 @Override
1818 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001819 final Icon ic = getIcon();
1820 if (ic != null) {
1821 out.writeInt(1);
1822 ic.writeToParcel(out, 0);
1823 } else {
1824 out.writeInt(0);
1825 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001826 TextUtils.writeToParcel(title, out, flags);
1827 if (actionIntent != null) {
1828 out.writeInt(1);
1829 actionIntent.writeToParcel(out, flags);
1830 } else {
1831 out.writeInt(0);
1832 }
Griff Hazen959591e2014-05-15 22:26:18 -07001833 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001834 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001835 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001836 out.writeInt(mSemanticAction);
Gustav Sennton005d7a02019-01-04 13:41:32 +00001837 out.writeInt(mIsContextual ? 1 : 0);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001838 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001839
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07001840 public static final @android.annotation.NonNull Parcelable.Creator<Action> CREATOR =
Griff Hazen959591e2014-05-15 22:26:18 -07001841 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001842 public Action createFromParcel(Parcel in) {
1843 return new Action(in);
1844 }
1845 public Action[] newArray(int size) {
1846 return new Action[size];
1847 }
1848 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001849
1850 /**
1851 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1852 * metadata or change options on an action builder.
1853 */
1854 public interface Extender {
1855 /**
1856 * Apply this extender to a notification action builder.
1857 * @param builder the builder to be modified.
1858 * @return the build object for chaining.
1859 */
1860 public Builder extend(Builder builder);
1861 }
1862
1863 /**
1864 * Wearable extender for notification actions. To add extensions to an action,
1865 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1866 * the {@code WearableExtender()} constructor and apply it to a
1867 * {@link android.app.Notification.Action.Builder} using
1868 * {@link android.app.Notification.Action.Builder#extend}.
1869 *
1870 * <pre class="prettyprint">
1871 * Notification.Action action = new Notification.Action.Builder(
1872 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001873 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001874 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001875 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001876 */
1877 public static final class WearableExtender implements Extender {
1878 /** Notification action extra which contains wearable extensions */
1879 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1880
Pete Gastaf6781d2014-10-07 15:17:05 -04001881 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001882 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001883 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1884 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1885 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001886
1887 // Flags bitwise-ored to mFlags
1888 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001889 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001890 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001891
1892 // Default value for flags integer
1893 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1894
1895 private int mFlags = DEFAULT_FLAGS;
1896
Pete Gastaf6781d2014-10-07 15:17:05 -04001897 private CharSequence mInProgressLabel;
1898 private CharSequence mConfirmLabel;
1899 private CharSequence mCancelLabel;
1900
Griff Hazen61a9e862014-05-22 16:05:19 -07001901 /**
1902 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1903 * options.
1904 */
1905 public WearableExtender() {
1906 }
1907
1908 /**
1909 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1910 * wearable options present in an existing notification action.
1911 * @param action the notification action to inspect.
1912 */
1913 public WearableExtender(Action action) {
1914 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1915 if (wearableBundle != null) {
1916 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001917 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1918 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1919 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001920 }
1921 }
1922
1923 /**
1924 * Apply wearable extensions to a notification action that is being built. This is
1925 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1926 * method of {@link android.app.Notification.Action.Builder}.
1927 */
1928 @Override
1929 public Action.Builder extend(Action.Builder builder) {
1930 Bundle wearableBundle = new Bundle();
1931
1932 if (mFlags != DEFAULT_FLAGS) {
1933 wearableBundle.putInt(KEY_FLAGS, mFlags);
1934 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001935 if (mInProgressLabel != null) {
1936 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1937 }
1938 if (mConfirmLabel != null) {
1939 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1940 }
1941 if (mCancelLabel != null) {
1942 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1943 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001944
1945 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1946 return builder;
1947 }
1948
1949 @Override
1950 public WearableExtender clone() {
1951 WearableExtender that = new WearableExtender();
1952 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001953 that.mInProgressLabel = this.mInProgressLabel;
1954 that.mConfirmLabel = this.mConfirmLabel;
1955 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001956 return that;
1957 }
1958
1959 /**
1960 * Set whether this action is available when the wearable device is not connected to
1961 * a companion device. The user can still trigger this action when the wearable device is
1962 * offline, but a visual hint will indicate that the action may not be available.
1963 * Defaults to true.
1964 */
1965 public WearableExtender setAvailableOffline(boolean availableOffline) {
1966 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1967 return this;
1968 }
1969
1970 /**
1971 * Get whether this action is available when the wearable device is not connected to
1972 * a companion device. The user can still trigger this action when the wearable device is
1973 * offline, but a visual hint will indicate that the action may not be available.
1974 * Defaults to true.
1975 */
1976 public boolean isAvailableOffline() {
1977 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1978 }
1979
1980 private void setFlag(int mask, boolean value) {
1981 if (value) {
1982 mFlags |= mask;
1983 } else {
1984 mFlags &= ~mask;
1985 }
1986 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001987
1988 /**
1989 * Set a label to display while the wearable is preparing to automatically execute the
1990 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1991 *
1992 * @param label the label to display while the action is being prepared to execute
1993 * @return this object for method chaining
1994 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001995 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001996 public WearableExtender setInProgressLabel(CharSequence label) {
1997 mInProgressLabel = label;
1998 return this;
1999 }
2000
2001 /**
2002 * Get the label to display while the wearable is preparing to automatically execute
2003 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
2004 *
2005 * @return the label to display while the action is being prepared to execute
2006 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002007 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002008 public CharSequence getInProgressLabel() {
2009 return mInProgressLabel;
2010 }
2011
2012 /**
2013 * Set a label to display to confirm that the action should be executed.
2014 * This is usually an imperative verb like "Send".
2015 *
2016 * @param label the label to confirm the action should be executed
2017 * @return this object for method chaining
2018 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002019 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002020 public WearableExtender setConfirmLabel(CharSequence label) {
2021 mConfirmLabel = label;
2022 return this;
2023 }
2024
2025 /**
2026 * Get the label to display to confirm that the action should be executed.
2027 * This is usually an imperative verb like "Send".
2028 *
2029 * @return the label to confirm the action should be executed
2030 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002031 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002032 public CharSequence getConfirmLabel() {
2033 return mConfirmLabel;
2034 }
2035
2036 /**
2037 * Set a label to display to cancel the action.
2038 * This is usually an imperative verb, like "Cancel".
2039 *
2040 * @param label the label to display to cancel the action
2041 * @return this object for method chaining
2042 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002043 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002044 public WearableExtender setCancelLabel(CharSequence label) {
2045 mCancelLabel = label;
2046 return this;
2047 }
2048
2049 /**
2050 * Get the label to display to cancel the action.
2051 * This is usually an imperative verb like "Cancel".
2052 *
2053 * @return the label to display to cancel the action
2054 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002055 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002056 public CharSequence getCancelLabel() {
2057 return mCancelLabel;
2058 }
Alex Hills9ab3a232016-04-05 14:54:56 -04002059
2060 /**
2061 * Set a hint that this Action will launch an {@link Activity} directly, telling the
2062 * platform that it can generate the appropriate transitions.
2063 * @param hintLaunchesActivity {@code true} if the content intent will launch
2064 * an activity and transitions should be generated, false otherwise.
2065 * @return this object for method chaining
2066 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04002067 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04002068 boolean hintLaunchesActivity) {
2069 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
2070 return this;
2071 }
2072
2073 /**
2074 * Get a hint that this Action will launch an {@link Activity} directly, telling the
2075 * platform that it can generate the appropriate transitions
2076 * @return {@code true} if the content intent will launch an activity and transitions
2077 * should be generated, false otherwise. The default value is {@code false} if this was
2078 * never set.
2079 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04002080 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04002081 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
2082 }
Alex Hills9f087612016-06-07 09:08:59 -04002083
2084 /**
2085 * Set a hint that this Action should be displayed inline.
2086 *
2087 * @param hintDisplayInline {@code true} if action should be displayed inline, false
2088 * otherwise
2089 * @return this object for method chaining
2090 */
2091 public WearableExtender setHintDisplayActionInline(
2092 boolean hintDisplayInline) {
2093 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
2094 return this;
2095 }
2096
2097 /**
2098 * Get a hint that this Action should be displayed inline.
2099 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08002100 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04002101 * otherwise. The default value is {@code false} if this was never set.
2102 */
2103 public boolean getHintDisplayActionInline() {
2104 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
2105 }
Griff Hazen61a9e862014-05-22 16:05:19 -07002106 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002107
2108 /**
2109 * Provides meaning to an {@link Action} that hints at what the associated
2110 * {@link PendingIntent} will do. For example, an {@link Action} with a
2111 * {@link PendingIntent} that replies to a text message notification may have the
2112 * {@link #SEMANTIC_ACTION_REPLY} {@code SemanticAction} set within it.
2113 *
2114 * @hide
2115 */
2116 @IntDef(prefix = { "SEMANTIC_ACTION_" }, value = {
2117 SEMANTIC_ACTION_NONE,
2118 SEMANTIC_ACTION_REPLY,
2119 SEMANTIC_ACTION_MARK_AS_READ,
2120 SEMANTIC_ACTION_MARK_AS_UNREAD,
2121 SEMANTIC_ACTION_DELETE,
2122 SEMANTIC_ACTION_ARCHIVE,
2123 SEMANTIC_ACTION_MUTE,
2124 SEMANTIC_ACTION_UNMUTE,
2125 SEMANTIC_ACTION_THUMBS_UP,
Kodlee Yincda5b092018-02-15 15:34:53 -08002126 SEMANTIC_ACTION_THUMBS_DOWN,
Gustav Sennton005d7a02019-01-04 13:41:32 +00002127 SEMANTIC_ACTION_CALL
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002128 })
2129 @Retention(RetentionPolicy.SOURCE)
2130 public @interface SemanticAction {}
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002131 }
2132
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002133 /**
2134 * Array of all {@link Action} structures attached to this notification by
2135 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
2136 * {@link android.service.notification.NotificationListenerService} that provide an alternative
2137 * interface for invoking actions.
2138 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05002139 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002140
2141 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002142 * Replacement version of this notification whose content will be shown
2143 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
2144 * and {@link #VISIBILITY_PUBLIC}.
2145 */
2146 public Notification publicVersion;
2147
2148 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002149 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08002150 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002151 */
2152 public Notification()
2153 {
2154 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002155 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002156 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157 }
2158
2159 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002160 * @hide
2161 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002162 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002163 public Notification(Context context, int icon, CharSequence tickerText, long when,
2164 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
2165 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04002166 new Builder(context)
2167 .setWhen(when)
2168 .setSmallIcon(icon)
2169 .setTicker(tickerText)
2170 .setContentTitle(contentTitle)
2171 .setContentText(contentText)
2172 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
2173 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002174 }
2175
2176 /**
2177 * Constructs a Notification object with the information needed to
2178 * have a status bar icon without the standard expanded view.
2179 *
2180 * @param icon The resource id of the icon to put in the status bar.
2181 * @param tickerText The text that flows by in the status bar when the notification first
2182 * activates.
2183 * @param when The time to show in the time field. In the System.currentTimeMillis
2184 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08002185 *
2186 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002187 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002188 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002189 public Notification(int icon, CharSequence tickerText, long when)
2190 {
2191 this.icon = icon;
2192 this.tickerText = tickerText;
2193 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002194 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002195 }
2196
2197 /**
2198 * Unflatten the notification from a parcel.
2199 */
Svet Ganovddb94882016-06-23 19:55:24 -07002200 @SuppressWarnings("unchecked")
2201 public Notification(Parcel parcel) {
2202 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
2203 // intents in extras are always written as the last entry.
2204 readFromParcelImpl(parcel);
2205 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002206 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07002207 }
2208
2209 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002210 {
2211 int version = parcel.readInt();
2212
Adrian Roosfb921842017-10-26 14:49:56 +02002213 mWhitelistToken = parcel.readStrongBinder();
2214 if (mWhitelistToken == null) {
2215 mWhitelistToken = processWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -07002216 }
2217 // Propagate this token to all pending intents that are unmarshalled from the parcel.
Adrian Roosfb921842017-10-26 14:49:56 +02002218 parcel.setClassCookie(PendingIntent.class, mWhitelistToken);
Dianne Hackborn98305522017-05-05 17:53:53 -07002219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002221 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04002222 if (parcel.readInt() != 0) {
2223 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04002224 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
2225 icon = mSmallIcon.getResId();
2226 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002227 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002228 number = parcel.readInt();
2229 if (parcel.readInt() != 0) {
2230 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2231 }
2232 if (parcel.readInt() != 0) {
2233 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2234 }
2235 if (parcel.readInt() != 0) {
2236 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2237 }
2238 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002239 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002240 }
2241 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002242 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
2243 }
Joe Onorato561d3852010-11-20 18:09:34 -08002244 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002245 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08002246 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002247 defaults = parcel.readInt();
2248 flags = parcel.readInt();
2249 if (parcel.readInt() != 0) {
2250 sound = Uri.CREATOR.createFromParcel(parcel);
2251 }
2252
2253 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04002254 if (parcel.readInt() != 0) {
2255 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
2256 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002257 vibrate = parcel.createLongArray();
2258 ledARGB = parcel.readInt();
2259 ledOnMS = parcel.readInt();
2260 ledOffMS = parcel.readInt();
2261 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002262
2263 if (parcel.readInt() != 0) {
2264 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2265 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002266
2267 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002268
John Spurlockfd7f1e02014-03-18 16:41:57 -04002269 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002270
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002271 mGroupKey = parcel.readString();
2272
2273 mSortKey = parcel.readString();
2274
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06002275 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Robin Leead7e72a2017-12-04 15:45:46 +01002276 fixDuplicateExtras();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002277
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002278 actions = parcel.createTypedArray(Action.CREATOR); // may be null
2279
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002280 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002281 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2282 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002283
Chris Wren8fd39ec2014-02-27 17:43:26 -05002284 if (parcel.readInt() != 0) {
2285 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2286 }
2287
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002288 visibility = parcel.readInt();
2289
2290 if (parcel.readInt() != 0) {
2291 publicVersion = Notification.CREATOR.createFromParcel(parcel);
2292 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002293
2294 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002295
2296 if (parcel.readInt() != 0) {
2297 mChannelId = parcel.readString();
2298 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002299 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05002300
2301 if (parcel.readInt() != 0) {
2302 mShortcutId = parcel.readString();
2303 }
2304
Felipe Leme90205ef2019-03-05 09:59:52 -08002305 if (parcel.readInt() != 0) {
2306 mLocusId = LocusId.CREATOR.createFromParcel(parcel);
2307 }
2308
Julia Reynolds13d898c2017-02-02 12:22:05 -05002309 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04002310
2311 if (parcel.readInt() != 0) {
2312 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2313 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002314
2315 mGroupAlertBehavior = parcel.readInt();
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002316 if (parcel.readInt() != 0) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002317 mBubbleMetadata = BubbleMetadata.CREATOR.createFromParcel(parcel);
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002318 }
Gustav Sennton761884c2018-11-19 17:40:19 +00002319
2320 mAllowSystemGeneratedContextualActions = parcel.readBoolean();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002321 }
2322
Andy Stadler110988c2010-12-03 14:29:16 -08002323 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07002324 public Notification clone() {
2325 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04002326 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002327 return that;
2328 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002329
Daniel Sandler1a497d32013-04-18 14:52:45 -04002330 /**
2331 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
2332 * of this into that.
2333 * @hide
2334 */
2335 public void cloneInto(Notification that, boolean heavy) {
Adrian Roosfb921842017-10-26 14:49:56 +02002336 that.mWhitelistToken = this.mWhitelistToken;
Joe Onorato18e69df2010-05-17 22:26:12 -07002337 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002338 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002339 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07002340 that.number = this.number;
2341
2342 // PendingIntents are global, so there's no reason (or way) to clone them.
2343 that.contentIntent = this.contentIntent;
2344 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002345 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07002346
2347 if (this.tickerText != null) {
2348 that.tickerText = this.tickerText.toString();
2349 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002350 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002351 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04002352 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002353 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07002354 that.contentView = this.contentView.clone();
2355 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002356 if (heavy && this.mLargeIcon != null) {
2357 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08002358 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01002359 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07002360 that.sound = this.sound; // android.net.Uri is immutable
2361 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002362 if (this.audioAttributes != null) {
2363 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
2364 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002365
2366 final long[] vibrate = this.vibrate;
2367 if (vibrate != null) {
2368 final int N = vibrate.length;
2369 final long[] vib = that.vibrate = new long[N];
2370 System.arraycopy(vibrate, 0, vib, 0, N);
2371 }
2372
2373 that.ledARGB = this.ledARGB;
2374 that.ledOnMS = this.ledOnMS;
2375 that.ledOffMS = this.ledOffMS;
2376 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002377
Joe Onorato18e69df2010-05-17 22:26:12 -07002378 that.flags = this.flags;
2379
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002380 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08002381
John Spurlockfd7f1e02014-03-18 16:41:57 -04002382 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002383
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002384 that.mGroupKey = this.mGroupKey;
2385
2386 that.mSortKey = this.mSortKey;
2387
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002388 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002389 try {
2390 that.extras = new Bundle(this.extras);
2391 // will unparcel
2392 that.extras.size();
2393 } catch (BadParcelableException e) {
2394 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
2395 that.extras = null;
2396 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002397 }
2398
Felipe Lemedd85da62016-06-28 11:29:54 -07002399 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2400 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002401 }
2402
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002403 if (this.actions != null) {
2404 that.actions = new Action[this.actions.length];
2405 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002406 if ( this.actions[i] != null) {
2407 that.actions[i] = this.actions[i].clone();
2408 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002409 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002410 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002411
Daniel Sandler1a497d32013-04-18 14:52:45 -04002412 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002413 that.bigContentView = this.bigContentView.clone();
2414 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002415
Chris Wren8fd39ec2014-02-27 17:43:26 -05002416 if (heavy && this.headsUpContentView != null) {
2417 that.headsUpContentView = this.headsUpContentView.clone();
2418 }
2419
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002420 that.visibility = this.visibility;
2421
2422 if (this.publicVersion != null) {
2423 that.publicVersion = new Notification();
2424 this.publicVersion.cloneInto(that.publicVersion, heavy);
2425 }
2426
Dan Sandler26e81cf2014-05-06 10:01:27 -04002427 that.color = this.color;
2428
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002429 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002430 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002431 that.mShortcutId = this.mShortcutId;
Felipe Leme90205ef2019-03-05 09:59:52 -08002432 that.mLocusId = this.mLocusId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002433 that.mBadgeIcon = this.mBadgeIcon;
2434 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002435 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002436 that.mBubbleMetadata = this.mBubbleMetadata;
Gustav Sennton761884c2018-11-19 17:40:19 +00002437 that.mAllowSystemGeneratedContextualActions = this.mAllowSystemGeneratedContextualActions;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002438
Daniel Sandler1a497d32013-04-18 14:52:45 -04002439 if (!heavy) {
2440 that.lightenPayload(); // will clean out extras
2441 }
2442 }
2443
2444 /**
Jeff Sharkey23b31182018-04-18 21:32:12 -06002445 * Note all {@link Uri} that are referenced internally, with the expectation
2446 * that Uri permission grants will need to be issued to ensure the recipient
2447 * of this object is able to render its contents.
2448 *
2449 * @hide
2450 */
2451 public void visitUris(@NonNull Consumer<Uri> visitor) {
2452 visitor.accept(sound);
2453
2454 if (tickerView != null) tickerView.visitUris(visitor);
2455 if (contentView != null) contentView.visitUris(visitor);
2456 if (bigContentView != null) bigContentView.visitUris(visitor);
2457 if (headsUpContentView != null) headsUpContentView.visitUris(visitor);
2458
2459 if (extras != null) {
2460 visitor.accept(extras.getParcelable(EXTRA_AUDIO_CONTENTS_URI));
Robin Leed107af62018-04-27 13:55:56 +02002461 if (extras.containsKey(EXTRA_BACKGROUND_IMAGE_URI)) {
2462 visitor.accept(Uri.parse(extras.getString(EXTRA_BACKGROUND_IMAGE_URI)));
2463 }
Jeff Sharkey23b31182018-04-18 21:32:12 -06002464 }
2465
2466 if (MessagingStyle.class.equals(getNotificationStyle()) && extras != null) {
2467 final Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
2468 if (!ArrayUtils.isEmpty(messages)) {
2469 for (MessagingStyle.Message message : MessagingStyle.Message
2470 .getMessagesFromBundleArray(messages)) {
2471 visitor.accept(message.getDataUri());
2472 }
2473 }
2474
2475 final Parcelable[] historic = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
2476 if (!ArrayUtils.isEmpty(historic)) {
2477 for (MessagingStyle.Message message : MessagingStyle.Message
2478 .getMessagesFromBundleArray(historic)) {
2479 visitor.accept(message.getDataUri());
2480 }
2481 }
2482 }
2483 }
2484
2485 /**
Daniel Sandler1a497d32013-04-18 14:52:45 -04002486 * Removes heavyweight parts of the Notification object for archival or for sending to
2487 * listeners when the full contents are not necessary.
2488 * @hide
2489 */
2490 public final void lightenPayload() {
2491 tickerView = null;
2492 contentView = null;
2493 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002494 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002495 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002496 if (extras != null && !extras.isEmpty()) {
2497 final Set<String> keyset = extras.keySet();
2498 final int N = keyset.size();
2499 final String[] keys = keyset.toArray(new String[N]);
2500 for (int i=0; i<N; i++) {
2501 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002502 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2503 continue;
2504 }
Dan Sandler50128532015-12-08 15:42:41 -05002505 final Object obj = extras.get(key);
2506 if (obj != null &&
2507 ( obj instanceof Parcelable
2508 || obj instanceof Parcelable[]
2509 || obj instanceof SparseArray
2510 || obj instanceof ArrayList)) {
2511 extras.remove(key);
2512 }
2513 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002514 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002515 }
2516
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002517 /**
2518 * Make sure this CharSequence is safe to put into a bundle, which basically
2519 * means it had better not be some custom Parcelable implementation.
2520 * @hide
2521 */
2522 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002523 if (cs == null) return cs;
2524 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2525 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2526 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002527 if (cs instanceof Parcelable) {
2528 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2529 + " instance is a custom Parcelable and not allowed in Notification");
2530 return cs.toString();
2531 }
Selim Cinek60a54252016-02-26 17:03:25 -08002532 return removeTextSizeSpans(cs);
2533 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002534
Selim Cinek60a54252016-02-26 17:03:25 -08002535 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2536 if (charSequence instanceof Spanned) {
2537 Spanned ss = (Spanned) charSequence;
2538 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2539 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2540 for (Object span : spans) {
2541 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002542 if (resultSpan instanceof CharacterStyle) {
2543 resultSpan = ((CharacterStyle) span).getUnderlying();
2544 }
2545 if (resultSpan instanceof TextAppearanceSpan) {
2546 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002547 resultSpan = new TextAppearanceSpan(
2548 originalSpan.getFamily(),
2549 originalSpan.getTextStyle(),
2550 -1,
2551 originalSpan.getTextColor(),
2552 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002553 } else if (resultSpan instanceof RelativeSizeSpan
2554 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002555 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002556 } else {
2557 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002558 }
2559 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2560 ss.getSpanFlags(span));
2561 }
2562 return builder;
2563 }
2564 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002565 }
2566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002567 public int describeContents() {
2568 return 0;
2569 }
2570
2571 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002572 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002573 */
Svet Ganovddb94882016-06-23 19:55:24 -07002574 public void writeToParcel(Parcel parcel, int flags) {
2575 // We need to mark all pending intents getting into the notification
2576 // system as being put there to later allow the notification ranker
2577 // to launch them and by doing so add the app to the battery saver white
2578 // list for a short period of time. The problem is that the system
2579 // cannot look into the extras as there may be parcelables there that
2580 // the platform does not know how to handle. To go around that we have
2581 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002582 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002583 if (collectPendingIntents) {
2584 PendingIntent.setOnMarshaledListener(
2585 (PendingIntent intent, Parcel out, int outFlags) -> {
2586 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002587 if (allPendingIntents == null) {
2588 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002589 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002590 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002591 }
2592 });
2593 }
2594 try {
2595 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002596 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002597 writeToParcelImpl(parcel, flags);
2598 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002599 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002600 } finally {
2601 if (collectPendingIntents) {
2602 PendingIntent.setOnMarshaledListener(null);
2603 }
2604 }
2605 }
2606
2607 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002608 parcel.writeInt(1);
2609
Adrian Roosfb921842017-10-26 14:49:56 +02002610 parcel.writeStrongBinder(mWhitelistToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002611 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002612 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002613 if (mSmallIcon == null && icon != 0) {
2614 // you snuck an icon in here without using the builder; let's try to keep it
2615 mSmallIcon = Icon.createWithResource("", icon);
2616 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002617 if (mSmallIcon != null) {
2618 parcel.writeInt(1);
2619 mSmallIcon.writeToParcel(parcel, 0);
2620 } else {
2621 parcel.writeInt(0);
2622 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002623 parcel.writeInt(number);
2624 if (contentIntent != null) {
2625 parcel.writeInt(1);
2626 contentIntent.writeToParcel(parcel, 0);
2627 } else {
2628 parcel.writeInt(0);
2629 }
2630 if (deleteIntent != null) {
2631 parcel.writeInt(1);
2632 deleteIntent.writeToParcel(parcel, 0);
2633 } else {
2634 parcel.writeInt(0);
2635 }
2636 if (tickerText != null) {
2637 parcel.writeInt(1);
2638 TextUtils.writeToParcel(tickerText, parcel, flags);
2639 } else {
2640 parcel.writeInt(0);
2641 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002642 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002643 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002644 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002645 } else {
2646 parcel.writeInt(0);
2647 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002648 if (contentView != null) {
2649 parcel.writeInt(1);
2650 contentView.writeToParcel(parcel, 0);
2651 } else {
2652 parcel.writeInt(0);
2653 }
Selim Cinek279fa862016-06-14 10:57:25 -07002654 if (mLargeIcon == null && largeIcon != null) {
2655 // you snuck an icon in here without using the builder; let's try to keep it
2656 mLargeIcon = Icon.createWithBitmap(largeIcon);
2657 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002658 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002659 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002660 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002661 } else {
2662 parcel.writeInt(0);
2663 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002664
2665 parcel.writeInt(defaults);
2666 parcel.writeInt(this.flags);
2667
2668 if (sound != null) {
2669 parcel.writeInt(1);
2670 sound.writeToParcel(parcel, 0);
2671 } else {
2672 parcel.writeInt(0);
2673 }
2674 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002675
2676 if (audioAttributes != null) {
2677 parcel.writeInt(1);
2678 audioAttributes.writeToParcel(parcel, 0);
2679 } else {
2680 parcel.writeInt(0);
2681 }
2682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002683 parcel.writeLongArray(vibrate);
2684 parcel.writeInt(ledARGB);
2685 parcel.writeInt(ledOnMS);
2686 parcel.writeInt(ledOffMS);
2687 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002688
2689 if (fullScreenIntent != null) {
2690 parcel.writeInt(1);
2691 fullScreenIntent.writeToParcel(parcel, 0);
2692 } else {
2693 parcel.writeInt(0);
2694 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002695
2696 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002697
John Spurlockfd7f1e02014-03-18 16:41:57 -04002698 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002699
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002700 parcel.writeString(mGroupKey);
2701
2702 parcel.writeString(mSortKey);
2703
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002704 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002705
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002706 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002707
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002708 if (bigContentView != null) {
2709 parcel.writeInt(1);
2710 bigContentView.writeToParcel(parcel, 0);
2711 } else {
2712 parcel.writeInt(0);
2713 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002714
Chris Wren8fd39ec2014-02-27 17:43:26 -05002715 if (headsUpContentView != null) {
2716 parcel.writeInt(1);
2717 headsUpContentView.writeToParcel(parcel, 0);
2718 } else {
2719 parcel.writeInt(0);
2720 }
2721
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002722 parcel.writeInt(visibility);
2723
2724 if (publicVersion != null) {
2725 parcel.writeInt(1);
2726 publicVersion.writeToParcel(parcel, 0);
2727 } else {
2728 parcel.writeInt(0);
2729 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002730
2731 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002732
2733 if (mChannelId != null) {
2734 parcel.writeInt(1);
2735 parcel.writeString(mChannelId);
2736 } else {
2737 parcel.writeInt(0);
2738 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002739 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002740
2741 if (mShortcutId != null) {
2742 parcel.writeInt(1);
2743 parcel.writeString(mShortcutId);
2744 } else {
2745 parcel.writeInt(0);
2746 }
2747
Felipe Leme90205ef2019-03-05 09:59:52 -08002748 if (mLocusId != null) {
2749 parcel.writeInt(1);
2750 mLocusId.writeToParcel(parcel, 0);
2751 } else {
2752 parcel.writeInt(0);
2753 }
2754
Julia Reynolds13d898c2017-02-02 12:22:05 -05002755 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002756
2757 if (mSettingsText != null) {
2758 parcel.writeInt(1);
2759 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2760 } else {
2761 parcel.writeInt(0);
2762 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002763
2764 parcel.writeInt(mGroupAlertBehavior);
Julia Reynoldsfc640012018-02-21 12:25:27 -05002765
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002766 if (mBubbleMetadata != null) {
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002767 parcel.writeInt(1);
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002768 mBubbleMetadata.writeToParcel(parcel, 0);
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002769 } else {
2770 parcel.writeInt(0);
2771 }
2772
Gustav Sennton761884c2018-11-19 17:40:19 +00002773 parcel.writeBoolean(mAllowSystemGeneratedContextualActions);
2774
Julia Reynoldsfc640012018-02-21 12:25:27 -05002775 // mUsesStandardHeader is not written because it should be recomputed in listeners
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002776 }
2777
2778 /**
2779 * Parcelable.Creator that instantiates Notification objects
2780 */
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07002781 public static final @android.annotation.NonNull Parcelable.Creator<Notification> CREATOR
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002782 = new Parcelable.Creator<Notification>()
2783 {
2784 public Notification createFromParcel(Parcel parcel)
2785 {
2786 return new Notification(parcel);
2787 }
2788
2789 public Notification[] newArray(int size)
2790 {
2791 return new Notification[size];
2792 }
2793 };
2794
2795 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05002796 * @hide
2797 */
2798 public static boolean areActionsVisiblyDifferent(Notification first, Notification second) {
2799 Notification.Action[] firstAs = first.actions;
2800 Notification.Action[] secondAs = second.actions;
2801 if (firstAs == null && secondAs != null || firstAs != null && secondAs == null) {
2802 return true;
2803 }
2804 if (firstAs != null && secondAs != null) {
2805 if (firstAs.length != secondAs.length) {
2806 return true;
2807 }
2808 for (int i = 0; i < firstAs.length; i++) {
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -04002809 if (!Objects.equals(String.valueOf(firstAs[i].title),
2810 String.valueOf(secondAs[i].title))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05002811 return true;
2812 }
2813 RemoteInput[] firstRs = firstAs[i].getRemoteInputs();
2814 RemoteInput[] secondRs = secondAs[i].getRemoteInputs();
2815 if (firstRs == null) {
2816 firstRs = new RemoteInput[0];
2817 }
2818 if (secondRs == null) {
2819 secondRs = new RemoteInput[0];
2820 }
2821 if (firstRs.length != secondRs.length) {
2822 return true;
2823 }
2824 for (int j = 0; j < firstRs.length; j++) {
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -04002825 if (!Objects.equals(String.valueOf(firstRs[j].getLabel()),
2826 String.valueOf(secondRs[j].getLabel()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05002827 return true;
2828 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05002829 }
2830 }
2831 }
2832 return false;
2833 }
2834
2835 /**
2836 * @hide
2837 */
2838 public static boolean areStyledNotificationsVisiblyDifferent(Builder first, Builder second) {
2839 if (first.getStyle() == null) {
2840 return second.getStyle() != null;
2841 }
2842 if (second.getStyle() == null) {
2843 return true;
2844 }
2845 return first.getStyle().areNotificationsVisiblyDifferent(second.getStyle());
2846 }
2847
2848 /**
2849 * @hide
2850 */
2851 public static boolean areRemoteViewsChanged(Builder first, Builder second) {
Julia Reynoldse5c60452018-04-30 14:41:36 -04002852 if (!Objects.equals(first.usesStandardHeader(), second.usesStandardHeader())) {
2853 return true;
2854 }
2855
2856 if (areRemoteViewsChanged(first.mN.contentView, second.mN.contentView)) {
2857 return true;
2858 }
2859 if (areRemoteViewsChanged(first.mN.bigContentView, second.mN.bigContentView)) {
2860 return true;
2861 }
2862 if (areRemoteViewsChanged(first.mN.headsUpContentView, second.mN.headsUpContentView)) {
2863 return true;
2864 }
2865
2866 return false;
2867 }
2868
2869 private static boolean areRemoteViewsChanged(RemoteViews first, RemoteViews second) {
2870 if (first == null && second == null) {
2871 return false;
2872 }
2873 if (first == null && second != null || first != null && second == null) {
2874 return true;
2875 }
2876
2877 if (!Objects.equals(first.getLayoutId(), second.getLayoutId())) {
2878 return true;
2879 }
2880
2881 if (!Objects.equals(first.getSequenceNumber(), second.getSequenceNumber())) {
2882 return true;
2883 }
2884
2885 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05002886 }
2887
2888 /**
Robin Leead7e72a2017-12-04 15:45:46 +01002889 * Parcelling creates multiple copies of objects in {@code extras}. Fix them.
2890 * <p>
2891 * For backwards compatibility {@code extras} holds some references to "real" member data such
2892 * as {@link getLargeIcon()} which is mirrored by {@link #EXTRA_LARGE_ICON}. This is mostly
2893 * fine as long as the object stays in one process.
2894 * <p>
2895 * However, once the notification goes into a parcel each reference gets marshalled separately,
2896 * wasting memory. Especially with large images on Auto and TV, this is worth fixing.
2897 */
2898 private void fixDuplicateExtras() {
2899 if (extras != null) {
Robin Leead7e72a2017-12-04 15:45:46 +01002900 fixDuplicateExtra(mLargeIcon, EXTRA_LARGE_ICON);
2901 }
2902 }
2903
2904 /**
2905 * If we find an extra that's exactly the same as one of the "real" fields but refers to a
2906 * separate object, replace it with the field's version to avoid holding duplicate copies.
2907 */
2908 private void fixDuplicateExtra(@Nullable Parcelable original, @NonNull String extraName) {
2909 if (original != null && extras.getParcelable(extraName) != null) {
2910 extras.putParcelable(extraName, original);
2911 }
2912 }
2913
2914 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002915 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2916 * layout.
2917 *
2918 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2919 * in the view.</p>
2920 * @param context The context for your application / activity.
2921 * @param contentTitle The title that goes in the expanded entry.
2922 * @param contentText The text that goes in the expanded entry.
2923 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2924 * If this is an activity, it must include the
2925 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002926 * that you take care of task management as described in the
2927 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2928 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002929 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002930 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002931 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002932 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002933 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002934 public void setLatestEventInfo(Context context,
2935 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002936 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2937 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2938 new Throwable());
2939 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002940
Selim Cinek4ac6f602016-06-13 15:47:03 -07002941 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2942 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2943 }
2944
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002945 // ensure that any information already set directly is preserved
2946 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002947
2948 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002949 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002950 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951 }
2952 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002953 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002954 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002955 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002956
2957 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002958 }
2959
Julia Reynoldsda303542015-11-23 14:00:20 -05002960 /**
2961 * @hide
2962 */
2963 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002964 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002965 }
2966
2967 /**
2968 * @hide
2969 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002970 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002971 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002972 }
2973
Yi Jin6b514142017-10-30 14:54:12 -07002974 /**
2975 * @hide
2976 */
2977 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2978 long token = proto.start(fieldId);
2979 proto.write(NotificationProto.CHANNEL_ID, getChannelId());
2980 proto.write(NotificationProto.HAS_TICKER_TEXT, this.tickerText != null);
2981 proto.write(NotificationProto.FLAGS, this.flags);
2982 proto.write(NotificationProto.COLOR, this.color);
2983 proto.write(NotificationProto.CATEGORY, this.category);
2984 proto.write(NotificationProto.GROUP_KEY, this.mGroupKey);
2985 proto.write(NotificationProto.SORT_KEY, this.mSortKey);
2986 if (this.actions != null) {
2987 proto.write(NotificationProto.ACTION_LENGTH, this.actions.length);
2988 }
2989 if (this.visibility >= VISIBILITY_SECRET && this.visibility <= VISIBILITY_PUBLIC) {
2990 proto.write(NotificationProto.VISIBILITY, this.visibility);
2991 }
2992 if (publicVersion != null) {
2993 publicVersion.writeToProto(proto, NotificationProto.PUBLIC_VERSION);
2994 }
2995 proto.end(token);
2996 }
2997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002998 @Override
2999 public String toString() {
3000 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04003001 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04003002 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04003003 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003004 sb.append(priority);
3005 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08003006 if (contentView != null) {
3007 sb.append(contentView.getPackage());
3008 sb.append("/0x");
3009 sb.append(Integer.toHexString(contentView.getLayoutId()));
3010 } else {
3011 sb.append("null");
3012 }
3013 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05003014 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
3015 sb.append("default");
3016 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003017 int N = this.vibrate.length-1;
3018 sb.append("[");
3019 for (int i=0; i<N; i++) {
3020 sb.append(this.vibrate[i]);
3021 sb.append(',');
3022 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02003023 if (N != -1) {
3024 sb.append(this.vibrate[N]);
3025 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003026 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003027 } else {
3028 sb.append("null");
3029 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003030 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05003031 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003032 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05003033 } else if (this.sound != null) {
3034 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003035 } else {
3036 sb.append("null");
3037 }
Chris Wren365b6d32015-07-16 10:39:26 -04003038 if (this.tickerText != null) {
3039 sb.append(" tick");
3040 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003041 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003042 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003043 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04003044 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04003045 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003046 if (this.category != null) {
3047 sb.append(" category=");
3048 sb.append(this.category);
3049 }
3050 if (this.mGroupKey != null) {
3051 sb.append(" groupKey=");
3052 sb.append(this.mGroupKey);
3053 }
3054 if (this.mSortKey != null) {
3055 sb.append(" sortKey=");
3056 sb.append(this.mSortKey);
3057 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003058 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04003059 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003060 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04003061 }
3062 sb.append(" vis=");
3063 sb.append(visibilityToString(this.visibility));
3064 if (this.publicVersion != null) {
3065 sb.append(" publicVersion=");
3066 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003067 }
Felipe Leme90205ef2019-03-05 09:59:52 -08003068 if (this.mLocusId != null) {
3069 sb.append(" locusId=");
3070 sb.append(this.mLocusId); // LocusId.toString() is PII safe.
3071 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003072 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003073 return sb.toString();
3074 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003075
Dan Sandler1b718782014-07-18 12:43:45 -04003076 /**
3077 * {@hide}
3078 */
3079 public static String visibilityToString(int vis) {
3080 switch (vis) {
3081 case VISIBILITY_PRIVATE:
3082 return "PRIVATE";
3083 case VISIBILITY_PUBLIC:
3084 return "PUBLIC";
3085 case VISIBILITY_SECRET:
3086 return "SECRET";
3087 default:
3088 return "UNKNOWN(" + String.valueOf(vis) + ")";
3089 }
3090 }
3091
Joe Onoratocb109a02011-01-18 17:57:41 -08003092 /**
John Spurlock1d881a12015-03-18 19:21:54 -04003093 * {@hide}
3094 */
3095 public static String priorityToString(@Priority int pri) {
3096 switch (pri) {
3097 case PRIORITY_MIN:
3098 return "MIN";
3099 case PRIORITY_LOW:
3100 return "LOW";
3101 case PRIORITY_DEFAULT:
3102 return "DEFAULT";
3103 case PRIORITY_HIGH:
3104 return "HIGH";
3105 case PRIORITY_MAX:
3106 return "MAX";
3107 default:
3108 return "UNKNOWN(" + String.valueOf(pri) + ")";
3109 }
3110 }
3111
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04003112 /**
3113 * @hide
3114 */
3115 public boolean hasCompletedProgress() {
3116 // not a progress notification; can't be complete
3117 if (!extras.containsKey(EXTRA_PROGRESS)
3118 || !extras.containsKey(EXTRA_PROGRESS_MAX)) {
3119 return false;
3120 }
3121 // many apps use max 0 for 'indeterminate'; not complete
3122 if (extras.getInt(EXTRA_PROGRESS_MAX) == 0) {
3123 return false;
3124 }
3125 return extras.getInt(EXTRA_PROGRESS) == extras.getInt(EXTRA_PROGRESS_MAX);
3126 }
3127
Jeff Sharkey000ce802017-04-29 13:13:27 -06003128 /** @removed */
3129 @Deprecated
Julia Reynolds37856052016-11-11 09:20:07 -05003130 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003131 return mChannelId;
3132 }
3133
3134 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003135 * Returns the id of the channel this notification posts to.
3136 */
3137 public String getChannelId() {
3138 return mChannelId;
3139 }
3140
Jeff Sharkey000ce802017-04-29 13:13:27 -06003141 /** @removed */
3142 @Deprecated
Julia Reynolds2a128742016-11-28 14:29:25 -05003143 public long getTimeout() {
3144 return mTimeout;
3145 }
3146
3147 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003148 * Returns the duration from posting after which this notification should be canceled by the
3149 * system, if it's not canceled already.
3150 */
3151 public long getTimeoutAfter() {
3152 return mTimeout;
3153 }
3154
3155 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003156 * Returns what icon should be shown for this notification if it is being displayed in a
3157 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
3158 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
3159 */
3160 public int getBadgeIconType() {
3161 return mBadgeIcon;
3162 }
3163
3164 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003165 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04003166 *
3167 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
3168 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003169 */
3170 public String getShortcutId() {
3171 return mShortcutId;
3172 }
3173
Felipe Leme90205ef2019-03-05 09:59:52 -08003174 /**
3175 * Gets the {@link LocusId} associated with this notification.
3176 *
Felipe Leme8c2360b2019-04-17 11:16:17 -07003177 * <p>Used by the Android system to correlate objects (such as
3178 * {@link ShortcutInfo} and {@link ContentCaptureContext}).
Felipe Leme90205ef2019-03-05 09:59:52 -08003179 */
3180 @Nullable
3181 public LocusId getLocusId() {
3182 return mLocusId;
3183 }
Julia Reynolds3aedded2017-03-31 14:42:09 -04003184
3185 /**
3186 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
3187 */
3188 public CharSequence getSettingsText() {
3189 return mSettingsText;
3190 }
3191
Julia Reynolds13d898c2017-02-02 12:22:05 -05003192 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003193 * Returns which type of notifications in a group are responsible for audibly alerting the
3194 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
3195 * {@link #GROUP_ALERT_SUMMARY}.
3196 */
3197 public @GroupAlertBehavior int getGroupAlertBehavior() {
3198 return mGroupAlertBehavior;
3199 }
3200
3201 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003202 * Returns the bubble metadata that will be used to display app content in a floating window
3203 * over the existing foreground activity.
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003204 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08003205 @Nullable
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003206 public BubbleMetadata getBubbleMetadata() {
3207 return mBubbleMetadata;
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003208 }
3209
Gustav Sennton79ebf4b2018-12-19 10:31:51 +00003210 /**
3211 * Returns whether the platform is allowed (by the app developer) to generate contextual actions
3212 * for this notification.
3213 */
Gustav Sennton761884c2018-11-19 17:40:19 +00003214 public boolean getAllowSystemGeneratedContextualActions() {
3215 return mAllowSystemGeneratedContextualActions;
3216 }
3217
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003218 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003219 * The small icon representing this notification in the status bar and content view.
3220 *
3221 * @return the small icon representing this notification.
3222 *
3223 * @see Builder#getSmallIcon()
3224 * @see Builder#setSmallIcon(Icon)
3225 */
3226 public Icon getSmallIcon() {
3227 return mSmallIcon;
3228 }
3229
3230 /**
3231 * Used when notifying to clean up legacy small icons.
3232 * @hide
3233 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003234 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04003235 public void setSmallIcon(Icon icon) {
3236 mSmallIcon = icon;
3237 }
3238
3239 /**
3240 * The large icon shown in this notification's content view.
3241 * @see Builder#getLargeIcon()
3242 * @see Builder#setLargeIcon(Icon)
3243 */
3244 public Icon getLargeIcon() {
3245 return mLargeIcon;
3246 }
3247
3248 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02003249 * @hide
3250 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003251 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003252 public boolean isGroupSummary() {
3253 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
3254 }
3255
3256 /**
3257 * @hide
3258 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003259 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003260 public boolean isGroupChild() {
3261 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
3262 }
3263
3264 /**
Julia Reynolds30203152017-05-26 13:36:31 -04003265 * @hide
3266 */
3267 public boolean suppressAlertingDueToGrouping() {
3268 if (isGroupSummary()
3269 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
3270 return true;
3271 } else if (isGroupChild()
3272 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
3273 return true;
3274 }
3275 return false;
3276 }
3277
Tony Mak638430e2018-10-08 19:19:10 +01003278
3279 /**
3280 * Finds and returns a remote input and its corresponding action.
3281 *
3282 * @param requiresFreeform requires the remoteinput to allow freeform or not.
3283 * @return the result pair, {@code null} if no result is found.
3284 *
3285 * @hide
3286 */
3287 @Nullable
3288 public Pair<RemoteInput, Action> findRemoteInputActionPair(boolean requiresFreeform) {
3289 if (actions == null) {
3290 return null;
3291 }
3292 for (Notification.Action action : actions) {
3293 if (action.getRemoteInputs() == null) {
3294 continue;
3295 }
3296 RemoteInput resultRemoteInput = null;
3297 for (RemoteInput remoteInput : action.getRemoteInputs()) {
3298 if (remoteInput.getAllowFreeFormInput() || !requiresFreeform) {
3299 resultRemoteInput = remoteInput;
3300 }
3301 }
3302 if (resultRemoteInput != null) {
3303 return Pair.create(resultRemoteInput, action);
3304 }
3305 }
3306 return null;
3307 }
3308
Julia Reynolds30203152017-05-26 13:36:31 -04003309 /**
Gustav Sennton005d7a02019-01-04 13:41:32 +00003310 * Returns the actions that are contextual out of the actions in this notification.
Gustav Senntoneab53682018-11-01 16:30:23 +00003311 *
3312 * @hide
3313 */
3314 public List<Notification.Action> getContextualActions() {
3315 if (actions == null) return Collections.emptyList();
3316
3317 List<Notification.Action> contextualActions = new ArrayList<>();
3318 for (Notification.Action action : actions) {
Gustav Sennton005d7a02019-01-04 13:41:32 +00003319 if (action.isContextual()) {
Gustav Senntoneab53682018-11-01 16:30:23 +00003320 contextualActions.add(action);
3321 }
3322 }
3323 return contextualActions;
3324 }
3325
3326 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003327 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08003328 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003329 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07003330 * content views using the platform's notification layout template. If your app supports
3331 * versions of Android as old as API level 4, you can instead use
3332 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
3333 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
3334 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08003335 *
Scott Main183bf112012-08-13 19:12:13 -07003336 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08003337 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003338 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07003339 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003340 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3341 * .setContentText(subject)
3342 * .setSmallIcon(R.drawable.new_mail)
3343 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003344 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003345 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08003346 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003347 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003348 /**
3349 * @hide
3350 */
3351 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
3352 "android.rebuild.contentViewActionCount";
3353 /**
3354 * @hide
3355 */
3356 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
3357 = "android.rebuild.bigViewActionCount";
3358 /**
3359 * @hide
3360 */
3361 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
3362 = "android.rebuild.hudViewActionCount";
3363
Selim Cinek6743c0b2017-01-18 18:24:01 -08003364 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
3365 SystemProperties.getBoolean("notifications.only_title", true);
3366
Selim Cinek389edcd2017-05-11 19:16:44 -07003367 /**
3368 * The lightness difference that has to be added to the primary text color to obtain the
3369 * secondary text color when the background is light.
3370 */
3371 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
3372
3373 /**
3374 * The lightness difference that has to be added to the primary text color to obtain the
3375 * secondary text color when the background is dark.
3376 * A bit less then the above value, since it looks better on dark backgrounds.
3377 */
3378 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
3379
Joe Onorato46439ce2010-11-19 13:56:21 -08003380 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003381 private Notification mN;
3382 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003383 private Style mStyle;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003384 @UnsupportedAppUsage
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003385 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Selim Cineke7238dd2017-12-14 17:48:32 -08003386 private ArrayList<Person> mPersonList = new ArrayList<>();
Lucas Dupina291d192018-06-07 13:59:42 -07003387 private ContrastColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08003388 private boolean mIsLegacy;
3389 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02003390
3391 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08003392 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
3393 */
3394 private int mCachedContrastColor = COLOR_INVALID;
3395 private int mCachedContrastColorIsFor = COLOR_INVALID;
Selim Cinekc3fec682019-06-06 18:11:07 -07003396
Selim Cinek4717d862018-04-19 09:19:15 +08003397 /**
3398 * A neutral color color that can be used for icons.
3399 */
3400 private int mNeutralColor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08003401
3402 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08003403 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
3404 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
3405 */
3406 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08003407 private int mTextColorsAreForBackground = COLOR_INVALID;
3408 private int mPrimaryTextColor = COLOR_INVALID;
3409 private int mSecondaryTextColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07003410 private int mBackgroundColor = COLOR_INVALID;
3411 private int mForegroundColor = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07003412 /**
3413 * A temporary location where actions are stored. If != null the view originally has action
3414 * but doesn't have any for this inflation.
3415 */
3416 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07003417 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003418
Anthony Chenad4d1582017-04-10 16:07:58 -07003419 private boolean mTintActionButtons;
3420 private boolean mInNightMode;
3421
Adrian Roos70d7aa32017-01-11 15:39:06 -08003422 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003423 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08003424 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003425 * @param context
3426 * A {@link Context} that will be used by the Builder to construct the
3427 * RemoteViews. The Context will not be held past the lifetime of this Builder
3428 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003429 * @param channelId
3430 * The constructed Notification will be posted on this
3431 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
3432 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003433 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003434 public Builder(Context context, String channelId) {
3435 this(context, (Notification) null);
3436 mN.mChannelId = channelId;
3437 }
3438
3439 /**
3440 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
3441 * instead. All posted Notifications must specify a NotificationChannel Id.
3442 */
3443 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003444 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003445 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003446 }
3447
Joe Onoratocb109a02011-01-18 17:57:41 -08003448 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003449 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02003450 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003451 public Builder(Context context, Notification toAdopt) {
3452 mContext = context;
Anthony Chenad4d1582017-04-10 16:07:58 -07003453 Resources res = mContext.getResources();
3454 mTintActionButtons = res.getBoolean(R.bool.config_tintNotificationActionButtons);
3455
3456 if (res.getBoolean(R.bool.config_enableNightMode)) {
3457 Configuration currentConfig = res.getConfiguration();
3458 mInNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
3459 == Configuration.UI_MODE_NIGHT_YES;
3460 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003461
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003462 if (toAdopt == null) {
3463 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003464 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
3465 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
3466 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003467 mN.priority = PRIORITY_DEFAULT;
3468 mN.visibility = VISIBILITY_PRIVATE;
3469 } else {
3470 mN = toAdopt;
3471 if (mN.actions != null) {
3472 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003473 }
3474
Selim Cineke7238dd2017-12-14 17:48:32 -08003475 if (mN.extras.containsKey(EXTRA_PEOPLE_LIST)) {
3476 ArrayList<Person> people = mN.extras.getParcelableArrayList(EXTRA_PEOPLE_LIST);
3477 mPersonList.addAll(people);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003478 }
3479
Selim Cinek4ac6f602016-06-13 15:47:03 -07003480 if (mN.getSmallIcon() == null && mN.icon != 0) {
3481 setSmallIcon(mN.icon);
3482 }
3483
3484 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
3485 setLargeIcon(mN.largeIcon);
3486 }
3487
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003488 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
3489 if (!TextUtils.isEmpty(templateClass)) {
3490 final Class<? extends Style> styleClass
3491 = getNotificationStyleClass(templateClass);
3492 if (styleClass == null) {
3493 Log.d(TAG, "Unknown style class: " + templateClass);
3494 } else {
3495 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003496 final Constructor<? extends Style> ctor =
3497 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003498 ctor.setAccessible(true);
3499 final Style style = ctor.newInstance();
3500 style.restoreFromExtras(mN.extras);
3501
3502 if (style != null) {
3503 setStyle(style);
3504 }
3505 } catch (Throwable t) {
3506 Log.e(TAG, "Could not create Style", t);
3507 }
3508 }
3509 }
3510
3511 }
3512 }
3513
Lucas Dupina291d192018-06-07 13:59:42 -07003514 private ContrastColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08003515 if (mColorUtil == null) {
Lucas Dupina291d192018-06-07 13:59:42 -07003516 mColorUtil = ContrastColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003517 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003518 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003519 }
3520
3521 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003522 * If this notification is duplicative of a Launcher shortcut, sets the
3523 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
3524 * the shortcut.
3525 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04003526 * This field will be ignored by Launchers that don't support badging, don't show
3527 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003528 *
3529 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
3530 * supersedes
3531 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003532 @NonNull
Julia Reynolds13d898c2017-02-02 12:22:05 -05003533 public Builder setShortcutId(String shortcutId) {
3534 mN.mShortcutId = shortcutId;
3535 return this;
3536 }
3537
3538 /**
Felipe Leme90205ef2019-03-05 09:59:52 -08003539 * Sets the {@link LocusId} associated with this notification.
3540 *
3541 * <p>This method should be called when the {@link LocusId} is used in other places (such
Felipe Leme8c2360b2019-04-17 11:16:17 -07003542 * as {@link ShortcutInfo} and {@link ContentCaptureContext}) so the Android system can
3543 * correlate them.
Felipe Leme90205ef2019-03-05 09:59:52 -08003544 */
3545 @NonNull
3546 public Builder setLocusId(@Nullable LocusId locusId) {
3547 mN.mLocusId = locusId;
3548 return this;
3549 }
3550
3551 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003552 * Sets which icon to display as a badge for this notification.
3553 *
3554 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
3555 * {@link #BADGE_ICON_LARGE}.
3556 *
3557 * Note: This value might be ignored, for launchers that don't support badge icons.
3558 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003559 @NonNull
Julia Reynolds612beb22017-03-30 10:48:30 -04003560 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04003561 mN.mBadgeIcon = icon;
3562 return this;
3563 }
3564
3565 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003566 * Sets the group alert behavior for this notification. Use this method to mute this
3567 * notification if alerts for this notification's group should be handled by a different
3568 * notification. This is only applicable for notifications that belong to a
Julia Reynolds399d9bf2017-08-11 12:52:14 -04003569 * {@link #setGroup(String) group}. This must be called on all notifications you want to
3570 * mute. For example, if you want only the summary of your group to make noise, all
3571 * children in the group should have the group alert behavior {@link #GROUP_ALERT_SUMMARY}.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003572 *
3573 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
3574 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003575 @NonNull
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003576 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
3577 mN.mGroupAlertBehavior = groupAlertBehavior;
3578 return this;
3579 }
3580
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003581 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003582 * Sets the {@link BubbleMetadata} that will be used to display app content in a floating
3583 * window over the existing foreground activity.
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003584 *
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003585 * <p>This data will be ignored unless the notification is posted to a channel that
3586 * allows {@link NotificationChannel#canBubble() bubbles}.</p>
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003587 *
Mady Mellor65dcaa92019-04-03 12:21:44 -07003588 * <p>Notifications allowed to bubble that have valid bubble metadata will display in
3589 * collapsed state outside of the notification shade on unlocked devices. When a user
3590 * interacts with the collapsed state, the bubble intent will be invoked and displayed.</p>
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003591 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003592 @NonNull
Mady Mellorcf8f1b22019-03-07 14:08:21 -08003593 public Builder setBubbleMetadata(@Nullable BubbleMetadata data) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003594 mN.mBubbleMetadata = data;
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003595 return this;
3596 }
3597
Jeff Sharkey000ce802017-04-29 13:13:27 -06003598 /** @removed */
3599 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003600 public Builder setChannel(String channelId) {
3601 mN.mChannelId = channelId;
3602 return this;
3603 }
3604
3605 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003606 * Specifies the channel the notification should be delivered on.
3607 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003608 @NonNull
Julia Reynoldsbad42972017-04-25 13:52:49 -04003609 public Builder setChannelId(String channelId) {
3610 mN.mChannelId = channelId;
3611 return this;
3612 }
3613
Jeff Sharkey000ce802017-04-29 13:13:27 -06003614 /** @removed */
3615 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05003616 public Builder setTimeout(long durationMs) {
3617 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05003618 return this;
3619 }
3620
3621 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003622 * Specifies a duration in milliseconds after which this notification should be canceled,
3623 * if it is not already canceled.
3624 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003625 @NonNull
Julia Reynoldsbad42972017-04-25 13:52:49 -04003626 public Builder setTimeoutAfter(long durationMs) {
3627 mN.mTimeout = durationMs;
3628 return this;
3629 }
3630
3631 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003632 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003633 *
3634 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
3635 * shown anymore by default and must be opted into by using
3636 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003637 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003638 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08003639 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003640 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003641 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003642 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08003643 return this;
3644 }
3645
Joe Onoratocb109a02011-01-18 17:57:41 -08003646 /**
Griff Hazen50c11652014-05-16 09:46:31 -07003647 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07003648 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003649 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
3650 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07003651 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003652 @NonNull
Daniel Sandler0c890492012-09-12 17:23:10 -07003653 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003654 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07003655 return this;
3656 }
3657
3658 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003659 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08003660 *
3661 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003662 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003663 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003664 * Useful when showing an elapsed time (like an ongoing phone call).
3665 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08003666 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07003667 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08003668 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003669 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003670 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07003671 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003672 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003673 @NonNull
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003674 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003675 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003676 return this;
3677 }
3678
3679 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08003680 * Sets the Chronometer to count down instead of counting up.
3681 *
3682 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
3683 * If it isn't set the chronometer will count up.
3684 *
3685 * @see #setUsesChronometer(boolean)
3686 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003687 @NonNull
Adrian Roos96b7e202016-05-17 13:50:38 -07003688 public Builder setChronometerCountDown(boolean countDown) {
3689 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003690 return this;
3691 }
3692
3693 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003694 * Set the small icon resource, which will be used to represent the notification in the
3695 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003696 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003697
3698 * The platform template for the expanded view will draw this icon in the left, unless a
3699 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3700 * icon will be moved to the right-hand side.
3701 *
3702
3703 * @param icon
3704 * A resource ID in the application's package of the drawable to use.
3705 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003706 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003707 @NonNull
Tor Norbye7b9c9122013-05-30 16:48:33 -07003708 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003709 return setSmallIcon(icon != 0
3710 ? Icon.createWithResource(mContext, icon)
3711 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003712 }
3713
Joe Onoratocb109a02011-01-18 17:57:41 -08003714 /**
3715 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3716 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3717 * LevelListDrawable}.
3718 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003719 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003720 * @param level The level to use for the icon.
3721 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003722 * @see Notification#icon
3723 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003724 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003725 @NonNull
Tor Norbye7b9c9122013-05-30 16:48:33 -07003726 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003727 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003728 return setSmallIcon(icon);
3729 }
3730
3731 /**
3732 * Set the small icon, which will be used to represent the notification in the
koprivadebd4ee2018-09-13 10:59:46 -07003733 * status bar and content view (unless overridden there by a
Dan Sandlerd63f9322015-05-06 15:18:49 -04003734 * {@link #setLargeIcon(Bitmap) large icon}).
3735 *
3736 * @param icon An Icon object to use.
3737 * @see Notification#icon
3738 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003739 @NonNull
Dan Sandlerd63f9322015-05-06 15:18:49 -04003740 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003741 mN.setSmallIcon(icon);
3742 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3743 mN.icon = icon.getResId();
3744 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003745 return this;
3746 }
3747
Joe Onoratocb109a02011-01-18 17:57:41 -08003748 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003749 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003750 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003751 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003752 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003753 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003754 return this;
3755 }
3756
Joe Onoratocb109a02011-01-18 17:57:41 -08003757 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003758 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003759 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003760 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003761 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003762 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003763 return this;
3764 }
3765
Joe Onoratocb109a02011-01-18 17:57:41 -08003766 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003767 * This provides some additional information that is displayed in the notification. No
3768 * guarantees are given where exactly it is displayed.
3769 *
3770 * <p>This information should only be provided if it provides an essential
3771 * benefit to the understanding of the notification. The more text you provide the
3772 * less readable it becomes. For example, an email client should only provide the account
3773 * name here if more than one email account has been added.</p>
3774 *
3775 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3776 * notification header area.
3777 *
3778 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3779 * this will be shown in the third line of text in the platform notification template.
3780 * You should not be using {@link #setProgress(int, int, boolean)} at the
3781 * same time on those versions; they occupy the same place.
3782 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003783 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003784 @NonNull
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003785 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003786 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003787 return this;
3788 }
3789
3790 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003791 * Provides text that will appear as a link to your application's settings.
3792 *
3793 * <p>This text does not appear within notification {@link Style templates} but may
3794 * appear when the user uses an affordance to learn more about the notification.
3795 * Additionally, this text will not appear unless you provide a valid link target by
3796 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3797 *
3798 * <p>This text is meant to be concise description about what the user can customize
3799 * when they click on this link. The recommended maximum length is 40 characters.
3800 * @param text
3801 * @return
3802 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003803 @NonNull
Julia Reynolds3aedded2017-03-31 14:42:09 -04003804 public Builder setSettingsText(CharSequence text) {
3805 mN.mSettingsText = safeCharSequence(text);
3806 return this;
3807 }
3808
3809 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003810 * Set the remote input history.
3811 *
3812 * This should be set to the most recent inputs that have been sent
3813 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3814 * longer relevant (e.g. for chat notifications once the other party has responded).
3815 *
3816 * The most recent input must be stored at the 0 index, the second most recent at the
3817 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3818 * and how much of each individual input is shown.
3819 *
3820 * <p>Note: The reply text will only be shown on notifications that have least one action
3821 * with a {@code RemoteInput}.</p>
3822 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003823 @NonNull
Adrian Roose458aa82015-12-08 16:17:19 -08003824 public Builder setRemoteInputHistory(CharSequence[] text) {
3825 if (text == null) {
3826 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3827 } else {
3828 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3829 CharSequence[] safe = new CharSequence[N];
3830 for (int i = 0; i < N; i++) {
3831 safe[i] = safeCharSequence(text[i]);
3832 }
3833 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3834 }
3835 return this;
3836 }
3837
3838 /**
Kenny Guya0f6de82018-04-06 16:20:16 +01003839 * Sets whether remote history entries view should have a spinner.
3840 * @hide
3841 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003842 @NonNull
Kenny Guya0f6de82018-04-06 16:20:16 +01003843 public Builder setShowRemoteInputSpinner(boolean showSpinner) {
3844 mN.extras.putBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER, showSpinner);
3845 return this;
3846 }
3847
3848 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +01003849 * Sets whether smart reply buttons should be hidden.
3850 * @hide
3851 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003852 @NonNull
Kenny Guy8cc15d22018-05-09 09:50:55 +01003853 public Builder setHideSmartReplies(boolean hideSmartReplies) {
3854 mN.extras.putBoolean(EXTRA_HIDE_SMART_REPLIES, hideSmartReplies);
3855 return this;
3856 }
3857
3858 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003859 * Sets the number of items this notification represents. May be displayed as a badge count
3860 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003861 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003862 @NonNull
Joe Onorato8595a3d2010-11-19 18:12:07 -08003863 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003864 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003865 return this;
3866 }
3867
Joe Onoratocb109a02011-01-18 17:57:41 -08003868 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003869 * A small piece of additional information pertaining to this notification.
3870 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003871 * The platform template will draw this on the last line of the notification, at the far
3872 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003873 *
3874 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3875 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3876 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003877 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003878 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003879 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003880 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003881 return this;
3882 }
3883
Joe Onoratocb109a02011-01-18 17:57:41 -08003884 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003885 * Set the progress this notification represents.
3886 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003887 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003888 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003889 @NonNull
Jeff Sharkey1c400132011-08-05 14:50:13 -07003890 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003891 mN.extras.putInt(EXTRA_PROGRESS, progress);
3892 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3893 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003894 return this;
3895 }
3896
3897 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003898 * Supply a custom RemoteViews to use instead of the platform template.
3899 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003900 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003901 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003902 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003903 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003904 return setCustomContentView(views);
3905 }
3906
3907 /**
3908 * Supply custom RemoteViews to use instead of the platform template.
3909 *
3910 * This will override the layout that would otherwise be constructed by this Builder
3911 * object.
3912 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003913 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003914 public Builder setCustomContentView(RemoteViews contentView) {
3915 mN.contentView = contentView;
3916 return this;
3917 }
3918
3919 /**
3920 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3921 *
3922 * This will override the expanded layout that would otherwise be constructed by this
3923 * Builder object.
3924 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003925 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003926 public Builder setCustomBigContentView(RemoteViews contentView) {
3927 mN.bigContentView = contentView;
3928 return this;
3929 }
3930
3931 /**
3932 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3933 *
3934 * This will override the heads-up layout that would otherwise be constructed by this
3935 * Builder object.
3936 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003937 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003938 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3939 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003940 return this;
3941 }
3942
Joe Onoratocb109a02011-01-18 17:57:41 -08003943 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003944 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3945 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003946 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3947 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3948 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003949 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003950 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003951 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003952 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003953 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003954 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003955 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003956 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003957 return this;
3958 }
3959
Joe Onoratocb109a02011-01-18 17:57:41 -08003960 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003961 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3962 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003963 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003964 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003965 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003966 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003967 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003968 return this;
3969 }
3970
Joe Onoratocb109a02011-01-18 17:57:41 -08003971 /**
3972 * An intent to launch instead of posting the notification to the status bar.
3973 * Only for use with extremely high-priority notifications demanding the user's
3974 * <strong>immediate</strong> attention, such as an incoming phone call or
3975 * alarm clock that the user has explicitly set to a particular time.
3976 * If this facility is used for something else, please give the user an option
3977 * to turn it off and use a normal notification, as this can be extremely
3978 * disruptive.
3979 *
Chris Wren47c20a12014-06-18 17:27:29 -04003980 * <p>
3981 * The system UI may choose to display a heads-up notification, instead of
3982 * launching this intent, while the user is using the device.
3983 * </p>
Julia Reynolds47fd15f2018-11-28 10:16:00 -05003984 * <p>Apps targeting {@link Build.VERSION_CODES#Q} and above will have to request
3985 * a permission ({@link android.Manifest.permission#USE_FULL_SCREEN_INTENT}) in order to
3986 * use full screen intents.</p>
Chris Wren47c20a12014-06-18 17:27:29 -04003987 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003988 * @param intent The pending intent to launch.
3989 * @param highPriority Passing true will cause this notification to be sent
3990 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003991 *
3992 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003993 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003994 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003995 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003996 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003997 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3998 return this;
3999 }
4000
Joe Onoratocb109a02011-01-18 17:57:41 -08004001 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04004002 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004003 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004004 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08004005 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004006 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08004007 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004008 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08004009 return this;
4010 }
4011
Joe Onoratocb109a02011-01-18 17:57:41 -08004012 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04004013 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004014 *
Joe Onoratocb109a02011-01-18 17:57:41 -08004015 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04004016 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004017 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004018 setTicker(tickerText);
4019 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08004020 return this;
4021 }
4022
Joe Onoratocb109a02011-01-18 17:57:41 -08004023 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04004024 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004025 *
4026 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04004027 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
4028 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04004029 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004030 @NonNull
Dan Sandlerd63f9322015-05-06 15:18:49 -04004031 public Builder setLargeIcon(Bitmap b) {
4032 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
4033 }
4034
4035 /**
4036 * Add a large icon to the notification content view.
4037 *
4038 * In the platform template, this image will be shown on the left of the notification view
4039 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
4040 * badge atop the large icon).
4041 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004042 @NonNull
Dan Sandlerd63f9322015-05-06 15:18:49 -04004043 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004044 mN.mLargeIcon = icon;
4045 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08004046 return this;
4047 }
4048
Joe Onoratocb109a02011-01-18 17:57:41 -08004049 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004050 * Set the sound to play.
4051 *
John Spurlockc0650f022014-07-19 13:22:39 -04004052 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
4053 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004054 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004055 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08004056 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004057 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08004058 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004059 mN.sound = sound;
4060 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08004061 return this;
4062 }
4063
Joe Onoratocb109a02011-01-18 17:57:41 -08004064 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004065 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08004066 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004067 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
4068 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004069 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08004070 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07004071 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004072 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08004073 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004074 mN.sound = sound;
4075 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08004076 return this;
4077 }
4078
Joe Onoratocb109a02011-01-18 17:57:41 -08004079 /**
John Spurlockc0650f022014-07-19 13:22:39 -04004080 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
4081 * use during playback.
4082 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004083 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04004084 * @see Notification#sound
4085 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004086 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04004087 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004088 mN.sound = sound;
4089 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04004090 return this;
4091 }
4092
4093 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08004094 * Set the vibration pattern to use.
4095 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004096 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
4097 * <code>pattern</code> parameter.
4098 *
Chris Wren47c20a12014-06-18 17:27:29 -04004099 * <p>
4100 * A notification that vibrates is more likely to be presented as a heads-up notification.
4101 * </p>
4102 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004103 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004104 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08004105 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004106 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004107 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004108 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08004109 return this;
4110 }
4111
Joe Onoratocb109a02011-01-18 17:57:41 -08004112 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004113 * Set the desired color for the indicator LED on the device, as well as the
4114 * blink duty cycle (specified in milliseconds).
4115 *
4116
4117 * Not all devices will honor all (or even any) of these values.
4118 *
Julia Reynolds529e3322017-02-06 08:33:01 -05004119 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004120 * @see Notification#ledARGB
4121 * @see Notification#ledOnMS
4122 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08004123 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004124 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08004125 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004126 mN.ledARGB = argb;
4127 mN.ledOnMS = onMs;
4128 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004129 if (onMs != 0 || offMs != 0) {
4130 mN.flags |= FLAG_SHOW_LIGHTS;
4131 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004132 return this;
4133 }
4134
Joe Onoratocb109a02011-01-18 17:57:41 -08004135 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004136 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08004137 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004138
4139 * Ongoing notifications cannot be dismissed by the user, so your application or service
4140 * must take care of canceling them.
4141 *
4142
4143 * They are typically used to indicate a background task that the user is actively engaged
4144 * with (e.g., playing music) or is pending in some way and therefore occupying the device
4145 * (e.g., a file download, sync operation, active network connection).
4146 *
4147
4148 * @see Notification#FLAG_ONGOING_EVENT
Joe Onoratocb109a02011-01-18 17:57:41 -08004149 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004150 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08004151 public Builder setOngoing(boolean ongoing) {
4152 setFlag(FLAG_ONGOING_EVENT, ongoing);
4153 return this;
4154 }
4155
Joe Onoratocb109a02011-01-18 17:57:41 -08004156 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08004157 * Set whether this notification should be colorized. When set, the color set with
4158 * {@link #setColor(int)} will be used as the background color of this notification.
4159 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08004160 * This should only be used for high priority ongoing tasks like navigation, an ongoing
4161 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08004162 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07004163 * For most styles, the coloring will only be applied if the notification is for a
4164 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08004165 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07004166 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08004167 *
Aurimas Liutikas7f695332018-05-31 21:07:32 -07004168 * @see #setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08004169 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08004170 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004171 @NonNull
Selim Cinek7b9605b2017-01-19 17:36:00 -08004172 public Builder setColorized(boolean colorize) {
4173 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
4174 return this;
4175 }
4176
4177 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08004178 * Set this flag if you would only like the sound, vibrate
4179 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004180 *
4181 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08004182 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004183 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08004184 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
4185 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
4186 return this;
4187 }
4188
Joe Onoratocb109a02011-01-18 17:57:41 -08004189 /**
Julia Reynolds04499532016-09-13 14:04:53 -04004190 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004191 *
4192 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08004193 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004194 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08004195 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08004196 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08004197 return this;
4198 }
4199
Joe Onoratocb109a02011-01-18 17:57:41 -08004200 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08004201 * Set whether or not this notification should not bridge to other devices.
4202 *
4203 * <p>Some notifications can be bridged to other devices for remote display.
4204 * This hint can be set to recommend this notification not be bridged.
4205 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004206 @NonNull
Griff Hazendfcb0802014-02-11 12:00:00 -08004207 public Builder setLocalOnly(boolean localOnly) {
4208 setFlag(FLAG_LOCAL_ONLY, localOnly);
4209 return this;
4210 }
4211
4212 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004213 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08004214 * <p>
4215 * The value should be one or more of the following fields combined with
4216 * bitwise-or:
4217 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
4218 * <p>
4219 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004220 *
4221 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05004222 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004223 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08004224 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004225 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004226 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004227 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08004228 return this;
4229 }
4230
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004231 /**
4232 * Set the priority of this notification.
4233 *
4234 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004235 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004236 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004237 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07004238 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004239 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004240 return this;
4241 }
Joe Malin8d40d042012-11-05 11:36:40 -08004242
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004243 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04004244 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08004245 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04004246 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004247 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004248 @NonNull
John Spurlockfd7f1e02014-03-18 16:41:57 -04004249 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004250 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004251 return this;
4252 }
4253
4254 /**
Chris Wrendde75302014-03-26 17:24:15 -04004255 * Add a person that is relevant to this notification.
4256 *
Chris Wrene6c48932014-09-29 17:19:27 -04004257 * <P>
4258 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04004259 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4260 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4261 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04004262 * </P>
4263 *
4264 * <P>
4265 * The person should be specified by the {@code String} representation of a
4266 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
4267 * </P>
4268 *
4269 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
4270 * URIs. The path part of these URIs must exist in the contacts database, in the
4271 * appropriate column, or the reference will be discarded as invalid. Telephone schema
4272 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
Selim Cineke7238dd2017-12-14 17:48:32 -08004273 * It is also possible to provide a URI with the schema {@code name:} in order to uniquely
4274 * identify a person without an entry in the contacts database.
Chris Wrene6c48932014-09-29 17:19:27 -04004275 * </P>
4276 *
4277 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04004278 * @see Notification#EXTRA_PEOPLE
Selim Cineke7238dd2017-12-14 17:48:32 -08004279 * @deprecated use {@link #addPerson(Person)}
Chris Wrendde75302014-03-26 17:24:15 -04004280 */
Chris Wrene6c48932014-09-29 17:19:27 -04004281 public Builder addPerson(String uri) {
Selim Cinek9acd6732018-03-23 16:39:02 -07004282 addPerson(new Person.Builder().setUri(uri).build());
Selim Cineke7238dd2017-12-14 17:48:32 -08004283 return this;
4284 }
4285
4286 /**
4287 * Add a person that is relevant to this notification.
4288 *
4289 * <P>
4290 * Depending on user preferences, this annotation may allow the notification to pass
4291 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4292 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4293 * appear more prominently in the user interface.
4294 * </P>
4295 *
4296 * <P>
4297 * A person should usually contain a uri in order to benefit from the ranking boost.
4298 * However, even if no uri is provided, it's beneficial to provide other people in the
4299 * notification, such that listeners and voice only devices can announce and handle them
4300 * properly.
4301 * </P>
4302 *
4303 * @param person the person to add.
4304 * @see Notification#EXTRA_PEOPLE_LIST
4305 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004306 @NonNull
Selim Cineke7238dd2017-12-14 17:48:32 -08004307 public Builder addPerson(Person person) {
4308 mPersonList.add(person);
Chris Wrendde75302014-03-26 17:24:15 -04004309 return this;
4310 }
4311
4312 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004313 * Set this notification to be part of a group of notifications sharing the same key.
4314 * Grouped notifications may display in a cluster or stack on devices which
4315 * support such rendering.
4316 *
4317 * <p>To make this notification the summary for its group, also call
4318 * {@link #setGroupSummary}. A sort order can be specified for group members by using
4319 * {@link #setSortKey}.
4320 * @param groupKey The group key of the group.
4321 * @return this object for method chaining
4322 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004323 @NonNull
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004324 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004325 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004326 return this;
4327 }
4328
4329 /**
4330 * Set this notification to be the group summary for a group of notifications.
4331 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04004332 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
4333 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004334 * @param isGroupSummary Whether this notification should be a group summary.
4335 * @return this object for method chaining
4336 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004337 @NonNull
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004338 public Builder setGroupSummary(boolean isGroupSummary) {
4339 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
4340 return this;
4341 }
4342
4343 /**
4344 * Set a sort key that orders this notification among other notifications from the
4345 * same package. This can be useful if an external sort was already applied and an app
4346 * would like to preserve this. Notifications will be sorted lexicographically using this
4347 * value, although providing different priorities in addition to providing sort key may
4348 * cause this value to be ignored.
4349 *
4350 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07004351 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004352 *
4353 * @see String#compareTo(String)
4354 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004355 @NonNull
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004356 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004357 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004358 return this;
4359 }
4360
4361 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004362 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004363 *
Griff Hazen720042b2014-02-24 15:46:56 -08004364 * <p>Values within the Bundle will replace existing extras values in this Builder.
4365 *
4366 * @see Notification#extras
4367 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004368 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07004369 public Builder addExtras(Bundle extras) {
4370 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004371 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08004372 }
4373 return this;
4374 }
4375
4376 /**
4377 * Set metadata for this notification.
4378 *
4379 * <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 -04004380 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004381 * called.
4382 *
Griff Hazen720042b2014-02-24 15:46:56 -08004383 * <p>Replaces any existing extras values with those from the provided Bundle.
4384 * Use {@link #addExtras} to merge in metadata instead.
4385 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004386 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004387 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004388 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07004389 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004390 if (extras != null) {
4391 mUserExtras = extras;
4392 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004393 return this;
4394 }
4395
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004396 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004397 * Get the current metadata Bundle used by this notification Builder.
4398 *
4399 * <p>The returned Bundle is shared with this Builder.
4400 *
4401 * <p>The current contents of this Bundle are copied into the Notification each time
4402 * {@link #build()} is called.
4403 *
4404 * @see Notification#extras
4405 */
4406 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004407 return mUserExtras;
4408 }
4409
4410 private Bundle getAllExtras() {
4411 final Bundle saveExtras = (Bundle) mUserExtras.clone();
4412 saveExtras.putAll(mN.extras);
4413 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08004414 }
4415
4416 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004417 * Add an action to this notification. Actions are typically displayed by
4418 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004419 * <p>
4420 * Every action must have an icon (32dp square and matching the
4421 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4422 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4423 * <p>
4424 * A notification in its expanded form can display up to 3 actions, from left to right in
4425 * the order they were added. Actions will not be displayed when the notification is
4426 * collapsed, however, so be sure that any essential functions may be accessed by the user
4427 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004428 *
4429 * @param icon Resource ID of a drawable that represents the action.
4430 * @param title Text describing the action.
4431 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04004432 *
4433 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004434 */
Dan Sandler86647982015-05-13 23:41:13 -04004435 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004436 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004437 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004438 return this;
4439 }
4440
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004441 /**
Griff Hazen959591e2014-05-15 22:26:18 -07004442 * Add an action to this notification. Actions are typically displayed by
4443 * the system as a button adjacent to the notification content.
4444 * <p>
4445 * Every action must have an icon (32dp square and matching the
4446 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4447 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4448 * <p>
4449 * A notification in its expanded form can display up to 3 actions, from left to right in
4450 * the order they were added. Actions will not be displayed when the notification is
4451 * collapsed, however, so be sure that any essential functions may be accessed by the user
4452 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
4453 *
4454 * @param action The action to add.
4455 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004456 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07004457 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08004458 if (action != null) {
4459 mActions.add(action);
4460 }
Griff Hazen959591e2014-05-15 22:26:18 -07004461 return this;
4462 }
4463
4464 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004465 * Alter the complete list of actions attached to this notification.
4466 * @see #addAction(Action).
4467 *
4468 * @param actions
4469 * @return
4470 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004471 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004472 public Builder setActions(Action... actions) {
4473 mActions.clear();
4474 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08004475 if (actions[i] != null) {
4476 mActions.add(actions[i]);
4477 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004478 }
4479 return this;
4480 }
4481
4482 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004483 * Add a rich notification style to be applied at build time.
4484 *
4485 * @param style Object responsible for modifying the notification style.
4486 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004487 @NonNull
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004488 public Builder setStyle(Style style) {
4489 if (mStyle != style) {
4490 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004491 if (mStyle != null) {
4492 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004493 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
4494 } else {
4495 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004496 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004497 }
4498 return this;
4499 }
4500
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004501 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05004502 * Returns the style set by {@link #setStyle(Style)}.
4503 */
4504 public Style getStyle() {
4505 return mStyle;
4506 }
4507
4508 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004509 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07004510 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004511 * @return The same Builder.
4512 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004513 @NonNull
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06004514 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004515 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004516 return this;
4517 }
4518
4519 /**
4520 * Supply a replacement Notification whose contents should be shown in insecure contexts
4521 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
4522 * @param n A replacement notification, presumably with some or all info redacted.
4523 * @return The same Builder.
4524 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004525 @NonNull
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004526 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004527 if (n != null) {
4528 mN.publicVersion = new Notification();
4529 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
4530 } else {
4531 mN.publicVersion = null;
4532 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004533 return this;
4534 }
4535
Griff Hazenb720abe2014-05-20 13:15:30 -07004536 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004537 * Apply an extender to this notification builder. Extenders may be used to add
4538 * metadata or change options on this builder.
4539 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004540 @NonNull
Griff Hazen61a9e862014-05-22 16:05:19 -07004541 public Builder extend(Extender extender) {
4542 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004543 return this;
4544 }
4545
Dan Sandler4e787062015-06-17 15:09:48 -04004546 /**
cncn9d0fd9c2019-11-20 11:05:02 -08004547 * Set the value for a notification flag
4548 *
4549 * @param mask Bit mask of the flag
4550 * @param value Status (on/off) of the flag
4551 *
4552 * @return The same Builder.
Dan Sandler4e787062015-06-17 15:09:48 -04004553 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004554 @NonNull
cncn9d0fd9c2019-11-20 11:05:02 -08004555 public Builder setFlag(@NotificationFlags int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08004556 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004557 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004558 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004559 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004560 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04004561 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08004562 }
4563
Dan Sandler26e81cf2014-05-06 10:01:27 -04004564 /**
4565 * Sets {@link Notification#color}.
4566 *
4567 * @param argb The accent color to use
4568 *
4569 * @return The same Builder.
4570 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004571 @NonNull
Tor Norbye80756e32015-03-02 09:39:27 -08004572 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004573 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004574 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04004575 return this;
4576 }
4577
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004578 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04004579 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
4580 // This user can never be a badged profile,
4581 // and also includes USER_ALL system notifications.
4582 return null;
4583 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02004584 // Note: This assumes that the current user can read the profile badge of the
4585 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08004586 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05004587 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004588 }
4589
4590 private Bitmap getProfileBadge() {
4591 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01004592 if (badge == null) {
4593 return null;
4594 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004595 final int size = mContext.getResources().getDimensionPixelSize(
4596 R.dimen.notification_badge_size);
4597 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004598 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004599 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004600 badge.draw(canvas);
4601 return bitmap;
4602 }
4603
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004604 private void bindProfileBadge(RemoteViews contentView, StandardTemplateParams p) {
Kenny Guy98193ea2014-07-24 19:54:37 +01004605 Bitmap profileBadge = getProfileBadge();
4606
Kenny Guy98193ea2014-07-24 19:54:37 +01004607 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004608 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
4609 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004610 if (isColorized(p)) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004611 contentView.setDrawableTint(R.id.profile_badge, false,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004612 getPrimaryTextColor(p), PorterDuff.Mode.SRC_ATOP);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004613 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004614 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004615 }
4616
Gus Prevasbae5ba32018-11-28 15:45:13 -05004617 private void bindAlertedIcon(RemoteViews contentView, StandardTemplateParams p) {
4618 contentView.setDrawableTint(
4619 R.id.alerted_icon,
4620 false /* targetBackground */,
4621 getNeutralColor(p),
4622 PorterDuff.Mode.SRC_ATOP);
4623 }
4624
Julia Reynoldsfc640012018-02-21 12:25:27 -05004625 /**
4626 * @hide
4627 */
4628 public boolean usesStandardHeader() {
4629 if (mN.mUsesStandardHeader) {
4630 return true;
4631 }
4632 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.N) {
4633 if (mN.contentView == null && mN.bigContentView == null) {
4634 return true;
4635 }
4636 }
4637 boolean contentViewUsesHeader = mN.contentView == null
4638 || STANDARD_LAYOUTS.contains(mN.contentView.getLayoutId());
4639 boolean bigContentViewUsesHeader = mN.bigContentView == null
4640 || STANDARD_LAYOUTS.contains(mN.bigContentView.getLayoutId());
4641 return contentViewUsesHeader && bigContentViewUsesHeader;
4642 }
4643
Christoph Studerfe718432014-09-01 18:21:18 +02004644 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004645 resetNotificationHeader(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02004646 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08004647 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004648 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08004649 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004650 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08004651 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08004652 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004653 }
4654
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004655 /**
4656 * Resets the notification header to its original state
4657 */
4658 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07004659 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
4660 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08004661 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004662 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02004663 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004664 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07004665 contentView.setTextViewText(R.id.header_text, null);
Selim Cinekafeed292017-12-12 17:32:44 -08004666 contentView.setViewVisibility(R.id.header_text_secondary, View.GONE);
4667 contentView.setTextViewText(R.id.header_text_secondary, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004668 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinekafeed292017-12-12 17:32:44 -08004669 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004670 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004671 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004672 contentView.setImageViewIcon(R.id.profile_badge, null);
4673 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Gus Prevasa3226492018-10-23 11:10:09 -04004674 contentView.setViewVisibility(R.id.alerted_icon, View.GONE);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004675 mN.mUsesStandardHeader = false;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004676 }
4677
Selim Cinek384804b2018-04-18 14:31:07 +08004678 private RemoteViews applyStandardTemplate(int resId, TemplateBindResult result) {
4679 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this),
4680 result);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004681 }
4682
Selim Cinek384804b2018-04-18 14:31:07 +08004683 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p,
4684 TemplateBindResult result) {
Kenny Guy77320062014-08-27 21:37:15 +01004685 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04004686
Christoph Studerfe718432014-09-01 18:21:18 +02004687 resetStandardTemplate(contentView);
4688
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004689 final Bundle ex = mN.extras;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004690 updateBackgroundColor(contentView, p);
4691 bindNotificationHeader(contentView, p);
Selim Cinek384804b2018-04-18 14:31:07 +08004692 bindLargeIconAndReply(contentView, p, result);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004693 boolean showProgress = handleProgressBar(contentView, ex, p);
Adrian Roos70d7aa32017-01-11 15:39:06 -08004694 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08004695 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004696 contentView.setTextViewText(R.id.title, processTextSpans(p.title));
Lucas Dupin00be88f2019-01-03 17:50:52 -08004697 setTextViewColorPrimary(contentView, R.id.title, p);
Selim Cinek954cc232016-05-20 13:29:23 -07004698 contentView.setViewLayoutWidth(R.id.title, showProgress
4699 ? ViewGroup.LayoutParams.WRAP_CONTENT
4700 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08004701 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08004702 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08004703 int textId = showProgress ? com.android.internal.R.id.text_line_1
4704 : com.android.internal.R.id.text;
Selim Cinek48f66b72017-08-18 16:17:51 -07004705 contentView.setTextViewText(textId, processTextSpans(p.text));
Lucas Dupin00be88f2019-01-03 17:50:52 -08004706 setTextViewColorSecondary(contentView, textId, p);
Selim Cinek41598732016-01-11 16:58:37 -08004707 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08004708 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08004709
Selim Cinek279fa862016-06-14 10:57:25 -07004710 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004711
Selim Cinek29603462015-11-17 19:04:39 -08004712 return contentView;
4713 }
4714
Selim Cinek48f66b72017-08-18 16:17:51 -07004715 private CharSequence processTextSpans(CharSequence text) {
Lucas Dupind3c99322018-09-27 10:22:29 -07004716 if (hasForegroundColor() || mInNightMode) {
Lucas Dupina291d192018-06-07 13:59:42 -07004717 return ContrastColorUtil.clearColorSpans(text);
Selim Cinek48f66b72017-08-18 16:17:51 -07004718 }
4719 return text;
4720 }
4721
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004722 private void setTextViewColorPrimary(RemoteViews contentView, int id,
4723 StandardTemplateParams p) {
4724 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004725 contentView.setTextColor(id, mPrimaryTextColor);
4726 }
4727
Selim Cinek48f66b72017-08-18 16:17:51 -07004728 private boolean hasForegroundColor() {
4729 return mForegroundColor != COLOR_INVALID;
4730 }
4731
Selim Cinek389edcd2017-05-11 19:16:44 -07004732 /**
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004733 * Return the primary text color using the existing template params
Selim Cinek389edcd2017-05-11 19:16:44 -07004734 * @hide
4735 */
4736 @VisibleForTesting
4737 public int getPrimaryTextColor() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004738 return getPrimaryTextColor(mParams);
4739 }
4740
4741 /**
4742 * @param p the template params to inflate this with
4743 * @return the primary text color
4744 * @hide
4745 */
4746 @VisibleForTesting
4747 public int getPrimaryTextColor(StandardTemplateParams p) {
4748 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004749 return mPrimaryTextColor;
4750 }
4751
Selim Cinek389edcd2017-05-11 19:16:44 -07004752 /**
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004753 * Return the secondary text color using the existing template params
Selim Cinek389edcd2017-05-11 19:16:44 -07004754 * @hide
4755 */
4756 @VisibleForTesting
4757 public int getSecondaryTextColor() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004758 return getSecondaryTextColor(mParams);
4759 }
4760
4761 /**
4762 * @param p the template params to inflate this with
4763 * @return the secondary text color
4764 * @hide
4765 */
4766 @VisibleForTesting
4767 public int getSecondaryTextColor(StandardTemplateParams p) {
4768 ensureColors(p);
Selim Cinek389edcd2017-05-11 19:16:44 -07004769 return mSecondaryTextColor;
4770 }
4771
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004772 private void setTextViewColorSecondary(RemoteViews contentView, int id,
4773 StandardTemplateParams p) {
4774 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004775 contentView.setTextColor(id, mSecondaryTextColor);
4776 }
4777
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004778 private void ensureColors(StandardTemplateParams p) {
4779 int backgroundColor = getBackgroundColor(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004780 if (mPrimaryTextColor == COLOR_INVALID
4781 || mSecondaryTextColor == COLOR_INVALID
Selim Cinek7b9605b2017-01-19 17:36:00 -08004782 || mTextColorsAreForBackground != backgroundColor) {
4783 mTextColorsAreForBackground = backgroundColor;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004784 if (!hasForegroundColor() || !isColorized(p)) {
Lucas Dupina291d192018-06-07 13:59:42 -07004785 mPrimaryTextColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004786 backgroundColor, mInNightMode);
Lucas Dupina291d192018-06-07 13:59:42 -07004787 mSecondaryTextColor = ContrastColorUtil.resolveSecondaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004788 backgroundColor, mInNightMode);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004789 if (backgroundColor != COLOR_DEFAULT && isColorized(p)) {
Lucas Dupina291d192018-06-07 13:59:42 -07004790 mPrimaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004791 mPrimaryTextColor, backgroundColor, 4.5);
Lucas Dupina291d192018-06-07 13:59:42 -07004792 mSecondaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004793 mSecondaryTextColor, backgroundColor, 4.5);
4794 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07004795 } else {
Lucas Dupina291d192018-06-07 13:59:42 -07004796 double backLum = ContrastColorUtil.calculateLuminance(backgroundColor);
4797 double textLum = ContrastColorUtil.calculateLuminance(mForegroundColor);
4798 double contrast = ContrastColorUtil.calculateContrast(mForegroundColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004799 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07004800 // We only respect the given colors if worst case Black or White still has
4801 // contrast
4802 boolean backgroundLight = backLum > textLum
4803 && satisfiesTextContrast(backgroundColor, Color.BLACK)
4804 || backLum <= textLum
4805 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004806 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07004807 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004808 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004809 mForegroundColor,
4810 backgroundColor,
4811 true /* findFG */,
4812 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004813 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004814 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004815 } else {
4816 mSecondaryTextColor =
Lucas Dupina291d192018-06-07 13:59:42 -07004817 ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004818 mForegroundColor,
4819 backgroundColor,
4820 true /* findFG */,
4821 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004822 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004823 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004824 }
4825 } else {
4826 mPrimaryTextColor = mForegroundColor;
Lucas Dupina291d192018-06-07 13:59:42 -07004827 mSecondaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004828 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4829 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Lucas Dupina291d192018-06-07 13:59:42 -07004830 if (ContrastColorUtil.calculateContrast(mSecondaryTextColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004831 backgroundColor) < 4.5f) {
4832 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07004833 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004834 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004835 mSecondaryTextColor,
4836 backgroundColor,
4837 true /* findFG */,
4838 4.5f);
4839 } else {
4840 mSecondaryTextColor
Lucas Dupina291d192018-06-07 13:59:42 -07004841 = ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004842 mSecondaryTextColor,
4843 backgroundColor,
4844 true /* findFG */,
4845 4.5f);
4846 }
Lucas Dupina291d192018-06-07 13:59:42 -07004847 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004848 mSecondaryTextColor, backgroundLight
4849 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4850 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004851 }
4852 }
4853 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004854 }
4855 }
4856
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004857 private void updateBackgroundColor(RemoteViews contentView,
4858 StandardTemplateParams p) {
4859 if (isColorized(p)) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004860 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004861 getBackgroundColor(p));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004862 } else {
4863 // Clear it!
4864 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4865 0);
4866 }
4867 }
4868
Selim Cinek860b6da2015-12-16 19:02:19 -08004869 /**
4870 * @param remoteView the remote view to update the minheight in
4871 * @param hasMinHeight does it have a mimHeight
4872 * @hide
4873 */
4874 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4875 int minHeight = 0;
4876 if (hasMinHeight) {
4877 // we need to set the minHeight of the notification
4878 minHeight = mContext.getResources().getDimensionPixelSize(
4879 com.android.internal.R.dimen.notification_min_content_height);
4880 }
4881 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4882 }
4883
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004884 private boolean handleProgressBar(RemoteViews contentView, Bundle ex,
4885 StandardTemplateParams p) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004886 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4887 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4888 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004889 if (p.hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004890 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004891 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004892 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004893 contentView.setProgressBackgroundTintList(
4894 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4895 R.color.notification_progress_background_color)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004896 if (getRawColor(p) != COLOR_DEFAULT) {
Beth Thibodeau38275c32019-05-08 16:36:59 -04004897 int color = isColorized(p) ? getPrimaryTextColor(p) : resolveContrastColor(p);
4898 ColorStateList colorStateList = ColorStateList.valueOf(color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004899 contentView.setProgressTintList(R.id.progress, colorStateList);
4900 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004901 }
Selim Cinek29603462015-11-17 19:04:39 -08004902 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004903 } else {
4904 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004905 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004906 }
Joe Onorato561d3852010-11-20 18:09:34 -08004907 }
4908
Selim Cinek384804b2018-04-18 14:31:07 +08004909 private void bindLargeIconAndReply(RemoteViews contentView, StandardTemplateParams p,
4910 TemplateBindResult result) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004911 boolean largeIconShown = bindLargeIcon(contentView, p);
4912 boolean replyIconShown = bindReplyIcon(contentView, p);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004913 boolean iconContainerVisible = largeIconShown || replyIconShown;
Selim Cinek384804b2018-04-18 14:31:07 +08004914 contentView.setViewVisibility(R.id.right_icon_container,
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004915 iconContainerVisible ? View.VISIBLE : View.GONE);
Selim Cinek1c72fa02018-04-23 18:00:54 +08004916 int marginEnd = calculateMarginEnd(largeIconShown, replyIconShown);
4917 contentView.setViewLayoutMarginEnd(R.id.line1, marginEnd);
4918 contentView.setViewLayoutMarginEnd(R.id.text, marginEnd);
4919 contentView.setViewLayoutMarginEnd(R.id.progress, marginEnd);
Selim Cinek384804b2018-04-18 14:31:07 +08004920 if (result != null) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08004921 result.setIconMarginEnd(marginEnd);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004922 result.setRightIconContainerVisible(iconContainerVisible);
Selim Cinek384804b2018-04-18 14:31:07 +08004923 }
4924 }
4925
Selim Cinek1c72fa02018-04-23 18:00:54 +08004926 private int calculateMarginEnd(boolean largeIconShown, boolean replyIconShown) {
4927 int marginEnd = 0;
4928 int contentMargin = mContext.getResources().getDimensionPixelSize(
4929 R.dimen.notification_content_margin_end);
4930 int iconSize = mContext.getResources().getDimensionPixelSize(
4931 R.dimen.notification_right_icon_size);
4932 if (replyIconShown) {
4933 // The size of the reply icon
4934 marginEnd += iconSize;
4935
4936 int replyInset = mContext.getResources().getDimensionPixelSize(
4937 R.dimen.notification_reply_inset);
4938 // We're subtracting the inset of the reply icon to make sure it's
4939 // aligned nicely on the right, and remove it from the following padding
4940 marginEnd -= replyInset * 2;
4941 }
4942 if (largeIconShown) {
4943 // adding size of the right icon
4944 marginEnd += iconSize;
4945
4946 if (replyIconShown) {
4947 // We also add some padding to the reply icon if it's around
4948 marginEnd += contentMargin;
4949 }
4950 }
4951 if (replyIconShown || largeIconShown) {
4952 // The padding to the content
4953 marginEnd += contentMargin;
4954 }
4955 return marginEnd;
4956 }
4957
Selim Cinek384804b2018-04-18 14:31:07 +08004958 /**
4959 * Bind the large icon.
4960 * @return if the largeIcon is visible
4961 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004962 private boolean bindLargeIcon(RemoteViews contentView, StandardTemplateParams p) {
Selim Cinek279fa862016-06-14 10:57:25 -07004963 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4964 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4965 }
Lucas Dupin00be88f2019-01-03 17:50:52 -08004966 boolean showLargeIcon = mN.mLargeIcon != null && !p.hideLargeIcon;
Selim Cinek88188f22017-09-19 16:46:56 -07004967 if (showLargeIcon) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004968 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4969 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004970 processLargeLegacyIcon(mN.mLargeIcon, contentView, p);
Selim Cinek88188f22017-09-19 16:46:56 -07004971 }
Selim Cinek384804b2018-04-18 14:31:07 +08004972 return showLargeIcon;
4973 }
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004974
Selim Cinek384804b2018-04-18 14:31:07 +08004975 /**
4976 * Bind the reply icon.
4977 * @return if the reply icon is visible
4978 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004979 private boolean bindReplyIcon(RemoteViews contentView, StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08004980 boolean actionVisible = !p.hideReplyIcon;
Selim Cinek384804b2018-04-18 14:31:07 +08004981 Action action = null;
Selim Cinek88188f22017-09-19 16:46:56 -07004982 if (actionVisible) {
Selim Cinek384804b2018-04-18 14:31:07 +08004983 action = findReplyAction();
4984 actionVisible = action != null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004985 }
Selim Cinek384804b2018-04-18 14:31:07 +08004986 if (actionVisible) {
4987 contentView.setViewVisibility(R.id.reply_icon_action, View.VISIBLE);
4988 contentView.setDrawableTint(R.id.reply_icon_action,
4989 false /* targetBackground */,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004990 getNeutralColor(p),
Selim Cinek384804b2018-04-18 14:31:07 +08004991 PorterDuff.Mode.SRC_ATOP);
4992 contentView.setOnClickPendingIntent(R.id.reply_icon_action, action.actionIntent);
4993 contentView.setRemoteInputs(R.id.reply_icon_action, action.mRemoteInputs);
4994 } else {
4995 contentView.setRemoteInputs(R.id.reply_icon_action, null);
4996 }
4997 contentView.setViewVisibility(R.id.reply_icon_action,
4998 actionVisible ? View.VISIBLE : View.GONE);
4999 return actionVisible;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07005000 }
5001
5002 private Action findReplyAction() {
5003 ArrayList<Action> actions = mActions;
5004 if (mOriginalActions != null) {
5005 actions = mOriginalActions;
5006 }
5007 int numActions = actions.size();
5008 for (int i = 0; i < numActions; i++) {
5009 Action action = actions.get(i);
5010 if (hasValidRemoteInput(action)) {
5011 return action;
5012 }
5013 }
5014 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005015 }
5016
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005017 private void bindNotificationHeader(RemoteViews contentView, StandardTemplateParams p) {
5018 bindSmallIcon(contentView, p);
5019 bindHeaderAppName(contentView, p);
Lucas Dupin00be88f2019-01-03 17:50:52 -08005020 bindHeaderText(contentView, p);
5021 bindHeaderTextSecondary(contentView, p);
5022 bindHeaderChronometerAndTime(contentView, p);
5023 bindProfileBadge(contentView, p);
5024 bindAlertedIcon(contentView, p);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005025 bindActivePermissions(contentView, p);
5026 bindExpandButton(contentView, p);
Julia Reynoldsfc640012018-02-21 12:25:27 -05005027 mN.mUsesStandardHeader = true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005028 }
5029
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005030 private void bindActivePermissions(RemoteViews contentView, StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005031 int color = getNeutralColor(p);
Julia Reynoldsb887b232018-04-10 16:38:08 -04005032 contentView.setDrawableTint(R.id.camera, false, color, PorterDuff.Mode.SRC_ATOP);
5033 contentView.setDrawableTint(R.id.mic, false, color, PorterDuff.Mode.SRC_ATOP);
5034 contentView.setDrawableTint(R.id.overlay, false, color, PorterDuff.Mode.SRC_ATOP);
5035 }
5036
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005037 private void bindExpandButton(RemoteViews contentView, StandardTemplateParams p) {
5038 int color = isColorized(p) ? getPrimaryTextColor(p) : getSecondaryTextColor(p);
Sunny Goyal5b153922017-09-21 21:00:36 -07005039 contentView.setDrawableTint(R.id.expand_button, false, color,
5040 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005041 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08005042 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005043 }
5044
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005045 private void bindHeaderChronometerAndTime(RemoteViews contentView,
5046 StandardTemplateParams p) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005047 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08005048 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005049 setTextViewColorSecondary(contentView, R.id.time_divider, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005050 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
5051 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
5052 contentView.setLong(R.id.chronometer, "setBase",
5053 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
5054 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07005055 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07005056 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005057 setTextViewColorSecondary(contentView, R.id.chronometer, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005058 } else {
5059 contentView.setViewVisibility(R.id.time, View.VISIBLE);
5060 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005061 setTextViewColorSecondary(contentView, R.id.time, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005062 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005063 } else {
5064 // We still want a time to be set but gone, such that we can show and hide it
5065 // on demand in case it's a child notification without anything in the header
5066 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005067 }
5068 }
5069
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005070 private void bindHeaderText(RemoteViews contentView, StandardTemplateParams p) {
5071 CharSequence summaryText = p.summaryText;
5072 if (summaryText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05005073 && mStyle.hasSummaryInHeader()) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005074 summaryText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05005075 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005076 if (summaryText == null
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07005077 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
5078 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005079 summaryText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07005080 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005081 if (summaryText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005082 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek48f66b72017-08-18 16:17:51 -07005083 contentView.setTextViewText(R.id.header_text, processTextSpans(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005084 processLegacyText(summaryText)));
5085 setTextViewColorSecondary(contentView, R.id.header_text, p);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07005086 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
5087 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005088 setTextViewColorSecondary(contentView, R.id.header_text_divider, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005089 }
5090 }
5091
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005092 private void bindHeaderTextSecondary(RemoteViews contentView, StandardTemplateParams p) {
5093 if (!TextUtils.isEmpty(p.headerTextSecondary)) {
Selim Cinekafeed292017-12-12 17:32:44 -08005094 contentView.setTextViewText(R.id.header_text_secondary, processTextSpans(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005095 processLegacyText(p.headerTextSecondary)));
5096 setTextViewColorSecondary(contentView, R.id.header_text_secondary, p);
Selim Cinekafeed292017-12-12 17:32:44 -08005097 contentView.setViewVisibility(R.id.header_text_secondary, View.VISIBLE);
5098 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005099 setTextViewColorSecondary(contentView, R.id.header_text_secondary_divider, p);
Selim Cinekafeed292017-12-12 17:32:44 -08005100 }
5101 }
5102
Adrian Rooseba05822016-04-22 17:09:27 -07005103 /**
5104 * @hide
5105 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005106 @UnsupportedAppUsage
Adrian Rooseba05822016-04-22 17:09:27 -07005107 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04005108 CharSequence name = null;
5109 final PackageManager pm = mContext.getPackageManager();
5110 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
5111 // only system packages which lump together a bunch of unrelated stuff
5112 // may substitute a different name to make the purpose of the
5113 // notification more clear. the correct package label should always
5114 // be accessible via SystemUI.
5115 final String pkg = mContext.getPackageName();
5116 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
5117 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
5118 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
5119 name = subName;
5120 } else {
5121 Log.w(TAG, "warning: pkg "
5122 + pkg + " attempting to substitute app name '" + subName
5123 + "' without holding perm "
5124 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
5125 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005126 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04005127 if (TextUtils.isEmpty(name)) {
5128 name = pm.getApplicationLabel(mContext.getApplicationInfo());
5129 }
5130 if (TextUtils.isEmpty(name)) {
5131 // still nothing?
5132 return null;
5133 }
5134
5135 return String.valueOf(name);
5136 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005137 private void bindHeaderAppName(RemoteViews contentView, StandardTemplateParams p) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04005138 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005139 if (isColorized(p)) {
5140 setTextViewColorPrimary(contentView, R.id.app_name_text, p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005141 } else {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005142 contentView.setTextColor(R.id.app_name_text, getSecondaryTextColor(p));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005143 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005144 }
5145
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005146 private boolean isColorized(StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005147 return p.allowColorization && mN.isColorized();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005148 }
5149
5150 private void bindSmallIcon(RemoteViews contentView, StandardTemplateParams p) {
Selim Cinek279fa862016-06-14 10:57:25 -07005151 if (mN.mSmallIcon == null && mN.icon != 0) {
5152 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
5153 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005154 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Sunny Goyal5b153922017-09-21 21:00:36 -07005155 contentView.setInt(R.id.icon, "setImageLevel", mN.iconLevel);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005156 processSmallIconColor(mN.mSmallIcon, contentView, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005157 }
5158
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005159 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005160 * @return true if the built notification will show the time or the chronometer; false
5161 * otherwise
5162 */
5163 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07005164 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005165 }
5166
Christoph Studerfe718432014-09-01 18:21:18 +02005167 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07005168 // actions_container is only reset when there are no actions to avoid focus issues with
5169 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02005170 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02005171 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08005172
5173 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
5174 big.setTextViewText(R.id.notification_material_reply_text_1, null);
Kenny Guya0f6de82018-04-06 16:20:16 +01005175 big.setViewVisibility(R.id.notification_material_reply_text_1_container, View.GONE);
5176 big.setViewVisibility(R.id.notification_material_reply_progress, View.GONE);
Adrian Roose458aa82015-12-08 16:17:19 -08005177
5178 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
5179 big.setTextViewText(R.id.notification_material_reply_text_2, null);
5180 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
5181 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07005182
Selim Cineked64a142018-02-06 18:06:01 -08005183 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
5184 R.dimen.notification_content_margin);
Christoph Studerfe718432014-09-01 18:21:18 +02005185 }
5186
Selim Cinek384804b2018-04-18 14:31:07 +08005187 private RemoteViews applyStandardTemplateWithActions(int layoutId,
5188 TemplateBindResult result) {
5189 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this),
5190 result);
Adrian Roos48d746a2016-04-12 14:57:28 -07005191 }
5192
Gustav Sennton1463d832018-11-06 16:12:48 +00005193 private static List<Notification.Action> filterOutContextualActions(
5194 List<Notification.Action> actions) {
5195 List<Notification.Action> nonContextualActions = new ArrayList<>();
5196 for (Notification.Action action : actions) {
Gustav Sennton005d7a02019-01-04 13:41:32 +00005197 if (!action.isContextual()) {
Gustav Sennton1463d832018-11-06 16:12:48 +00005198 nonContextualActions.add(action);
5199 }
5200 }
5201 return nonContextualActions;
5202 }
5203
Adrian Roos70d7aa32017-01-11 15:39:06 -08005204 private RemoteViews applyStandardTemplateWithActions(int layoutId,
Selim Cinek384804b2018-04-18 14:31:07 +08005205 StandardTemplateParams p, TemplateBindResult result) {
5206 RemoteViews big = applyStandardTemplate(layoutId, p, result);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005207
Christoph Studerfe718432014-09-01 18:21:18 +02005208 resetStandardTemplateWithActions(big);
5209
Adrian Roose458aa82015-12-08 16:17:19 -08005210 boolean validRemoteInput = false;
5211
Gustav Sennton1463d832018-11-06 16:12:48 +00005212 // In the UI contextual actions appear separately from the standard actions, so we
5213 // filter them out here.
5214 List<Notification.Action> nonContextualActions = filterOutContextualActions(mActions);
5215
5216 int N = nonContextualActions.size();
Lucas Dupin00be88f2019-01-03 17:50:52 -08005217 boolean emphazisedMode = mN.fullScreenIntent != null;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005218 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005219 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08005220 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005221 big.setViewVisibility(R.id.actions, View.VISIBLE);
Selim Cineked64a142018-02-06 18:06:01 -08005222 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Daniel Sandler8680bf82012-05-15 16:52:52 -04005223 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005224 for (int i=0; i<N; i++) {
Gustav Sennton1463d832018-11-06 16:12:48 +00005225 Action action = nonContextualActions.get(i);
5226
Selim Cinekffcc7cf2018-02-06 17:43:51 -08005227 boolean actionHasValidInput = hasValidRemoteInput(action);
5228 validRemoteInput |= actionHasValidInput;
Adrian Roose458aa82015-12-08 16:17:19 -08005229
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005230 final RemoteViews button = generateActionButton(action, emphazisedMode, p);
Selim Cinek396caca2018-04-10 17:46:46 -07005231 if (actionHasValidInput && !emphazisedMode) {
Selim Cinekffcc7cf2018-02-06 17:43:51 -08005232 // Clear the drawable
5233 button.setInt(R.id.action0, "setBackgroundResource", 0);
5234 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005235 big.addView(R.id.actions, button);
5236 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07005237 } else {
5238 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005239 }
Adrian Roose458aa82015-12-08 16:17:19 -08005240
5241 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Lucas Dupin00be88f2019-01-03 17:50:52 -08005242 if (validRemoteInput && replyText != null
Selim Cinekbee4e072018-05-21 22:06:43 -07005243 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])
5244 && p.maxRemoteInputHistory > 0) {
Kenny Guya0f6de82018-04-06 16:20:16 +01005245 boolean showSpinner = mN.extras.getBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER);
Adrian Roose458aa82015-12-08 16:17:19 -08005246 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Kenny Guya0f6de82018-04-06 16:20:16 +01005247 big.setViewVisibility(R.id.notification_material_reply_text_1_container,
5248 View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005249 big.setTextViewText(R.id.notification_material_reply_text_1,
5250 processTextSpans(replyText[0]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005251 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1, p);
Kenny Guya0f6de82018-04-06 16:20:16 +01005252 big.setViewVisibility(R.id.notification_material_reply_progress,
5253 showSpinner ? View.VISIBLE : View.GONE);
5254 big.setProgressIndeterminateTintList(
5255 R.id.notification_material_reply_progress,
5256 ColorStateList.valueOf(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005257 isColorized(p) ? getPrimaryTextColor(p) : resolveContrastColor(p)));
Adrian Roose458aa82015-12-08 16:17:19 -08005258
Selim Cinekbee4e072018-05-21 22:06:43 -07005259 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])
5260 && p.maxRemoteInputHistory > 1) {
Adrian Roose458aa82015-12-08 16:17:19 -08005261 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005262 big.setTextViewText(R.id.notification_material_reply_text_2,
5263 processTextSpans(replyText[1]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005264 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2, p);
Adrian Roose458aa82015-12-08 16:17:19 -08005265
Selim Cinekbee4e072018-05-21 22:06:43 -07005266 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])
5267 && p.maxRemoteInputHistory > 2) {
Adrian Roose458aa82015-12-08 16:17:19 -08005268 big.setViewVisibility(
5269 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005270 big.setTextViewText(R.id.notification_material_reply_text_3,
5271 processTextSpans(replyText[2]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005272 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3, p);
Adrian Roose458aa82015-12-08 16:17:19 -08005273 }
5274 }
5275 }
5276
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005277 return big;
5278 }
5279
Adrian Roose458aa82015-12-08 16:17:19 -08005280 private boolean hasValidRemoteInput(Action action) {
5281 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
5282 // Weird actions
5283 return false;
5284 }
5285
5286 RemoteInput[] remoteInputs = action.getRemoteInputs();
5287 if (remoteInputs == null) {
5288 return false;
5289 }
5290
5291 for (RemoteInput r : remoteInputs) {
5292 CharSequence[] choices = r.getChoices();
5293 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
5294 return true;
5295 }
5296 }
5297 return false;
5298 }
5299
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005300 /**
5301 * Construct a RemoteViews for the final 1U notification layout. In order:
5302 * 1. Custom contentView from the caller
5303 * 2. Style's proposed content view
5304 * 3. Standard template view
5305 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005306 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005307 return createContentView(false /* increasedheight */ );
5308 }
5309
5310 /**
5311 * Construct a RemoteViews for the smaller content view.
5312 *
5313 * @param increasedHeight true if this layout be created with an increased height. Some
5314 * styles may support showing more then just that basic 1U size
5315 * and the system may decide to render important notifications
5316 * slightly bigger even when collapsed.
5317 *
5318 * @hide
5319 */
5320 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005321 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005322 return mN.contentView;
5323 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005324 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005325 if (styleView != null) {
5326 return styleView;
5327 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005328 }
Selim Cinek384804b2018-04-18 14:31:07 +08005329 return applyStandardTemplate(getBaseLayoutResource(), null /* result */);
Joe Onorato46439ce2010-11-19 13:56:21 -08005330 }
5331
Selim Cineka7679b62017-05-10 16:33:25 -07005332 private boolean useExistingRemoteView() {
5333 return mStyle == null || (!mStyle.displayCustomViewInline()
5334 && !mRebuildStyledRemoteViews);
5335 }
5336
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005337 /**
5338 * Construct a RemoteViews for the final big notification layout.
5339 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005340 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05005341 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07005342 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005343 return mN.bigContentView;
5344 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05005345 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08005346 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005347 } else if (mActions.size() != 0) {
Selim Cinek384804b2018-04-18 14:31:07 +08005348 result = applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5349 null /* result */);
Selim Cinek850a8542015-11-11 11:48:36 -05005350 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08005351 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05005352 return result;
5353 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005354
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005355 /**
Selim Cinek414ad332017-02-24 19:06:12 -08005356 * Construct a RemoteViews for the final notification header only. This will not be
5357 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005358 *
5359 * @hide
5360 */
Lucas Dupin00be88f2019-01-03 17:50:52 -08005361 public RemoteViews makeNotificationHeader() {
5362 return makeNotificationHeader(mParams.reset().fillTextsFrom(this));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005363 }
5364
5365 /**
5366 * Construct a RemoteViews for the final notification header only. This will not be
5367 * colorized.
5368 *
5369 * @param p the template params to inflate this with
5370 */
5371 private RemoteViews makeNotificationHeader(StandardTemplateParams p) {
5372 // Headers on their own are never colorized
5373 p.disallowColorization();
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005374 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Lucas Dupin00be88f2019-01-03 17:50:52 -08005375 R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005376 resetNotificationHeader(header);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005377 bindNotificationHeader(header, p);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005378 return header;
5379 }
5380
Adrian Roos487374f2017-01-11 15:48:14 -08005381 /**
5382 * Construct a RemoteViews for the ambient version of the notification.
5383 *
5384 * @hide
5385 */
5386 public RemoteViews makeAmbientNotification() {
Selim Cinek0e069942019-01-24 16:09:41 -08005387 RemoteViews headsUpContentView = createHeadsUpContentView(false /* increasedHeight */);
5388 if (headsUpContentView != null) {
5389 return headsUpContentView;
5390 }
5391 return createContentView();
Adrian Roos487374f2017-01-11 15:48:14 -08005392 }
5393
Selim Cinek29603462015-11-17 19:04:39 -08005394 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005395 if (result != null) {
5396 result.setViewVisibility(R.id.text_line_1, View.GONE);
5397 }
Selim Cinek29603462015-11-17 19:04:39 -08005398 }
5399
Selim Cinek6743c0b2017-01-18 18:24:01 -08005400 /**
5401 * Adapt the Notification header if this view is used as an expanded view.
5402 *
5403 * @hide
5404 */
5405 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005406 if (result != null) {
5407 result.setBoolean(R.id.notification_header, "setExpanded", true);
5408 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005409 }
5410
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005411 /**
5412 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005413 *
5414 * @param increasedHeight true if this layout be created with an increased height. Some
5415 * styles may support showing more then just that basic 1U size
5416 * and the system may decide to render important notifications
5417 * slightly bigger even when collapsed.
5418 *
5419 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005420 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005421 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005422 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005423 return mN.headsUpContentView;
5424 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08005425 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
5426 if (styleView != null) {
5427 return styleView;
5428 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05005429 } else if (mActions.size() == 0) {
5430 return null;
5431 }
5432
Selim Cinekbee4e072018-05-21 22:06:43 -07005433 // We only want at most a single remote input history to be shown here, otherwise
5434 // the content would become squished.
5435 StandardTemplateParams p = mParams.reset().fillTextsFrom(this)
5436 .setMaxRemoteInputHistory(1);
5437 return applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5438 p,
5439 null /* result */);
Chris Wren8fd39ec2014-02-27 17:43:26 -05005440 }
5441
Selim Cinek624c02db2015-12-14 21:00:02 -08005442 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08005443 * Construct a RemoteViews for the final heads-up notification layout.
5444 */
5445 public RemoteViews createHeadsUpContentView() {
5446 return createHeadsUpContentView(false /* useIncreasedHeight */);
5447 }
5448
5449 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08005450 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5451 *
Selim Cinekabcc2012019-07-23 18:44:07 -07005452 * @param isLowPriority is this notification low priority
Selim Cinek624c02db2015-12-14 21:00:02 -08005453 * @hide
5454 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005455 @UnsupportedAppUsage
Selim Cinekabcc2012019-07-23 18:44:07 -07005456 public RemoteViews makePublicContentView(boolean isLowPriority) {
Selim Cinek624c02db2015-12-14 21:00:02 -08005457 if (mN.publicVersion != null) {
5458 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Selim Cinekc3fec682019-06-06 18:11:07 -07005459 return builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08005460 }
5461 Bundle savedBundle = mN.extras;
5462 Style style = mStyle;
5463 mStyle = null;
5464 Icon largeIcon = mN.mLargeIcon;
5465 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07005466 Bitmap largeIconLegacy = mN.largeIcon;
5467 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005468 ArrayList<Action> actions = mActions;
5469 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08005470 Bundle publicExtras = new Bundle();
5471 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
5472 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
5473 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
5474 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07005475 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
5476 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cineked07b962018-04-30 14:39:35 -07005477 String appName = savedBundle.getString(EXTRA_SUBSTITUTE_APP_NAME);
5478 if (appName != null) {
5479 publicExtras.putString(EXTRA_SUBSTITUTE_APP_NAME, appName);
5480 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005481 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07005482 RemoteViews view;
Selim Cinekabcc2012019-07-23 18:44:07 -07005483 StandardTemplateParams params = mParams.reset().fillTextsFrom(this);
5484 if (isLowPriority) {
5485 params.forceDefaultColor();
5486 }
5487 view = makeNotificationHeader(params);
Lucas Dupin00be88f2019-01-03 17:50:52 -08005488 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
Selim Cinek624c02db2015-12-14 21:00:02 -08005489 mN.extras = savedBundle;
5490 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07005491 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005492 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08005493 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005494 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08005495 }
5496
Selim Cinek6743c0b2017-01-18 18:24:01 -08005497 /**
5498 * Construct a content view for the display when low - priority
5499 *
5500 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
5501 * a new subtext is created consisting of the content of the
5502 * notification.
5503 * @hide
5504 */
5505 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005506 StandardTemplateParams p = mParams.reset()
5507 .forceDefaultColor()
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005508 .fillTextsFrom(this);
5509 if (!useRegularSubtext || TextUtils.isEmpty(mParams.summaryText)) {
5510 p.summaryText(createSummaryText());
Selim Cinek6743c0b2017-01-18 18:24:01 -08005511 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005512 RemoteViews header = makeNotificationHeader(p);
Selim Cinek1b554392017-02-28 17:22:49 -08005513 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08005514 return header;
5515 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005516
Selim Cinek6743c0b2017-01-18 18:24:01 -08005517 private CharSequence createSummaryText() {
5518 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
5519 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
5520 return titleText;
5521 }
5522 SpannableStringBuilder summary = new SpannableStringBuilder();
5523 if (titleText == null) {
5524 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
5525 }
5526 BidiFormatter bidi = BidiFormatter.getInstance();
5527 if (titleText != null) {
5528 summary.append(bidi.unicodeWrap(titleText));
5529 }
5530 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
5531 if (titleText != null && contentText != null) {
5532 summary.append(bidi.unicodeWrap(mContext.getText(
5533 R.string.notification_header_divider_symbol_with_spaces)));
5534 }
5535 if (contentText != null) {
5536 summary.append(bidi.unicodeWrap(contentText));
5537 }
5538 return summary;
5539 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05005540
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005541 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005542 StandardTemplateParams p) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04005543 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07005544 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005545 emphazisedMode ? getEmphasizedActionLayoutResource()
5546 : tombstone ? getActionTombstoneLayoutResource()
5547 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04005548 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04005549 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04005550 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005551 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005552 if (action.mRemoteInputs != null) {
5553 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
5554 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005555 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07005556 // change the background bgColor
Selim Cinek981962e2016-07-20 20:41:58 -07005557 CharSequence title = action.title;
5558 ColorStateList[] outResultColor = null;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005559 int background = resolveBackgroundColor(p);
Selim Cinek981962e2016-07-20 20:41:58 -07005560 if (isLegacy()) {
Lucas Dupina291d192018-06-07 13:59:42 -07005561 title = ContrastColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07005562 } else {
5563 outResultColor = new ColorStateList[1];
Selim Cinek396caca2018-04-10 17:46:46 -07005564 title = ensureColorSpanContrast(title, background, outResultColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005565 }
Selim Cinek48f66b72017-08-18 16:17:51 -07005566 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005567 setTextViewColorPrimary(button, R.id.action0, p);
Selim Cinek396caca2018-04-10 17:46:46 -07005568 int rippleColor;
5569 boolean hasColorOverride = outResultColor != null && outResultColor[0] != null;
5570 if (hasColorOverride) {
5571 // There's a span spanning the full text, let's take it and use it as the
5572 // background color
5573 background = outResultColor[0].getDefaultColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005574 int textColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07005575 background, mInNightMode);
Selim Cinek396caca2018-04-10 17:46:46 -07005576 button.setTextColor(R.id.action0, textColor);
5577 rippleColor = textColor;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005578 } else if (getRawColor(p) != COLOR_DEFAULT && !isColorized(p)
Lucas Dupinf15c5e52019-04-01 19:00:12 -07005579 && mTintActionButtons && !mInNightMode) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005580 rippleColor = resolveContrastColor(p);
Selim Cinek396caca2018-04-10 17:46:46 -07005581 button.setTextColor(R.id.action0, rippleColor);
5582 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005583 rippleColor = getPrimaryTextColor(p);
Selim Cinek981962e2016-07-20 20:41:58 -07005584 }
Selim Cinek396caca2018-04-10 17:46:46 -07005585 // We only want about 20% alpha for the ripple
5586 rippleColor = (rippleColor & 0x00ffffff) | 0x33000000;
5587 button.setColorStateList(R.id.action0, "setRippleColor",
5588 ColorStateList.valueOf(rippleColor));
5589 button.setColorStateList(R.id.action0, "setButtonBackground",
5590 ColorStateList.valueOf(background));
5591 button.setBoolean(R.id.action0, "setHasStroke", !hasColorOverride);
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005592 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07005593 button.setTextViewText(R.id.action0, processTextSpans(
5594 processLegacyText(action.title)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005595 if (isColorized(p)) {
5596 setTextViewColorPrimary(button, R.id.action0, p);
5597 } else if (getRawColor(p) != COLOR_DEFAULT && mTintActionButtons) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005598 button.setTextColor(R.id.action0, resolveContrastColor(p));
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005599 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005600 }
Tony Mak7d4b3a52018-11-27 17:29:36 +00005601 button.setIntTag(R.id.action0, R.id.notification_action_index_tag,
5602 mActions.indexOf(action));
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005603 return button;
5604 }
5605
Joe Onoratocb109a02011-01-18 17:57:41 -08005606 /**
Selim Cinek981962e2016-07-20 20:41:58 -07005607 * Ensures contrast on color spans against a background color. also returns the color of the
5608 * text if a span was found that spans over the whole text.
5609 *
5610 * @param charSequence the charSequence on which the spans are
5611 * @param background the background color to ensure the contrast against
5612 * @param outResultColor an array in which a color will be returned as the first element if
5613 * there exists a full length color span.
5614 * @return the contrasted charSequence
5615 */
5616 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
5617 ColorStateList[] outResultColor) {
5618 if (charSequence instanceof Spanned) {
5619 Spanned ss = (Spanned) charSequence;
5620 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
5621 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
5622 for (Object span : spans) {
5623 Object resultSpan = span;
5624 int spanStart = ss.getSpanStart(span);
5625 int spanEnd = ss.getSpanEnd(span);
5626 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
5627 if (resultSpan instanceof CharacterStyle) {
5628 resultSpan = ((CharacterStyle) span).getUnderlying();
5629 }
5630 if (resultSpan instanceof TextAppearanceSpan) {
5631 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
5632 ColorStateList textColor = originalSpan.getTextColor();
5633 if (textColor != null) {
5634 int[] colors = textColor.getColors();
5635 int[] newColors = new int[colors.length];
5636 for (int i = 0; i < newColors.length; i++) {
Lucas Dupina291d192018-06-07 13:59:42 -07005637 newColors[i] = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005638 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005639 }
5640 textColor = new ColorStateList(textColor.getStates().clone(),
5641 newColors);
Selim Cinek396caca2018-04-10 17:46:46 -07005642 if (fullLength) {
5643 outResultColor[0] = textColor;
5644 // Let's drop the color from the span
5645 textColor = null;
5646 }
Selim Cinek981962e2016-07-20 20:41:58 -07005647 resultSpan = new TextAppearanceSpan(
5648 originalSpan.getFamily(),
5649 originalSpan.getTextStyle(),
5650 originalSpan.getTextSize(),
5651 textColor,
5652 originalSpan.getLinkTextColor());
Selim Cinek981962e2016-07-20 20:41:58 -07005653 }
5654 } else if (resultSpan instanceof ForegroundColorSpan) {
5655 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
5656 int foregroundColor = originalSpan.getForegroundColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005657 foregroundColor = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005658 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005659 if (fullLength) {
5660 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
Selim Cinek396caca2018-04-10 17:46:46 -07005661 resultSpan = null;
5662 } else {
5663 resultSpan = new ForegroundColorSpan(foregroundColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005664 }
5665 } else {
5666 resultSpan = span;
5667 }
Selim Cinek396caca2018-04-10 17:46:46 -07005668 if (resultSpan != null) {
5669 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
5670 }
Selim Cinek981962e2016-07-20 20:41:58 -07005671 }
5672 return builder;
5673 }
5674 return charSequence;
5675 }
5676
5677 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005678 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07005679 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005680 */
5681 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005682 if (!mIsLegacyInitialized) {
5683 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
5684 < Build.VERSION_CODES.LOLLIPOP;
5685 mIsLegacyInitialized = true;
5686 }
5687 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005688 }
5689
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005690 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08005691 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
Lucas Dupin00be88f2019-01-03 17:50:52 -08005692 if (isAlreadyLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005693 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005694 } else {
5695 return charSequence;
5696 }
5697 }
5698
Dan Sandler26e81cf2014-05-06 10:01:27 -04005699 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005700 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04005701 */
Adrian Roos487374f2017-01-11 15:48:14 -08005702 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005703 StandardTemplateParams p) {
Selim Cinekea4bef72015-12-02 15:51:10 -08005704 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005705 int color;
Lucas Dupin00be88f2019-01-03 17:50:52 -08005706 if (isColorized(p)) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005707 color = getPrimaryTextColor(p);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005708 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005709 color = resolveContrastColor(p);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005710 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005711 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07005712 contentView.setDrawableTint(R.id.icon, false, color,
5713 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005714
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005715 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005716 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08005717 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005718 }
5719
Dan Sandler26e81cf2014-05-06 10:01:27 -04005720 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005721 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04005722 * if it's grayscale).
5723 */
5724 // TODO: also check bounds, transparency, that sort of thing.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005725 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView,
5726 StandardTemplateParams p) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04005727 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005728 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005729 // resolve color will fall back to the default when legacy
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005730 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(p),
Sunny Goyal5b153922017-09-21 21:00:36 -07005731 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01005732 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005733 }
5734
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05005735 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005736 if (mN.color != COLOR_DEFAULT) {
5737 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02005738 }
Jorim Jaggi74419312014-06-10 20:57:21 +02005739 }
5740
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005741 int resolveContrastColor(StandardTemplateParams p) {
5742 int rawColor = getRawColor(p);
5743 if (mCachedContrastColorIsFor == rawColor && mCachedContrastColor != COLOR_INVALID) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08005744 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005745 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005746
Selim Cinekac5f0272017-05-02 16:05:41 -07005747 int color;
Selim Cinekc7f5a822018-03-20 19:32:06 -07005748 int background = mContext.getColor(
5749 com.android.internal.R.color.notification_material_background_color);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005750 if (rawColor == COLOR_DEFAULT) {
5751 ensureColors(p);
Lucas Dupinf03e7522018-06-25 16:21:13 -07005752 color = ContrastColorUtil.resolveDefaultColor(mContext, background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005753 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005754 color = ContrastColorUtil.resolveContrastColor(mContext, rawColor,
Anthony Chenad4d1582017-04-10 16:07:58 -07005755 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005756 }
5757 if (Color.alpha(color) < 255) {
5758 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005759 color = ContrastColorUtil.compositeColors(color, background);
Selim Cinekac5f0272017-05-02 16:05:41 -07005760 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005761 mCachedContrastColorIsFor = rawColor;
Selim Cinekac5f0272017-05-02 16:05:41 -07005762 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005763 }
5764
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005765 /**
5766 * Return the raw color of this Notification, which doesn't necessarily satisfy contrast.
5767 *
5768 * @see #resolveContrastColor(StandardTemplateParams) for the contrasted color
5769 * @param p the template params to inflate this with
5770 */
5771 private int getRawColor(StandardTemplateParams p) {
5772 if (p.forceDefaultColor) {
5773 return COLOR_DEFAULT;
5774 }
5775 return mN.color;
5776 }
5777
Selim Cinek4717d862018-04-19 09:19:15 +08005778 int resolveNeutralColor() {
5779 if (mNeutralColor != COLOR_INVALID) {
5780 return mNeutralColor;
5781 }
5782 int background = mContext.getColor(
5783 com.android.internal.R.color.notification_material_background_color);
Lucas Dupinf03e7522018-06-25 16:21:13 -07005784 mNeutralColor = ContrastColorUtil.resolveDefaultColor(mContext, background,
5785 mInNightMode);
Selim Cinek4717d862018-04-19 09:19:15 +08005786 if (Color.alpha(mNeutralColor) < 255) {
5787 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005788 mNeutralColor = ContrastColorUtil.compositeColors(mNeutralColor, background);
Selim Cinek4717d862018-04-19 09:19:15 +08005789 }
5790 return mNeutralColor;
5791 }
5792
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005793 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005794 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005795 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08005796 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005797 @NonNull
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005798 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005799 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005800 mN.actions = new Action[mActions.size()];
5801 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005802 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005803 if (!mPersonList.isEmpty()) {
Selim Cineke7238dd2017-12-14 17:48:32 -08005804 mN.extras.putParcelableArrayList(EXTRA_PEOPLE_LIST, mPersonList);
Dan Sandler0bf2ed82013-12-21 23:33:41 -06005805 }
Selim Cinek247fa012016-02-18 09:50:48 -08005806 if (mN.bigContentView != null || mN.contentView != null
5807 || mN.headsUpContentView != null) {
5808 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
5809 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005810 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08005811 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005812
Julia Reynolds3b848122016-02-26 10:45:32 -05005813 /**
5814 * Creates a Builder from an existing notification so further changes can be made.
5815 * @param context The context for your application / activity.
5816 * @param n The notification to create a Builder from.
5817 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005818 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005819 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005820 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005821 ApplicationInfo applicationInfo = n.extras.getParcelable(
5822 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005823 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05005824 if (applicationInfo != null) {
5825 try {
5826 builderContext = context.createApplicationContext(applicationInfo,
5827 Context.CONTEXT_RESTRICTED);
5828 } catch (NameNotFoundException e) {
5829 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
5830 builderContext = context; // try with our context
5831 }
5832 } else {
5833 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02005834 }
5835
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005836 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005837 }
5838
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005839 /**
Gustav Sennton761884c2018-11-19 17:40:19 +00005840 * Determines whether the platform can generate contextual actions for a notification.
5841 * By default this is true.
5842 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005843 @NonNull
Gustav Sennton761884c2018-11-19 17:40:19 +00005844 public Builder setAllowSystemGeneratedContextualActions(boolean allowed) {
5845 mN.mAllowSystemGeneratedContextualActions = allowed;
5846 return this;
5847 }
5848
5849 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005850 * @deprecated Use {@link #build()} instead.
5851 */
5852 @Deprecated
5853 public Notification getNotification() {
5854 return build();
5855 }
5856
5857 /**
5858 * Combine all of the options that have been set and return a new {@link Notification}
5859 * object.
5860 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005861 @NonNull
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005862 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005863 // first, add any extras from the calling code
5864 if (mUserExtras != null) {
5865 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005866 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005867
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005868 mN.creationTime = System.currentTimeMillis();
5869
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005870 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05005871 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02005872
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005873 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005874
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005875 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02005876 mStyle.reduceImageSizes(mContext);
5877 mStyle.purgeResources();
Selim Cinek90343862018-02-01 11:07:11 -08005878 mStyle.validate(mContext);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005879 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005880 }
Selim Cinekd0426622017-07-11 13:19:59 +02005881 mN.reduceImageSizes(mContext);
5882
Adrian Roos5081c0d2016-02-26 16:04:19 -08005883 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07005884 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005885 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005886 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005887 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
5888 mN.contentView.getSequenceNumber());
5889 }
5890 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005891 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005892 if (mN.bigContentView != null) {
5893 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
5894 mN.bigContentView.getSequenceNumber());
5895 }
5896 }
5897 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005898 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005899 if (mN.headsUpContentView != null) {
5900 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
5901 mN.headsUpContentView.getSequenceNumber());
5902 }
5903 }
5904 }
5905
Julia Reynolds4c0c2022016-02-02 15:11:59 -05005906 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
5907 mN.flags |= FLAG_SHOW_LIGHTS;
5908 }
5909
Adrian Roosfb921842017-10-26 14:49:56 +02005910 mN.allPendingIntents = null;
5911
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005912 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005913 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005914
5915 /**
5916 * Apply this Builder to an existing {@link Notification} object.
5917 *
5918 * @hide
5919 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005920 @NonNull
5921 public Notification buildInto(@NonNull Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005922 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005923 return n;
5924 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005925
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005926 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005927 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005928 * change. Also removes extenders on low ram devices, as
5929 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005930 *
5931 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5932 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005933 * @hide
5934 */
Kristian Monsen05f34792018-04-09 10:27:16 +02005935 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam,
5936 Context context) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005937 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005938
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005939 // Only strip views for known Styles because we won't know how to
5940 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005941 if (!isLowRam
5942 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005943 && getNotificationStyleClass(templateClass) == null) {
5944 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005945 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005946
5947 // Only strip unmodified BuilderRemoteViews.
5948 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005949 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005950 n.contentView.getSequenceNumber();
5951 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005952 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005953 n.bigContentView.getSequenceNumber();
5954 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005955 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005956 n.headsUpContentView.getSequenceNumber();
5957
5958 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005959 if (!isLowRam
5960 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005961 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005962 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005963
5964 Notification clone = n.clone();
5965 if (stripContentView) {
5966 clone.contentView = null;
5967 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5968 }
5969 if (stripBigContentView) {
5970 clone.bigContentView = null;
5971 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5972 }
5973 if (stripHeadsUpContentView) {
5974 clone.headsUpContentView = null;
5975 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5976 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005977 if (isLowRam) {
Kristian Monsen05f34792018-04-09 10:27:16 +02005978 String[] allowedServices = context.getResources().getStringArray(
5979 R.array.config_allowedManagedServicesOnLowRamDevices);
5980 if (allowedServices.length == 0) {
5981 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5982 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5983 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5984 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005985 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005986 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005987 }
5988
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005989 @UnsupportedAppUsage
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005990 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005991 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005992 }
5993
5994 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005995 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005996 }
5997
5998 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005999 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006000 }
6001
6002 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02006003 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006004 }
6005
6006 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07006007 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006008 }
6009
Adrian Roosc1a80b02016-04-05 14:54:55 -07006010 private int getMessagingLayoutResource() {
6011 return R.layout.notification_template_material_messaging;
6012 }
6013
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006014 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07006015 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006016 }
6017
Selim Cinek06e9e1f2016-07-08 17:14:16 -07006018 private int getEmphasizedActionLayoutResource() {
6019 return R.layout.notification_material_action_emphasized;
6020 }
6021
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006022 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07006023 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006024 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08006025
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006026 private int getBackgroundColor(StandardTemplateParams p) {
6027 if (isColorized(p)) {
6028 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : getRawColor(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08006029 } else {
Selim Cinekc7f5a822018-03-20 19:32:06 -07006030 return COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08006031 }
6032 }
6033
Selim Cinek396caca2018-04-10 17:46:46 -07006034 /**
Selim Cinek4717d862018-04-19 09:19:15 +08006035 * Gets a neutral color that can be used for icons or similar that should not stand out.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006036 * @param p the template params to inflate this with
Selim Cinek4717d862018-04-19 09:19:15 +08006037 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006038 private int getNeutralColor(StandardTemplateParams p) {
6039 if (isColorized(p)) {
6040 return getSecondaryTextColor(p);
Selim Cinek4717d862018-04-19 09:19:15 +08006041 } else {
6042 return resolveNeutralColor();
6043 }
6044 }
6045
6046 /**
Selim Cinek396caca2018-04-10 17:46:46 -07006047 * Same as getBackgroundColor but also resolved the default color to the background.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006048 * @param p the template params to inflate this with
Selim Cinek396caca2018-04-10 17:46:46 -07006049 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006050 private int resolveBackgroundColor(StandardTemplateParams p) {
6051 int backgroundColor = getBackgroundColor(p);
Selim Cinek396caca2018-04-10 17:46:46 -07006052 if (backgroundColor == COLOR_DEFAULT) {
6053 backgroundColor = mContext.getColor(
6054 com.android.internal.R.color.notification_material_background_color);
6055 }
6056 return backgroundColor;
6057 }
6058
Anthony Chenad4d1582017-04-10 16:07:58 -07006059 private boolean shouldTintActionButtons() {
6060 return mTintActionButtons;
6061 }
6062
Selim Cinek99104832017-01-25 14:47:33 -08006063 private boolean textColorsNeedInversion() {
6064 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
6065 return false;
6066 }
6067 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
6068 return targetSdkVersion > Build.VERSION_CODES.M
6069 && targetSdkVersion < Build.VERSION_CODES.O;
6070 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07006071
6072 /**
6073 * Set a color palette to be used as the background and textColors
6074 *
6075 * @param backgroundColor the color to be used as the background
6076 * @param foregroundColor the color to be used as the foreground
6077 *
6078 * @hide
6079 */
6080 public void setColorPalette(int backgroundColor, int foregroundColor) {
6081 mBackgroundColor = backgroundColor;
6082 mForegroundColor = foregroundColor;
6083 mTextColorsAreForBackground = COLOR_INVALID;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006084 ensureColors(mParams.reset().fillTextsFrom(this));
Selim Cinek5fb73f82017-04-20 16:55:38 -07006085 }
Selim Cinekac5f0272017-05-02 16:05:41 -07006086
6087 /**
Selim Cineka7679b62017-05-10 16:33:25 -07006088 * Forces all styled remoteViews to be built from scratch and not use any cached
6089 * RemoteViews.
6090 * This is needed for legacy apps that are baking in their remoteviews into the
6091 * notification.
6092 *
6093 * @hide
6094 */
6095 public void setRebuildStyledRemoteViews(boolean rebuild) {
6096 mRebuildStyledRemoteViews = rebuild;
6097 }
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006098
6099 /**
6100 * Get the text that should be displayed in the statusBar when heads upped. This is
6101 * usually just the app name, but may be different depending on the style.
6102 *
6103 * @param publicMode If true, return a text that is safe to display in public.
6104 *
6105 * @hide
6106 */
6107 public CharSequence getHeadsUpStatusBarText(boolean publicMode) {
6108 if (mStyle != null && !publicMode) {
6109 CharSequence text = mStyle.getHeadsUpStatusBarText();
6110 if (!TextUtils.isEmpty(text)) {
6111 return text;
6112 }
6113 }
6114 return loadHeaderAppName();
6115 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08006116 }
6117
6118 /**
Selim Cinekd0426622017-07-11 13:19:59 +02006119 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
6120 * remote views.
6121 *
6122 * @hide
6123 */
6124 void reduceImageSizes(Context context) {
6125 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
6126 return;
6127 }
Selim Cineka8cb1262017-08-15 16:53:44 -07006128 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006129 if (mLargeIcon != null || largeIcon != null) {
6130 Resources resources = context.getResources();
6131 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07006132 int maxWidth = resources.getDimensionPixelSize(isLowRam
6133 ? R.dimen.notification_right_icon_size_low_ram
6134 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006135 int maxHeight = maxWidth;
6136 if (MediaStyle.class.equals(style)
6137 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006138 maxHeight = resources.getDimensionPixelSize(isLowRam
6139 ? R.dimen.notification_media_image_max_height_low_ram
6140 : R.dimen.notification_media_image_max_height);
6141 maxWidth = resources.getDimensionPixelSize(isLowRam
6142 ? R.dimen.notification_media_image_max_width_low_ram
6143 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006144 }
6145 if (mLargeIcon != null) {
6146 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
6147 }
6148 if (largeIcon != null) {
6149 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
6150 }
6151 }
Selim Cineka8cb1262017-08-15 16:53:44 -07006152 reduceImageSizesForRemoteView(contentView, context, isLowRam);
6153 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
6154 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02006155 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
6156 }
6157
Selim Cineka8cb1262017-08-15 16:53:44 -07006158 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
6159 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02006160 if (remoteView != null) {
6161 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006162 int maxWidth = resources.getDimensionPixelSize(isLowRam
6163 ? R.dimen.notification_custom_view_max_image_width_low_ram
6164 : R.dimen.notification_custom_view_max_image_width);
6165 int maxHeight = resources.getDimensionPixelSize(isLowRam
6166 ? R.dimen.notification_custom_view_max_image_height_low_ram
6167 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02006168 remoteView.reduceImageSizes(maxWidth, maxHeight);
6169 }
6170 }
6171
6172 /**
Selim Cinek22714f12017-04-13 16:23:53 -07006173 * @return whether this notification is a foreground service notification
Gus Prevasb0c1a462018-11-05 11:06:15 -05006174 * @hide
Selim Cinek7b9605b2017-01-19 17:36:00 -08006175 */
Gus Prevasb0c1a462018-11-05 11:06:15 -05006176 public boolean isForegroundService() {
Selim Cinek22714f12017-04-13 16:23:53 -07006177 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08006178 }
6179
6180 /**
Selim Cinek99104832017-01-25 14:47:33 -08006181 * @return whether this notification has a media session attached
6182 * @hide
6183 */
6184 public boolean hasMediaSession() {
6185 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
6186 }
6187
6188 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006189 * @return the style class of this notification
6190 * @hide
6191 */
6192 public Class<? extends Notification.Style> getNotificationStyle() {
6193 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
6194
6195 if (!TextUtils.isEmpty(templateClass)) {
6196 return Notification.getNotificationStyleClass(templateClass);
6197 }
6198 return null;
6199 }
6200
6201 /**
Selim Cinek22714f12017-04-13 16:23:53 -07006202 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08006203 *
6204 * @hide
6205 */
6206 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07006207 if (isColorizedMedia()) {
6208 return true;
6209 }
Julia Reynolds4db59552017-06-30 13:34:01 -04006210 return extras.getBoolean(EXTRA_COLORIZED)
6211 && (hasColorizedPermission() || isForegroundService());
6212 }
6213
6214 /**
6215 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
6216 * permission. The permission is checked when a notification is enqueued.
6217 */
6218 private boolean hasColorizedPermission() {
6219 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07006220 }
6221
6222 /**
6223 * @return true if this notification is colorized and it is a media notification
6224 *
6225 * @hide
6226 */
6227 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08006228 Class<? extends Style> style = getNotificationStyle();
6229 if (MediaStyle.class.equals(style)) {
6230 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
6231 if ((colorized == null || colorized) && hasMediaSession()) {
6232 return true;
6233 }
6234 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6235 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
6236 return true;
6237 }
6238 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07006239 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006240 }
6241
Selim Cinek0847acd2017-04-24 19:48:29 -07006242
6243 /**
6244 * @return true if this is a media notification
6245 *
6246 * @hide
6247 */
6248 public boolean isMediaNotification() {
6249 Class<? extends Style> style = getNotificationStyle();
6250 if (MediaStyle.class.equals(style)) {
6251 return true;
6252 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6253 return true;
6254 }
6255 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006256 }
6257
Mady Mellor7eb18ef2019-03-27 14:03:46 -07006258 /**
Julia Tuttle28fb1a92019-05-03 18:07:45 -04006259 * @return true if this notification is showing as a bubble
Mady Mellor7eb18ef2019-03-27 14:03:46 -07006260 *
6261 * @hide
6262 */
6263 public boolean isBubbleNotification() {
6264 return (flags & Notification.FLAG_BUBBLE) != 0;
6265 }
6266
Selim Cinek279fa862016-06-14 10:57:25 -07006267 private boolean hasLargeIcon() {
6268 return mLargeIcon != null || largeIcon != null;
6269 }
6270
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006271 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006272 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006273 * @hide
6274 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07006275 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006276 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
6277 }
6278
6279 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006280 * @return true if the notification will show a chronometer; false otherwise
6281 * @hide
6282 */
6283 public boolean showsChronometer() {
6284 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
6285 }
6286
6287 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04006288 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05006289 */
6290 @SystemApi
6291 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
6292 Class<? extends Style>[] classes = new Class[] {
6293 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
6294 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
6295 MessagingStyle.class };
6296 for (Class<? extends Style> innerClass : classes) {
6297 if (templateClass.equals(innerClass.getName())) {
6298 return innerClass;
6299 }
6300 }
6301 return null;
6302 }
6303
6304 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006305 * An object that can apply a rich notification style to a {@link Notification.Builder}
6306 * object.
6307 */
Griff Hazendfcb0802014-02-11 12:00:00 -08006308 public static abstract class Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07006309
6310 /**
6311 * The number of items allowed simulatanously in the remote input history.
6312 * @hide
6313 */
6314 static final int MAX_REMOTE_INPUT_HISTORY_LINES = 3;
Chris Wrend6297db2012-05-03 16:20:13 -04006315 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006316
6317 /**
6318 * @hide
6319 */
6320 protected CharSequence mSummaryText = null;
6321
6322 /**
6323 * @hide
6324 */
6325 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04006326
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006327 protected Builder mBuilder;
6328
Chris Wrend6297db2012-05-03 16:20:13 -04006329 /**
6330 * Overrides ContentTitle in the big form of the template.
6331 * This defaults to the value passed to setContentTitle().
6332 */
6333 protected void internalSetBigContentTitle(CharSequence title) {
6334 mBigContentTitle = title;
6335 }
6336
6337 /**
6338 * Set the first line of text after the detail section in the big form of the template.
6339 */
6340 protected void internalSetSummaryText(CharSequence cs) {
6341 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04006342 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04006343 }
6344
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006345 public void setBuilder(Builder builder) {
6346 if (mBuilder != builder) {
6347 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07006348 if (mBuilder != null) {
6349 mBuilder.setStyle(this);
6350 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006351 }
6352 }
6353
Chris Wrend6297db2012-05-03 16:20:13 -04006354 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006355 if (mBuilder == null) {
6356 throw new IllegalArgumentException("Style requires a valid Builder object");
6357 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006358 }
Chris Wrend6297db2012-05-03 16:20:13 -04006359
6360 protected RemoteViews getStandardView(int layoutId) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006361 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder);
6362 return getStandardView(layoutId, p, null);
Selim Cinek384804b2018-04-18 14:31:07 +08006363 }
6364
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006365
Selim Cinek384804b2018-04-18 14:31:07 +08006366 /**
6367 * Get the standard view for this style.
6368 *
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006369 * @param layoutId The layout id to use.
6370 * @param p the params for this inflation.
Selim Cinek384804b2018-04-18 14:31:07 +08006371 * @param result The result where template bind information is saved.
6372 * @return A remoteView for this style.
6373 * @hide
6374 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006375 protected RemoteViews getStandardView(int layoutId, StandardTemplateParams p,
6376 TemplateBindResult result) {
Chris Wrend6297db2012-05-03 16:20:13 -04006377 checkBuilder();
6378
6379 if (mBigContentTitle != null) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006380 p.title = mBigContentTitle;
Chris Wrend6297db2012-05-03 16:20:13 -04006381 }
6382
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006383 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId, p,
6384 result);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006385
Chris Wrend6297db2012-05-03 16:20:13 -04006386 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
6387 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04006388 } else {
6389 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04006390 }
6391
Chris Wrend6297db2012-05-03 16:20:13 -04006392 return contentView;
6393 }
6394
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006395 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006396 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006397 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08006398 *
6399 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006400 * @hide
6401 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08006402 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006403 return null;
6404 }
6405
6406 /**
6407 * Construct a Style-specific RemoteViews for the final big notification layout.
6408 * @hide
6409 */
6410 public RemoteViews makeBigContentView() {
6411 return null;
6412 }
6413
6414 /**
6415 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08006416 *
6417 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006418 * @hide
6419 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006420 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006421 return null;
6422 }
6423
6424 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006425 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006426 * @hide
6427 */
6428 public void addExtras(Bundle extras) {
6429 if (mSummaryTextSet) {
6430 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
6431 }
6432 if (mBigContentTitle != null) {
6433 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
6434 }
Chris Wren91ad5632013-06-05 15:05:57 -04006435 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006436 }
6437
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006438 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006439 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006440 * @hide
6441 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02006442 protected void restoreFromExtras(Bundle extras) {
6443 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
6444 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
6445 mSummaryTextSet = true;
6446 }
6447 if (extras.containsKey(EXTRA_TITLE_BIG)) {
6448 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
6449 }
6450 }
6451
6452
6453 /**
6454 * @hide
6455 */
6456 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006457 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006458 return wip;
6459 }
6460
Daniel Sandler0ec46202015-06-24 01:27:05 -04006461 /**
6462 * @hide
6463 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006464 public void purgeResources() {}
6465
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006466 /**
6467 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
6468 * attached to.
6469 *
6470 * @return the fully constructed Notification.
6471 */
6472 public Notification build() {
6473 checkBuilder();
6474 return mBuilder.build();
6475 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006476
6477 /**
6478 * @hide
6479 * @return true if the style positions the progress bar on the second line; false if the
6480 * style hides the progress bar
6481 */
6482 protected boolean hasProgress() {
6483 return true;
6484 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006485
6486 /**
6487 * @hide
6488 * @return Whether we should put the summary be put into the notification header
6489 */
6490 public boolean hasSummaryInHeader() {
6491 return true;
6492 }
Selim Cinek593610c2016-02-16 18:42:57 -08006493
6494 /**
6495 * @hide
6496 * @return Whether custom content views are displayed inline in the style
6497 */
6498 public boolean displayCustomViewInline() {
6499 return false;
6500 }
Selim Cinekd0426622017-07-11 13:19:59 +02006501
6502 /**
6503 * Reduces the image sizes contained in this style.
6504 *
6505 * @hide
6506 */
6507 public void reduceImageSizes(Context context) {
6508 }
Selim Cinek90343862018-02-01 11:07:11 -08006509
6510 /**
6511 * Validate that this style was properly composed. This is called at build time.
6512 * @hide
6513 */
6514 public void validate(Context context) {
6515 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006516
6517 /**
6518 * @hide
6519 */
6520 public abstract boolean areNotificationsVisiblyDifferent(Style other);
Selim Cinekc7f5a822018-03-20 19:32:06 -07006521
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006522 /**
koprivaa1a78482018-10-09 10:09:23 -07006523 * @return the text that should be displayed in the statusBar when heads-upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006524 * If {@code null} is returned, the default implementation will be used.
6525 *
6526 * @hide
6527 */
6528 public CharSequence getHeadsUpStatusBarText() {
6529 return null;
6530 }
Joe Onorato46439ce2010-11-19 13:56:21 -08006531 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006532
6533 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006534 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08006535 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006536 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006537 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006538 * Notification notif = new Notification.Builder(mContext)
6539 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
6540 * .setContentText(subject)
6541 * .setSmallIcon(R.drawable.new_post)
6542 * .setLargeIcon(aBitmap)
6543 * .setStyle(new Notification.BigPictureStyle()
6544 * .bigPicture(aBigBitmap))
6545 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006546 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006547 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006548 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006549 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006550 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006551 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006552 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006553 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006554
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006555 public BigPictureStyle() {
6556 }
6557
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006558 /**
6559 * @deprecated use {@code BigPictureStyle()}.
6560 */
6561 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006562 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006563 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006564 }
6565
Chris Wrend6297db2012-05-03 16:20:13 -04006566 /**
6567 * Overrides ContentTitle in the big form of the template.
6568 * This defaults to the value passed to setContentTitle().
6569 */
6570 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006571 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006572 return this;
6573 }
6574
6575 /**
6576 * Set the first line of text after the detail section in the big form of the template.
6577 */
6578 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006579 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006580 return this;
6581 }
6582
Chris Wren0bd664d2012-08-01 13:56:56 -04006583 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05006584 * @hide
6585 */
6586 public Bitmap getBigPicture() {
6587 return mPicture;
6588 }
6589
6590 /**
Chris Wren0bd664d2012-08-01 13:56:56 -04006591 * Provide the bitmap to be used as the payload for the BigPicture notification.
6592 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006593 public BigPictureStyle bigPicture(Bitmap b) {
6594 mPicture = b;
6595 return this;
6596 }
6597
Chris Wren3745a3d2012-05-22 15:11:52 -04006598 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04006599 * Override the large icon when the big notification is shown.
6600 */
6601 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04006602 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
6603 }
6604
6605 /**
6606 * Override the large icon when the big notification is shown.
6607 */
6608 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04006609 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006610 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006611 return this;
6612 }
6613
Riley Andrews0394a0c2015-11-03 23:36:52 -08006614 /** @hide */
6615 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
6616
Daniel Sandler0ec46202015-06-24 01:27:05 -04006617 /**
6618 * @hide
6619 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006620 @Override
6621 public void purgeResources() {
6622 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08006623 if (mPicture != null &&
6624 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08006625 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006626 mPicture = mPicture.createAshmemBitmap();
6627 }
6628 if (mBigLargeIcon != null) {
6629 mBigLargeIcon.convertToAshmem();
6630 }
6631 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01006632
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006633 /**
6634 * @hide
6635 */
Selim Cinekd0426622017-07-11 13:19:59 +02006636 @Override
6637 public void reduceImageSizes(Context context) {
6638 super.reduceImageSizes(context);
6639 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006640 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006641 if (mPicture != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006642 int maxPictureWidth = resources.getDimensionPixelSize(isLowRam
6643 ? R.dimen.notification_big_picture_max_height_low_ram
6644 : R.dimen.notification_big_picture_max_height);
6645 int maxPictureHeight = resources.getDimensionPixelSize(isLowRam
6646 ? R.dimen.notification_big_picture_max_width_low_ram
6647 : R.dimen.notification_big_picture_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006648 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
6649 }
6650 if (mBigLargeIcon != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006651 int rightIconSize = resources.getDimensionPixelSize(isLowRam
6652 ? R.dimen.notification_right_icon_size_low_ram
6653 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006654 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
6655 }
6656 }
6657
6658 /**
6659 * @hide
6660 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006661 public RemoteViews makeBigContentView() {
6662 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01006663 // This covers the following cases:
6664 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006665 // mN.mLargeIcon
6666 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04006667 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07006668 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006669 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006670 oldLargeIcon = mBuilder.mN.mLargeIcon;
6671 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006672 // The legacy largeIcon might not allow us to clear the image, as it's taken in
6673 // replacement if the other one is null. Because we're restoring these legacy icons
6674 // for old listeners, this is in general non-null.
6675 largeIconLegacy = mBuilder.mN.largeIcon;
6676 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006677 }
6678
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006679 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder);
Selim Cinek384804b2018-04-18 14:31:07 +08006680 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource(),
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006681 p, null /* result */);
Selim Cinek03d0d652015-11-13 13:18:09 -05006682 if (mSummaryTextSet) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006683 contentView.setTextViewText(R.id.text, mBuilder.processTextSpans(
6684 mBuilder.processLegacyText(mSummaryText)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006685 mBuilder.setTextViewColorSecondary(contentView, R.id.text, p);
Selim Cinekc848c3a2016-01-13 15:27:30 -08006686 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05006687 }
Selim Cinek279fa862016-06-14 10:57:25 -07006688 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08006689
Christoph Studer5c510ee2014-12-15 16:32:27 +01006690 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006691 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006692 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006693 }
6694
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006695 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006696 return contentView;
6697 }
6698
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006699 /**
6700 * @hide
6701 */
6702 public void addExtras(Bundle extras) {
6703 super.addExtras(extras);
6704
6705 if (mBigLargeIconSet) {
6706 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
6707 }
6708 extras.putParcelable(EXTRA_PICTURE, mPicture);
6709 }
6710
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006711 /**
6712 * @hide
6713 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006714 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02006715 protected void restoreFromExtras(Bundle extras) {
6716 super.restoreFromExtras(extras);
6717
6718 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01006719 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02006720 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04006721 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006722 mPicture = extras.getParcelable(EXTRA_PICTURE);
6723 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006724
6725 /**
6726 * @hide
6727 */
6728 @Override
6729 public boolean hasSummaryInHeader() {
6730 return false;
6731 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006732
6733 /**
6734 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006735 * Note that we aren't actually comparing the contents of the bitmaps here, so this
6736 * is only doing a cursory inspection. Bitmaps of equal size will appear the same.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006737 */
6738 @Override
6739 public boolean areNotificationsVisiblyDifferent(Style other) {
6740 if (other == null || getClass() != other.getClass()) {
6741 return true;
6742 }
6743 BigPictureStyle otherS = (BigPictureStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006744 return areBitmapsObviouslyDifferent(getBigPicture(), otherS.getBigPicture());
6745 }
6746
6747 private static boolean areBitmapsObviouslyDifferent(Bitmap a, Bitmap b) {
6748 if (a == b) {
6749 return false;
6750 }
6751 if (a == null || b == null) {
6752 return true;
6753 }
6754 return a.getWidth() != b.getWidth()
6755 || a.getHeight() != b.getHeight()
6756 || a.getConfig() != b.getConfig()
6757 || a.getGenerationId() != b.getGenerationId();
Julia Reynolds7217dc92018-03-07 12:12:09 -05006758 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006759 }
6760
6761 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006762 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08006763 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006764 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006765 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006766 * Notification notif = new Notification.Builder(mContext)
6767 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
6768 * .setContentText(subject)
6769 * .setSmallIcon(R.drawable.new_mail)
6770 * .setLargeIcon(aBitmap)
6771 * .setStyle(new Notification.BigTextStyle()
6772 * .bigText(aVeryLongString))
6773 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006774 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006775 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006776 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006777 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006778 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006779
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006780 private CharSequence mBigText;
6781
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006782 public BigTextStyle() {
6783 }
6784
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006785 /**
6786 * @deprecated use {@code BigTextStyle()}.
6787 */
6788 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006789 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006790 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006791 }
6792
Chris Wrend6297db2012-05-03 16:20:13 -04006793 /**
6794 * Overrides ContentTitle in the big form of the template.
6795 * This defaults to the value passed to setContentTitle().
6796 */
6797 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006798 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006799 return this;
6800 }
6801
6802 /**
6803 * Set the first line of text after the detail section in the big form of the template.
6804 */
6805 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006806 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006807 return this;
6808 }
6809
Chris Wren0bd664d2012-08-01 13:56:56 -04006810 /**
6811 * Provide the longer text to be displayed in the big form of the
6812 * template in place of the content text.
6813 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006814 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006815 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006816 return this;
6817 }
6818
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006819 /**
6820 * @hide
6821 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05006822 public CharSequence getBigText() {
6823 return mBigText;
6824 }
6825
6826 /**
6827 * @hide
6828 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006829 public void addExtras(Bundle extras) {
6830 super.addExtras(extras);
6831
Christoph Studer4600f9b2014-07-22 22:44:43 +02006832 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
6833 }
6834
6835 /**
6836 * @hide
6837 */
6838 @Override
6839 protected void restoreFromExtras(Bundle extras) {
6840 super.restoreFromExtras(extras);
6841
6842 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006843 }
6844
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006845 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006846 * @param increasedHeight true if this layout be created with an increased height.
6847 *
6848 * @hide
6849 */
6850 @Override
6851 public RemoteViews makeContentView(boolean increasedHeight) {
6852 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006853 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006854 mBuilder.mActions = new ArrayList<>();
6855 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006856 mBuilder.mActions = mBuilder.mOriginalActions;
6857 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006858 return remoteViews;
6859 }
6860 return super.makeContentView(increasedHeight);
6861 }
6862
6863 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006864 * @hide
6865 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006866 @Override
6867 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
6868 if (increasedHeight && mBuilder.mActions.size() > 0) {
6869 return makeBigContentView();
6870 }
6871 return super.makeHeadsUpContentView(increasedHeight);
6872 }
6873
6874 /**
6875 * @hide
6876 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006877 public RemoteViews makeBigContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006878 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder).text(null);
Selim Cinek384804b2018-04-18 14:31:07 +08006879 TemplateBindResult result = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006880 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource(), p,
6881 result);
Selim Cinek1c72fa02018-04-23 18:00:54 +08006882 contentView.setInt(R.id.big_text, "setImageEndMargin", result.getIconMarginEnd());
Joe Malin8d40d042012-11-05 11:36:40 -08006883
Selim Cinek3a2c4b92015-12-17 17:01:17 -08006884 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07006885 if (TextUtils.isEmpty(bigTextText)) {
6886 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
6887 // experience
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006888 bigTextText = mBuilder.processLegacyText(
6889 mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT));
Selim Cinek75998782016-04-26 10:39:17 -07006890 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006891 contentView.setTextViewText(R.id.big_text, mBuilder.processTextSpans(bigTextText));
6892 mBuilder.setTextViewColorSecondary(contentView, R.id.big_text, p);
6893 contentView.setViewVisibility(R.id.big_text,
6894 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08006895 contentView.setBoolean(R.id.big_text, "setHasImage",
6896 result.isRightIconContainerVisible());
Selim Cinek4fb12d32015-11-19 18:10:48 -08006897
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006898 return contentView;
6899 }
6900
Julia Reynolds7217dc92018-03-07 12:12:09 -05006901 /**
6902 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006903 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006904 */
6905 @Override
6906 public boolean areNotificationsVisiblyDifferent(Style other) {
6907 if (other == null || getClass() != other.getClass()) {
6908 return true;
6909 }
6910 BigTextStyle newS = (BigTextStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006911 return !Objects.equals(String.valueOf(getBigText()), String.valueOf(newS.getBigText()));
Julia Reynolds7217dc92018-03-07 12:12:09 -05006912 }
6913
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006914 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006915
6916 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006917 * Helper class for generating large-format notifications that include multiple back-and-forth
6918 * messages of varying types between any number of people.
6919 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006920 * <p>
Alex Hillsfc737de2016-03-23 17:33:02 -04006921 * If the platform does not provide large-format notifications, this method has no effect. The
6922 * user will always see the normal notification view.
Selim Cinekce8794f2018-05-23 16:46:05 -07006923 *
6924 * <p>
6925 * If the app is targeting Android P and above, it is required to use the {@link Person}
6926 * class in order to get an optimal rendering of the notification and its avatars. For
6927 * conversations involving multiple people, the app should also make sure that it marks the
6928 * conversation as a group with {@link #setGroupConversation(boolean)}.
6929 *
6930 * <p>
6931 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior.
6932 * Here's an example of how this may be used:
Alex Hillsfc737de2016-03-23 17:33:02 -04006933 * <pre class="prettyprint">
6934 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006935 * Person user = new Person.Builder().setIcon(userIcon).setName(userName).build();
6936 * MessagingStyle style = new MessagingStyle(user)
6937 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getPerson())
6938 * .addMessage(messages[2].getText(), messages[2].getTime(), messages[2].getPerson())
6939 * .setGroupConversation(hasMultiplePeople());
6940 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006941 * Notification noti = new Notification.Builder()
Selim Cinekce8794f2018-05-23 16:46:05 -07006942 * .setContentTitle(&quot;2 new messages with &quot; + sender.toString())
Alex Hillsfc737de2016-03-23 17:33:02 -04006943 * .setContentText(subject)
6944 * .setSmallIcon(R.drawable.new_message)
6945 * .setLargeIcon(aBitmap)
Selim Cinekce8794f2018-05-23 16:46:05 -07006946 * .setStyle(style)
Alex Hillsfc737de2016-03-23 17:33:02 -04006947 * .build();
6948 * </pre>
6949 */
6950 public static class MessagingStyle extends Style {
6951
6952 /**
6953 * The maximum number of messages that will be retained in the Notification itself (the
6954 * number displayed is up to the platform).
6955 */
6956 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
6957
Selim Cinekcb8b9852017-12-15 18:01:52 -08006958 @NonNull Person mUser;
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006959 @Nullable CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04006960 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08006961 List<Message> mHistoricMessages = new ArrayList<>();
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006962 boolean mIsGroupConversation;
Alex Hillsfc737de2016-03-23 17:33:02 -04006963
6964 MessagingStyle() {
6965 }
6966
6967 /**
Alex Hillsfd590442016-10-07 09:52:44 -04006968 * @param userDisplayName Required - the name to be displayed for any replies sent by the
6969 * user before the posting app reposts the notification with those messages after they've
6970 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04006971 * {@link #addMessage(Notification.MessagingStyle.Message)}
Selim Cinekcb8b9852017-12-15 18:01:52 -08006972 *
6973 * @deprecated use {@code MessagingStyle(Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006974 */
Alex Hillsfd590442016-10-07 09:52:44 -04006975 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Selim Cinek9acd6732018-03-23 16:39:02 -07006976 this(new Person.Builder().setName(userDisplayName).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006977 }
6978
6979 /**
6980 * @param user Required - The person displayed for any messages that are sent by the
6981 * user. Any messages added with {@link #addMessage(Notification.MessagingStyle.Message)}
6982 * who don't have a Person associated with it will be displayed as if they were sent
Selim Cinek90343862018-02-01 11:07:11 -08006983 * by this user. The user also needs to have a valid name associated with it, which will
6984 * be enforced starting in Android P.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006985 */
6986 public MessagingStyle(@NonNull Person user) {
6987 mUser = user;
Selim Cinek90343862018-02-01 11:07:11 -08006988 }
6989
6990 /**
6991 * Validate that this style was properly composed. This is called at build time.
6992 * @hide
6993 */
6994 @Override
6995 public void validate(Context context) {
6996 super.validate(context);
6997 if (context.getApplicationInfo().targetSdkVersion
6998 >= Build.VERSION_CODES.P && (mUser == null || mUser.getName() == null)) {
6999 throw new RuntimeException("User must be valid and have a name.");
Selim Cinekcb8b9852017-12-15 18:01:52 -08007000 }
7001 }
7002
7003 /**
koprivaa1a78482018-10-09 10:09:23 -07007004 * @return the text that should be displayed in the statusBar when heads upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08007005 * If {@code null} is returned, the default implementation will be used.
7006 *
7007 * @hide
7008 */
7009 @Override
7010 public CharSequence getHeadsUpStatusBarText() {
7011 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
7012 ? super.mBigContentTitle
7013 : mConversationTitle;
7014 if (!TextUtils.isEmpty(conversationTitle) && !hasOnlyWhiteSpaceSenders()) {
7015 return conversationTitle;
7016 }
7017 return null;
7018 }
7019
7020 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08007021 * @return the user to be displayed for any replies sent by the user
7022 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007023 @NonNull
Selim Cinekcb8b9852017-12-15 18:01:52 -08007024 public Person getUser() {
7025 return mUser;
Alex Hillsfc737de2016-03-23 17:33:02 -04007026 }
7027
7028 /**
7029 * Returns the name to be displayed for any replies sent by the user
Selim Cinekcb8b9852017-12-15 18:01:52 -08007030 *
7031 * @deprecated use {@link #getUser()} instead
Alex Hillsfc737de2016-03-23 17:33:02 -04007032 */
7033 public CharSequence getUserDisplayName() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007034 return mUser.getName();
Alex Hillsfc737de2016-03-23 17:33:02 -04007035 }
7036
7037 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007038 * Sets the title to be displayed on this conversation. May be set to {@code null}.
7039 *
Kodlee Yin14656422017-12-22 17:00:46 -08007040 * <p>This API's behavior was changed in SDK version {@link Build.VERSION_CODES#P}. If your
7041 * application's target version is less than {@link Build.VERSION_CODES#P}, setting a
7042 * conversation title to a non-null value will make {@link #isGroupConversation()} return
7043 * {@code true} and passing {@code null} will make it return {@code false}. In
7044 * {@link Build.VERSION_CODES#P} and beyond, use {@link #setGroupConversation(boolean)}
7045 * to set group conversation status.
7046 *
7047 * @param conversationTitle Title displayed for this conversation
7048 * @return this object for method chaining
Alex Hillsfc737de2016-03-23 17:33:02 -04007049 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007050 public MessagingStyle setConversationTitle(@Nullable CharSequence conversationTitle) {
Alex Hillsfc737de2016-03-23 17:33:02 -04007051 mConversationTitle = conversationTitle;
7052 return this;
7053 }
7054
7055 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007056 * Return the title to be displayed on this conversation. May return {@code null}.
Alex Hillsfc737de2016-03-23 17:33:02 -04007057 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007058 @Nullable
Alex Hillsfc737de2016-03-23 17:33:02 -04007059 public CharSequence getConversationTitle() {
7060 return mConversationTitle;
7061 }
7062
7063 /**
7064 * Adds a message for display by this notification. Convenience call for a simple
7065 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
7066 * @param text A {@link CharSequence} to be displayed as the message content
7067 * @param timestamp Time at which the message arrived
7068 * @param sender A {@link CharSequence} to be used for displaying the name of the
7069 * sender. Should be <code>null</code> for messages by the current user, in which case
7070 * the platform will insert {@link #getUserDisplayName()}.
7071 * Should be unique amongst all individuals in the conversation, and should be
7072 * consistent during re-posts of the notification.
7073 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07007074 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Alex Hillsfc737de2016-03-23 17:33:02 -04007075 *
7076 * @return this object for method chaining
Selim Cinekcb8b9852017-12-15 18:01:52 -08007077 *
7078 * @deprecated use {@link #addMessage(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04007079 */
7080 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007081 return addMessage(text, timestamp,
Selim Cinek9acd6732018-03-23 16:39:02 -07007082 sender == null ? null : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08007083 }
7084
7085 /**
7086 * Adds a message for display by this notification. Convenience call for a simple
7087 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
7088 * @param text A {@link CharSequence} to be displayed as the message content
7089 * @param timestamp Time at which the message arrived
7090 * @param sender The {@link Person} who sent the message.
7091 * Should be <code>null</code> for messages by the current user, in which case
7092 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7093 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07007094 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Selim Cinekcb8b9852017-12-15 18:01:52 -08007095 *
7096 * @return this object for method chaining
7097 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007098 public MessagingStyle addMessage(@NonNull CharSequence text, long timestamp,
7099 @Nullable Person sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08007100 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04007101 }
7102
7103 /**
7104 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08007105 *
7106 * <p>The messages should be added in chronologic order, i.e. the oldest first,
7107 * the newest last.
7108 *
Alex Hillsfc737de2016-03-23 17:33:02 -04007109 * @param message The {@link Message} to be displayed
7110 * @return this object for method chaining
7111 */
7112 public MessagingStyle addMessage(Message message) {
7113 mMessages.add(message);
7114 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
7115 mMessages.remove(0);
7116 }
7117 return this;
7118 }
7119
7120 /**
Adrian Roos437cd562017-01-18 15:47:03 -08007121 * Adds a {@link Message} for historic context in this notification.
7122 *
7123 * <p>Messages should be added as historic if they are not the main subject of the
7124 * notification but may give context to a conversation. The system may choose to present
7125 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
7126 *
7127 * <p>The messages should be added in chronologic order, i.e. the oldest first,
7128 * the newest last.
7129 *
7130 * @param message The historic {@link Message} to be added
7131 * @return this object for method chaining
7132 */
7133 public MessagingStyle addHistoricMessage(Message message) {
7134 mHistoricMessages.add(message);
7135 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
7136 mHistoricMessages.remove(0);
7137 }
7138 return this;
7139 }
7140
7141 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007142 * Gets the list of {@code Message} objects that represent the notification
7143 */
7144 public List<Message> getMessages() {
7145 return mMessages;
7146 }
7147
7148 /**
Adrian Roos437cd562017-01-18 15:47:03 -08007149 * Gets the list of historic {@code Message}s in the notification.
7150 */
7151 public List<Message> getHistoricMessages() {
7152 return mHistoricMessages;
7153 }
7154
7155 /**
Selim Cinekce8794f2018-05-23 16:46:05 -07007156 * Sets whether this conversation notification represents a group. If the app is targeting
7157 * Android P, this is required if the app wants to display the largeIcon set with
7158 * {@link Notification.Builder#setLargeIcon(Bitmap)}, otherwise it will be hidden.
Kodlee Yin14656422017-12-22 17:00:46 -08007159 *
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007160 * @param isGroupConversation {@code true} if the conversation represents a group,
7161 * {@code false} otherwise.
7162 * @return this object for method chaining
7163 */
7164 public MessagingStyle setGroupConversation(boolean isGroupConversation) {
7165 mIsGroupConversation = isGroupConversation;
7166 return this;
7167 }
7168
7169 /**
Kodlee Yin14656422017-12-22 17:00:46 -08007170 * Returns {@code true} if this notification represents a group conversation, otherwise
7171 * {@code false}.
7172 *
7173 * <p> If the application that generated this {@link MessagingStyle} targets an SDK version
7174 * less than {@link Build.VERSION_CODES#P}, this method becomes dependent on whether or
7175 * not the conversation title is set; returning {@code true} if the conversation title is
7176 * a non-null value, or {@code false} otherwise. From {@link Build.VERSION_CODES#P} forward,
7177 * this method returns what's set by {@link #setGroupConversation(boolean)} allowing for
7178 * named, non-group conversations.
7179 *
7180 * @see #setConversationTitle(CharSequence)
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007181 */
7182 public boolean isGroupConversation() {
Kodlee Yin14656422017-12-22 17:00:46 -08007183 // When target SDK version is < P, a non-null conversation title dictates if this is
7184 // as group conversation.
7185 if (mBuilder != null
7186 && mBuilder.mContext.getApplicationInfo().targetSdkVersion
7187 < Build.VERSION_CODES.P) {
7188 return mConversationTitle != null;
7189 }
7190
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007191 return mIsGroupConversation;
7192 }
7193
7194 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007195 * @hide
7196 */
7197 @Override
7198 public void addExtras(Bundle extras) {
7199 super.addExtras(extras);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007200 if (mUser != null) {
7201 // For legacy usages
7202 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUser.getName());
7203 extras.putParcelable(EXTRA_MESSAGING_PERSON, mUser);
Alex Hillsfc737de2016-03-23 17:33:02 -04007204 }
7205 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007206 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04007207 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007208 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
7209 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04007210 }
Adrian Roos437cd562017-01-18 15:47:03 -08007211 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
7212 Message.getBundleArrayForMessages(mHistoricMessages));
7213 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007214
7215 fixTitleAndTextExtras(extras);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007216 extras.putBoolean(EXTRA_IS_GROUP_CONVERSATION, mIsGroupConversation);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007217 }
7218
7219 private void fixTitleAndTextExtras(Bundle extras) {
7220 Message m = findLatestIncomingMessage();
7221 CharSequence text = (m == null) ? null : m.mText;
7222 CharSequence sender = m == null ? null
Selim Cinekcb8b9852017-12-15 18:01:52 -08007223 : m.mSender == null || TextUtils.isEmpty(m.mSender.getName())
7224 ? mUser.getName() : m.mSender.getName();
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007225 CharSequence title;
7226 if (!TextUtils.isEmpty(mConversationTitle)) {
7227 if (!TextUtils.isEmpty(sender)) {
7228 BidiFormatter bidi = BidiFormatter.getInstance();
7229 title = mBuilder.mContext.getString(
7230 com.android.internal.R.string.notification_messaging_title_template,
Selim Cinekcb8b9852017-12-15 18:01:52 -08007231 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(sender));
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007232 } else {
7233 title = mConversationTitle;
7234 }
7235 } else {
7236 title = sender;
7237 }
7238
7239 if (title != null) {
7240 extras.putCharSequence(EXTRA_TITLE, title);
7241 }
7242 if (text != null) {
7243 extras.putCharSequence(EXTRA_TEXT, text);
7244 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007245 }
7246
7247 /**
7248 * @hide
7249 */
7250 @Override
7251 protected void restoreFromExtras(Bundle extras) {
7252 super.restoreFromExtras(extras);
7253
Selim Cinekcb8b9852017-12-15 18:01:52 -08007254 mUser = extras.getParcelable(EXTRA_MESSAGING_PERSON);
7255 if (mUser == null) {
7256 CharSequence displayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
Selim Cinek9acd6732018-03-23 16:39:02 -07007257 mUser = new Person.Builder().setName(displayName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007258 }
Adrian Roos96b7e202016-05-17 13:50:38 -07007259 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08007260 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007261 mMessages = Message.getMessagesFromBundleArray(messages);
Adrian Roos437cd562017-01-18 15:47:03 -08007262 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007263 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007264 mIsGroupConversation = extras.getBoolean(EXTRA_IS_GROUP_CONVERSATION);
Alex Hillsfc737de2016-03-23 17:33:02 -04007265 }
7266
7267 /**
7268 * @hide
7269 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07007270 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007271 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07007272 mBuilder.mOriginalActions = mBuilder.mActions;
7273 mBuilder.mActions = new ArrayList<>();
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007274 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007275 false /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007276 mBuilder.mActions = mBuilder.mOriginalActions;
7277 mBuilder.mOriginalActions = null;
7278 return remoteViews;
Adrian Roosc1a80b02016-04-05 14:54:55 -07007279 }
7280
Julia Reynolds7217dc92018-03-07 12:12:09 -05007281 /**
7282 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04007283 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05007284 */
7285 @Override
7286 public boolean areNotificationsVisiblyDifferent(Style other) {
7287 if (other == null || getClass() != other.getClass()) {
7288 return true;
7289 }
7290 MessagingStyle newS = (MessagingStyle) other;
7291 List<MessagingStyle.Message> oldMs = getMessages();
7292 List<MessagingStyle.Message> newMs = newS.getMessages();
7293
Dan Sandler7d67bd42018-05-15 14:06:38 -04007294 if (oldMs == null || newMs == null) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007295 newMs = new ArrayList<>();
7296 }
7297
7298 int n = oldMs.size();
7299 if (n != newMs.size()) {
7300 return true;
7301 }
7302 for (int i = 0; i < n; i++) {
7303 MessagingStyle.Message oldM = oldMs.get(i);
7304 MessagingStyle.Message newM = newMs.get(i);
Dan Sandler7d67bd42018-05-15 14:06:38 -04007305 if (!Objects.equals(
7306 String.valueOf(oldM.getText()),
7307 String.valueOf(newM.getText()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007308 return true;
7309 }
7310 if (!Objects.equals(oldM.getDataUri(), newM.getDataUri())) {
7311 return true;
7312 }
Dan Sandler7d67bd42018-05-15 14:06:38 -04007313 String oldSender = String.valueOf(oldM.getSenderPerson() == null
7314 ? oldM.getSender()
7315 : oldM.getSenderPerson().getName());
7316 String newSender = String.valueOf(newM.getSenderPerson() == null
7317 ? newM.getSender()
7318 : newM.getSenderPerson().getName());
Julia Reynolds7217dc92018-03-07 12:12:09 -05007319 if (!Objects.equals(oldSender, newSender)) {
7320 return true;
7321 }
7322
7323 String oldKey = oldM.getSenderPerson() == null
7324 ? null : oldM.getSenderPerson().getKey();
7325 String newKey = newM.getSenderPerson() == null
7326 ? null : newM.getSenderPerson().getKey();
7327 if (!Objects.equals(oldKey, newKey)) {
7328 return true;
7329 }
7330 // Other fields (like timestamp) intentionally excluded
7331 }
7332 return false;
7333 }
7334
Adrian Roosc1a80b02016-04-05 14:54:55 -07007335 private Message findLatestIncomingMessage() {
Selim Cinek88188f22017-09-19 16:46:56 -07007336 return findLatestIncomingMessage(mMessages);
7337 }
7338
7339 /**
7340 * @hide
7341 */
7342 @Nullable
7343 public static Message findLatestIncomingMessage(
7344 List<Message> messages) {
7345 for (int i = messages.size() - 1; i >= 0; i--) {
7346 Message m = messages.get(i);
Adrian Roosc1a80b02016-04-05 14:54:55 -07007347 // Incoming messages have a non-empty sender.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007348 if (m.mSender != null && !TextUtils.isEmpty(m.mSender.getName())) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07007349 return m;
7350 }
7351 }
Selim Cinek88188f22017-09-19 16:46:56 -07007352 if (!messages.isEmpty()) {
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007353 // No incoming messages, fall back to outgoing message
Selim Cinek88188f22017-09-19 16:46:56 -07007354 return messages.get(messages.size() - 1);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007355 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07007356 return null;
7357 }
7358
7359 /**
7360 * @hide
7361 */
7362 @Override
7363 public RemoteViews makeBigContentView() {
Selim Cinek384804b2018-04-18 14:31:07 +08007364 return makeMessagingView(false /* displayImagesAtEnd */, true /* hideLargeIcon */);
Selim Cinekafeed292017-12-12 17:32:44 -08007365 }
7366
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007367 /**
7368 * Create a messaging layout.
7369 *
7370 * @param displayImagesAtEnd should images be displayed at the end of the content instead
7371 * of inline.
Selim Cinek384804b2018-04-18 14:31:07 +08007372 * @param hideRightIcons Should the reply affordance be shown at the end of the notification
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007373 * @return the created remoteView.
7374 */
Selim Cinekafeed292017-12-12 17:32:44 -08007375 @NonNull
Selim Cinek384804b2018-04-18 14:31:07 +08007376 private RemoteViews makeMessagingView(boolean displayImagesAtEnd, boolean hideRightIcons) {
Selim Cinek88188f22017-09-19 16:46:56 -07007377 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
Adrian Roosc1a80b02016-04-05 14:54:55 -07007378 ? super.mBigContentTitle
7379 : mConversationTitle;
Selim Cinekce8794f2018-05-23 16:46:05 -07007380 boolean atLeastP = mBuilder.mContext.getApplicationInfo().targetSdkVersion
7381 >= Build.VERSION_CODES.P;
7382 boolean isOneToOne;
Selim Cinek2dd3e722018-01-19 11:06:06 -08007383 CharSequence nameReplacement = null;
Selim Cinekce8794f2018-05-23 16:46:05 -07007384 Icon avatarReplacement = null;
7385 if (!atLeastP) {
7386 isOneToOne = TextUtils.isEmpty(conversationTitle);
7387 avatarReplacement = mBuilder.mN.mLargeIcon;
7388 if (hasOnlyWhiteSpaceSenders()) {
7389 isOneToOne = true;
7390 nameReplacement = conversationTitle;
7391 conversationTitle = null;
7392 }
7393 } else {
7394 isOneToOne = !isGroupConversation();
Adrian Roosb1f427c2016-05-26 12:27:15 -07007395 }
Selim Cinek384804b2018-04-18 14:31:07 +08007396 TemplateBindResult bindResult = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007397 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).title(
7398 conversationTitle).text(null)
7399 .hideLargeIcon(hideRightIcons || isOneToOne)
7400 .hideReplyIcon(hideRightIcons)
7401 .headerTextSecondary(conversationTitle);
Adrian Roos48d746a2016-04-12 14:57:28 -07007402 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07007403 mBuilder.getMessagingLayoutResource(),
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007404 p,
Selim Cinek384804b2018-04-18 14:31:07 +08007405 bindResult);
Selim Cinek88188f22017-09-19 16:46:56 -07007406 addExtras(mBuilder.mN.extras);
Selim Cinekafeed292017-12-12 17:32:44 -08007407 // also update the end margin if there is an image
Selim Cinek1c72fa02018-04-23 18:00:54 +08007408 contentView.setViewLayoutMarginEnd(R.id.notification_messaging,
7409 bindResult.getIconMarginEnd());
Selim Cinek88188f22017-09-19 16:46:56 -07007410 contentView.setInt(R.id.status_bar_latest_event_content, "setLayoutColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007411 mBuilder.isColorized(p) ? mBuilder.getPrimaryTextColor(p)
7412 : mBuilder.resolveContrastColor(p));
Kenny Guy14d035c2018-05-02 19:10:36 +01007413 contentView.setInt(R.id.status_bar_latest_event_content, "setSenderTextColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007414 mBuilder.getPrimaryTextColor(p));
Kenny Guy14d035c2018-05-02 19:10:36 +01007415 contentView.setInt(R.id.status_bar_latest_event_content, "setMessageTextColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007416 mBuilder.getSecondaryTextColor(p));
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007417 contentView.setBoolean(R.id.status_bar_latest_event_content, "setDisplayImagesAtEnd",
7418 displayImagesAtEnd);
Selim Cinekce8794f2018-05-23 16:46:05 -07007419 contentView.setIcon(R.id.status_bar_latest_event_content, "setAvatarReplacement",
7420 avatarReplacement);
Selim Cinek2dd3e722018-01-19 11:06:06 -08007421 contentView.setCharSequence(R.id.status_bar_latest_event_content, "setNameReplacement",
7422 nameReplacement);
Selim Cinek88188f22017-09-19 16:46:56 -07007423 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsOneToOne",
7424 isOneToOne);
7425 contentView.setBundle(R.id.status_bar_latest_event_content, "setData",
7426 mBuilder.mN.extras);
Alex Hillsfc737de2016-03-23 17:33:02 -04007427 return contentView;
7428 }
7429
Selim Cinekf7409db2017-10-24 16:17:14 -07007430 private boolean hasOnlyWhiteSpaceSenders() {
7431 for (int i = 0; i < mMessages.size(); i++) {
7432 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007433 Person sender = m.getSenderPerson();
7434 if (sender != null && !isWhiteSpace(sender.getName())) {
Selim Cinekf7409db2017-10-24 16:17:14 -07007435 return false;
7436 }
7437 }
7438 return true;
7439 }
7440
7441 private boolean isWhiteSpace(CharSequence sender) {
7442 if (TextUtils.isEmpty(sender)) {
7443 return true;
7444 }
7445 if (sender.toString().matches("^\\s*$")) {
7446 return true;
7447 }
7448 // Let's check if we only have 0 whitespace chars. Some apps did this as a workaround
7449 // For the presentation that we had.
7450 for (int i = 0; i < sender.length(); i++) {
7451 char c = sender.charAt(i);
7452 if (c != '\u200B') {
7453 return false;
7454 }
7455 }
7456 return true;
7457 }
7458
Selim Cinek88188f22017-09-19 16:46:56 -07007459 private CharSequence createConversationTitleFromMessages() {
7460 ArraySet<CharSequence> names = new ArraySet<>();
7461 for (int i = 0; i < mMessages.size(); i++) {
7462 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007463 Person sender = m.getSenderPerson();
Selim Cinek88188f22017-09-19 16:46:56 -07007464 if (sender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007465 names.add(sender.getName());
Selim Cinek88188f22017-09-19 16:46:56 -07007466 }
7467 }
7468 SpannableStringBuilder title = new SpannableStringBuilder();
7469 int size = names.size();
7470 for (int i = 0; i < size; i++) {
7471 CharSequence name = names.valueAt(i);
7472 if (!TextUtils.isEmpty(title)) {
7473 title.append(", ");
7474 }
7475 title.append(BidiFormatter.getInstance().unicodeWrap(name));
7476 }
7477 return title;
7478 }
7479
Adrian Roosdedd1df2016-04-26 16:38:47 -07007480 /**
7481 * @hide
7482 */
7483 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007484 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007485 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007486 true /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007487 remoteViews.setInt(R.id.notification_messaging, "setMaxDisplayedLines", 1);
7488 return remoteViews;
Adrian Roosdedd1df2016-04-26 16:38:47 -07007489 }
7490
Adrian Roosc1a80b02016-04-05 14:54:55 -07007491 private static TextAppearanceSpan makeFontColorSpan(int color) {
7492 return new TextAppearanceSpan(null, 0, 0,
7493 ColorStateList.valueOf(color), null);
7494 }
7495
Alex Hillsd9b04d92016-04-11 16:38:16 -04007496 public static final class Message {
Tony Mak09db2ea2018-06-27 18:12:48 +01007497 /** @hide */
7498 public static final String KEY_TEXT = "text";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007499 static final String KEY_TIMESTAMP = "time";
7500 static final String KEY_SENDER = "sender";
Selim Cinekcb8b9852017-12-15 18:01:52 -08007501 static final String KEY_SENDER_PERSON = "sender_person";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007502 static final String KEY_DATA_MIME_TYPE = "type";
7503 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08007504 static final String KEY_EXTRAS_BUNDLE = "extras";
Kenny Guya0f6de82018-04-06 16:20:16 +01007505 static final String KEY_REMOTE_INPUT_HISTORY = "remote_input_history";
Alex Hillsfc737de2016-03-23 17:33:02 -04007506
7507 private final CharSequence mText;
7508 private final long mTimestamp;
Selim Cinekcb8b9852017-12-15 18:01:52 -08007509 @Nullable
7510 private final Person mSender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007511 /** True if this message was generated from the extra
7512 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}
7513 */
7514 private final boolean mRemoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007515
Shane Brennan5a871862017-03-11 13:14:17 -08007516 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007517 private String mDataMimeType;
7518 private Uri mDataUri;
7519
7520 /**
7521 * Constructor
7522 * @param text A {@link CharSequence} to be displayed as the message content
7523 * @param timestamp Time at which the message arrived
7524 * @param sender A {@link CharSequence} to be used for displaying the name of the
7525 * sender. Should be <code>null</code> for messages by the current user, in which case
7526 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
7527 * Should be unique amongst all individuals in the conversation, and should be
7528 * consistent during re-posts of the notification.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007529 *
7530 * @deprecated use {@code Message(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04007531 */
7532 public Message(CharSequence text, long timestamp, CharSequence sender){
Selim Cinek9acd6732018-03-23 16:39:02 -07007533 this(text, timestamp, sender == null ? null
7534 : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08007535 }
7536
7537 /**
7538 * Constructor
7539 * @param text A {@link CharSequence} to be displayed as the message content
7540 * @param timestamp Time at which the message arrived
7541 * @param sender The {@link Person} who sent the message.
7542 * Should be <code>null</code> for messages by the current user, in which case
7543 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7544 * <p>
Selim Cinek9acd6732018-03-23 16:39:02 -07007545 * The person provided should contain an Icon, set with
7546 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7547 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7548 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7549 * to differentiate between the different users.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007550 * </p>
7551 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007552 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender) {
Kenny Guya0f6de82018-04-06 16:20:16 +01007553 this(text, timestamp, sender, false /* remoteHistory */);
7554 }
7555
7556 /**
7557 * Constructor
7558 * @param text A {@link CharSequence} to be displayed as the message content
7559 * @param timestamp Time at which the message arrived
7560 * @param sender The {@link Person} who sent the message.
7561 * Should be <code>null</code> for messages by the current user, in which case
7562 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7563 * @param remoteInputHistory True if the messages was generated from the extra
7564 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7565 * <p>
7566 * The person provided should contain an Icon, set with
7567 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7568 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7569 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7570 * to differentiate between the different users.
7571 * </p>
7572 * @hide
7573 */
7574 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender,
7575 boolean remoteInputHistory) {
Alex Hillsfc737de2016-03-23 17:33:02 -04007576 mText = text;
7577 mTimestamp = timestamp;
7578 mSender = sender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007579 mRemoteInputHistory = remoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007580 }
7581
7582 /**
7583 * Sets a binary blob of data and an associated MIME type for a message. In the case
7584 * where the platform doesn't support the MIME type, the original text provided in the
7585 * constructor will be used.
7586 * @param dataMimeType The MIME type of the content. See
7587 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
7588 * types on Android and Android Wear.
7589 * @param dataUri The uri containing the content whose type is given by the MIME type.
7590 * <p class="note">
7591 * <ol>
7592 * <li>Notification Listeners including the System UI need permission to access the
7593 * data the Uri points to. The recommended ways to do this are:</li>
7594 * <li>Store the data in your own ContentProvider, making sure that other apps have
7595 * the correct permission to access your provider. The preferred mechanism for
7596 * providing access is to use per-URI permissions which are temporary and only
7597 * grant access to the receiving application. An easy way to create a
7598 * ContentProvider like this is to use the FileProvider helper class.</li>
7599 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
7600 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
7601 * also store non-media types (see MediaStore.Files for more info). Files can be
7602 * inserted into the MediaStore using scanFile() after which a content:// style
7603 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
7604 * Note that once added to the system MediaStore the content is accessible to any
7605 * app on the device.</li>
7606 * </ol>
7607 * @return this object for method chaining
7608 */
7609 public Message setData(String dataMimeType, Uri dataUri) {
7610 mDataMimeType = dataMimeType;
7611 mDataUri = dataUri;
7612 return this;
7613 }
7614
Alex Hillsfc737de2016-03-23 17:33:02 -04007615 /**
7616 * Get the text to be used for this message, or the fallback text if a type and content
7617 * Uri have been set
7618 */
7619 public CharSequence getText() {
7620 return mText;
7621 }
7622
7623 /**
7624 * Get the time at which this message arrived
7625 */
7626 public long getTimestamp() {
7627 return mTimestamp;
7628 }
7629
7630 /**
Shane Brennan5a871862017-03-11 13:14:17 -08007631 * Get the extras Bundle for this message.
7632 */
7633 public Bundle getExtras() {
7634 return mExtras;
7635 }
7636
7637 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007638 * Get the text used to display the contact's name in the messaging experience
Selim Cinekcb8b9852017-12-15 18:01:52 -08007639 *
7640 * @deprecated use {@link #getSenderPerson()}
Alex Hillsfc737de2016-03-23 17:33:02 -04007641 */
7642 public CharSequence getSender() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007643 return mSender == null ? null : mSender.getName();
7644 }
7645
7646 /**
7647 * Get the sender associated with this message.
7648 */
7649 @Nullable
7650 public Person getSenderPerson() {
Alex Hillsfc737de2016-03-23 17:33:02 -04007651 return mSender;
7652 }
7653
7654 /**
7655 * Get the MIME type of the data pointed to by the Uri
7656 */
7657 public String getDataMimeType() {
7658 return mDataMimeType;
7659 }
7660
7661 /**
koprivaa1a78482018-10-09 10:09:23 -07007662 * Get the Uri pointing to the content of the message. Can be null, in which case
Alex Hillsfc737de2016-03-23 17:33:02 -04007663 * {@see #getText()} is used.
7664 */
7665 public Uri getDataUri() {
7666 return mDataUri;
7667 }
7668
Kenny Guya0f6de82018-04-06 16:20:16 +01007669 /**
7670 * @return True if the message was generated from
7671 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7672 * @hide
7673 */
7674 public boolean isRemoteInputHistory() {
7675 return mRemoteInputHistory;
7676 }
7677
Beverlye98937a2018-11-15 10:18:57 -05007678 /**
7679 * @hide
7680 */
7681 @VisibleForTesting
7682 public Bundle toBundle() {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007683 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007684 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007685 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04007686 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007687 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04007688 if (mSender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007689 // Legacy listeners need this
7690 bundle.putCharSequence(KEY_SENDER, mSender.getName());
7691 bundle.putParcelable(KEY_SENDER_PERSON, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04007692 }
7693 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007694 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04007695 }
7696 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007697 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04007698 }
Shane Brennan5a871862017-03-11 13:14:17 -08007699 if (mExtras != null) {
7700 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
7701 }
Kenny Guya0f6de82018-04-06 16:20:16 +01007702 if (mRemoteInputHistory) {
7703 bundle.putBoolean(KEY_REMOTE_INPUT_HISTORY, mRemoteInputHistory);
7704 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007705 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04007706 }
7707
Alex Hillsd9b04d92016-04-11 16:38:16 -04007708 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
7709 Bundle[] bundles = new Bundle[messages.size()];
7710 final int N = messages.size();
7711 for (int i = 0; i < N; i++) {
7712 bundles[i] = messages.get(i).toBundle();
7713 }
7714 return bundles;
7715 }
7716
Selim Cinek88188f22017-09-19 16:46:56 -07007717 /**
7718 * @return A list of messages read from the bundles.
7719 *
7720 * @hide
7721 */
7722 public static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
7723 if (bundles == null) {
7724 return new ArrayList<>();
7725 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007726 List<Message> messages = new ArrayList<>(bundles.length);
7727 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07007728 if (bundles[i] instanceof Bundle) {
7729 Message message = getMessageFromBundle((Bundle)bundles[i]);
7730 if (message != null) {
7731 messages.add(message);
7732 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007733 }
7734 }
7735 return messages;
7736 }
7737
Selim Cinekb0dc61b2018-05-22 18:49:36 -07007738 /**
7739 * @return The message that is stored in the bundle or null if the message couldn't be
7740 * resolved.
7741 *
7742 * @hide
7743 */
7744 @Nullable
7745 public static Message getMessageFromBundle(Bundle bundle) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007746 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07007747 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007748 return null;
7749 } else {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007750
7751 Person senderPerson = bundle.getParcelable(KEY_SENDER_PERSON);
7752 if (senderPerson == null) {
7753 // Legacy apps that use compat don't actually provide the sender objects
7754 // We need to fix the compat version to provide people / use
7755 // the native api instead
7756 CharSequence senderName = bundle.getCharSequence(KEY_SENDER);
7757 if (senderName != null) {
Selim Cinek9acd6732018-03-23 16:39:02 -07007758 senderPerson = new Person.Builder().setName(senderName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007759 }
7760 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007761 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
Selim Cinekcb8b9852017-12-15 18:01:52 -08007762 bundle.getLong(KEY_TIMESTAMP),
Kenny Guya0f6de82018-04-06 16:20:16 +01007763 senderPerson,
7764 bundle.getBoolean(KEY_REMOTE_INPUT_HISTORY, false));
Alex Hillsd9b04d92016-04-11 16:38:16 -04007765 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
7766 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007767 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
7768 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04007769 }
Shane Brennan5a871862017-03-11 13:14:17 -08007770 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
7771 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
7772 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007773 return message;
7774 }
7775 } catch (ClassCastException e) {
7776 return null;
7777 }
7778 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007779 }
7780 }
7781
7782 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04007783 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08007784 *
Robert Ly91c5ce32014-06-08 15:37:00 -07007785 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04007786 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07007787 * Notification notif = new Notification.Builder(mContext)
7788 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
7789 * .setContentText(subject)
7790 * .setSmallIcon(R.drawable.new_mail)
7791 * .setLargeIcon(aBitmap)
7792 * .setStyle(new Notification.InboxStyle()
7793 * .addLine(str1)
7794 * .addLine(str2)
7795 * .setContentTitle(&quot;&quot;)
7796 * .setSummaryText(&quot;+3 more&quot;))
7797 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04007798 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08007799 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04007800 * @see Notification#bigContentView
7801 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007802 public static class InboxStyle extends Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07007803
7804 /**
7805 * The number of lines of remote input history allowed until we start reducing lines.
7806 */
7807 private static final int NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION = 1;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007808 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
7809
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007810 public InboxStyle() {
7811 }
7812
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007813 /**
7814 * @deprecated use {@code InboxStyle()}.
7815 */
7816 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04007817 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007818 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04007819 }
7820
Chris Wrend6297db2012-05-03 16:20:13 -04007821 /**
7822 * Overrides ContentTitle in the big form of the template.
7823 * This defaults to the value passed to setContentTitle().
7824 */
7825 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007826 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04007827 return this;
7828 }
7829
7830 /**
7831 * Set the first line of text after the detail section in the big form of the template.
7832 */
7833 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007834 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04007835 return this;
7836 }
7837
Chris Wren0bd664d2012-08-01 13:56:56 -04007838 /**
7839 * Append a line to the digest section of the Inbox notification.
7840 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04007841 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007842 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04007843 return this;
7844 }
7845
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007846 /**
7847 * @hide
7848 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05007849 public ArrayList<CharSequence> getLines() {
7850 return mTexts;
7851 }
7852
7853 /**
7854 * @hide
7855 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007856 public void addExtras(Bundle extras) {
7857 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007858
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007859 CharSequence[] a = new CharSequence[mTexts.size()];
7860 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
7861 }
7862
Christoph Studer4600f9b2014-07-22 22:44:43 +02007863 /**
7864 * @hide
7865 */
7866 @Override
7867 protected void restoreFromExtras(Bundle extras) {
7868 super.restoreFromExtras(extras);
7869
7870 mTexts.clear();
7871 if (extras.containsKey(EXTRA_TEXT_LINES)) {
7872 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
7873 }
7874 }
7875
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007876 /**
7877 * @hide
7878 */
7879 public RemoteViews makeBigContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007880 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder).text(null);
Selim Cinek384804b2018-04-18 14:31:07 +08007881 TemplateBindResult result = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007882 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource(), p, result);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007883
Chris Wrend6297db2012-05-03 16:20:13 -04007884 int[] rowIds = {R.id.inbox_text0, R.id.inbox_text1, R.id.inbox_text2, R.id.inbox_text3,
Chris Wren29bb6d92012-05-17 18:09:42 -04007885 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04007886
Chris Wren4ed80d52012-05-17 09:30:03 -04007887 // Make sure all rows are gone in case we reuse a view.
7888 for (int rowId : rowIds) {
7889 contentView.setViewVisibility(rowId, View.GONE);
7890 }
7891
Daniel Sandler879c5e02012-04-17 16:46:51 -04007892 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07007893 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7894 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08007895 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07007896 int onlyViewId = 0;
7897 int maxRows = rowIds.length;
7898 if (mBuilder.mActions.size() > 0) {
7899 maxRows--;
7900 }
Selim Cinekbee4e072018-05-21 22:06:43 -07007901 CharSequence[] remoteInputHistory = mBuilder.mN.extras.getCharSequenceArray(
7902 EXTRA_REMOTE_INPUT_HISTORY);
7903 if (remoteInputHistory != null
7904 && remoteInputHistory.length > NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION) {
7905 // Let's remove some messages to make room for the remote input history.
7906 // 1 is always able to fit, but let's remove them if they are 2 or 3
7907 int numRemoteInputs = Math.min(remoteInputHistory.length,
7908 MAX_REMOTE_INPUT_HISTORY_LINES);
7909 int totalNumRows = mTexts.size() + numRemoteInputs
7910 - NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION;
7911 if (totalNumRows > maxRows) {
7912 int overflow = totalNumRows - maxRows;
7913 if (mTexts.size() > maxRows) {
7914 // Heuristic: if the Texts don't fit anyway, we'll rather drop the last
7915 // few messages, even with the remote input
7916 maxRows -= overflow;
7917 } else {
7918 // otherwise we drop the first messages
7919 i = overflow;
7920 }
7921 }
7922 }
Selim Cinek07c80172016-04-21 16:40:47 -07007923 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007924 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07007925 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007926 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07007927 contentView.setTextViewText(rowIds[i],
7928 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007929 mBuilder.setTextViewColorSecondary(contentView, rowIds[i], p);
Selim Cinek07c80172016-04-21 16:40:47 -07007930 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek384804b2018-04-18 14:31:07 +08007931 handleInboxImageMargin(contentView, rowIds[i], first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007932 result.getIconMarginEnd());
Selim Cinek07c80172016-04-21 16:40:47 -07007933 if (first) {
7934 onlyViewId = rowIds[i];
7935 } else {
7936 onlyViewId = 0;
7937 }
Selim Cinek247fa012016-02-18 09:50:48 -08007938 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007939 }
7940 i++;
7941 }
Selim Cinek07c80172016-04-21 16:40:47 -07007942 if (onlyViewId != 0) {
7943 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
7944 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7945 R.dimen.notification_text_margin_top);
7946 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
7947 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007948
Daniel Sandler879c5e02012-04-17 16:46:51 -04007949 return contentView;
7950 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007951
Julia Reynolds7217dc92018-03-07 12:12:09 -05007952 /**
7953 * @hide
7954 */
7955 @Override
7956 public boolean areNotificationsVisiblyDifferent(Style other) {
7957 if (other == null || getClass() != other.getClass()) {
7958 return true;
7959 }
7960 InboxStyle newS = (InboxStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04007961
7962 final ArrayList<CharSequence> myLines = getLines();
7963 final ArrayList<CharSequence> newLines = newS.getLines();
7964 final int n = myLines.size();
7965 if (n != newLines.size()) {
7966 return true;
7967 }
7968
7969 for (int i = 0; i < n; i++) {
7970 if (!Objects.equals(
7971 String.valueOf(myLines.get(i)),
7972 String.valueOf(newLines.get(i)))) {
7973 return true;
7974 }
7975 }
7976 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05007977 }
7978
Selim Cinek384804b2018-04-18 14:31:07 +08007979 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007980 int marginEndValue) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08007981 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08007982 if (first) {
7983 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
7984 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
7985 boolean hasProgress = max != 0 || ind;
Selim Cinek384804b2018-04-18 14:31:07 +08007986 if (!hasProgress) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08007987 endMargin = marginEndValue;
Selim Cinek247fa012016-02-18 09:50:48 -08007988 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007989 }
Selim Cinek1c72fa02018-04-23 18:00:54 +08007990 contentView.setViewLayoutMarginEnd(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08007991 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04007992 }
Dan Sandler842dd772014-05-15 09:36:47 -04007993
7994 /**
7995 * Notification style for media playback notifications.
7996 *
7997 * In the expanded form, {@link Notification#bigContentView}, up to 5
7998 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04007999 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04008000 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
8001 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
8002 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08008003 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04008004 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
8005 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01008006 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04008007 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08008008 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01008009 * Notifications created with MediaStyle will have their category set to
8010 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
8011 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08008012 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07008013 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
8014 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04008015 * the System UI can identify this as a notification representing an active media session
8016 * and respond accordingly (by showing album artwork in the lockscreen, for example).
8017 *
Selim Cinek99104832017-01-25 14:47:33 -08008018 * <p>
8019 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
8020 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
8021 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
8022 * <p>
8023 *
Dan Sandler842dd772014-05-15 09:36:47 -04008024 * To use this style with your Notification, feed it to
8025 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8026 * <pre class="prettyprint">
8027 * Notification noti = new Notification.Builder()
8028 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01008029 * .setContentTitle(&quot;Track title&quot;)
8030 * .setContentText(&quot;Artist - Album&quot;)
8031 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01008032 * .setStyle(<b>new Notification.MediaStyle()</b>
8033 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04008034 * .build();
8035 * </pre>
8036 *
8037 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08008038 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04008039 */
8040 public static class MediaStyle extends Style {
Gus Prevas9cc96602018-10-11 11:24:23 -04008041 // Changing max media buttons requires also changing templates
8042 // (notification_template_material_media and notification_template_material_big_media).
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008043 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04008044 static final int MAX_MEDIA_BUTTONS = 5;
Gus Prevas9cc96602018-10-11 11:24:23 -04008045 @IdRes private static final int[] MEDIA_BUTTON_IDS = {
8046 R.id.action0,
8047 R.id.action1,
8048 R.id.action2,
8049 R.id.action3,
8050 R.id.action4,
8051 };
Dan Sandler842dd772014-05-15 09:36:47 -04008052
8053 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07008054 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04008055
8056 public MediaStyle() {
8057 }
8058
Adrian Roosf5faf9d2016-05-23 13:56:15 -07008059 /**
8060 * @deprecated use {@code MediaStyle()}.
8061 */
8062 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04008063 public MediaStyle(Builder builder) {
8064 setBuilder(builder);
8065 }
8066
8067 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008068 * Request up to 3 actions (by index in the order of addition) to be shown in the compact
Dan Sandler842dd772014-05-15 09:36:47 -04008069 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008070 *
8071 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04008072 */
8073 public MediaStyle setShowActionsInCompactView(int...actions) {
8074 mActionsToShowInCompact = actions;
8075 return this;
8076 }
8077
8078 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07008079 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
8080 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04008081 */
Jeff Browndba34ba2014-06-24 20:46:03 -07008082 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04008083 mToken = token;
8084 return this;
8085 }
8086
Christoph Studer4600f9b2014-07-22 22:44:43 +02008087 /**
8088 * @hide
8089 */
Dan Sandler842dd772014-05-15 09:36:47 -04008090 @Override
Mathew Inwood61e8ae62018-08-14 14:17:44 +01008091 @UnsupportedAppUsage
Dan Sandler842dd772014-05-15 09:36:47 -04008092 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02008093 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01008094 if (wip.category == null) {
8095 wip.category = Notification.CATEGORY_TRANSPORT;
8096 }
Dan Sandler842dd772014-05-15 09:36:47 -04008097 return wip;
8098 }
8099
Christoph Studer4600f9b2014-07-22 22:44:43 +02008100 /**
8101 * @hide
8102 */
8103 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008104 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04008105 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02008106 }
8107
8108 /**
8109 * @hide
8110 */
8111 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04008112 public RemoteViews makeBigContentView() {
8113 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02008114 }
8115
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008116 /**
8117 * @hide
8118 */
8119 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008120 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek16f334e2019-07-18 16:00:55 -07008121 return makeMediaContentView();
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008122 }
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.
Julia Tuttled9f48352019-04-15 14:06:54 -04008712 *
8713 * <p>An intent is required.</p>
8714 *
8715 * @throws IllegalArgumentException if intent is null
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008716 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008717 @NonNull
8718 public BubbleMetadata.Builder setIntent(@NonNull PendingIntent intent) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008719 if (intent == null) {
8720 throw new IllegalArgumentException("Bubble requires non-null pending intent");
8721 }
8722 mPendingIntent = intent;
8723 return this;
8724 }
8725
8726 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008727 * Sets the icon that will represent the bubble when it is collapsed.
8728 *
8729 * <p>An icon is required and should be representative of the content within the bubble.
8730 * If your app produces multiple bubbles, the image should be unique for each of them.
8731 * </p>
Mady Mellor9848a6c2019-03-19 15:29:05 -07008732 *
8733 * <p>The shape of a bubble icon is adaptive and can match the device theme.
8734 *
8735 * If your icon is bitmap-based, you should create it using
8736 * {@link Icon#createWithAdaptiveBitmap(Bitmap)}, otherwise this method will throw.
8737 *
8738 * If your icon is not bitmap-based, you should expect that the icon will be tinted.
8739 * </p>
Julia Tuttled9f48352019-04-15 14:06:54 -04008740 *
8741 * @throws IllegalArgumentException if icon is null or a non-adaptive bitmap
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008742 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008743 @NonNull
8744 public BubbleMetadata.Builder setIcon(@NonNull Icon icon) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008745 if (icon == null) {
8746 throw new IllegalArgumentException("Bubbles require non-null icon");
8747 }
Mady Mellor9848a6c2019-03-19 15:29:05 -07008748 if (icon.getType() == TYPE_BITMAP) {
8749 throw new IllegalArgumentException("When using bitmap based icons, Bubbles "
8750 + "require TYPE_ADAPTIVE_BITMAP, please use"
8751 + " Icon#createWithAdaptiveBitmap instead");
8752 }
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008753 mIcon = icon;
8754 return this;
8755 }
8756
8757 /**
Mady Mellor7af771a2019-03-07 15:04:54 -08008758 * Sets the desired height in DPs for the app content defined by
Julia Tuttled9f48352019-04-15 14:06:54 -04008759 * {@link #setIntent(PendingIntent)}.
8760 *
8761 * <p>This height may not be respected if there is not enough space on the screen or if
8762 * the provided height is too small to be useful.</p>
8763 *
8764 * <p>If {@link #setDesiredHeightResId(int)} was previously called on this builder, the
Mady Mellor7af771a2019-03-07 15:04:54 -08008765 * previous value set will be cleared after calling this method, and this value will
Julia Tuttled9f48352019-04-15 14:06:54 -04008766 * be used instead.</p>
8767 *
8768 * <p>A desired height (in DPs or via resID) is optional.</p>
8769 *
8770 * @see #setDesiredHeightResId(int)
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008771 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008772 @NonNull
Mady Mellor8a529e22019-03-25 17:37:55 -07008773 public BubbleMetadata.Builder setDesiredHeight(@Dimension(unit = DP) int height) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008774 mDesiredHeight = Math.max(height, 0);
Mady Mellor7af771a2019-03-07 15:04:54 -08008775 mDesiredHeightResId = 0;
8776 return this;
8777 }
8778
8779
8780 /**
8781 * Sets the desired height via resId for the app content defined by
Julia Tuttled9f48352019-04-15 14:06:54 -04008782 * {@link #setIntent(PendingIntent)}.
8783 *
8784 * <p>This height may not be respected if there is not enough space on the screen or if
8785 * the provided height is too small to be useful.</p>
8786 *
8787 * <p>If {@link #setDesiredHeight(int)} was previously called on this builder, the
Mady Mellor7af771a2019-03-07 15:04:54 -08008788 * previous value set will be cleared after calling this method, and this value will
Julia Tuttled9f48352019-04-15 14:06:54 -04008789 * be used instead.</p>
8790 *
8791 * <p>A desired height (in DPs or via resID) is optional.</p>
8792 *
8793 * @see #setDesiredHeight(int)
Mady Mellor7af771a2019-03-07 15:04:54 -08008794 */
8795 @NonNull
8796 public BubbleMetadata.Builder setDesiredHeightResId(@DimenRes int heightResId) {
8797 mDesiredHeightResId = heightResId;
8798 mDesiredHeight = 0;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008799 return this;
8800 }
8801
8802 /**
Julia Tuttled9f48352019-04-15 14:06:54 -04008803 * Sets whether the bubble will be posted in its expanded state (with the contents of
8804 * {@link #getIntent()} in a floating window).
Mady Mellor0157ff22019-01-24 15:31:48 -08008805 *
Julia Tuttled9f48352019-04-15 14:06:54 -04008806 * <p>This flag has no effect if the app posting the bubble is not in the foreground.
Mady Mellor0157ff22019-01-24 15:31:48 -08008807 * </p>
8808 *
Julia Tuttled9f48352019-04-15 14:06:54 -04008809 * <p>Generally, this flag should only be set if the user has performed an action to
Mady Mellor0157ff22019-01-24 15:31:48 -08008810 * request or create a bubble.</p>
Julia Tuttled9f48352019-04-15 14:06:54 -04008811 *
8812 * <p>Setting this flag is optional; it defaults to false.</p>
Mady Mellor0157ff22019-01-24 15:31:48 -08008813 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008814 @NonNull
Mady Mellor0157ff22019-01-24 15:31:48 -08008815 public BubbleMetadata.Builder setAutoExpandBubble(boolean shouldExpand) {
8816 setFlag(FLAG_AUTO_EXPAND_BUBBLE, shouldExpand);
8817 return this;
8818 }
8819
8820 /**
Julia Tuttled9f48352019-04-15 14:06:54 -04008821 * Sets whether the bubble will be posted <b>without</b> the associated notification in
8822 * the notification shade.
Mady Mellor8a1f0252019-04-01 11:31:34 -07008823 *
Julia Tuttled9f48352019-04-15 14:06:54 -04008824 * <p>This flag has no effect if the app posting the bubble is not in the foreground.
Mady Mellor8a1f0252019-04-01 11:31:34 -07008825 * </p>
8826 *
Julia Tuttled9f48352019-04-15 14:06:54 -04008827 * <p>Generally, this flag should only be set if the user has performed an action to
Mady Mellor8a1f0252019-04-01 11:31:34 -07008828 * request or create a bubble, or if the user has seen the content in the notification
8829 * and the notification is no longer relevant.</p>
Julia Tuttled9f48352019-04-15 14:06:54 -04008830 *
8831 * <p>Setting this flag is optional; it defaults to false.</p>
Mady Mellor8a1f0252019-04-01 11:31:34 -07008832 */
8833 @NonNull
Julia Tuttle4f1bf202019-05-06 13:40:05 -04008834 public BubbleMetadata.Builder setSuppressNotification(boolean shouldSuppressNotif) {
8835 setFlag(FLAG_SUPPRESS_NOTIFICATION, shouldSuppressNotif);
Mady Mellor0157ff22019-01-24 15:31:48 -08008836 return this;
8837 }
8838
8839 /**
Julia Tuttled9f48352019-04-15 14:06:54 -04008840 * Sets an intent to send when this bubble is explicitly removed by the user.
8841 *
8842 * <p>Setting a delete intent is optional.</p>
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008843 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008844 @NonNull
8845 public BubbleMetadata.Builder setDeleteIntent(@Nullable PendingIntent deleteIntent) {
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008846 mDeleteIntent = deleteIntent;
8847 return this;
8848 }
8849
8850 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008851 * Creates the {@link BubbleMetadata} defined by this builder.
Julia Tuttled9f48352019-04-15 14:06:54 -04008852 *
8853 * @throws IllegalStateException if {@link #setIntent(PendingIntent)} and/or
8854 * {@link #setIcon(Icon)} have not been called on this
8855 * builder.
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008856 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008857 @NonNull
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008858 public BubbleMetadata build() {
8859 if (mPendingIntent == null) {
8860 throw new IllegalStateException("Must supply pending intent to bubble");
8861 }
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008862 if (mIcon == null) {
8863 throw new IllegalStateException("Must supply an icon for the bubble");
8864 }
Mady Mellor138f4c92019-02-20 16:12:17 -08008865 BubbleMetadata data = new BubbleMetadata(mPendingIntent, mDeleteIntent,
Mady Mellor7af771a2019-03-07 15:04:54 -08008866 mIcon, mDesiredHeight, mDesiredHeightResId);
Mady Mellor0157ff22019-01-24 15:31:48 -08008867 data.setFlags(mFlags);
8868 return data;
8869 }
8870
8871 /**
8872 * @hide
8873 */
8874 public BubbleMetadata.Builder setFlag(int mask, boolean value) {
8875 if (value) {
8876 mFlags |= mask;
8877 } else {
8878 mFlags &= ~mask;
8879 }
8880 return this;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008881 }
8882 }
8883 }
8884
8885
Christoph Studer4600f9b2014-07-22 22:44:43 +02008886 // When adding a new Style subclass here, don't forget to update
8887 // Builder.getNotificationStyleClass.
8888
Griff Hazen61a9e862014-05-22 16:05:19 -07008889 /**
8890 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
8891 * metadata or change options on a notification builder.
8892 */
8893 public interface Extender {
8894 /**
8895 * Apply this extender to a notification builder.
8896 * @param builder the builder to be modified.
8897 * @return the build object for chaining.
8898 */
8899 public Builder extend(Builder builder);
8900 }
8901
8902 /**
8903 * Helper class to add wearable extensions to notifications.
8904 * <p class="note"> See
8905 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
8906 * for Android Wear</a> for more information on how to use this class.
8907 * <p>
8908 * To create a notification with wearable extensions:
8909 * <ol>
8910 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
8911 * properties.
8912 * <li>Create a {@link android.app.Notification.WearableExtender}.
8913 * <li>Set wearable-specific properties using the
8914 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
8915 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
8916 * notification.
8917 * <li>Post the notification to the notification system with the
8918 * {@code NotificationManager.notify(...)} methods.
8919 * </ol>
8920 *
8921 * <pre class="prettyprint">
8922 * Notification notif = new Notification.Builder(mContext)
8923 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
8924 * .setContentText(subject)
8925 * .setSmallIcon(R.drawable.new_mail)
8926 * .extend(new Notification.WearableExtender()
8927 * .setContentIcon(R.drawable.new_mail))
8928 * .build();
8929 * NotificationManager notificationManger =
8930 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
8931 * notificationManger.notify(0, notif);</pre>
8932 *
8933 * <p>Wearable extensions can be accessed on an existing notification by using the
8934 * {@code WearableExtender(Notification)} constructor,
8935 * and then using the {@code get} methods to access values.
8936 *
8937 * <pre class="prettyprint">
8938 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
8939 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07008940 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008941 */
8942 public static final class WearableExtender implements Extender {
8943 /**
8944 * Sentinel value for an action index that is unset.
8945 */
8946 public static final int UNSET_ACTION_INDEX = -1;
8947
8948 /**
8949 * Size value for use with {@link #setCustomSizePreset} to show this notification with
8950 * default sizing.
8951 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07008952 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07008953 * on their content.
Gus Prevasd7363752018-09-18 14:35:15 -04008954 *
8955 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008956 */
Gus Prevasd7363752018-09-18 14:35:15 -04008957 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008958 public static final int SIZE_DEFAULT = 0;
8959
8960 /**
8961 * Size value for use with {@link #setCustomSizePreset} to show this notification
8962 * with an extra small size.
8963 * <p>This value is only applicable for custom display notifications created using
8964 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008965 *
8966 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008967 */
Gus Prevasd7363752018-09-18 14:35:15 -04008968 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008969 public static final int SIZE_XSMALL = 1;
8970
8971 /**
8972 * Size value for use with {@link #setCustomSizePreset} to show this notification
8973 * with a small size.
8974 * <p>This value is only applicable for custom display notifications created using
8975 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008976 *
8977 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008978 */
Gus Prevasd7363752018-09-18 14:35:15 -04008979 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008980 public static final int SIZE_SMALL = 2;
8981
8982 /**
8983 * Size value for use with {@link #setCustomSizePreset} to show this notification
8984 * with a medium size.
8985 * <p>This value is only applicable for custom display notifications created using
8986 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008987 *
8988 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008989 */
Gus Prevasd7363752018-09-18 14:35:15 -04008990 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008991 public static final int SIZE_MEDIUM = 3;
8992
8993 /**
8994 * Size value for use with {@link #setCustomSizePreset} to show this notification
8995 * with a large size.
8996 * <p>This value is only applicable for custom display notifications created using
8997 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008998 *
8999 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009000 */
Gus Prevasd7363752018-09-18 14:35:15 -04009001 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009002 public static final int SIZE_LARGE = 4;
9003
Griff Hazend5f11f92014-05-27 15:40:09 -07009004 /**
9005 * Size value for use with {@link #setCustomSizePreset} to show this notification
9006 * full screen.
9007 * <p>This value is only applicable for custom display notifications created using
9008 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04009009 *
9010 * @deprecated Display intents are no longer supported.
Griff Hazend5f11f92014-05-27 15:40:09 -07009011 */
Gus Prevasd7363752018-09-18 14:35:15 -04009012 @Deprecated
Griff Hazend5f11f92014-05-27 15:40:09 -07009013 public static final int SIZE_FULL_SCREEN = 5;
9014
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009015 /**
9016 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
9017 * short amount of time when this notification is displayed on the screen. This
9018 * is the default value.
Gus Prevasd7363752018-09-18 14:35:15 -04009019 *
9020 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009021 */
Gus Prevasd7363752018-09-18 14:35:15 -04009022 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009023 public static final int SCREEN_TIMEOUT_SHORT = 0;
9024
9025 /**
9026 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
9027 * for a longer amount of time when this notification is displayed on the screen.
Gus Prevasd7363752018-09-18 14:35:15 -04009028 *
9029 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009030 */
Gus Prevasd7363752018-09-18 14:35:15 -04009031 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009032 public static final int SCREEN_TIMEOUT_LONG = -1;
9033
Griff Hazen61a9e862014-05-22 16:05:19 -07009034 /** Notification extra which contains wearable extensions */
9035 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
9036
Pete Gastaf6781d2014-10-07 15:17:05 -04009037 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07009038 private static final String KEY_ACTIONS = "actions";
9039 private static final String KEY_FLAGS = "flags";
9040 private static final String KEY_DISPLAY_INTENT = "displayIntent";
9041 private static final String KEY_PAGES = "pages";
9042 private static final String KEY_BACKGROUND = "background";
9043 private static final String KEY_CONTENT_ICON = "contentIcon";
9044 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
9045 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
9046 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
9047 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
9048 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009049 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04009050 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009051 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07009052
9053 // Flags bitwise-ored to mFlags
9054 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
9055 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
9056 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
9057 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009058 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04009059 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04009060 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07009061
9062 // Default value for flags integer
9063 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
9064
9065 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
9066 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
9067
9068 private ArrayList<Action> mActions = new ArrayList<Action>();
9069 private int mFlags = DEFAULT_FLAGS;
9070 private PendingIntent mDisplayIntent;
9071 private ArrayList<Notification> mPages = new ArrayList<Notification>();
9072 private Bitmap mBackground;
9073 private int mContentIcon;
9074 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
9075 private int mContentActionIndex = UNSET_ACTION_INDEX;
9076 private int mCustomSizePreset = SIZE_DEFAULT;
9077 private int mCustomContentHeight;
9078 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009079 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04009080 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009081 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07009082
9083 /**
9084 * Create a {@link android.app.Notification.WearableExtender} with default
9085 * options.
9086 */
9087 public WearableExtender() {
9088 }
9089
9090 public WearableExtender(Notification notif) {
9091 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
9092 if (wearableBundle != null) {
9093 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
9094 if (actions != null) {
9095 mActions.addAll(actions);
9096 }
9097
9098 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
9099 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
9100
9101 Notification[] pages = getNotificationArrayFromBundle(
9102 wearableBundle, KEY_PAGES);
9103 if (pages != null) {
9104 Collections.addAll(mPages, pages);
9105 }
9106
9107 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
9108 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
9109 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
9110 DEFAULT_CONTENT_ICON_GRAVITY);
9111 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
9112 UNSET_ACTION_INDEX);
9113 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
9114 SIZE_DEFAULT);
9115 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
9116 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009117 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04009118 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009119 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07009120 }
9121 }
9122
9123 /**
9124 * Apply wearable extensions to a notification that is being built. This is typically
9125 * called by the {@link android.app.Notification.Builder#extend} method of
9126 * {@link android.app.Notification.Builder}.
9127 */
9128 @Override
9129 public Notification.Builder extend(Notification.Builder builder) {
9130 Bundle wearableBundle = new Bundle();
9131
9132 if (!mActions.isEmpty()) {
9133 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
9134 }
9135 if (mFlags != DEFAULT_FLAGS) {
9136 wearableBundle.putInt(KEY_FLAGS, mFlags);
9137 }
9138 if (mDisplayIntent != null) {
9139 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
9140 }
9141 if (!mPages.isEmpty()) {
9142 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
9143 new Notification[mPages.size()]));
9144 }
9145 if (mBackground != null) {
9146 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
9147 }
9148 if (mContentIcon != 0) {
9149 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
9150 }
9151 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
9152 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
9153 }
9154 if (mContentActionIndex != UNSET_ACTION_INDEX) {
9155 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
9156 mContentActionIndex);
9157 }
9158 if (mCustomSizePreset != SIZE_DEFAULT) {
9159 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
9160 }
9161 if (mCustomContentHeight != 0) {
9162 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
9163 }
9164 if (mGravity != DEFAULT_GRAVITY) {
9165 wearableBundle.putInt(KEY_GRAVITY, mGravity);
9166 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009167 if (mHintScreenTimeout != 0) {
9168 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
9169 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04009170 if (mDismissalId != null) {
9171 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
9172 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009173 if (mBridgeTag != null) {
9174 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
9175 }
Griff Hazen61a9e862014-05-22 16:05:19 -07009176
9177 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
9178 return builder;
9179 }
9180
9181 @Override
9182 public WearableExtender clone() {
9183 WearableExtender that = new WearableExtender();
9184 that.mActions = new ArrayList<Action>(this.mActions);
9185 that.mFlags = this.mFlags;
9186 that.mDisplayIntent = this.mDisplayIntent;
9187 that.mPages = new ArrayList<Notification>(this.mPages);
9188 that.mBackground = this.mBackground;
9189 that.mContentIcon = this.mContentIcon;
9190 that.mContentIconGravity = this.mContentIconGravity;
9191 that.mContentActionIndex = this.mContentActionIndex;
9192 that.mCustomSizePreset = this.mCustomSizePreset;
9193 that.mCustomContentHeight = this.mCustomContentHeight;
9194 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009195 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04009196 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009197 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07009198 return that;
9199 }
9200
9201 /**
9202 * Add a wearable action to this notification.
9203 *
9204 * <p>When wearable actions are added using this method, the set of actions that
9205 * show on a wearable device splits from devices that only show actions added
9206 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
9207 * of which actions display on different devices.
9208 *
9209 * @param action the action to add to this notification
9210 * @return this object for method chaining
9211 * @see android.app.Notification.Action
9212 */
9213 public WearableExtender addAction(Action action) {
9214 mActions.add(action);
9215 return this;
9216 }
9217
9218 /**
9219 * Adds wearable actions to this notification.
9220 *
9221 * <p>When wearable actions are added using this method, the set of actions that
9222 * show on a wearable device splits from devices that only show actions added
9223 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
9224 * of which actions display on different devices.
9225 *
9226 * @param actions the actions to add to this notification
9227 * @return this object for method chaining
9228 * @see android.app.Notification.Action
9229 */
9230 public WearableExtender addActions(List<Action> actions) {
9231 mActions.addAll(actions);
9232 return this;
9233 }
9234
9235 /**
9236 * Clear all wearable actions present on this builder.
9237 * @return this object for method chaining.
9238 * @see #addAction
9239 */
9240 public WearableExtender clearActions() {
9241 mActions.clear();
9242 return this;
9243 }
9244
9245 /**
9246 * Get the wearable actions present on this notification.
9247 */
9248 public List<Action> getActions() {
9249 return mActions;
9250 }
9251
9252 /**
9253 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07009254 * this notification. The {@link PendingIntent} provided should be for an activity.
9255 *
9256 * <pre class="prettyprint">
9257 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
9258 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
9259 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
9260 * Notification notif = new Notification.Builder(context)
9261 * .extend(new Notification.WearableExtender()
9262 * .setDisplayIntent(displayPendingIntent)
9263 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
9264 * .build();</pre>
9265 *
9266 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07009267 * should have an empty task affinity. It is also recommended to use the device
9268 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07009269 *
9270 * <p>Example AndroidManifest.xml entry:
9271 * <pre class="prettyprint">
9272 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
9273 * android:exported=&quot;true&quot;
9274 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07009275 * android:taskAffinity=&quot;&quot;
9276 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07009277 *
9278 * @param intent the {@link PendingIntent} for an activity
9279 * @return this object for method chaining
9280 * @see android.app.Notification.WearableExtender#getDisplayIntent
Gus Prevasd7363752018-09-18 14:35:15 -04009281 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009282 */
Gus Prevasd7363752018-09-18 14:35:15 -04009283 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009284 public WearableExtender setDisplayIntent(PendingIntent intent) {
9285 mDisplayIntent = intent;
9286 return this;
9287 }
9288
9289 /**
9290 * Get the intent to launch inside of an activity view when displaying this
9291 * notification. This {@code PendingIntent} should be for an activity.
Gus Prevasd7363752018-09-18 14:35:15 -04009292 *
9293 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009294 */
Gus Prevasd7363752018-09-18 14:35:15 -04009295 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009296 public PendingIntent getDisplayIntent() {
9297 return mDisplayIntent;
9298 }
9299
9300 /**
9301 * Add an additional page of content to display with this notification. The current
9302 * notification forms the first page, and pages added using this function form
9303 * subsequent pages. This field can be used to separate a notification into multiple
9304 * sections.
9305 *
9306 * @param page the notification to add as another page
9307 * @return this object for method chaining
9308 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04009309 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009310 */
Gus Prevasd7363752018-09-18 14:35:15 -04009311 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009312 public WearableExtender addPage(Notification page) {
9313 mPages.add(page);
9314 return this;
9315 }
9316
9317 /**
9318 * Add additional pages of content to display with this notification. The current
9319 * notification forms the first page, and pages added using this function form
9320 * subsequent pages. This field can be used to separate a notification into multiple
9321 * sections.
9322 *
9323 * @param pages a list of notifications
9324 * @return this object for method chaining
9325 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04009326 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009327 */
Gus Prevasd7363752018-09-18 14:35:15 -04009328 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009329 public WearableExtender addPages(List<Notification> pages) {
9330 mPages.addAll(pages);
9331 return this;
9332 }
9333
9334 /**
9335 * Clear all additional pages present on this builder.
9336 * @return this object for method chaining.
9337 * @see #addPage
Gus Prevasd7363752018-09-18 14:35:15 -04009338 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009339 */
Gus Prevasd7363752018-09-18 14:35:15 -04009340 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009341 public WearableExtender clearPages() {
9342 mPages.clear();
9343 return this;
9344 }
9345
9346 /**
9347 * Get the array of additional pages of content for displaying this notification. The
9348 * current notification forms the first page, and elements within this array form
9349 * subsequent pages. This field can be used to separate a notification into multiple
9350 * sections.
9351 * @return the pages for this notification
Gus Prevasd7363752018-09-18 14:35:15 -04009352 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009353 */
Gus Prevasd7363752018-09-18 14:35:15 -04009354 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009355 public List<Notification> getPages() {
9356 return mPages;
9357 }
9358
9359 /**
9360 * Set a background image to be displayed behind the notification content.
9361 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
9362 * will work with any notification style.
9363 *
9364 * @param background the background bitmap
9365 * @return this object for method chaining
9366 * @see android.app.Notification.WearableExtender#getBackground
Gus Prevasd7363752018-09-18 14:35:15 -04009367 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009368 */
Gus Prevasd7363752018-09-18 14:35:15 -04009369 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009370 public WearableExtender setBackground(Bitmap background) {
9371 mBackground = background;
9372 return this;
9373 }
9374
9375 /**
9376 * Get a background image to be displayed behind the notification content.
9377 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
9378 * will work with any notification style.
9379 *
9380 * @return the background image
9381 * @see android.app.Notification.WearableExtender#setBackground
Gus Prevasd7363752018-09-18 14:35:15 -04009382 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009383 */
Gus Prevasd7363752018-09-18 14:35:15 -04009384 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009385 public Bitmap getBackground() {
9386 return mBackground;
9387 }
9388
9389 /**
9390 * Set an icon that goes with the content of this notification.
9391 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009392 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009393 public WearableExtender setContentIcon(int icon) {
9394 mContentIcon = icon;
9395 return this;
9396 }
9397
9398 /**
9399 * Get an icon that goes with the content of this notification.
9400 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009401 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009402 public int getContentIcon() {
9403 return mContentIcon;
9404 }
9405
9406 /**
9407 * Set the gravity that the content icon should have within the notification display.
9408 * Supported values include {@link android.view.Gravity#START} and
9409 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
9410 * @see #setContentIcon
9411 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009412 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009413 public WearableExtender setContentIconGravity(int contentIconGravity) {
9414 mContentIconGravity = contentIconGravity;
9415 return this;
9416 }
9417
9418 /**
9419 * Get the gravity that the content icon should have within the notification display.
9420 * Supported values include {@link android.view.Gravity#START} and
9421 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
9422 * @see #getContentIcon
9423 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009424 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009425 public int getContentIconGravity() {
9426 return mContentIconGravity;
9427 }
9428
9429 /**
Gus Prevasd7363752018-09-18 14:35:15 -04009430 * Set an action from this notification's actions as the primary action. If the action has a
9431 * {@link RemoteInput} associated with it, shortcuts to the options for that input are shown
9432 * directly on the notification.
Griff Hazen14f57992014-05-26 09:07:14 -07009433 *
Gus Prevasd7363752018-09-18 14:35:15 -04009434 * @param actionIndex The index of the primary action.
Griff Hazen14f57992014-05-26 09:07:14 -07009435 * If wearable actions were added to the main notification, this index
9436 * will apply to that list, otherwise it will apply to the regular
9437 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07009438 */
9439 public WearableExtender setContentAction(int actionIndex) {
9440 mContentActionIndex = actionIndex;
9441 return this;
9442 }
9443
9444 /**
Gus Prevasd7363752018-09-18 14:35:15 -04009445 * Get the index of the notification action, if any, that was specified as the primary
9446 * action.
Griff Hazen14f57992014-05-26 09:07:14 -07009447 *
9448 * <p>If wearable specific actions were added to the main notification, this index will
9449 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07009450 *
9451 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07009452 */
9453 public int getContentAction() {
9454 return mContentActionIndex;
9455 }
9456
9457 /**
9458 * Set the gravity that this notification should have within the available viewport space.
9459 * Supported values include {@link android.view.Gravity#TOP},
9460 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
9461 * The default value is {@link android.view.Gravity#BOTTOM}.
9462 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009463 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009464 public WearableExtender setGravity(int gravity) {
9465 mGravity = gravity;
9466 return this;
9467 }
9468
9469 /**
9470 * Get the gravity that this notification should have within the available viewport space.
9471 * Supported values include {@link android.view.Gravity#TOP},
9472 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
9473 * The default value is {@link android.view.Gravity#BOTTOM}.
9474 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009475 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009476 public int getGravity() {
9477 return mGravity;
9478 }
9479
9480 /**
9481 * Set the custom size preset for the display of this notification out of the available
9482 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
9483 * {@link #SIZE_LARGE}.
9484 * <p>Some custom size presets are only applicable for custom display notifications created
9485 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
9486 * documentation for the preset in question. See also
9487 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
9488 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009489 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009490 public WearableExtender setCustomSizePreset(int sizePreset) {
9491 mCustomSizePreset = sizePreset;
9492 return this;
9493 }
9494
9495 /**
9496 * Get the custom size preset for the display of this notification out of the available
9497 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
9498 * {@link #SIZE_LARGE}.
9499 * <p>Some custom size presets are only applicable for custom display notifications created
9500 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
9501 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
9502 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009503 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009504 public int getCustomSizePreset() {
9505 return mCustomSizePreset;
9506 }
9507
9508 /**
9509 * Set the custom height in pixels for the display of this notification's content.
9510 * <p>This option is only available for custom display notifications created
9511 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
9512 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
9513 * {@link #getCustomContentHeight}.
9514 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009515 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009516 public WearableExtender setCustomContentHeight(int height) {
9517 mCustomContentHeight = height;
9518 return this;
9519 }
9520
9521 /**
9522 * Get the custom height in pixels for the display of this notification's content.
9523 * <p>This option is only available for custom display notifications created
9524 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
9525 * {@link #setCustomContentHeight}.
9526 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009527 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009528 public int getCustomContentHeight() {
9529 return mCustomContentHeight;
9530 }
9531
9532 /**
9533 * Set whether the scrolling position for the contents of this notification should start
9534 * at the bottom of the contents instead of the top when the contents are too long to
9535 * display within the screen. Default is false (start scroll at the top).
9536 */
9537 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
9538 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
9539 return this;
9540 }
9541
9542 /**
9543 * Get whether the scrolling position for the contents of this notification should start
9544 * at the bottom of the contents instead of the top when the contents are too long to
9545 * display within the screen. Default is false (start scroll at the top).
9546 */
9547 public boolean getStartScrollBottom() {
9548 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
9549 }
9550
9551 /**
9552 * Set whether the content intent is available when the wearable device is not connected
9553 * to a companion device. The user can still trigger this intent when the wearable device
9554 * is offline, but a visual hint will indicate that the content intent may not be available.
9555 * Defaults to true.
9556 */
9557 public WearableExtender setContentIntentAvailableOffline(
9558 boolean contentIntentAvailableOffline) {
9559 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
9560 return this;
9561 }
9562
9563 /**
9564 * Get whether the content intent is available when the wearable device is not connected
9565 * to a companion device. The user can still trigger this intent when the wearable device
9566 * is offline, but a visual hint will indicate that the content intent may not be available.
9567 * Defaults to true.
9568 */
9569 public boolean getContentIntentAvailableOffline() {
9570 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
9571 }
9572
9573 /**
9574 * Set a hint that this notification's icon should not be displayed.
9575 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
9576 * @return this object for method chaining
9577 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009578 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009579 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
9580 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
9581 return this;
9582 }
9583
9584 /**
9585 * Get a hint that this notification's icon should not be displayed.
9586 * @return {@code true} if this icon should not be displayed, false otherwise.
9587 * The default value is {@code false} if this was never set.
9588 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009589 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009590 public boolean getHintHideIcon() {
9591 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
9592 }
9593
9594 /**
9595 * Set a visual hint that only the background image of this notification should be
9596 * displayed, and other semantic content should be hidden. This hint is only applicable
9597 * to sub-pages added using {@link #addPage}.
9598 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009599 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009600 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
9601 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
9602 return this;
9603 }
9604
9605 /**
9606 * Get a visual hint that only the background image of this notification should be
9607 * displayed, and other semantic content should be hidden. This hint is only applicable
9608 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
9609 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009610 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009611 public boolean getHintShowBackgroundOnly() {
9612 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
9613 }
9614
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009615 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009616 * Set a hint that this notification's background should not be clipped if possible,
9617 * and should instead be resized to fully display on the screen, retaining the aspect
9618 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009619 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
9620 * @return this object for method chaining
9621 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009622 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009623 public WearableExtender setHintAvoidBackgroundClipping(
9624 boolean hintAvoidBackgroundClipping) {
9625 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
9626 return this;
9627 }
9628
9629 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009630 * Get a hint that this notification's background should not be clipped if possible,
9631 * and should instead be resized to fully display on the screen, retaining the aspect
9632 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009633 * @return {@code true} if it's ok if the background is clipped on the screen, false
9634 * otherwise. The default value is {@code false} if this was never set.
9635 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009636 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009637 public boolean getHintAvoidBackgroundClipping() {
9638 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
9639 }
9640
9641 /**
9642 * Set a hint that the screen should remain on for at least this duration when
9643 * this notification is displayed on the screen.
9644 * @param timeout The requested screen timeout in milliseconds. Can also be either
9645 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9646 * @return this object for method chaining
9647 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009648 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009649 public WearableExtender setHintScreenTimeout(int timeout) {
9650 mHintScreenTimeout = timeout;
9651 return this;
9652 }
9653
9654 /**
9655 * Get the duration, in milliseconds, that the screen should remain on for
9656 * when this notification is displayed.
9657 * @return the duration in milliseconds if > 0, or either one of the sentinel values
9658 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9659 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009660 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009661 public int getHintScreenTimeout() {
9662 return mHintScreenTimeout;
9663 }
9664
Alex Hills9ab3a232016-04-05 14:54:56 -04009665 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04009666 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
9667 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9668 * qr codes, as well as other simple black-and-white tickets.
9669 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
9670 * @return this object for method chaining
Gus Prevasd7363752018-09-18 14:35:15 -04009671 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009672 */
Gus Prevasd7363752018-09-18 14:35:15 -04009673 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009674 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
9675 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
9676 return this;
9677 }
9678
9679 /**
9680 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
9681 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9682 * qr codes, as well as other simple black-and-white tickets.
9683 * @return {@code true} if it should be displayed in ambient, false otherwise
9684 * otherwise. The default value is {@code false} if this was never set.
Gus Prevasd7363752018-09-18 14:35:15 -04009685 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009686 */
Gus Prevasd7363752018-09-18 14:35:15 -04009687 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009688 public boolean getHintAmbientBigPicture() {
9689 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
9690 }
9691
9692 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04009693 * Set a hint that this notification's content intent will launch an {@link Activity}
9694 * directly, telling the platform that it can generate the appropriate transitions.
9695 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
9696 * an activity and transitions should be generated, false otherwise.
9697 * @return this object for method chaining
9698 */
9699 public WearableExtender setHintContentIntentLaunchesActivity(
9700 boolean hintContentIntentLaunchesActivity) {
9701 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
9702 return this;
9703 }
9704
9705 /**
9706 * Get a hint that this notification's content intent will launch an {@link Activity}
9707 * directly, telling the platform that it can generate the appropriate transitions
9708 * @return {@code true} if the content intent will launch an activity and transitions should
9709 * be generated, false otherwise. The default value is {@code false} if this was never set.
9710 */
9711 public boolean getHintContentIntentLaunchesActivity() {
9712 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
9713 }
9714
Nadia Benbernou948627e2016-04-14 14:41:08 -04009715 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009716 * Sets the dismissal id for this notification. If a notification is posted with a
9717 * dismissal id, then when that notification is canceled, notifications on other wearables
9718 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04009719 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009720 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04009721 * @param dismissalId the dismissal id of the notification.
9722 * @return this object for method chaining
9723 */
9724 public WearableExtender setDismissalId(String dismissalId) {
9725 mDismissalId = dismissalId;
9726 return this;
9727 }
9728
9729 /**
9730 * Returns the dismissal id of the notification.
9731 * @return the dismissal id of the notification or null if it has not been set.
9732 */
9733 public String getDismissalId() {
9734 return mDismissalId;
9735 }
9736
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009737 /**
9738 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
9739 * posted from a phone to provide finer-grained control on what notifications are bridged
9740 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
9741 * Features to Notifications</a> for more information.
9742 * @param bridgeTag the bridge tag of the notification.
9743 * @return this object for method chaining
9744 */
9745 public WearableExtender setBridgeTag(String bridgeTag) {
9746 mBridgeTag = bridgeTag;
9747 return this;
9748 }
9749
9750 /**
9751 * Returns the bridge tag of the notification.
9752 * @return the bridge tag or null if not present.
9753 */
9754 public String getBridgeTag() {
9755 return mBridgeTag;
9756 }
9757
Griff Hazen61a9e862014-05-22 16:05:19 -07009758 private void setFlag(int mask, boolean value) {
9759 if (value) {
9760 mFlags |= mask;
9761 } else {
9762 mFlags &= ~mask;
9763 }
9764 }
9765 }
9766
9767 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009768 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
9769 * with car extensions:
9770 *
9771 * <ol>
9772 * <li>Create an {@link Notification.Builder}, setting any desired
9773 * properties.
9774 * <li>Create a {@link CarExtender}.
9775 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
9776 * {@link CarExtender}.
9777 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9778 * to apply the extensions to a notification.
9779 * </ol>
9780 *
9781 * <pre class="prettyprint">
9782 * Notification notification = new Notification.Builder(context)
9783 * ...
9784 * .extend(new CarExtender()
9785 * .set*(...))
9786 * .build();
9787 * </pre>
9788 *
9789 * <p>Car extensions can be accessed on an existing notification by using the
9790 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
9791 * to access values.
9792 */
9793 public static final class CarExtender implements Extender {
9794 private static final String TAG = "CarExtender";
9795
9796 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
9797 private static final String EXTRA_LARGE_ICON = "large_icon";
9798 private static final String EXTRA_CONVERSATION = "car_conversation";
9799 private static final String EXTRA_COLOR = "app_color";
9800
9801 private Bitmap mLargeIcon;
9802 private UnreadConversation mUnreadConversation;
9803 private int mColor = Notification.COLOR_DEFAULT;
9804
9805 /**
9806 * Create a {@link CarExtender} with default options.
9807 */
9808 public CarExtender() {
9809 }
9810
9811 /**
9812 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
9813 *
9814 * @param notif The notification from which to copy options.
9815 */
9816 public CarExtender(Notification notif) {
9817 Bundle carBundle = notif.extras == null ?
9818 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
9819 if (carBundle != null) {
9820 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
9821 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
9822
9823 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
9824 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
9825 }
9826 }
9827
9828 /**
9829 * Apply car extensions to a notification that is being built. This is typically called by
9830 * the {@link Notification.Builder#extend(Notification.Extender)}
9831 * method of {@link Notification.Builder}.
9832 */
9833 @Override
9834 public Notification.Builder extend(Notification.Builder builder) {
9835 Bundle carExtensions = new Bundle();
9836
9837 if (mLargeIcon != null) {
9838 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
9839 }
9840 if (mColor != Notification.COLOR_DEFAULT) {
9841 carExtensions.putInt(EXTRA_COLOR, mColor);
9842 }
9843
9844 if (mUnreadConversation != null) {
9845 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
9846 carExtensions.putBundle(EXTRA_CONVERSATION, b);
9847 }
9848
9849 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
9850 return builder;
9851 }
9852
9853 /**
9854 * Sets the accent color to use when Android Auto presents the notification.
9855 *
9856 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
9857 * to accent the displayed notification. However, not all colors are acceptable in an
9858 * automotive setting. This method can be used to override the color provided in the
9859 * notification in such a situation.
9860 */
Tor Norbye80756e32015-03-02 09:39:27 -08009861 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009862 mColor = color;
9863 return this;
9864 }
9865
9866 /**
9867 * Gets the accent color.
9868 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04009869 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009870 */
Tor Norbye80756e32015-03-02 09:39:27 -08009871 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009872 public int getColor() {
9873 return mColor;
9874 }
9875
9876 /**
9877 * Sets the large icon of the car notification.
9878 *
9879 * If no large icon is set in the extender, Android Auto will display the icon
9880 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
9881 *
9882 * @param largeIcon The large icon to use in the car notification.
9883 * @return This object for method chaining.
9884 */
9885 public CarExtender setLargeIcon(Bitmap largeIcon) {
9886 mLargeIcon = largeIcon;
9887 return this;
9888 }
9889
9890 /**
9891 * Gets the large icon used in this car notification, or null if no icon has been set.
9892 *
9893 * @return The large icon for the car notification.
9894 * @see CarExtender#setLargeIcon
9895 */
9896 public Bitmap getLargeIcon() {
9897 return mLargeIcon;
9898 }
9899
9900 /**
9901 * Sets the unread conversation in a message notification.
9902 *
9903 * @param unreadConversation The unread part of the conversation this notification conveys.
9904 * @return This object for method chaining.
9905 */
9906 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
9907 mUnreadConversation = unreadConversation;
9908 return this;
9909 }
9910
9911 /**
9912 * Returns the unread conversation conveyed by this notification.
9913 * @see #setUnreadConversation(UnreadConversation)
9914 */
9915 public UnreadConversation getUnreadConversation() {
9916 return mUnreadConversation;
9917 }
9918
9919 /**
9920 * A class which holds the unread messages from a conversation.
9921 */
9922 public static class UnreadConversation {
9923 private static final String KEY_AUTHOR = "author";
9924 private static final String KEY_TEXT = "text";
9925 private static final String KEY_MESSAGES = "messages";
9926 private static final String KEY_REMOTE_INPUT = "remote_input";
9927 private static final String KEY_ON_REPLY = "on_reply";
9928 private static final String KEY_ON_READ = "on_read";
9929 private static final String KEY_PARTICIPANTS = "participants";
9930 private static final String KEY_TIMESTAMP = "timestamp";
9931
9932 private final String[] mMessages;
9933 private final RemoteInput mRemoteInput;
9934 private final PendingIntent mReplyPendingIntent;
9935 private final PendingIntent mReadPendingIntent;
9936 private final String[] mParticipants;
9937 private final long mLatestTimestamp;
9938
9939 UnreadConversation(String[] messages, RemoteInput remoteInput,
9940 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
9941 String[] participants, long latestTimestamp) {
9942 mMessages = messages;
9943 mRemoteInput = remoteInput;
9944 mReadPendingIntent = readPendingIntent;
9945 mReplyPendingIntent = replyPendingIntent;
9946 mParticipants = participants;
9947 mLatestTimestamp = latestTimestamp;
9948 }
9949
9950 /**
9951 * Gets the list of messages conveyed by this notification.
9952 */
9953 public String[] getMessages() {
9954 return mMessages;
9955 }
9956
9957 /**
9958 * Gets the remote input that will be used to convey the response to a message list, or
9959 * null if no such remote input exists.
9960 */
9961 public RemoteInput getRemoteInput() {
9962 return mRemoteInput;
9963 }
9964
9965 /**
9966 * Gets the pending intent that will be triggered when the user replies to this
9967 * notification.
9968 */
9969 public PendingIntent getReplyPendingIntent() {
9970 return mReplyPendingIntent;
9971 }
9972
9973 /**
9974 * Gets the pending intent that Android Auto will send after it reads aloud all messages
9975 * in this object's message list.
9976 */
9977 public PendingIntent getReadPendingIntent() {
9978 return mReadPendingIntent;
9979 }
9980
9981 /**
9982 * Gets the participants in the conversation.
9983 */
9984 public String[] getParticipants() {
9985 return mParticipants;
9986 }
9987
9988 /**
9989 * Gets the firs participant in the conversation.
9990 */
9991 public String getParticipant() {
9992 return mParticipants.length > 0 ? mParticipants[0] : null;
9993 }
9994
9995 /**
9996 * Gets the timestamp of the conversation.
9997 */
9998 public long getLatestTimestamp() {
9999 return mLatestTimestamp;
10000 }
10001
10002 Bundle getBundleForUnreadConversation() {
10003 Bundle b = new Bundle();
10004 String author = null;
10005 if (mParticipants != null && mParticipants.length > 1) {
10006 author = mParticipants[0];
10007 }
10008 Parcelable[] messages = new Parcelable[mMessages.length];
10009 for (int i = 0; i < messages.length; i++) {
10010 Bundle m = new Bundle();
10011 m.putString(KEY_TEXT, mMessages[i]);
10012 m.putString(KEY_AUTHOR, author);
10013 messages[i] = m;
10014 }
10015 b.putParcelableArray(KEY_MESSAGES, messages);
10016 if (mRemoteInput != null) {
10017 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
10018 }
10019 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
10020 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
10021 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
10022 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
10023 return b;
10024 }
10025
10026 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
10027 if (b == null) {
10028 return null;
10029 }
10030 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
10031 String[] messages = null;
10032 if (parcelableMessages != null) {
10033 String[] tmp = new String[parcelableMessages.length];
10034 boolean success = true;
10035 for (int i = 0; i < tmp.length; i++) {
10036 if (!(parcelableMessages[i] instanceof Bundle)) {
10037 success = false;
10038 break;
10039 }
10040 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
10041 if (tmp[i] == null) {
10042 success = false;
10043 break;
10044 }
10045 }
10046 if (success) {
10047 messages = tmp;
10048 } else {
10049 return null;
10050 }
10051 }
10052
10053 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
10054 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
10055
10056 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
10057
10058 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
10059 if (participants == null || participants.length != 1) {
10060 return null;
10061 }
10062
10063 return new UnreadConversation(messages,
10064 remoteInput,
10065 onReply,
10066 onRead,
10067 participants, b.getLong(KEY_TIMESTAMP));
10068 }
10069 };
10070
10071 /**
10072 * Builder class for {@link CarExtender.UnreadConversation} objects.
10073 */
10074 public static class Builder {
10075 private final List<String> mMessages = new ArrayList<String>();
10076 private final String mParticipant;
10077 private RemoteInput mRemoteInput;
10078 private PendingIntent mReadPendingIntent;
10079 private PendingIntent mReplyPendingIntent;
10080 private long mLatestTimestamp;
10081
10082 /**
10083 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
10084 *
10085 * @param name The name of the other participant in the conversation.
10086 */
10087 public Builder(String name) {
10088 mParticipant = name;
10089 }
10090
10091 /**
10092 * Appends a new unread message to the list of messages for this conversation.
10093 *
10094 * The messages should be added from oldest to newest.
10095 *
10096 * @param message The text of the new unread message.
10097 * @return This object for method chaining.
10098 */
10099 public Builder addMessage(String message) {
10100 mMessages.add(message);
10101 return this;
10102 }
10103
10104 /**
10105 * Sets the pending intent and remote input which will convey the reply to this
10106 * notification.
10107 *
10108 * @param pendingIntent The pending intent which will be triggered on a reply.
10109 * @param remoteInput The remote input parcelable which will carry the reply.
10110 * @return This object for method chaining.
10111 *
10112 * @see CarExtender.UnreadConversation#getRemoteInput
10113 * @see CarExtender.UnreadConversation#getReplyPendingIntent
10114 */
10115 public Builder setReplyAction(
10116 PendingIntent pendingIntent, RemoteInput remoteInput) {
10117 mRemoteInput = remoteInput;
10118 mReplyPendingIntent = pendingIntent;
10119
10120 return this;
10121 }
10122
10123 /**
10124 * Sets the pending intent that will be sent once the messages in this notification
10125 * are read.
10126 *
10127 * @param pendingIntent The pending intent to use.
10128 * @return This object for method chaining.
10129 */
10130 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
10131 mReadPendingIntent = pendingIntent;
10132 return this;
10133 }
10134
10135 /**
10136 * Sets the timestamp of the most recent message in an unread conversation.
10137 *
10138 * If a messaging notification has been posted by your application and has not
10139 * yet been cancelled, posting a later notification with the same id and tag
10140 * but without a newer timestamp may result in Android Auto not displaying a
10141 * heads up notification for the later notification.
10142 *
10143 * @param timestamp The timestamp of the most recent message in the conversation.
10144 * @return This object for method chaining.
10145 */
10146 public Builder setLatestTimestamp(long timestamp) {
10147 mLatestTimestamp = timestamp;
10148 return this;
10149 }
10150
10151 /**
10152 * Builds a new unread conversation object.
10153 *
10154 * @return The new unread conversation object.
10155 */
10156 public UnreadConversation build() {
10157 String[] messages = mMessages.toArray(new String[mMessages.size()]);
10158 String[] participants = { mParticipant };
10159 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
10160 mReadPendingIntent, participants, mLatestTimestamp);
10161 }
10162 }
10163 }
10164
10165 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010166 * <p>Helper class to add Android TV extensions to notifications. To create a notification
10167 * with a TV extension:
10168 *
10169 * <ol>
10170 * <li>Create an {@link Notification.Builder}, setting any desired properties.
10171 * <li>Create a {@link TvExtender}.
10172 * <li>Set TV-specific properties using the {@code set} methods of
10173 * {@link TvExtender}.
10174 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
10175 * to apply the extension to a notification.
10176 * </ol>
10177 *
10178 * <pre class="prettyprint">
10179 * Notification notification = new Notification.Builder(context)
10180 * ...
10181 * .extend(new TvExtender()
10182 * .set*(...))
10183 * .build();
10184 * </pre>
10185 *
10186 * <p>TV extensions can be accessed on an existing notification by using the
10187 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
10188 * to access values.
10189 *
10190 * @hide
10191 */
10192 @SystemApi
10193 public static final class TvExtender implements Extender {
10194 private static final String TAG = "TvExtender";
10195
10196 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
10197 private static final String EXTRA_FLAGS = "flags";
10198 private static final String EXTRA_CONTENT_INTENT = "content_intent";
10199 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010200 private static final String EXTRA_CHANNEL_ID = "channel_id";
Rhiannon Malia1a083932018-01-24 15:02:30 -080010201 private static final String EXTRA_SUPPRESS_SHOW_OVER_APPS = "suppressShowOverApps";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010202
10203 // Flags bitwise-ored to mFlags
10204 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
10205
10206 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010207 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010208 private PendingIntent mContentIntent;
10209 private PendingIntent mDeleteIntent;
Rhiannon Malia1a083932018-01-24 15:02:30 -080010210 private boolean mSuppressShowOverApps;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010211
10212 /**
10213 * Create a {@link TvExtender} with default options.
10214 */
10215 public TvExtender() {
10216 mFlags = FLAG_AVAILABLE_ON_TV;
10217 }
10218
10219 /**
10220 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
10221 *
10222 * @param notif The notification from which to copy options.
10223 */
10224 public TvExtender(Notification notif) {
10225 Bundle bundle = notif.extras == null ?
10226 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
10227 if (bundle != null) {
10228 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010229 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Rhiannon Malia1a083932018-01-24 15:02:30 -080010230 mSuppressShowOverApps = bundle.getBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010231 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
10232 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
10233 }
10234 }
10235
10236 /**
10237 * Apply a TV extension to a notification that is being built. This is typically called by
10238 * the {@link Notification.Builder#extend(Notification.Extender)}
10239 * method of {@link Notification.Builder}.
10240 */
10241 @Override
10242 public Notification.Builder extend(Notification.Builder builder) {
10243 Bundle bundle = new Bundle();
10244
10245 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010246 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Rhiannon Malia1a083932018-01-24 15:02:30 -080010247 bundle.putBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS, mSuppressShowOverApps);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010248 if (mContentIntent != null) {
10249 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
10250 }
10251
10252 if (mDeleteIntent != null) {
10253 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
10254 }
10255
10256 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
10257 return builder;
10258 }
10259
10260 /**
10261 * Returns true if this notification should be shown on TV. This method return true
10262 * if the notification was extended with a TvExtender.
10263 */
10264 public boolean isAvailableOnTv() {
10265 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
10266 }
10267
10268 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010269 * Specifies the channel the notification should be delivered on when shown on TV.
10270 * It can be different from the channel that the notification is delivered to when
10271 * posting on a non-TV device.
10272 */
10273 public TvExtender setChannel(String channelId) {
10274 mChannelId = channelId;
10275 return this;
10276 }
10277
10278 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -040010279 * Specifies the channel the notification should be delivered on when shown on TV.
10280 * It can be different from the channel that the notification is delivered to when
10281 * posting on a non-TV device.
10282 */
10283 public TvExtender setChannelId(String channelId) {
10284 mChannelId = channelId;
10285 return this;
10286 }
10287
Jeff Sharkey000ce802017-04-29 13:13:27 -060010288 /** @removed */
10289 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010290 public String getChannel() {
10291 return mChannelId;
10292 }
10293
10294 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -040010295 * Returns the id of the channel this notification posts to on TV.
10296 */
10297 public String getChannelId() {
10298 return mChannelId;
10299 }
10300
10301 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010302 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
10303 * If provided, it is used instead of the content intent specified
10304 * at the level of Notification.
10305 */
10306 public TvExtender setContentIntent(PendingIntent intent) {
10307 mContentIntent = intent;
10308 return this;
10309 }
10310
10311 /**
10312 * Returns the TV-specific content intent. If this method returns null, the
10313 * main content intent on the notification should be used.
10314 *
10315 * @see {@link Notification#contentIntent}
10316 */
10317 public PendingIntent getContentIntent() {
10318 return mContentIntent;
10319 }
10320
10321 /**
10322 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
10323 * by the user on TV. If provided, it is used instead of the delete intent specified
10324 * at the level of Notification.
10325 */
10326 public TvExtender setDeleteIntent(PendingIntent intent) {
10327 mDeleteIntent = intent;
10328 return this;
10329 }
10330
10331 /**
10332 * Returns the TV-specific delete intent. If this method returns null, the
10333 * main delete intent on the notification should be used.
10334 *
10335 * @see {@link Notification#deleteIntent}
10336 */
10337 public PendingIntent getDeleteIntent() {
10338 return mDeleteIntent;
10339 }
Rhiannon Malia1a083932018-01-24 15:02:30 -080010340
10341 /**
10342 * Specifies whether this notification should suppress showing a message over top of apps
10343 * outside of the launcher.
10344 */
10345 public TvExtender setSuppressShowOverApps(boolean suppress) {
10346 mSuppressShowOverApps = suppress;
10347 return this;
10348 }
10349
10350 /**
10351 * Returns true if this notification should not show messages over top of apps
10352 * outside of the launcher.
10353 */
10354 public boolean getSuppressShowOverApps() {
10355 return mSuppressShowOverApps;
10356 }
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010357 }
10358
10359 /**
Griff Hazen61a9e862014-05-22 16:05:19 -070010360 * Get an array of Notification objects from a parcelable array bundle field.
10361 * Update the bundle to have a typed array so fetches in the future don't need
10362 * to do an array copy.
10363 */
10364 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
10365 Parcelable[] array = bundle.getParcelableArray(key);
10366 if (array instanceof Notification[] || array == null) {
10367 return (Notification[]) array;
10368 }
10369 Notification[] typedArray = Arrays.copyOf(array, array.length,
10370 Notification[].class);
10371 bundle.putParcelableArray(key, typedArray);
10372 return typedArray;
10373 }
Christoph Studer4600f9b2014-07-22 22:44:43 +020010374
10375 private static class BuilderRemoteViews extends RemoteViews {
10376 public BuilderRemoteViews(Parcel parcel) {
10377 super(parcel);
10378 }
10379
Kenny Guy77320062014-08-27 21:37:15 +010010380 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
10381 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +020010382 }
10383
10384 @Override
10385 public BuilderRemoteViews clone() {
10386 Parcel p = Parcel.obtain();
10387 writeToParcel(p, 0);
10388 p.setDataPosition(0);
10389 BuilderRemoteViews brv = new BuilderRemoteViews(p);
10390 p.recycle();
10391 return brv;
10392 }
10393 }
Adrian Roos70d7aa32017-01-11 15:39:06 -080010394
Selim Cinek384804b2018-04-18 14:31:07 +080010395 /**
10396 * A result object where information about the template that was created is saved.
10397 */
10398 private static class TemplateBindResult {
Selim Cinek1c72fa02018-04-23 18:00:54 +080010399 int mIconMarginEnd;
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010400 boolean mRightIconContainerVisible;
Selim Cinek384804b2018-04-18 14:31:07 +080010401
10402 /**
Selim Cinek1c72fa02018-04-23 18:00:54 +080010403 * Get the margin end that needs to be added to any fields that may overlap
Selim Cinek384804b2018-04-18 14:31:07 +080010404 * with the right actions.
10405 */
Selim Cinek1c72fa02018-04-23 18:00:54 +080010406 public int getIconMarginEnd() {
10407 return mIconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +080010408 }
10409
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010410 /**
10411 * Is the icon container visible on the right size because of the reply button or the
10412 * right icon.
10413 */
10414 public boolean isRightIconContainerVisible() {
10415 return mRightIconContainerVisible;
10416 }
10417
Selim Cinek1c72fa02018-04-23 18:00:54 +080010418 public void setIconMarginEnd(int iconMarginEnd) {
10419 this.mIconMarginEnd = iconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +080010420 }
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010421
10422 public void setRightIconContainerVisible(boolean iconContainerVisible) {
10423 mRightIconContainerVisible = iconContainerVisible;
10424 }
Selim Cinek384804b2018-04-18 14:31:07 +080010425 }
10426
Adrian Roos70d7aa32017-01-11 15:39:06 -080010427 private static class StandardTemplateParams {
10428 boolean hasProgress = true;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010429 CharSequence title;
10430 CharSequence text;
Selim Cinekafeed292017-12-12 17:32:44 -080010431 CharSequence headerTextSecondary;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010432 CharSequence summaryText;
Selim Cinekbee4e072018-05-21 22:06:43 -070010433 int maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek88188f22017-09-19 16:46:56 -070010434 boolean hideLargeIcon;
Selim Cinek384804b2018-04-18 14:31:07 +080010435 boolean hideReplyIcon;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010436 boolean allowColorization = true;
10437 boolean forceDefaultColor = false;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010438
10439 final StandardTemplateParams reset() {
10440 hasProgress = true;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010441 title = null;
10442 text = null;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010443 summaryText = null;
Selim Cinekafeed292017-12-12 17:32:44 -080010444 headerTextSecondary = null;
Selim Cinekbee4e072018-05-21 22:06:43 -070010445 maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010446 allowColorization = true;
10447 forceDefaultColor = false;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010448 return this;
10449 }
10450
10451 final StandardTemplateParams hasProgress(boolean hasProgress) {
10452 this.hasProgress = hasProgress;
10453 return this;
10454 }
10455
10456 final StandardTemplateParams title(CharSequence title) {
10457 this.title = title;
10458 return this;
10459 }
10460
10461 final StandardTemplateParams text(CharSequence text) {
10462 this.text = text;
10463 return this;
10464 }
10465
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010466 final StandardTemplateParams summaryText(CharSequence text) {
10467 this.summaryText = text;
10468 return this;
10469 }
10470
Selim Cinekafeed292017-12-12 17:32:44 -080010471 final StandardTemplateParams headerTextSecondary(CharSequence text) {
10472 this.headerTextSecondary = text;
10473 return this;
10474 }
10475
Selim Cinek384804b2018-04-18 14:31:07 +080010476 final StandardTemplateParams hideLargeIcon(boolean hideLargeIcon) {
10477 this.hideLargeIcon = hideLargeIcon;
Selim Cinek88188f22017-09-19 16:46:56 -070010478 return this;
10479 }
10480
Selim Cinek384804b2018-04-18 14:31:07 +080010481 final StandardTemplateParams hideReplyIcon(boolean hideReplyIcon) {
10482 this.hideReplyIcon = hideReplyIcon;
Selim Cinek88188f22017-09-19 16:46:56 -070010483 return this;
10484 }
10485
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010486 final StandardTemplateParams disallowColorization() {
10487 this.allowColorization = false;
10488 return this;
10489 }
10490
10491 final StandardTemplateParams forceDefaultColor() {
10492 this.forceDefaultColor = true;
10493 return this;
10494 }
10495
Adrian Roos70d7aa32017-01-11 15:39:06 -080010496 final StandardTemplateParams fillTextsFrom(Builder b) {
10497 Bundle extras = b.mN.extras;
Lucas Dupin00be88f2019-01-03 17:50:52 -080010498 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE));
Lucas Dupin06c5e642017-09-13 16:34:58 -070010499
Lucas Dupin06c5e642017-09-13 16:34:58 -070010500 CharSequence text = extras.getCharSequence(EXTRA_BIG_TEXT);
Lucas Dupin00be88f2019-01-03 17:50:52 -080010501 if (TextUtils.isEmpty(text)) {
Lucas Dupin06c5e642017-09-13 16:34:58 -070010502 text = extras.getCharSequence(EXTRA_TEXT);
10503 }
Lucas Dupin00be88f2019-01-03 17:50:52 -080010504 this.text = b.processLegacyText(text);
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010505 this.summaryText = extras.getCharSequence(EXTRA_SUB_TEXT);
Adrian Roos70d7aa32017-01-11 15:39:06 -080010506 return this;
10507 }
Selim Cinekbee4e072018-05-21 22:06:43 -070010508
10509 /**
10510 * Set the maximum lines of remote input history lines allowed.
10511 * @param maxRemoteInputHistory The number of lines.
10512 * @return The builder for method chaining.
10513 */
10514 public StandardTemplateParams setMaxRemoteInputHistory(int maxRemoteInputHistory) {
10515 this.maxRemoteInputHistory = maxRemoteInputHistory;
10516 return this;
10517 }
Adrian Roos70d7aa32017-01-11 15:39:06 -080010518 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010519}