blob: d95d3bacca5c1bceb1f7f05f20c87bad3d9fc2ab [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Mady Mellor8a529e22019-03-25 17:37:55 -070019import static android.annotation.Dimension.DP;
Mady Mellor9848a6c2019-03-19 15:29:05 -070020import static android.graphics.drawable.Icon.TYPE_BITMAP;
21
Lucas Dupina291d192018-06-07 13:59:42 -070022import static com.android.internal.util.ContrastColorUtil.satisfiesTextContrast;
Selim Cinek389edcd2017-05-11 19:16:44 -070023
Tor Norbye80756e32015-03-02 09:39:27 -080024import android.annotation.ColorInt;
Mady Mellor7af771a2019-03-07 15:04:54 -080025import android.annotation.DimenRes;
Mady Mellor8a529e22019-03-25 17:37:55 -070026import android.annotation.Dimension;
Tor Norbye7b9c9122013-05-30 16:48:33 -070027import android.annotation.DrawableRes;
Gus Prevas9cc96602018-10-11 11:24:23 -040028import android.annotation.IdRes;
Tor Norbyed9273d62013-05-30 15:59:53 -070029import android.annotation.IntDef;
Alex Hillsfd590442016-10-07 09:52:44 -040030import android.annotation.NonNull;
Selim Cinek88188f22017-09-19 16:46:56 -070031import android.annotation.Nullable;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060032import android.annotation.RequiresPermission;
Daniel Sandler01df1c62014-06-09 10:54:01 -040033import android.annotation.SdkConstant;
34import android.annotation.SdkConstant.SdkConstantType;
Julia Reynoldse46bb372016-03-17 11:05:58 -040035import android.annotation.SystemApi;
Mathew Inwood61e8ae62018-08-14 14:17:44 +010036import android.annotation.UnsupportedAppUsage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.content.Context;
38import android.content.Intent;
Felipe Leme90205ef2019-03-05 09:59:52 -080039import android.content.LocusId;
Kenny Guy77320062014-08-27 21:37:15 +010040import android.content.pm.ApplicationInfo;
Dan Sandler732bd6c2016-04-12 14:20:32 -040041import android.content.pm.PackageManager;
Christoph Studer4600f9b2014-07-22 22:44:43 +020042import android.content.pm.PackageManager.NameNotFoundException;
Julia Reynolds13d898c2017-02-02 12:22:05 -050043import android.content.pm.ShortcutInfo;
Jorim Jaggief72a192014-08-26 21:57:46 +020044import android.content.res.ColorStateList;
Anthony Chenad4d1582017-04-10 16:07:58 -070045import android.content.res.Configuration;
46import android.content.res.Resources;
Gus Prevas9cc96602018-10-11 11:24:23 -040047import android.content.res.TypedArray;
Joe Onoratoef1e7762010-09-17 18:38:38 -040048import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010049import android.graphics.Canvas;
Adrian Roosc1a80b02016-04-05 14:54:55 -070050import android.graphics.Color;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010051import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010052import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040053import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040054import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070055import android.media.AudioManager;
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -080056import android.media.PlayerBase;
Jeff Browndba34ba2014-06-24 20:46:03 -070057import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040059import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020060import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050061import android.os.Bundle;
Dianne Hackborn98305522017-05-05 17:53:53 -070062import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.os.Parcel;
64import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040065import android.os.SystemClock;
Selim Cinek6743c0b2017-01-18 18:24:01 -080066import android.os.SystemProperties;
Jeff Sharkey6d515712012-09-20 16:06:08 -070067import android.os.UserHandle;
Adrian Roosc1a80b02016-04-05 14:54:55 -070068import android.text.BidiFormatter;
Selim Cinek60a54252016-02-26 17:03:25 -080069import android.text.SpannableStringBuilder;
70import android.text.Spanned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.text.TextUtils;
Selim Cinek60a54252016-02-26 17:03:25 -080072import android.text.style.AbsoluteSizeSpan;
Selim Cinek89991a22016-03-07 19:51:54 -080073import android.text.style.CharacterStyle;
Selim Cinek981962e2016-07-20 20:41:58 -070074import android.text.style.ForegroundColorSpan;
Selim Cinek60a54252016-02-26 17:03:25 -080075import android.text.style.RelativeSizeSpan;
76import android.text.style.TextAppearanceSpan;
Svet Ganovddb94882016-06-23 19:55:24 -070077import android.util.ArraySet;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040078import android.util.Log;
Tony Mak638430e2018-10-08 19:19:10 +010079import android.util.Pair;
Dan Sandler50128532015-12-08 15:42:41 -050080import android.util.SparseArray;
Yi Jin6b514142017-10-30 14:54:12 -070081import android.util.proto.ProtoOutputStream;
Griff Hazen61a9e862014-05-22 16:05:19 -070082import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080083import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080084import android.view.View;
Selim Cinek954cc232016-05-20 13:29:23 -070085import android.view.ViewGroup;
Felipe Leme90205ef2019-03-05 09:59:52 -080086import android.view.contentcapture.ContentCaptureContext;
Jeff Sharkey1c400132011-08-05 14:50:13 -070087import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import android.widget.RemoteViews;
89
Griff Hazen959591e2014-05-15 22:26:18 -070090import com.android.internal.R;
Selim Cinek389edcd2017-05-11 19:16:44 -070091import com.android.internal.annotations.VisibleForTesting;
Svet Ganovddb94882016-06-23 19:55:24 -070092import com.android.internal.util.ArrayUtils;
Lucas Dupina291d192018-06-07 13:59:42 -070093import com.android.internal.util.ContrastColorUtil;
Griff Hazen959591e2014-05-15 22:26:18 -070094
Tor Norbyed9273d62013-05-30 15:59:53 -070095import java.lang.annotation.Retention;
96import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020097import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050098import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070099import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700100import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -0700101import java.util.List;
Julia Reynolds7217dc92018-03-07 12:12:09 -0500102import java.util.Objects;
Dan Sandler50128532015-12-08 15:42:41 -0500103import java.util.Set;
Jeff Sharkey23b31182018-04-18 21:32:12 -0600104import java.util.function.Consumer;
Joe Onorato561d3852010-11-20 18:09:34 -0800105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106/**
107 * A class that represents how a persistent notification is to be presented to
108 * the user using the {@link android.app.NotificationManager}.
109 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800110 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
111 * easier to construct Notifications.</p>
112 *
Joe Fernandez558459f2011-10-13 16:47:36 -0700113 * <div class="special reference">
114 * <h3>Developer Guides</h3>
115 * <p>For a guide to creating notifications, read the
116 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
117 * developer guide.</p>
118 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 */
120public class Notification implements Parcelable
121{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -0400122 private static final String TAG = "Notification";
123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -0400125 * An activity that provides a user interface for adjusting notification preferences for its
Julia Reynolds3aedded2017-03-31 14:42:09 -0400126 * containing application.
Daniel Sandler01df1c62014-06-09 10:54:01 -0400127 */
128 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
129 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
130 = "android.intent.category.NOTIFICATION_PREFERENCES";
131
132 /**
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500133 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
134 * contain a {@link NotificationChannel#getId() channel id} that can be used to narrow down
Julia Reynolds3aedded2017-03-31 14:42:09 -0400135 * what settings should be shown in the target app.
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500136 */
137 public static final String EXTRA_CHANNEL_ID = "android.intent.extra.CHANNEL_ID";
138
139 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -0400140 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds005c8b92017-08-24 10:35:53 -0400141 * contain a {@link NotificationChannelGroup#getId() group id} that can be used to narrow down
142 * what settings should be shown in the target app.
143 */
144 public static final String EXTRA_CHANNEL_GROUP_ID = "android.intent.extra.CHANNEL_GROUP_ID";
145
146 /**
147 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds3aedded2017-03-31 14:42:09 -0400148 * contain the tag provided to {@link NotificationManager#notify(String, int, Notification)}
149 * that can be used to narrow down what settings should be shown in the target app.
150 */
151 public static final String EXTRA_NOTIFICATION_TAG = "android.intent.extra.NOTIFICATION_TAG";
152
153 /**
154 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
155 * contain the id provided to {@link NotificationManager#notify(String, int, Notification)}
156 * that can be used to narrow down what settings should be shown in the target app.
157 */
158 public static final String EXTRA_NOTIFICATION_ID = "android.intent.extra.NOTIFICATION_ID";
159
160 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 * Use all default values (where applicable).
162 */
163 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 /**
166 * Use the default notification sound. This will ignore any given
167 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500168 *
Chris Wren47c20a12014-06-18 17:27:29 -0400169 * <p>
170 * A notification that is noisy is more likely to be presented as a heads-up notification.
171 * </p>
172 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500174 */
175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 public static final int DEFAULT_SOUND = 1;
177
178 /**
179 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500180 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700181 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500182 *
Chris Wren47c20a12014-06-18 17:27:29 -0400183 * <p>
184 * A notification that vibrates is more likely to be presented as a heads-up notification.
185 * </p>
186 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500188 */
189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 /**
193 * Use the default notification lights. This will ignore the
194 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
195 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500196 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500198 */
199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200203 * Maximum length of CharSequences accepted by Builder and friends.
204 *
205 * <p>
206 * Avoids spamming the system with overly large strings such as full e-mails.
207 */
208 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
209
210 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800211 * Maximum entries of reply text that are accepted by Builder and friends.
212 */
213 private static final int MAX_REPLY_HISTORY = 5;
214
Tony Mak09db2ea2018-06-27 18:12:48 +0100215 /**
Gustav Sennton1463d832018-11-06 16:12:48 +0000216 * Maximum number of (generic) action buttons in a notification (contextual action buttons are
217 * handled separately).
Tony Mak09db2ea2018-06-27 18:12:48 +0100218 * @hide
219 */
220 public static final int MAX_ACTION_BUTTONS = 3;
Selim Cinekde4de0e2018-01-24 16:21:07 -0800221
222 /**
223 * If the notification contained an unsent draft for a RemoteInput when the user clicked on it,
224 * we're adding the draft as a String extra to the {@link #contentIntent} using this key.
225 *
226 * <p>Apps may use this extra to prepopulate text fields in the app, where the user usually
227 * sends messages.</p>
228 */
229 public static final String EXTRA_REMOTE_INPUT_DRAFT = "android.remoteInputDraft";
230
Adrian Roose458aa82015-12-08 16:17:19 -0800231 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500232 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800233 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500234 * Default value: {@link System#currentTimeMillis() Now}.
235 *
236 * Choose a timestamp that will be most relevant to the user. For most finite events, this
237 * corresponds to the time the event happened (or will happen, in the case of events that have
238 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800239 * timestamped according to when the activity began.
240 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500241 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800242 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500243 * <ul>
244 * <li>Notification of a new chat message should be stamped when the message was received.</li>
245 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
246 * <li>Notification of a completed file download should be stamped when the download finished.</li>
247 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
248 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
249 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800250 * </ul>
251 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700252 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
253 * anymore by default and must be opted into by using
254 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 */
256 public long when;
257
258 /**
Selim Cinekb85f36fd2016-04-20 18:46:36 -0700259 * The creation time of the notification
260 */
261 private long creationTime;
262
263 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400265 *
266 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 */
Dan Sandler86647982015-05-13 23:41:13 -0400268 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700269 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 public int icon;
271
272 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800273 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
274 * leave it at its default value of 0.
275 *
276 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700277 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800278 */
279 public int iconLevel;
280
281 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500282 * The number of events that this notification represents. For example, in a new mail
283 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800284 *
Julia Reynolds30331982017-04-27 10:12:50 -0400285 * The system may or may not use this field to modify the appearance of the notification.
Julia Reynolds13d898c2017-02-02 12:22:05 -0500286 * Starting with {@link android.os.Build.VERSION_CODES#O}, the number may be displayed as a
287 * badge icon in Launchers that support badging.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 */
Julia Reynolds30331982017-04-27 10:12:50 -0400289 public int number = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290
291 /**
292 * The intent to execute when the expanded status entry is clicked. If
293 * this is an activity, it must include the
294 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800295 * that you take care of task management as described in the
296 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800297 * Stack</a> document. In particular, make sure to read the notification section
298 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
299 * Notifications</a> for the correct ways to launch an application from a
300 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 */
302 public PendingIntent contentIntent;
303
304 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500305 * The intent to execute when the notification is explicitly dismissed by the user, either with
306 * the "Clear All" button or by swiping it away individually.
307 *
308 * This probably shouldn't be launching an activity since several of those will be sent
309 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 */
311 public PendingIntent deleteIntent;
312
313 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700314 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800315 *
Chris Wren47c20a12014-06-18 17:27:29 -0400316 * <p>
317 * The system UI may choose to display a heads-up notification, instead of
318 * launching this intent, while the user is using the device.
319 * </p>
320 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800321 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400322 */
323 public PendingIntent fullScreenIntent;
324
325 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400326 * Text that summarizes this notification for accessibility services.
327 *
328 * As of the L release, this text is no longer shown on screen, but it is still useful to
329 * accessibility services (where it serves as an audible announcement of the notification's
330 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400331 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800332 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 */
334 public CharSequence tickerText;
335
336 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400337 * Formerly, a view showing the {@link #tickerText}.
338 *
339 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400340 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400341 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800342 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400343
344 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400345 * The view that will represent this notification in the notification list (which is pulled
346 * down from the status bar).
347 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500348 * As of N, this field may be null. The notification view is determined by the inputs
349 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400350 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400352 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 public RemoteViews contentView;
354
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400355 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400356 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400357 * opportunity to show more detail. The system UI may choose to show this
358 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400359 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500360 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400361 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
362 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400363 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400364 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400365 public RemoteViews bigContentView;
366
Chris Wren8fd39ec2014-02-27 17:43:26 -0500367
368 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400369 * A medium-format version of {@link #contentView}, providing the Notification an
370 * opportunity to add action buttons to contentView. At its discretion, the system UI may
371 * choose to show this as a heads-up notification, which will pop up so the user can see
372 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400373 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500374 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400375 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
376 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500377 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400378 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500379 public RemoteViews headsUpContentView;
380
Julia Reynoldsfc640012018-02-21 12:25:27 -0500381 private boolean mUsesStandardHeader;
382
383 private static final ArraySet<Integer> STANDARD_LAYOUTS = new ArraySet<>();
384 static {
385 STANDARD_LAYOUTS.add(R.layout.notification_template_material_base);
386 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_base);
387 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_picture);
388 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_text);
389 STANDARD_LAYOUTS.add(R.layout.notification_template_material_inbox);
390 STANDARD_LAYOUTS.add(R.layout.notification_template_material_messaging);
391 STANDARD_LAYOUTS.add(R.layout.notification_template_material_media);
392 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_media);
Julia Reynoldsfc640012018-02-21 12:25:27 -0500393 STANDARD_LAYOUTS.add(R.layout.notification_template_header);
Julia Reynoldsfc640012018-02-21 12:25:27 -0500394 }
395
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400396 /**
Dan Sandler86647982015-05-13 23:41:13 -0400397 * A large bitmap to be shown in the notification content area.
398 *
399 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 */
Dan Sandler86647982015-05-13 23:41:13 -0400401 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800402 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403
404 /**
405 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500406 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400408 * A notification that is noisy is more likely to be presented as a heads-up notification.
409 * </p>
410 *
411 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500412 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500414 * @deprecated use {@link NotificationChannel#getSound()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500416 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 public Uri sound;
418
419 /**
420 * Use this constant as the value for audioStreamType to request that
421 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700422 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400423 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500424 * @deprecated Use {@link NotificationChannel#getAudioAttributes()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700426 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 public static final int STREAM_DEFAULT = -1;
428
429 /**
430 * The audio stream type to use when playing the sound.
431 * Should be one of the STREAM_ constants from
432 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400433 *
434 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700436 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 public int audioStreamType = STREAM_DEFAULT;
438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400440 * The default value of {@link #audioAttributes}.
441 */
442 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
443 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
444 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
445 .build();
446
447 /**
448 * The {@link AudioAttributes audio attributes} to use when playing the sound.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500449 *
450 * @deprecated use {@link NotificationChannel#getAudioAttributes()} instead.
John Spurlockc0650f022014-07-19 13:22:39 -0400451 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500452 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -0400453 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
454
455 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500456 * The pattern with which to vibrate.
457 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 * <p>
459 * To vibrate the default pattern, see {@link #defaults}.
460 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500461 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 * @see android.os.Vibrator#vibrate(long[],int)
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500463 * @deprecated use {@link NotificationChannel#getVibrationPattern()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500465 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 public long[] vibrate;
467
468 /**
469 * The color of the led. The hardware will do its best approximation.
470 *
471 * @see #FLAG_SHOW_LIGHTS
472 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500473 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 */
Tor Norbye80756e32015-03-02 09:39:27 -0800475 @ColorInt
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500476 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 public int ledARGB;
478
479 /**
480 * The number of milliseconds for the LED to be on while it's flashing.
481 * The hardware will do its best approximation.
482 *
483 * @see #FLAG_SHOW_LIGHTS
484 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500485 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500487 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 public int ledOnMS;
489
490 /**
491 * The number of milliseconds for the LED to be off while it's flashing.
492 * The hardware will do its best approximation.
493 *
494 * @see #FLAG_SHOW_LIGHTS
495 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500496 *
497 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500499 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 public int ledOffMS;
501
502 /**
503 * Specifies which values should be taken from the defaults.
504 * <p>
505 * To set, OR the desired from {@link #DEFAULT_SOUND},
506 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
507 * values, use {@link #DEFAULT_ALL}.
508 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500509 *
510 * @deprecated use {@link NotificationChannel#getSound()} and
511 * {@link NotificationChannel#shouldShowLights()} and
512 * {@link NotificationChannel#shouldVibrate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500514 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 public int defaults;
516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 /**
518 * Bit to be bitwise-ored into the {@link #flags} field that should be
519 * set if you want the LED on for this notification.
520 * <ul>
521 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
522 * or 0 for both ledOnMS and ledOffMS.</li>
523 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
524 * <li>To flash the LED, pass the number of milliseconds that it should
525 * be on and off to ledOnMS and ledOffMS.</li>
526 * </ul>
527 * <p>
528 * Since hardware varies, you are not guaranteed that any of the values
Ricardo Loo Forondaf8f6d0a2018-01-25 08:42:43 -0800529 * you pass are honored exactly. Use the system defaults if possible
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 * because they will be set to values that work on any given hardware.
531 * <p>
532 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500533 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500534 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500536 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
538
539 /**
540 * Bit to be bitwise-ored into the {@link #flags} field that should be
541 * set if this notification is in reference to something that is ongoing,
542 * like a phone call. It should not be set if this notification is in
543 * reference to something that happened at a particular point in time,
544 * like a missed phone call.
545 */
546 public static final int FLAG_ONGOING_EVENT = 0x00000002;
547
548 /**
549 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700550 * the audio will be repeated until the notification is
551 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 */
553 public static final int FLAG_INSISTENT = 0x00000004;
554
555 /**
556 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700557 * set if you would only like the sound, vibrate and ticker to be played
558 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 */
560 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
561
562 /**
563 * Bit to be bitwise-ored into the {@link #flags} field that should be
564 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500565 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 */
567 public static final int FLAG_AUTO_CANCEL = 0x00000010;
568
569 /**
570 * Bit to be bitwise-ored into the {@link #flags} field that should be
571 * set if the notification should not be canceled when the user clicks
572 * the Clear all button.
573 */
574 public static final int FLAG_NO_CLEAR = 0x00000020;
575
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700576 /**
577 * Bit to be bitwise-ored into the {@link #flags} field that should be
578 * set if this notification represents a currently running service. This
579 * will normally be set for you by {@link Service#startForeground}.
580 */
581 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
582
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400583 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500584 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800585 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500586 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400587 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700588 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500589 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400590
Griff Hazendfcb0802014-02-11 12:00:00 -0800591 /**
592 * Bit to be bitswise-ored into the {@link #flags} field that should be
593 * set if this notification is relevant to the current device only
594 * and it is not recommended that it bridge to other devices.
595 */
596 public static final int FLAG_LOCAL_ONLY = 0x00000100;
597
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700598 /**
599 * Bit to be bitswise-ored into the {@link #flags} field that should be
600 * set if this notification is the group summary for a group of notifications.
601 * Grouped notifications may display in a cluster or stack on devices which
602 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
603 */
604 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
605
Julia Reynoldse46bb372016-03-17 11:05:58 -0400606 /**
607 * Bit to be bitswise-ored into the {@link #flags} field that should be
608 * set if this notification is the group summary for an auto-group of notifications.
609 *
610 * @hide
611 */
612 @SystemApi
613 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
614
Julia Reynolds4db59552017-06-30 13:34:01 -0400615 /**
616 * @hide
617 */
618 public static final int FLAG_CAN_COLORIZE = 0x00000800;
619
Mady Mellor7eb18ef2019-03-27 14:03:46 -0700620 /**
Julia Tuttle28fb1a92019-05-03 18:07:45 -0400621 * Bit to be bitswised-ored into the {@link #flags} field that should be
622 * set by the system if this notification is showing as a bubble.
Mady Mellor65dcaa92019-04-03 12:21:44 -0700623 *
Julia Tuttle28fb1a92019-05-03 18:07:45 -0400624 * Applications cannot set this flag directly; they should instead call
625 * {@link Notification.Builder#setBubbleMetadata(BubbleMetadata)} to
626 * request that a notification be displayed as a bubble, and then check
627 * this flag to see whether that request was honored by the system.
Mady Mellor7eb18ef2019-03-27 14:03:46 -0700628 */
629 public static final int FLAG_BUBBLE = 0x00001000;
630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 public int flags;
632
Tor Norbyed9273d62013-05-30 15:59:53 -0700633 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700634 @IntDef(prefix = { "PRIORITY_" }, value = {
635 PRIORITY_DEFAULT,
636 PRIORITY_LOW,
637 PRIORITY_MIN,
638 PRIORITY_HIGH,
639 PRIORITY_MAX
640 })
Tor Norbyed9273d62013-05-30 15:59:53 -0700641 @Retention(RetentionPolicy.SOURCE)
642 public @interface Priority {}
643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500645 * Default notification {@link #priority}. If your application does not prioritize its own
646 * notifications, use this value for all notifications.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500647 *
648 * @deprecated use {@link NotificationManager#IMPORTANCE_DEFAULT} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500649 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500650 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500651 public static final int PRIORITY_DEFAULT = 0;
652
653 /**
654 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
655 * items smaller, or at a different position in the list, compared with your app's
656 * {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500657 *
658 * @deprecated use {@link NotificationManager#IMPORTANCE_LOW} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500659 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500660 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500661 public static final int PRIORITY_LOW = -1;
662
663 /**
664 * Lowest {@link #priority}; these items might not be shown to the user except under special
665 * circumstances, such as detailed notification logs.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500666 *
667 * @deprecated use {@link NotificationManager#IMPORTANCE_MIN} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500668 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500669 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500670 public static final int PRIORITY_MIN = -2;
671
672 /**
673 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
674 * show these items larger, or at a different position in notification lists, compared with
675 * your app's {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500676 *
677 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500678 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500679 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500680 public static final int PRIORITY_HIGH = 1;
681
682 /**
683 * Highest {@link #priority}, for your application's most important items that require the
684 * user's prompt attention or input.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500685 *
686 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500687 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500688 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500689 public static final int PRIORITY_MAX = 2;
690
691 /**
692 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800693 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500694 * Priority is an indication of how much of the user's valuable attention should be consumed by
695 * this notification. Low-priority notifications may be hidden from the user in certain
696 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500697 * system will make a determination about how to interpret this priority when presenting
698 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400699 *
700 * <p>
701 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
702 * as a heads-up notification.
703 * </p>
704 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500705 * @deprecated use {@link NotificationChannel#getImportance()} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500706 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700707 @Priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500708 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500709 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800710
Dan Sandler26e81cf2014-05-06 10:01:27 -0400711 /**
712 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
713 * to be applied by the standard Style templates when presenting this notification.
714 *
715 * The current template design constructs a colorful header image by overlaying the
716 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
717 * ignored.
718 */
Tor Norbye80756e32015-03-02 09:39:27 -0800719 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400720 public int color = COLOR_DEFAULT;
721
722 /**
723 * Special value of {@link #color} telling the system not to decorate this notification with
724 * any special color but instead use default colors when presenting this notification.
725 */
Tor Norbye80756e32015-03-02 09:39:27 -0800726 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400727 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600728
729 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800730 * Special value of {@link #color} used as a place holder for an invalid color.
Selim Cinek99104832017-01-25 14:47:33 -0800731 * @hide
Adrian Roos4ff3b122016-02-01 12:26:13 -0800732 */
733 @ColorInt
Selim Cinek99104832017-01-25 14:47:33 -0800734 public static final int COLOR_INVALID = 1;
Adrian Roos4ff3b122016-02-01 12:26:13 -0800735
736 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700737 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
738 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600739 * lockscreen).
740 *
741 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
742 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
743 * shown in all situations, but the contents are only available if the device is unlocked for
744 * the appropriate user.
745 *
746 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
747 * can be read even in an "insecure" context (that is, above a secure lockscreen).
748 * To modify the public version of this notification—for example, to redact some portions—see
749 * {@link Builder#setPublicVersion(Notification)}.
750 *
751 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
752 * and ticker until the user has bypassed the lockscreen.
753 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600754 public @Visibility int visibility;
755
756 /** @hide */
757 @IntDef(prefix = { "VISIBILITY_" }, value = {
758 VISIBILITY_PUBLIC,
759 VISIBILITY_PRIVATE,
760 VISIBILITY_SECRET,
761 })
762 @Retention(RetentionPolicy.SOURCE)
763 public @interface Visibility {}
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600764
Griff Hazenfc3922d2014-08-20 11:56:44 -0700765 /**
766 * Notification visibility: Show this notification in its entirety on all lockscreens.
767 *
768 * {@see #visibility}
769 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600770 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700771
772 /**
773 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
774 * private information on secure lockscreens.
775 *
776 * {@see #visibility}
777 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600778 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700779
780 /**
781 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
782 *
783 * {@see #visibility}
784 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600785 public static final int VISIBILITY_SECRET = -1;
786
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500787 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400788 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500789 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400790 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500791
792 /**
Adora Zhangaa90e872018-03-12 14:07:50 -0700793 * Notification category: map turn-by-turn navigation.
794 */
795 public static final String CATEGORY_NAVIGATION = "navigation";
796
797 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400798 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500799 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400800 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500801
802 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400803 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500804 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400805 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500806
807 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400808 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500809 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400810 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500811
812 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400813 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500814 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400815 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500816
817 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400818 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500819 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400820 public static final String CATEGORY_ALARM = "alarm";
821
822 /**
823 * Notification category: progress of a long-running background operation.
824 */
825 public static final String CATEGORY_PROGRESS = "progress";
826
827 /**
828 * Notification category: social network or sharing update.
829 */
830 public static final String CATEGORY_SOCIAL = "social";
831
832 /**
833 * Notification category: error in background operation or authentication status.
834 */
835 public static final String CATEGORY_ERROR = "err";
836
837 /**
838 * Notification category: media transport control for playback.
839 */
840 public static final String CATEGORY_TRANSPORT = "transport";
841
842 /**
843 * Notification category: system or device status update. Reserved for system use.
844 */
845 public static final String CATEGORY_SYSTEM = "sys";
846
847 /**
848 * Notification category: indication of running background service.
849 */
850 public static final String CATEGORY_SERVICE = "service";
851
852 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400853 * Notification category: a specific, timely recommendation for a single thing.
854 * For example, a news app might want to recommend a news story it believes the user will
855 * want to read next.
856 */
857 public static final String CATEGORY_RECOMMENDATION = "recommendation";
858
859 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400860 * Notification category: ongoing information about device or contextual status.
861 */
862 public static final String CATEGORY_STATUS = "status";
863
864 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400865 * Notification category: user-scheduled reminder.
866 */
867 public static final String CATEGORY_REMINDER = "reminder";
868
869 /**
Adora Zhangaa90e872018-03-12 14:07:50 -0700870 * Notification category: extreme car emergencies.
871 * @hide
872 */
873 @SystemApi
874 public static final String CATEGORY_CAR_EMERGENCY = "car_emergency";
875
876 /**
877 * Notification category: car warnings.
878 * @hide
879 */
880 @SystemApi
881 public static final String CATEGORY_CAR_WARNING = "car_warning";
882
883 /**
884 * Notification category: general car system information.
885 * @hide
886 */
887 @SystemApi
888 public static final String CATEGORY_CAR_INFORMATION = "car_information";
889
890 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400891 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
892 * that best describes this Notification. May be used by the system for ranking and filtering.
893 */
894 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500895
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100896 @UnsupportedAppUsage
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700897 private String mGroupKey;
898
899 /**
900 * Get the key used to group this notification into a cluster or stack
901 * with other notifications on devices which support such rendering.
902 */
903 public String getGroup() {
904 return mGroupKey;
905 }
906
907 private String mSortKey;
908
909 /**
910 * Get a sort key that orders this notification among other notifications from the
911 * same package. This can be useful if an external sort was already applied and an app
912 * would like to preserve this. Notifications will be sorted lexicographically using this
913 * value, although providing different priorities in addition to providing sort key may
914 * cause this value to be ignored.
915 *
916 * <p>This sort key can also be used to order members of a notification group. See
917 * {@link Builder#setGroup}.
918 *
919 * @see String#compareTo(String)
920 */
921 public String getSortKey() {
922 return mSortKey;
923 }
924
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500925 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400926 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400927 * <p>
928 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
929 * APIs, and are intended to be used by
930 * {@link android.service.notification.NotificationListenerService} implementations to extract
931 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500932 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400933 public Bundle extras = new Bundle();
934
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400935 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700936 * All pending intents in the notification as the system needs to be able to access them but
937 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700938 * custom parcelable objects.
939 *
940 * @hide
941 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100942 @UnsupportedAppUsage
Felipe Lemedd85da62016-06-28 11:29:54 -0700943 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700944
945 /**
Dianne Hackborn98305522017-05-05 17:53:53 -0700946 * Token identifying the notification that is applying doze/bgcheck whitelisting to the
947 * pending intents inside of it, so only those will get the behavior.
948 *
949 * @hide
950 */
Adrian Roosfb921842017-10-26 14:49:56 +0200951 private IBinder mWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -0700952
953 /**
954 * Must be set by a process to start associating tokens with Notification objects
955 * coming in to it. This is set by NotificationManagerService.
956 *
957 * @hide
958 */
959 static public IBinder processWhitelistToken;
960
961 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400962 * {@link #extras} key: this is the title of the notification,
963 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
964 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500965 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400966
967 /**
968 * {@link #extras} key: this is the title of the notification when shown in expanded form,
969 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
970 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400971 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400972
973 /**
974 * {@link #extras} key: this is the main text payload, as supplied to
975 * {@link Builder#setContentText(CharSequence)}.
976 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500977 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400978
979 /**
980 * {@link #extras} key: this is a third line of text, as supplied to
981 * {@link Builder#setSubText(CharSequence)}.
982 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400983 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400984
985 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800986 * {@link #extras} key: this is the remote input history, as supplied to
987 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700988 *
989 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
990 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
991 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
992 * notifications once the other party has responded).
993 *
994 * The extra with this key is of type CharSequence[] and contains the most recent entry at
995 * the 0 index, the second most recent at the 1 index, etc.
996 *
997 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800998 */
999 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
1000
1001 /**
Kenny Guya0f6de82018-04-06 16:20:16 +01001002 * {@link #extras} key: boolean as supplied to
1003 * {@link Builder#setShowRemoteInputSpinner(boolean)}.
1004 *
1005 * If set to true, then the view displaying the remote input history from
1006 * {@link Builder#setRemoteInputHistory(CharSequence[])} will have a progress spinner.
1007 *
1008 * @see Builder#setShowRemoteInputSpinner(boolean)
1009 * @hide
1010 */
1011 public static final String EXTRA_SHOW_REMOTE_INPUT_SPINNER = "android.remoteInputSpinner";
1012
1013 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +01001014 * {@link #extras} key: boolean as supplied to
1015 * {@link Builder#setHideSmartReplies(boolean)}.
1016 *
1017 * If set to true, then any smart reply buttons will be hidden.
1018 *
1019 * @see Builder#setHideSmartReplies(boolean)
1020 * @hide
1021 */
1022 public static final String EXTRA_HIDE_SMART_REPLIES = "android.hideSmartReplies";
1023
1024 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001025 * {@link #extras} key: this is a small piece of additional text as supplied to
1026 * {@link Builder#setContentInfo(CharSequence)}.
1027 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001028 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001029
1030 /**
1031 * {@link #extras} key: this is a line of summary information intended to be shown
1032 * alongside expanded notifications, as supplied to (e.g.)
1033 * {@link BigTextStyle#setSummaryText(CharSequence)}.
1034 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001035 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001036
1037 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02001038 * {@link #extras} key: this is the longer text shown in the big form of a
1039 * {@link BigTextStyle} notification, as supplied to
1040 * {@link BigTextStyle#bigText(CharSequence)}.
1041 */
1042 public static final String EXTRA_BIG_TEXT = "android.bigText";
1043
1044 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001045 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
1046 * supplied to {@link Builder#setSmallIcon(int)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -04001047 *
1048 * @deprecated Use {@link #getSmallIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001049 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04001050 @Deprecated
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001051 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001052
1053 /**
1054 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
1055 * notification payload, as
1056 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -04001057 *
1058 * @deprecated Use {@link #getLargeIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001059 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04001060 @Deprecated
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001061 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001062
1063 /**
1064 * {@link #extras} key: this is a bitmap to be used instead of the one from
1065 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
1066 * shown in its expanded form, as supplied to
1067 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
1068 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001069 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001070
1071 /**
1072 * {@link #extras} key: this is the progress value supplied to
1073 * {@link Builder#setProgress(int, int, boolean)}.
1074 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001075 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001076
1077 /**
1078 * {@link #extras} key: this is the maximum value supplied to
1079 * {@link Builder#setProgress(int, int, boolean)}.
1080 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001081 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001082
1083 /**
1084 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
1085 * {@link Builder#setProgress(int, int, boolean)}.
1086 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001087 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001088
1089 /**
1090 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
1091 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
1092 * {@link Builder#setUsesChronometer(boolean)}.
1093 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001094 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001095
1096 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08001097 * {@link #extras} key: whether the chronometer set on the notification should count down
1098 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -07001099 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -08001100 */
Adrian Roos96b7e202016-05-17 13:50:38 -07001101 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -08001102
1103 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001104 * {@link #extras} key: whether {@link #when} should be shown,
1105 * as supplied to {@link Builder#setShowWhen(boolean)}.
1106 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001107 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001108
1109 /**
1110 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
1111 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
1112 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001113 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001114
1115 /**
1116 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
1117 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
1118 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001119 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -04001120
1121 /**
1122 * {@link #extras} key: A string representing the name of the specific
1123 * {@link android.app.Notification.Style} used to create this notification.
1124 */
Chris Wren91ad5632013-06-05 15:05:57 -04001125 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001126
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001127 /**
Chris Wrene6c48932014-09-29 17:19:27 -04001128 * {@link #extras} key: A String array containing the people that this notification relates to,
1129 * each of which was supplied to {@link Builder#addPerson(String)}.
Selim Cineke7238dd2017-12-14 17:48:32 -08001130 *
1131 * @deprecated the actual objects are now in {@link #EXTRA_PEOPLE_LIST}
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001132 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001133 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001134
1135 /**
Selim Cineke7238dd2017-12-14 17:48:32 -08001136 * {@link #extras} key: An arrayList of {@link Person} objects containing the people that
1137 * this notification relates to.
1138 */
1139 public static final String EXTRA_PEOPLE_LIST = "android.people.list";
1140
1141 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04001142 * Allow certain system-generated notifications to appear before the device is provisioned.
1143 * Only available to notifications coming from the android package.
1144 * @hide
1145 */
Maurice Lam96c10032017-03-29 15:42:38 -07001146 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001147 @RequiresPermission(android.Manifest.permission.NOTIFICATION_DURING_SETUP)
John Spurlockfd7f1e02014-03-18 16:41:57 -04001148 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
1149
1150 /**
Robin Leed107af62018-04-27 13:55:56 +02001151 * {@link #extras} key:
1152 * flat {@link String} representation of a {@link android.content.ContentUris content URI}
1153 * pointing to an image that can be displayed in the background when the notification is
1154 * selected. Used on television platforms. The URI must point to an image stream suitable for
1155 * passing into {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001156 * BitmapFactory.decodeStream}; all other content types will be ignored.
Jose Limae9e3b3b2014-05-18 23:44:50 -07001157 */
1158 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
1159
1160 /**
Dan Sandler842dd772014-05-15 09:36:47 -04001161 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -07001162 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -04001163 * {@link android.app.Notification.MediaStyle} notification.
1164 */
1165 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
1166
1167 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +01001168 * {@link #extras} key: the indices of actions to be shown in the compact view,
1169 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
1170 */
1171 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
1172
Christoph Studer943aa672014-08-03 20:31:16 +02001173 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04001174 * {@link #extras} key: the username to be displayed for all messages sent by the user including
1175 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -07001176 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1177 * {@link CharSequence}
Selim Cinekcb8b9852017-12-15 18:01:52 -08001178 *
1179 * @deprecated use {@link #EXTRA_MESSAGING_PERSON}
Alex Hillsfc737de2016-03-23 17:33:02 -04001180 */
1181 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
1182
1183 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08001184 * {@link #extras} key: the person to be displayed for all messages sent by the user including
1185 * direct replies
1186 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1187 * {@link Person}
1188 */
1189 public static final String EXTRA_MESSAGING_PERSON = "android.messagingUser";
1190
1191 /**
Adrian Roos96b7e202016-05-17 13:50:38 -07001192 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -04001193 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -04001194 */
Alex Hillsd9b04d92016-04-11 16:38:16 -04001195 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -04001196
1197 /**
1198 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
1199 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -07001200 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1201 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -04001202 */
1203 public static final String EXTRA_MESSAGES = "android.messages";
1204
1205 /**
Adrian Roos437cd562017-01-18 15:47:03 -08001206 * {@link #extras} key: an array of
1207 * {@link android.app.Notification.MessagingStyle#addHistoricMessage historic}
1208 * {@link android.app.Notification.MessagingStyle.Message} bundles provided by a
1209 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1210 * array of bundles.
1211 */
1212 public static final String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
1213
1214 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08001215 * {@link #extras} key: whether the {@link android.app.Notification.MessagingStyle} notification
1216 * represents a group conversation.
1217 */
1218 public static final String EXTRA_IS_GROUP_CONVERSATION = "android.isGroupConversation";
1219
1220 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08001221 * {@link #extras} key: whether the notification should be colorized as
Gustav Senntondf4c2442019-03-18 11:41:11 +00001222 * supplied to {@link Builder#setColorized(boolean)}.
Selim Cinek7b9605b2017-01-19 17:36:00 -08001223 */
1224 public static final String EXTRA_COLORIZED = "android.colorized";
1225
1226 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001227 * @hide
1228 */
1229 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
1230
Selim Cinek247fa012016-02-18 09:50:48 -08001231 /**
1232 * @hide
1233 */
1234 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
1235
Shane Brennan472a3b32016-12-12 15:28:10 -08001236 /**
Selim Cinekd0426622017-07-11 13:19:59 +02001237 * @hide
1238 */
1239 public static final String EXTRA_REDUCED_IMAGES = "android.reduced.images";
1240
1241 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001242 * {@link #extras} key: the audio contents of this notification.
1243 *
1244 * This is for use when rendering the notification on an audio-focused interface;
1245 * the audio contents are a complete sound sample that contains the contents/body of the
1246 * notification. This may be used in substitute of a Text-to-Speech reading of the
1247 * notification. For example if the notification represents a voice message this should point
1248 * to the audio of that message.
1249 *
1250 * The data stored under this key should be a String representation of a Uri that contains the
1251 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1252 *
1253 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1254 * has a field for holding data URI. That field can be used for audio.
1255 * See {@code Message#setData}.
1256 *
1257 * Example usage:
1258 * <pre>
1259 * {@code
1260 * Notification.Builder myBuilder = (build your Notification as normal);
1261 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1262 * }
1263 * </pre>
1264 */
1265 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1266
Dan Sandler80eaa592016-04-14 23:34:54 -04001267 /** @hide */
1268 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001269 @RequiresPermission(android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME)
Dan Sandler732bd6c2016-04-12 14:20:32 -04001270 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1271
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001272 /**
Julia Reynolds3c7de112018-03-28 09:33:13 -04001273 * This is set on the notifications shown by system_server about apps running foreground
1274 * services. It indicates that the notification should be shown
1275 * only if any of the given apps do not already have a properly tagged
1276 * {@link #FLAG_FOREGROUND_SERVICE} notification currently visible to the user.
1277 * This is a string array of all package names of the apps.
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001278 * @hide
1279 */
1280 public static final String EXTRA_FOREGROUND_APPS = "android.foregroundApps";
1281
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001282 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04001283 private Icon mSmallIcon;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001284 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04001285 private Icon mLargeIcon;
1286
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001287 @UnsupportedAppUsage
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001288 private String mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001289 private long mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001290
Julia Reynolds13d898c2017-02-02 12:22:05 -05001291 private String mShortcutId;
Felipe Leme90205ef2019-03-05 09:59:52 -08001292 private LocusId mLocusId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04001293 private CharSequence mSettingsText;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001294
Mady Mellorc39b4ae2019-01-09 17:11:37 -08001295 private BubbleMetadata mBubbleMetadata;
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04001296
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001297 /** @hide */
1298 @IntDef(prefix = { "GROUP_ALERT_" }, value = {
1299 GROUP_ALERT_ALL, GROUP_ALERT_CHILDREN, GROUP_ALERT_SUMMARY
1300 })
1301 @Retention(RetentionPolicy.SOURCE)
1302 public @interface GroupAlertBehavior {}
1303
1304 /**
1305 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all notifications in a
1306 * group with sound or vibration ought to make sound or vibrate (respectively), so this
1307 * notification will not be muted when it is in a group.
1308 */
1309 public static final int GROUP_ALERT_ALL = 0;
1310
1311 /**
1312 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all children
1313 * notification in a group should be silenced (no sound or vibration) even if they are posted
1314 * to a {@link NotificationChannel} that has sound and/or vibration. Use this constant to
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001315 * mute this notification if this notification is a group child. This must be applied to all
1316 * children notifications you want to mute.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001317 *
1318 * <p> For example, you might want to use this constant if you post a number of children
1319 * notifications at once (say, after a periodic sync), and only need to notify the user
1320 * audibly once.
1321 */
1322 public static final int GROUP_ALERT_SUMMARY = 1;
1323
1324 /**
1325 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that the summary
1326 * notification in a group should be silenced (no sound or vibration) even if they are
1327 * posted to a {@link NotificationChannel} that has sound and/or vibration. Use this constant
1328 * to mute this notification if this notification is a group summary.
1329 *
1330 * <p>For example, you might want to use this constant if only the children notifications
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001331 * in your group have content and the summary is only used to visually group notifications
1332 * rather than to alert the user that new information is available.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001333 */
1334 public static final int GROUP_ALERT_CHILDREN = 2;
1335
Julia Reynolds2f431e22017-06-07 14:12:09 +00001336 private int mGroupAlertBehavior = GROUP_ALERT_ALL;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001337
Julia Reynolds13d898c2017-02-02 12:22:05 -05001338 /**
1339 * If this notification is being shown as a badge, always show as a number.
1340 */
1341 public static final int BADGE_ICON_NONE = 0;
1342
1343 /**
1344 * If this notification is being shown as a badge, use the {@link #getSmallIcon()} to
1345 * represent this notification.
1346 */
1347 public static final int BADGE_ICON_SMALL = 1;
1348
1349 /**
1350 * If this notification is being shown as a badge, use the {@link #getLargeIcon()} to
1351 * represent this notification.
1352 */
1353 public static final int BADGE_ICON_LARGE = 2;
Julia Reynolds7d5b4da2017-03-20 10:18:49 -04001354 private int mBadgeIcon = BADGE_ICON_NONE;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001355
Chris Wren51c75102013-07-16 20:49:17 -04001356 /**
Gustav Sennton761884c2018-11-19 17:40:19 +00001357 * Determines whether the platform can generate contextual actions for a notification.
1358 */
1359 private boolean mAllowSystemGeneratedContextualActions = true;
1360
1361 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001362 * Structure to encapsulate a named action that can be shown as part of this notification.
1363 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1364 * selected by the user.
1365 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001366 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1367 * or {@link Notification.Builder#addAction(Notification.Action)}
1368 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001369 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001370 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001371 /**
1372 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1373 * {@link RemoteInput}s.
1374 *
1375 * This is intended for {@link RemoteInput}s that only accept data, meaning
1376 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
Gustav Senntondf4c2442019-03-18 11:41:11 +00001377 * is null or empty, and {@link RemoteInput#getAllowedDataTypes} is non-null and not
Shane Brennan472a3b32016-12-12 15:28:10 -08001378 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1379 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1380 *
1381 * You can test if a RemoteInput matches these constraints using
1382 * {@link RemoteInput#isDataOnly}.
1383 */
1384 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1385
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001386 /**
1387 * {@link }: No semantic action defined.
1388 */
1389 public static final int SEMANTIC_ACTION_NONE = 0;
1390
1391 /**
1392 * {@code SemanticAction}: Reply to a conversation, chat, group, or wherever replies
1393 * may be appropriate.
1394 */
1395 public static final int SEMANTIC_ACTION_REPLY = 1;
1396
1397 /**
1398 * {@code SemanticAction}: Mark content as read.
1399 */
1400 public static final int SEMANTIC_ACTION_MARK_AS_READ = 2;
1401
1402 /**
1403 * {@code SemanticAction}: Mark content as unread.
1404 */
1405 public static final int SEMANTIC_ACTION_MARK_AS_UNREAD = 3;
1406
1407 /**
1408 * {@code SemanticAction}: Delete the content associated with the notification. This
1409 * could mean deleting an email, message, etc.
1410 */
1411 public static final int SEMANTIC_ACTION_DELETE = 4;
1412
1413 /**
1414 * {@code SemanticAction}: Archive the content associated with the notification. This
1415 * could mean archiving an email, message, etc.
1416 */
1417 public static final int SEMANTIC_ACTION_ARCHIVE = 5;
1418
1419 /**
1420 * {@code SemanticAction}: Mute the content associated with the notification. This could
1421 * mean silencing a conversation or currently playing media.
1422 */
1423 public static final int SEMANTIC_ACTION_MUTE = 6;
1424
1425 /**
1426 * {@code SemanticAction}: Unmute the content associated with the notification. This could
1427 * mean un-silencing a conversation or currently playing media.
1428 */
1429 public static final int SEMANTIC_ACTION_UNMUTE = 7;
1430
1431 /**
1432 * {@code SemanticAction}: Mark content with a thumbs up.
1433 */
1434 public static final int SEMANTIC_ACTION_THUMBS_UP = 8;
1435
1436 /**
1437 * {@code SemanticAction}: Mark content with a thumbs down.
1438 */
1439 public static final int SEMANTIC_ACTION_THUMBS_DOWN = 9;
1440
Kodlee Yincda5b092018-02-15 15:34:53 -08001441 /**
1442 * {@code SemanticAction}: Call a contact, group, etc.
1443 */
1444 public static final int SEMANTIC_ACTION_CALL = 10;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001445
Griff Hazen959591e2014-05-15 22:26:18 -07001446 private final Bundle mExtras;
Mathew Inwood8c854f82018-09-14 12:35:36 +01001447 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Dan Sandler86647982015-05-13 23:41:13 -04001448 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001449 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001450 private boolean mAllowGeneratedReplies = true;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001451 private final @SemanticAction int mSemanticAction;
Gustav Sennton005d7a02019-01-04 13:41:32 +00001452 private final boolean mIsContextual;
Griff Hazen959591e2014-05-15 22:26:18 -07001453
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001454 /**
1455 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001456 *
1457 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001458 */
Dan Sandler86647982015-05-13 23:41:13 -04001459 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001460 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001461
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001462 /**
1463 * Title of the action.
1464 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001465 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001466
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001467 /**
1468 * Intent to send when the user invokes this action. May be null, in which case the action
1469 * may be rendered in a disabled presentation by the system UI.
1470 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001471 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001472
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001473 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001474 if (in.readInt() != 0) {
1475 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001476 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1477 icon = mIcon.getResId();
1478 }
Dan Sandler86647982015-05-13 23:41:13 -04001479 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001480 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1481 if (in.readInt() == 1) {
1482 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1483 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001484 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001485 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001486 mAllowGeneratedReplies = in.readInt() == 1;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001487 mSemanticAction = in.readInt();
Gustav Sennton005d7a02019-01-04 13:41:32 +00001488 mIsContextual = in.readInt() == 1;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001489 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001490
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001491 /**
Dan Sandler86647982015-05-13 23:41:13 -04001492 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001493 */
Dan Sandler86647982015-05-13 23:41:13 -04001494 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001495 public Action(int icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001496 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true,
Gustav Sennton005d7a02019-01-04 13:41:32 +00001497 SEMANTIC_ACTION_NONE, false /* isContextual */);
Griff Hazen959591e2014-05-15 22:26:18 -07001498 }
1499
Adrian Roos7af53622016-10-12 13:44:05 -07001500 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001501 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001502 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
Gustav Sennton005d7a02019-01-04 13:41:32 +00001503 @SemanticAction int semanticAction, boolean isContextual) {
Dan Sandler86647982015-05-13 23:41:13 -04001504 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001505 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1506 this.icon = icon.getResId();
1507 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001508 this.title = title;
1509 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001510 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001511 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001512 this.mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001513 this.mSemanticAction = semanticAction;
Gustav Sennton005d7a02019-01-04 13:41:32 +00001514 this.mIsContextual = isContextual;
Griff Hazen959591e2014-05-15 22:26:18 -07001515 }
1516
1517 /**
Dan Sandler86647982015-05-13 23:41:13 -04001518 * Return an icon representing the action.
1519 */
1520 public Icon getIcon() {
1521 if (mIcon == null && icon != 0) {
1522 // you snuck an icon in here without using the builder; let's try to keep it
1523 mIcon = Icon.createWithResource("", icon);
1524 }
1525 return mIcon;
1526 }
1527
1528 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001529 * Get additional metadata carried around with this Action.
1530 */
1531 public Bundle getExtras() {
1532 return mExtras;
1533 }
1534
1535 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001536 * Return whether the platform should automatically generate possible replies for this
1537 * {@link Action}
1538 */
1539 public boolean getAllowGeneratedReplies() {
1540 return mAllowGeneratedReplies;
1541 }
1542
1543 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001544 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001545 * May return null if no remote inputs were added. Only returns inputs which accept
1546 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001547 */
1548 public RemoteInput[] getRemoteInputs() {
1549 return mRemoteInputs;
1550 }
1551
1552 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001553 * Returns the {@code SemanticAction} associated with this {@link Action}. A
1554 * {@code SemanticAction} denotes what an {@link Action}'s {@link PendingIntent} will do
1555 * (eg. reply, mark as read, delete, etc).
1556 */
1557 public @SemanticAction int getSemanticAction() {
1558 return mSemanticAction;
1559 }
1560
1561 /**
Gustav Sennton005d7a02019-01-04 13:41:32 +00001562 * Returns whether this is a contextual Action, i.e. whether the action is dependent on the
1563 * notification message body. An example of a contextual action could be an action opening a
1564 * map application with an address shown in the notification.
1565 */
1566 public boolean isContextual() {
1567 return mIsContextual;
1568 }
1569
1570 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001571 * Get the list of inputs to be collected from the user that ONLY accept data when this
1572 * action is sent. These remote inputs are guaranteed to return true on a call to
1573 * {@link RemoteInput#isDataOnly}.
1574 *
Shane Brennanf670d6c2017-04-25 13:05:45 -07001575 * Returns null if there are no data-only remote inputs.
Shane Brennan472a3b32016-12-12 15:28:10 -08001576 *
1577 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1578 * of non-textual RemoteInputs do not access these remote inputs.
1579 */
1580 public RemoteInput[] getDataOnlyRemoteInputs() {
1581 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1582 }
1583
1584 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001585 * Builder class for {@link Action} objects.
1586 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001587 public static final class Builder {
Gustav Senntone1fc87b2019-03-18 14:31:28 +00001588 @Nullable private final Icon mIcon;
1589 @Nullable private final CharSequence mTitle;
1590 @Nullable private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001591 private boolean mAllowGeneratedReplies = true;
Gustav Senntone1fc87b2019-03-18 14:31:28 +00001592 @NonNull private final Bundle mExtras;
1593 @Nullable private ArrayList<RemoteInput> mRemoteInputs;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001594 private @SemanticAction int mSemanticAction;
Gustav Sennton005d7a02019-01-04 13:41:32 +00001595 private boolean mIsContextual;
Griff Hazen959591e2014-05-15 22:26:18 -07001596
1597 /**
1598 * Construct a new builder for {@link Action} object.
1599 * @param icon icon to show for this action
1600 * @param title the title of the action
1601 * @param intent the {@link PendingIntent} to fire when users trigger this action
1602 */
Dan Sandler86647982015-05-13 23:41:13 -04001603 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001604 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001605 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001606 }
1607
1608 /**
1609 * Construct a new builder for {@link Action} object.
1610 * @param icon icon to show for this action
1611 * @param title the title of the action
1612 * @param intent the {@link PendingIntent} to fire when users trigger this action
1613 */
1614 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001615 this(icon, title, intent, new Bundle(), null, true, SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001616 }
1617
1618 /**
1619 * Construct a new builder for {@link Action} object using the fields from an
1620 * {@link Action}.
1621 * @param action the action to read fields from.
1622 */
1623 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001624 this(action.getIcon(), action.title, action.actionIntent,
1625 new Bundle(action.mExtras), action.getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001626 action.getAllowGeneratedReplies(), action.getSemanticAction());
Griff Hazen959591e2014-05-15 22:26:18 -07001627 }
1628
Gustav Senntone1fc87b2019-03-18 14:31:28 +00001629 private Builder(@Nullable Icon icon, @Nullable CharSequence title,
1630 @Nullable PendingIntent intent, @NonNull Bundle extras,
1631 @Nullable RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1632 @SemanticAction int semanticAction) {
Griff Hazen959591e2014-05-15 22:26:18 -07001633 mIcon = icon;
1634 mTitle = title;
1635 mIntent = intent;
1636 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001637 if (remoteInputs != null) {
1638 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1639 Collections.addAll(mRemoteInputs, remoteInputs);
1640 }
Adrian Roos7af53622016-10-12 13:44:05 -07001641 mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001642 mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001643 }
1644
1645 /**
1646 * Merge additional metadata into this builder.
1647 *
1648 * <p>Values within the Bundle will replace existing extras values in this Builder.
1649 *
1650 * @see Notification.Action#extras
1651 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001652 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07001653 public Builder addExtras(Bundle extras) {
1654 if (extras != null) {
1655 mExtras.putAll(extras);
1656 }
1657 return this;
1658 }
1659
1660 /**
1661 * Get the metadata Bundle used by this Builder.
1662 *
1663 * <p>The returned Bundle is shared with this Builder.
1664 */
Gustav Senntone1fc87b2019-03-18 14:31:28 +00001665 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07001666 public Bundle getExtras() {
1667 return mExtras;
1668 }
1669
1670 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001671 * Add an input to be collected from the user when this action is sent.
1672 * Response values can be retrieved from the fired intent by using the
1673 * {@link RemoteInput#getResultsFromIntent} function.
1674 * @param remoteInput a {@link RemoteInput} to add to the action
1675 * @return this object for method chaining
1676 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001677 @NonNull
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001678 public Builder addRemoteInput(RemoteInput remoteInput) {
1679 if (mRemoteInputs == null) {
1680 mRemoteInputs = new ArrayList<RemoteInput>();
1681 }
1682 mRemoteInputs.add(remoteInput);
1683 return this;
1684 }
1685
1686 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001687 * Set whether the platform should automatically generate possible replies to add to
1688 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1689 * {@link RemoteInput}, this has no effect.
1690 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1691 * otherwise
1692 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001693 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001694 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001695 @NonNull
Alex Hills42b0c4d2016-04-26 13:35:36 -04001696 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1697 mAllowGeneratedReplies = allowGeneratedReplies;
1698 return this;
1699 }
1700
1701 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001702 * Sets the {@code SemanticAction} for this {@link Action}. A
1703 * {@code SemanticAction} denotes what an {@link Action}'s
1704 * {@link PendingIntent} will do (eg. reply, mark as read, delete, etc).
1705 * @param semanticAction a SemanticAction defined within {@link Action} with
1706 * {@code SEMANTIC_ACTION_} prefixes
1707 * @return this object for method chaining
1708 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001709 @NonNull
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001710 public Builder setSemanticAction(@SemanticAction int semanticAction) {
1711 mSemanticAction = semanticAction;
1712 return this;
1713 }
1714
1715 /**
Gustav Sennton005d7a02019-01-04 13:41:32 +00001716 * Sets whether this {@link Action} is a contextual action, i.e. whether the action is
1717 * dependent on the notification message body. An example of a contextual action could
1718 * be an action opening a map application with an address shown in the notification.
1719 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001720 @NonNull
Gustav Sennton005d7a02019-01-04 13:41:32 +00001721 public Builder setContextual(boolean isContextual) {
1722 mIsContextual = isContextual;
1723 return this;
1724 }
1725
1726 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001727 * Apply an extender to this action builder. Extenders may be used to add
1728 * metadata or change options on this builder.
1729 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001730 @NonNull
Griff Hazen61a9e862014-05-22 16:05:19 -07001731 public Builder extend(Extender extender) {
1732 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001733 return this;
1734 }
1735
1736 /**
Gustav Senntonc98b1632018-11-23 12:26:15 +00001737 * Throws an NPE if we are building a contextual action missing one of the fields
1738 * necessary to display the action.
1739 */
1740 private void checkContextualActionNullFields() {
Gustav Sennton005d7a02019-01-04 13:41:32 +00001741 if (!mIsContextual) return;
Gustav Senntonc98b1632018-11-23 12:26:15 +00001742
1743 if (mIcon == null) {
1744 throw new NullPointerException("Contextual Actions must contain a valid icon");
1745 }
1746
1747 if (mIntent == null) {
1748 throw new NullPointerException(
1749 "Contextual Actions must contain a valid PendingIntent");
1750 }
1751 }
1752
1753 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001754 * Combine all of the options that have been set and return a new {@link Action}
1755 * object.
1756 * @return the built action
1757 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001758 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07001759 public Action build() {
Gustav Senntonc98b1632018-11-23 12:26:15 +00001760 checkContextualActionNullFields();
1761
Shane Brennan472a3b32016-12-12 15:28:10 -08001762 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1763 RemoteInput[] previousDataInputs =
1764 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001765 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001766 for (RemoteInput input : previousDataInputs) {
1767 dataOnlyInputs.add(input);
1768 }
1769 }
1770 List<RemoteInput> textInputs = new ArrayList<>();
1771 if (mRemoteInputs != null) {
1772 for (RemoteInput input : mRemoteInputs) {
1773 if (input.isDataOnly()) {
1774 dataOnlyInputs.add(input);
1775 } else {
1776 textInputs.add(input);
1777 }
1778 }
1779 }
1780 if (!dataOnlyInputs.isEmpty()) {
1781 RemoteInput[] dataInputsArr =
1782 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1783 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1784 }
1785 RemoteInput[] textInputsArr = textInputs.isEmpty()
1786 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1787 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Gustav Sennton005d7a02019-01-04 13:41:32 +00001788 mAllowGeneratedReplies, mSemanticAction, mIsContextual);
Griff Hazen959591e2014-05-15 22:26:18 -07001789 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001790 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001791
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001792 @Override
1793 public Action clone() {
1794 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001795 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001796 title,
1797 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001798 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001799 getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001800 getAllowGeneratedReplies(),
Gustav Sennton005d7a02019-01-04 13:41:32 +00001801 getSemanticAction(),
1802 isContextual());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001803 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001804
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001805 @Override
1806 public int describeContents() {
1807 return 0;
1808 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001809
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001810 @Override
1811 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001812 final Icon ic = getIcon();
1813 if (ic != null) {
1814 out.writeInt(1);
1815 ic.writeToParcel(out, 0);
1816 } else {
1817 out.writeInt(0);
1818 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001819 TextUtils.writeToParcel(title, out, flags);
1820 if (actionIntent != null) {
1821 out.writeInt(1);
1822 actionIntent.writeToParcel(out, flags);
1823 } else {
1824 out.writeInt(0);
1825 }
Griff Hazen959591e2014-05-15 22:26:18 -07001826 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001827 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001828 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001829 out.writeInt(mSemanticAction);
Gustav Sennton005d7a02019-01-04 13:41:32 +00001830 out.writeInt(mIsContextual ? 1 : 0);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001831 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001832
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07001833 public static final @android.annotation.NonNull Parcelable.Creator<Action> CREATOR =
Griff Hazen959591e2014-05-15 22:26:18 -07001834 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001835 public Action createFromParcel(Parcel in) {
1836 return new Action(in);
1837 }
1838 public Action[] newArray(int size) {
1839 return new Action[size];
1840 }
1841 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001842
1843 /**
1844 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1845 * metadata or change options on an action builder.
1846 */
1847 public interface Extender {
1848 /**
1849 * Apply this extender to a notification action builder.
1850 * @param builder the builder to be modified.
1851 * @return the build object for chaining.
1852 */
1853 public Builder extend(Builder builder);
1854 }
1855
1856 /**
1857 * Wearable extender for notification actions. To add extensions to an action,
1858 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1859 * the {@code WearableExtender()} constructor and apply it to a
1860 * {@link android.app.Notification.Action.Builder} using
1861 * {@link android.app.Notification.Action.Builder#extend}.
1862 *
1863 * <pre class="prettyprint">
1864 * Notification.Action action = new Notification.Action.Builder(
1865 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001866 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001867 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001868 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001869 */
1870 public static final class WearableExtender implements Extender {
1871 /** Notification action extra which contains wearable extensions */
1872 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1873
Pete Gastaf6781d2014-10-07 15:17:05 -04001874 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001875 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001876 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1877 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1878 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001879
1880 // Flags bitwise-ored to mFlags
1881 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001882 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001883 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001884
1885 // Default value for flags integer
1886 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1887
1888 private int mFlags = DEFAULT_FLAGS;
1889
Pete Gastaf6781d2014-10-07 15:17:05 -04001890 private CharSequence mInProgressLabel;
1891 private CharSequence mConfirmLabel;
1892 private CharSequence mCancelLabel;
1893
Griff Hazen61a9e862014-05-22 16:05:19 -07001894 /**
1895 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1896 * options.
1897 */
1898 public WearableExtender() {
1899 }
1900
1901 /**
1902 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1903 * wearable options present in an existing notification action.
1904 * @param action the notification action to inspect.
1905 */
1906 public WearableExtender(Action action) {
1907 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1908 if (wearableBundle != null) {
1909 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001910 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1911 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1912 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001913 }
1914 }
1915
1916 /**
1917 * Apply wearable extensions to a notification action that is being built. This is
1918 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1919 * method of {@link android.app.Notification.Action.Builder}.
1920 */
1921 @Override
1922 public Action.Builder extend(Action.Builder builder) {
1923 Bundle wearableBundle = new Bundle();
1924
1925 if (mFlags != DEFAULT_FLAGS) {
1926 wearableBundle.putInt(KEY_FLAGS, mFlags);
1927 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001928 if (mInProgressLabel != null) {
1929 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1930 }
1931 if (mConfirmLabel != null) {
1932 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1933 }
1934 if (mCancelLabel != null) {
1935 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1936 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001937
1938 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1939 return builder;
1940 }
1941
1942 @Override
1943 public WearableExtender clone() {
1944 WearableExtender that = new WearableExtender();
1945 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001946 that.mInProgressLabel = this.mInProgressLabel;
1947 that.mConfirmLabel = this.mConfirmLabel;
1948 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001949 return that;
1950 }
1951
1952 /**
1953 * Set whether this action is available when the wearable device is not connected to
1954 * a companion device. The user can still trigger this action when the wearable device is
1955 * offline, but a visual hint will indicate that the action may not be available.
1956 * Defaults to true.
1957 */
1958 public WearableExtender setAvailableOffline(boolean availableOffline) {
1959 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1960 return this;
1961 }
1962
1963 /**
1964 * Get whether this action is available when the wearable device is not connected to
1965 * a companion device. The user can still trigger this action when the wearable device is
1966 * offline, but a visual hint will indicate that the action may not be available.
1967 * Defaults to true.
1968 */
1969 public boolean isAvailableOffline() {
1970 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1971 }
1972
1973 private void setFlag(int mask, boolean value) {
1974 if (value) {
1975 mFlags |= mask;
1976 } else {
1977 mFlags &= ~mask;
1978 }
1979 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001980
1981 /**
1982 * Set a label to display while the wearable is preparing to automatically execute the
1983 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1984 *
1985 * @param label the label to display while the action is being prepared to execute
1986 * @return this object for method chaining
1987 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001988 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001989 public WearableExtender setInProgressLabel(CharSequence label) {
1990 mInProgressLabel = label;
1991 return this;
1992 }
1993
1994 /**
1995 * Get the label to display while the wearable is preparing to automatically execute
1996 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1997 *
1998 * @return the label to display while the action is being prepared to execute
1999 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002000 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002001 public CharSequence getInProgressLabel() {
2002 return mInProgressLabel;
2003 }
2004
2005 /**
2006 * Set a label to display to confirm that the action should be executed.
2007 * This is usually an imperative verb like "Send".
2008 *
2009 * @param label the label to confirm the action should be executed
2010 * @return this object for method chaining
2011 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002012 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002013 public WearableExtender setConfirmLabel(CharSequence label) {
2014 mConfirmLabel = label;
2015 return this;
2016 }
2017
2018 /**
2019 * Get the label to display to confirm that the action should be executed.
2020 * This is usually an imperative verb like "Send".
2021 *
2022 * @return the label to confirm the action should be executed
2023 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002024 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002025 public CharSequence getConfirmLabel() {
2026 return mConfirmLabel;
2027 }
2028
2029 /**
2030 * Set a label to display to cancel the action.
2031 * This is usually an imperative verb, like "Cancel".
2032 *
2033 * @param label the label to display to cancel the action
2034 * @return this object for method chaining
2035 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002036 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002037 public WearableExtender setCancelLabel(CharSequence label) {
2038 mCancelLabel = label;
2039 return this;
2040 }
2041
2042 /**
2043 * Get the label to display to cancel the action.
2044 * This is usually an imperative verb like "Cancel".
2045 *
2046 * @return the label to display to cancel the action
2047 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002048 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002049 public CharSequence getCancelLabel() {
2050 return mCancelLabel;
2051 }
Alex Hills9ab3a232016-04-05 14:54:56 -04002052
2053 /**
2054 * Set a hint that this Action will launch an {@link Activity} directly, telling the
2055 * platform that it can generate the appropriate transitions.
2056 * @param hintLaunchesActivity {@code true} if the content intent will launch
2057 * an activity and transitions should be generated, false otherwise.
2058 * @return this object for method chaining
2059 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04002060 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04002061 boolean hintLaunchesActivity) {
2062 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
2063 return this;
2064 }
2065
2066 /**
2067 * Get a hint that this Action will launch an {@link Activity} directly, telling the
2068 * platform that it can generate the appropriate transitions
2069 * @return {@code true} if the content intent will launch an activity and transitions
2070 * should be generated, false otherwise. The default value is {@code false} if this was
2071 * never set.
2072 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04002073 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04002074 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
2075 }
Alex Hills9f087612016-06-07 09:08:59 -04002076
2077 /**
2078 * Set a hint that this Action should be displayed inline.
2079 *
2080 * @param hintDisplayInline {@code true} if action should be displayed inline, false
2081 * otherwise
2082 * @return this object for method chaining
2083 */
2084 public WearableExtender setHintDisplayActionInline(
2085 boolean hintDisplayInline) {
2086 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
2087 return this;
2088 }
2089
2090 /**
2091 * Get a hint that this Action should be displayed inline.
2092 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08002093 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04002094 * otherwise. The default value is {@code false} if this was never set.
2095 */
2096 public boolean getHintDisplayActionInline() {
2097 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
2098 }
Griff Hazen61a9e862014-05-22 16:05:19 -07002099 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002100
2101 /**
2102 * Provides meaning to an {@link Action} that hints at what the associated
2103 * {@link PendingIntent} will do. For example, an {@link Action} with a
2104 * {@link PendingIntent} that replies to a text message notification may have the
2105 * {@link #SEMANTIC_ACTION_REPLY} {@code SemanticAction} set within it.
2106 *
2107 * @hide
2108 */
2109 @IntDef(prefix = { "SEMANTIC_ACTION_" }, value = {
2110 SEMANTIC_ACTION_NONE,
2111 SEMANTIC_ACTION_REPLY,
2112 SEMANTIC_ACTION_MARK_AS_READ,
2113 SEMANTIC_ACTION_MARK_AS_UNREAD,
2114 SEMANTIC_ACTION_DELETE,
2115 SEMANTIC_ACTION_ARCHIVE,
2116 SEMANTIC_ACTION_MUTE,
2117 SEMANTIC_ACTION_UNMUTE,
2118 SEMANTIC_ACTION_THUMBS_UP,
Kodlee Yincda5b092018-02-15 15:34:53 -08002119 SEMANTIC_ACTION_THUMBS_DOWN,
Gustav Sennton005d7a02019-01-04 13:41:32 +00002120 SEMANTIC_ACTION_CALL
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002121 })
2122 @Retention(RetentionPolicy.SOURCE)
2123 public @interface SemanticAction {}
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002124 }
2125
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002126 /**
2127 * Array of all {@link Action} structures attached to this notification by
2128 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
2129 * {@link android.service.notification.NotificationListenerService} that provide an alternative
2130 * interface for invoking actions.
2131 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05002132 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002133
2134 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002135 * Replacement version of this notification whose content will be shown
2136 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
2137 * and {@link #VISIBILITY_PUBLIC}.
2138 */
2139 public Notification publicVersion;
2140
2141 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002142 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08002143 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002144 */
2145 public Notification()
2146 {
2147 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002148 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002149 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002150 }
2151
2152 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002153 * @hide
2154 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002155 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002156 public Notification(Context context, int icon, CharSequence tickerText, long when,
2157 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
2158 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04002159 new Builder(context)
2160 .setWhen(when)
2161 .setSmallIcon(icon)
2162 .setTicker(tickerText)
2163 .setContentTitle(contentTitle)
2164 .setContentText(contentText)
2165 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
2166 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002167 }
2168
2169 /**
2170 * Constructs a Notification object with the information needed to
2171 * have a status bar icon without the standard expanded view.
2172 *
2173 * @param icon The resource id of the icon to put in the status bar.
2174 * @param tickerText The text that flows by in the status bar when the notification first
2175 * activates.
2176 * @param when The time to show in the time field. In the System.currentTimeMillis
2177 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08002178 *
2179 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002180 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002181 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002182 public Notification(int icon, CharSequence tickerText, long when)
2183 {
2184 this.icon = icon;
2185 this.tickerText = tickerText;
2186 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002187 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002188 }
2189
2190 /**
2191 * Unflatten the notification from a parcel.
2192 */
Svet Ganovddb94882016-06-23 19:55:24 -07002193 @SuppressWarnings("unchecked")
2194 public Notification(Parcel parcel) {
2195 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
2196 // intents in extras are always written as the last entry.
2197 readFromParcelImpl(parcel);
2198 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002199 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07002200 }
2201
2202 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002203 {
2204 int version = parcel.readInt();
2205
Adrian Roosfb921842017-10-26 14:49:56 +02002206 mWhitelistToken = parcel.readStrongBinder();
2207 if (mWhitelistToken == null) {
2208 mWhitelistToken = processWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -07002209 }
2210 // Propagate this token to all pending intents that are unmarshalled from the parcel.
Adrian Roosfb921842017-10-26 14:49:56 +02002211 parcel.setClassCookie(PendingIntent.class, mWhitelistToken);
Dianne Hackborn98305522017-05-05 17:53:53 -07002212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002213 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002214 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04002215 if (parcel.readInt() != 0) {
2216 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04002217 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
2218 icon = mSmallIcon.getResId();
2219 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002220 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002221 number = parcel.readInt();
2222 if (parcel.readInt() != 0) {
2223 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2224 }
2225 if (parcel.readInt() != 0) {
2226 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2227 }
2228 if (parcel.readInt() != 0) {
2229 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2230 }
2231 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002232 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002233 }
2234 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002235 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
2236 }
Joe Onorato561d3852010-11-20 18:09:34 -08002237 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002238 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08002239 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002240 defaults = parcel.readInt();
2241 flags = parcel.readInt();
2242 if (parcel.readInt() != 0) {
2243 sound = Uri.CREATOR.createFromParcel(parcel);
2244 }
2245
2246 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04002247 if (parcel.readInt() != 0) {
2248 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
2249 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002250 vibrate = parcel.createLongArray();
2251 ledARGB = parcel.readInt();
2252 ledOnMS = parcel.readInt();
2253 ledOffMS = parcel.readInt();
2254 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002255
2256 if (parcel.readInt() != 0) {
2257 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2258 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002259
2260 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002261
John Spurlockfd7f1e02014-03-18 16:41:57 -04002262 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002263
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002264 mGroupKey = parcel.readString();
2265
2266 mSortKey = parcel.readString();
2267
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06002268 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Robin Leead7e72a2017-12-04 15:45:46 +01002269 fixDuplicateExtras();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002270
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002271 actions = parcel.createTypedArray(Action.CREATOR); // may be null
2272
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002273 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002274 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2275 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002276
Chris Wren8fd39ec2014-02-27 17:43:26 -05002277 if (parcel.readInt() != 0) {
2278 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2279 }
2280
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002281 visibility = parcel.readInt();
2282
2283 if (parcel.readInt() != 0) {
2284 publicVersion = Notification.CREATOR.createFromParcel(parcel);
2285 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002286
2287 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002288
2289 if (parcel.readInt() != 0) {
2290 mChannelId = parcel.readString();
2291 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002292 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05002293
2294 if (parcel.readInt() != 0) {
2295 mShortcutId = parcel.readString();
2296 }
2297
Felipe Leme90205ef2019-03-05 09:59:52 -08002298 if (parcel.readInt() != 0) {
2299 mLocusId = LocusId.CREATOR.createFromParcel(parcel);
2300 }
2301
Julia Reynolds13d898c2017-02-02 12:22:05 -05002302 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04002303
2304 if (parcel.readInt() != 0) {
2305 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2306 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002307
2308 mGroupAlertBehavior = parcel.readInt();
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002309 if (parcel.readInt() != 0) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002310 mBubbleMetadata = BubbleMetadata.CREATOR.createFromParcel(parcel);
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002311 }
Gustav Sennton761884c2018-11-19 17:40:19 +00002312
2313 mAllowSystemGeneratedContextualActions = parcel.readBoolean();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002314 }
2315
Andy Stadler110988c2010-12-03 14:29:16 -08002316 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07002317 public Notification clone() {
2318 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04002319 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002320 return that;
2321 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002322
Daniel Sandler1a497d32013-04-18 14:52:45 -04002323 /**
2324 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
2325 * of this into that.
2326 * @hide
2327 */
2328 public void cloneInto(Notification that, boolean heavy) {
Adrian Roosfb921842017-10-26 14:49:56 +02002329 that.mWhitelistToken = this.mWhitelistToken;
Joe Onorato18e69df2010-05-17 22:26:12 -07002330 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002331 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002332 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07002333 that.number = this.number;
2334
2335 // PendingIntents are global, so there's no reason (or way) to clone them.
2336 that.contentIntent = this.contentIntent;
2337 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002338 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07002339
2340 if (this.tickerText != null) {
2341 that.tickerText = this.tickerText.toString();
2342 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002343 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002344 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04002345 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002346 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07002347 that.contentView = this.contentView.clone();
2348 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002349 if (heavy && this.mLargeIcon != null) {
2350 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08002351 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01002352 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07002353 that.sound = this.sound; // android.net.Uri is immutable
2354 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002355 if (this.audioAttributes != null) {
2356 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
2357 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002358
2359 final long[] vibrate = this.vibrate;
2360 if (vibrate != null) {
2361 final int N = vibrate.length;
2362 final long[] vib = that.vibrate = new long[N];
2363 System.arraycopy(vibrate, 0, vib, 0, N);
2364 }
2365
2366 that.ledARGB = this.ledARGB;
2367 that.ledOnMS = this.ledOnMS;
2368 that.ledOffMS = this.ledOffMS;
2369 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002370
Joe Onorato18e69df2010-05-17 22:26:12 -07002371 that.flags = this.flags;
2372
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002373 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08002374
John Spurlockfd7f1e02014-03-18 16:41:57 -04002375 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002376
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002377 that.mGroupKey = this.mGroupKey;
2378
2379 that.mSortKey = this.mSortKey;
2380
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002381 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002382 try {
2383 that.extras = new Bundle(this.extras);
2384 // will unparcel
2385 that.extras.size();
2386 } catch (BadParcelableException e) {
2387 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
2388 that.extras = null;
2389 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002390 }
2391
Felipe Lemedd85da62016-06-28 11:29:54 -07002392 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2393 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002394 }
2395
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002396 if (this.actions != null) {
2397 that.actions = new Action[this.actions.length];
2398 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002399 if ( this.actions[i] != null) {
2400 that.actions[i] = this.actions[i].clone();
2401 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002402 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002403 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002404
Daniel Sandler1a497d32013-04-18 14:52:45 -04002405 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002406 that.bigContentView = this.bigContentView.clone();
2407 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002408
Chris Wren8fd39ec2014-02-27 17:43:26 -05002409 if (heavy && this.headsUpContentView != null) {
2410 that.headsUpContentView = this.headsUpContentView.clone();
2411 }
2412
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002413 that.visibility = this.visibility;
2414
2415 if (this.publicVersion != null) {
2416 that.publicVersion = new Notification();
2417 this.publicVersion.cloneInto(that.publicVersion, heavy);
2418 }
2419
Dan Sandler26e81cf2014-05-06 10:01:27 -04002420 that.color = this.color;
2421
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002422 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002423 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002424 that.mShortcutId = this.mShortcutId;
Felipe Leme90205ef2019-03-05 09:59:52 -08002425 that.mLocusId = this.mLocusId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002426 that.mBadgeIcon = this.mBadgeIcon;
2427 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002428 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002429 that.mBubbleMetadata = this.mBubbleMetadata;
Gustav Sennton761884c2018-11-19 17:40:19 +00002430 that.mAllowSystemGeneratedContextualActions = this.mAllowSystemGeneratedContextualActions;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002431
Daniel Sandler1a497d32013-04-18 14:52:45 -04002432 if (!heavy) {
2433 that.lightenPayload(); // will clean out extras
2434 }
2435 }
2436
2437 /**
Jeff Sharkey23b31182018-04-18 21:32:12 -06002438 * Note all {@link Uri} that are referenced internally, with the expectation
2439 * that Uri permission grants will need to be issued to ensure the recipient
2440 * of this object is able to render its contents.
2441 *
2442 * @hide
2443 */
2444 public void visitUris(@NonNull Consumer<Uri> visitor) {
2445 visitor.accept(sound);
2446
2447 if (tickerView != null) tickerView.visitUris(visitor);
2448 if (contentView != null) contentView.visitUris(visitor);
2449 if (bigContentView != null) bigContentView.visitUris(visitor);
2450 if (headsUpContentView != null) headsUpContentView.visitUris(visitor);
2451
2452 if (extras != null) {
2453 visitor.accept(extras.getParcelable(EXTRA_AUDIO_CONTENTS_URI));
Robin Leed107af62018-04-27 13:55:56 +02002454 if (extras.containsKey(EXTRA_BACKGROUND_IMAGE_URI)) {
2455 visitor.accept(Uri.parse(extras.getString(EXTRA_BACKGROUND_IMAGE_URI)));
2456 }
Jeff Sharkey23b31182018-04-18 21:32:12 -06002457 }
2458
2459 if (MessagingStyle.class.equals(getNotificationStyle()) && extras != null) {
2460 final Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
2461 if (!ArrayUtils.isEmpty(messages)) {
2462 for (MessagingStyle.Message message : MessagingStyle.Message
2463 .getMessagesFromBundleArray(messages)) {
2464 visitor.accept(message.getDataUri());
2465 }
2466 }
2467
2468 final Parcelable[] historic = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
2469 if (!ArrayUtils.isEmpty(historic)) {
2470 for (MessagingStyle.Message message : MessagingStyle.Message
2471 .getMessagesFromBundleArray(historic)) {
2472 visitor.accept(message.getDataUri());
2473 }
2474 }
2475 }
2476 }
2477
2478 /**
Daniel Sandler1a497d32013-04-18 14:52:45 -04002479 * Removes heavyweight parts of the Notification object for archival or for sending to
2480 * listeners when the full contents are not necessary.
2481 * @hide
2482 */
2483 public final void lightenPayload() {
2484 tickerView = null;
2485 contentView = null;
2486 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002487 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002488 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002489 if (extras != null && !extras.isEmpty()) {
2490 final Set<String> keyset = extras.keySet();
2491 final int N = keyset.size();
2492 final String[] keys = keyset.toArray(new String[N]);
2493 for (int i=0; i<N; i++) {
2494 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002495 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2496 continue;
2497 }
Dan Sandler50128532015-12-08 15:42:41 -05002498 final Object obj = extras.get(key);
2499 if (obj != null &&
2500 ( obj instanceof Parcelable
2501 || obj instanceof Parcelable[]
2502 || obj instanceof SparseArray
2503 || obj instanceof ArrayList)) {
2504 extras.remove(key);
2505 }
2506 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002507 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002508 }
2509
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002510 /**
2511 * Make sure this CharSequence is safe to put into a bundle, which basically
2512 * means it had better not be some custom Parcelable implementation.
2513 * @hide
2514 */
2515 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002516 if (cs == null) return cs;
2517 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2518 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2519 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002520 if (cs instanceof Parcelable) {
2521 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2522 + " instance is a custom Parcelable and not allowed in Notification");
2523 return cs.toString();
2524 }
Selim Cinek60a54252016-02-26 17:03:25 -08002525 return removeTextSizeSpans(cs);
2526 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002527
Selim Cinek60a54252016-02-26 17:03:25 -08002528 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2529 if (charSequence instanceof Spanned) {
2530 Spanned ss = (Spanned) charSequence;
2531 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2532 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2533 for (Object span : spans) {
2534 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002535 if (resultSpan instanceof CharacterStyle) {
2536 resultSpan = ((CharacterStyle) span).getUnderlying();
2537 }
2538 if (resultSpan instanceof TextAppearanceSpan) {
2539 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002540 resultSpan = new TextAppearanceSpan(
2541 originalSpan.getFamily(),
2542 originalSpan.getTextStyle(),
2543 -1,
2544 originalSpan.getTextColor(),
2545 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002546 } else if (resultSpan instanceof RelativeSizeSpan
2547 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002548 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002549 } else {
2550 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002551 }
2552 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2553 ss.getSpanFlags(span));
2554 }
2555 return builder;
2556 }
2557 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002558 }
2559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002560 public int describeContents() {
2561 return 0;
2562 }
2563
2564 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002565 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002566 */
Svet Ganovddb94882016-06-23 19:55:24 -07002567 public void writeToParcel(Parcel parcel, int flags) {
2568 // We need to mark all pending intents getting into the notification
2569 // system as being put there to later allow the notification ranker
2570 // to launch them and by doing so add the app to the battery saver white
2571 // list for a short period of time. The problem is that the system
2572 // cannot look into the extras as there may be parcelables there that
2573 // the platform does not know how to handle. To go around that we have
2574 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002575 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002576 if (collectPendingIntents) {
2577 PendingIntent.setOnMarshaledListener(
2578 (PendingIntent intent, Parcel out, int outFlags) -> {
2579 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002580 if (allPendingIntents == null) {
2581 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002582 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002583 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002584 }
2585 });
2586 }
2587 try {
2588 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002589 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002590 writeToParcelImpl(parcel, flags);
2591 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002592 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002593 } finally {
2594 if (collectPendingIntents) {
2595 PendingIntent.setOnMarshaledListener(null);
2596 }
2597 }
2598 }
2599
2600 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002601 parcel.writeInt(1);
2602
Adrian Roosfb921842017-10-26 14:49:56 +02002603 parcel.writeStrongBinder(mWhitelistToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002604 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002605 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002606 if (mSmallIcon == null && icon != 0) {
2607 // you snuck an icon in here without using the builder; let's try to keep it
2608 mSmallIcon = Icon.createWithResource("", icon);
2609 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002610 if (mSmallIcon != null) {
2611 parcel.writeInt(1);
2612 mSmallIcon.writeToParcel(parcel, 0);
2613 } else {
2614 parcel.writeInt(0);
2615 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616 parcel.writeInt(number);
2617 if (contentIntent != null) {
2618 parcel.writeInt(1);
2619 contentIntent.writeToParcel(parcel, 0);
2620 } else {
2621 parcel.writeInt(0);
2622 }
2623 if (deleteIntent != null) {
2624 parcel.writeInt(1);
2625 deleteIntent.writeToParcel(parcel, 0);
2626 } else {
2627 parcel.writeInt(0);
2628 }
2629 if (tickerText != null) {
2630 parcel.writeInt(1);
2631 TextUtils.writeToParcel(tickerText, parcel, flags);
2632 } else {
2633 parcel.writeInt(0);
2634 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002635 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002636 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002637 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002638 } else {
2639 parcel.writeInt(0);
2640 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002641 if (contentView != null) {
2642 parcel.writeInt(1);
2643 contentView.writeToParcel(parcel, 0);
2644 } else {
2645 parcel.writeInt(0);
2646 }
Selim Cinek279fa862016-06-14 10:57:25 -07002647 if (mLargeIcon == null && largeIcon != null) {
2648 // you snuck an icon in here without using the builder; let's try to keep it
2649 mLargeIcon = Icon.createWithBitmap(largeIcon);
2650 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002651 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002652 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002653 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002654 } else {
2655 parcel.writeInt(0);
2656 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002657
2658 parcel.writeInt(defaults);
2659 parcel.writeInt(this.flags);
2660
2661 if (sound != null) {
2662 parcel.writeInt(1);
2663 sound.writeToParcel(parcel, 0);
2664 } else {
2665 parcel.writeInt(0);
2666 }
2667 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002668
2669 if (audioAttributes != null) {
2670 parcel.writeInt(1);
2671 audioAttributes.writeToParcel(parcel, 0);
2672 } else {
2673 parcel.writeInt(0);
2674 }
2675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002676 parcel.writeLongArray(vibrate);
2677 parcel.writeInt(ledARGB);
2678 parcel.writeInt(ledOnMS);
2679 parcel.writeInt(ledOffMS);
2680 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002681
2682 if (fullScreenIntent != null) {
2683 parcel.writeInt(1);
2684 fullScreenIntent.writeToParcel(parcel, 0);
2685 } else {
2686 parcel.writeInt(0);
2687 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002688
2689 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002690
John Spurlockfd7f1e02014-03-18 16:41:57 -04002691 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002692
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002693 parcel.writeString(mGroupKey);
2694
2695 parcel.writeString(mSortKey);
2696
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002697 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002698
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002699 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002700
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002701 if (bigContentView != null) {
2702 parcel.writeInt(1);
2703 bigContentView.writeToParcel(parcel, 0);
2704 } else {
2705 parcel.writeInt(0);
2706 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002707
Chris Wren8fd39ec2014-02-27 17:43:26 -05002708 if (headsUpContentView != null) {
2709 parcel.writeInt(1);
2710 headsUpContentView.writeToParcel(parcel, 0);
2711 } else {
2712 parcel.writeInt(0);
2713 }
2714
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002715 parcel.writeInt(visibility);
2716
2717 if (publicVersion != null) {
2718 parcel.writeInt(1);
2719 publicVersion.writeToParcel(parcel, 0);
2720 } else {
2721 parcel.writeInt(0);
2722 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002723
2724 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002725
2726 if (mChannelId != null) {
2727 parcel.writeInt(1);
2728 parcel.writeString(mChannelId);
2729 } else {
2730 parcel.writeInt(0);
2731 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002732 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002733
2734 if (mShortcutId != null) {
2735 parcel.writeInt(1);
2736 parcel.writeString(mShortcutId);
2737 } else {
2738 parcel.writeInt(0);
2739 }
2740
Felipe Leme90205ef2019-03-05 09:59:52 -08002741 if (mLocusId != null) {
2742 parcel.writeInt(1);
2743 mLocusId.writeToParcel(parcel, 0);
2744 } else {
2745 parcel.writeInt(0);
2746 }
2747
Julia Reynolds13d898c2017-02-02 12:22:05 -05002748 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002749
2750 if (mSettingsText != null) {
2751 parcel.writeInt(1);
2752 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2753 } else {
2754 parcel.writeInt(0);
2755 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002756
2757 parcel.writeInt(mGroupAlertBehavior);
Julia Reynoldsfc640012018-02-21 12:25:27 -05002758
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002759 if (mBubbleMetadata != null) {
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002760 parcel.writeInt(1);
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002761 mBubbleMetadata.writeToParcel(parcel, 0);
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002762 } else {
2763 parcel.writeInt(0);
2764 }
2765
Gustav Sennton761884c2018-11-19 17:40:19 +00002766 parcel.writeBoolean(mAllowSystemGeneratedContextualActions);
2767
Julia Reynoldsfc640012018-02-21 12:25:27 -05002768 // mUsesStandardHeader is not written because it should be recomputed in listeners
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002769 }
2770
2771 /**
2772 * Parcelable.Creator that instantiates Notification objects
2773 */
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07002774 public static final @android.annotation.NonNull Parcelable.Creator<Notification> CREATOR
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775 = new Parcelable.Creator<Notification>()
2776 {
2777 public Notification createFromParcel(Parcel parcel)
2778 {
2779 return new Notification(parcel);
2780 }
2781
2782 public Notification[] newArray(int size)
2783 {
2784 return new Notification[size];
2785 }
2786 };
2787
2788 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05002789 * @hide
2790 */
2791 public static boolean areActionsVisiblyDifferent(Notification first, Notification second) {
2792 Notification.Action[] firstAs = first.actions;
2793 Notification.Action[] secondAs = second.actions;
2794 if (firstAs == null && secondAs != null || firstAs != null && secondAs == null) {
2795 return true;
2796 }
2797 if (firstAs != null && secondAs != null) {
2798 if (firstAs.length != secondAs.length) {
2799 return true;
2800 }
2801 for (int i = 0; i < firstAs.length; i++) {
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -04002802 if (!Objects.equals(String.valueOf(firstAs[i].title),
2803 String.valueOf(secondAs[i].title))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05002804 return true;
2805 }
2806 RemoteInput[] firstRs = firstAs[i].getRemoteInputs();
2807 RemoteInput[] secondRs = secondAs[i].getRemoteInputs();
2808 if (firstRs == null) {
2809 firstRs = new RemoteInput[0];
2810 }
2811 if (secondRs == null) {
2812 secondRs = new RemoteInput[0];
2813 }
2814 if (firstRs.length != secondRs.length) {
2815 return true;
2816 }
2817 for (int j = 0; j < firstRs.length; j++) {
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -04002818 if (!Objects.equals(String.valueOf(firstRs[j].getLabel()),
2819 String.valueOf(secondRs[j].getLabel()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05002820 return true;
2821 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05002822 }
2823 }
2824 }
2825 return false;
2826 }
2827
2828 /**
2829 * @hide
2830 */
2831 public static boolean areStyledNotificationsVisiblyDifferent(Builder first, Builder second) {
2832 if (first.getStyle() == null) {
2833 return second.getStyle() != null;
2834 }
2835 if (second.getStyle() == null) {
2836 return true;
2837 }
2838 return first.getStyle().areNotificationsVisiblyDifferent(second.getStyle());
2839 }
2840
2841 /**
2842 * @hide
2843 */
2844 public static boolean areRemoteViewsChanged(Builder first, Builder second) {
Julia Reynoldse5c60452018-04-30 14:41:36 -04002845 if (!Objects.equals(first.usesStandardHeader(), second.usesStandardHeader())) {
2846 return true;
2847 }
2848
2849 if (areRemoteViewsChanged(first.mN.contentView, second.mN.contentView)) {
2850 return true;
2851 }
2852 if (areRemoteViewsChanged(first.mN.bigContentView, second.mN.bigContentView)) {
2853 return true;
2854 }
2855 if (areRemoteViewsChanged(first.mN.headsUpContentView, second.mN.headsUpContentView)) {
2856 return true;
2857 }
2858
2859 return false;
2860 }
2861
2862 private static boolean areRemoteViewsChanged(RemoteViews first, RemoteViews second) {
2863 if (first == null && second == null) {
2864 return false;
2865 }
2866 if (first == null && second != null || first != null && second == null) {
2867 return true;
2868 }
2869
2870 if (!Objects.equals(first.getLayoutId(), second.getLayoutId())) {
2871 return true;
2872 }
2873
2874 if (!Objects.equals(first.getSequenceNumber(), second.getSequenceNumber())) {
2875 return true;
2876 }
2877
2878 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05002879 }
2880
2881 /**
Robin Leead7e72a2017-12-04 15:45:46 +01002882 * Parcelling creates multiple copies of objects in {@code extras}. Fix them.
2883 * <p>
2884 * For backwards compatibility {@code extras} holds some references to "real" member data such
2885 * as {@link getLargeIcon()} which is mirrored by {@link #EXTRA_LARGE_ICON}. This is mostly
2886 * fine as long as the object stays in one process.
2887 * <p>
2888 * However, once the notification goes into a parcel each reference gets marshalled separately,
2889 * wasting memory. Especially with large images on Auto and TV, this is worth fixing.
2890 */
2891 private void fixDuplicateExtras() {
2892 if (extras != null) {
Robin Leead7e72a2017-12-04 15:45:46 +01002893 fixDuplicateExtra(mLargeIcon, EXTRA_LARGE_ICON);
2894 }
2895 }
2896
2897 /**
2898 * If we find an extra that's exactly the same as one of the "real" fields but refers to a
2899 * separate object, replace it with the field's version to avoid holding duplicate copies.
2900 */
2901 private void fixDuplicateExtra(@Nullable Parcelable original, @NonNull String extraName) {
2902 if (original != null && extras.getParcelable(extraName) != null) {
2903 extras.putParcelable(extraName, original);
2904 }
2905 }
2906
2907 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002908 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2909 * layout.
2910 *
2911 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2912 * in the view.</p>
2913 * @param context The context for your application / activity.
2914 * @param contentTitle The title that goes in the expanded entry.
2915 * @param contentText The text that goes in the expanded entry.
2916 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2917 * If this is an activity, it must include the
2918 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002919 * that you take care of task management as described in the
2920 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2921 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002922 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002923 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002924 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002926 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002927 public void setLatestEventInfo(Context context,
2928 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002929 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2930 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2931 new Throwable());
2932 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002933
Selim Cinek4ac6f602016-06-13 15:47:03 -07002934 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2935 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2936 }
2937
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002938 // ensure that any information already set directly is preserved
2939 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002940
2941 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002942 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002943 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002944 }
2945 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002946 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002947 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002948 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002949
2950 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951 }
2952
Julia Reynoldsda303542015-11-23 14:00:20 -05002953 /**
2954 * @hide
2955 */
2956 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002957 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002958 }
2959
2960 /**
2961 * @hide
2962 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002963 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002964 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002965 }
2966
Yi Jin6b514142017-10-30 14:54:12 -07002967 /**
2968 * @hide
2969 */
2970 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2971 long token = proto.start(fieldId);
2972 proto.write(NotificationProto.CHANNEL_ID, getChannelId());
2973 proto.write(NotificationProto.HAS_TICKER_TEXT, this.tickerText != null);
2974 proto.write(NotificationProto.FLAGS, this.flags);
2975 proto.write(NotificationProto.COLOR, this.color);
2976 proto.write(NotificationProto.CATEGORY, this.category);
2977 proto.write(NotificationProto.GROUP_KEY, this.mGroupKey);
2978 proto.write(NotificationProto.SORT_KEY, this.mSortKey);
2979 if (this.actions != null) {
2980 proto.write(NotificationProto.ACTION_LENGTH, this.actions.length);
2981 }
2982 if (this.visibility >= VISIBILITY_SECRET && this.visibility <= VISIBILITY_PUBLIC) {
2983 proto.write(NotificationProto.VISIBILITY, this.visibility);
2984 }
2985 if (publicVersion != null) {
2986 publicVersion.writeToProto(proto, NotificationProto.PUBLIC_VERSION);
2987 }
2988 proto.end(token);
2989 }
2990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002991 @Override
2992 public String toString() {
2993 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002994 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04002995 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002996 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002997 sb.append(priority);
2998 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002999 if (contentView != null) {
3000 sb.append(contentView.getPackage());
3001 sb.append("/0x");
3002 sb.append(Integer.toHexString(contentView.getLayoutId()));
3003 } else {
3004 sb.append("null");
3005 }
3006 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05003007 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
3008 sb.append("default");
3009 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003010 int N = this.vibrate.length-1;
3011 sb.append("[");
3012 for (int i=0; i<N; i++) {
3013 sb.append(this.vibrate[i]);
3014 sb.append(',');
3015 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02003016 if (N != -1) {
3017 sb.append(this.vibrate[N]);
3018 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003019 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003020 } else {
3021 sb.append("null");
3022 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003023 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05003024 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003025 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05003026 } else if (this.sound != null) {
3027 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003028 } else {
3029 sb.append("null");
3030 }
Chris Wren365b6d32015-07-16 10:39:26 -04003031 if (this.tickerText != null) {
3032 sb.append(" tick");
3033 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003034 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003035 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003036 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04003037 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04003038 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003039 if (this.category != null) {
3040 sb.append(" category=");
3041 sb.append(this.category);
3042 }
3043 if (this.mGroupKey != null) {
3044 sb.append(" groupKey=");
3045 sb.append(this.mGroupKey);
3046 }
3047 if (this.mSortKey != null) {
3048 sb.append(" sortKey=");
3049 sb.append(this.mSortKey);
3050 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003051 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04003052 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003053 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04003054 }
3055 sb.append(" vis=");
3056 sb.append(visibilityToString(this.visibility));
3057 if (this.publicVersion != null) {
3058 sb.append(" publicVersion=");
3059 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003060 }
Felipe Leme90205ef2019-03-05 09:59:52 -08003061 if (this.mLocusId != null) {
3062 sb.append(" locusId=");
3063 sb.append(this.mLocusId); // LocusId.toString() is PII safe.
3064 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003065 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003066 return sb.toString();
3067 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003068
Dan Sandler1b718782014-07-18 12:43:45 -04003069 /**
3070 * {@hide}
3071 */
3072 public static String visibilityToString(int vis) {
3073 switch (vis) {
3074 case VISIBILITY_PRIVATE:
3075 return "PRIVATE";
3076 case VISIBILITY_PUBLIC:
3077 return "PUBLIC";
3078 case VISIBILITY_SECRET:
3079 return "SECRET";
3080 default:
3081 return "UNKNOWN(" + String.valueOf(vis) + ")";
3082 }
3083 }
3084
Joe Onoratocb109a02011-01-18 17:57:41 -08003085 /**
John Spurlock1d881a12015-03-18 19:21:54 -04003086 * {@hide}
3087 */
3088 public static String priorityToString(@Priority int pri) {
3089 switch (pri) {
3090 case PRIORITY_MIN:
3091 return "MIN";
3092 case PRIORITY_LOW:
3093 return "LOW";
3094 case PRIORITY_DEFAULT:
3095 return "DEFAULT";
3096 case PRIORITY_HIGH:
3097 return "HIGH";
3098 case PRIORITY_MAX:
3099 return "MAX";
3100 default:
3101 return "UNKNOWN(" + String.valueOf(pri) + ")";
3102 }
3103 }
3104
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04003105 /**
3106 * @hide
3107 */
3108 public boolean hasCompletedProgress() {
3109 // not a progress notification; can't be complete
3110 if (!extras.containsKey(EXTRA_PROGRESS)
3111 || !extras.containsKey(EXTRA_PROGRESS_MAX)) {
3112 return false;
3113 }
3114 // many apps use max 0 for 'indeterminate'; not complete
3115 if (extras.getInt(EXTRA_PROGRESS_MAX) == 0) {
3116 return false;
3117 }
3118 return extras.getInt(EXTRA_PROGRESS) == extras.getInt(EXTRA_PROGRESS_MAX);
3119 }
3120
Jeff Sharkey000ce802017-04-29 13:13:27 -06003121 /** @removed */
3122 @Deprecated
Julia Reynolds37856052016-11-11 09:20:07 -05003123 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003124 return mChannelId;
3125 }
3126
3127 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003128 * Returns the id of the channel this notification posts to.
3129 */
3130 public String getChannelId() {
3131 return mChannelId;
3132 }
3133
Jeff Sharkey000ce802017-04-29 13:13:27 -06003134 /** @removed */
3135 @Deprecated
Julia Reynolds2a128742016-11-28 14:29:25 -05003136 public long getTimeout() {
3137 return mTimeout;
3138 }
3139
3140 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003141 * Returns the duration from posting after which this notification should be canceled by the
3142 * system, if it's not canceled already.
3143 */
3144 public long getTimeoutAfter() {
3145 return mTimeout;
3146 }
3147
3148 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003149 * Returns what icon should be shown for this notification if it is being displayed in a
3150 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
3151 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
3152 */
3153 public int getBadgeIconType() {
3154 return mBadgeIcon;
3155 }
3156
3157 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003158 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04003159 *
3160 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
3161 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003162 */
3163 public String getShortcutId() {
3164 return mShortcutId;
3165 }
3166
Felipe Leme90205ef2019-03-05 09:59:52 -08003167 /**
3168 * Gets the {@link LocusId} associated with this notification.
3169 *
Felipe Leme8c2360b2019-04-17 11:16:17 -07003170 * <p>Used by the Android system to correlate objects (such as
3171 * {@link ShortcutInfo} and {@link ContentCaptureContext}).
Felipe Leme90205ef2019-03-05 09:59:52 -08003172 */
3173 @Nullable
3174 public LocusId getLocusId() {
3175 return mLocusId;
3176 }
Julia Reynolds3aedded2017-03-31 14:42:09 -04003177
3178 /**
3179 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
3180 */
3181 public CharSequence getSettingsText() {
3182 return mSettingsText;
3183 }
3184
Julia Reynolds13d898c2017-02-02 12:22:05 -05003185 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003186 * Returns which type of notifications in a group are responsible for audibly alerting the
3187 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
3188 * {@link #GROUP_ALERT_SUMMARY}.
3189 */
3190 public @GroupAlertBehavior int getGroupAlertBehavior() {
3191 return mGroupAlertBehavior;
3192 }
3193
3194 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003195 * Returns the bubble metadata that will be used to display app content in a floating window
3196 * over the existing foreground activity.
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003197 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08003198 @Nullable
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003199 public BubbleMetadata getBubbleMetadata() {
3200 return mBubbleMetadata;
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003201 }
3202
Gustav Sennton79ebf4b2018-12-19 10:31:51 +00003203 /**
3204 * Returns whether the platform is allowed (by the app developer) to generate contextual actions
3205 * for this notification.
3206 */
Gustav Sennton761884c2018-11-19 17:40:19 +00003207 public boolean getAllowSystemGeneratedContextualActions() {
3208 return mAllowSystemGeneratedContextualActions;
3209 }
3210
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003211 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003212 * The small icon representing this notification in the status bar and content view.
3213 *
3214 * @return the small icon representing this notification.
3215 *
3216 * @see Builder#getSmallIcon()
3217 * @see Builder#setSmallIcon(Icon)
3218 */
3219 public Icon getSmallIcon() {
3220 return mSmallIcon;
3221 }
3222
3223 /**
3224 * Used when notifying to clean up legacy small icons.
3225 * @hide
3226 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003227 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04003228 public void setSmallIcon(Icon icon) {
3229 mSmallIcon = icon;
3230 }
3231
3232 /**
3233 * The large icon shown in this notification's content view.
3234 * @see Builder#getLargeIcon()
3235 * @see Builder#setLargeIcon(Icon)
3236 */
3237 public Icon getLargeIcon() {
3238 return mLargeIcon;
3239 }
3240
3241 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02003242 * @hide
3243 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003244 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003245 public boolean isGroupSummary() {
3246 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
3247 }
3248
3249 /**
3250 * @hide
3251 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003252 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003253 public boolean isGroupChild() {
3254 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
3255 }
3256
3257 /**
Julia Reynolds30203152017-05-26 13:36:31 -04003258 * @hide
3259 */
3260 public boolean suppressAlertingDueToGrouping() {
3261 if (isGroupSummary()
3262 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
3263 return true;
3264 } else if (isGroupChild()
3265 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
3266 return true;
3267 }
3268 return false;
3269 }
3270
Tony Mak638430e2018-10-08 19:19:10 +01003271
3272 /**
3273 * Finds and returns a remote input and its corresponding action.
3274 *
3275 * @param requiresFreeform requires the remoteinput to allow freeform or not.
3276 * @return the result pair, {@code null} if no result is found.
3277 *
3278 * @hide
3279 */
3280 @Nullable
3281 public Pair<RemoteInput, Action> findRemoteInputActionPair(boolean requiresFreeform) {
3282 if (actions == null) {
3283 return null;
3284 }
3285 for (Notification.Action action : actions) {
3286 if (action.getRemoteInputs() == null) {
3287 continue;
3288 }
3289 RemoteInput resultRemoteInput = null;
3290 for (RemoteInput remoteInput : action.getRemoteInputs()) {
3291 if (remoteInput.getAllowFreeFormInput() || !requiresFreeform) {
3292 resultRemoteInput = remoteInput;
3293 }
3294 }
3295 if (resultRemoteInput != null) {
3296 return Pair.create(resultRemoteInput, action);
3297 }
3298 }
3299 return null;
3300 }
3301
Julia Reynolds30203152017-05-26 13:36:31 -04003302 /**
Gustav Sennton005d7a02019-01-04 13:41:32 +00003303 * Returns the actions that are contextual out of the actions in this notification.
Gustav Senntoneab53682018-11-01 16:30:23 +00003304 *
3305 * @hide
3306 */
3307 public List<Notification.Action> getContextualActions() {
3308 if (actions == null) return Collections.emptyList();
3309
3310 List<Notification.Action> contextualActions = new ArrayList<>();
3311 for (Notification.Action action : actions) {
Gustav Sennton005d7a02019-01-04 13:41:32 +00003312 if (action.isContextual()) {
Gustav Senntoneab53682018-11-01 16:30:23 +00003313 contextualActions.add(action);
3314 }
3315 }
3316 return contextualActions;
3317 }
3318
3319 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003320 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08003321 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003322 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07003323 * content views using the platform's notification layout template. If your app supports
3324 * versions of Android as old as API level 4, you can instead use
3325 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
3326 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
3327 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08003328 *
Scott Main183bf112012-08-13 19:12:13 -07003329 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08003330 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003331 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07003332 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003333 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3334 * .setContentText(subject)
3335 * .setSmallIcon(R.drawable.new_mail)
3336 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003337 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003338 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08003339 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003340 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003341 /**
3342 * @hide
3343 */
3344 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
3345 "android.rebuild.contentViewActionCount";
3346 /**
3347 * @hide
3348 */
3349 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
3350 = "android.rebuild.bigViewActionCount";
3351 /**
3352 * @hide
3353 */
3354 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
3355 = "android.rebuild.hudViewActionCount";
3356
Selim Cinek6743c0b2017-01-18 18:24:01 -08003357 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
3358 SystemProperties.getBoolean("notifications.only_title", true);
3359
Selim Cinek389edcd2017-05-11 19:16:44 -07003360 /**
3361 * The lightness difference that has to be added to the primary text color to obtain the
3362 * secondary text color when the background is light.
3363 */
3364 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
3365
3366 /**
3367 * The lightness difference that has to be added to the primary text color to obtain the
3368 * secondary text color when the background is dark.
3369 * A bit less then the above value, since it looks better on dark backgrounds.
3370 */
3371 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
3372
Joe Onorato46439ce2010-11-19 13:56:21 -08003373 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003374 private Notification mN;
3375 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003376 private Style mStyle;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003377 @UnsupportedAppUsage
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003378 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Selim Cineke7238dd2017-12-14 17:48:32 -08003379 private ArrayList<Person> mPersonList = new ArrayList<>();
Lucas Dupina291d192018-06-07 13:59:42 -07003380 private ContrastColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08003381 private boolean mIsLegacy;
3382 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02003383
3384 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08003385 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
3386 */
3387 private int mCachedContrastColor = COLOR_INVALID;
3388 private int mCachedContrastColorIsFor = COLOR_INVALID;
Selim Cinekc3fec682019-06-06 18:11:07 -07003389
Selim Cinek4717d862018-04-19 09:19:15 +08003390 /**
3391 * A neutral color color that can be used for icons.
3392 */
3393 private int mNeutralColor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08003394
3395 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08003396 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
3397 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
3398 */
3399 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08003400 private int mTextColorsAreForBackground = COLOR_INVALID;
3401 private int mPrimaryTextColor = COLOR_INVALID;
3402 private int mSecondaryTextColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07003403 private int mBackgroundColor = COLOR_INVALID;
3404 private int mForegroundColor = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07003405 /**
3406 * A temporary location where actions are stored. If != null the view originally has action
3407 * but doesn't have any for this inflation.
3408 */
3409 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07003410 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003411
Anthony Chenad4d1582017-04-10 16:07:58 -07003412 private boolean mTintActionButtons;
3413 private boolean mInNightMode;
3414
Adrian Roos70d7aa32017-01-11 15:39:06 -08003415 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003416 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08003417 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003418 * @param context
3419 * A {@link Context} that will be used by the Builder to construct the
3420 * RemoteViews. The Context will not be held past the lifetime of this Builder
3421 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003422 * @param channelId
3423 * The constructed Notification will be posted on this
3424 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
3425 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003426 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003427 public Builder(Context context, String channelId) {
3428 this(context, (Notification) null);
3429 mN.mChannelId = channelId;
3430 }
3431
3432 /**
3433 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
3434 * instead. All posted Notifications must specify a NotificationChannel Id.
3435 */
3436 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003437 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003438 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003439 }
3440
Joe Onoratocb109a02011-01-18 17:57:41 -08003441 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003442 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02003443 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003444 public Builder(Context context, Notification toAdopt) {
3445 mContext = context;
Anthony Chenad4d1582017-04-10 16:07:58 -07003446 Resources res = mContext.getResources();
3447 mTintActionButtons = res.getBoolean(R.bool.config_tintNotificationActionButtons);
3448
3449 if (res.getBoolean(R.bool.config_enableNightMode)) {
3450 Configuration currentConfig = res.getConfiguration();
3451 mInNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
3452 == Configuration.UI_MODE_NIGHT_YES;
3453 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003454
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003455 if (toAdopt == null) {
3456 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003457 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
3458 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
3459 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003460 mN.priority = PRIORITY_DEFAULT;
3461 mN.visibility = VISIBILITY_PRIVATE;
3462 } else {
3463 mN = toAdopt;
3464 if (mN.actions != null) {
3465 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003466 }
3467
Selim Cineke7238dd2017-12-14 17:48:32 -08003468 if (mN.extras.containsKey(EXTRA_PEOPLE_LIST)) {
3469 ArrayList<Person> people = mN.extras.getParcelableArrayList(EXTRA_PEOPLE_LIST);
3470 mPersonList.addAll(people);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003471 }
3472
Selim Cinek4ac6f602016-06-13 15:47:03 -07003473 if (mN.getSmallIcon() == null && mN.icon != 0) {
3474 setSmallIcon(mN.icon);
3475 }
3476
3477 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
3478 setLargeIcon(mN.largeIcon);
3479 }
3480
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003481 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
3482 if (!TextUtils.isEmpty(templateClass)) {
3483 final Class<? extends Style> styleClass
3484 = getNotificationStyleClass(templateClass);
3485 if (styleClass == null) {
3486 Log.d(TAG, "Unknown style class: " + templateClass);
3487 } else {
3488 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003489 final Constructor<? extends Style> ctor =
3490 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003491 ctor.setAccessible(true);
3492 final Style style = ctor.newInstance();
3493 style.restoreFromExtras(mN.extras);
3494
3495 if (style != null) {
3496 setStyle(style);
3497 }
3498 } catch (Throwable t) {
3499 Log.e(TAG, "Could not create Style", t);
3500 }
3501 }
3502 }
3503
3504 }
3505 }
3506
Lucas Dupina291d192018-06-07 13:59:42 -07003507 private ContrastColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08003508 if (mColorUtil == null) {
Lucas Dupina291d192018-06-07 13:59:42 -07003509 mColorUtil = ContrastColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003510 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003511 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003512 }
3513
3514 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003515 * If this notification is duplicative of a Launcher shortcut, sets the
3516 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
3517 * the shortcut.
3518 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04003519 * This field will be ignored by Launchers that don't support badging, don't show
3520 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003521 *
3522 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
3523 * supersedes
3524 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003525 @NonNull
Julia Reynolds13d898c2017-02-02 12:22:05 -05003526 public Builder setShortcutId(String shortcutId) {
3527 mN.mShortcutId = shortcutId;
3528 return this;
3529 }
3530
3531 /**
Felipe Leme90205ef2019-03-05 09:59:52 -08003532 * Sets the {@link LocusId} associated with this notification.
3533 *
3534 * <p>This method should be called when the {@link LocusId} is used in other places (such
Felipe Leme8c2360b2019-04-17 11:16:17 -07003535 * as {@link ShortcutInfo} and {@link ContentCaptureContext}) so the Android system can
3536 * correlate them.
Felipe Leme90205ef2019-03-05 09:59:52 -08003537 */
3538 @NonNull
3539 public Builder setLocusId(@Nullable LocusId locusId) {
3540 mN.mLocusId = locusId;
3541 return this;
3542 }
3543
3544 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003545 * Sets which icon to display as a badge for this notification.
3546 *
3547 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
3548 * {@link #BADGE_ICON_LARGE}.
3549 *
3550 * Note: This value might be ignored, for launchers that don't support badge icons.
3551 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003552 @NonNull
Julia Reynolds612beb22017-03-30 10:48:30 -04003553 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04003554 mN.mBadgeIcon = icon;
3555 return this;
3556 }
3557
3558 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003559 * Sets the group alert behavior for this notification. Use this method to mute this
3560 * notification if alerts for this notification's group should be handled by a different
3561 * notification. This is only applicable for notifications that belong to a
Julia Reynolds399d9bf2017-08-11 12:52:14 -04003562 * {@link #setGroup(String) group}. This must be called on all notifications you want to
3563 * mute. For example, if you want only the summary of your group to make noise, all
3564 * children in the group should have the group alert behavior {@link #GROUP_ALERT_SUMMARY}.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003565 *
3566 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
3567 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003568 @NonNull
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003569 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
3570 mN.mGroupAlertBehavior = groupAlertBehavior;
3571 return this;
3572 }
3573
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003574 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003575 * Sets the {@link BubbleMetadata} that will be used to display app content in a floating
3576 * window over the existing foreground activity.
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003577 *
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003578 * <p>This data will be ignored unless the notification is posted to a channel that
3579 * allows {@link NotificationChannel#canBubble() bubbles}.</p>
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003580 *
Mady Mellor65dcaa92019-04-03 12:21:44 -07003581 * <p>Notifications allowed to bubble that have valid bubble metadata will display in
3582 * collapsed state outside of the notification shade on unlocked devices. When a user
3583 * interacts with the collapsed state, the bubble intent will be invoked and displayed.</p>
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003584 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003585 @NonNull
Mady Mellorcf8f1b22019-03-07 14:08:21 -08003586 public Builder setBubbleMetadata(@Nullable BubbleMetadata data) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003587 mN.mBubbleMetadata = data;
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003588 return this;
3589 }
3590
Jeff Sharkey000ce802017-04-29 13:13:27 -06003591 /** @removed */
3592 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003593 public Builder setChannel(String channelId) {
3594 mN.mChannelId = channelId;
3595 return this;
3596 }
3597
3598 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003599 * Specifies the channel the notification should be delivered on.
3600 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003601 @NonNull
Julia Reynoldsbad42972017-04-25 13:52:49 -04003602 public Builder setChannelId(String channelId) {
3603 mN.mChannelId = channelId;
3604 return this;
3605 }
3606
Jeff Sharkey000ce802017-04-29 13:13:27 -06003607 /** @removed */
3608 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05003609 public Builder setTimeout(long durationMs) {
3610 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05003611 return this;
3612 }
3613
3614 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003615 * Specifies a duration in milliseconds after which this notification should be canceled,
3616 * if it is not already canceled.
3617 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003618 @NonNull
Julia Reynoldsbad42972017-04-25 13:52:49 -04003619 public Builder setTimeoutAfter(long durationMs) {
3620 mN.mTimeout = durationMs;
3621 return this;
3622 }
3623
3624 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003625 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003626 *
3627 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
3628 * shown anymore by default and must be opted into by using
3629 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003630 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003631 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08003632 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003633 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003634 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003635 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08003636 return this;
3637 }
3638
Joe Onoratocb109a02011-01-18 17:57:41 -08003639 /**
Griff Hazen50c11652014-05-16 09:46:31 -07003640 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07003641 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003642 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
3643 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07003644 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003645 @NonNull
Daniel Sandler0c890492012-09-12 17:23:10 -07003646 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003647 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07003648 return this;
3649 }
3650
3651 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003652 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08003653 *
3654 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003655 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003656 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003657 * Useful when showing an elapsed time (like an ongoing phone call).
3658 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08003659 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07003660 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08003661 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003662 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003663 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07003664 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003665 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003666 @NonNull
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003667 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003668 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003669 return this;
3670 }
3671
3672 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08003673 * Sets the Chronometer to count down instead of counting up.
3674 *
3675 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
3676 * If it isn't set the chronometer will count up.
3677 *
3678 * @see #setUsesChronometer(boolean)
3679 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003680 @NonNull
Adrian Roos96b7e202016-05-17 13:50:38 -07003681 public Builder setChronometerCountDown(boolean countDown) {
3682 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003683 return this;
3684 }
3685
3686 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003687 * Set the small icon resource, which will be used to represent the notification in the
3688 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003689 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003690
3691 * The platform template for the expanded view will draw this icon in the left, unless a
3692 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3693 * icon will be moved to the right-hand side.
3694 *
3695
3696 * @param icon
3697 * A resource ID in the application's package of the drawable to use.
3698 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003699 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003700 @NonNull
Tor Norbye7b9c9122013-05-30 16:48:33 -07003701 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003702 return setSmallIcon(icon != 0
3703 ? Icon.createWithResource(mContext, icon)
3704 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003705 }
3706
Joe Onoratocb109a02011-01-18 17:57:41 -08003707 /**
3708 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3709 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3710 * LevelListDrawable}.
3711 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003712 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003713 * @param level The level to use for the icon.
3714 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003715 * @see Notification#icon
3716 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003717 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003718 @NonNull
Tor Norbye7b9c9122013-05-30 16:48:33 -07003719 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003720 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003721 return setSmallIcon(icon);
3722 }
3723
3724 /**
3725 * Set the small icon, which will be used to represent the notification in the
koprivadebd4ee2018-09-13 10:59:46 -07003726 * status bar and content view (unless overridden there by a
Dan Sandlerd63f9322015-05-06 15:18:49 -04003727 * {@link #setLargeIcon(Bitmap) large icon}).
3728 *
3729 * @param icon An Icon object to use.
3730 * @see Notification#icon
3731 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003732 @NonNull
Dan Sandlerd63f9322015-05-06 15:18:49 -04003733 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003734 mN.setSmallIcon(icon);
3735 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3736 mN.icon = icon.getResId();
3737 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003738 return this;
3739 }
3740
Joe Onoratocb109a02011-01-18 17:57:41 -08003741 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003742 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003743 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003744 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003745 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003746 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003747 return this;
3748 }
3749
Joe Onoratocb109a02011-01-18 17:57:41 -08003750 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003751 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003752 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003753 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003754 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003755 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003756 return this;
3757 }
3758
Joe Onoratocb109a02011-01-18 17:57:41 -08003759 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003760 * This provides some additional information that is displayed in the notification. No
3761 * guarantees are given where exactly it is displayed.
3762 *
3763 * <p>This information should only be provided if it provides an essential
3764 * benefit to the understanding of the notification. The more text you provide the
3765 * less readable it becomes. For example, an email client should only provide the account
3766 * name here if more than one email account has been added.</p>
3767 *
3768 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3769 * notification header area.
3770 *
3771 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3772 * this will be shown in the third line of text in the platform notification template.
3773 * You should not be using {@link #setProgress(int, int, boolean)} at the
3774 * same time on those versions; they occupy the same place.
3775 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003776 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003777 @NonNull
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003778 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003779 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003780 return this;
3781 }
3782
3783 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003784 * Provides text that will appear as a link to your application's settings.
3785 *
3786 * <p>This text does not appear within notification {@link Style templates} but may
3787 * appear when the user uses an affordance to learn more about the notification.
3788 * Additionally, this text will not appear unless you provide a valid link target by
3789 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3790 *
3791 * <p>This text is meant to be concise description about what the user can customize
3792 * when they click on this link. The recommended maximum length is 40 characters.
3793 * @param text
3794 * @return
3795 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003796 @NonNull
Julia Reynolds3aedded2017-03-31 14:42:09 -04003797 public Builder setSettingsText(CharSequence text) {
3798 mN.mSettingsText = safeCharSequence(text);
3799 return this;
3800 }
3801
3802 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003803 * Set the remote input history.
3804 *
3805 * This should be set to the most recent inputs that have been sent
3806 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3807 * longer relevant (e.g. for chat notifications once the other party has responded).
3808 *
3809 * The most recent input must be stored at the 0 index, the second most recent at the
3810 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3811 * and how much of each individual input is shown.
3812 *
3813 * <p>Note: The reply text will only be shown on notifications that have least one action
3814 * with a {@code RemoteInput}.</p>
3815 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003816 @NonNull
Adrian Roose458aa82015-12-08 16:17:19 -08003817 public Builder setRemoteInputHistory(CharSequence[] text) {
3818 if (text == null) {
3819 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3820 } else {
3821 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3822 CharSequence[] safe = new CharSequence[N];
3823 for (int i = 0; i < N; i++) {
3824 safe[i] = safeCharSequence(text[i]);
3825 }
3826 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3827 }
3828 return this;
3829 }
3830
3831 /**
Kenny Guya0f6de82018-04-06 16:20:16 +01003832 * Sets whether remote history entries view should have a spinner.
3833 * @hide
3834 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003835 @NonNull
Kenny Guya0f6de82018-04-06 16:20:16 +01003836 public Builder setShowRemoteInputSpinner(boolean showSpinner) {
3837 mN.extras.putBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER, showSpinner);
3838 return this;
3839 }
3840
3841 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +01003842 * Sets whether smart reply buttons should be hidden.
3843 * @hide
3844 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003845 @NonNull
Kenny Guy8cc15d22018-05-09 09:50:55 +01003846 public Builder setHideSmartReplies(boolean hideSmartReplies) {
3847 mN.extras.putBoolean(EXTRA_HIDE_SMART_REPLIES, hideSmartReplies);
3848 return this;
3849 }
3850
3851 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003852 * Sets the number of items this notification represents. May be displayed as a badge count
3853 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003854 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003855 @NonNull
Joe Onorato8595a3d2010-11-19 18:12:07 -08003856 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003857 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003858 return this;
3859 }
3860
Joe Onoratocb109a02011-01-18 17:57:41 -08003861 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003862 * A small piece of additional information pertaining to this notification.
3863 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003864 * The platform template will draw this on the last line of the notification, at the far
3865 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003866 *
3867 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3868 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3869 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003870 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003871 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003872 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003873 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003874 return this;
3875 }
3876
Joe Onoratocb109a02011-01-18 17:57:41 -08003877 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003878 * Set the progress this notification represents.
3879 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003880 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003881 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003882 @NonNull
Jeff Sharkey1c400132011-08-05 14:50:13 -07003883 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003884 mN.extras.putInt(EXTRA_PROGRESS, progress);
3885 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3886 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003887 return this;
3888 }
3889
3890 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003891 * Supply a custom RemoteViews to use instead of the platform template.
3892 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003893 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003894 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003895 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003896 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003897 return setCustomContentView(views);
3898 }
3899
3900 /**
3901 * Supply custom RemoteViews to use instead of the platform template.
3902 *
3903 * This will override the layout that would otherwise be constructed by this Builder
3904 * object.
3905 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003906 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003907 public Builder setCustomContentView(RemoteViews contentView) {
3908 mN.contentView = contentView;
3909 return this;
3910 }
3911
3912 /**
3913 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3914 *
3915 * This will override the expanded layout that would otherwise be constructed by this
3916 * Builder object.
3917 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003918 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003919 public Builder setCustomBigContentView(RemoteViews contentView) {
3920 mN.bigContentView = contentView;
3921 return this;
3922 }
3923
3924 /**
3925 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3926 *
3927 * This will override the heads-up layout that would otherwise be constructed by this
3928 * Builder object.
3929 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003930 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003931 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3932 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003933 return this;
3934 }
3935
Joe Onoratocb109a02011-01-18 17:57:41 -08003936 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003937 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3938 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003939 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3940 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3941 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003942 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003943 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003944 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003945 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003946 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003947 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003948 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003949 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003950 return this;
3951 }
3952
Joe Onoratocb109a02011-01-18 17:57:41 -08003953 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003954 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3955 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003956 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003957 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003958 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003959 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003960 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003961 return this;
3962 }
3963
Joe Onoratocb109a02011-01-18 17:57:41 -08003964 /**
3965 * An intent to launch instead of posting the notification to the status bar.
3966 * Only for use with extremely high-priority notifications demanding the user's
3967 * <strong>immediate</strong> attention, such as an incoming phone call or
3968 * alarm clock that the user has explicitly set to a particular time.
3969 * If this facility is used for something else, please give the user an option
3970 * to turn it off and use a normal notification, as this can be extremely
3971 * disruptive.
3972 *
Chris Wren47c20a12014-06-18 17:27:29 -04003973 * <p>
3974 * The system UI may choose to display a heads-up notification, instead of
3975 * launching this intent, while the user is using the device.
3976 * </p>
Julia Reynolds47fd15f2018-11-28 10:16:00 -05003977 * <p>Apps targeting {@link Build.VERSION_CODES#Q} and above will have to request
3978 * a permission ({@link android.Manifest.permission#USE_FULL_SCREEN_INTENT}) in order to
3979 * use full screen intents.</p>
Chris Wren47c20a12014-06-18 17:27:29 -04003980 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003981 * @param intent The pending intent to launch.
3982 * @param highPriority Passing true will cause this notification to be sent
3983 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003984 *
3985 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003986 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003987 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003988 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003989 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003990 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3991 return this;
3992 }
3993
Joe Onoratocb109a02011-01-18 17:57:41 -08003994 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003995 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003996 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003997 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003998 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003999 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08004000 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004001 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08004002 return this;
4003 }
4004
Joe Onoratocb109a02011-01-18 17:57:41 -08004005 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04004006 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004007 *
Joe Onoratocb109a02011-01-18 17:57:41 -08004008 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04004009 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004010 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004011 setTicker(tickerText);
4012 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08004013 return this;
4014 }
4015
Joe Onoratocb109a02011-01-18 17:57:41 -08004016 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04004017 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004018 *
4019 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04004020 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
4021 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04004022 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004023 @NonNull
Dan Sandlerd63f9322015-05-06 15:18:49 -04004024 public Builder setLargeIcon(Bitmap b) {
4025 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
4026 }
4027
4028 /**
4029 * Add a large icon to the notification content view.
4030 *
4031 * In the platform template, this image will be shown on the left of the notification view
4032 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
4033 * badge atop the large icon).
4034 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004035 @NonNull
Dan Sandlerd63f9322015-05-06 15:18:49 -04004036 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004037 mN.mLargeIcon = icon;
4038 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08004039 return this;
4040 }
4041
Joe Onoratocb109a02011-01-18 17:57:41 -08004042 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004043 * Set the sound to play.
4044 *
John Spurlockc0650f022014-07-19 13:22:39 -04004045 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
4046 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004047 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004048 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08004049 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004050 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08004051 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004052 mN.sound = sound;
4053 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08004054 return this;
4055 }
4056
Joe Onoratocb109a02011-01-18 17:57:41 -08004057 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004058 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08004059 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004060 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
4061 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004062 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08004063 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07004064 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004065 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08004066 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004067 mN.sound = sound;
4068 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08004069 return this;
4070 }
4071
Joe Onoratocb109a02011-01-18 17:57:41 -08004072 /**
John Spurlockc0650f022014-07-19 13:22:39 -04004073 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
4074 * use during playback.
4075 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004076 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04004077 * @see Notification#sound
4078 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004079 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04004080 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004081 mN.sound = sound;
4082 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04004083 return this;
4084 }
4085
4086 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08004087 * Set the vibration pattern to use.
4088 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004089 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
4090 * <code>pattern</code> parameter.
4091 *
Chris Wren47c20a12014-06-18 17:27:29 -04004092 * <p>
4093 * A notification that vibrates is more likely to be presented as a heads-up notification.
4094 * </p>
4095 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004096 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004097 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08004098 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004099 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004100 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004101 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08004102 return this;
4103 }
4104
Joe Onoratocb109a02011-01-18 17:57:41 -08004105 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004106 * Set the desired color for the indicator LED on the device, as well as the
4107 * blink duty cycle (specified in milliseconds).
4108 *
4109
4110 * Not all devices will honor all (or even any) of these values.
4111 *
Julia Reynolds529e3322017-02-06 08:33:01 -05004112 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004113 * @see Notification#ledARGB
4114 * @see Notification#ledOnMS
4115 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08004116 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004117 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08004118 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004119 mN.ledARGB = argb;
4120 mN.ledOnMS = onMs;
4121 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004122 if (onMs != 0 || offMs != 0) {
4123 mN.flags |= FLAG_SHOW_LIGHTS;
4124 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004125 return this;
4126 }
4127
Joe Onoratocb109a02011-01-18 17:57:41 -08004128 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004129 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08004130 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004131
4132 * Ongoing notifications cannot be dismissed by the user, so your application or service
4133 * must take care of canceling them.
4134 *
4135
4136 * They are typically used to indicate a background task that the user is actively engaged
4137 * with (e.g., playing music) or is pending in some way and therefore occupying the device
4138 * (e.g., a file download, sync operation, active network connection).
4139 *
4140
4141 * @see Notification#FLAG_ONGOING_EVENT
Joe Onoratocb109a02011-01-18 17:57:41 -08004142 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004143 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08004144 public Builder setOngoing(boolean ongoing) {
4145 setFlag(FLAG_ONGOING_EVENT, ongoing);
4146 return this;
4147 }
4148
Joe Onoratocb109a02011-01-18 17:57:41 -08004149 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08004150 * Set whether this notification should be colorized. When set, the color set with
4151 * {@link #setColor(int)} will be used as the background color of this notification.
4152 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08004153 * This should only be used for high priority ongoing tasks like navigation, an ongoing
4154 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08004155 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07004156 * For most styles, the coloring will only be applied if the notification is for a
4157 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08004158 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07004159 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08004160 *
Aurimas Liutikas7f695332018-05-31 21:07:32 -07004161 * @see #setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08004162 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08004163 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004164 @NonNull
Selim Cinek7b9605b2017-01-19 17:36:00 -08004165 public Builder setColorized(boolean colorize) {
4166 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
4167 return this;
4168 }
4169
4170 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08004171 * Set this flag if you would only like the sound, vibrate
4172 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004173 *
4174 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08004175 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004176 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08004177 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
4178 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
4179 return this;
4180 }
4181
Joe Onoratocb109a02011-01-18 17:57:41 -08004182 /**
Julia Reynolds04499532016-09-13 14:04:53 -04004183 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004184 *
4185 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08004186 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004187 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08004188 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08004189 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08004190 return this;
4191 }
4192
Joe Onoratocb109a02011-01-18 17:57:41 -08004193 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08004194 * Set whether or not this notification should not bridge to other devices.
4195 *
4196 * <p>Some notifications can be bridged to other devices for remote display.
4197 * This hint can be set to recommend this notification not be bridged.
4198 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004199 @NonNull
Griff Hazendfcb0802014-02-11 12:00:00 -08004200 public Builder setLocalOnly(boolean localOnly) {
4201 setFlag(FLAG_LOCAL_ONLY, localOnly);
4202 return this;
4203 }
4204
4205 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004206 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08004207 * <p>
4208 * The value should be one or more of the following fields combined with
4209 * bitwise-or:
4210 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
4211 * <p>
4212 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004213 *
4214 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05004215 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004216 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08004217 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004218 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004219 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004220 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08004221 return this;
4222 }
4223
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004224 /**
4225 * Set the priority of this notification.
4226 *
4227 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004228 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004229 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004230 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07004231 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004232 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004233 return this;
4234 }
Joe Malin8d40d042012-11-05 11:36:40 -08004235
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004236 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04004237 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08004238 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04004239 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004240 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004241 @NonNull
John Spurlockfd7f1e02014-03-18 16:41:57 -04004242 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004243 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004244 return this;
4245 }
4246
4247 /**
Chris Wrendde75302014-03-26 17:24:15 -04004248 * Add a person that is relevant to this notification.
4249 *
Chris Wrene6c48932014-09-29 17:19:27 -04004250 * <P>
4251 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04004252 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4253 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4254 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04004255 * </P>
4256 *
4257 * <P>
4258 * The person should be specified by the {@code String} representation of a
4259 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
4260 * </P>
4261 *
4262 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
4263 * URIs. The path part of these URIs must exist in the contacts database, in the
4264 * appropriate column, or the reference will be discarded as invalid. Telephone schema
4265 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
Selim Cineke7238dd2017-12-14 17:48:32 -08004266 * It is also possible to provide a URI with the schema {@code name:} in order to uniquely
4267 * identify a person without an entry in the contacts database.
Chris Wrene6c48932014-09-29 17:19:27 -04004268 * </P>
4269 *
4270 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04004271 * @see Notification#EXTRA_PEOPLE
Selim Cineke7238dd2017-12-14 17:48:32 -08004272 * @deprecated use {@link #addPerson(Person)}
Chris Wrendde75302014-03-26 17:24:15 -04004273 */
Chris Wrene6c48932014-09-29 17:19:27 -04004274 public Builder addPerson(String uri) {
Selim Cinek9acd6732018-03-23 16:39:02 -07004275 addPerson(new Person.Builder().setUri(uri).build());
Selim Cineke7238dd2017-12-14 17:48:32 -08004276 return this;
4277 }
4278
4279 /**
4280 * Add a person that is relevant to this notification.
4281 *
4282 * <P>
4283 * Depending on user preferences, this annotation may allow the notification to pass
4284 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4285 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4286 * appear more prominently in the user interface.
4287 * </P>
4288 *
4289 * <P>
4290 * A person should usually contain a uri in order to benefit from the ranking boost.
4291 * However, even if no uri is provided, it's beneficial to provide other people in the
4292 * notification, such that listeners and voice only devices can announce and handle them
4293 * properly.
4294 * </P>
4295 *
4296 * @param person the person to add.
4297 * @see Notification#EXTRA_PEOPLE_LIST
4298 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004299 @NonNull
Selim Cineke7238dd2017-12-14 17:48:32 -08004300 public Builder addPerson(Person person) {
4301 mPersonList.add(person);
Chris Wrendde75302014-03-26 17:24:15 -04004302 return this;
4303 }
4304
4305 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004306 * Set this notification to be part of a group of notifications sharing the same key.
4307 * Grouped notifications may display in a cluster or stack on devices which
4308 * support such rendering.
4309 *
4310 * <p>To make this notification the summary for its group, also call
4311 * {@link #setGroupSummary}. A sort order can be specified for group members by using
4312 * {@link #setSortKey}.
4313 * @param groupKey The group key of the group.
4314 * @return this object for method chaining
4315 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004316 @NonNull
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004317 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004318 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004319 return this;
4320 }
4321
4322 /**
4323 * Set this notification to be the group summary for a group of notifications.
4324 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04004325 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
4326 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004327 * @param isGroupSummary Whether this notification should be a group summary.
4328 * @return this object for method chaining
4329 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004330 @NonNull
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004331 public Builder setGroupSummary(boolean isGroupSummary) {
4332 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
4333 return this;
4334 }
4335
4336 /**
4337 * Set a sort key that orders this notification among other notifications from the
4338 * same package. This can be useful if an external sort was already applied and an app
4339 * would like to preserve this. Notifications will be sorted lexicographically using this
4340 * value, although providing different priorities in addition to providing sort key may
4341 * cause this value to be ignored.
4342 *
4343 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07004344 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004345 *
4346 * @see String#compareTo(String)
4347 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004348 @NonNull
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004349 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004350 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004351 return this;
4352 }
4353
4354 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004355 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004356 *
Griff Hazen720042b2014-02-24 15:46:56 -08004357 * <p>Values within the Bundle will replace existing extras values in this Builder.
4358 *
4359 * @see Notification#extras
4360 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004361 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07004362 public Builder addExtras(Bundle extras) {
4363 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004364 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08004365 }
4366 return this;
4367 }
4368
4369 /**
4370 * Set metadata for this notification.
4371 *
4372 * <p>A reference to the Bundle is held for the lifetime of this Builder, and the Bundle's
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004373 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004374 * called.
4375 *
Griff Hazen720042b2014-02-24 15:46:56 -08004376 * <p>Replaces any existing extras values with those from the provided Bundle.
4377 * Use {@link #addExtras} to merge in metadata instead.
4378 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004379 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004380 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004381 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07004382 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004383 if (extras != null) {
4384 mUserExtras = extras;
4385 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004386 return this;
4387 }
4388
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004389 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004390 * Get the current metadata Bundle used by this notification Builder.
4391 *
4392 * <p>The returned Bundle is shared with this Builder.
4393 *
4394 * <p>The current contents of this Bundle are copied into the Notification each time
4395 * {@link #build()} is called.
4396 *
4397 * @see Notification#extras
4398 */
4399 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004400 return mUserExtras;
4401 }
4402
4403 private Bundle getAllExtras() {
4404 final Bundle saveExtras = (Bundle) mUserExtras.clone();
4405 saveExtras.putAll(mN.extras);
4406 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08004407 }
4408
4409 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004410 * Add an action to this notification. Actions are typically displayed by
4411 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004412 * <p>
4413 * Every action must have an icon (32dp square and matching the
4414 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4415 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4416 * <p>
4417 * A notification in its expanded form can display up to 3 actions, from left to right in
4418 * the order they were added. Actions will not be displayed when the notification is
4419 * collapsed, however, so be sure that any essential functions may be accessed by the user
4420 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004421 *
4422 * @param icon Resource ID of a drawable that represents the action.
4423 * @param title Text describing the action.
4424 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04004425 *
4426 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004427 */
Dan Sandler86647982015-05-13 23:41:13 -04004428 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004429 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004430 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004431 return this;
4432 }
4433
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004434 /**
Griff Hazen959591e2014-05-15 22:26:18 -07004435 * Add an action to this notification. Actions are typically displayed by
4436 * the system as a button adjacent to the notification content.
4437 * <p>
4438 * Every action must have an icon (32dp square and matching the
4439 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4440 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4441 * <p>
4442 * A notification in its expanded form can display up to 3 actions, from left to right in
4443 * the order they were added. Actions will not be displayed when the notification is
4444 * collapsed, however, so be sure that any essential functions may be accessed by the user
4445 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
4446 *
4447 * @param action The action to add.
4448 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004449 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07004450 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08004451 if (action != null) {
4452 mActions.add(action);
4453 }
Griff Hazen959591e2014-05-15 22:26:18 -07004454 return this;
4455 }
4456
4457 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004458 * Alter the complete list of actions attached to this notification.
4459 * @see #addAction(Action).
4460 *
4461 * @param actions
4462 * @return
4463 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004464 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004465 public Builder setActions(Action... actions) {
4466 mActions.clear();
4467 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08004468 if (actions[i] != null) {
4469 mActions.add(actions[i]);
4470 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004471 }
4472 return this;
4473 }
4474
4475 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004476 * Add a rich notification style to be applied at build time.
4477 *
4478 * @param style Object responsible for modifying the notification style.
4479 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004480 @NonNull
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004481 public Builder setStyle(Style style) {
4482 if (mStyle != style) {
4483 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004484 if (mStyle != null) {
4485 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004486 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
4487 } else {
4488 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004489 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004490 }
4491 return this;
4492 }
4493
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004494 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05004495 * Returns the style set by {@link #setStyle(Style)}.
4496 */
4497 public Style getStyle() {
4498 return mStyle;
4499 }
4500
4501 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004502 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07004503 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004504 * @return The same Builder.
4505 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004506 @NonNull
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06004507 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004508 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004509 return this;
4510 }
4511
4512 /**
4513 * Supply a replacement Notification whose contents should be shown in insecure contexts
4514 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
4515 * @param n A replacement notification, presumably with some or all info redacted.
4516 * @return The same Builder.
4517 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004518 @NonNull
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004519 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004520 if (n != null) {
4521 mN.publicVersion = new Notification();
4522 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
4523 } else {
4524 mN.publicVersion = null;
4525 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004526 return this;
4527 }
4528
Griff Hazenb720abe2014-05-20 13:15:30 -07004529 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004530 * Apply an extender to this notification builder. Extenders may be used to add
4531 * metadata or change options on this builder.
4532 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004533 @NonNull
Griff Hazen61a9e862014-05-22 16:05:19 -07004534 public Builder extend(Extender extender) {
4535 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004536 return this;
4537 }
4538
Dan Sandler4e787062015-06-17 15:09:48 -04004539 /**
4540 * @hide
4541 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004542 @NonNull
Julia Reynoldse46bb372016-03-17 11:05:58 -04004543 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08004544 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004545 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004546 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004547 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004548 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04004549 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08004550 }
4551
Dan Sandler26e81cf2014-05-06 10:01:27 -04004552 /**
4553 * Sets {@link Notification#color}.
4554 *
4555 * @param argb The accent color to use
4556 *
4557 * @return The same Builder.
4558 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004559 @NonNull
Tor Norbye80756e32015-03-02 09:39:27 -08004560 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004561 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004562 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04004563 return this;
4564 }
4565
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004566 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04004567 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
4568 // This user can never be a badged profile,
4569 // and also includes USER_ALL system notifications.
4570 return null;
4571 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02004572 // Note: This assumes that the current user can read the profile badge of the
4573 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08004574 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05004575 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004576 }
4577
4578 private Bitmap getProfileBadge() {
4579 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01004580 if (badge == null) {
4581 return null;
4582 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004583 final int size = mContext.getResources().getDimensionPixelSize(
4584 R.dimen.notification_badge_size);
4585 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004586 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004587 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004588 badge.draw(canvas);
4589 return bitmap;
4590 }
4591
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004592 private void bindProfileBadge(RemoteViews contentView, StandardTemplateParams p) {
Kenny Guy98193ea2014-07-24 19:54:37 +01004593 Bitmap profileBadge = getProfileBadge();
4594
Kenny Guy98193ea2014-07-24 19:54:37 +01004595 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004596 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
4597 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004598 if (isColorized(p)) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004599 contentView.setDrawableTint(R.id.profile_badge, false,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004600 getPrimaryTextColor(p), PorterDuff.Mode.SRC_ATOP);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004601 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004602 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004603 }
4604
Gus Prevasbae5ba32018-11-28 15:45:13 -05004605 private void bindAlertedIcon(RemoteViews contentView, StandardTemplateParams p) {
4606 contentView.setDrawableTint(
4607 R.id.alerted_icon,
4608 false /* targetBackground */,
4609 getNeutralColor(p),
4610 PorterDuff.Mode.SRC_ATOP);
4611 }
4612
Julia Reynoldsfc640012018-02-21 12:25:27 -05004613 /**
4614 * @hide
4615 */
4616 public boolean usesStandardHeader() {
4617 if (mN.mUsesStandardHeader) {
4618 return true;
4619 }
4620 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.N) {
4621 if (mN.contentView == null && mN.bigContentView == null) {
4622 return true;
4623 }
4624 }
4625 boolean contentViewUsesHeader = mN.contentView == null
4626 || STANDARD_LAYOUTS.contains(mN.contentView.getLayoutId());
4627 boolean bigContentViewUsesHeader = mN.bigContentView == null
4628 || STANDARD_LAYOUTS.contains(mN.bigContentView.getLayoutId());
4629 return contentViewUsesHeader && bigContentViewUsesHeader;
4630 }
4631
Christoph Studerfe718432014-09-01 18:21:18 +02004632 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004633 resetNotificationHeader(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02004634 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08004635 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004636 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08004637 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004638 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08004639 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08004640 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004641 }
4642
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004643 /**
4644 * Resets the notification header to its original state
4645 */
4646 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07004647 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
4648 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08004649 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004650 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02004651 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004652 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07004653 contentView.setTextViewText(R.id.header_text, null);
Selim Cinekafeed292017-12-12 17:32:44 -08004654 contentView.setViewVisibility(R.id.header_text_secondary, View.GONE);
4655 contentView.setTextViewText(R.id.header_text_secondary, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004656 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinekafeed292017-12-12 17:32:44 -08004657 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004658 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004659 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004660 contentView.setImageViewIcon(R.id.profile_badge, null);
4661 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Gus Prevasa3226492018-10-23 11:10:09 -04004662 contentView.setViewVisibility(R.id.alerted_icon, View.GONE);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004663 mN.mUsesStandardHeader = false;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004664 }
4665
Selim Cinek384804b2018-04-18 14:31:07 +08004666 private RemoteViews applyStandardTemplate(int resId, TemplateBindResult result) {
4667 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this),
4668 result);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004669 }
4670
Selim Cinek384804b2018-04-18 14:31:07 +08004671 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p,
4672 TemplateBindResult result) {
Kenny Guy77320062014-08-27 21:37:15 +01004673 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04004674
Christoph Studerfe718432014-09-01 18:21:18 +02004675 resetStandardTemplate(contentView);
4676
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004677 final Bundle ex = mN.extras;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004678 updateBackgroundColor(contentView, p);
4679 bindNotificationHeader(contentView, p);
Selim Cinek384804b2018-04-18 14:31:07 +08004680 bindLargeIconAndReply(contentView, p, result);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004681 boolean showProgress = handleProgressBar(contentView, ex, p);
Adrian Roos70d7aa32017-01-11 15:39:06 -08004682 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08004683 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004684 contentView.setTextViewText(R.id.title, processTextSpans(p.title));
Lucas Dupin00be88f2019-01-03 17:50:52 -08004685 setTextViewColorPrimary(contentView, R.id.title, p);
Selim Cinek954cc232016-05-20 13:29:23 -07004686 contentView.setViewLayoutWidth(R.id.title, showProgress
4687 ? ViewGroup.LayoutParams.WRAP_CONTENT
4688 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08004689 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08004690 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08004691 int textId = showProgress ? com.android.internal.R.id.text_line_1
4692 : com.android.internal.R.id.text;
Selim Cinek48f66b72017-08-18 16:17:51 -07004693 contentView.setTextViewText(textId, processTextSpans(p.text));
Lucas Dupin00be88f2019-01-03 17:50:52 -08004694 setTextViewColorSecondary(contentView, textId, p);
Selim Cinek41598732016-01-11 16:58:37 -08004695 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08004696 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08004697
Selim Cinek279fa862016-06-14 10:57:25 -07004698 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004699
Selim Cinek29603462015-11-17 19:04:39 -08004700 return contentView;
4701 }
4702
Selim Cinek48f66b72017-08-18 16:17:51 -07004703 private CharSequence processTextSpans(CharSequence text) {
Lucas Dupind3c99322018-09-27 10:22:29 -07004704 if (hasForegroundColor() || mInNightMode) {
Lucas Dupina291d192018-06-07 13:59:42 -07004705 return ContrastColorUtil.clearColorSpans(text);
Selim Cinek48f66b72017-08-18 16:17:51 -07004706 }
4707 return text;
4708 }
4709
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004710 private void setTextViewColorPrimary(RemoteViews contentView, int id,
4711 StandardTemplateParams p) {
4712 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004713 contentView.setTextColor(id, mPrimaryTextColor);
4714 }
4715
Selim Cinek48f66b72017-08-18 16:17:51 -07004716 private boolean hasForegroundColor() {
4717 return mForegroundColor != COLOR_INVALID;
4718 }
4719
Selim Cinek389edcd2017-05-11 19:16:44 -07004720 /**
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004721 * Return the primary text color using the existing template params
Selim Cinek389edcd2017-05-11 19:16:44 -07004722 * @hide
4723 */
4724 @VisibleForTesting
4725 public int getPrimaryTextColor() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004726 return getPrimaryTextColor(mParams);
4727 }
4728
4729 /**
4730 * @param p the template params to inflate this with
4731 * @return the primary text color
4732 * @hide
4733 */
4734 @VisibleForTesting
4735 public int getPrimaryTextColor(StandardTemplateParams p) {
4736 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004737 return mPrimaryTextColor;
4738 }
4739
Selim Cinek389edcd2017-05-11 19:16:44 -07004740 /**
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004741 * Return the secondary text color using the existing template params
Selim Cinek389edcd2017-05-11 19:16:44 -07004742 * @hide
4743 */
4744 @VisibleForTesting
4745 public int getSecondaryTextColor() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004746 return getSecondaryTextColor(mParams);
4747 }
4748
4749 /**
4750 * @param p the template params to inflate this with
4751 * @return the secondary text color
4752 * @hide
4753 */
4754 @VisibleForTesting
4755 public int getSecondaryTextColor(StandardTemplateParams p) {
4756 ensureColors(p);
Selim Cinek389edcd2017-05-11 19:16:44 -07004757 return mSecondaryTextColor;
4758 }
4759
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004760 private void setTextViewColorSecondary(RemoteViews contentView, int id,
4761 StandardTemplateParams p) {
4762 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004763 contentView.setTextColor(id, mSecondaryTextColor);
4764 }
4765
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004766 private void ensureColors(StandardTemplateParams p) {
4767 int backgroundColor = getBackgroundColor(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004768 if (mPrimaryTextColor == COLOR_INVALID
4769 || mSecondaryTextColor == COLOR_INVALID
Selim Cinek7b9605b2017-01-19 17:36:00 -08004770 || mTextColorsAreForBackground != backgroundColor) {
4771 mTextColorsAreForBackground = backgroundColor;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004772 if (!hasForegroundColor() || !isColorized(p)) {
Lucas Dupina291d192018-06-07 13:59:42 -07004773 mPrimaryTextColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004774 backgroundColor, mInNightMode);
Lucas Dupina291d192018-06-07 13:59:42 -07004775 mSecondaryTextColor = ContrastColorUtil.resolveSecondaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004776 backgroundColor, mInNightMode);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004777 if (backgroundColor != COLOR_DEFAULT && isColorized(p)) {
Lucas Dupina291d192018-06-07 13:59:42 -07004778 mPrimaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004779 mPrimaryTextColor, backgroundColor, 4.5);
Lucas Dupina291d192018-06-07 13:59:42 -07004780 mSecondaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004781 mSecondaryTextColor, backgroundColor, 4.5);
4782 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07004783 } else {
Lucas Dupina291d192018-06-07 13:59:42 -07004784 double backLum = ContrastColorUtil.calculateLuminance(backgroundColor);
4785 double textLum = ContrastColorUtil.calculateLuminance(mForegroundColor);
4786 double contrast = ContrastColorUtil.calculateContrast(mForegroundColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004787 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07004788 // We only respect the given colors if worst case Black or White still has
4789 // contrast
4790 boolean backgroundLight = backLum > textLum
4791 && satisfiesTextContrast(backgroundColor, Color.BLACK)
4792 || backLum <= textLum
4793 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004794 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07004795 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004796 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004797 mForegroundColor,
4798 backgroundColor,
4799 true /* findFG */,
4800 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004801 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004802 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004803 } else {
4804 mSecondaryTextColor =
Lucas Dupina291d192018-06-07 13:59:42 -07004805 ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004806 mForegroundColor,
4807 backgroundColor,
4808 true /* findFG */,
4809 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004810 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004811 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004812 }
4813 } else {
4814 mPrimaryTextColor = mForegroundColor;
Lucas Dupina291d192018-06-07 13:59:42 -07004815 mSecondaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004816 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4817 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Lucas Dupina291d192018-06-07 13:59:42 -07004818 if (ContrastColorUtil.calculateContrast(mSecondaryTextColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004819 backgroundColor) < 4.5f) {
4820 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07004821 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004822 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004823 mSecondaryTextColor,
4824 backgroundColor,
4825 true /* findFG */,
4826 4.5f);
4827 } else {
4828 mSecondaryTextColor
Lucas Dupina291d192018-06-07 13:59:42 -07004829 = ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004830 mSecondaryTextColor,
4831 backgroundColor,
4832 true /* findFG */,
4833 4.5f);
4834 }
Lucas Dupina291d192018-06-07 13:59:42 -07004835 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004836 mSecondaryTextColor, backgroundLight
4837 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4838 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004839 }
4840 }
4841 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004842 }
4843 }
4844
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004845 private void updateBackgroundColor(RemoteViews contentView,
4846 StandardTemplateParams p) {
4847 if (isColorized(p)) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004848 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004849 getBackgroundColor(p));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004850 } else {
4851 // Clear it!
4852 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4853 0);
4854 }
4855 }
4856
Selim Cinek860b6da2015-12-16 19:02:19 -08004857 /**
4858 * @param remoteView the remote view to update the minheight in
4859 * @param hasMinHeight does it have a mimHeight
4860 * @hide
4861 */
4862 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4863 int minHeight = 0;
4864 if (hasMinHeight) {
4865 // we need to set the minHeight of the notification
4866 minHeight = mContext.getResources().getDimensionPixelSize(
4867 com.android.internal.R.dimen.notification_min_content_height);
4868 }
4869 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4870 }
4871
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004872 private boolean handleProgressBar(RemoteViews contentView, Bundle ex,
4873 StandardTemplateParams p) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004874 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4875 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4876 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004877 if (p.hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004878 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004879 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004880 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004881 contentView.setProgressBackgroundTintList(
4882 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4883 R.color.notification_progress_background_color)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004884 if (getRawColor(p) != COLOR_DEFAULT) {
Beth Thibodeau38275c32019-05-08 16:36:59 -04004885 int color = isColorized(p) ? getPrimaryTextColor(p) : resolveContrastColor(p);
4886 ColorStateList colorStateList = ColorStateList.valueOf(color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004887 contentView.setProgressTintList(R.id.progress, colorStateList);
4888 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004889 }
Selim Cinek29603462015-11-17 19:04:39 -08004890 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004891 } else {
4892 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004893 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004894 }
Joe Onorato561d3852010-11-20 18:09:34 -08004895 }
4896
Selim Cinek384804b2018-04-18 14:31:07 +08004897 private void bindLargeIconAndReply(RemoteViews contentView, StandardTemplateParams p,
4898 TemplateBindResult result) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004899 boolean largeIconShown = bindLargeIcon(contentView, p);
4900 boolean replyIconShown = bindReplyIcon(contentView, p);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004901 boolean iconContainerVisible = largeIconShown || replyIconShown;
Selim Cinek384804b2018-04-18 14:31:07 +08004902 contentView.setViewVisibility(R.id.right_icon_container,
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004903 iconContainerVisible ? View.VISIBLE : View.GONE);
Selim Cinek1c72fa02018-04-23 18:00:54 +08004904 int marginEnd = calculateMarginEnd(largeIconShown, replyIconShown);
4905 contentView.setViewLayoutMarginEnd(R.id.line1, marginEnd);
4906 contentView.setViewLayoutMarginEnd(R.id.text, marginEnd);
4907 contentView.setViewLayoutMarginEnd(R.id.progress, marginEnd);
Selim Cinek384804b2018-04-18 14:31:07 +08004908 if (result != null) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08004909 result.setIconMarginEnd(marginEnd);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004910 result.setRightIconContainerVisible(iconContainerVisible);
Selim Cinek384804b2018-04-18 14:31:07 +08004911 }
4912 }
4913
Selim Cinek1c72fa02018-04-23 18:00:54 +08004914 private int calculateMarginEnd(boolean largeIconShown, boolean replyIconShown) {
4915 int marginEnd = 0;
4916 int contentMargin = mContext.getResources().getDimensionPixelSize(
4917 R.dimen.notification_content_margin_end);
4918 int iconSize = mContext.getResources().getDimensionPixelSize(
4919 R.dimen.notification_right_icon_size);
4920 if (replyIconShown) {
4921 // The size of the reply icon
4922 marginEnd += iconSize;
4923
4924 int replyInset = mContext.getResources().getDimensionPixelSize(
4925 R.dimen.notification_reply_inset);
4926 // We're subtracting the inset of the reply icon to make sure it's
4927 // aligned nicely on the right, and remove it from the following padding
4928 marginEnd -= replyInset * 2;
4929 }
4930 if (largeIconShown) {
4931 // adding size of the right icon
4932 marginEnd += iconSize;
4933
4934 if (replyIconShown) {
4935 // We also add some padding to the reply icon if it's around
4936 marginEnd += contentMargin;
4937 }
4938 }
4939 if (replyIconShown || largeIconShown) {
4940 // The padding to the content
4941 marginEnd += contentMargin;
4942 }
4943 return marginEnd;
4944 }
4945
Selim Cinek384804b2018-04-18 14:31:07 +08004946 /**
4947 * Bind the large icon.
4948 * @return if the largeIcon is visible
4949 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004950 private boolean bindLargeIcon(RemoteViews contentView, StandardTemplateParams p) {
Selim Cinek279fa862016-06-14 10:57:25 -07004951 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4952 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4953 }
Lucas Dupin00be88f2019-01-03 17:50:52 -08004954 boolean showLargeIcon = mN.mLargeIcon != null && !p.hideLargeIcon;
Selim Cinek88188f22017-09-19 16:46:56 -07004955 if (showLargeIcon) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004956 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4957 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004958 processLargeLegacyIcon(mN.mLargeIcon, contentView, p);
Selim Cinek88188f22017-09-19 16:46:56 -07004959 }
Selim Cinek384804b2018-04-18 14:31:07 +08004960 return showLargeIcon;
4961 }
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004962
Selim Cinek384804b2018-04-18 14:31:07 +08004963 /**
4964 * Bind the reply icon.
4965 * @return if the reply icon is visible
4966 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004967 private boolean bindReplyIcon(RemoteViews contentView, StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08004968 boolean actionVisible = !p.hideReplyIcon;
Selim Cinek384804b2018-04-18 14:31:07 +08004969 Action action = null;
Selim Cinek88188f22017-09-19 16:46:56 -07004970 if (actionVisible) {
Selim Cinek384804b2018-04-18 14:31:07 +08004971 action = findReplyAction();
4972 actionVisible = action != null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004973 }
Selim Cinek384804b2018-04-18 14:31:07 +08004974 if (actionVisible) {
4975 contentView.setViewVisibility(R.id.reply_icon_action, View.VISIBLE);
4976 contentView.setDrawableTint(R.id.reply_icon_action,
4977 false /* targetBackground */,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004978 getNeutralColor(p),
Selim Cinek384804b2018-04-18 14:31:07 +08004979 PorterDuff.Mode.SRC_ATOP);
4980 contentView.setOnClickPendingIntent(R.id.reply_icon_action, action.actionIntent);
4981 contentView.setRemoteInputs(R.id.reply_icon_action, action.mRemoteInputs);
4982 } else {
4983 contentView.setRemoteInputs(R.id.reply_icon_action, null);
4984 }
4985 contentView.setViewVisibility(R.id.reply_icon_action,
4986 actionVisible ? View.VISIBLE : View.GONE);
4987 return actionVisible;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004988 }
4989
4990 private Action findReplyAction() {
4991 ArrayList<Action> actions = mActions;
4992 if (mOriginalActions != null) {
4993 actions = mOriginalActions;
4994 }
4995 int numActions = actions.size();
4996 for (int i = 0; i < numActions; i++) {
4997 Action action = actions.get(i);
4998 if (hasValidRemoteInput(action)) {
4999 return action;
5000 }
5001 }
5002 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005003 }
5004
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005005 private void bindNotificationHeader(RemoteViews contentView, StandardTemplateParams p) {
5006 bindSmallIcon(contentView, p);
5007 bindHeaderAppName(contentView, p);
Lucas Dupin00be88f2019-01-03 17:50:52 -08005008 bindHeaderText(contentView, p);
5009 bindHeaderTextSecondary(contentView, p);
5010 bindHeaderChronometerAndTime(contentView, p);
5011 bindProfileBadge(contentView, p);
5012 bindAlertedIcon(contentView, p);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005013 bindActivePermissions(contentView, p);
5014 bindExpandButton(contentView, p);
Julia Reynoldsfc640012018-02-21 12:25:27 -05005015 mN.mUsesStandardHeader = true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005016 }
5017
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005018 private void bindActivePermissions(RemoteViews contentView, StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005019 int color = getNeutralColor(p);
Julia Reynoldsb887b232018-04-10 16:38:08 -04005020 contentView.setDrawableTint(R.id.camera, false, color, PorterDuff.Mode.SRC_ATOP);
5021 contentView.setDrawableTint(R.id.mic, false, color, PorterDuff.Mode.SRC_ATOP);
5022 contentView.setDrawableTint(R.id.overlay, false, color, PorterDuff.Mode.SRC_ATOP);
5023 }
5024
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005025 private void bindExpandButton(RemoteViews contentView, StandardTemplateParams p) {
5026 int color = isColorized(p) ? getPrimaryTextColor(p) : getSecondaryTextColor(p);
Sunny Goyal5b153922017-09-21 21:00:36 -07005027 contentView.setDrawableTint(R.id.expand_button, false, color,
5028 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005029 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08005030 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005031 }
5032
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005033 private void bindHeaderChronometerAndTime(RemoteViews contentView,
5034 StandardTemplateParams p) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005035 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08005036 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005037 setTextViewColorSecondary(contentView, R.id.time_divider, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005038 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
5039 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
5040 contentView.setLong(R.id.chronometer, "setBase",
5041 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
5042 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07005043 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07005044 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005045 setTextViewColorSecondary(contentView, R.id.chronometer, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005046 } else {
5047 contentView.setViewVisibility(R.id.time, View.VISIBLE);
5048 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005049 setTextViewColorSecondary(contentView, R.id.time, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005050 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005051 } else {
5052 // We still want a time to be set but gone, such that we can show and hide it
5053 // on demand in case it's a child notification without anything in the header
5054 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005055 }
5056 }
5057
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005058 private void bindHeaderText(RemoteViews contentView, StandardTemplateParams p) {
5059 CharSequence summaryText = p.summaryText;
5060 if (summaryText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05005061 && mStyle.hasSummaryInHeader()) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005062 summaryText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05005063 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005064 if (summaryText == null
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07005065 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
5066 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005067 summaryText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07005068 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005069 if (summaryText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005070 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek48f66b72017-08-18 16:17:51 -07005071 contentView.setTextViewText(R.id.header_text, processTextSpans(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005072 processLegacyText(summaryText)));
5073 setTextViewColorSecondary(contentView, R.id.header_text, p);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07005074 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
5075 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005076 setTextViewColorSecondary(contentView, R.id.header_text_divider, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005077 }
5078 }
5079
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005080 private void bindHeaderTextSecondary(RemoteViews contentView, StandardTemplateParams p) {
5081 if (!TextUtils.isEmpty(p.headerTextSecondary)) {
Selim Cinekafeed292017-12-12 17:32:44 -08005082 contentView.setTextViewText(R.id.header_text_secondary, processTextSpans(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005083 processLegacyText(p.headerTextSecondary)));
5084 setTextViewColorSecondary(contentView, R.id.header_text_secondary, p);
Selim Cinekafeed292017-12-12 17:32:44 -08005085 contentView.setViewVisibility(R.id.header_text_secondary, View.VISIBLE);
5086 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005087 setTextViewColorSecondary(contentView, R.id.header_text_secondary_divider, p);
Selim Cinekafeed292017-12-12 17:32:44 -08005088 }
5089 }
5090
Adrian Rooseba05822016-04-22 17:09:27 -07005091 /**
5092 * @hide
5093 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005094 @UnsupportedAppUsage
Adrian Rooseba05822016-04-22 17:09:27 -07005095 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04005096 CharSequence name = null;
5097 final PackageManager pm = mContext.getPackageManager();
5098 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
5099 // only system packages which lump together a bunch of unrelated stuff
5100 // may substitute a different name to make the purpose of the
5101 // notification more clear. the correct package label should always
5102 // be accessible via SystemUI.
5103 final String pkg = mContext.getPackageName();
5104 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
5105 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
5106 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
5107 name = subName;
5108 } else {
5109 Log.w(TAG, "warning: pkg "
5110 + pkg + " attempting to substitute app name '" + subName
5111 + "' without holding perm "
5112 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
5113 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005114 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04005115 if (TextUtils.isEmpty(name)) {
5116 name = pm.getApplicationLabel(mContext.getApplicationInfo());
5117 }
5118 if (TextUtils.isEmpty(name)) {
5119 // still nothing?
5120 return null;
5121 }
5122
5123 return String.valueOf(name);
5124 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005125 private void bindHeaderAppName(RemoteViews contentView, StandardTemplateParams p) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04005126 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005127 if (isColorized(p)) {
5128 setTextViewColorPrimary(contentView, R.id.app_name_text, p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005129 } else {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005130 contentView.setTextColor(R.id.app_name_text, getSecondaryTextColor(p));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005131 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005132 }
5133
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005134 private boolean isColorized(StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005135 return p.allowColorization && mN.isColorized();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005136 }
5137
5138 private void bindSmallIcon(RemoteViews contentView, StandardTemplateParams p) {
Selim Cinek279fa862016-06-14 10:57:25 -07005139 if (mN.mSmallIcon == null && mN.icon != 0) {
5140 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
5141 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005142 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Sunny Goyal5b153922017-09-21 21:00:36 -07005143 contentView.setInt(R.id.icon, "setImageLevel", mN.iconLevel);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005144 processSmallIconColor(mN.mSmallIcon, contentView, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005145 }
5146
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005147 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005148 * @return true if the built notification will show the time or the chronometer; false
5149 * otherwise
5150 */
5151 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07005152 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005153 }
5154
Christoph Studerfe718432014-09-01 18:21:18 +02005155 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07005156 // actions_container is only reset when there are no actions to avoid focus issues with
5157 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02005158 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02005159 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08005160
5161 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
5162 big.setTextViewText(R.id.notification_material_reply_text_1, null);
Kenny Guya0f6de82018-04-06 16:20:16 +01005163 big.setViewVisibility(R.id.notification_material_reply_text_1_container, View.GONE);
5164 big.setViewVisibility(R.id.notification_material_reply_progress, View.GONE);
Adrian Roose458aa82015-12-08 16:17:19 -08005165
5166 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
5167 big.setTextViewText(R.id.notification_material_reply_text_2, null);
5168 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
5169 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07005170
Selim Cineked64a142018-02-06 18:06:01 -08005171 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
5172 R.dimen.notification_content_margin);
Christoph Studerfe718432014-09-01 18:21:18 +02005173 }
5174
Selim Cinek384804b2018-04-18 14:31:07 +08005175 private RemoteViews applyStandardTemplateWithActions(int layoutId,
5176 TemplateBindResult result) {
5177 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this),
5178 result);
Adrian Roos48d746a2016-04-12 14:57:28 -07005179 }
5180
Gustav Sennton1463d832018-11-06 16:12:48 +00005181 private static List<Notification.Action> filterOutContextualActions(
5182 List<Notification.Action> actions) {
5183 List<Notification.Action> nonContextualActions = new ArrayList<>();
5184 for (Notification.Action action : actions) {
Gustav Sennton005d7a02019-01-04 13:41:32 +00005185 if (!action.isContextual()) {
Gustav Sennton1463d832018-11-06 16:12:48 +00005186 nonContextualActions.add(action);
5187 }
5188 }
5189 return nonContextualActions;
5190 }
5191
Adrian Roos70d7aa32017-01-11 15:39:06 -08005192 private RemoteViews applyStandardTemplateWithActions(int layoutId,
Selim Cinek384804b2018-04-18 14:31:07 +08005193 StandardTemplateParams p, TemplateBindResult result) {
5194 RemoteViews big = applyStandardTemplate(layoutId, p, result);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005195
Christoph Studerfe718432014-09-01 18:21:18 +02005196 resetStandardTemplateWithActions(big);
5197
Adrian Roose458aa82015-12-08 16:17:19 -08005198 boolean validRemoteInput = false;
5199
Gustav Sennton1463d832018-11-06 16:12:48 +00005200 // In the UI contextual actions appear separately from the standard actions, so we
5201 // filter them out here.
5202 List<Notification.Action> nonContextualActions = filterOutContextualActions(mActions);
5203
5204 int N = nonContextualActions.size();
Lucas Dupin00be88f2019-01-03 17:50:52 -08005205 boolean emphazisedMode = mN.fullScreenIntent != null;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005206 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005207 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08005208 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005209 big.setViewVisibility(R.id.actions, View.VISIBLE);
Selim Cineked64a142018-02-06 18:06:01 -08005210 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Daniel Sandler8680bf82012-05-15 16:52:52 -04005211 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005212 for (int i=0; i<N; i++) {
Gustav Sennton1463d832018-11-06 16:12:48 +00005213 Action action = nonContextualActions.get(i);
5214
Selim Cinekffcc7cf2018-02-06 17:43:51 -08005215 boolean actionHasValidInput = hasValidRemoteInput(action);
5216 validRemoteInput |= actionHasValidInput;
Adrian Roose458aa82015-12-08 16:17:19 -08005217
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005218 final RemoteViews button = generateActionButton(action, emphazisedMode, p);
Selim Cinek396caca2018-04-10 17:46:46 -07005219 if (actionHasValidInput && !emphazisedMode) {
Selim Cinekffcc7cf2018-02-06 17:43:51 -08005220 // Clear the drawable
5221 button.setInt(R.id.action0, "setBackgroundResource", 0);
5222 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005223 big.addView(R.id.actions, button);
5224 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07005225 } else {
5226 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005227 }
Adrian Roose458aa82015-12-08 16:17:19 -08005228
5229 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Lucas Dupin00be88f2019-01-03 17:50:52 -08005230 if (validRemoteInput && replyText != null
Selim Cinekbee4e072018-05-21 22:06:43 -07005231 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])
5232 && p.maxRemoteInputHistory > 0) {
Kenny Guya0f6de82018-04-06 16:20:16 +01005233 boolean showSpinner = mN.extras.getBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER);
Adrian Roose458aa82015-12-08 16:17:19 -08005234 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Kenny Guya0f6de82018-04-06 16:20:16 +01005235 big.setViewVisibility(R.id.notification_material_reply_text_1_container,
5236 View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005237 big.setTextViewText(R.id.notification_material_reply_text_1,
5238 processTextSpans(replyText[0]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005239 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1, p);
Kenny Guya0f6de82018-04-06 16:20:16 +01005240 big.setViewVisibility(R.id.notification_material_reply_progress,
5241 showSpinner ? View.VISIBLE : View.GONE);
5242 big.setProgressIndeterminateTintList(
5243 R.id.notification_material_reply_progress,
5244 ColorStateList.valueOf(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005245 isColorized(p) ? getPrimaryTextColor(p) : resolveContrastColor(p)));
Adrian Roose458aa82015-12-08 16:17:19 -08005246
Selim Cinekbee4e072018-05-21 22:06:43 -07005247 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])
5248 && p.maxRemoteInputHistory > 1) {
Adrian Roose458aa82015-12-08 16:17:19 -08005249 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005250 big.setTextViewText(R.id.notification_material_reply_text_2,
5251 processTextSpans(replyText[1]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005252 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2, p);
Adrian Roose458aa82015-12-08 16:17:19 -08005253
Selim Cinekbee4e072018-05-21 22:06:43 -07005254 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])
5255 && p.maxRemoteInputHistory > 2) {
Adrian Roose458aa82015-12-08 16:17:19 -08005256 big.setViewVisibility(
5257 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005258 big.setTextViewText(R.id.notification_material_reply_text_3,
5259 processTextSpans(replyText[2]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005260 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3, p);
Adrian Roose458aa82015-12-08 16:17:19 -08005261 }
5262 }
5263 }
5264
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005265 return big;
5266 }
5267
Adrian Roose458aa82015-12-08 16:17:19 -08005268 private boolean hasValidRemoteInput(Action action) {
5269 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
5270 // Weird actions
5271 return false;
5272 }
5273
5274 RemoteInput[] remoteInputs = action.getRemoteInputs();
5275 if (remoteInputs == null) {
5276 return false;
5277 }
5278
5279 for (RemoteInput r : remoteInputs) {
5280 CharSequence[] choices = r.getChoices();
5281 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
5282 return true;
5283 }
5284 }
5285 return false;
5286 }
5287
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005288 /**
5289 * Construct a RemoteViews for the final 1U notification layout. In order:
5290 * 1. Custom contentView from the caller
5291 * 2. Style's proposed content view
5292 * 3. Standard template view
5293 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005294 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005295 return createContentView(false /* increasedheight */ );
5296 }
5297
5298 /**
5299 * Construct a RemoteViews for the smaller content view.
5300 *
5301 * @param increasedHeight true if this layout be created with an increased height. Some
5302 * styles may support showing more then just that basic 1U size
5303 * and the system may decide to render important notifications
5304 * slightly bigger even when collapsed.
5305 *
5306 * @hide
5307 */
5308 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005309 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005310 return mN.contentView;
5311 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005312 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005313 if (styleView != null) {
5314 return styleView;
5315 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005316 }
Selim Cinek384804b2018-04-18 14:31:07 +08005317 return applyStandardTemplate(getBaseLayoutResource(), null /* result */);
Joe Onorato46439ce2010-11-19 13:56:21 -08005318 }
5319
Selim Cineka7679b62017-05-10 16:33:25 -07005320 private boolean useExistingRemoteView() {
5321 return mStyle == null || (!mStyle.displayCustomViewInline()
5322 && !mRebuildStyledRemoteViews);
5323 }
5324
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005325 /**
5326 * Construct a RemoteViews for the final big notification layout.
5327 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005328 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05005329 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07005330 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005331 return mN.bigContentView;
5332 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05005333 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08005334 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005335 } else if (mActions.size() != 0) {
Selim Cinek384804b2018-04-18 14:31:07 +08005336 result = applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5337 null /* result */);
Selim Cinek850a8542015-11-11 11:48:36 -05005338 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08005339 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05005340 return result;
5341 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005342
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005343 /**
Selim Cinek414ad332017-02-24 19:06:12 -08005344 * Construct a RemoteViews for the final notification header only. This will not be
5345 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005346 *
5347 * @hide
5348 */
Lucas Dupin00be88f2019-01-03 17:50:52 -08005349 public RemoteViews makeNotificationHeader() {
5350 return makeNotificationHeader(mParams.reset().fillTextsFrom(this));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005351 }
5352
5353 /**
5354 * Construct a RemoteViews for the final notification header only. This will not be
5355 * colorized.
5356 *
5357 * @param p the template params to inflate this with
5358 */
5359 private RemoteViews makeNotificationHeader(StandardTemplateParams p) {
5360 // Headers on their own are never colorized
5361 p.disallowColorization();
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005362 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Lucas Dupin00be88f2019-01-03 17:50:52 -08005363 R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005364 resetNotificationHeader(header);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005365 bindNotificationHeader(header, p);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005366 return header;
5367 }
5368
Adrian Roos487374f2017-01-11 15:48:14 -08005369 /**
5370 * Construct a RemoteViews for the ambient version of the notification.
5371 *
5372 * @hide
5373 */
5374 public RemoteViews makeAmbientNotification() {
Selim Cinek0e069942019-01-24 16:09:41 -08005375 RemoteViews headsUpContentView = createHeadsUpContentView(false /* increasedHeight */);
5376 if (headsUpContentView != null) {
5377 return headsUpContentView;
5378 }
5379 return createContentView();
Adrian Roos487374f2017-01-11 15:48:14 -08005380 }
5381
Selim Cinek29603462015-11-17 19:04:39 -08005382 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005383 if (result != null) {
5384 result.setViewVisibility(R.id.text_line_1, View.GONE);
5385 }
Selim Cinek29603462015-11-17 19:04:39 -08005386 }
5387
Selim Cinek6743c0b2017-01-18 18:24:01 -08005388 /**
5389 * Adapt the Notification header if this view is used as an expanded view.
5390 *
5391 * @hide
5392 */
5393 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005394 if (result != null) {
5395 result.setBoolean(R.id.notification_header, "setExpanded", true);
5396 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005397 }
5398
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005399 /**
5400 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005401 *
5402 * @param increasedHeight true if this layout be created with an increased height. Some
5403 * styles may support showing more then just that basic 1U size
5404 * and the system may decide to render important notifications
5405 * slightly bigger even when collapsed.
5406 *
5407 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005408 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005409 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005410 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005411 return mN.headsUpContentView;
5412 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08005413 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
5414 if (styleView != null) {
5415 return styleView;
5416 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05005417 } else if (mActions.size() == 0) {
5418 return null;
5419 }
5420
Selim Cinekbee4e072018-05-21 22:06:43 -07005421 // We only want at most a single remote input history to be shown here, otherwise
5422 // the content would become squished.
5423 StandardTemplateParams p = mParams.reset().fillTextsFrom(this)
5424 .setMaxRemoteInputHistory(1);
5425 return applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5426 p,
5427 null /* result */);
Chris Wren8fd39ec2014-02-27 17:43:26 -05005428 }
5429
Selim Cinek624c02db2015-12-14 21:00:02 -08005430 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08005431 * Construct a RemoteViews for the final heads-up notification layout.
5432 */
5433 public RemoteViews createHeadsUpContentView() {
5434 return createHeadsUpContentView(false /* useIncreasedHeight */);
5435 }
5436
5437 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08005438 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5439 *
5440 * @hide
5441 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005442 @UnsupportedAppUsage
Selim Cinek624c02db2015-12-14 21:00:02 -08005443 public RemoteViews makePublicContentView() {
5444 if (mN.publicVersion != null) {
5445 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Selim Cinekc3fec682019-06-06 18:11:07 -07005446 return builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08005447 }
5448 Bundle savedBundle = mN.extras;
5449 Style style = mStyle;
5450 mStyle = null;
5451 Icon largeIcon = mN.mLargeIcon;
5452 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07005453 Bitmap largeIconLegacy = mN.largeIcon;
5454 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005455 ArrayList<Action> actions = mActions;
5456 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08005457 Bundle publicExtras = new Bundle();
5458 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
5459 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
5460 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
5461 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07005462 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
5463 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cineked07b962018-04-30 14:39:35 -07005464 String appName = savedBundle.getString(EXTRA_SUBSTITUTE_APP_NAME);
5465 if (appName != null) {
5466 publicExtras.putString(EXTRA_SUBSTITUTE_APP_NAME, appName);
5467 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005468 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07005469 RemoteViews view;
Lucas Dupin00be88f2019-01-03 17:50:52 -08005470 view = makeNotificationHeader();
5471 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
Selim Cinek624c02db2015-12-14 21:00:02 -08005472 mN.extras = savedBundle;
5473 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07005474 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005475 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08005476 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005477 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08005478 }
5479
Selim Cinek6743c0b2017-01-18 18:24:01 -08005480 /**
5481 * Construct a content view for the display when low - priority
5482 *
5483 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
5484 * a new subtext is created consisting of the content of the
5485 * notification.
5486 * @hide
5487 */
5488 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005489 StandardTemplateParams p = mParams.reset()
5490 .forceDefaultColor()
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005491 .fillTextsFrom(this);
5492 if (!useRegularSubtext || TextUtils.isEmpty(mParams.summaryText)) {
5493 p.summaryText(createSummaryText());
Selim Cinek6743c0b2017-01-18 18:24:01 -08005494 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005495 RemoteViews header = makeNotificationHeader(p);
Selim Cinek1b554392017-02-28 17:22:49 -08005496 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08005497 return header;
5498 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005499
Selim Cinek6743c0b2017-01-18 18:24:01 -08005500 private CharSequence createSummaryText() {
5501 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
5502 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
5503 return titleText;
5504 }
5505 SpannableStringBuilder summary = new SpannableStringBuilder();
5506 if (titleText == null) {
5507 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
5508 }
5509 BidiFormatter bidi = BidiFormatter.getInstance();
5510 if (titleText != null) {
5511 summary.append(bidi.unicodeWrap(titleText));
5512 }
5513 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
5514 if (titleText != null && contentText != null) {
5515 summary.append(bidi.unicodeWrap(mContext.getText(
5516 R.string.notification_header_divider_symbol_with_spaces)));
5517 }
5518 if (contentText != null) {
5519 summary.append(bidi.unicodeWrap(contentText));
5520 }
5521 return summary;
5522 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05005523
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005524 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005525 StandardTemplateParams p) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04005526 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07005527 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005528 emphazisedMode ? getEmphasizedActionLayoutResource()
5529 : tombstone ? getActionTombstoneLayoutResource()
5530 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04005531 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04005532 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04005533 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005534 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005535 if (action.mRemoteInputs != null) {
5536 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
5537 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005538 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07005539 // change the background bgColor
Selim Cinek981962e2016-07-20 20:41:58 -07005540 CharSequence title = action.title;
5541 ColorStateList[] outResultColor = null;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005542 int background = resolveBackgroundColor(p);
Selim Cinek981962e2016-07-20 20:41:58 -07005543 if (isLegacy()) {
Lucas Dupina291d192018-06-07 13:59:42 -07005544 title = ContrastColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07005545 } else {
5546 outResultColor = new ColorStateList[1];
Selim Cinek396caca2018-04-10 17:46:46 -07005547 title = ensureColorSpanContrast(title, background, outResultColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005548 }
Selim Cinek48f66b72017-08-18 16:17:51 -07005549 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005550 setTextViewColorPrimary(button, R.id.action0, p);
Selim Cinek396caca2018-04-10 17:46:46 -07005551 int rippleColor;
5552 boolean hasColorOverride = outResultColor != null && outResultColor[0] != null;
5553 if (hasColorOverride) {
5554 // There's a span spanning the full text, let's take it and use it as the
5555 // background color
5556 background = outResultColor[0].getDefaultColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005557 int textColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07005558 background, mInNightMode);
Selim Cinek396caca2018-04-10 17:46:46 -07005559 button.setTextColor(R.id.action0, textColor);
5560 rippleColor = textColor;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005561 } else if (getRawColor(p) != COLOR_DEFAULT && !isColorized(p)
Lucas Dupinf15c5e52019-04-01 19:00:12 -07005562 && mTintActionButtons && !mInNightMode) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005563 rippleColor = resolveContrastColor(p);
Selim Cinek396caca2018-04-10 17:46:46 -07005564 button.setTextColor(R.id.action0, rippleColor);
5565 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005566 rippleColor = getPrimaryTextColor(p);
Selim Cinek981962e2016-07-20 20:41:58 -07005567 }
Selim Cinek396caca2018-04-10 17:46:46 -07005568 // We only want about 20% alpha for the ripple
5569 rippleColor = (rippleColor & 0x00ffffff) | 0x33000000;
5570 button.setColorStateList(R.id.action0, "setRippleColor",
5571 ColorStateList.valueOf(rippleColor));
5572 button.setColorStateList(R.id.action0, "setButtonBackground",
5573 ColorStateList.valueOf(background));
5574 button.setBoolean(R.id.action0, "setHasStroke", !hasColorOverride);
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005575 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07005576 button.setTextViewText(R.id.action0, processTextSpans(
5577 processLegacyText(action.title)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005578 if (isColorized(p)) {
5579 setTextViewColorPrimary(button, R.id.action0, p);
5580 } else if (getRawColor(p) != COLOR_DEFAULT && mTintActionButtons) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005581 button.setTextColor(R.id.action0, resolveContrastColor(p));
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005582 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005583 }
Tony Mak7d4b3a52018-11-27 17:29:36 +00005584 button.setIntTag(R.id.action0, R.id.notification_action_index_tag,
5585 mActions.indexOf(action));
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005586 return button;
5587 }
5588
Joe Onoratocb109a02011-01-18 17:57:41 -08005589 /**
Selim Cinek981962e2016-07-20 20:41:58 -07005590 * Ensures contrast on color spans against a background color. also returns the color of the
5591 * text if a span was found that spans over the whole text.
5592 *
5593 * @param charSequence the charSequence on which the spans are
5594 * @param background the background color to ensure the contrast against
5595 * @param outResultColor an array in which a color will be returned as the first element if
5596 * there exists a full length color span.
5597 * @return the contrasted charSequence
5598 */
5599 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
5600 ColorStateList[] outResultColor) {
5601 if (charSequence instanceof Spanned) {
5602 Spanned ss = (Spanned) charSequence;
5603 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
5604 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
5605 for (Object span : spans) {
5606 Object resultSpan = span;
5607 int spanStart = ss.getSpanStart(span);
5608 int spanEnd = ss.getSpanEnd(span);
5609 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
5610 if (resultSpan instanceof CharacterStyle) {
5611 resultSpan = ((CharacterStyle) span).getUnderlying();
5612 }
5613 if (resultSpan instanceof TextAppearanceSpan) {
5614 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
5615 ColorStateList textColor = originalSpan.getTextColor();
5616 if (textColor != null) {
5617 int[] colors = textColor.getColors();
5618 int[] newColors = new int[colors.length];
5619 for (int i = 0; i < newColors.length; i++) {
Lucas Dupina291d192018-06-07 13:59:42 -07005620 newColors[i] = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005621 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005622 }
5623 textColor = new ColorStateList(textColor.getStates().clone(),
5624 newColors);
Selim Cinek396caca2018-04-10 17:46:46 -07005625 if (fullLength) {
5626 outResultColor[0] = textColor;
5627 // Let's drop the color from the span
5628 textColor = null;
5629 }
Selim Cinek981962e2016-07-20 20:41:58 -07005630 resultSpan = new TextAppearanceSpan(
5631 originalSpan.getFamily(),
5632 originalSpan.getTextStyle(),
5633 originalSpan.getTextSize(),
5634 textColor,
5635 originalSpan.getLinkTextColor());
Selim Cinek981962e2016-07-20 20:41:58 -07005636 }
5637 } else if (resultSpan instanceof ForegroundColorSpan) {
5638 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
5639 int foregroundColor = originalSpan.getForegroundColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005640 foregroundColor = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005641 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005642 if (fullLength) {
5643 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
Selim Cinek396caca2018-04-10 17:46:46 -07005644 resultSpan = null;
5645 } else {
5646 resultSpan = new ForegroundColorSpan(foregroundColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005647 }
5648 } else {
5649 resultSpan = span;
5650 }
Selim Cinek396caca2018-04-10 17:46:46 -07005651 if (resultSpan != null) {
5652 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
5653 }
Selim Cinek981962e2016-07-20 20:41:58 -07005654 }
5655 return builder;
5656 }
5657 return charSequence;
5658 }
5659
5660 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005661 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07005662 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005663 */
5664 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005665 if (!mIsLegacyInitialized) {
5666 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
5667 < Build.VERSION_CODES.LOLLIPOP;
5668 mIsLegacyInitialized = true;
5669 }
5670 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005671 }
5672
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005673 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08005674 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
Lucas Dupin00be88f2019-01-03 17:50:52 -08005675 if (isAlreadyLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005676 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005677 } else {
5678 return charSequence;
5679 }
5680 }
5681
Dan Sandler26e81cf2014-05-06 10:01:27 -04005682 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005683 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04005684 */
Adrian Roos487374f2017-01-11 15:48:14 -08005685 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005686 StandardTemplateParams p) {
Selim Cinekea4bef72015-12-02 15:51:10 -08005687 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005688 int color;
Lucas Dupin00be88f2019-01-03 17:50:52 -08005689 if (isColorized(p)) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005690 color = getPrimaryTextColor(p);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005691 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005692 color = resolveContrastColor(p);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005693 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005694 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07005695 contentView.setDrawableTint(R.id.icon, false, color,
5696 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005697
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005698 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005699 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08005700 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005701 }
5702
Dan Sandler26e81cf2014-05-06 10:01:27 -04005703 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005704 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04005705 * if it's grayscale).
5706 */
5707 // TODO: also check bounds, transparency, that sort of thing.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005708 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView,
5709 StandardTemplateParams p) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04005710 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005711 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005712 // resolve color will fall back to the default when legacy
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005713 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(p),
Sunny Goyal5b153922017-09-21 21:00:36 -07005714 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01005715 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005716 }
5717
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05005718 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005719 if (mN.color != COLOR_DEFAULT) {
5720 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02005721 }
Jorim Jaggi74419312014-06-10 20:57:21 +02005722 }
5723
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005724 int resolveContrastColor(StandardTemplateParams p) {
5725 int rawColor = getRawColor(p);
5726 if (mCachedContrastColorIsFor == rawColor && mCachedContrastColor != COLOR_INVALID) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08005727 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005728 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005729
Selim Cinekac5f0272017-05-02 16:05:41 -07005730 int color;
Selim Cinekc7f5a822018-03-20 19:32:06 -07005731 int background = mContext.getColor(
5732 com.android.internal.R.color.notification_material_background_color);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005733 if (rawColor == COLOR_DEFAULT) {
5734 ensureColors(p);
Lucas Dupinf03e7522018-06-25 16:21:13 -07005735 color = ContrastColorUtil.resolveDefaultColor(mContext, background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005736 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005737 color = ContrastColorUtil.resolveContrastColor(mContext, rawColor,
Anthony Chenad4d1582017-04-10 16:07:58 -07005738 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005739 }
5740 if (Color.alpha(color) < 255) {
5741 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005742 color = ContrastColorUtil.compositeColors(color, background);
Selim Cinekac5f0272017-05-02 16:05:41 -07005743 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005744 mCachedContrastColorIsFor = rawColor;
Selim Cinekac5f0272017-05-02 16:05:41 -07005745 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005746 }
5747
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005748 /**
5749 * Return the raw color of this Notification, which doesn't necessarily satisfy contrast.
5750 *
5751 * @see #resolveContrastColor(StandardTemplateParams) for the contrasted color
5752 * @param p the template params to inflate this with
5753 */
5754 private int getRawColor(StandardTemplateParams p) {
5755 if (p.forceDefaultColor) {
5756 return COLOR_DEFAULT;
5757 }
5758 return mN.color;
5759 }
5760
Selim Cinek4717d862018-04-19 09:19:15 +08005761 int resolveNeutralColor() {
5762 if (mNeutralColor != COLOR_INVALID) {
5763 return mNeutralColor;
5764 }
5765 int background = mContext.getColor(
5766 com.android.internal.R.color.notification_material_background_color);
Lucas Dupinf03e7522018-06-25 16:21:13 -07005767 mNeutralColor = ContrastColorUtil.resolveDefaultColor(mContext, background,
5768 mInNightMode);
Selim Cinek4717d862018-04-19 09:19:15 +08005769 if (Color.alpha(mNeutralColor) < 255) {
5770 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005771 mNeutralColor = ContrastColorUtil.compositeColors(mNeutralColor, background);
Selim Cinek4717d862018-04-19 09:19:15 +08005772 }
5773 return mNeutralColor;
5774 }
5775
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005776 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005777 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005778 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08005779 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005780 @NonNull
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005781 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005782 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005783 mN.actions = new Action[mActions.size()];
5784 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005785 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005786 if (!mPersonList.isEmpty()) {
Selim Cineke7238dd2017-12-14 17:48:32 -08005787 mN.extras.putParcelableArrayList(EXTRA_PEOPLE_LIST, mPersonList);
Dan Sandler0bf2ed82013-12-21 23:33:41 -06005788 }
Selim Cinek247fa012016-02-18 09:50:48 -08005789 if (mN.bigContentView != null || mN.contentView != null
5790 || mN.headsUpContentView != null) {
5791 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
5792 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005793 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08005794 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005795
Julia Reynolds3b848122016-02-26 10:45:32 -05005796 /**
5797 * Creates a Builder from an existing notification so further changes can be made.
5798 * @param context The context for your application / activity.
5799 * @param n The notification to create a Builder from.
5800 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005801 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005802 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005803 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005804 ApplicationInfo applicationInfo = n.extras.getParcelable(
5805 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005806 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05005807 if (applicationInfo != null) {
5808 try {
5809 builderContext = context.createApplicationContext(applicationInfo,
5810 Context.CONTEXT_RESTRICTED);
5811 } catch (NameNotFoundException e) {
5812 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
5813 builderContext = context; // try with our context
5814 }
5815 } else {
5816 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02005817 }
5818
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005819 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005820 }
5821
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005822 /**
Gustav Sennton761884c2018-11-19 17:40:19 +00005823 * Determines whether the platform can generate contextual actions for a notification.
5824 * By default this is true.
5825 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005826 @NonNull
Gustav Sennton761884c2018-11-19 17:40:19 +00005827 public Builder setAllowSystemGeneratedContextualActions(boolean allowed) {
5828 mN.mAllowSystemGeneratedContextualActions = allowed;
5829 return this;
5830 }
5831
5832 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005833 * @deprecated Use {@link #build()} instead.
5834 */
5835 @Deprecated
5836 public Notification getNotification() {
5837 return build();
5838 }
5839
5840 /**
5841 * Combine all of the options that have been set and return a new {@link Notification}
5842 * object.
5843 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005844 @NonNull
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005845 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005846 // first, add any extras from the calling code
5847 if (mUserExtras != null) {
5848 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005849 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005850
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005851 mN.creationTime = System.currentTimeMillis();
5852
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005853 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05005854 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02005855
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005856 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005857
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005858 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02005859 mStyle.reduceImageSizes(mContext);
5860 mStyle.purgeResources();
Selim Cinek90343862018-02-01 11:07:11 -08005861 mStyle.validate(mContext);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005862 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005863 }
Selim Cinekd0426622017-07-11 13:19:59 +02005864 mN.reduceImageSizes(mContext);
5865
Adrian Roos5081c0d2016-02-26 16:04:19 -08005866 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07005867 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005868 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005869 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005870 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
5871 mN.contentView.getSequenceNumber());
5872 }
5873 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005874 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005875 if (mN.bigContentView != null) {
5876 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
5877 mN.bigContentView.getSequenceNumber());
5878 }
5879 }
5880 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005881 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005882 if (mN.headsUpContentView != null) {
5883 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
5884 mN.headsUpContentView.getSequenceNumber());
5885 }
5886 }
5887 }
5888
Julia Reynolds4c0c2022016-02-02 15:11:59 -05005889 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
5890 mN.flags |= FLAG_SHOW_LIGHTS;
5891 }
5892
Adrian Roosfb921842017-10-26 14:49:56 +02005893 mN.allPendingIntents = null;
5894
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005895 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005896 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005897
5898 /**
5899 * Apply this Builder to an existing {@link Notification} object.
5900 *
5901 * @hide
5902 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005903 @NonNull
5904 public Notification buildInto(@NonNull Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005905 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005906 return n;
5907 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005908
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005909 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005910 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005911 * change. Also removes extenders on low ram devices, as
5912 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005913 *
5914 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5915 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005916 * @hide
5917 */
Kristian Monsen05f34792018-04-09 10:27:16 +02005918 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam,
5919 Context context) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005920 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005921
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005922 // Only strip views for known Styles because we won't know how to
5923 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005924 if (!isLowRam
5925 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005926 && getNotificationStyleClass(templateClass) == null) {
5927 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005928 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005929
5930 // Only strip unmodified BuilderRemoteViews.
5931 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005932 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005933 n.contentView.getSequenceNumber();
5934 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005935 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005936 n.bigContentView.getSequenceNumber();
5937 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005938 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005939 n.headsUpContentView.getSequenceNumber();
5940
5941 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005942 if (!isLowRam
5943 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005944 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005945 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005946
5947 Notification clone = n.clone();
5948 if (stripContentView) {
5949 clone.contentView = null;
5950 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5951 }
5952 if (stripBigContentView) {
5953 clone.bigContentView = null;
5954 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5955 }
5956 if (stripHeadsUpContentView) {
5957 clone.headsUpContentView = null;
5958 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5959 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005960 if (isLowRam) {
Kristian Monsen05f34792018-04-09 10:27:16 +02005961 String[] allowedServices = context.getResources().getStringArray(
5962 R.array.config_allowedManagedServicesOnLowRamDevices);
5963 if (allowedServices.length == 0) {
5964 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5965 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5966 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5967 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005968 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005969 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005970 }
5971
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005972 @UnsupportedAppUsage
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005973 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005974 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005975 }
5976
5977 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005978 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005979 }
5980
5981 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005982 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005983 }
5984
5985 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005986 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005987 }
5988
5989 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005990 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005991 }
5992
Adrian Roosc1a80b02016-04-05 14:54:55 -07005993 private int getMessagingLayoutResource() {
5994 return R.layout.notification_template_material_messaging;
5995 }
5996
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005997 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005998 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005999 }
6000
Selim Cinek06e9e1f2016-07-08 17:14:16 -07006001 private int getEmphasizedActionLayoutResource() {
6002 return R.layout.notification_material_action_emphasized;
6003 }
6004
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006005 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07006006 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006007 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08006008
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006009 private int getBackgroundColor(StandardTemplateParams p) {
6010 if (isColorized(p)) {
6011 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : getRawColor(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08006012 } else {
Selim Cinekc7f5a822018-03-20 19:32:06 -07006013 return COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08006014 }
6015 }
6016
Selim Cinek396caca2018-04-10 17:46:46 -07006017 /**
Selim Cinek4717d862018-04-19 09:19:15 +08006018 * Gets a neutral color that can be used for icons or similar that should not stand out.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006019 * @param p the template params to inflate this with
Selim Cinek4717d862018-04-19 09:19:15 +08006020 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006021 private int getNeutralColor(StandardTemplateParams p) {
6022 if (isColorized(p)) {
6023 return getSecondaryTextColor(p);
Selim Cinek4717d862018-04-19 09:19:15 +08006024 } else {
6025 return resolveNeutralColor();
6026 }
6027 }
6028
6029 /**
Selim Cinek396caca2018-04-10 17:46:46 -07006030 * Same as getBackgroundColor but also resolved the default color to the background.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006031 * @param p the template params to inflate this with
Selim Cinek396caca2018-04-10 17:46:46 -07006032 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006033 private int resolveBackgroundColor(StandardTemplateParams p) {
6034 int backgroundColor = getBackgroundColor(p);
Selim Cinek396caca2018-04-10 17:46:46 -07006035 if (backgroundColor == COLOR_DEFAULT) {
6036 backgroundColor = mContext.getColor(
6037 com.android.internal.R.color.notification_material_background_color);
6038 }
6039 return backgroundColor;
6040 }
6041
Anthony Chenad4d1582017-04-10 16:07:58 -07006042 private boolean shouldTintActionButtons() {
6043 return mTintActionButtons;
6044 }
6045
Selim Cinek99104832017-01-25 14:47:33 -08006046 private boolean textColorsNeedInversion() {
6047 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
6048 return false;
6049 }
6050 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
6051 return targetSdkVersion > Build.VERSION_CODES.M
6052 && targetSdkVersion < Build.VERSION_CODES.O;
6053 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07006054
6055 /**
6056 * Set a color palette to be used as the background and textColors
6057 *
6058 * @param backgroundColor the color to be used as the background
6059 * @param foregroundColor the color to be used as the foreground
6060 *
6061 * @hide
6062 */
6063 public void setColorPalette(int backgroundColor, int foregroundColor) {
6064 mBackgroundColor = backgroundColor;
6065 mForegroundColor = foregroundColor;
6066 mTextColorsAreForBackground = COLOR_INVALID;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006067 ensureColors(mParams.reset().fillTextsFrom(this));
Selim Cinek5fb73f82017-04-20 16:55:38 -07006068 }
Selim Cinekac5f0272017-05-02 16:05:41 -07006069
6070 /**
Selim Cineka7679b62017-05-10 16:33:25 -07006071 * Forces all styled remoteViews to be built from scratch and not use any cached
6072 * RemoteViews.
6073 * This is needed for legacy apps that are baking in their remoteviews into the
6074 * notification.
6075 *
6076 * @hide
6077 */
6078 public void setRebuildStyledRemoteViews(boolean rebuild) {
6079 mRebuildStyledRemoteViews = rebuild;
6080 }
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006081
6082 /**
6083 * Get the text that should be displayed in the statusBar when heads upped. This is
6084 * usually just the app name, but may be different depending on the style.
6085 *
6086 * @param publicMode If true, return a text that is safe to display in public.
6087 *
6088 * @hide
6089 */
6090 public CharSequence getHeadsUpStatusBarText(boolean publicMode) {
6091 if (mStyle != null && !publicMode) {
6092 CharSequence text = mStyle.getHeadsUpStatusBarText();
6093 if (!TextUtils.isEmpty(text)) {
6094 return text;
6095 }
6096 }
6097 return loadHeaderAppName();
6098 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08006099 }
6100
6101 /**
Selim Cinekd0426622017-07-11 13:19:59 +02006102 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
6103 * remote views.
6104 *
6105 * @hide
6106 */
6107 void reduceImageSizes(Context context) {
6108 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
6109 return;
6110 }
Selim Cineka8cb1262017-08-15 16:53:44 -07006111 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006112 if (mLargeIcon != null || largeIcon != null) {
6113 Resources resources = context.getResources();
6114 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07006115 int maxWidth = resources.getDimensionPixelSize(isLowRam
6116 ? R.dimen.notification_right_icon_size_low_ram
6117 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006118 int maxHeight = maxWidth;
6119 if (MediaStyle.class.equals(style)
6120 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006121 maxHeight = resources.getDimensionPixelSize(isLowRam
6122 ? R.dimen.notification_media_image_max_height_low_ram
6123 : R.dimen.notification_media_image_max_height);
6124 maxWidth = resources.getDimensionPixelSize(isLowRam
6125 ? R.dimen.notification_media_image_max_width_low_ram
6126 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006127 }
6128 if (mLargeIcon != null) {
6129 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
6130 }
6131 if (largeIcon != null) {
6132 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
6133 }
6134 }
Selim Cineka8cb1262017-08-15 16:53:44 -07006135 reduceImageSizesForRemoteView(contentView, context, isLowRam);
6136 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
6137 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02006138 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
6139 }
6140
Selim Cineka8cb1262017-08-15 16:53:44 -07006141 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
6142 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02006143 if (remoteView != null) {
6144 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006145 int maxWidth = resources.getDimensionPixelSize(isLowRam
6146 ? R.dimen.notification_custom_view_max_image_width_low_ram
6147 : R.dimen.notification_custom_view_max_image_width);
6148 int maxHeight = resources.getDimensionPixelSize(isLowRam
6149 ? R.dimen.notification_custom_view_max_image_height_low_ram
6150 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02006151 remoteView.reduceImageSizes(maxWidth, maxHeight);
6152 }
6153 }
6154
6155 /**
Selim Cinek22714f12017-04-13 16:23:53 -07006156 * @return whether this notification is a foreground service notification
Gus Prevasb0c1a462018-11-05 11:06:15 -05006157 * @hide
Selim Cinek7b9605b2017-01-19 17:36:00 -08006158 */
Gus Prevasb0c1a462018-11-05 11:06:15 -05006159 public boolean isForegroundService() {
Selim Cinek22714f12017-04-13 16:23:53 -07006160 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08006161 }
6162
6163 /**
Selim Cinek99104832017-01-25 14:47:33 -08006164 * @return whether this notification has a media session attached
6165 * @hide
6166 */
6167 public boolean hasMediaSession() {
6168 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
6169 }
6170
6171 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006172 * @return the style class of this notification
6173 * @hide
6174 */
6175 public Class<? extends Notification.Style> getNotificationStyle() {
6176 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
6177
6178 if (!TextUtils.isEmpty(templateClass)) {
6179 return Notification.getNotificationStyleClass(templateClass);
6180 }
6181 return null;
6182 }
6183
6184 /**
Selim Cinek22714f12017-04-13 16:23:53 -07006185 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08006186 *
6187 * @hide
6188 */
6189 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07006190 if (isColorizedMedia()) {
6191 return true;
6192 }
Julia Reynolds4db59552017-06-30 13:34:01 -04006193 return extras.getBoolean(EXTRA_COLORIZED)
6194 && (hasColorizedPermission() || isForegroundService());
6195 }
6196
6197 /**
6198 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
6199 * permission. The permission is checked when a notification is enqueued.
6200 */
6201 private boolean hasColorizedPermission() {
6202 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07006203 }
6204
6205 /**
6206 * @return true if this notification is colorized and it is a media notification
6207 *
6208 * @hide
6209 */
6210 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08006211 Class<? extends Style> style = getNotificationStyle();
6212 if (MediaStyle.class.equals(style)) {
6213 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
6214 if ((colorized == null || colorized) && hasMediaSession()) {
6215 return true;
6216 }
6217 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6218 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
6219 return true;
6220 }
6221 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07006222 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006223 }
6224
Selim Cinek0847acd2017-04-24 19:48:29 -07006225
6226 /**
6227 * @return true if this is a media notification
6228 *
6229 * @hide
6230 */
6231 public boolean isMediaNotification() {
6232 Class<? extends Style> style = getNotificationStyle();
6233 if (MediaStyle.class.equals(style)) {
6234 return true;
6235 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6236 return true;
6237 }
6238 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006239 }
6240
Mady Mellor7eb18ef2019-03-27 14:03:46 -07006241 /**
Julia Tuttle28fb1a92019-05-03 18:07:45 -04006242 * @return true if this notification is showing as a bubble
Mady Mellor7eb18ef2019-03-27 14:03:46 -07006243 *
6244 * @hide
6245 */
6246 public boolean isBubbleNotification() {
6247 return (flags & Notification.FLAG_BUBBLE) != 0;
6248 }
6249
Selim Cinek279fa862016-06-14 10:57:25 -07006250 private boolean hasLargeIcon() {
6251 return mLargeIcon != null || largeIcon != null;
6252 }
6253
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006254 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006255 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006256 * @hide
6257 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07006258 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006259 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
6260 }
6261
6262 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006263 * @return true if the notification will show a chronometer; false otherwise
6264 * @hide
6265 */
6266 public boolean showsChronometer() {
6267 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
6268 }
6269
6270 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04006271 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05006272 */
6273 @SystemApi
6274 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
6275 Class<? extends Style>[] classes = new Class[] {
6276 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
6277 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
6278 MessagingStyle.class };
6279 for (Class<? extends Style> innerClass : classes) {
6280 if (templateClass.equals(innerClass.getName())) {
6281 return innerClass;
6282 }
6283 }
6284 return null;
6285 }
6286
6287 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006288 * An object that can apply a rich notification style to a {@link Notification.Builder}
6289 * object.
6290 */
Griff Hazendfcb0802014-02-11 12:00:00 -08006291 public static abstract class Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07006292
6293 /**
6294 * The number of items allowed simulatanously in the remote input history.
6295 * @hide
6296 */
6297 static final int MAX_REMOTE_INPUT_HISTORY_LINES = 3;
Chris Wrend6297db2012-05-03 16:20:13 -04006298 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006299
6300 /**
6301 * @hide
6302 */
6303 protected CharSequence mSummaryText = null;
6304
6305 /**
6306 * @hide
6307 */
6308 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04006309
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006310 protected Builder mBuilder;
6311
Chris Wrend6297db2012-05-03 16:20:13 -04006312 /**
6313 * Overrides ContentTitle in the big form of the template.
6314 * This defaults to the value passed to setContentTitle().
6315 */
6316 protected void internalSetBigContentTitle(CharSequence title) {
6317 mBigContentTitle = title;
6318 }
6319
6320 /**
6321 * Set the first line of text after the detail section in the big form of the template.
6322 */
6323 protected void internalSetSummaryText(CharSequence cs) {
6324 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04006325 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04006326 }
6327
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006328 public void setBuilder(Builder builder) {
6329 if (mBuilder != builder) {
6330 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07006331 if (mBuilder != null) {
6332 mBuilder.setStyle(this);
6333 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006334 }
6335 }
6336
Chris Wrend6297db2012-05-03 16:20:13 -04006337 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006338 if (mBuilder == null) {
6339 throw new IllegalArgumentException("Style requires a valid Builder object");
6340 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006341 }
Chris Wrend6297db2012-05-03 16:20:13 -04006342
6343 protected RemoteViews getStandardView(int layoutId) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006344 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder);
6345 return getStandardView(layoutId, p, null);
Selim Cinek384804b2018-04-18 14:31:07 +08006346 }
6347
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006348
Selim Cinek384804b2018-04-18 14:31:07 +08006349 /**
6350 * Get the standard view for this style.
6351 *
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006352 * @param layoutId The layout id to use.
6353 * @param p the params for this inflation.
Selim Cinek384804b2018-04-18 14:31:07 +08006354 * @param result The result where template bind information is saved.
6355 * @return A remoteView for this style.
6356 * @hide
6357 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006358 protected RemoteViews getStandardView(int layoutId, StandardTemplateParams p,
6359 TemplateBindResult result) {
Chris Wrend6297db2012-05-03 16:20:13 -04006360 checkBuilder();
6361
6362 if (mBigContentTitle != null) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006363 p.title = mBigContentTitle;
Chris Wrend6297db2012-05-03 16:20:13 -04006364 }
6365
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006366 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId, p,
6367 result);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006368
Chris Wrend6297db2012-05-03 16:20:13 -04006369 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
6370 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04006371 } else {
6372 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04006373 }
6374
Chris Wrend6297db2012-05-03 16:20:13 -04006375 return contentView;
6376 }
6377
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006378 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006379 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006380 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08006381 *
6382 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006383 * @hide
6384 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08006385 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006386 return null;
6387 }
6388
6389 /**
6390 * Construct a Style-specific RemoteViews for the final big notification layout.
6391 * @hide
6392 */
6393 public RemoteViews makeBigContentView() {
6394 return null;
6395 }
6396
6397 /**
6398 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08006399 *
6400 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006401 * @hide
6402 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006403 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006404 return null;
6405 }
6406
6407 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006408 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006409 * @hide
6410 */
6411 public void addExtras(Bundle extras) {
6412 if (mSummaryTextSet) {
6413 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
6414 }
6415 if (mBigContentTitle != null) {
6416 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
6417 }
Chris Wren91ad5632013-06-05 15:05:57 -04006418 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006419 }
6420
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006421 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006422 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006423 * @hide
6424 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02006425 protected void restoreFromExtras(Bundle extras) {
6426 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
6427 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
6428 mSummaryTextSet = true;
6429 }
6430 if (extras.containsKey(EXTRA_TITLE_BIG)) {
6431 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
6432 }
6433 }
6434
6435
6436 /**
6437 * @hide
6438 */
6439 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006440 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006441 return wip;
6442 }
6443
Daniel Sandler0ec46202015-06-24 01:27:05 -04006444 /**
6445 * @hide
6446 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006447 public void purgeResources() {}
6448
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006449 /**
6450 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
6451 * attached to.
6452 *
6453 * @return the fully constructed Notification.
6454 */
6455 public Notification build() {
6456 checkBuilder();
6457 return mBuilder.build();
6458 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006459
6460 /**
6461 * @hide
6462 * @return true if the style positions the progress bar on the second line; false if the
6463 * style hides the progress bar
6464 */
6465 protected boolean hasProgress() {
6466 return true;
6467 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006468
6469 /**
6470 * @hide
6471 * @return Whether we should put the summary be put into the notification header
6472 */
6473 public boolean hasSummaryInHeader() {
6474 return true;
6475 }
Selim Cinek593610c2016-02-16 18:42:57 -08006476
6477 /**
6478 * @hide
6479 * @return Whether custom content views are displayed inline in the style
6480 */
6481 public boolean displayCustomViewInline() {
6482 return false;
6483 }
Selim Cinekd0426622017-07-11 13:19:59 +02006484
6485 /**
6486 * Reduces the image sizes contained in this style.
6487 *
6488 * @hide
6489 */
6490 public void reduceImageSizes(Context context) {
6491 }
Selim Cinek90343862018-02-01 11:07:11 -08006492
6493 /**
6494 * Validate that this style was properly composed. This is called at build time.
6495 * @hide
6496 */
6497 public void validate(Context context) {
6498 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006499
6500 /**
6501 * @hide
6502 */
6503 public abstract boolean areNotificationsVisiblyDifferent(Style other);
Selim Cinekc7f5a822018-03-20 19:32:06 -07006504
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006505 /**
koprivaa1a78482018-10-09 10:09:23 -07006506 * @return the text that should be displayed in the statusBar when heads-upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006507 * If {@code null} is returned, the default implementation will be used.
6508 *
6509 * @hide
6510 */
6511 public CharSequence getHeadsUpStatusBarText() {
6512 return null;
6513 }
Joe Onorato46439ce2010-11-19 13:56:21 -08006514 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006515
6516 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006517 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08006518 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006519 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006520 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006521 * Notification notif = new Notification.Builder(mContext)
6522 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
6523 * .setContentText(subject)
6524 * .setSmallIcon(R.drawable.new_post)
6525 * .setLargeIcon(aBitmap)
6526 * .setStyle(new Notification.BigPictureStyle()
6527 * .bigPicture(aBigBitmap))
6528 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006529 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006530 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006531 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006532 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006533 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006534 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006535 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006536 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006537
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006538 public BigPictureStyle() {
6539 }
6540
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006541 /**
6542 * @deprecated use {@code BigPictureStyle()}.
6543 */
6544 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006545 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006546 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006547 }
6548
Chris Wrend6297db2012-05-03 16:20:13 -04006549 /**
6550 * Overrides ContentTitle in the big form of the template.
6551 * This defaults to the value passed to setContentTitle().
6552 */
6553 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006554 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006555 return this;
6556 }
6557
6558 /**
6559 * Set the first line of text after the detail section in the big form of the template.
6560 */
6561 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006562 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006563 return this;
6564 }
6565
Chris Wren0bd664d2012-08-01 13:56:56 -04006566 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05006567 * @hide
6568 */
6569 public Bitmap getBigPicture() {
6570 return mPicture;
6571 }
6572
6573 /**
Chris Wren0bd664d2012-08-01 13:56:56 -04006574 * Provide the bitmap to be used as the payload for the BigPicture notification.
6575 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006576 public BigPictureStyle bigPicture(Bitmap b) {
6577 mPicture = b;
6578 return this;
6579 }
6580
Chris Wren3745a3d2012-05-22 15:11:52 -04006581 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04006582 * Override the large icon when the big notification is shown.
6583 */
6584 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04006585 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
6586 }
6587
6588 /**
6589 * Override the large icon when the big notification is shown.
6590 */
6591 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04006592 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006593 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006594 return this;
6595 }
6596
Riley Andrews0394a0c2015-11-03 23:36:52 -08006597 /** @hide */
6598 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
6599
Daniel Sandler0ec46202015-06-24 01:27:05 -04006600 /**
6601 * @hide
6602 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006603 @Override
6604 public void purgeResources() {
6605 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08006606 if (mPicture != null &&
6607 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08006608 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006609 mPicture = mPicture.createAshmemBitmap();
6610 }
6611 if (mBigLargeIcon != null) {
6612 mBigLargeIcon.convertToAshmem();
6613 }
6614 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01006615
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006616 /**
6617 * @hide
6618 */
Selim Cinekd0426622017-07-11 13:19:59 +02006619 @Override
6620 public void reduceImageSizes(Context context) {
6621 super.reduceImageSizes(context);
6622 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006623 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006624 if (mPicture != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006625 int maxPictureWidth = resources.getDimensionPixelSize(isLowRam
6626 ? R.dimen.notification_big_picture_max_height_low_ram
6627 : R.dimen.notification_big_picture_max_height);
6628 int maxPictureHeight = resources.getDimensionPixelSize(isLowRam
6629 ? R.dimen.notification_big_picture_max_width_low_ram
6630 : R.dimen.notification_big_picture_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006631 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
6632 }
6633 if (mBigLargeIcon != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006634 int rightIconSize = resources.getDimensionPixelSize(isLowRam
6635 ? R.dimen.notification_right_icon_size_low_ram
6636 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006637 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
6638 }
6639 }
6640
6641 /**
6642 * @hide
6643 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006644 public RemoteViews makeBigContentView() {
6645 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01006646 // This covers the following cases:
6647 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006648 // mN.mLargeIcon
6649 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04006650 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07006651 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006652 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006653 oldLargeIcon = mBuilder.mN.mLargeIcon;
6654 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006655 // The legacy largeIcon might not allow us to clear the image, as it's taken in
6656 // replacement if the other one is null. Because we're restoring these legacy icons
6657 // for old listeners, this is in general non-null.
6658 largeIconLegacy = mBuilder.mN.largeIcon;
6659 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006660 }
6661
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006662 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder);
Selim Cinek384804b2018-04-18 14:31:07 +08006663 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource(),
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006664 p, null /* result */);
Selim Cinek03d0d652015-11-13 13:18:09 -05006665 if (mSummaryTextSet) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006666 contentView.setTextViewText(R.id.text, mBuilder.processTextSpans(
6667 mBuilder.processLegacyText(mSummaryText)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006668 mBuilder.setTextViewColorSecondary(contentView, R.id.text, p);
Selim Cinekc848c3a2016-01-13 15:27:30 -08006669 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05006670 }
Selim Cinek279fa862016-06-14 10:57:25 -07006671 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08006672
Christoph Studer5c510ee2014-12-15 16:32:27 +01006673 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006674 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006675 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006676 }
6677
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006678 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006679 return contentView;
6680 }
6681
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006682 /**
6683 * @hide
6684 */
6685 public void addExtras(Bundle extras) {
6686 super.addExtras(extras);
6687
6688 if (mBigLargeIconSet) {
6689 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
6690 }
6691 extras.putParcelable(EXTRA_PICTURE, mPicture);
6692 }
6693
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006694 /**
6695 * @hide
6696 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006697 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02006698 protected void restoreFromExtras(Bundle extras) {
6699 super.restoreFromExtras(extras);
6700
6701 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01006702 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02006703 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04006704 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006705 mPicture = extras.getParcelable(EXTRA_PICTURE);
6706 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006707
6708 /**
6709 * @hide
6710 */
6711 @Override
6712 public boolean hasSummaryInHeader() {
6713 return false;
6714 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006715
6716 /**
6717 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006718 * Note that we aren't actually comparing the contents of the bitmaps here, so this
6719 * is only doing a cursory inspection. Bitmaps of equal size will appear the same.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006720 */
6721 @Override
6722 public boolean areNotificationsVisiblyDifferent(Style other) {
6723 if (other == null || getClass() != other.getClass()) {
6724 return true;
6725 }
6726 BigPictureStyle otherS = (BigPictureStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006727 return areBitmapsObviouslyDifferent(getBigPicture(), otherS.getBigPicture());
6728 }
6729
6730 private static boolean areBitmapsObviouslyDifferent(Bitmap a, Bitmap b) {
6731 if (a == b) {
6732 return false;
6733 }
6734 if (a == null || b == null) {
6735 return true;
6736 }
6737 return a.getWidth() != b.getWidth()
6738 || a.getHeight() != b.getHeight()
6739 || a.getConfig() != b.getConfig()
6740 || a.getGenerationId() != b.getGenerationId();
Julia Reynolds7217dc92018-03-07 12:12:09 -05006741 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006742 }
6743
6744 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006745 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08006746 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006747 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006748 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006749 * Notification notif = new Notification.Builder(mContext)
6750 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
6751 * .setContentText(subject)
6752 * .setSmallIcon(R.drawable.new_mail)
6753 * .setLargeIcon(aBitmap)
6754 * .setStyle(new Notification.BigTextStyle()
6755 * .bigText(aVeryLongString))
6756 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006757 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006758 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006759 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006760 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006761 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006762
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006763 private CharSequence mBigText;
6764
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006765 public BigTextStyle() {
6766 }
6767
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006768 /**
6769 * @deprecated use {@code BigTextStyle()}.
6770 */
6771 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006772 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006773 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006774 }
6775
Chris Wrend6297db2012-05-03 16:20:13 -04006776 /**
6777 * Overrides ContentTitle in the big form of the template.
6778 * This defaults to the value passed to setContentTitle().
6779 */
6780 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006781 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006782 return this;
6783 }
6784
6785 /**
6786 * Set the first line of text after the detail section in the big form of the template.
6787 */
6788 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006789 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006790 return this;
6791 }
6792
Chris Wren0bd664d2012-08-01 13:56:56 -04006793 /**
6794 * Provide the longer text to be displayed in the big form of the
6795 * template in place of the content text.
6796 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006797 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006798 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006799 return this;
6800 }
6801
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006802 /**
6803 * @hide
6804 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05006805 public CharSequence getBigText() {
6806 return mBigText;
6807 }
6808
6809 /**
6810 * @hide
6811 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006812 public void addExtras(Bundle extras) {
6813 super.addExtras(extras);
6814
Christoph Studer4600f9b2014-07-22 22:44:43 +02006815 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
6816 }
6817
6818 /**
6819 * @hide
6820 */
6821 @Override
6822 protected void restoreFromExtras(Bundle extras) {
6823 super.restoreFromExtras(extras);
6824
6825 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006826 }
6827
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006828 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006829 * @param increasedHeight true if this layout be created with an increased height.
6830 *
6831 * @hide
6832 */
6833 @Override
6834 public RemoteViews makeContentView(boolean increasedHeight) {
6835 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006836 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006837 mBuilder.mActions = new ArrayList<>();
6838 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006839 mBuilder.mActions = mBuilder.mOriginalActions;
6840 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006841 return remoteViews;
6842 }
6843 return super.makeContentView(increasedHeight);
6844 }
6845
6846 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006847 * @hide
6848 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006849 @Override
6850 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
6851 if (increasedHeight && mBuilder.mActions.size() > 0) {
6852 return makeBigContentView();
6853 }
6854 return super.makeHeadsUpContentView(increasedHeight);
6855 }
6856
6857 /**
6858 * @hide
6859 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006860 public RemoteViews makeBigContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006861 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder).text(null);
Selim Cinek384804b2018-04-18 14:31:07 +08006862 TemplateBindResult result = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006863 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource(), p,
6864 result);
Selim Cinek1c72fa02018-04-23 18:00:54 +08006865 contentView.setInt(R.id.big_text, "setImageEndMargin", result.getIconMarginEnd());
Joe Malin8d40d042012-11-05 11:36:40 -08006866
Selim Cinek3a2c4b92015-12-17 17:01:17 -08006867 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07006868 if (TextUtils.isEmpty(bigTextText)) {
6869 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
6870 // experience
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006871 bigTextText = mBuilder.processLegacyText(
6872 mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT));
Selim Cinek75998782016-04-26 10:39:17 -07006873 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006874 contentView.setTextViewText(R.id.big_text, mBuilder.processTextSpans(bigTextText));
6875 mBuilder.setTextViewColorSecondary(contentView, R.id.big_text, p);
6876 contentView.setViewVisibility(R.id.big_text,
6877 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08006878 contentView.setBoolean(R.id.big_text, "setHasImage",
6879 result.isRightIconContainerVisible());
Selim Cinek4fb12d32015-11-19 18:10:48 -08006880
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006881 return contentView;
6882 }
6883
Julia Reynolds7217dc92018-03-07 12:12:09 -05006884 /**
6885 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006886 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006887 */
6888 @Override
6889 public boolean areNotificationsVisiblyDifferent(Style other) {
6890 if (other == null || getClass() != other.getClass()) {
6891 return true;
6892 }
6893 BigTextStyle newS = (BigTextStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006894 return !Objects.equals(String.valueOf(getBigText()), String.valueOf(newS.getBigText()));
Julia Reynolds7217dc92018-03-07 12:12:09 -05006895 }
6896
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006897 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006898
6899 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006900 * Helper class for generating large-format notifications that include multiple back-and-forth
6901 * messages of varying types between any number of people.
6902 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006903 * <p>
Alex Hillsfc737de2016-03-23 17:33:02 -04006904 * If the platform does not provide large-format notifications, this method has no effect. The
6905 * user will always see the normal notification view.
Selim Cinekce8794f2018-05-23 16:46:05 -07006906 *
6907 * <p>
6908 * If the app is targeting Android P and above, it is required to use the {@link Person}
6909 * class in order to get an optimal rendering of the notification and its avatars. For
6910 * conversations involving multiple people, the app should also make sure that it marks the
6911 * conversation as a group with {@link #setGroupConversation(boolean)}.
6912 *
6913 * <p>
6914 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior.
6915 * Here's an example of how this may be used:
Alex Hillsfc737de2016-03-23 17:33:02 -04006916 * <pre class="prettyprint">
6917 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006918 * Person user = new Person.Builder().setIcon(userIcon).setName(userName).build();
6919 * MessagingStyle style = new MessagingStyle(user)
6920 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getPerson())
6921 * .addMessage(messages[2].getText(), messages[2].getTime(), messages[2].getPerson())
6922 * .setGroupConversation(hasMultiplePeople());
6923 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006924 * Notification noti = new Notification.Builder()
Selim Cinekce8794f2018-05-23 16:46:05 -07006925 * .setContentTitle(&quot;2 new messages with &quot; + sender.toString())
Alex Hillsfc737de2016-03-23 17:33:02 -04006926 * .setContentText(subject)
6927 * .setSmallIcon(R.drawable.new_message)
6928 * .setLargeIcon(aBitmap)
Selim Cinekce8794f2018-05-23 16:46:05 -07006929 * .setStyle(style)
Alex Hillsfc737de2016-03-23 17:33:02 -04006930 * .build();
6931 * </pre>
6932 */
6933 public static class MessagingStyle extends Style {
6934
6935 /**
6936 * The maximum number of messages that will be retained in the Notification itself (the
6937 * number displayed is up to the platform).
6938 */
6939 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
6940
Selim Cinekcb8b9852017-12-15 18:01:52 -08006941 @NonNull Person mUser;
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006942 @Nullable CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04006943 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08006944 List<Message> mHistoricMessages = new ArrayList<>();
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006945 boolean mIsGroupConversation;
Alex Hillsfc737de2016-03-23 17:33:02 -04006946
6947 MessagingStyle() {
6948 }
6949
6950 /**
Alex Hillsfd590442016-10-07 09:52:44 -04006951 * @param userDisplayName Required - the name to be displayed for any replies sent by the
6952 * user before the posting app reposts the notification with those messages after they've
6953 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04006954 * {@link #addMessage(Notification.MessagingStyle.Message)}
Selim Cinekcb8b9852017-12-15 18:01:52 -08006955 *
6956 * @deprecated use {@code MessagingStyle(Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006957 */
Alex Hillsfd590442016-10-07 09:52:44 -04006958 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Selim Cinek9acd6732018-03-23 16:39:02 -07006959 this(new Person.Builder().setName(userDisplayName).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006960 }
6961
6962 /**
6963 * @param user Required - The person displayed for any messages that are sent by the
6964 * user. Any messages added with {@link #addMessage(Notification.MessagingStyle.Message)}
6965 * who don't have a Person associated with it will be displayed as if they were sent
Selim Cinek90343862018-02-01 11:07:11 -08006966 * by this user. The user also needs to have a valid name associated with it, which will
6967 * be enforced starting in Android P.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006968 */
6969 public MessagingStyle(@NonNull Person user) {
6970 mUser = user;
Selim Cinek90343862018-02-01 11:07:11 -08006971 }
6972
6973 /**
6974 * Validate that this style was properly composed. This is called at build time.
6975 * @hide
6976 */
6977 @Override
6978 public void validate(Context context) {
6979 super.validate(context);
6980 if (context.getApplicationInfo().targetSdkVersion
6981 >= Build.VERSION_CODES.P && (mUser == null || mUser.getName() == null)) {
6982 throw new RuntimeException("User must be valid and have a name.");
Selim Cinekcb8b9852017-12-15 18:01:52 -08006983 }
6984 }
6985
6986 /**
koprivaa1a78482018-10-09 10:09:23 -07006987 * @return the text that should be displayed in the statusBar when heads upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006988 * If {@code null} is returned, the default implementation will be used.
6989 *
6990 * @hide
6991 */
6992 @Override
6993 public CharSequence getHeadsUpStatusBarText() {
6994 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
6995 ? super.mBigContentTitle
6996 : mConversationTitle;
6997 if (!TextUtils.isEmpty(conversationTitle) && !hasOnlyWhiteSpaceSenders()) {
6998 return conversationTitle;
6999 }
7000 return null;
7001 }
7002
7003 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08007004 * @return the user to be displayed for any replies sent by the user
7005 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007006 @NonNull
Selim Cinekcb8b9852017-12-15 18:01:52 -08007007 public Person getUser() {
7008 return mUser;
Alex Hillsfc737de2016-03-23 17:33:02 -04007009 }
7010
7011 /**
7012 * Returns the name to be displayed for any replies sent by the user
Selim Cinekcb8b9852017-12-15 18:01:52 -08007013 *
7014 * @deprecated use {@link #getUser()} instead
Alex Hillsfc737de2016-03-23 17:33:02 -04007015 */
7016 public CharSequence getUserDisplayName() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007017 return mUser.getName();
Alex Hillsfc737de2016-03-23 17:33:02 -04007018 }
7019
7020 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007021 * Sets the title to be displayed on this conversation. May be set to {@code null}.
7022 *
Kodlee Yin14656422017-12-22 17:00:46 -08007023 * <p>This API's behavior was changed in SDK version {@link Build.VERSION_CODES#P}. If your
7024 * application's target version is less than {@link Build.VERSION_CODES#P}, setting a
7025 * conversation title to a non-null value will make {@link #isGroupConversation()} return
7026 * {@code true} and passing {@code null} will make it return {@code false}. In
7027 * {@link Build.VERSION_CODES#P} and beyond, use {@link #setGroupConversation(boolean)}
7028 * to set group conversation status.
7029 *
7030 * @param conversationTitle Title displayed for this conversation
7031 * @return this object for method chaining
Alex Hillsfc737de2016-03-23 17:33:02 -04007032 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007033 public MessagingStyle setConversationTitle(@Nullable CharSequence conversationTitle) {
Alex Hillsfc737de2016-03-23 17:33:02 -04007034 mConversationTitle = conversationTitle;
7035 return this;
7036 }
7037
7038 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007039 * Return the title to be displayed on this conversation. May return {@code null}.
Alex Hillsfc737de2016-03-23 17:33:02 -04007040 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007041 @Nullable
Alex Hillsfc737de2016-03-23 17:33:02 -04007042 public CharSequence getConversationTitle() {
7043 return mConversationTitle;
7044 }
7045
7046 /**
7047 * Adds a message for display by this notification. Convenience call for a simple
7048 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
7049 * @param text A {@link CharSequence} to be displayed as the message content
7050 * @param timestamp Time at which the message arrived
7051 * @param sender A {@link CharSequence} to be used for displaying the name of the
7052 * sender. Should be <code>null</code> for messages by the current user, in which case
7053 * the platform will insert {@link #getUserDisplayName()}.
7054 * Should be unique amongst all individuals in the conversation, and should be
7055 * consistent during re-posts of the notification.
7056 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07007057 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Alex Hillsfc737de2016-03-23 17:33:02 -04007058 *
7059 * @return this object for method chaining
Selim Cinekcb8b9852017-12-15 18:01:52 -08007060 *
7061 * @deprecated use {@link #addMessage(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04007062 */
7063 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007064 return addMessage(text, timestamp,
Selim Cinek9acd6732018-03-23 16:39:02 -07007065 sender == null ? null : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08007066 }
7067
7068 /**
7069 * Adds a message for display by this notification. Convenience call for a simple
7070 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
7071 * @param text A {@link CharSequence} to be displayed as the message content
7072 * @param timestamp Time at which the message arrived
7073 * @param sender The {@link Person} who sent the message.
7074 * Should be <code>null</code> for messages by the current user, in which case
7075 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7076 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07007077 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Selim Cinekcb8b9852017-12-15 18:01:52 -08007078 *
7079 * @return this object for method chaining
7080 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007081 public MessagingStyle addMessage(@NonNull CharSequence text, long timestamp,
7082 @Nullable Person sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08007083 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04007084 }
7085
7086 /**
7087 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08007088 *
7089 * <p>The messages should be added in chronologic order, i.e. the oldest first,
7090 * the newest last.
7091 *
Alex Hillsfc737de2016-03-23 17:33:02 -04007092 * @param message The {@link Message} to be displayed
7093 * @return this object for method chaining
7094 */
7095 public MessagingStyle addMessage(Message message) {
7096 mMessages.add(message);
7097 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
7098 mMessages.remove(0);
7099 }
7100 return this;
7101 }
7102
7103 /**
Adrian Roos437cd562017-01-18 15:47:03 -08007104 * Adds a {@link Message} for historic context in this notification.
7105 *
7106 * <p>Messages should be added as historic if they are not the main subject of the
7107 * notification but may give context to a conversation. The system may choose to present
7108 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
7109 *
7110 * <p>The messages should be added in chronologic order, i.e. the oldest first,
7111 * the newest last.
7112 *
7113 * @param message The historic {@link Message} to be added
7114 * @return this object for method chaining
7115 */
7116 public MessagingStyle addHistoricMessage(Message message) {
7117 mHistoricMessages.add(message);
7118 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
7119 mHistoricMessages.remove(0);
7120 }
7121 return this;
7122 }
7123
7124 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007125 * Gets the list of {@code Message} objects that represent the notification
7126 */
7127 public List<Message> getMessages() {
7128 return mMessages;
7129 }
7130
7131 /**
Adrian Roos437cd562017-01-18 15:47:03 -08007132 * Gets the list of historic {@code Message}s in the notification.
7133 */
7134 public List<Message> getHistoricMessages() {
7135 return mHistoricMessages;
7136 }
7137
7138 /**
Selim Cinekce8794f2018-05-23 16:46:05 -07007139 * Sets whether this conversation notification represents a group. If the app is targeting
7140 * Android P, this is required if the app wants to display the largeIcon set with
7141 * {@link Notification.Builder#setLargeIcon(Bitmap)}, otherwise it will be hidden.
Kodlee Yin14656422017-12-22 17:00:46 -08007142 *
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007143 * @param isGroupConversation {@code true} if the conversation represents a group,
7144 * {@code false} otherwise.
7145 * @return this object for method chaining
7146 */
7147 public MessagingStyle setGroupConversation(boolean isGroupConversation) {
7148 mIsGroupConversation = isGroupConversation;
7149 return this;
7150 }
7151
7152 /**
Kodlee Yin14656422017-12-22 17:00:46 -08007153 * Returns {@code true} if this notification represents a group conversation, otherwise
7154 * {@code false}.
7155 *
7156 * <p> If the application that generated this {@link MessagingStyle} targets an SDK version
7157 * less than {@link Build.VERSION_CODES#P}, this method becomes dependent on whether or
7158 * not the conversation title is set; returning {@code true} if the conversation title is
7159 * a non-null value, or {@code false} otherwise. From {@link Build.VERSION_CODES#P} forward,
7160 * this method returns what's set by {@link #setGroupConversation(boolean)} allowing for
7161 * named, non-group conversations.
7162 *
7163 * @see #setConversationTitle(CharSequence)
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007164 */
7165 public boolean isGroupConversation() {
Kodlee Yin14656422017-12-22 17:00:46 -08007166 // When target SDK version is < P, a non-null conversation title dictates if this is
7167 // as group conversation.
7168 if (mBuilder != null
7169 && mBuilder.mContext.getApplicationInfo().targetSdkVersion
7170 < Build.VERSION_CODES.P) {
7171 return mConversationTitle != null;
7172 }
7173
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007174 return mIsGroupConversation;
7175 }
7176
7177 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007178 * @hide
7179 */
7180 @Override
7181 public void addExtras(Bundle extras) {
7182 super.addExtras(extras);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007183 if (mUser != null) {
7184 // For legacy usages
7185 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUser.getName());
7186 extras.putParcelable(EXTRA_MESSAGING_PERSON, mUser);
Alex Hillsfc737de2016-03-23 17:33:02 -04007187 }
7188 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007189 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04007190 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007191 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
7192 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04007193 }
Adrian Roos437cd562017-01-18 15:47:03 -08007194 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
7195 Message.getBundleArrayForMessages(mHistoricMessages));
7196 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007197
7198 fixTitleAndTextExtras(extras);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007199 extras.putBoolean(EXTRA_IS_GROUP_CONVERSATION, mIsGroupConversation);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007200 }
7201
7202 private void fixTitleAndTextExtras(Bundle extras) {
7203 Message m = findLatestIncomingMessage();
7204 CharSequence text = (m == null) ? null : m.mText;
7205 CharSequence sender = m == null ? null
Selim Cinekcb8b9852017-12-15 18:01:52 -08007206 : m.mSender == null || TextUtils.isEmpty(m.mSender.getName())
7207 ? mUser.getName() : m.mSender.getName();
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007208 CharSequence title;
7209 if (!TextUtils.isEmpty(mConversationTitle)) {
7210 if (!TextUtils.isEmpty(sender)) {
7211 BidiFormatter bidi = BidiFormatter.getInstance();
7212 title = mBuilder.mContext.getString(
7213 com.android.internal.R.string.notification_messaging_title_template,
Selim Cinekcb8b9852017-12-15 18:01:52 -08007214 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(sender));
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007215 } else {
7216 title = mConversationTitle;
7217 }
7218 } else {
7219 title = sender;
7220 }
7221
7222 if (title != null) {
7223 extras.putCharSequence(EXTRA_TITLE, title);
7224 }
7225 if (text != null) {
7226 extras.putCharSequence(EXTRA_TEXT, text);
7227 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007228 }
7229
7230 /**
7231 * @hide
7232 */
7233 @Override
7234 protected void restoreFromExtras(Bundle extras) {
7235 super.restoreFromExtras(extras);
7236
Selim Cinekcb8b9852017-12-15 18:01:52 -08007237 mUser = extras.getParcelable(EXTRA_MESSAGING_PERSON);
7238 if (mUser == null) {
7239 CharSequence displayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
Selim Cinek9acd6732018-03-23 16:39:02 -07007240 mUser = new Person.Builder().setName(displayName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007241 }
Adrian Roos96b7e202016-05-17 13:50:38 -07007242 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08007243 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007244 mMessages = Message.getMessagesFromBundleArray(messages);
Adrian Roos437cd562017-01-18 15:47:03 -08007245 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007246 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007247 mIsGroupConversation = extras.getBoolean(EXTRA_IS_GROUP_CONVERSATION);
Alex Hillsfc737de2016-03-23 17:33:02 -04007248 }
7249
7250 /**
7251 * @hide
7252 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07007253 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007254 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07007255 mBuilder.mOriginalActions = mBuilder.mActions;
7256 mBuilder.mActions = new ArrayList<>();
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007257 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007258 false /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007259 mBuilder.mActions = mBuilder.mOriginalActions;
7260 mBuilder.mOriginalActions = null;
7261 return remoteViews;
Adrian Roosc1a80b02016-04-05 14:54:55 -07007262 }
7263
Julia Reynolds7217dc92018-03-07 12:12:09 -05007264 /**
7265 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04007266 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05007267 */
7268 @Override
7269 public boolean areNotificationsVisiblyDifferent(Style other) {
7270 if (other == null || getClass() != other.getClass()) {
7271 return true;
7272 }
7273 MessagingStyle newS = (MessagingStyle) other;
7274 List<MessagingStyle.Message> oldMs = getMessages();
7275 List<MessagingStyle.Message> newMs = newS.getMessages();
7276
Dan Sandler7d67bd42018-05-15 14:06:38 -04007277 if (oldMs == null || newMs == null) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007278 newMs = new ArrayList<>();
7279 }
7280
7281 int n = oldMs.size();
7282 if (n != newMs.size()) {
7283 return true;
7284 }
7285 for (int i = 0; i < n; i++) {
7286 MessagingStyle.Message oldM = oldMs.get(i);
7287 MessagingStyle.Message newM = newMs.get(i);
Dan Sandler7d67bd42018-05-15 14:06:38 -04007288 if (!Objects.equals(
7289 String.valueOf(oldM.getText()),
7290 String.valueOf(newM.getText()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007291 return true;
7292 }
7293 if (!Objects.equals(oldM.getDataUri(), newM.getDataUri())) {
7294 return true;
7295 }
Dan Sandler7d67bd42018-05-15 14:06:38 -04007296 String oldSender = String.valueOf(oldM.getSenderPerson() == null
7297 ? oldM.getSender()
7298 : oldM.getSenderPerson().getName());
7299 String newSender = String.valueOf(newM.getSenderPerson() == null
7300 ? newM.getSender()
7301 : newM.getSenderPerson().getName());
Julia Reynolds7217dc92018-03-07 12:12:09 -05007302 if (!Objects.equals(oldSender, newSender)) {
7303 return true;
7304 }
7305
7306 String oldKey = oldM.getSenderPerson() == null
7307 ? null : oldM.getSenderPerson().getKey();
7308 String newKey = newM.getSenderPerson() == null
7309 ? null : newM.getSenderPerson().getKey();
7310 if (!Objects.equals(oldKey, newKey)) {
7311 return true;
7312 }
7313 // Other fields (like timestamp) intentionally excluded
7314 }
7315 return false;
7316 }
7317
Adrian Roosc1a80b02016-04-05 14:54:55 -07007318 private Message findLatestIncomingMessage() {
Selim Cinek88188f22017-09-19 16:46:56 -07007319 return findLatestIncomingMessage(mMessages);
7320 }
7321
7322 /**
7323 * @hide
7324 */
7325 @Nullable
7326 public static Message findLatestIncomingMessage(
7327 List<Message> messages) {
7328 for (int i = messages.size() - 1; i >= 0; i--) {
7329 Message m = messages.get(i);
Adrian Roosc1a80b02016-04-05 14:54:55 -07007330 // Incoming messages have a non-empty sender.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007331 if (m.mSender != null && !TextUtils.isEmpty(m.mSender.getName())) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07007332 return m;
7333 }
7334 }
Selim Cinek88188f22017-09-19 16:46:56 -07007335 if (!messages.isEmpty()) {
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007336 // No incoming messages, fall back to outgoing message
Selim Cinek88188f22017-09-19 16:46:56 -07007337 return messages.get(messages.size() - 1);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007338 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07007339 return null;
7340 }
7341
7342 /**
7343 * @hide
7344 */
7345 @Override
7346 public RemoteViews makeBigContentView() {
Selim Cinek384804b2018-04-18 14:31:07 +08007347 return makeMessagingView(false /* displayImagesAtEnd */, true /* hideLargeIcon */);
Selim Cinekafeed292017-12-12 17:32:44 -08007348 }
7349
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007350 /**
7351 * Create a messaging layout.
7352 *
7353 * @param displayImagesAtEnd should images be displayed at the end of the content instead
7354 * of inline.
Selim Cinek384804b2018-04-18 14:31:07 +08007355 * @param hideRightIcons Should the reply affordance be shown at the end of the notification
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007356 * @return the created remoteView.
7357 */
Selim Cinekafeed292017-12-12 17:32:44 -08007358 @NonNull
Selim Cinek384804b2018-04-18 14:31:07 +08007359 private RemoteViews makeMessagingView(boolean displayImagesAtEnd, boolean hideRightIcons) {
Selim Cinek88188f22017-09-19 16:46:56 -07007360 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
Adrian Roosc1a80b02016-04-05 14:54:55 -07007361 ? super.mBigContentTitle
7362 : mConversationTitle;
Selim Cinekce8794f2018-05-23 16:46:05 -07007363 boolean atLeastP = mBuilder.mContext.getApplicationInfo().targetSdkVersion
7364 >= Build.VERSION_CODES.P;
7365 boolean isOneToOne;
Selim Cinek2dd3e722018-01-19 11:06:06 -08007366 CharSequence nameReplacement = null;
Selim Cinekce8794f2018-05-23 16:46:05 -07007367 Icon avatarReplacement = null;
7368 if (!atLeastP) {
7369 isOneToOne = TextUtils.isEmpty(conversationTitle);
7370 avatarReplacement = mBuilder.mN.mLargeIcon;
7371 if (hasOnlyWhiteSpaceSenders()) {
7372 isOneToOne = true;
7373 nameReplacement = conversationTitle;
7374 conversationTitle = null;
7375 }
7376 } else {
7377 isOneToOne = !isGroupConversation();
Adrian Roosb1f427c2016-05-26 12:27:15 -07007378 }
Selim Cinek384804b2018-04-18 14:31:07 +08007379 TemplateBindResult bindResult = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007380 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).title(
7381 conversationTitle).text(null)
7382 .hideLargeIcon(hideRightIcons || isOneToOne)
7383 .hideReplyIcon(hideRightIcons)
7384 .headerTextSecondary(conversationTitle);
Adrian Roos48d746a2016-04-12 14:57:28 -07007385 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07007386 mBuilder.getMessagingLayoutResource(),
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007387 p,
Selim Cinek384804b2018-04-18 14:31:07 +08007388 bindResult);
Selim Cinek88188f22017-09-19 16:46:56 -07007389 addExtras(mBuilder.mN.extras);
Selim Cinekafeed292017-12-12 17:32:44 -08007390 // also update the end margin if there is an image
Selim Cinek1c72fa02018-04-23 18:00:54 +08007391 contentView.setViewLayoutMarginEnd(R.id.notification_messaging,
7392 bindResult.getIconMarginEnd());
Selim Cinek88188f22017-09-19 16:46:56 -07007393 contentView.setInt(R.id.status_bar_latest_event_content, "setLayoutColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007394 mBuilder.isColorized(p) ? mBuilder.getPrimaryTextColor(p)
7395 : mBuilder.resolveContrastColor(p));
Kenny Guy14d035c2018-05-02 19:10:36 +01007396 contentView.setInt(R.id.status_bar_latest_event_content, "setSenderTextColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007397 mBuilder.getPrimaryTextColor(p));
Kenny Guy14d035c2018-05-02 19:10:36 +01007398 contentView.setInt(R.id.status_bar_latest_event_content, "setMessageTextColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007399 mBuilder.getSecondaryTextColor(p));
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007400 contentView.setBoolean(R.id.status_bar_latest_event_content, "setDisplayImagesAtEnd",
7401 displayImagesAtEnd);
Selim Cinekce8794f2018-05-23 16:46:05 -07007402 contentView.setIcon(R.id.status_bar_latest_event_content, "setAvatarReplacement",
7403 avatarReplacement);
Selim Cinek2dd3e722018-01-19 11:06:06 -08007404 contentView.setCharSequence(R.id.status_bar_latest_event_content, "setNameReplacement",
7405 nameReplacement);
Selim Cinek88188f22017-09-19 16:46:56 -07007406 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsOneToOne",
7407 isOneToOne);
7408 contentView.setBundle(R.id.status_bar_latest_event_content, "setData",
7409 mBuilder.mN.extras);
Alex Hillsfc737de2016-03-23 17:33:02 -04007410 return contentView;
7411 }
7412
Selim Cinekf7409db2017-10-24 16:17:14 -07007413 private boolean hasOnlyWhiteSpaceSenders() {
7414 for (int i = 0; i < mMessages.size(); i++) {
7415 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007416 Person sender = m.getSenderPerson();
7417 if (sender != null && !isWhiteSpace(sender.getName())) {
Selim Cinekf7409db2017-10-24 16:17:14 -07007418 return false;
7419 }
7420 }
7421 return true;
7422 }
7423
7424 private boolean isWhiteSpace(CharSequence sender) {
7425 if (TextUtils.isEmpty(sender)) {
7426 return true;
7427 }
7428 if (sender.toString().matches("^\\s*$")) {
7429 return true;
7430 }
7431 // Let's check if we only have 0 whitespace chars. Some apps did this as a workaround
7432 // For the presentation that we had.
7433 for (int i = 0; i < sender.length(); i++) {
7434 char c = sender.charAt(i);
7435 if (c != '\u200B') {
7436 return false;
7437 }
7438 }
7439 return true;
7440 }
7441
Selim Cinek88188f22017-09-19 16:46:56 -07007442 private CharSequence createConversationTitleFromMessages() {
7443 ArraySet<CharSequence> names = new ArraySet<>();
7444 for (int i = 0; i < mMessages.size(); i++) {
7445 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007446 Person sender = m.getSenderPerson();
Selim Cinek88188f22017-09-19 16:46:56 -07007447 if (sender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007448 names.add(sender.getName());
Selim Cinek88188f22017-09-19 16:46:56 -07007449 }
7450 }
7451 SpannableStringBuilder title = new SpannableStringBuilder();
7452 int size = names.size();
7453 for (int i = 0; i < size; i++) {
7454 CharSequence name = names.valueAt(i);
7455 if (!TextUtils.isEmpty(title)) {
7456 title.append(", ");
7457 }
7458 title.append(BidiFormatter.getInstance().unicodeWrap(name));
7459 }
7460 return title;
7461 }
7462
Adrian Roosdedd1df2016-04-26 16:38:47 -07007463 /**
7464 * @hide
7465 */
7466 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007467 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007468 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007469 true /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007470 remoteViews.setInt(R.id.notification_messaging, "setMaxDisplayedLines", 1);
7471 return remoteViews;
Adrian Roosdedd1df2016-04-26 16:38:47 -07007472 }
7473
Adrian Roosc1a80b02016-04-05 14:54:55 -07007474 private static TextAppearanceSpan makeFontColorSpan(int color) {
7475 return new TextAppearanceSpan(null, 0, 0,
7476 ColorStateList.valueOf(color), null);
7477 }
7478
Alex Hillsd9b04d92016-04-11 16:38:16 -04007479 public static final class Message {
Tony Mak09db2ea2018-06-27 18:12:48 +01007480 /** @hide */
7481 public static final String KEY_TEXT = "text";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007482 static final String KEY_TIMESTAMP = "time";
7483 static final String KEY_SENDER = "sender";
Selim Cinekcb8b9852017-12-15 18:01:52 -08007484 static final String KEY_SENDER_PERSON = "sender_person";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007485 static final String KEY_DATA_MIME_TYPE = "type";
7486 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08007487 static final String KEY_EXTRAS_BUNDLE = "extras";
Kenny Guya0f6de82018-04-06 16:20:16 +01007488 static final String KEY_REMOTE_INPUT_HISTORY = "remote_input_history";
Alex Hillsfc737de2016-03-23 17:33:02 -04007489
7490 private final CharSequence mText;
7491 private final long mTimestamp;
Selim Cinekcb8b9852017-12-15 18:01:52 -08007492 @Nullable
7493 private final Person mSender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007494 /** True if this message was generated from the extra
7495 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}
7496 */
7497 private final boolean mRemoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007498
Shane Brennan5a871862017-03-11 13:14:17 -08007499 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007500 private String mDataMimeType;
7501 private Uri mDataUri;
7502
7503 /**
7504 * Constructor
7505 * @param text A {@link CharSequence} to be displayed as the message content
7506 * @param timestamp Time at which the message arrived
7507 * @param sender A {@link CharSequence} to be used for displaying the name of the
7508 * sender. Should be <code>null</code> for messages by the current user, in which case
7509 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
7510 * Should be unique amongst all individuals in the conversation, and should be
7511 * consistent during re-posts of the notification.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007512 *
7513 * @deprecated use {@code Message(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04007514 */
7515 public Message(CharSequence text, long timestamp, CharSequence sender){
Selim Cinek9acd6732018-03-23 16:39:02 -07007516 this(text, timestamp, sender == null ? null
7517 : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08007518 }
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 The {@link Person} who sent the message.
7525 * Should be <code>null</code> for messages by the current user, in which case
7526 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7527 * <p>
Selim Cinek9acd6732018-03-23 16:39:02 -07007528 * The person provided should contain an Icon, set with
7529 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7530 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7531 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7532 * to differentiate between the different users.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007533 * </p>
7534 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007535 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender) {
Kenny Guya0f6de82018-04-06 16:20:16 +01007536 this(text, timestamp, sender, false /* remoteHistory */);
7537 }
7538
7539 /**
7540 * Constructor
7541 * @param text A {@link CharSequence} to be displayed as the message content
7542 * @param timestamp Time at which the message arrived
7543 * @param sender The {@link Person} who sent the message.
7544 * Should be <code>null</code> for messages by the current user, in which case
7545 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7546 * @param remoteInputHistory True if the messages was generated from the extra
7547 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7548 * <p>
7549 * The person provided should contain an Icon, set with
7550 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7551 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7552 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7553 * to differentiate between the different users.
7554 * </p>
7555 * @hide
7556 */
7557 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender,
7558 boolean remoteInputHistory) {
Alex Hillsfc737de2016-03-23 17:33:02 -04007559 mText = text;
7560 mTimestamp = timestamp;
7561 mSender = sender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007562 mRemoteInputHistory = remoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007563 }
7564
7565 /**
7566 * Sets a binary blob of data and an associated MIME type for a message. In the case
7567 * where the platform doesn't support the MIME type, the original text provided in the
7568 * constructor will be used.
7569 * @param dataMimeType The MIME type of the content. See
7570 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
7571 * types on Android and Android Wear.
7572 * @param dataUri The uri containing the content whose type is given by the MIME type.
7573 * <p class="note">
7574 * <ol>
7575 * <li>Notification Listeners including the System UI need permission to access the
7576 * data the Uri points to. The recommended ways to do this are:</li>
7577 * <li>Store the data in your own ContentProvider, making sure that other apps have
7578 * the correct permission to access your provider. The preferred mechanism for
7579 * providing access is to use per-URI permissions which are temporary and only
7580 * grant access to the receiving application. An easy way to create a
7581 * ContentProvider like this is to use the FileProvider helper class.</li>
7582 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
7583 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
7584 * also store non-media types (see MediaStore.Files for more info). Files can be
7585 * inserted into the MediaStore using scanFile() after which a content:// style
7586 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
7587 * Note that once added to the system MediaStore the content is accessible to any
7588 * app on the device.</li>
7589 * </ol>
7590 * @return this object for method chaining
7591 */
7592 public Message setData(String dataMimeType, Uri dataUri) {
7593 mDataMimeType = dataMimeType;
7594 mDataUri = dataUri;
7595 return this;
7596 }
7597
Alex Hillsfc737de2016-03-23 17:33:02 -04007598 /**
7599 * Get the text to be used for this message, or the fallback text if a type and content
7600 * Uri have been set
7601 */
7602 public CharSequence getText() {
7603 return mText;
7604 }
7605
7606 /**
7607 * Get the time at which this message arrived
7608 */
7609 public long getTimestamp() {
7610 return mTimestamp;
7611 }
7612
7613 /**
Shane Brennan5a871862017-03-11 13:14:17 -08007614 * Get the extras Bundle for this message.
7615 */
7616 public Bundle getExtras() {
7617 return mExtras;
7618 }
7619
7620 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007621 * Get the text used to display the contact's name in the messaging experience
Selim Cinekcb8b9852017-12-15 18:01:52 -08007622 *
7623 * @deprecated use {@link #getSenderPerson()}
Alex Hillsfc737de2016-03-23 17:33:02 -04007624 */
7625 public CharSequence getSender() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007626 return mSender == null ? null : mSender.getName();
7627 }
7628
7629 /**
7630 * Get the sender associated with this message.
7631 */
7632 @Nullable
7633 public Person getSenderPerson() {
Alex Hillsfc737de2016-03-23 17:33:02 -04007634 return mSender;
7635 }
7636
7637 /**
7638 * Get the MIME type of the data pointed to by the Uri
7639 */
7640 public String getDataMimeType() {
7641 return mDataMimeType;
7642 }
7643
7644 /**
koprivaa1a78482018-10-09 10:09:23 -07007645 * Get the Uri pointing to the content of the message. Can be null, in which case
Alex Hillsfc737de2016-03-23 17:33:02 -04007646 * {@see #getText()} is used.
7647 */
7648 public Uri getDataUri() {
7649 return mDataUri;
7650 }
7651
Kenny Guya0f6de82018-04-06 16:20:16 +01007652 /**
7653 * @return True if the message was generated from
7654 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7655 * @hide
7656 */
7657 public boolean isRemoteInputHistory() {
7658 return mRemoteInputHistory;
7659 }
7660
Beverlye98937a2018-11-15 10:18:57 -05007661 /**
7662 * @hide
7663 */
7664 @VisibleForTesting
7665 public Bundle toBundle() {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007666 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007667 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007668 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04007669 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007670 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04007671 if (mSender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007672 // Legacy listeners need this
7673 bundle.putCharSequence(KEY_SENDER, mSender.getName());
7674 bundle.putParcelable(KEY_SENDER_PERSON, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04007675 }
7676 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007677 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04007678 }
7679 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007680 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04007681 }
Shane Brennan5a871862017-03-11 13:14:17 -08007682 if (mExtras != null) {
7683 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
7684 }
Kenny Guya0f6de82018-04-06 16:20:16 +01007685 if (mRemoteInputHistory) {
7686 bundle.putBoolean(KEY_REMOTE_INPUT_HISTORY, mRemoteInputHistory);
7687 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007688 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04007689 }
7690
Alex Hillsd9b04d92016-04-11 16:38:16 -04007691 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
7692 Bundle[] bundles = new Bundle[messages.size()];
7693 final int N = messages.size();
7694 for (int i = 0; i < N; i++) {
7695 bundles[i] = messages.get(i).toBundle();
7696 }
7697 return bundles;
7698 }
7699
Selim Cinek88188f22017-09-19 16:46:56 -07007700 /**
7701 * @return A list of messages read from the bundles.
7702 *
7703 * @hide
7704 */
7705 public static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
7706 if (bundles == null) {
7707 return new ArrayList<>();
7708 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007709 List<Message> messages = new ArrayList<>(bundles.length);
7710 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07007711 if (bundles[i] instanceof Bundle) {
7712 Message message = getMessageFromBundle((Bundle)bundles[i]);
7713 if (message != null) {
7714 messages.add(message);
7715 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007716 }
7717 }
7718 return messages;
7719 }
7720
Selim Cinekb0dc61b2018-05-22 18:49:36 -07007721 /**
7722 * @return The message that is stored in the bundle or null if the message couldn't be
7723 * resolved.
7724 *
7725 * @hide
7726 */
7727 @Nullable
7728 public static Message getMessageFromBundle(Bundle bundle) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007729 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07007730 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007731 return null;
7732 } else {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007733
7734 Person senderPerson = bundle.getParcelable(KEY_SENDER_PERSON);
7735 if (senderPerson == null) {
7736 // Legacy apps that use compat don't actually provide the sender objects
7737 // We need to fix the compat version to provide people / use
7738 // the native api instead
7739 CharSequence senderName = bundle.getCharSequence(KEY_SENDER);
7740 if (senderName != null) {
Selim Cinek9acd6732018-03-23 16:39:02 -07007741 senderPerson = new Person.Builder().setName(senderName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007742 }
7743 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007744 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
Selim Cinekcb8b9852017-12-15 18:01:52 -08007745 bundle.getLong(KEY_TIMESTAMP),
Kenny Guya0f6de82018-04-06 16:20:16 +01007746 senderPerson,
7747 bundle.getBoolean(KEY_REMOTE_INPUT_HISTORY, false));
Alex Hillsd9b04d92016-04-11 16:38:16 -04007748 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
7749 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007750 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
7751 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04007752 }
Shane Brennan5a871862017-03-11 13:14:17 -08007753 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
7754 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
7755 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007756 return message;
7757 }
7758 } catch (ClassCastException e) {
7759 return null;
7760 }
7761 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007762 }
7763 }
7764
7765 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04007766 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08007767 *
Robert Ly91c5ce32014-06-08 15:37:00 -07007768 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04007769 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07007770 * Notification notif = new Notification.Builder(mContext)
7771 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
7772 * .setContentText(subject)
7773 * .setSmallIcon(R.drawable.new_mail)
7774 * .setLargeIcon(aBitmap)
7775 * .setStyle(new Notification.InboxStyle()
7776 * .addLine(str1)
7777 * .addLine(str2)
7778 * .setContentTitle(&quot;&quot;)
7779 * .setSummaryText(&quot;+3 more&quot;))
7780 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04007781 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08007782 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04007783 * @see Notification#bigContentView
7784 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007785 public static class InboxStyle extends Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07007786
7787 /**
7788 * The number of lines of remote input history allowed until we start reducing lines.
7789 */
7790 private static final int NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION = 1;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007791 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
7792
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007793 public InboxStyle() {
7794 }
7795
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007796 /**
7797 * @deprecated use {@code InboxStyle()}.
7798 */
7799 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04007800 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007801 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04007802 }
7803
Chris Wrend6297db2012-05-03 16:20:13 -04007804 /**
7805 * Overrides ContentTitle in the big form of the template.
7806 * This defaults to the value passed to setContentTitle().
7807 */
7808 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007809 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04007810 return this;
7811 }
7812
7813 /**
7814 * Set the first line of text after the detail section in the big form of the template.
7815 */
7816 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007817 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04007818 return this;
7819 }
7820
Chris Wren0bd664d2012-08-01 13:56:56 -04007821 /**
7822 * Append a line to the digest section of the Inbox notification.
7823 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04007824 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007825 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04007826 return this;
7827 }
7828
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007829 /**
7830 * @hide
7831 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05007832 public ArrayList<CharSequence> getLines() {
7833 return mTexts;
7834 }
7835
7836 /**
7837 * @hide
7838 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007839 public void addExtras(Bundle extras) {
7840 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007841
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007842 CharSequence[] a = new CharSequence[mTexts.size()];
7843 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
7844 }
7845
Christoph Studer4600f9b2014-07-22 22:44:43 +02007846 /**
7847 * @hide
7848 */
7849 @Override
7850 protected void restoreFromExtras(Bundle extras) {
7851 super.restoreFromExtras(extras);
7852
7853 mTexts.clear();
7854 if (extras.containsKey(EXTRA_TEXT_LINES)) {
7855 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
7856 }
7857 }
7858
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007859 /**
7860 * @hide
7861 */
7862 public RemoteViews makeBigContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007863 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder).text(null);
Selim Cinek384804b2018-04-18 14:31:07 +08007864 TemplateBindResult result = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007865 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource(), p, result);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007866
Chris Wrend6297db2012-05-03 16:20:13 -04007867 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 -04007868 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04007869
Chris Wren4ed80d52012-05-17 09:30:03 -04007870 // Make sure all rows are gone in case we reuse a view.
7871 for (int rowId : rowIds) {
7872 contentView.setViewVisibility(rowId, View.GONE);
7873 }
7874
Daniel Sandler879c5e02012-04-17 16:46:51 -04007875 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07007876 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7877 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08007878 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07007879 int onlyViewId = 0;
7880 int maxRows = rowIds.length;
7881 if (mBuilder.mActions.size() > 0) {
7882 maxRows--;
7883 }
Selim Cinekbee4e072018-05-21 22:06:43 -07007884 CharSequence[] remoteInputHistory = mBuilder.mN.extras.getCharSequenceArray(
7885 EXTRA_REMOTE_INPUT_HISTORY);
7886 if (remoteInputHistory != null
7887 && remoteInputHistory.length > NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION) {
7888 // Let's remove some messages to make room for the remote input history.
7889 // 1 is always able to fit, but let's remove them if they are 2 or 3
7890 int numRemoteInputs = Math.min(remoteInputHistory.length,
7891 MAX_REMOTE_INPUT_HISTORY_LINES);
7892 int totalNumRows = mTexts.size() + numRemoteInputs
7893 - NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION;
7894 if (totalNumRows > maxRows) {
7895 int overflow = totalNumRows - maxRows;
7896 if (mTexts.size() > maxRows) {
7897 // Heuristic: if the Texts don't fit anyway, we'll rather drop the last
7898 // few messages, even with the remote input
7899 maxRows -= overflow;
7900 } else {
7901 // otherwise we drop the first messages
7902 i = overflow;
7903 }
7904 }
7905 }
Selim Cinek07c80172016-04-21 16:40:47 -07007906 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007907 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07007908 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007909 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07007910 contentView.setTextViewText(rowIds[i],
7911 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007912 mBuilder.setTextViewColorSecondary(contentView, rowIds[i], p);
Selim Cinek07c80172016-04-21 16:40:47 -07007913 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek384804b2018-04-18 14:31:07 +08007914 handleInboxImageMargin(contentView, rowIds[i], first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007915 result.getIconMarginEnd());
Selim Cinek07c80172016-04-21 16:40:47 -07007916 if (first) {
7917 onlyViewId = rowIds[i];
7918 } else {
7919 onlyViewId = 0;
7920 }
Selim Cinek247fa012016-02-18 09:50:48 -08007921 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007922 }
7923 i++;
7924 }
Selim Cinek07c80172016-04-21 16:40:47 -07007925 if (onlyViewId != 0) {
7926 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
7927 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7928 R.dimen.notification_text_margin_top);
7929 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
7930 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007931
Daniel Sandler879c5e02012-04-17 16:46:51 -04007932 return contentView;
7933 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007934
Julia Reynolds7217dc92018-03-07 12:12:09 -05007935 /**
7936 * @hide
7937 */
7938 @Override
7939 public boolean areNotificationsVisiblyDifferent(Style other) {
7940 if (other == null || getClass() != other.getClass()) {
7941 return true;
7942 }
7943 InboxStyle newS = (InboxStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04007944
7945 final ArrayList<CharSequence> myLines = getLines();
7946 final ArrayList<CharSequence> newLines = newS.getLines();
7947 final int n = myLines.size();
7948 if (n != newLines.size()) {
7949 return true;
7950 }
7951
7952 for (int i = 0; i < n; i++) {
7953 if (!Objects.equals(
7954 String.valueOf(myLines.get(i)),
7955 String.valueOf(newLines.get(i)))) {
7956 return true;
7957 }
7958 }
7959 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05007960 }
7961
Selim Cinek384804b2018-04-18 14:31:07 +08007962 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007963 int marginEndValue) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08007964 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08007965 if (first) {
7966 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
7967 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
7968 boolean hasProgress = max != 0 || ind;
Selim Cinek384804b2018-04-18 14:31:07 +08007969 if (!hasProgress) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08007970 endMargin = marginEndValue;
Selim Cinek247fa012016-02-18 09:50:48 -08007971 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007972 }
Selim Cinek1c72fa02018-04-23 18:00:54 +08007973 contentView.setViewLayoutMarginEnd(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08007974 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04007975 }
Dan Sandler842dd772014-05-15 09:36:47 -04007976
7977 /**
7978 * Notification style for media playback notifications.
7979 *
7980 * In the expanded form, {@link Notification#bigContentView}, up to 5
7981 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04007982 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04007983 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
7984 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
7985 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08007986 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04007987 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
7988 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01007989 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04007990 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08007991 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007992 * Notifications created with MediaStyle will have their category set to
7993 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
7994 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08007995 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07007996 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
7997 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04007998 * the System UI can identify this as a notification representing an active media session
7999 * and respond accordingly (by showing album artwork in the lockscreen, for example).
8000 *
Selim Cinek99104832017-01-25 14:47:33 -08008001 * <p>
8002 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
8003 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
8004 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
8005 * <p>
8006 *
Dan Sandler842dd772014-05-15 09:36:47 -04008007 * To use this style with your Notification, feed it to
8008 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8009 * <pre class="prettyprint">
8010 * Notification noti = new Notification.Builder()
8011 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01008012 * .setContentTitle(&quot;Track title&quot;)
8013 * .setContentText(&quot;Artist - Album&quot;)
8014 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01008015 * .setStyle(<b>new Notification.MediaStyle()</b>
8016 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04008017 * .build();
8018 * </pre>
8019 *
8020 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08008021 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04008022 */
8023 public static class MediaStyle extends Style {
Gus Prevas9cc96602018-10-11 11:24:23 -04008024 // Changing max media buttons requires also changing templates
8025 // (notification_template_material_media and notification_template_material_big_media).
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008026 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04008027 static final int MAX_MEDIA_BUTTONS = 5;
Gus Prevas9cc96602018-10-11 11:24:23 -04008028 @IdRes private static final int[] MEDIA_BUTTON_IDS = {
8029 R.id.action0,
8030 R.id.action1,
8031 R.id.action2,
8032 R.id.action3,
8033 R.id.action4,
8034 };
Dan Sandler842dd772014-05-15 09:36:47 -04008035
8036 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07008037 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04008038
8039 public MediaStyle() {
8040 }
8041
Adrian Roosf5faf9d2016-05-23 13:56:15 -07008042 /**
8043 * @deprecated use {@code MediaStyle()}.
8044 */
8045 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04008046 public MediaStyle(Builder builder) {
8047 setBuilder(builder);
8048 }
8049
8050 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008051 * 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 -04008052 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008053 *
8054 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04008055 */
8056 public MediaStyle setShowActionsInCompactView(int...actions) {
8057 mActionsToShowInCompact = actions;
8058 return this;
8059 }
8060
8061 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07008062 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
8063 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04008064 */
Jeff Browndba34ba2014-06-24 20:46:03 -07008065 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04008066 mToken = token;
8067 return this;
8068 }
8069
Christoph Studer4600f9b2014-07-22 22:44:43 +02008070 /**
8071 * @hide
8072 */
Dan Sandler842dd772014-05-15 09:36:47 -04008073 @Override
Mathew Inwood61e8ae62018-08-14 14:17:44 +01008074 @UnsupportedAppUsage
Dan Sandler842dd772014-05-15 09:36:47 -04008075 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02008076 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01008077 if (wip.category == null) {
8078 wip.category = Notification.CATEGORY_TRANSPORT;
8079 }
Dan Sandler842dd772014-05-15 09:36:47 -04008080 return wip;
8081 }
8082
Christoph Studer4600f9b2014-07-22 22:44:43 +02008083 /**
8084 * @hide
8085 */
8086 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008087 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04008088 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02008089 }
8090
8091 /**
8092 * @hide
8093 */
8094 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04008095 public RemoteViews makeBigContentView() {
8096 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02008097 }
8098
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008099 /**
8100 * @hide
8101 */
8102 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008103 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008104 RemoteViews expanded = makeMediaBigContentView();
8105 return expanded != null ? expanded : makeMediaContentView();
8106 }
8107
Dan Sandler842dd772014-05-15 09:36:47 -04008108 /** @hide */
8109 @Override
8110 public void addExtras(Bundle extras) {
8111 super.addExtras(extras);
8112
8113 if (mToken != null) {
8114 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
8115 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01008116 if (mActionsToShowInCompact != null) {
8117 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
8118 }
Dan Sandler842dd772014-05-15 09:36:47 -04008119 }
8120
Christoph Studer4600f9b2014-07-22 22:44:43 +02008121 /**
8122 * @hide
8123 */
8124 @Override
8125 protected void restoreFromExtras(Bundle extras) {
8126 super.restoreFromExtras(extras);
8127
8128 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
8129 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
8130 }
8131 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
8132 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
8133 }
8134 }
8135
Julia Reynolds7217dc92018-03-07 12:12:09 -05008136 /**
8137 * @hide
8138 */
8139 @Override
8140 public boolean areNotificationsVisiblyDifferent(Style other) {
8141 if (other == null || getClass() != other.getClass()) {
8142 return true;
8143 }
8144 // All fields to compare are on the Notification object
8145 return false;
8146 }
8147
Gus Prevas9cc96602018-10-11 11:24:23 -04008148 private void bindMediaActionButton(RemoteViews container, @IdRes int buttonId,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008149 Action action, StandardTemplateParams p) {
Dan Sandler842dd772014-05-15 09:36:47 -04008150 final boolean tombstone = (action.actionIntent == null);
Gus Prevas9cc96602018-10-11 11:24:23 -04008151 container.setViewVisibility(buttonId, View.VISIBLE);
8152 container.setImageViewIcon(buttonId, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07008153
8154 // If the action buttons should not be tinted, then just use the default
8155 // notification color. Otherwise, just use the passed-in color.
Gus Prevas9cc96602018-10-11 11:24:23 -04008156 Resources resources = mBuilder.mContext.getResources();
8157 Configuration currentConfig = resources.getConfiguration();
Lucas Dupinf03e7522018-06-25 16:21:13 -07008158 boolean inNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
8159 == Configuration.UI_MODE_NIGHT_YES;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008160 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized(p)
8161 ? getActionColor(p)
Lucas Dupina291d192018-06-07 13:59:42 -07008162 : ContrastColorUtil.resolveColor(mBuilder.mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07008163 Notification.COLOR_DEFAULT, inNightMode);
Anthony Chenad4d1582017-04-10 16:07:58 -07008164
Gus Prevas9cc96602018-10-11 11:24:23 -04008165 container.setDrawableTint(buttonId, false, tintColor,
Sunny Goyal5b153922017-09-21 21:00:36 -07008166 PorterDuff.Mode.SRC_ATOP);
Gus Prevas9cc96602018-10-11 11:24:23 -04008167
8168 final TypedArray typedArray = mBuilder.mContext.obtainStyledAttributes(
8169 new int[]{ android.R.attr.colorControlHighlight });
8170 int rippleAlpha = Color.alpha(typedArray.getColor(0, 0));
8171 typedArray.recycle();
8172 int rippleColor = Color.argb(rippleAlpha, Color.red(tintColor), Color.green(tintColor),
8173 Color.blue(tintColor));
8174 container.setRippleDrawableColor(buttonId, ColorStateList.valueOf(rippleColor));
8175
Dan Sandler842dd772014-05-15 09:36:47 -04008176 if (!tombstone) {
Gus Prevas9cc96602018-10-11 11:24:23 -04008177 container.setOnClickPendingIntent(buttonId, action.actionIntent);
Dan Sandler842dd772014-05-15 09:36:47 -04008178 }
Gus Prevas9cc96602018-10-11 11:24:23 -04008179 container.setContentDescription(buttonId, action.title);
Dan Sandler842dd772014-05-15 09:36:47 -04008180 }
8181
8182 private RemoteViews makeMediaContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008183 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).fillTextsFrom(
8184 mBuilder);
Dan Sandler842dd772014-05-15 09:36:47 -04008185 RemoteViews view = mBuilder.applyStandardTemplate(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008186 R.layout.notification_template_material_media, p,
Selim Cinek384804b2018-04-18 14:31:07 +08008187 null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04008188
8189 final int numActions = mBuilder.mActions.size();
Gus Prevas9cc96602018-10-11 11:24:23 -04008190 final int numActionsToShow = mActionsToShowInCompact == null
Dan Sandler842dd772014-05-15 09:36:47 -04008191 ? 0
8192 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Gus Prevas9cc96602018-10-11 11:24:23 -04008193 if (numActionsToShow > numActions) {
8194 throw new IllegalArgumentException(String.format(
8195 "setShowActionsInCompactView: action %d out of bounds (max %d)",
8196 numActions, numActions - 1));
8197 }
8198 for (int i = 0; i < MAX_MEDIA_BUTTONS_IN_COMPACT; i++) {
8199 if (i < numActionsToShow) {
Dan Sandler842dd772014-05-15 09:36:47 -04008200 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008201 bindMediaActionButton(view, MEDIA_BUTTON_IDS[i], action, p);
Gus Prevas9cc96602018-10-11 11:24:23 -04008202 } else {
8203 view.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04008204 }
8205 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08008206 handleImage(view);
8207 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008208 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07008209 if (mBuilder.mN.hasLargeIcon()) {
Selim Cinek384804b2018-04-18 14:31:07 +08008210 endMargin = R.dimen.notification_media_image_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08008211 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008212 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04008213 return view;
8214 }
8215
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008216 private int getActionColor(StandardTemplateParams p) {
8217 return mBuilder.isColorized(p) ? mBuilder.getPrimaryTextColor(p)
8218 : mBuilder.resolveContrastColor(p);
Selim Cinek99104832017-01-25 14:47:33 -08008219 }
8220
Dan Sandler842dd772014-05-15 09:36:47 -04008221 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008222 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008223 // Dont add an expanded view if there is no more content to be revealed
8224 int actionsInCompact = mActionsToShowInCompact == null
8225 ? 0
8226 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07008227 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008228 return null;
8229 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008230 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).fillTextsFrom(
8231 mBuilder);
Selim Cinek5bf069a2015-11-10 19:14:27 -05008232 RemoteViews big = mBuilder.applyStandardTemplate(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008233 R.layout.notification_template_material_big_media, p , null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04008234
Gus Prevas9cc96602018-10-11 11:24:23 -04008235 for (int i = 0; i < MAX_MEDIA_BUTTONS; i++) {
8236 if (i < actionCount) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008237 bindMediaActionButton(big, MEDIA_BUTTON_IDS[i], mBuilder.mActions.get(i), p);
Gus Prevas9cc96602018-10-11 11:24:23 -04008238 } else {
8239 big.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04008240 }
8241 }
linanson2bcd4032019-01-14 15:22:04 +08008242 bindMediaActionButton(big, R.id.media_seamless, new Action(R.drawable.ic_media_seamless,
8243 mBuilder.mContext.getString(
8244 com.android.internal.R.string.ext_media_seamless_action), null), p);
8245 big.setViewVisibility(R.id.media_seamless, View.GONE);
Selim Cinek5bf069a2015-11-10 19:14:27 -05008246 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04008247 return big;
8248 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008249
Selim Cinek5bf069a2015-11-10 19:14:27 -05008250 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07008251 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008252 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
8253 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008254 }
8255 }
8256
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008257 /**
8258 * @hide
8259 */
8260 @Override
8261 protected boolean hasProgress() {
8262 return false;
8263 }
Dan Sandler842dd772014-05-15 09:36:47 -04008264 }
Griff Hazen61a9e862014-05-22 16:05:19 -07008265
Selim Cinek593610c2016-02-16 18:42:57 -08008266 /**
8267 * Notification style for custom views that are decorated by the system
8268 *
8269 * <p>Instead of providing a notification that is completely custom, a developer can set this
8270 * style and still obtain system decorations like the notification header with the expand
8271 * affordance and actions.
8272 *
8273 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8274 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8275 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8276 * corresponding custom views to display.
8277 *
8278 * To use this style with your Notification, feed it to
8279 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8280 * <pre class="prettyprint">
8281 * Notification noti = new Notification.Builder()
8282 * .setSmallIcon(R.drawable.ic_stat_player)
8283 * .setLargeIcon(albumArtBitmap))
8284 * .setCustomContentView(contentView);
8285 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
8286 * .build();
8287 * </pre>
8288 */
8289 public static class DecoratedCustomViewStyle extends Style {
8290
8291 public DecoratedCustomViewStyle() {
8292 }
8293
Selim Cinek593610c2016-02-16 18:42:57 -08008294 /**
8295 * @hide
8296 */
8297 public boolean displayCustomViewInline() {
8298 return true;
8299 }
8300
8301 /**
8302 * @hide
8303 */
8304 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008305 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008306 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
8307 }
8308
8309 /**
8310 * @hide
8311 */
8312 @Override
8313 public RemoteViews makeBigContentView() {
8314 return makeDecoratedBigContentView();
8315 }
8316
8317 /**
8318 * @hide
8319 */
8320 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008321 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008322 return makeDecoratedHeadsUpContentView();
8323 }
8324
Selim Cinek593610c2016-02-16 18:42:57 -08008325 private RemoteViews makeDecoratedHeadsUpContentView() {
8326 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
8327 ? mBuilder.mN.contentView
8328 : mBuilder.mN.headsUpContentView;
8329 if (mBuilder.mActions.size() == 0) {
8330 return makeStandardTemplateWithCustomContent(headsUpContentView);
8331 }
Selim Cinek384804b2018-04-18 14:31:07 +08008332 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008333 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008334 mBuilder.getBigBaseLayoutResource(), result);
8335 buildIntoRemoteViewContent(remoteViews, headsUpContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008336 return remoteViews;
8337 }
8338
Selim Cinek593610c2016-02-16 18:42:57 -08008339 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
Selim Cinek384804b2018-04-18 14:31:07 +08008340 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008341 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08008342 mBuilder.getBaseLayoutResource(), result);
8343 buildIntoRemoteViewContent(remoteViews, customContent, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008344 return remoteViews;
8345 }
8346
Selim Cinek593610c2016-02-16 18:42:57 -08008347 private RemoteViews makeDecoratedBigContentView() {
8348 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
8349 ? mBuilder.mN.contentView
8350 : mBuilder.mN.bigContentView;
8351 if (mBuilder.mActions.size() == 0) {
8352 return makeStandardTemplateWithCustomContent(bigContentView);
8353 }
Selim Cinek384804b2018-04-18 14:31:07 +08008354 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008355 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008356 mBuilder.getBigBaseLayoutResource(), result);
8357 buildIntoRemoteViewContent(remoteViews, bigContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008358 return remoteViews;
8359 }
Selim Cinek247fa012016-02-18 09:50:48 -08008360
8361 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
Selim Cinek384804b2018-04-18 14:31:07 +08008362 RemoteViews customContent, TemplateBindResult result) {
Selim Cinek6fe4a102019-01-25 14:38:24 -08008363 int childIndex = -1;
Adrian Roos5081c0d2016-02-26 16:04:19 -08008364 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008365 // Need to clone customContent before adding, because otherwise it can no longer be
8366 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008367 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07008368 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
8369 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Sunny Goyalc12d31c2018-11-12 16:29:18 -08008370 remoteViews.addFlags(RemoteViews.FLAG_REAPPLY_DISALLOWED);
Selim Cinek6fe4a102019-01-25 14:38:24 -08008371 childIndex = 0;
Adrian Roos5081c0d2016-02-26 16:04:19 -08008372 }
Selim Cinek6fe4a102019-01-25 14:38:24 -08008373 remoteViews.setIntTag(R.id.notification_main_column,
8374 com.android.internal.R.id.notification_custom_view_index_tag,
8375 childIndex);
Selim Cinek247fa012016-02-18 09:50:48 -08008376 // also update the end margin if there is an image
Selim Cinek384804b2018-04-18 14:31:07 +08008377 Resources resources = mBuilder.mContext.getResources();
8378 int endMargin = resources.getDimensionPixelSize(
Selim Cinek1c72fa02018-04-23 18:00:54 +08008379 R.dimen.notification_content_margin_end) + result.getIconMarginEnd();
Selim Cinek384804b2018-04-18 14:31:07 +08008380 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08008381 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05008382
8383 /**
8384 * @hide
8385 */
8386 @Override
8387 public boolean areNotificationsVisiblyDifferent(Style other) {
8388 if (other == null || getClass() != other.getClass()) {
8389 return true;
8390 }
8391 // Comparison done for all custom RemoteViews, independent of style
8392 return false;
8393 }
Selim Cinek593610c2016-02-16 18:42:57 -08008394 }
8395
Selim Cinek03eb3b72016-02-18 10:39:45 -08008396 /**
8397 * Notification style for media custom views that are decorated by the system
8398 *
8399 * <p>Instead of providing a media notification that is completely custom, a developer can set
8400 * this style and still obtain system decorations like the notification header with the expand
8401 * affordance and actions.
8402 *
8403 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8404 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8405 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8406 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08008407 * <p>
8408 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
8409 * notification by using {@link Notification.Builder#setColorized(boolean)}.
8410 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08008411 * To use this style with your Notification, feed it to
8412 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8413 * <pre class="prettyprint">
8414 * Notification noti = new Notification.Builder()
8415 * .setSmallIcon(R.drawable.ic_stat_player)
8416 * .setLargeIcon(albumArtBitmap))
8417 * .setCustomContentView(contentView);
8418 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
8419 * .setMediaSession(mySession))
8420 * .build();
8421 * </pre>
8422 *
8423 * @see android.app.Notification.DecoratedCustomViewStyle
8424 * @see android.app.Notification.MediaStyle
8425 */
8426 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
8427
8428 public DecoratedMediaCustomViewStyle() {
8429 }
8430
Selim Cinek03eb3b72016-02-18 10:39:45 -08008431 /**
8432 * @hide
8433 */
8434 public boolean displayCustomViewInline() {
8435 return true;
8436 }
8437
8438 /**
8439 * @hide
8440 */
8441 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008442 public RemoteViews makeContentView(boolean increasedHeight) {
8443 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008444 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8445 mBuilder.mN.contentView);
8446 }
8447
8448 /**
8449 * @hide
8450 */
8451 @Override
8452 public RemoteViews makeBigContentView() {
8453 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
8454 ? mBuilder.mN.bigContentView
8455 : mBuilder.mN.contentView;
8456 return makeBigContentViewWithCustomContent(customRemoteView);
8457 }
8458
8459 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
8460 RemoteViews remoteViews = super.makeBigContentView();
8461 if (remoteViews != null) {
8462 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
8463 customRemoteView);
8464 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08008465 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008466 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8467 customRemoteView);
8468 } else {
8469 return null;
8470 }
8471 }
8472
8473 /**
8474 * @hide
8475 */
8476 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008477 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08008478 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
8479 ? mBuilder.mN.headsUpContentView
8480 : mBuilder.mN.contentView;
8481 return makeBigContentViewWithCustomContent(customRemoteView);
8482 }
8483
Julia Reynolds7217dc92018-03-07 12:12:09 -05008484 /**
8485 * @hide
8486 */
8487 @Override
8488 public boolean areNotificationsVisiblyDifferent(Style other) {
8489 if (other == null || getClass() != other.getClass()) {
8490 return true;
8491 }
8492 // Comparison done for all custom RemoteViews, independent of style
8493 return false;
8494 }
8495
Selim Cinek03eb3b72016-02-18 10:39:45 -08008496 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
8497 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08008498 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008499 // Need to clone customContent before adding, because otherwise it can no longer be
8500 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008501 customContent = customContent.clone();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008502 customContent.overrideTextColors(mBuilder.getPrimaryTextColor(mBuilder.mParams));
Selim Cinekf91017e2016-03-14 12:25:09 -07008503 remoteViews.removeAllViews(id);
8504 remoteViews.addView(id, customContent);
Sunny Goyalc12d31c2018-11-12 16:29:18 -08008505 remoteViews.addFlags(RemoteViews.FLAG_REAPPLY_DISALLOWED);
Adrian Roos5081c0d2016-02-26 16:04:19 -08008506 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08008507 return remoteViews;
8508 }
8509 }
8510
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008511 /**
8512 * Encapsulates the information needed to display a notification as a bubble.
8513 *
8514 * <p>A bubble is used to display app content in a floating window over the existing
8515 * foreground activity. A bubble has a collapsed state represented by an icon,
8516 * {@link BubbleMetadata.Builder#setIcon(Icon)} and an expanded state which is populated
8517 * via {@link BubbleMetadata.Builder#setIntent(PendingIntent)}.</p>
8518 *
8519 * <b>Notifications with a valid and allowed bubble will display in collapsed state
8520 * outside of the notification shade on unlocked devices. When a user interacts with the
8521 * collapsed bubble, the bubble intent will be invoked and displayed.</b>
8522 *
8523 * @see Notification.Builder#setBubbleMetadata(BubbleMetadata)
8524 */
8525 public static final class BubbleMetadata implements Parcelable {
8526
8527 private PendingIntent mPendingIntent;
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008528 private PendingIntent mDeleteIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008529 private Icon mIcon;
8530 private int mDesiredHeight;
Mady Mellor7af771a2019-03-07 15:04:54 -08008531 @DimenRes private int mDesiredHeightResId;
Mady Mellor0157ff22019-01-24 15:31:48 -08008532 private int mFlags;
8533
8534 /**
8535 * If set and the app creating the bubble is in the foreground, the bubble will be posted
8536 * in its expanded state, with the contents of {@link #getIntent()} in a floating window.
8537 *
Mady Mellor1f4703a2019-07-12 13:23:51 -07008538 * <p>This flag has no effect if the app posting the bubble is not in the foreground.
8539 * The app is considered foreground if it is visible and on the screen, note that
8540 * a foreground service does not qualify.
8541 * </p>
Mady Mellor0157ff22019-01-24 15:31:48 -08008542 *
8543 * <p>Generally this flag should only be set if the user has performed an action to request
8544 * or create a bubble.</p>
Mady Mellora7731962019-06-17 17:57:02 -07008545 *
8546 * @hide
Mady Mellor0157ff22019-01-24 15:31:48 -08008547 */
Mady Mellora7731962019-06-17 17:57:02 -07008548 public static final int FLAG_AUTO_EXPAND_BUBBLE = 0x00000001;
Mady Mellor0157ff22019-01-24 15:31:48 -08008549
8550 /**
Mady Mellor8a1f0252019-04-01 11:31:34 -07008551 * If set and the app posting the bubble is in the foreground, the bubble will
8552 * be posted <b>without</b> the associated notification in the notification shade.
Mady Mellor0157ff22019-01-24 15:31:48 -08008553 *
Mady Mellor1f4703a2019-07-12 13:23:51 -07008554 * <p>This flag has no effect if the app posting the bubble is not in the foreground.
8555 * The app is considered foreground if it is visible and on the screen, note that
8556 * a foreground service does not qualify.
8557 * </p>
Mady Mellor0157ff22019-01-24 15:31:48 -08008558 *
8559 * <p>Generally this flag should only be set if the user has performed an action to request
Mady Mellor8a1f0252019-04-01 11:31:34 -07008560 * or create a bubble, or if the user has seen the content in the notification and the
8561 * notification is no longer relevant.</p>
Mady Mellora7731962019-06-17 17:57:02 -07008562 *
8563 * @hide
Mady Mellor0157ff22019-01-24 15:31:48 -08008564 */
Mady Mellora7731962019-06-17 17:57:02 -07008565 public static final int FLAG_SUPPRESS_NOTIFICATION = 0x00000002;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008566
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008567 private BubbleMetadata(PendingIntent expandIntent, PendingIntent deleteIntent,
Mady Mellor7af771a2019-03-07 15:04:54 -08008568 Icon icon, int height, @DimenRes int heightResId) {
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008569 mPendingIntent = expandIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008570 mIcon = icon;
8571 mDesiredHeight = height;
Mady Mellor7af771a2019-03-07 15:04:54 -08008572 mDesiredHeightResId = heightResId;
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008573 mDeleteIntent = deleteIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008574 }
8575
8576 private BubbleMetadata(Parcel in) {
8577 mPendingIntent = PendingIntent.CREATOR.createFromParcel(in);
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008578 mIcon = Icon.CREATOR.createFromParcel(in);
8579 mDesiredHeight = in.readInt();
Mady Mellor0157ff22019-01-24 15:31:48 -08008580 mFlags = in.readInt();
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008581 if (in.readInt() != 0) {
8582 mDeleteIntent = PendingIntent.CREATOR.createFromParcel(in);
8583 }
Mady Mellor7af771a2019-03-07 15:04:54 -08008584 mDesiredHeightResId = in.readInt();
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008585 }
8586
8587 /**
8588 * @return the pending intent used to populate the floating window for this bubble.
8589 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008590 @NonNull
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008591 public PendingIntent getIntent() {
8592 return mPendingIntent;
8593 }
8594
8595 /**
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008596 * @return the pending intent to send when the bubble is dismissed by a user, if one exists.
8597 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008598 @Nullable
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008599 public PendingIntent getDeleteIntent() {
8600 return mDeleteIntent;
8601 }
8602
8603 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008604 * @return the icon that will be displayed for this bubble when it is collapsed.
8605 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008606 @NonNull
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008607 public Icon getIcon() {
8608 return mIcon;
8609 }
8610
8611 /**
Mady Mellor7af771a2019-03-07 15:04:54 -08008612 * @return the ideal height, in DPs, for the floating window that app content defined by
Mady Mellor8a529e22019-03-25 17:37:55 -07008613 * {@link #getIntent()} for this bubble. A value of 0 indicates a desired height has not
8614 * been set.
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008615 */
Mady Mellor8a529e22019-03-25 17:37:55 -07008616 @Dimension(unit = DP)
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008617 public int getDesiredHeight() {
8618 return mDesiredHeight;
8619 }
8620
Mady Mellor0157ff22019-01-24 15:31:48 -08008621 /**
Mady Mellor7af771a2019-03-07 15:04:54 -08008622 * @return the resId of ideal height for the floating window that app content defined by
Mady Mellor8a529e22019-03-25 17:37:55 -07008623 * {@link #getIntent()} for this bubble. A value of 0 indicates a res value has not
8624 * been provided for the desired height.
Mady Mellor7af771a2019-03-07 15:04:54 -08008625 */
8626 @DimenRes
8627 public int getDesiredHeightResId() {
8628 return mDesiredHeightResId;
8629 }
8630
8631 /**
Mady Mellor0157ff22019-01-24 15:31:48 -08008632 * @return whether this bubble should auto expand when it is posted.
8633 *
8634 * @see BubbleMetadata.Builder#setAutoExpandBubble(boolean)
8635 */
8636 public boolean getAutoExpandBubble() {
8637 return (mFlags & FLAG_AUTO_EXPAND_BUBBLE) != 0;
8638 }
8639
8640 /**
Mady Mellorc529d6d2019-04-16 14:22:52 -07008641 * @return whether this bubble should suppress the notification when it is posted.
8642 *
8643 * @see BubbleMetadata.Builder#setSuppressNotification(boolean)
8644 */
8645 public boolean isNotificationSuppressed() {
Mady Mellor8a1f0252019-04-01 11:31:34 -07008646 return (mFlags & FLAG_SUPPRESS_NOTIFICATION) != 0;
Mady Mellor0157ff22019-01-24 15:31:48 -08008647 }
8648
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07008649 public static final @android.annotation.NonNull Parcelable.Creator<BubbleMetadata> CREATOR =
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008650 new Parcelable.Creator<BubbleMetadata>() {
8651
8652 @Override
8653 public BubbleMetadata createFromParcel(Parcel source) {
8654 return new BubbleMetadata(source);
8655 }
8656
8657 @Override
8658 public BubbleMetadata[] newArray(int size) {
8659 return new BubbleMetadata[size];
8660 }
8661 };
8662
8663 @Override
8664 public int describeContents() {
8665 return 0;
8666 }
8667
8668 @Override
8669 public void writeToParcel(Parcel out, int flags) {
8670 mPendingIntent.writeToParcel(out, 0);
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008671 mIcon.writeToParcel(out, 0);
8672 out.writeInt(mDesiredHeight);
Mady Mellor0157ff22019-01-24 15:31:48 -08008673 out.writeInt(mFlags);
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008674 out.writeInt(mDeleteIntent != null ? 1 : 0);
8675 if (mDeleteIntent != null) {
8676 mDeleteIntent.writeToParcel(out, 0);
8677 }
Mady Mellor7af771a2019-03-07 15:04:54 -08008678 out.writeInt(mDesiredHeightResId);
Mady Mellor0157ff22019-01-24 15:31:48 -08008679 }
8680
Mady Mellora7731962019-06-17 17:57:02 -07008681 /**
8682 * @hide
8683 */
8684 public void setFlags(int flags) {
Mady Mellor0157ff22019-01-24 15:31:48 -08008685 mFlags = flags;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008686 }
8687
8688 /**
Mady Mellora7731962019-06-17 17:57:02 -07008689 * @hide
8690 */
8691 public int getFlags() {
8692 return mFlags;
8693 }
8694
8695 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008696 * Builder to construct a {@link BubbleMetadata} object.
8697 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008698 public static final class Builder {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008699
8700 private PendingIntent mPendingIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008701 private Icon mIcon;
8702 private int mDesiredHeight;
Mady Mellor7af771a2019-03-07 15:04:54 -08008703 @DimenRes private int mDesiredHeightResId;
Mady Mellor0157ff22019-01-24 15:31:48 -08008704 private int mFlags;
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008705 private PendingIntent mDeleteIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008706
8707 /**
8708 * Constructs a new builder object.
8709 */
8710 public Builder() {
8711 }
8712
8713 /**
8714 * Sets the intent that will be used when the bubble is expanded. This will display the
8715 * app content in a floating window over the existing foreground activity.
Julia Tuttled9f48352019-04-15 14:06:54 -04008716 *
8717 * <p>An intent is required.</p>
8718 *
8719 * @throws IllegalArgumentException if intent is null
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008720 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008721 @NonNull
8722 public BubbleMetadata.Builder setIntent(@NonNull PendingIntent intent) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008723 if (intent == null) {
8724 throw new IllegalArgumentException("Bubble requires non-null pending intent");
8725 }
8726 mPendingIntent = intent;
8727 return this;
8728 }
8729
8730 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008731 * Sets the icon that will represent the bubble when it is collapsed.
8732 *
8733 * <p>An icon is required and should be representative of the content within the bubble.
8734 * If your app produces multiple bubbles, the image should be unique for each of them.
8735 * </p>
Mady Mellor9848a6c2019-03-19 15:29:05 -07008736 *
8737 * <p>The shape of a bubble icon is adaptive and can match the device theme.
8738 *
8739 * If your icon is bitmap-based, you should create it using
8740 * {@link Icon#createWithAdaptiveBitmap(Bitmap)}, otherwise this method will throw.
8741 *
8742 * If your icon is not bitmap-based, you should expect that the icon will be tinted.
8743 * </p>
Julia Tuttled9f48352019-04-15 14:06:54 -04008744 *
8745 * @throws IllegalArgumentException if icon is null or a non-adaptive bitmap
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008746 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008747 @NonNull
8748 public BubbleMetadata.Builder setIcon(@NonNull Icon icon) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008749 if (icon == null) {
8750 throw new IllegalArgumentException("Bubbles require non-null icon");
8751 }
Mady Mellor9848a6c2019-03-19 15:29:05 -07008752 if (icon.getType() == TYPE_BITMAP) {
8753 throw new IllegalArgumentException("When using bitmap based icons, Bubbles "
8754 + "require TYPE_ADAPTIVE_BITMAP, please use"
8755 + " Icon#createWithAdaptiveBitmap instead");
8756 }
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008757 mIcon = icon;
8758 return this;
8759 }
8760
8761 /**
Mady Mellor7af771a2019-03-07 15:04:54 -08008762 * Sets the desired height in DPs for the app content defined by
Julia Tuttled9f48352019-04-15 14:06:54 -04008763 * {@link #setIntent(PendingIntent)}.
8764 *
8765 * <p>This height may not be respected if there is not enough space on the screen or if
8766 * the provided height is too small to be useful.</p>
8767 *
8768 * <p>If {@link #setDesiredHeightResId(int)} was previously called on this builder, the
Mady Mellor7af771a2019-03-07 15:04:54 -08008769 * previous value set will be cleared after calling this method, and this value will
Julia Tuttled9f48352019-04-15 14:06:54 -04008770 * be used instead.</p>
8771 *
8772 * <p>A desired height (in DPs or via resID) is optional.</p>
8773 *
8774 * @see #setDesiredHeightResId(int)
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008775 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008776 @NonNull
Mady Mellor8a529e22019-03-25 17:37:55 -07008777 public BubbleMetadata.Builder setDesiredHeight(@Dimension(unit = DP) int height) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008778 mDesiredHeight = Math.max(height, 0);
Mady Mellor7af771a2019-03-07 15:04:54 -08008779 mDesiredHeightResId = 0;
8780 return this;
8781 }
8782
8783
8784 /**
8785 * Sets the desired height via resId for the app content defined by
Julia Tuttled9f48352019-04-15 14:06:54 -04008786 * {@link #setIntent(PendingIntent)}.
8787 *
8788 * <p>This height may not be respected if there is not enough space on the screen or if
8789 * the provided height is too small to be useful.</p>
8790 *
8791 * <p>If {@link #setDesiredHeight(int)} was previously called on this builder, the
Mady Mellor7af771a2019-03-07 15:04:54 -08008792 * previous value set will be cleared after calling this method, and this value will
Julia Tuttled9f48352019-04-15 14:06:54 -04008793 * be used instead.</p>
8794 *
8795 * <p>A desired height (in DPs or via resID) is optional.</p>
8796 *
8797 * @see #setDesiredHeight(int)
Mady Mellor7af771a2019-03-07 15:04:54 -08008798 */
8799 @NonNull
8800 public BubbleMetadata.Builder setDesiredHeightResId(@DimenRes int heightResId) {
8801 mDesiredHeightResId = heightResId;
8802 mDesiredHeight = 0;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008803 return this;
8804 }
8805
8806 /**
Julia Tuttled9f48352019-04-15 14:06:54 -04008807 * Sets whether the bubble will be posted in its expanded state (with the contents of
8808 * {@link #getIntent()} in a floating window).
Mady Mellor0157ff22019-01-24 15:31:48 -08008809 *
Julia Tuttled9f48352019-04-15 14:06:54 -04008810 * <p>This flag has no effect if the app posting the bubble is not in the foreground.
Mady Mellor1f4703a2019-07-12 13:23:51 -07008811 * The app is considered foreground if it is visible and on the screen, note that
8812 * a foreground service does not qualify.
Mady Mellor0157ff22019-01-24 15:31:48 -08008813 * </p>
8814 *
Julia Tuttled9f48352019-04-15 14:06:54 -04008815 * <p>Generally, this flag should only be set if the user has performed an action to
Mady Mellor0157ff22019-01-24 15:31:48 -08008816 * request or create a bubble.</p>
Julia Tuttled9f48352019-04-15 14:06:54 -04008817 *
8818 * <p>Setting this flag is optional; it defaults to false.</p>
Mady Mellor0157ff22019-01-24 15:31:48 -08008819 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008820 @NonNull
Mady Mellor0157ff22019-01-24 15:31:48 -08008821 public BubbleMetadata.Builder setAutoExpandBubble(boolean shouldExpand) {
8822 setFlag(FLAG_AUTO_EXPAND_BUBBLE, shouldExpand);
8823 return this;
8824 }
8825
8826 /**
Julia Tuttled9f48352019-04-15 14:06:54 -04008827 * Sets whether the bubble will be posted <b>without</b> the associated notification in
8828 * the notification shade.
Mady Mellor8a1f0252019-04-01 11:31:34 -07008829 *
Julia Tuttled9f48352019-04-15 14:06:54 -04008830 * <p>This flag has no effect if the app posting the bubble is not in the foreground.
Mady Mellor1f4703a2019-07-12 13:23:51 -07008831 * The app is considered foreground if it is visible and on the screen, note that
8832 * a foreground service does not qualify.
Mady Mellor8a1f0252019-04-01 11:31:34 -07008833 * </p>
8834 *
Julia Tuttled9f48352019-04-15 14:06:54 -04008835 * <p>Generally, this flag should only be set if the user has performed an action to
Mady Mellor8a1f0252019-04-01 11:31:34 -07008836 * request or create a bubble, or if the user has seen the content in the notification
8837 * and the notification is no longer relevant.</p>
Julia Tuttled9f48352019-04-15 14:06:54 -04008838 *
8839 * <p>Setting this flag is optional; it defaults to false.</p>
Mady Mellor8a1f0252019-04-01 11:31:34 -07008840 */
8841 @NonNull
Julia Tuttle4f1bf202019-05-06 13:40:05 -04008842 public BubbleMetadata.Builder setSuppressNotification(boolean shouldSuppressNotif) {
8843 setFlag(FLAG_SUPPRESS_NOTIFICATION, shouldSuppressNotif);
Mady Mellor0157ff22019-01-24 15:31:48 -08008844 return this;
8845 }
8846
8847 /**
Julia Tuttled9f48352019-04-15 14:06:54 -04008848 * Sets an intent to send when this bubble is explicitly removed by the user.
8849 *
8850 * <p>Setting a delete intent is optional.</p>
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008851 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008852 @NonNull
8853 public BubbleMetadata.Builder setDeleteIntent(@Nullable PendingIntent deleteIntent) {
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008854 mDeleteIntent = deleteIntent;
8855 return this;
8856 }
8857
8858 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008859 * Creates the {@link BubbleMetadata} defined by this builder.
Julia Tuttled9f48352019-04-15 14:06:54 -04008860 *
8861 * @throws IllegalStateException if {@link #setIntent(PendingIntent)} and/or
8862 * {@link #setIcon(Icon)} have not been called on this
8863 * builder.
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008864 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008865 @NonNull
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008866 public BubbleMetadata build() {
8867 if (mPendingIntent == null) {
8868 throw new IllegalStateException("Must supply pending intent to bubble");
8869 }
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008870 if (mIcon == null) {
8871 throw new IllegalStateException("Must supply an icon for the bubble");
8872 }
Mady Mellor138f4c92019-02-20 16:12:17 -08008873 BubbleMetadata data = new BubbleMetadata(mPendingIntent, mDeleteIntent,
Mady Mellor7af771a2019-03-07 15:04:54 -08008874 mIcon, mDesiredHeight, mDesiredHeightResId);
Mady Mellor0157ff22019-01-24 15:31:48 -08008875 data.setFlags(mFlags);
8876 return data;
8877 }
8878
8879 /**
8880 * @hide
8881 */
8882 public BubbleMetadata.Builder setFlag(int mask, boolean value) {
8883 if (value) {
8884 mFlags |= mask;
8885 } else {
8886 mFlags &= ~mask;
8887 }
8888 return this;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008889 }
8890 }
8891 }
8892
8893
Christoph Studer4600f9b2014-07-22 22:44:43 +02008894 // When adding a new Style subclass here, don't forget to update
8895 // Builder.getNotificationStyleClass.
8896
Griff Hazen61a9e862014-05-22 16:05:19 -07008897 /**
8898 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
8899 * metadata or change options on a notification builder.
8900 */
8901 public interface Extender {
8902 /**
8903 * Apply this extender to a notification builder.
8904 * @param builder the builder to be modified.
8905 * @return the build object for chaining.
8906 */
8907 public Builder extend(Builder builder);
8908 }
8909
8910 /**
8911 * Helper class to add wearable extensions to notifications.
8912 * <p class="note"> See
8913 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
8914 * for Android Wear</a> for more information on how to use this class.
8915 * <p>
8916 * To create a notification with wearable extensions:
8917 * <ol>
8918 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
8919 * properties.
8920 * <li>Create a {@link android.app.Notification.WearableExtender}.
8921 * <li>Set wearable-specific properties using the
8922 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
8923 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
8924 * notification.
8925 * <li>Post the notification to the notification system with the
8926 * {@code NotificationManager.notify(...)} methods.
8927 * </ol>
8928 *
8929 * <pre class="prettyprint">
8930 * Notification notif = new Notification.Builder(mContext)
8931 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
8932 * .setContentText(subject)
8933 * .setSmallIcon(R.drawable.new_mail)
8934 * .extend(new Notification.WearableExtender()
8935 * .setContentIcon(R.drawable.new_mail))
8936 * .build();
8937 * NotificationManager notificationManger =
8938 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
8939 * notificationManger.notify(0, notif);</pre>
8940 *
8941 * <p>Wearable extensions can be accessed on an existing notification by using the
8942 * {@code WearableExtender(Notification)} constructor,
8943 * and then using the {@code get} methods to access values.
8944 *
8945 * <pre class="prettyprint">
8946 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
8947 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07008948 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008949 */
8950 public static final class WearableExtender implements Extender {
8951 /**
8952 * Sentinel value for an action index that is unset.
8953 */
8954 public static final int UNSET_ACTION_INDEX = -1;
8955
8956 /**
8957 * Size value for use with {@link #setCustomSizePreset} to show this notification with
8958 * default sizing.
8959 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07008960 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07008961 * on their content.
Gus Prevasd7363752018-09-18 14:35:15 -04008962 *
8963 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008964 */
Gus Prevasd7363752018-09-18 14:35:15 -04008965 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008966 public static final int SIZE_DEFAULT = 0;
8967
8968 /**
8969 * Size value for use with {@link #setCustomSizePreset} to show this notification
8970 * with an extra small size.
8971 * <p>This value is only applicable for custom display notifications created using
8972 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008973 *
8974 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008975 */
Gus Prevasd7363752018-09-18 14:35:15 -04008976 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008977 public static final int SIZE_XSMALL = 1;
8978
8979 /**
8980 * Size value for use with {@link #setCustomSizePreset} to show this notification
8981 * with a small size.
8982 * <p>This value is only applicable for custom display notifications created using
8983 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008984 *
8985 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008986 */
Gus Prevasd7363752018-09-18 14:35:15 -04008987 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008988 public static final int SIZE_SMALL = 2;
8989
8990 /**
8991 * Size value for use with {@link #setCustomSizePreset} to show this notification
8992 * with a medium size.
8993 * <p>This value is only applicable for custom display notifications created using
8994 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008995 *
8996 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008997 */
Gus Prevasd7363752018-09-18 14:35:15 -04008998 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008999 public static final int SIZE_MEDIUM = 3;
9000
9001 /**
9002 * Size value for use with {@link #setCustomSizePreset} to show this notification
9003 * with a large size.
9004 * <p>This value is only applicable for custom display notifications created using
9005 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04009006 *
9007 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009008 */
Gus Prevasd7363752018-09-18 14:35:15 -04009009 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009010 public static final int SIZE_LARGE = 4;
9011
Griff Hazend5f11f92014-05-27 15:40:09 -07009012 /**
9013 * Size value for use with {@link #setCustomSizePreset} to show this notification
9014 * full screen.
9015 * <p>This value is only applicable for custom display notifications created using
9016 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04009017 *
9018 * @deprecated Display intents are no longer supported.
Griff Hazend5f11f92014-05-27 15:40:09 -07009019 */
Gus Prevasd7363752018-09-18 14:35:15 -04009020 @Deprecated
Griff Hazend5f11f92014-05-27 15:40:09 -07009021 public static final int SIZE_FULL_SCREEN = 5;
9022
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009023 /**
9024 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
9025 * short amount of time when this notification is displayed on the screen. This
9026 * is the default value.
Gus Prevasd7363752018-09-18 14:35:15 -04009027 *
9028 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009029 */
Gus Prevasd7363752018-09-18 14:35:15 -04009030 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009031 public static final int SCREEN_TIMEOUT_SHORT = 0;
9032
9033 /**
9034 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
9035 * for a longer amount of time when this notification is displayed on the screen.
Gus Prevasd7363752018-09-18 14:35:15 -04009036 *
9037 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009038 */
Gus Prevasd7363752018-09-18 14:35:15 -04009039 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009040 public static final int SCREEN_TIMEOUT_LONG = -1;
9041
Griff Hazen61a9e862014-05-22 16:05:19 -07009042 /** Notification extra which contains wearable extensions */
9043 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
9044
Pete Gastaf6781d2014-10-07 15:17:05 -04009045 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07009046 private static final String KEY_ACTIONS = "actions";
9047 private static final String KEY_FLAGS = "flags";
9048 private static final String KEY_DISPLAY_INTENT = "displayIntent";
9049 private static final String KEY_PAGES = "pages";
9050 private static final String KEY_BACKGROUND = "background";
9051 private static final String KEY_CONTENT_ICON = "contentIcon";
9052 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
9053 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
9054 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
9055 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
9056 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009057 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04009058 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009059 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07009060
9061 // Flags bitwise-ored to mFlags
9062 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
9063 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
9064 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
9065 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009066 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04009067 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04009068 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07009069
9070 // Default value for flags integer
9071 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
9072
9073 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
9074 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
9075
9076 private ArrayList<Action> mActions = new ArrayList<Action>();
9077 private int mFlags = DEFAULT_FLAGS;
9078 private PendingIntent mDisplayIntent;
9079 private ArrayList<Notification> mPages = new ArrayList<Notification>();
9080 private Bitmap mBackground;
9081 private int mContentIcon;
9082 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
9083 private int mContentActionIndex = UNSET_ACTION_INDEX;
9084 private int mCustomSizePreset = SIZE_DEFAULT;
9085 private int mCustomContentHeight;
9086 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009087 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04009088 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009089 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07009090
9091 /**
9092 * Create a {@link android.app.Notification.WearableExtender} with default
9093 * options.
9094 */
9095 public WearableExtender() {
9096 }
9097
9098 public WearableExtender(Notification notif) {
9099 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
9100 if (wearableBundle != null) {
9101 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
9102 if (actions != null) {
9103 mActions.addAll(actions);
9104 }
9105
9106 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
9107 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
9108
9109 Notification[] pages = getNotificationArrayFromBundle(
9110 wearableBundle, KEY_PAGES);
9111 if (pages != null) {
9112 Collections.addAll(mPages, pages);
9113 }
9114
9115 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
9116 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
9117 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
9118 DEFAULT_CONTENT_ICON_GRAVITY);
9119 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
9120 UNSET_ACTION_INDEX);
9121 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
9122 SIZE_DEFAULT);
9123 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
9124 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009125 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04009126 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009127 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07009128 }
9129 }
9130
9131 /**
9132 * Apply wearable extensions to a notification that is being built. This is typically
9133 * called by the {@link android.app.Notification.Builder#extend} method of
9134 * {@link android.app.Notification.Builder}.
9135 */
9136 @Override
9137 public Notification.Builder extend(Notification.Builder builder) {
9138 Bundle wearableBundle = new Bundle();
9139
9140 if (!mActions.isEmpty()) {
9141 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
9142 }
9143 if (mFlags != DEFAULT_FLAGS) {
9144 wearableBundle.putInt(KEY_FLAGS, mFlags);
9145 }
9146 if (mDisplayIntent != null) {
9147 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
9148 }
9149 if (!mPages.isEmpty()) {
9150 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
9151 new Notification[mPages.size()]));
9152 }
9153 if (mBackground != null) {
9154 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
9155 }
9156 if (mContentIcon != 0) {
9157 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
9158 }
9159 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
9160 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
9161 }
9162 if (mContentActionIndex != UNSET_ACTION_INDEX) {
9163 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
9164 mContentActionIndex);
9165 }
9166 if (mCustomSizePreset != SIZE_DEFAULT) {
9167 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
9168 }
9169 if (mCustomContentHeight != 0) {
9170 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
9171 }
9172 if (mGravity != DEFAULT_GRAVITY) {
9173 wearableBundle.putInt(KEY_GRAVITY, mGravity);
9174 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009175 if (mHintScreenTimeout != 0) {
9176 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
9177 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04009178 if (mDismissalId != null) {
9179 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
9180 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009181 if (mBridgeTag != null) {
9182 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
9183 }
Griff Hazen61a9e862014-05-22 16:05:19 -07009184
9185 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
9186 return builder;
9187 }
9188
9189 @Override
9190 public WearableExtender clone() {
9191 WearableExtender that = new WearableExtender();
9192 that.mActions = new ArrayList<Action>(this.mActions);
9193 that.mFlags = this.mFlags;
9194 that.mDisplayIntent = this.mDisplayIntent;
9195 that.mPages = new ArrayList<Notification>(this.mPages);
9196 that.mBackground = this.mBackground;
9197 that.mContentIcon = this.mContentIcon;
9198 that.mContentIconGravity = this.mContentIconGravity;
9199 that.mContentActionIndex = this.mContentActionIndex;
9200 that.mCustomSizePreset = this.mCustomSizePreset;
9201 that.mCustomContentHeight = this.mCustomContentHeight;
9202 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009203 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04009204 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009205 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07009206 return that;
9207 }
9208
9209 /**
9210 * Add a wearable action to this notification.
9211 *
9212 * <p>When wearable actions are added using this method, the set of actions that
9213 * show on a wearable device splits from devices that only show actions added
9214 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
9215 * of which actions display on different devices.
9216 *
9217 * @param action the action to add to this notification
9218 * @return this object for method chaining
9219 * @see android.app.Notification.Action
9220 */
9221 public WearableExtender addAction(Action action) {
9222 mActions.add(action);
9223 return this;
9224 }
9225
9226 /**
9227 * Adds wearable actions to this notification.
9228 *
9229 * <p>When wearable actions are added using this method, the set of actions that
9230 * show on a wearable device splits from devices that only show actions added
9231 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
9232 * of which actions display on different devices.
9233 *
9234 * @param actions the actions to add to this notification
9235 * @return this object for method chaining
9236 * @see android.app.Notification.Action
9237 */
9238 public WearableExtender addActions(List<Action> actions) {
9239 mActions.addAll(actions);
9240 return this;
9241 }
9242
9243 /**
9244 * Clear all wearable actions present on this builder.
9245 * @return this object for method chaining.
9246 * @see #addAction
9247 */
9248 public WearableExtender clearActions() {
9249 mActions.clear();
9250 return this;
9251 }
9252
9253 /**
9254 * Get the wearable actions present on this notification.
9255 */
9256 public List<Action> getActions() {
9257 return mActions;
9258 }
9259
9260 /**
9261 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07009262 * this notification. The {@link PendingIntent} provided should be for an activity.
9263 *
9264 * <pre class="prettyprint">
9265 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
9266 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
9267 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
9268 * Notification notif = new Notification.Builder(context)
9269 * .extend(new Notification.WearableExtender()
9270 * .setDisplayIntent(displayPendingIntent)
9271 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
9272 * .build();</pre>
9273 *
9274 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07009275 * should have an empty task affinity. It is also recommended to use the device
9276 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07009277 *
9278 * <p>Example AndroidManifest.xml entry:
9279 * <pre class="prettyprint">
9280 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
9281 * android:exported=&quot;true&quot;
9282 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07009283 * android:taskAffinity=&quot;&quot;
9284 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07009285 *
9286 * @param intent the {@link PendingIntent} for an activity
9287 * @return this object for method chaining
9288 * @see android.app.Notification.WearableExtender#getDisplayIntent
Gus Prevasd7363752018-09-18 14:35:15 -04009289 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009290 */
Gus Prevasd7363752018-09-18 14:35:15 -04009291 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009292 public WearableExtender setDisplayIntent(PendingIntent intent) {
9293 mDisplayIntent = intent;
9294 return this;
9295 }
9296
9297 /**
9298 * Get the intent to launch inside of an activity view when displaying this
9299 * notification. This {@code PendingIntent} should be for an activity.
Gus Prevasd7363752018-09-18 14:35:15 -04009300 *
9301 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009302 */
Gus Prevasd7363752018-09-18 14:35:15 -04009303 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009304 public PendingIntent getDisplayIntent() {
9305 return mDisplayIntent;
9306 }
9307
9308 /**
9309 * Add an additional page of content to display with this notification. The current
9310 * notification forms the first page, and pages added using this function form
9311 * subsequent pages. This field can be used to separate a notification into multiple
9312 * sections.
9313 *
9314 * @param page the notification to add as another page
9315 * @return this object for method chaining
9316 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04009317 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009318 */
Gus Prevasd7363752018-09-18 14:35:15 -04009319 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009320 public WearableExtender addPage(Notification page) {
9321 mPages.add(page);
9322 return this;
9323 }
9324
9325 /**
9326 * Add additional pages of content to display with this notification. The current
9327 * notification forms the first page, and pages added using this function form
9328 * subsequent pages. This field can be used to separate a notification into multiple
9329 * sections.
9330 *
9331 * @param pages a list of notifications
9332 * @return this object for method chaining
9333 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04009334 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009335 */
Gus Prevasd7363752018-09-18 14:35:15 -04009336 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009337 public WearableExtender addPages(List<Notification> pages) {
9338 mPages.addAll(pages);
9339 return this;
9340 }
9341
9342 /**
9343 * Clear all additional pages present on this builder.
9344 * @return this object for method chaining.
9345 * @see #addPage
Gus Prevasd7363752018-09-18 14:35:15 -04009346 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009347 */
Gus Prevasd7363752018-09-18 14:35:15 -04009348 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009349 public WearableExtender clearPages() {
9350 mPages.clear();
9351 return this;
9352 }
9353
9354 /**
9355 * Get the array of additional pages of content for displaying this notification. The
9356 * current notification forms the first page, and elements within this array form
9357 * subsequent pages. This field can be used to separate a notification into multiple
9358 * sections.
9359 * @return the pages for this notification
Gus Prevasd7363752018-09-18 14:35:15 -04009360 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009361 */
Gus Prevasd7363752018-09-18 14:35:15 -04009362 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009363 public List<Notification> getPages() {
9364 return mPages;
9365 }
9366
9367 /**
9368 * Set a background image to be displayed behind the notification content.
9369 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
9370 * will work with any notification style.
9371 *
9372 * @param background the background bitmap
9373 * @return this object for method chaining
9374 * @see android.app.Notification.WearableExtender#getBackground
Gus Prevasd7363752018-09-18 14:35:15 -04009375 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009376 */
Gus Prevasd7363752018-09-18 14:35:15 -04009377 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009378 public WearableExtender setBackground(Bitmap background) {
9379 mBackground = background;
9380 return this;
9381 }
9382
9383 /**
9384 * Get a background image to be displayed behind the notification content.
9385 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
9386 * will work with any notification style.
9387 *
9388 * @return the background image
9389 * @see android.app.Notification.WearableExtender#setBackground
Gus Prevasd7363752018-09-18 14:35:15 -04009390 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009391 */
Gus Prevasd7363752018-09-18 14:35:15 -04009392 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009393 public Bitmap getBackground() {
9394 return mBackground;
9395 }
9396
9397 /**
9398 * Set an icon that goes with the content of this notification.
9399 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009400 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009401 public WearableExtender setContentIcon(int icon) {
9402 mContentIcon = icon;
9403 return this;
9404 }
9405
9406 /**
9407 * Get an icon that goes with the content of this notification.
9408 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009409 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009410 public int getContentIcon() {
9411 return mContentIcon;
9412 }
9413
9414 /**
9415 * Set the gravity that the content icon should have within the notification display.
9416 * Supported values include {@link android.view.Gravity#START} and
9417 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
9418 * @see #setContentIcon
9419 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009420 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009421 public WearableExtender setContentIconGravity(int contentIconGravity) {
9422 mContentIconGravity = contentIconGravity;
9423 return this;
9424 }
9425
9426 /**
9427 * Get the gravity that the content icon should have within the notification display.
9428 * Supported values include {@link android.view.Gravity#START} and
9429 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
9430 * @see #getContentIcon
9431 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009432 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009433 public int getContentIconGravity() {
9434 return mContentIconGravity;
9435 }
9436
9437 /**
Gus Prevasd7363752018-09-18 14:35:15 -04009438 * Set an action from this notification's actions as the primary action. If the action has a
9439 * {@link RemoteInput} associated with it, shortcuts to the options for that input are shown
9440 * directly on the notification.
Griff Hazen14f57992014-05-26 09:07:14 -07009441 *
Gus Prevasd7363752018-09-18 14:35:15 -04009442 * @param actionIndex The index of the primary action.
Griff Hazen14f57992014-05-26 09:07:14 -07009443 * If wearable actions were added to the main notification, this index
9444 * will apply to that list, otherwise it will apply to the regular
9445 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07009446 */
9447 public WearableExtender setContentAction(int actionIndex) {
9448 mContentActionIndex = actionIndex;
9449 return this;
9450 }
9451
9452 /**
Gus Prevasd7363752018-09-18 14:35:15 -04009453 * Get the index of the notification action, if any, that was specified as the primary
9454 * action.
Griff Hazen14f57992014-05-26 09:07:14 -07009455 *
9456 * <p>If wearable specific actions were added to the main notification, this index will
9457 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07009458 *
9459 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07009460 */
9461 public int getContentAction() {
9462 return mContentActionIndex;
9463 }
9464
9465 /**
9466 * Set the gravity that this notification should have within the available viewport space.
9467 * Supported values include {@link android.view.Gravity#TOP},
9468 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
9469 * The default value is {@link android.view.Gravity#BOTTOM}.
9470 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009471 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009472 public WearableExtender setGravity(int gravity) {
9473 mGravity = gravity;
9474 return this;
9475 }
9476
9477 /**
9478 * Get the gravity that this notification should have within the available viewport space.
9479 * Supported values include {@link android.view.Gravity#TOP},
9480 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
9481 * The default value is {@link android.view.Gravity#BOTTOM}.
9482 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009483 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009484 public int getGravity() {
9485 return mGravity;
9486 }
9487
9488 /**
9489 * Set the custom size preset for the display of this notification out of the available
9490 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
9491 * {@link #SIZE_LARGE}.
9492 * <p>Some custom size presets are only applicable for custom display notifications created
9493 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
9494 * documentation for the preset in question. See also
9495 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
9496 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009497 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009498 public WearableExtender setCustomSizePreset(int sizePreset) {
9499 mCustomSizePreset = sizePreset;
9500 return this;
9501 }
9502
9503 /**
9504 * Get the custom size preset for the display of this notification out of the available
9505 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
9506 * {@link #SIZE_LARGE}.
9507 * <p>Some custom size presets are only applicable for custom display notifications created
9508 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
9509 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
9510 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009511 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009512 public int getCustomSizePreset() {
9513 return mCustomSizePreset;
9514 }
9515
9516 /**
9517 * Set the custom height in pixels for the display of this notification's content.
9518 * <p>This option is only available for custom display notifications created
9519 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
9520 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
9521 * {@link #getCustomContentHeight}.
9522 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009523 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009524 public WearableExtender setCustomContentHeight(int height) {
9525 mCustomContentHeight = height;
9526 return this;
9527 }
9528
9529 /**
9530 * Get the custom height in pixels for the display of this notification's content.
9531 * <p>This option is only available for custom display notifications created
9532 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
9533 * {@link #setCustomContentHeight}.
9534 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009535 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009536 public int getCustomContentHeight() {
9537 return mCustomContentHeight;
9538 }
9539
9540 /**
9541 * Set whether the scrolling position for the contents of this notification should start
9542 * at the bottom of the contents instead of the top when the contents are too long to
9543 * display within the screen. Default is false (start scroll at the top).
9544 */
9545 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
9546 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
9547 return this;
9548 }
9549
9550 /**
9551 * Get whether the scrolling position for the contents of this notification should start
9552 * at the bottom of the contents instead of the top when the contents are too long to
9553 * display within the screen. Default is false (start scroll at the top).
9554 */
9555 public boolean getStartScrollBottom() {
9556 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
9557 }
9558
9559 /**
9560 * Set whether the content intent is available when the wearable device is not connected
9561 * to a companion device. The user can still trigger this intent when the wearable device
9562 * is offline, but a visual hint will indicate that the content intent may not be available.
9563 * Defaults to true.
9564 */
9565 public WearableExtender setContentIntentAvailableOffline(
9566 boolean contentIntentAvailableOffline) {
9567 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
9568 return this;
9569 }
9570
9571 /**
9572 * Get whether the content intent is available when the wearable device is not connected
9573 * to a companion device. The user can still trigger this intent when the wearable device
9574 * is offline, but a visual hint will indicate that the content intent may not be available.
9575 * Defaults to true.
9576 */
9577 public boolean getContentIntentAvailableOffline() {
9578 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
9579 }
9580
9581 /**
9582 * Set a hint that this notification's icon should not be displayed.
9583 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
9584 * @return this object for method chaining
9585 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009586 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009587 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
9588 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
9589 return this;
9590 }
9591
9592 /**
9593 * Get a hint that this notification's icon should not be displayed.
9594 * @return {@code true} if this icon should not be displayed, false otherwise.
9595 * The default value is {@code false} if this was never set.
9596 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009597 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009598 public boolean getHintHideIcon() {
9599 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
9600 }
9601
9602 /**
9603 * Set a visual hint that only the background image of this notification should be
9604 * displayed, and other semantic content should be hidden. This hint is only applicable
9605 * to sub-pages added using {@link #addPage}.
9606 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009607 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009608 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
9609 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
9610 return this;
9611 }
9612
9613 /**
9614 * Get a visual hint that only the background image of this notification should be
9615 * displayed, and other semantic content should be hidden. This hint is only applicable
9616 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
9617 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009618 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009619 public boolean getHintShowBackgroundOnly() {
9620 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
9621 }
9622
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009623 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009624 * Set a hint that this notification's background should not be clipped if possible,
9625 * and should instead be resized to fully display on the screen, retaining the aspect
9626 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009627 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
9628 * @return this object for method chaining
9629 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009630 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009631 public WearableExtender setHintAvoidBackgroundClipping(
9632 boolean hintAvoidBackgroundClipping) {
9633 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
9634 return this;
9635 }
9636
9637 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009638 * Get a hint that this notification's background should not be clipped if possible,
9639 * and should instead be resized to fully display on the screen, retaining the aspect
9640 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009641 * @return {@code true} if it's ok if the background is clipped on the screen, false
9642 * otherwise. The default value is {@code false} if this was never set.
9643 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009644 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009645 public boolean getHintAvoidBackgroundClipping() {
9646 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
9647 }
9648
9649 /**
9650 * Set a hint that the screen should remain on for at least this duration when
9651 * this notification is displayed on the screen.
9652 * @param timeout The requested screen timeout in milliseconds. Can also be either
9653 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9654 * @return this object for method chaining
9655 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009656 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009657 public WearableExtender setHintScreenTimeout(int timeout) {
9658 mHintScreenTimeout = timeout;
9659 return this;
9660 }
9661
9662 /**
9663 * Get the duration, in milliseconds, that the screen should remain on for
9664 * when this notification is displayed.
9665 * @return the duration in milliseconds if > 0, or either one of the sentinel values
9666 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9667 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009668 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009669 public int getHintScreenTimeout() {
9670 return mHintScreenTimeout;
9671 }
9672
Alex Hills9ab3a232016-04-05 14:54:56 -04009673 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04009674 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
9675 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9676 * qr codes, as well as other simple black-and-white tickets.
9677 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
9678 * @return this object for method chaining
Gus Prevasd7363752018-09-18 14:35:15 -04009679 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009680 */
Gus Prevasd7363752018-09-18 14:35:15 -04009681 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009682 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
9683 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
9684 return this;
9685 }
9686
9687 /**
9688 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
9689 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9690 * qr codes, as well as other simple black-and-white tickets.
9691 * @return {@code true} if it should be displayed in ambient, false otherwise
9692 * otherwise. The default value is {@code false} if this was never set.
Gus Prevasd7363752018-09-18 14:35:15 -04009693 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009694 */
Gus Prevasd7363752018-09-18 14:35:15 -04009695 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009696 public boolean getHintAmbientBigPicture() {
9697 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
9698 }
9699
9700 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04009701 * Set a hint that this notification's content intent will launch an {@link Activity}
9702 * directly, telling the platform that it can generate the appropriate transitions.
9703 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
9704 * an activity and transitions should be generated, false otherwise.
9705 * @return this object for method chaining
9706 */
9707 public WearableExtender setHintContentIntentLaunchesActivity(
9708 boolean hintContentIntentLaunchesActivity) {
9709 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
9710 return this;
9711 }
9712
9713 /**
9714 * Get a hint that this notification's content intent will launch an {@link Activity}
9715 * directly, telling the platform that it can generate the appropriate transitions
9716 * @return {@code true} if the content intent will launch an activity and transitions should
9717 * be generated, false otherwise. The default value is {@code false} if this was never set.
9718 */
9719 public boolean getHintContentIntentLaunchesActivity() {
9720 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
9721 }
9722
Nadia Benbernou948627e2016-04-14 14:41:08 -04009723 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009724 * Sets the dismissal id for this notification. If a notification is posted with a
9725 * dismissal id, then when that notification is canceled, notifications on other wearables
9726 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04009727 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009728 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04009729 * @param dismissalId the dismissal id of the notification.
9730 * @return this object for method chaining
9731 */
9732 public WearableExtender setDismissalId(String dismissalId) {
9733 mDismissalId = dismissalId;
9734 return this;
9735 }
9736
9737 /**
9738 * Returns the dismissal id of the notification.
9739 * @return the dismissal id of the notification or null if it has not been set.
9740 */
9741 public String getDismissalId() {
9742 return mDismissalId;
9743 }
9744
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009745 /**
9746 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
9747 * posted from a phone to provide finer-grained control on what notifications are bridged
9748 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
9749 * Features to Notifications</a> for more information.
9750 * @param bridgeTag the bridge tag of the notification.
9751 * @return this object for method chaining
9752 */
9753 public WearableExtender setBridgeTag(String bridgeTag) {
9754 mBridgeTag = bridgeTag;
9755 return this;
9756 }
9757
9758 /**
9759 * Returns the bridge tag of the notification.
9760 * @return the bridge tag or null if not present.
9761 */
9762 public String getBridgeTag() {
9763 return mBridgeTag;
9764 }
9765
Griff Hazen61a9e862014-05-22 16:05:19 -07009766 private void setFlag(int mask, boolean value) {
9767 if (value) {
9768 mFlags |= mask;
9769 } else {
9770 mFlags &= ~mask;
9771 }
9772 }
9773 }
9774
9775 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009776 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
9777 * with car extensions:
9778 *
9779 * <ol>
9780 * <li>Create an {@link Notification.Builder}, setting any desired
9781 * properties.
9782 * <li>Create a {@link CarExtender}.
9783 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
9784 * {@link CarExtender}.
9785 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9786 * to apply the extensions to a notification.
9787 * </ol>
9788 *
9789 * <pre class="prettyprint">
9790 * Notification notification = new Notification.Builder(context)
9791 * ...
9792 * .extend(new CarExtender()
9793 * .set*(...))
9794 * .build();
9795 * </pre>
9796 *
9797 * <p>Car extensions can be accessed on an existing notification by using the
9798 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
9799 * to access values.
9800 */
9801 public static final class CarExtender implements Extender {
9802 private static final String TAG = "CarExtender";
9803
9804 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
9805 private static final String EXTRA_LARGE_ICON = "large_icon";
9806 private static final String EXTRA_CONVERSATION = "car_conversation";
9807 private static final String EXTRA_COLOR = "app_color";
9808
9809 private Bitmap mLargeIcon;
9810 private UnreadConversation mUnreadConversation;
9811 private int mColor = Notification.COLOR_DEFAULT;
9812
9813 /**
9814 * Create a {@link CarExtender} with default options.
9815 */
9816 public CarExtender() {
9817 }
9818
9819 /**
9820 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
9821 *
9822 * @param notif The notification from which to copy options.
9823 */
9824 public CarExtender(Notification notif) {
9825 Bundle carBundle = notif.extras == null ?
9826 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
9827 if (carBundle != null) {
9828 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
9829 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
9830
9831 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
9832 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
9833 }
9834 }
9835
9836 /**
9837 * Apply car extensions to a notification that is being built. This is typically called by
9838 * the {@link Notification.Builder#extend(Notification.Extender)}
9839 * method of {@link Notification.Builder}.
9840 */
9841 @Override
9842 public Notification.Builder extend(Notification.Builder builder) {
9843 Bundle carExtensions = new Bundle();
9844
9845 if (mLargeIcon != null) {
9846 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
9847 }
9848 if (mColor != Notification.COLOR_DEFAULT) {
9849 carExtensions.putInt(EXTRA_COLOR, mColor);
9850 }
9851
9852 if (mUnreadConversation != null) {
9853 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
9854 carExtensions.putBundle(EXTRA_CONVERSATION, b);
9855 }
9856
9857 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
9858 return builder;
9859 }
9860
9861 /**
9862 * Sets the accent color to use when Android Auto presents the notification.
9863 *
9864 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
9865 * to accent the displayed notification. However, not all colors are acceptable in an
9866 * automotive setting. This method can be used to override the color provided in the
9867 * notification in such a situation.
9868 */
Tor Norbye80756e32015-03-02 09:39:27 -08009869 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009870 mColor = color;
9871 return this;
9872 }
9873
9874 /**
9875 * Gets the accent color.
9876 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04009877 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009878 */
Tor Norbye80756e32015-03-02 09:39:27 -08009879 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009880 public int getColor() {
9881 return mColor;
9882 }
9883
9884 /**
9885 * Sets the large icon of the car notification.
9886 *
9887 * If no large icon is set in the extender, Android Auto will display the icon
9888 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
9889 *
9890 * @param largeIcon The large icon to use in the car notification.
9891 * @return This object for method chaining.
9892 */
9893 public CarExtender setLargeIcon(Bitmap largeIcon) {
9894 mLargeIcon = largeIcon;
9895 return this;
9896 }
9897
9898 /**
9899 * Gets the large icon used in this car notification, or null if no icon has been set.
9900 *
9901 * @return The large icon for the car notification.
9902 * @see CarExtender#setLargeIcon
9903 */
9904 public Bitmap getLargeIcon() {
9905 return mLargeIcon;
9906 }
9907
9908 /**
9909 * Sets the unread conversation in a message notification.
9910 *
9911 * @param unreadConversation The unread part of the conversation this notification conveys.
9912 * @return This object for method chaining.
9913 */
9914 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
9915 mUnreadConversation = unreadConversation;
9916 return this;
9917 }
9918
9919 /**
9920 * Returns the unread conversation conveyed by this notification.
9921 * @see #setUnreadConversation(UnreadConversation)
9922 */
9923 public UnreadConversation getUnreadConversation() {
9924 return mUnreadConversation;
9925 }
9926
9927 /**
9928 * A class which holds the unread messages from a conversation.
9929 */
9930 public static class UnreadConversation {
9931 private static final String KEY_AUTHOR = "author";
9932 private static final String KEY_TEXT = "text";
9933 private static final String KEY_MESSAGES = "messages";
9934 private static final String KEY_REMOTE_INPUT = "remote_input";
9935 private static final String KEY_ON_REPLY = "on_reply";
9936 private static final String KEY_ON_READ = "on_read";
9937 private static final String KEY_PARTICIPANTS = "participants";
9938 private static final String KEY_TIMESTAMP = "timestamp";
9939
9940 private final String[] mMessages;
9941 private final RemoteInput mRemoteInput;
9942 private final PendingIntent mReplyPendingIntent;
9943 private final PendingIntent mReadPendingIntent;
9944 private final String[] mParticipants;
9945 private final long mLatestTimestamp;
9946
9947 UnreadConversation(String[] messages, RemoteInput remoteInput,
9948 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
9949 String[] participants, long latestTimestamp) {
9950 mMessages = messages;
9951 mRemoteInput = remoteInput;
9952 mReadPendingIntent = readPendingIntent;
9953 mReplyPendingIntent = replyPendingIntent;
9954 mParticipants = participants;
9955 mLatestTimestamp = latestTimestamp;
9956 }
9957
9958 /**
9959 * Gets the list of messages conveyed by this notification.
9960 */
9961 public String[] getMessages() {
9962 return mMessages;
9963 }
9964
9965 /**
9966 * Gets the remote input that will be used to convey the response to a message list, or
9967 * null if no such remote input exists.
9968 */
9969 public RemoteInput getRemoteInput() {
9970 return mRemoteInput;
9971 }
9972
9973 /**
9974 * Gets the pending intent that will be triggered when the user replies to this
9975 * notification.
9976 */
9977 public PendingIntent getReplyPendingIntent() {
9978 return mReplyPendingIntent;
9979 }
9980
9981 /**
9982 * Gets the pending intent that Android Auto will send after it reads aloud all messages
9983 * in this object's message list.
9984 */
9985 public PendingIntent getReadPendingIntent() {
9986 return mReadPendingIntent;
9987 }
9988
9989 /**
9990 * Gets the participants in the conversation.
9991 */
9992 public String[] getParticipants() {
9993 return mParticipants;
9994 }
9995
9996 /**
9997 * Gets the firs participant in the conversation.
9998 */
9999 public String getParticipant() {
10000 return mParticipants.length > 0 ? mParticipants[0] : null;
10001 }
10002
10003 /**
10004 * Gets the timestamp of the conversation.
10005 */
10006 public long getLatestTimestamp() {
10007 return mLatestTimestamp;
10008 }
10009
10010 Bundle getBundleForUnreadConversation() {
10011 Bundle b = new Bundle();
10012 String author = null;
10013 if (mParticipants != null && mParticipants.length > 1) {
10014 author = mParticipants[0];
10015 }
10016 Parcelable[] messages = new Parcelable[mMessages.length];
10017 for (int i = 0; i < messages.length; i++) {
10018 Bundle m = new Bundle();
10019 m.putString(KEY_TEXT, mMessages[i]);
10020 m.putString(KEY_AUTHOR, author);
10021 messages[i] = m;
10022 }
10023 b.putParcelableArray(KEY_MESSAGES, messages);
10024 if (mRemoteInput != null) {
10025 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
10026 }
10027 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
10028 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
10029 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
10030 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
10031 return b;
10032 }
10033
10034 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
10035 if (b == null) {
10036 return null;
10037 }
10038 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
10039 String[] messages = null;
10040 if (parcelableMessages != null) {
10041 String[] tmp = new String[parcelableMessages.length];
10042 boolean success = true;
10043 for (int i = 0; i < tmp.length; i++) {
10044 if (!(parcelableMessages[i] instanceof Bundle)) {
10045 success = false;
10046 break;
10047 }
10048 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
10049 if (tmp[i] == null) {
10050 success = false;
10051 break;
10052 }
10053 }
10054 if (success) {
10055 messages = tmp;
10056 } else {
10057 return null;
10058 }
10059 }
10060
10061 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
10062 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
10063
10064 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
10065
10066 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
10067 if (participants == null || participants.length != 1) {
10068 return null;
10069 }
10070
10071 return new UnreadConversation(messages,
10072 remoteInput,
10073 onReply,
10074 onRead,
10075 participants, b.getLong(KEY_TIMESTAMP));
10076 }
10077 };
10078
10079 /**
10080 * Builder class for {@link CarExtender.UnreadConversation} objects.
10081 */
10082 public static class Builder {
10083 private final List<String> mMessages = new ArrayList<String>();
10084 private final String mParticipant;
10085 private RemoteInput mRemoteInput;
10086 private PendingIntent mReadPendingIntent;
10087 private PendingIntent mReplyPendingIntent;
10088 private long mLatestTimestamp;
10089
10090 /**
10091 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
10092 *
10093 * @param name The name of the other participant in the conversation.
10094 */
10095 public Builder(String name) {
10096 mParticipant = name;
10097 }
10098
10099 /**
10100 * Appends a new unread message to the list of messages for this conversation.
10101 *
10102 * The messages should be added from oldest to newest.
10103 *
10104 * @param message The text of the new unread message.
10105 * @return This object for method chaining.
10106 */
10107 public Builder addMessage(String message) {
10108 mMessages.add(message);
10109 return this;
10110 }
10111
10112 /**
10113 * Sets the pending intent and remote input which will convey the reply to this
10114 * notification.
10115 *
10116 * @param pendingIntent The pending intent which will be triggered on a reply.
10117 * @param remoteInput The remote input parcelable which will carry the reply.
10118 * @return This object for method chaining.
10119 *
10120 * @see CarExtender.UnreadConversation#getRemoteInput
10121 * @see CarExtender.UnreadConversation#getReplyPendingIntent
10122 */
10123 public Builder setReplyAction(
10124 PendingIntent pendingIntent, RemoteInput remoteInput) {
10125 mRemoteInput = remoteInput;
10126 mReplyPendingIntent = pendingIntent;
10127
10128 return this;
10129 }
10130
10131 /**
10132 * Sets the pending intent that will be sent once the messages in this notification
10133 * are read.
10134 *
10135 * @param pendingIntent The pending intent to use.
10136 * @return This object for method chaining.
10137 */
10138 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
10139 mReadPendingIntent = pendingIntent;
10140 return this;
10141 }
10142
10143 /**
10144 * Sets the timestamp of the most recent message in an unread conversation.
10145 *
10146 * If a messaging notification has been posted by your application and has not
10147 * yet been cancelled, posting a later notification with the same id and tag
10148 * but without a newer timestamp may result in Android Auto not displaying a
10149 * heads up notification for the later notification.
10150 *
10151 * @param timestamp The timestamp of the most recent message in the conversation.
10152 * @return This object for method chaining.
10153 */
10154 public Builder setLatestTimestamp(long timestamp) {
10155 mLatestTimestamp = timestamp;
10156 return this;
10157 }
10158
10159 /**
10160 * Builds a new unread conversation object.
10161 *
10162 * @return The new unread conversation object.
10163 */
10164 public UnreadConversation build() {
10165 String[] messages = mMessages.toArray(new String[mMessages.size()]);
10166 String[] participants = { mParticipant };
10167 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
10168 mReadPendingIntent, participants, mLatestTimestamp);
10169 }
10170 }
10171 }
10172
10173 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010174 * <p>Helper class to add Android TV extensions to notifications. To create a notification
10175 * with a TV extension:
10176 *
10177 * <ol>
10178 * <li>Create an {@link Notification.Builder}, setting any desired properties.
10179 * <li>Create a {@link TvExtender}.
10180 * <li>Set TV-specific properties using the {@code set} methods of
10181 * {@link TvExtender}.
10182 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
10183 * to apply the extension to a notification.
10184 * </ol>
10185 *
10186 * <pre class="prettyprint">
10187 * Notification notification = new Notification.Builder(context)
10188 * ...
10189 * .extend(new TvExtender()
10190 * .set*(...))
10191 * .build();
10192 * </pre>
10193 *
10194 * <p>TV extensions can be accessed on an existing notification by using the
10195 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
10196 * to access values.
10197 *
10198 * @hide
10199 */
10200 @SystemApi
10201 public static final class TvExtender implements Extender {
10202 private static final String TAG = "TvExtender";
10203
10204 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
10205 private static final String EXTRA_FLAGS = "flags";
10206 private static final String EXTRA_CONTENT_INTENT = "content_intent";
10207 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010208 private static final String EXTRA_CHANNEL_ID = "channel_id";
Rhiannon Malia1a083932018-01-24 15:02:30 -080010209 private static final String EXTRA_SUPPRESS_SHOW_OVER_APPS = "suppressShowOverApps";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010210
10211 // Flags bitwise-ored to mFlags
10212 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
10213
10214 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010215 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010216 private PendingIntent mContentIntent;
10217 private PendingIntent mDeleteIntent;
Rhiannon Malia1a083932018-01-24 15:02:30 -080010218 private boolean mSuppressShowOverApps;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010219
10220 /**
10221 * Create a {@link TvExtender} with default options.
10222 */
10223 public TvExtender() {
10224 mFlags = FLAG_AVAILABLE_ON_TV;
10225 }
10226
10227 /**
10228 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
10229 *
10230 * @param notif The notification from which to copy options.
10231 */
10232 public TvExtender(Notification notif) {
10233 Bundle bundle = notif.extras == null ?
10234 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
10235 if (bundle != null) {
10236 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010237 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Rhiannon Malia1a083932018-01-24 15:02:30 -080010238 mSuppressShowOverApps = bundle.getBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010239 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
10240 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
10241 }
10242 }
10243
10244 /**
10245 * Apply a TV extension to a notification that is being built. This is typically called by
10246 * the {@link Notification.Builder#extend(Notification.Extender)}
10247 * method of {@link Notification.Builder}.
10248 */
10249 @Override
10250 public Notification.Builder extend(Notification.Builder builder) {
10251 Bundle bundle = new Bundle();
10252
10253 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010254 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Rhiannon Malia1a083932018-01-24 15:02:30 -080010255 bundle.putBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS, mSuppressShowOverApps);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010256 if (mContentIntent != null) {
10257 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
10258 }
10259
10260 if (mDeleteIntent != null) {
10261 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
10262 }
10263
10264 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
10265 return builder;
10266 }
10267
10268 /**
10269 * Returns true if this notification should be shown on TV. This method return true
10270 * if the notification was extended with a TvExtender.
10271 */
10272 public boolean isAvailableOnTv() {
10273 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
10274 }
10275
10276 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010277 * Specifies the channel the notification should be delivered on when shown on TV.
10278 * It can be different from the channel that the notification is delivered to when
10279 * posting on a non-TV device.
10280 */
10281 public TvExtender setChannel(String channelId) {
10282 mChannelId = channelId;
10283 return this;
10284 }
10285
10286 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -040010287 * Specifies the channel the notification should be delivered on when shown on TV.
10288 * It can be different from the channel that the notification is delivered to when
10289 * posting on a non-TV device.
10290 */
10291 public TvExtender setChannelId(String channelId) {
10292 mChannelId = channelId;
10293 return this;
10294 }
10295
Jeff Sharkey000ce802017-04-29 13:13:27 -060010296 /** @removed */
10297 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010298 public String getChannel() {
10299 return mChannelId;
10300 }
10301
10302 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -040010303 * Returns the id of the channel this notification posts to on TV.
10304 */
10305 public String getChannelId() {
10306 return mChannelId;
10307 }
10308
10309 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010310 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
10311 * If provided, it is used instead of the content intent specified
10312 * at the level of Notification.
10313 */
10314 public TvExtender setContentIntent(PendingIntent intent) {
10315 mContentIntent = intent;
10316 return this;
10317 }
10318
10319 /**
10320 * Returns the TV-specific content intent. If this method returns null, the
10321 * main content intent on the notification should be used.
10322 *
10323 * @see {@link Notification#contentIntent}
10324 */
10325 public PendingIntent getContentIntent() {
10326 return mContentIntent;
10327 }
10328
10329 /**
10330 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
10331 * by the user on TV. If provided, it is used instead of the delete intent specified
10332 * at the level of Notification.
10333 */
10334 public TvExtender setDeleteIntent(PendingIntent intent) {
10335 mDeleteIntent = intent;
10336 return this;
10337 }
10338
10339 /**
10340 * Returns the TV-specific delete intent. If this method returns null, the
10341 * main delete intent on the notification should be used.
10342 *
10343 * @see {@link Notification#deleteIntent}
10344 */
10345 public PendingIntent getDeleteIntent() {
10346 return mDeleteIntent;
10347 }
Rhiannon Malia1a083932018-01-24 15:02:30 -080010348
10349 /**
10350 * Specifies whether this notification should suppress showing a message over top of apps
10351 * outside of the launcher.
10352 */
10353 public TvExtender setSuppressShowOverApps(boolean suppress) {
10354 mSuppressShowOverApps = suppress;
10355 return this;
10356 }
10357
10358 /**
10359 * Returns true if this notification should not show messages over top of apps
10360 * outside of the launcher.
10361 */
10362 public boolean getSuppressShowOverApps() {
10363 return mSuppressShowOverApps;
10364 }
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010365 }
10366
10367 /**
Griff Hazen61a9e862014-05-22 16:05:19 -070010368 * Get an array of Notification objects from a parcelable array bundle field.
10369 * Update the bundle to have a typed array so fetches in the future don't need
10370 * to do an array copy.
10371 */
10372 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
10373 Parcelable[] array = bundle.getParcelableArray(key);
10374 if (array instanceof Notification[] || array == null) {
10375 return (Notification[]) array;
10376 }
10377 Notification[] typedArray = Arrays.copyOf(array, array.length,
10378 Notification[].class);
10379 bundle.putParcelableArray(key, typedArray);
10380 return typedArray;
10381 }
Christoph Studer4600f9b2014-07-22 22:44:43 +020010382
10383 private static class BuilderRemoteViews extends RemoteViews {
10384 public BuilderRemoteViews(Parcel parcel) {
10385 super(parcel);
10386 }
10387
Kenny Guy77320062014-08-27 21:37:15 +010010388 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
10389 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +020010390 }
10391
10392 @Override
10393 public BuilderRemoteViews clone() {
10394 Parcel p = Parcel.obtain();
10395 writeToParcel(p, 0);
10396 p.setDataPosition(0);
10397 BuilderRemoteViews brv = new BuilderRemoteViews(p);
10398 p.recycle();
10399 return brv;
10400 }
10401 }
Adrian Roos70d7aa32017-01-11 15:39:06 -080010402
Selim Cinek384804b2018-04-18 14:31:07 +080010403 /**
10404 * A result object where information about the template that was created is saved.
10405 */
10406 private static class TemplateBindResult {
Selim Cinek1c72fa02018-04-23 18:00:54 +080010407 int mIconMarginEnd;
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010408 boolean mRightIconContainerVisible;
Selim Cinek384804b2018-04-18 14:31:07 +080010409
10410 /**
Selim Cinek1c72fa02018-04-23 18:00:54 +080010411 * Get the margin end that needs to be added to any fields that may overlap
Selim Cinek384804b2018-04-18 14:31:07 +080010412 * with the right actions.
10413 */
Selim Cinek1c72fa02018-04-23 18:00:54 +080010414 public int getIconMarginEnd() {
10415 return mIconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +080010416 }
10417
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010418 /**
10419 * Is the icon container visible on the right size because of the reply button or the
10420 * right icon.
10421 */
10422 public boolean isRightIconContainerVisible() {
10423 return mRightIconContainerVisible;
10424 }
10425
Selim Cinek1c72fa02018-04-23 18:00:54 +080010426 public void setIconMarginEnd(int iconMarginEnd) {
10427 this.mIconMarginEnd = iconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +080010428 }
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010429
10430 public void setRightIconContainerVisible(boolean iconContainerVisible) {
10431 mRightIconContainerVisible = iconContainerVisible;
10432 }
Selim Cinek384804b2018-04-18 14:31:07 +080010433 }
10434
Adrian Roos70d7aa32017-01-11 15:39:06 -080010435 private static class StandardTemplateParams {
10436 boolean hasProgress = true;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010437 CharSequence title;
10438 CharSequence text;
Selim Cinekafeed292017-12-12 17:32:44 -080010439 CharSequence headerTextSecondary;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010440 CharSequence summaryText;
Selim Cinekbee4e072018-05-21 22:06:43 -070010441 int maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek88188f22017-09-19 16:46:56 -070010442 boolean hideLargeIcon;
Selim Cinek384804b2018-04-18 14:31:07 +080010443 boolean hideReplyIcon;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010444 boolean allowColorization = true;
10445 boolean forceDefaultColor = false;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010446
10447 final StandardTemplateParams reset() {
10448 hasProgress = true;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010449 title = null;
10450 text = null;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010451 summaryText = null;
Selim Cinekafeed292017-12-12 17:32:44 -080010452 headerTextSecondary = null;
Selim Cinekbee4e072018-05-21 22:06:43 -070010453 maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010454 allowColorization = true;
10455 forceDefaultColor = false;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010456 return this;
10457 }
10458
10459 final StandardTemplateParams hasProgress(boolean hasProgress) {
10460 this.hasProgress = hasProgress;
10461 return this;
10462 }
10463
10464 final StandardTemplateParams title(CharSequence title) {
10465 this.title = title;
10466 return this;
10467 }
10468
10469 final StandardTemplateParams text(CharSequence text) {
10470 this.text = text;
10471 return this;
10472 }
10473
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010474 final StandardTemplateParams summaryText(CharSequence text) {
10475 this.summaryText = text;
10476 return this;
10477 }
10478
Selim Cinekafeed292017-12-12 17:32:44 -080010479 final StandardTemplateParams headerTextSecondary(CharSequence text) {
10480 this.headerTextSecondary = text;
10481 return this;
10482 }
10483
Selim Cinek384804b2018-04-18 14:31:07 +080010484 final StandardTemplateParams hideLargeIcon(boolean hideLargeIcon) {
10485 this.hideLargeIcon = hideLargeIcon;
Selim Cinek88188f22017-09-19 16:46:56 -070010486 return this;
10487 }
10488
Selim Cinek384804b2018-04-18 14:31:07 +080010489 final StandardTemplateParams hideReplyIcon(boolean hideReplyIcon) {
10490 this.hideReplyIcon = hideReplyIcon;
Selim Cinek88188f22017-09-19 16:46:56 -070010491 return this;
10492 }
10493
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010494 final StandardTemplateParams disallowColorization() {
10495 this.allowColorization = false;
10496 return this;
10497 }
10498
10499 final StandardTemplateParams forceDefaultColor() {
10500 this.forceDefaultColor = true;
10501 return this;
10502 }
10503
Adrian Roos70d7aa32017-01-11 15:39:06 -080010504 final StandardTemplateParams fillTextsFrom(Builder b) {
10505 Bundle extras = b.mN.extras;
Lucas Dupin00be88f2019-01-03 17:50:52 -080010506 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE));
Lucas Dupin06c5e642017-09-13 16:34:58 -070010507
Lucas Dupin06c5e642017-09-13 16:34:58 -070010508 CharSequence text = extras.getCharSequence(EXTRA_BIG_TEXT);
Lucas Dupin00be88f2019-01-03 17:50:52 -080010509 if (TextUtils.isEmpty(text)) {
Lucas Dupin06c5e642017-09-13 16:34:58 -070010510 text = extras.getCharSequence(EXTRA_TEXT);
10511 }
Lucas Dupin00be88f2019-01-03 17:50:52 -080010512 this.text = b.processLegacyText(text);
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010513 this.summaryText = extras.getCharSequence(EXTRA_SUB_TEXT);
Adrian Roos70d7aa32017-01-11 15:39:06 -080010514 return this;
10515 }
Selim Cinekbee4e072018-05-21 22:06:43 -070010516
10517 /**
10518 * Set the maximum lines of remote input history lines allowed.
10519 * @param maxRemoteInputHistory The number of lines.
10520 * @return The builder for method chaining.
10521 */
10522 public StandardTemplateParams setMaxRemoteInputHistory(int maxRemoteInputHistory) {
10523 this.maxRemoteInputHistory = maxRemoteInputHistory;
10524 return this;
10525 }
Adrian Roos70d7aa32017-01-11 15:39:06 -080010526 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010527}