blob: a8a34d2856e2f3a9f9a26a8a3ec677fb5b3789fe [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
Lucas Dupina291d192018-06-07 13:59:42 -070019import static com.android.internal.util.ContrastColorUtil.satisfiesTextContrast;
Selim Cinek389edcd2017-05-11 19:16:44 -070020
Tor Norbye80756e32015-03-02 09:39:27 -080021import android.annotation.ColorInt;
Tor Norbye7b9c9122013-05-30 16:48:33 -070022import android.annotation.DrawableRes;
Gus Prevas9cc96602018-10-11 11:24:23 -040023import android.annotation.IdRes;
Tor Norbyed9273d62013-05-30 15:59:53 -070024import android.annotation.IntDef;
Alex Hillsfd590442016-10-07 09:52:44 -040025import android.annotation.NonNull;
Selim Cinek88188f22017-09-19 16:46:56 -070026import android.annotation.Nullable;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060027import android.annotation.RequiresPermission;
Daniel Sandler01df1c62014-06-09 10:54:01 -040028import android.annotation.SdkConstant;
29import android.annotation.SdkConstant.SdkConstantType;
Julia Reynoldse46bb372016-03-17 11:05:58 -040030import android.annotation.SystemApi;
Mathew Inwood61e8ae62018-08-14 14:17:44 +010031import android.annotation.UnsupportedAppUsage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.content.Context;
33import android.content.Intent;
Felipe Leme90205ef2019-03-05 09:59:52 -080034import android.content.LocusId;
Kenny Guy77320062014-08-27 21:37:15 +010035import android.content.pm.ApplicationInfo;
Dan Sandler732bd6c2016-04-12 14:20:32 -040036import android.content.pm.PackageManager;
Christoph Studer4600f9b2014-07-22 22:44:43 +020037import android.content.pm.PackageManager.NameNotFoundException;
Julia Reynolds13d898c2017-02-02 12:22:05 -050038import android.content.pm.ShortcutInfo;
Jorim Jaggief72a192014-08-26 21:57:46 +020039import android.content.res.ColorStateList;
Anthony Chenad4d1582017-04-10 16:07:58 -070040import android.content.res.Configuration;
41import android.content.res.Resources;
Gus Prevas9cc96602018-10-11 11:24:23 -040042import android.content.res.TypedArray;
Joe Onoratoef1e7762010-09-17 18:38:38 -040043import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010044import android.graphics.Canvas;
Adrian Roosc1a80b02016-04-05 14:54:55 -070045import android.graphics.Color;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010046import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010047import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040048import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040049import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070050import android.media.AudioManager;
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -080051import android.media.PlayerBase;
Jeff Browndba34ba2014-06-24 20:46:03 -070052import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040054import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020055import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050056import android.os.Bundle;
Dianne Hackborn98305522017-05-05 17:53:53 -070057import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.os.Parcel;
59import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040060import android.os.SystemClock;
Selim Cinek6743c0b2017-01-18 18:24:01 -080061import android.os.SystemProperties;
Jeff Sharkey6d515712012-09-20 16:06:08 -070062import android.os.UserHandle;
Adrian Roosc1a80b02016-04-05 14:54:55 -070063import android.text.BidiFormatter;
Selim Cinek60a54252016-02-26 17:03:25 -080064import android.text.SpannableStringBuilder;
65import android.text.Spanned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.text.TextUtils;
Selim Cinek60a54252016-02-26 17:03:25 -080067import android.text.style.AbsoluteSizeSpan;
Selim Cinek89991a22016-03-07 19:51:54 -080068import android.text.style.CharacterStyle;
Selim Cinek981962e2016-07-20 20:41:58 -070069import android.text.style.ForegroundColorSpan;
Selim Cinek60a54252016-02-26 17:03:25 -080070import android.text.style.RelativeSizeSpan;
71import android.text.style.TextAppearanceSpan;
Svet Ganovddb94882016-06-23 19:55:24 -070072import android.util.ArraySet;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040073import android.util.Log;
Tony Mak638430e2018-10-08 19:19:10 +010074import android.util.Pair;
Dan Sandler50128532015-12-08 15:42:41 -050075import android.util.SparseArray;
Yi Jin6b514142017-10-30 14:54:12 -070076import android.util.proto.ProtoOutputStream;
Griff Hazen61a9e862014-05-22 16:05:19 -070077import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080078import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080079import android.view.View;
Selim Cinek954cc232016-05-20 13:29:23 -070080import android.view.ViewGroup;
Felipe Leme90205ef2019-03-05 09:59:52 -080081import android.view.contentcapture.ContentCaptureContext;
Jeff Sharkey1c400132011-08-05 14:50:13 -070082import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import android.widget.RemoteViews;
84
Griff Hazen959591e2014-05-15 22:26:18 -070085import com.android.internal.R;
Selim Cinek389edcd2017-05-11 19:16:44 -070086import com.android.internal.annotations.VisibleForTesting;
Svet Ganovddb94882016-06-23 19:55:24 -070087import com.android.internal.util.ArrayUtils;
Lucas Dupina291d192018-06-07 13:59:42 -070088import com.android.internal.util.ContrastColorUtil;
Griff Hazen959591e2014-05-15 22:26:18 -070089
Tor Norbyed9273d62013-05-30 15:59:53 -070090import java.lang.annotation.Retention;
91import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020092import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050093import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070094import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070095import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070096import java.util.List;
Julia Reynolds7217dc92018-03-07 12:12:09 -050097import java.util.Objects;
Dan Sandler50128532015-12-08 15:42:41 -050098import java.util.Set;
Jeff Sharkey23b31182018-04-18 21:32:12 -060099import java.util.function.Consumer;
Joe Onorato561d3852010-11-20 18:09:34 -0800100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101/**
102 * A class that represents how a persistent notification is to be presented to
103 * the user using the {@link android.app.NotificationManager}.
104 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800105 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
106 * easier to construct Notifications.</p>
107 *
Joe Fernandez558459f2011-10-13 16:47:36 -0700108 * <div class="special reference">
109 * <h3>Developer Guides</h3>
110 * <p>For a guide to creating notifications, read the
111 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
112 * developer guide.</p>
113 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 */
115public class Notification implements Parcelable
116{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -0400117 private static final String TAG = "Notification";
118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -0400120 * An activity that provides a user interface for adjusting notification preferences for its
Julia Reynolds3aedded2017-03-31 14:42:09 -0400121 * containing application.
Daniel Sandler01df1c62014-06-09 10:54:01 -0400122 */
123 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
124 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
125 = "android.intent.category.NOTIFICATION_PREFERENCES";
126
127 /**
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500128 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
129 * contain a {@link NotificationChannel#getId() channel id} that can be used to narrow down
Julia Reynolds3aedded2017-03-31 14:42:09 -0400130 * what settings should be shown in the target app.
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500131 */
132 public static final String EXTRA_CHANNEL_ID = "android.intent.extra.CHANNEL_ID";
133
134 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -0400135 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds005c8b92017-08-24 10:35:53 -0400136 * contain a {@link NotificationChannelGroup#getId() group id} that can be used to narrow down
137 * what settings should be shown in the target app.
138 */
139 public static final String EXTRA_CHANNEL_GROUP_ID = "android.intent.extra.CHANNEL_GROUP_ID";
140
141 /**
142 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds3aedded2017-03-31 14:42:09 -0400143 * contain the tag provided to {@link NotificationManager#notify(String, int, Notification)}
144 * that can be used to narrow down what settings should be shown in the target app.
145 */
146 public static final String EXTRA_NOTIFICATION_TAG = "android.intent.extra.NOTIFICATION_TAG";
147
148 /**
149 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
150 * contain the id provided to {@link NotificationManager#notify(String, int, Notification)}
151 * that can be used to narrow down what settings should be shown in the target app.
152 */
153 public static final String EXTRA_NOTIFICATION_ID = "android.intent.extra.NOTIFICATION_ID";
154
155 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 * Use all default values (where applicable).
157 */
158 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 /**
161 * Use the default notification sound. This will ignore any given
162 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500163 *
Chris Wren47c20a12014-06-18 17:27:29 -0400164 * <p>
165 * A notification that is noisy is more likely to be presented as a heads-up notification.
166 * </p>
167 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500169 */
170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 public static final int DEFAULT_SOUND = 1;
172
173 /**
174 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500175 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700176 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500177 *
Chris Wren47c20a12014-06-18 17:27:29 -0400178 * <p>
179 * A notification that vibrates is more likely to be presented as a heads-up notification.
180 * </p>
181 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500183 */
184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 /**
188 * Use the default notification lights. This will ignore the
189 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
190 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500191 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500193 */
194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200198 * Maximum length of CharSequences accepted by Builder and friends.
199 *
200 * <p>
201 * Avoids spamming the system with overly large strings such as full e-mails.
202 */
203 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
204
205 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800206 * Maximum entries of reply text that are accepted by Builder and friends.
207 */
208 private static final int MAX_REPLY_HISTORY = 5;
209
Tony Mak09db2ea2018-06-27 18:12:48 +0100210 /**
Gustav Sennton1463d832018-11-06 16:12:48 +0000211 * Maximum number of (generic) action buttons in a notification (contextual action buttons are
212 * handled separately).
Tony Mak09db2ea2018-06-27 18:12:48 +0100213 * @hide
214 */
215 public static final int MAX_ACTION_BUTTONS = 3;
Selim Cinekde4de0e2018-01-24 16:21:07 -0800216
217 /**
218 * If the notification contained an unsent draft for a RemoteInput when the user clicked on it,
219 * we're adding the draft as a String extra to the {@link #contentIntent} using this key.
220 *
221 * <p>Apps may use this extra to prepopulate text fields in the app, where the user usually
222 * sends messages.</p>
223 */
224 public static final String EXTRA_REMOTE_INPUT_DRAFT = "android.remoteInputDraft";
225
Adrian Roose458aa82015-12-08 16:17:19 -0800226 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500227 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800228 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500229 * Default value: {@link System#currentTimeMillis() Now}.
230 *
231 * Choose a timestamp that will be most relevant to the user. For most finite events, this
232 * corresponds to the time the event happened (or will happen, in the case of events that have
233 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800234 * timestamped according to when the activity began.
235 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500236 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800237 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500238 * <ul>
239 * <li>Notification of a new chat message should be stamped when the message was received.</li>
240 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
241 * <li>Notification of a completed file download should be stamped when the download finished.</li>
242 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
243 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
244 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800245 * </ul>
246 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700247 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
248 * anymore by default and must be opted into by using
249 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 */
251 public long when;
252
253 /**
Selim Cinekb85f36fd2016-04-20 18:46:36 -0700254 * The creation time of the notification
255 */
256 private long creationTime;
257
258 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400260 *
261 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 */
Dan Sandler86647982015-05-13 23:41:13 -0400263 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700264 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 public int icon;
266
267 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800268 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
269 * leave it at its default value of 0.
270 *
271 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700272 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800273 */
274 public int iconLevel;
275
276 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500277 * The number of events that this notification represents. For example, in a new mail
278 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800279 *
Julia Reynolds30331982017-04-27 10:12:50 -0400280 * The system may or may not use this field to modify the appearance of the notification.
Julia Reynolds13d898c2017-02-02 12:22:05 -0500281 * Starting with {@link android.os.Build.VERSION_CODES#O}, the number may be displayed as a
282 * badge icon in Launchers that support badging.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 */
Julia Reynolds30331982017-04-27 10:12:50 -0400284 public int number = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285
286 /**
287 * The intent to execute when the expanded status entry is clicked. If
288 * this is an activity, it must include the
289 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800290 * that you take care of task management as described in the
291 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800292 * Stack</a> document. In particular, make sure to read the notification section
293 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
294 * Notifications</a> for the correct ways to launch an application from a
295 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 */
297 public PendingIntent contentIntent;
298
299 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500300 * The intent to execute when the notification is explicitly dismissed by the user, either with
301 * the "Clear All" button or by swiping it away individually.
302 *
303 * This probably shouldn't be launching an activity since several of those will be sent
304 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 */
306 public PendingIntent deleteIntent;
307
308 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700309 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800310 *
Chris Wren47c20a12014-06-18 17:27:29 -0400311 * <p>
312 * The system UI may choose to display a heads-up notification, instead of
313 * launching this intent, while the user is using the device.
314 * </p>
315 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800316 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400317 */
318 public PendingIntent fullScreenIntent;
319
320 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400321 * Text that summarizes this notification for accessibility services.
322 *
323 * As of the L release, this text is no longer shown on screen, but it is still useful to
324 * accessibility services (where it serves as an audible announcement of the notification's
325 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400326 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800327 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 */
329 public CharSequence tickerText;
330
331 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400332 * Formerly, a view showing the {@link #tickerText}.
333 *
334 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400335 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400336 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800337 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400338
339 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400340 * The view that will represent this notification in the notification list (which is pulled
341 * down from the status bar).
342 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500343 * As of N, this field may be null. The notification view is determined by the inputs
344 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400345 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400347 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 public RemoteViews contentView;
349
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400350 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400351 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400352 * opportunity to show more detail. The system UI may choose to show this
353 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400354 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500355 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400356 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
357 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400358 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400359 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400360 public RemoteViews bigContentView;
361
Chris Wren8fd39ec2014-02-27 17:43:26 -0500362
363 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400364 * A medium-format version of {@link #contentView}, providing the Notification an
365 * opportunity to add action buttons to contentView. At its discretion, the system UI may
366 * choose to show this as a heads-up notification, which will pop up so the user can see
367 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400368 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500369 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400370 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
371 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500372 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400373 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500374 public RemoteViews headsUpContentView;
375
Julia Reynoldsfc640012018-02-21 12:25:27 -0500376 private boolean mUsesStandardHeader;
377
378 private static final ArraySet<Integer> STANDARD_LAYOUTS = new ArraySet<>();
379 static {
380 STANDARD_LAYOUTS.add(R.layout.notification_template_material_base);
381 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_base);
382 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_picture);
383 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_text);
384 STANDARD_LAYOUTS.add(R.layout.notification_template_material_inbox);
385 STANDARD_LAYOUTS.add(R.layout.notification_template_material_messaging);
386 STANDARD_LAYOUTS.add(R.layout.notification_template_material_media);
387 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_media);
Julia Reynoldsfc640012018-02-21 12:25:27 -0500388 STANDARD_LAYOUTS.add(R.layout.notification_template_header);
Julia Reynoldsfc640012018-02-21 12:25:27 -0500389 }
390
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400391 /**
Dan Sandler86647982015-05-13 23:41:13 -0400392 * A large bitmap to be shown in the notification content area.
393 *
394 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 */
Dan Sandler86647982015-05-13 23:41:13 -0400396 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800397 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398
399 /**
400 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500401 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400403 * A notification that is noisy is more likely to be presented as a heads-up notification.
404 * </p>
405 *
406 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500407 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500409 * @deprecated use {@link NotificationChannel#getSound()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500411 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 public Uri sound;
413
414 /**
415 * Use this constant as the value for audioStreamType to request that
416 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700417 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400418 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500419 * @deprecated Use {@link NotificationChannel#getAudioAttributes()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700421 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 public static final int STREAM_DEFAULT = -1;
423
424 /**
425 * The audio stream type to use when playing the sound.
426 * Should be one of the STREAM_ constants from
427 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400428 *
429 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700431 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 public int audioStreamType = STREAM_DEFAULT;
433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400435 * The default value of {@link #audioAttributes}.
436 */
437 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
438 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
439 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
440 .build();
441
442 /**
443 * The {@link AudioAttributes audio attributes} to use when playing the sound.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500444 *
445 * @deprecated use {@link NotificationChannel#getAudioAttributes()} instead.
John Spurlockc0650f022014-07-19 13:22:39 -0400446 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500447 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -0400448 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
449
450 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500451 * The pattern with which to vibrate.
452 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 * <p>
454 * To vibrate the default pattern, see {@link #defaults}.
455 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500456 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 * @see android.os.Vibrator#vibrate(long[],int)
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500458 * @deprecated use {@link NotificationChannel#getVibrationPattern()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500460 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 public long[] vibrate;
462
463 /**
464 * The color of the led. The hardware will do its best approximation.
465 *
466 * @see #FLAG_SHOW_LIGHTS
467 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500468 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 */
Tor Norbye80756e32015-03-02 09:39:27 -0800470 @ColorInt
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500471 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 public int ledARGB;
473
474 /**
475 * The number of milliseconds for the LED to be on while it's flashing.
476 * The hardware will do its best approximation.
477 *
478 * @see #FLAG_SHOW_LIGHTS
479 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500480 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500482 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 public int ledOnMS;
484
485 /**
486 * The number of milliseconds for the LED to be off while it's flashing.
487 * The hardware will do its best approximation.
488 *
489 * @see #FLAG_SHOW_LIGHTS
490 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500491 *
492 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500494 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 public int ledOffMS;
496
497 /**
498 * Specifies which values should be taken from the defaults.
499 * <p>
500 * To set, OR the desired from {@link #DEFAULT_SOUND},
501 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
502 * values, use {@link #DEFAULT_ALL}.
503 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500504 *
505 * @deprecated use {@link NotificationChannel#getSound()} and
506 * {@link NotificationChannel#shouldShowLights()} and
507 * {@link NotificationChannel#shouldVibrate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500509 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 public int defaults;
511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 /**
513 * Bit to be bitwise-ored into the {@link #flags} field that should be
514 * set if you want the LED on for this notification.
515 * <ul>
516 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
517 * or 0 for both ledOnMS and ledOffMS.</li>
518 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
519 * <li>To flash the LED, pass the number of milliseconds that it should
520 * be on and off to ledOnMS and ledOffMS.</li>
521 * </ul>
522 * <p>
523 * Since hardware varies, you are not guaranteed that any of the values
Ricardo Loo Forondaf8f6d0a2018-01-25 08:42:43 -0800524 * you pass are honored exactly. Use the system defaults if possible
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 * because they will be set to values that work on any given hardware.
526 * <p>
527 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500528 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500529 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500531 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
533
534 /**
535 * Bit to be bitwise-ored into the {@link #flags} field that should be
536 * set if this notification is in reference to something that is ongoing,
537 * like a phone call. It should not be set if this notification is in
538 * reference to something that happened at a particular point in time,
539 * like a missed phone call.
540 */
541 public static final int FLAG_ONGOING_EVENT = 0x00000002;
542
543 /**
544 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700545 * the audio will be repeated until the notification is
546 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 */
548 public static final int FLAG_INSISTENT = 0x00000004;
549
550 /**
551 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700552 * set if you would only like the sound, vibrate and ticker to be played
553 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 */
555 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
556
557 /**
558 * Bit to be bitwise-ored into the {@link #flags} field that should be
559 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500560 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 */
562 public static final int FLAG_AUTO_CANCEL = 0x00000010;
563
564 /**
565 * Bit to be bitwise-ored into the {@link #flags} field that should be
566 * set if the notification should not be canceled when the user clicks
567 * the Clear all button.
568 */
569 public static final int FLAG_NO_CLEAR = 0x00000020;
570
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700571 /**
572 * Bit to be bitwise-ored into the {@link #flags} field that should be
573 * set if this notification represents a currently running service. This
574 * will normally be set for you by {@link Service#startForeground}.
575 */
576 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
577
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400578 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500579 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800580 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500581 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400582 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700583 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500584 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400585
Griff Hazendfcb0802014-02-11 12:00:00 -0800586 /**
587 * Bit to be bitswise-ored into the {@link #flags} field that should be
588 * set if this notification is relevant to the current device only
589 * and it is not recommended that it bridge to other devices.
590 */
591 public static final int FLAG_LOCAL_ONLY = 0x00000100;
592
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700593 /**
594 * Bit to be bitswise-ored into the {@link #flags} field that should be
595 * set if this notification is the group summary for a group of notifications.
596 * Grouped notifications may display in a cluster or stack on devices which
597 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
598 */
599 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
600
Julia Reynoldse46bb372016-03-17 11:05:58 -0400601 /**
602 * Bit to be bitswise-ored into the {@link #flags} field that should be
603 * set if this notification is the group summary for an auto-group of notifications.
604 *
605 * @hide
606 */
607 @SystemApi
608 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
609
Julia Reynolds4db59552017-06-30 13:34:01 -0400610 /**
611 * @hide
612 */
613 public static final int FLAG_CAN_COLORIZE = 0x00000800;
614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 public int flags;
616
Tor Norbyed9273d62013-05-30 15:59:53 -0700617 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700618 @IntDef(prefix = { "PRIORITY_" }, value = {
619 PRIORITY_DEFAULT,
620 PRIORITY_LOW,
621 PRIORITY_MIN,
622 PRIORITY_HIGH,
623 PRIORITY_MAX
624 })
Tor Norbyed9273d62013-05-30 15:59:53 -0700625 @Retention(RetentionPolicy.SOURCE)
626 public @interface Priority {}
627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500629 * Default notification {@link #priority}. If your application does not prioritize its own
630 * notifications, use this value for all notifications.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500631 *
632 * @deprecated use {@link NotificationManager#IMPORTANCE_DEFAULT} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500633 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500634 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500635 public static final int PRIORITY_DEFAULT = 0;
636
637 /**
638 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
639 * items smaller, or at a different position in the list, compared with your app's
640 * {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500641 *
642 * @deprecated use {@link NotificationManager#IMPORTANCE_LOW} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500643 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500644 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500645 public static final int PRIORITY_LOW = -1;
646
647 /**
648 * Lowest {@link #priority}; these items might not be shown to the user except under special
649 * circumstances, such as detailed notification logs.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500650 *
651 * @deprecated use {@link NotificationManager#IMPORTANCE_MIN} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500652 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500653 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500654 public static final int PRIORITY_MIN = -2;
655
656 /**
657 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
658 * show these items larger, or at a different position in notification lists, compared with
659 * your app's {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500660 *
661 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500662 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500663 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500664 public static final int PRIORITY_HIGH = 1;
665
666 /**
667 * Highest {@link #priority}, for your application's most important items that require the
668 * user's prompt attention or input.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500669 *
670 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500671 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500672 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500673 public static final int PRIORITY_MAX = 2;
674
675 /**
676 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800677 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500678 * Priority is an indication of how much of the user's valuable attention should be consumed by
679 * this notification. Low-priority notifications may be hidden from the user in certain
680 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500681 * system will make a determination about how to interpret this priority when presenting
682 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400683 *
684 * <p>
685 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
686 * as a heads-up notification.
687 * </p>
688 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500689 * @deprecated use {@link NotificationChannel#getImportance()} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500690 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700691 @Priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500692 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500693 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800694
Dan Sandler26e81cf2014-05-06 10:01:27 -0400695 /**
696 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
697 * to be applied by the standard Style templates when presenting this notification.
698 *
699 * The current template design constructs a colorful header image by overlaying the
700 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
701 * ignored.
702 */
Tor Norbye80756e32015-03-02 09:39:27 -0800703 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400704 public int color = COLOR_DEFAULT;
705
706 /**
707 * Special value of {@link #color} telling the system not to decorate this notification with
708 * any special color but instead use default colors when presenting this notification.
709 */
Tor Norbye80756e32015-03-02 09:39:27 -0800710 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400711 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600712
713 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800714 * Special value of {@link #color} used as a place holder for an invalid color.
Selim Cinek99104832017-01-25 14:47:33 -0800715 * @hide
Adrian Roos4ff3b122016-02-01 12:26:13 -0800716 */
717 @ColorInt
Selim Cinek99104832017-01-25 14:47:33 -0800718 public static final int COLOR_INVALID = 1;
Adrian Roos4ff3b122016-02-01 12:26:13 -0800719
720 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700721 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
722 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600723 * lockscreen).
724 *
725 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
726 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
727 * shown in all situations, but the contents are only available if the device is unlocked for
728 * the appropriate user.
729 *
730 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
731 * can be read even in an "insecure" context (that is, above a secure lockscreen).
732 * To modify the public version of this notification—for example, to redact some portions—see
733 * {@link Builder#setPublicVersion(Notification)}.
734 *
735 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
736 * and ticker until the user has bypassed the lockscreen.
737 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600738 public @Visibility int visibility;
739
740 /** @hide */
741 @IntDef(prefix = { "VISIBILITY_" }, value = {
742 VISIBILITY_PUBLIC,
743 VISIBILITY_PRIVATE,
744 VISIBILITY_SECRET,
745 })
746 @Retention(RetentionPolicy.SOURCE)
747 public @interface Visibility {}
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600748
Griff Hazenfc3922d2014-08-20 11:56:44 -0700749 /**
750 * Notification visibility: Show this notification in its entirety on all lockscreens.
751 *
752 * {@see #visibility}
753 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600754 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700755
756 /**
757 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
758 * private information on secure lockscreens.
759 *
760 * {@see #visibility}
761 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600762 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700763
764 /**
765 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
766 *
767 * {@see #visibility}
768 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600769 public static final int VISIBILITY_SECRET = -1;
770
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500771 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400772 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500773 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400774 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500775
776 /**
Adora Zhangaa90e872018-03-12 14:07:50 -0700777 * Notification category: map turn-by-turn navigation.
778 */
779 public static final String CATEGORY_NAVIGATION = "navigation";
780
781 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400782 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500783 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400784 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500785
786 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400787 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500788 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400789 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500790
791 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400792 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500793 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400794 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500795
796 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400797 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500798 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400799 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500800
801 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400802 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500803 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400804 public static final String CATEGORY_ALARM = "alarm";
805
806 /**
807 * Notification category: progress of a long-running background operation.
808 */
809 public static final String CATEGORY_PROGRESS = "progress";
810
811 /**
812 * Notification category: social network or sharing update.
813 */
814 public static final String CATEGORY_SOCIAL = "social";
815
816 /**
817 * Notification category: error in background operation or authentication status.
818 */
819 public static final String CATEGORY_ERROR = "err";
820
821 /**
822 * Notification category: media transport control for playback.
823 */
824 public static final String CATEGORY_TRANSPORT = "transport";
825
826 /**
827 * Notification category: system or device status update. Reserved for system use.
828 */
829 public static final String CATEGORY_SYSTEM = "sys";
830
831 /**
832 * Notification category: indication of running background service.
833 */
834 public static final String CATEGORY_SERVICE = "service";
835
836 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400837 * Notification category: a specific, timely recommendation for a single thing.
838 * For example, a news app might want to recommend a news story it believes the user will
839 * want to read next.
840 */
841 public static final String CATEGORY_RECOMMENDATION = "recommendation";
842
843 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400844 * Notification category: ongoing information about device or contextual status.
845 */
846 public static final String CATEGORY_STATUS = "status";
847
848 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400849 * Notification category: user-scheduled reminder.
850 */
851 public static final String CATEGORY_REMINDER = "reminder";
852
853 /**
Adora Zhangaa90e872018-03-12 14:07:50 -0700854 * Notification category: extreme car emergencies.
855 * @hide
856 */
857 @SystemApi
858 public static final String CATEGORY_CAR_EMERGENCY = "car_emergency";
859
860 /**
861 * Notification category: car warnings.
862 * @hide
863 */
864 @SystemApi
865 public static final String CATEGORY_CAR_WARNING = "car_warning";
866
867 /**
868 * Notification category: general car system information.
869 * @hide
870 */
871 @SystemApi
872 public static final String CATEGORY_CAR_INFORMATION = "car_information";
873
874 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400875 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
876 * that best describes this Notification. May be used by the system for ranking and filtering.
877 */
878 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500879
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100880 @UnsupportedAppUsage
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700881 private String mGroupKey;
882
883 /**
884 * Get the key used to group this notification into a cluster or stack
885 * with other notifications on devices which support such rendering.
886 */
887 public String getGroup() {
888 return mGroupKey;
889 }
890
891 private String mSortKey;
892
893 /**
894 * Get a sort key that orders this notification among other notifications from the
895 * same package. This can be useful if an external sort was already applied and an app
896 * would like to preserve this. Notifications will be sorted lexicographically using this
897 * value, although providing different priorities in addition to providing sort key may
898 * cause this value to be ignored.
899 *
900 * <p>This sort key can also be used to order members of a notification group. See
901 * {@link Builder#setGroup}.
902 *
903 * @see String#compareTo(String)
904 */
905 public String getSortKey() {
906 return mSortKey;
907 }
908
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500909 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400910 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400911 * <p>
912 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
913 * APIs, and are intended to be used by
914 * {@link android.service.notification.NotificationListenerService} implementations to extract
915 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500916 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400917 public Bundle extras = new Bundle();
918
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400919 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700920 * All pending intents in the notification as the system needs to be able to access them but
921 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700922 * custom parcelable objects.
923 *
924 * @hide
925 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100926 @UnsupportedAppUsage
Felipe Lemedd85da62016-06-28 11:29:54 -0700927 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700928
929 /**
Dianne Hackborn98305522017-05-05 17:53:53 -0700930 * Token identifying the notification that is applying doze/bgcheck whitelisting to the
931 * pending intents inside of it, so only those will get the behavior.
932 *
933 * @hide
934 */
Adrian Roosfb921842017-10-26 14:49:56 +0200935 private IBinder mWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -0700936
937 /**
938 * Must be set by a process to start associating tokens with Notification objects
939 * coming in to it. This is set by NotificationManagerService.
940 *
941 * @hide
942 */
943 static public IBinder processWhitelistToken;
944
945 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400946 * {@link #extras} key: this is the title of the notification,
947 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
948 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500949 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400950
951 /**
952 * {@link #extras} key: this is the title of the notification when shown in expanded form,
953 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
954 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400955 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400956
957 /**
958 * {@link #extras} key: this is the main text payload, as supplied to
959 * {@link Builder#setContentText(CharSequence)}.
960 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500961 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400962
963 /**
964 * {@link #extras} key: this is a third line of text, as supplied to
965 * {@link Builder#setSubText(CharSequence)}.
966 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400967 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400968
969 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800970 * {@link #extras} key: this is the remote input history, as supplied to
971 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700972 *
973 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
974 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
975 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
976 * notifications once the other party has responded).
977 *
978 * The extra with this key is of type CharSequence[] and contains the most recent entry at
979 * the 0 index, the second most recent at the 1 index, etc.
980 *
981 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800982 */
983 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
984
985 /**
Kenny Guya0f6de82018-04-06 16:20:16 +0100986 * {@link #extras} key: boolean as supplied to
987 * {@link Builder#setShowRemoteInputSpinner(boolean)}.
988 *
989 * If set to true, then the view displaying the remote input history from
990 * {@link Builder#setRemoteInputHistory(CharSequence[])} will have a progress spinner.
991 *
992 * @see Builder#setShowRemoteInputSpinner(boolean)
993 * @hide
994 */
995 public static final String EXTRA_SHOW_REMOTE_INPUT_SPINNER = "android.remoteInputSpinner";
996
997 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +0100998 * {@link #extras} key: boolean as supplied to
999 * {@link Builder#setHideSmartReplies(boolean)}.
1000 *
1001 * If set to true, then any smart reply buttons will be hidden.
1002 *
1003 * @see Builder#setHideSmartReplies(boolean)
1004 * @hide
1005 */
1006 public static final String EXTRA_HIDE_SMART_REPLIES = "android.hideSmartReplies";
1007
1008 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001009 * {@link #extras} key: this is a small piece of additional text as supplied to
1010 * {@link Builder#setContentInfo(CharSequence)}.
1011 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001012 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001013
1014 /**
1015 * {@link #extras} key: this is a line of summary information intended to be shown
1016 * alongside expanded notifications, as supplied to (e.g.)
1017 * {@link BigTextStyle#setSummaryText(CharSequence)}.
1018 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001019 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001020
1021 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02001022 * {@link #extras} key: this is the longer text shown in the big form of a
1023 * {@link BigTextStyle} notification, as supplied to
1024 * {@link BigTextStyle#bigText(CharSequence)}.
1025 */
1026 public static final String EXTRA_BIG_TEXT = "android.bigText";
1027
1028 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001029 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
1030 * supplied to {@link Builder#setSmallIcon(int)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -04001031 *
1032 * @deprecated Use {@link #getSmallIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001033 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04001034 @Deprecated
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001035 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001036
1037 /**
1038 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
1039 * notification payload, as
1040 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -04001041 *
1042 * @deprecated Use {@link #getLargeIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001043 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04001044 @Deprecated
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001045 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001046
1047 /**
1048 * {@link #extras} key: this is a bitmap to be used instead of the one from
1049 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
1050 * shown in its expanded form, as supplied to
1051 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
1052 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001053 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001054
1055 /**
1056 * {@link #extras} key: this is the progress value supplied to
1057 * {@link Builder#setProgress(int, int, boolean)}.
1058 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001059 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001060
1061 /**
1062 * {@link #extras} key: this is the maximum value supplied to
1063 * {@link Builder#setProgress(int, int, boolean)}.
1064 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001065 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001066
1067 /**
1068 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
1069 * {@link Builder#setProgress(int, int, boolean)}.
1070 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001071 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001072
1073 /**
1074 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
1075 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
1076 * {@link Builder#setUsesChronometer(boolean)}.
1077 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001078 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001079
1080 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08001081 * {@link #extras} key: whether the chronometer set on the notification should count down
1082 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -07001083 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -08001084 */
Adrian Roos96b7e202016-05-17 13:50:38 -07001085 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -08001086
1087 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001088 * {@link #extras} key: whether {@link #when} should be shown,
1089 * as supplied to {@link Builder#setShowWhen(boolean)}.
1090 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001091 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001092
1093 /**
1094 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
1095 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
1096 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001097 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001098
1099 /**
1100 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
1101 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
1102 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001103 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -04001104
1105 /**
1106 * {@link #extras} key: A string representing the name of the specific
1107 * {@link android.app.Notification.Style} used to create this notification.
1108 */
Chris Wren91ad5632013-06-05 15:05:57 -04001109 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001110
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001111 /**
Chris Wrene6c48932014-09-29 17:19:27 -04001112 * {@link #extras} key: A String array containing the people that this notification relates to,
1113 * each of which was supplied to {@link Builder#addPerson(String)}.
Selim Cineke7238dd2017-12-14 17:48:32 -08001114 *
1115 * @deprecated the actual objects are now in {@link #EXTRA_PEOPLE_LIST}
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001116 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001117 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001118
1119 /**
Selim Cineke7238dd2017-12-14 17:48:32 -08001120 * {@link #extras} key: An arrayList of {@link Person} objects containing the people that
1121 * this notification relates to.
1122 */
1123 public static final String EXTRA_PEOPLE_LIST = "android.people.list";
1124
1125 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04001126 * Allow certain system-generated notifications to appear before the device is provisioned.
1127 * Only available to notifications coming from the android package.
1128 * @hide
1129 */
Maurice Lam96c10032017-03-29 15:42:38 -07001130 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001131 @RequiresPermission(android.Manifest.permission.NOTIFICATION_DURING_SETUP)
John Spurlockfd7f1e02014-03-18 16:41:57 -04001132 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
1133
1134 /**
Robin Leed107af62018-04-27 13:55:56 +02001135 * {@link #extras} key:
1136 * flat {@link String} representation of a {@link android.content.ContentUris content URI}
1137 * pointing to an image that can be displayed in the background when the notification is
1138 * selected. Used on television platforms. The URI must point to an image stream suitable for
1139 * passing into {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001140 * BitmapFactory.decodeStream}; all other content types will be ignored.
Jose Limae9e3b3b2014-05-18 23:44:50 -07001141 */
1142 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
1143
1144 /**
Dan Sandler842dd772014-05-15 09:36:47 -04001145 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -07001146 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -04001147 * {@link android.app.Notification.MediaStyle} notification.
1148 */
1149 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
1150
1151 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +01001152 * {@link #extras} key: the indices of actions to be shown in the compact view,
1153 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
1154 */
1155 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
1156
Christoph Studer943aa672014-08-03 20:31:16 +02001157 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04001158 * {@link #extras} key: the username to be displayed for all messages sent by the user including
1159 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -07001160 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1161 * {@link CharSequence}
Selim Cinekcb8b9852017-12-15 18:01:52 -08001162 *
1163 * @deprecated use {@link #EXTRA_MESSAGING_PERSON}
Alex Hillsfc737de2016-03-23 17:33:02 -04001164 */
1165 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
1166
1167 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08001168 * {@link #extras} key: the person to be displayed for all messages sent by the user including
1169 * direct replies
1170 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1171 * {@link Person}
1172 */
1173 public static final String EXTRA_MESSAGING_PERSON = "android.messagingUser";
1174
1175 /**
Adrian Roos96b7e202016-05-17 13:50:38 -07001176 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -04001177 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -04001178 */
Alex Hillsd9b04d92016-04-11 16:38:16 -04001179 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -04001180
1181 /**
1182 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
1183 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -07001184 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1185 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -04001186 */
1187 public static final String EXTRA_MESSAGES = "android.messages";
1188
1189 /**
Adrian Roos437cd562017-01-18 15:47:03 -08001190 * {@link #extras} key: an array of
1191 * {@link android.app.Notification.MessagingStyle#addHistoricMessage historic}
1192 * {@link android.app.Notification.MessagingStyle.Message} bundles provided by a
1193 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1194 * array of bundles.
1195 */
1196 public static final String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
1197
1198 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08001199 * {@link #extras} key: whether the {@link android.app.Notification.MessagingStyle} notification
1200 * represents a group conversation.
1201 */
1202 public static final String EXTRA_IS_GROUP_CONVERSATION = "android.isGroupConversation";
1203
1204 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08001205 * {@link #extras} key: whether the notification should be colorized as
1206 * supplied to {@link Builder#setColorized(boolean)}}.
1207 */
1208 public static final String EXTRA_COLORIZED = "android.colorized";
1209
1210 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001211 * @hide
1212 */
1213 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
1214
Selim Cinek247fa012016-02-18 09:50:48 -08001215 /**
1216 * @hide
1217 */
1218 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
1219
Shane Brennan472a3b32016-12-12 15:28:10 -08001220 /**
Selim Cinekd0426622017-07-11 13:19:59 +02001221 * @hide
1222 */
1223 public static final String EXTRA_REDUCED_IMAGES = "android.reduced.images";
1224
1225 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001226 * {@link #extras} key: the audio contents of this notification.
1227 *
1228 * This is for use when rendering the notification on an audio-focused interface;
1229 * the audio contents are a complete sound sample that contains the contents/body of the
1230 * notification. This may be used in substitute of a Text-to-Speech reading of the
1231 * notification. For example if the notification represents a voice message this should point
1232 * to the audio of that message.
1233 *
1234 * The data stored under this key should be a String representation of a Uri that contains the
1235 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1236 *
1237 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1238 * has a field for holding data URI. That field can be used for audio.
1239 * See {@code Message#setData}.
1240 *
1241 * Example usage:
1242 * <pre>
1243 * {@code
1244 * Notification.Builder myBuilder = (build your Notification as normal);
1245 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1246 * }
1247 * </pre>
1248 */
1249 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1250
Dan Sandler80eaa592016-04-14 23:34:54 -04001251 /** @hide */
1252 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001253 @RequiresPermission(android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME)
Dan Sandler732bd6c2016-04-12 14:20:32 -04001254 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1255
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001256 /**
Julia Reynolds3c7de112018-03-28 09:33:13 -04001257 * This is set on the notifications shown by system_server about apps running foreground
1258 * services. It indicates that the notification should be shown
1259 * only if any of the given apps do not already have a properly tagged
1260 * {@link #FLAG_FOREGROUND_SERVICE} notification currently visible to the user.
1261 * This is a string array of all package names of the apps.
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001262 * @hide
1263 */
1264 public static final String EXTRA_FOREGROUND_APPS = "android.foregroundApps";
1265
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001266 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04001267 private Icon mSmallIcon;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001268 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04001269 private Icon mLargeIcon;
1270
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001271 @UnsupportedAppUsage
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001272 private String mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001273 private long mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001274
Julia Reynolds13d898c2017-02-02 12:22:05 -05001275 private String mShortcutId;
Felipe Leme90205ef2019-03-05 09:59:52 -08001276 private LocusId mLocusId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04001277 private CharSequence mSettingsText;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001278
Mady Mellorc39b4ae2019-01-09 17:11:37 -08001279 private BubbleMetadata mBubbleMetadata;
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04001280
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001281 /** @hide */
1282 @IntDef(prefix = { "GROUP_ALERT_" }, value = {
1283 GROUP_ALERT_ALL, GROUP_ALERT_CHILDREN, GROUP_ALERT_SUMMARY
1284 })
1285 @Retention(RetentionPolicy.SOURCE)
1286 public @interface GroupAlertBehavior {}
1287
1288 /**
1289 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all notifications in a
1290 * group with sound or vibration ought to make sound or vibrate (respectively), so this
1291 * notification will not be muted when it is in a group.
1292 */
1293 public static final int GROUP_ALERT_ALL = 0;
1294
1295 /**
1296 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all children
1297 * notification in a group should be silenced (no sound or vibration) even if they are posted
1298 * to a {@link NotificationChannel} that has sound and/or vibration. Use this constant to
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001299 * mute this notification if this notification is a group child. This must be applied to all
1300 * children notifications you want to mute.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001301 *
1302 * <p> For example, you might want to use this constant if you post a number of children
1303 * notifications at once (say, after a periodic sync), and only need to notify the user
1304 * audibly once.
1305 */
1306 public static final int GROUP_ALERT_SUMMARY = 1;
1307
1308 /**
1309 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that the summary
1310 * notification in a group should be silenced (no sound or vibration) even if they are
1311 * posted to a {@link NotificationChannel} that has sound and/or vibration. Use this constant
1312 * to mute this notification if this notification is a group summary.
1313 *
1314 * <p>For example, you might want to use this constant if only the children notifications
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001315 * in your group have content and the summary is only used to visually group notifications
1316 * rather than to alert the user that new information is available.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001317 */
1318 public static final int GROUP_ALERT_CHILDREN = 2;
1319
Julia Reynolds2f431e22017-06-07 14:12:09 +00001320 private int mGroupAlertBehavior = GROUP_ALERT_ALL;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001321
Julia Reynolds13d898c2017-02-02 12:22:05 -05001322 /**
1323 * If this notification is being shown as a badge, always show as a number.
1324 */
1325 public static final int BADGE_ICON_NONE = 0;
1326
1327 /**
1328 * If this notification is being shown as a badge, use the {@link #getSmallIcon()} to
1329 * represent this notification.
1330 */
1331 public static final int BADGE_ICON_SMALL = 1;
1332
1333 /**
1334 * If this notification is being shown as a badge, use the {@link #getLargeIcon()} to
1335 * represent this notification.
1336 */
1337 public static final int BADGE_ICON_LARGE = 2;
Julia Reynolds7d5b4da2017-03-20 10:18:49 -04001338 private int mBadgeIcon = BADGE_ICON_NONE;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001339
Chris Wren51c75102013-07-16 20:49:17 -04001340 /**
Gustav Sennton761884c2018-11-19 17:40:19 +00001341 * Determines whether the platform can generate contextual actions for a notification.
1342 */
1343 private boolean mAllowSystemGeneratedContextualActions = true;
1344
1345 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001346 * Structure to encapsulate a named action that can be shown as part of this notification.
1347 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1348 * selected by the user.
1349 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001350 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1351 * or {@link Notification.Builder#addAction(Notification.Action)}
1352 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001353 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001354 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001355 /**
1356 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1357 * {@link RemoteInput}s.
1358 *
1359 * This is intended for {@link RemoteInput}s that only accept data, meaning
1360 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
1361 * is null or empty, and {@link RemoteInput#getAllowedDataTypes is non-null and not
1362 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1363 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1364 *
1365 * You can test if a RemoteInput matches these constraints using
1366 * {@link RemoteInput#isDataOnly}.
1367 */
1368 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1369
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001370 /**
1371 * {@link }: No semantic action defined.
1372 */
1373 public static final int SEMANTIC_ACTION_NONE = 0;
1374
1375 /**
1376 * {@code SemanticAction}: Reply to a conversation, chat, group, or wherever replies
1377 * may be appropriate.
1378 */
1379 public static final int SEMANTIC_ACTION_REPLY = 1;
1380
1381 /**
1382 * {@code SemanticAction}: Mark content as read.
1383 */
1384 public static final int SEMANTIC_ACTION_MARK_AS_READ = 2;
1385
1386 /**
1387 * {@code SemanticAction}: Mark content as unread.
1388 */
1389 public static final int SEMANTIC_ACTION_MARK_AS_UNREAD = 3;
1390
1391 /**
1392 * {@code SemanticAction}: Delete the content associated with the notification. This
1393 * could mean deleting an email, message, etc.
1394 */
1395 public static final int SEMANTIC_ACTION_DELETE = 4;
1396
1397 /**
1398 * {@code SemanticAction}: Archive the content associated with the notification. This
1399 * could mean archiving an email, message, etc.
1400 */
1401 public static final int SEMANTIC_ACTION_ARCHIVE = 5;
1402
1403 /**
1404 * {@code SemanticAction}: Mute the content associated with the notification. This could
1405 * mean silencing a conversation or currently playing media.
1406 */
1407 public static final int SEMANTIC_ACTION_MUTE = 6;
1408
1409 /**
1410 * {@code SemanticAction}: Unmute the content associated with the notification. This could
1411 * mean un-silencing a conversation or currently playing media.
1412 */
1413 public static final int SEMANTIC_ACTION_UNMUTE = 7;
1414
1415 /**
1416 * {@code SemanticAction}: Mark content with a thumbs up.
1417 */
1418 public static final int SEMANTIC_ACTION_THUMBS_UP = 8;
1419
1420 /**
1421 * {@code SemanticAction}: Mark content with a thumbs down.
1422 */
1423 public static final int SEMANTIC_ACTION_THUMBS_DOWN = 9;
1424
Kodlee Yincda5b092018-02-15 15:34:53 -08001425 /**
1426 * {@code SemanticAction}: Call a contact, group, etc.
1427 */
1428 public static final int SEMANTIC_ACTION_CALL = 10;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001429
Griff Hazen959591e2014-05-15 22:26:18 -07001430 private final Bundle mExtras;
Mathew Inwood8c854f82018-09-14 12:35:36 +01001431 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Dan Sandler86647982015-05-13 23:41:13 -04001432 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001433 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001434 private boolean mAllowGeneratedReplies = true;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001435 private final @SemanticAction int mSemanticAction;
Gustav Sennton005d7a02019-01-04 13:41:32 +00001436 private final boolean mIsContextual;
Griff Hazen959591e2014-05-15 22:26:18 -07001437
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001438 /**
1439 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001440 *
1441 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001442 */
Dan Sandler86647982015-05-13 23:41:13 -04001443 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001444 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001445
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001446 /**
1447 * Title of the action.
1448 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001449 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001450
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001451 /**
1452 * Intent to send when the user invokes this action. May be null, in which case the action
1453 * may be rendered in a disabled presentation by the system UI.
1454 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001455 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001456
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001457 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001458 if (in.readInt() != 0) {
1459 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001460 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1461 icon = mIcon.getResId();
1462 }
Dan Sandler86647982015-05-13 23:41:13 -04001463 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001464 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1465 if (in.readInt() == 1) {
1466 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1467 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001468 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001469 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001470 mAllowGeneratedReplies = in.readInt() == 1;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001471 mSemanticAction = in.readInt();
Gustav Sennton005d7a02019-01-04 13:41:32 +00001472 mIsContextual = in.readInt() == 1;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001473 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001474
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001475 /**
Dan Sandler86647982015-05-13 23:41:13 -04001476 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001477 */
Dan Sandler86647982015-05-13 23:41:13 -04001478 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001479 public Action(int icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001480 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true,
Gustav Sennton005d7a02019-01-04 13:41:32 +00001481 SEMANTIC_ACTION_NONE, false /* isContextual */);
Griff Hazen959591e2014-05-15 22:26:18 -07001482 }
1483
Adrian Roos7af53622016-10-12 13:44:05 -07001484 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001485 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001486 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
Gustav Sennton005d7a02019-01-04 13:41:32 +00001487 @SemanticAction int semanticAction, boolean isContextual) {
Dan Sandler86647982015-05-13 23:41:13 -04001488 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001489 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1490 this.icon = icon.getResId();
1491 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001492 this.title = title;
1493 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001494 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001495 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001496 this.mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001497 this.mSemanticAction = semanticAction;
Gustav Sennton005d7a02019-01-04 13:41:32 +00001498 this.mIsContextual = isContextual;
Griff Hazen959591e2014-05-15 22:26:18 -07001499 }
1500
1501 /**
Dan Sandler86647982015-05-13 23:41:13 -04001502 * Return an icon representing the action.
1503 */
1504 public Icon getIcon() {
1505 if (mIcon == null && icon != 0) {
1506 // you snuck an icon in here without using the builder; let's try to keep it
1507 mIcon = Icon.createWithResource("", icon);
1508 }
1509 return mIcon;
1510 }
1511
1512 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001513 * Get additional metadata carried around with this Action.
1514 */
1515 public Bundle getExtras() {
1516 return mExtras;
1517 }
1518
1519 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001520 * Return whether the platform should automatically generate possible replies for this
1521 * {@link Action}
1522 */
1523 public boolean getAllowGeneratedReplies() {
1524 return mAllowGeneratedReplies;
1525 }
1526
1527 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001528 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001529 * May return null if no remote inputs were added. Only returns inputs which accept
1530 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001531 */
1532 public RemoteInput[] getRemoteInputs() {
1533 return mRemoteInputs;
1534 }
1535
1536 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001537 * Returns the {@code SemanticAction} associated with this {@link Action}. A
1538 * {@code SemanticAction} denotes what an {@link Action}'s {@link PendingIntent} will do
1539 * (eg. reply, mark as read, delete, etc).
1540 */
1541 public @SemanticAction int getSemanticAction() {
1542 return mSemanticAction;
1543 }
1544
1545 /**
Gustav Sennton005d7a02019-01-04 13:41:32 +00001546 * Returns whether this is a contextual Action, i.e. whether the action is dependent on the
1547 * notification message body. An example of a contextual action could be an action opening a
1548 * map application with an address shown in the notification.
1549 */
1550 public boolean isContextual() {
1551 return mIsContextual;
1552 }
1553
1554 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001555 * Get the list of inputs to be collected from the user that ONLY accept data when this
1556 * action is sent. These remote inputs are guaranteed to return true on a call to
1557 * {@link RemoteInput#isDataOnly}.
1558 *
Shane Brennanf670d6c2017-04-25 13:05:45 -07001559 * Returns null if there are no data-only remote inputs.
Shane Brennan472a3b32016-12-12 15:28:10 -08001560 *
1561 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1562 * of non-textual RemoteInputs do not access these remote inputs.
1563 */
1564 public RemoteInput[] getDataOnlyRemoteInputs() {
1565 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1566 }
1567
1568 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001569 * Builder class for {@link Action} objects.
1570 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001571 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001572 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001573 private final CharSequence mTitle;
1574 private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001575 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001576 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001577 private ArrayList<RemoteInput> mRemoteInputs;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001578 private @SemanticAction int mSemanticAction;
Gustav Sennton005d7a02019-01-04 13:41:32 +00001579 private boolean mIsContextual;
Griff Hazen959591e2014-05-15 22:26:18 -07001580
1581 /**
1582 * Construct a new builder for {@link Action} object.
1583 * @param icon icon to show for this action
1584 * @param title the title of the action
1585 * @param intent the {@link PendingIntent} to fire when users trigger this action
1586 */
Dan Sandler86647982015-05-13 23:41:13 -04001587 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001588 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001589 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001590 }
1591
1592 /**
1593 * Construct a new builder for {@link Action} object.
1594 * @param icon icon to show for this action
1595 * @param title the title of the action
1596 * @param intent the {@link PendingIntent} to fire when users trigger this action
1597 */
1598 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001599 this(icon, title, intent, new Bundle(), null, true, SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001600 }
1601
1602 /**
1603 * Construct a new builder for {@link Action} object using the fields from an
1604 * {@link Action}.
1605 * @param action the action to read fields from.
1606 */
1607 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001608 this(action.getIcon(), action.title, action.actionIntent,
1609 new Bundle(action.mExtras), action.getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001610 action.getAllowGeneratedReplies(), action.getSemanticAction());
Griff Hazen959591e2014-05-15 22:26:18 -07001611 }
1612
Dan Sandler86647982015-05-13 23:41:13 -04001613 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001614 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1615 @SemanticAction int semanticAction) {
Griff Hazen959591e2014-05-15 22:26:18 -07001616 mIcon = icon;
1617 mTitle = title;
1618 mIntent = intent;
1619 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001620 if (remoteInputs != null) {
1621 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1622 Collections.addAll(mRemoteInputs, remoteInputs);
1623 }
Adrian Roos7af53622016-10-12 13:44:05 -07001624 mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001625 mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001626 }
1627
1628 /**
1629 * Merge additional metadata into this builder.
1630 *
1631 * <p>Values within the Bundle will replace existing extras values in this Builder.
1632 *
1633 * @see Notification.Action#extras
1634 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001635 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07001636 public Builder addExtras(Bundle extras) {
1637 if (extras != null) {
1638 mExtras.putAll(extras);
1639 }
1640 return this;
1641 }
1642
1643 /**
1644 * Get the metadata Bundle used by this Builder.
1645 *
1646 * <p>The returned Bundle is shared with this Builder.
1647 */
1648 public Bundle getExtras() {
1649 return mExtras;
1650 }
1651
1652 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001653 * Add an input to be collected from the user when this action is sent.
1654 * Response values can be retrieved from the fired intent by using the
1655 * {@link RemoteInput#getResultsFromIntent} function.
1656 * @param remoteInput a {@link RemoteInput} to add to the action
1657 * @return this object for method chaining
1658 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001659 @NonNull
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001660 public Builder addRemoteInput(RemoteInput remoteInput) {
1661 if (mRemoteInputs == null) {
1662 mRemoteInputs = new ArrayList<RemoteInput>();
1663 }
1664 mRemoteInputs.add(remoteInput);
1665 return this;
1666 }
1667
1668 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001669 * Set whether the platform should automatically generate possible replies to add to
1670 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1671 * {@link RemoteInput}, this has no effect.
1672 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1673 * otherwise
1674 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001675 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001676 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001677 @NonNull
Alex Hills42b0c4d2016-04-26 13:35:36 -04001678 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1679 mAllowGeneratedReplies = allowGeneratedReplies;
1680 return this;
1681 }
1682
1683 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001684 * Sets the {@code SemanticAction} for this {@link Action}. A
1685 * {@code SemanticAction} denotes what an {@link Action}'s
1686 * {@link PendingIntent} will do (eg. reply, mark as read, delete, etc).
1687 * @param semanticAction a SemanticAction defined within {@link Action} with
1688 * {@code SEMANTIC_ACTION_} prefixes
1689 * @return this object for method chaining
1690 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001691 @NonNull
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001692 public Builder setSemanticAction(@SemanticAction int semanticAction) {
1693 mSemanticAction = semanticAction;
1694 return this;
1695 }
1696
1697 /**
Gustav Sennton005d7a02019-01-04 13:41:32 +00001698 * Sets whether this {@link Action} is a contextual action, i.e. whether the action is
1699 * dependent on the notification message body. An example of a contextual action could
1700 * be an action opening a map application with an address shown in the notification.
1701 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001702 @NonNull
Gustav Sennton005d7a02019-01-04 13:41:32 +00001703 public Builder setContextual(boolean isContextual) {
1704 mIsContextual = isContextual;
1705 return this;
1706 }
1707
1708 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001709 * Apply an extender to this action builder. Extenders may be used to add
1710 * metadata or change options on this builder.
1711 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001712 @NonNull
Griff Hazen61a9e862014-05-22 16:05:19 -07001713 public Builder extend(Extender extender) {
1714 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001715 return this;
1716 }
1717
1718 /**
Gustav Senntonc98b1632018-11-23 12:26:15 +00001719 * Throws an NPE if we are building a contextual action missing one of the fields
1720 * necessary to display the action.
1721 */
1722 private void checkContextualActionNullFields() {
Gustav Sennton005d7a02019-01-04 13:41:32 +00001723 if (!mIsContextual) return;
Gustav Senntonc98b1632018-11-23 12:26:15 +00001724
1725 if (mIcon == null) {
1726 throw new NullPointerException("Contextual Actions must contain a valid icon");
1727 }
1728
1729 if (mIntent == null) {
1730 throw new NullPointerException(
1731 "Contextual Actions must contain a valid PendingIntent");
1732 }
1733 }
1734
1735 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001736 * Combine all of the options that have been set and return a new {@link Action}
1737 * object.
1738 * @return the built action
1739 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001740 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07001741 public Action build() {
Gustav Senntonc98b1632018-11-23 12:26:15 +00001742 checkContextualActionNullFields();
1743
Shane Brennan472a3b32016-12-12 15:28:10 -08001744 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1745 RemoteInput[] previousDataInputs =
1746 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001747 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001748 for (RemoteInput input : previousDataInputs) {
1749 dataOnlyInputs.add(input);
1750 }
1751 }
1752 List<RemoteInput> textInputs = new ArrayList<>();
1753 if (mRemoteInputs != null) {
1754 for (RemoteInput input : mRemoteInputs) {
1755 if (input.isDataOnly()) {
1756 dataOnlyInputs.add(input);
1757 } else {
1758 textInputs.add(input);
1759 }
1760 }
1761 }
1762 if (!dataOnlyInputs.isEmpty()) {
1763 RemoteInput[] dataInputsArr =
1764 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1765 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1766 }
1767 RemoteInput[] textInputsArr = textInputs.isEmpty()
1768 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1769 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Gustav Sennton005d7a02019-01-04 13:41:32 +00001770 mAllowGeneratedReplies, mSemanticAction, mIsContextual);
Griff Hazen959591e2014-05-15 22:26:18 -07001771 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001772 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001773
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001774 @Override
1775 public Action clone() {
1776 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001777 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001778 title,
1779 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001780 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001781 getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001782 getAllowGeneratedReplies(),
Gustav Sennton005d7a02019-01-04 13:41:32 +00001783 getSemanticAction(),
1784 isContextual());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001785 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001786
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001787 @Override
1788 public int describeContents() {
1789 return 0;
1790 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001791
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001792 @Override
1793 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001794 final Icon ic = getIcon();
1795 if (ic != null) {
1796 out.writeInt(1);
1797 ic.writeToParcel(out, 0);
1798 } else {
1799 out.writeInt(0);
1800 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001801 TextUtils.writeToParcel(title, out, flags);
1802 if (actionIntent != null) {
1803 out.writeInt(1);
1804 actionIntent.writeToParcel(out, flags);
1805 } else {
1806 out.writeInt(0);
1807 }
Griff Hazen959591e2014-05-15 22:26:18 -07001808 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001809 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001810 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001811 out.writeInt(mSemanticAction);
Gustav Sennton005d7a02019-01-04 13:41:32 +00001812 out.writeInt(mIsContextual ? 1 : 0);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001813 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001814
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07001815 public static final @android.annotation.NonNull Parcelable.Creator<Action> CREATOR =
Griff Hazen959591e2014-05-15 22:26:18 -07001816 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001817 public Action createFromParcel(Parcel in) {
1818 return new Action(in);
1819 }
1820 public Action[] newArray(int size) {
1821 return new Action[size];
1822 }
1823 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001824
1825 /**
1826 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1827 * metadata or change options on an action builder.
1828 */
1829 public interface Extender {
1830 /**
1831 * Apply this extender to a notification action builder.
1832 * @param builder the builder to be modified.
1833 * @return the build object for chaining.
1834 */
1835 public Builder extend(Builder builder);
1836 }
1837
1838 /**
1839 * Wearable extender for notification actions. To add extensions to an action,
1840 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1841 * the {@code WearableExtender()} constructor and apply it to a
1842 * {@link android.app.Notification.Action.Builder} using
1843 * {@link android.app.Notification.Action.Builder#extend}.
1844 *
1845 * <pre class="prettyprint">
1846 * Notification.Action action = new Notification.Action.Builder(
1847 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001848 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001849 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001850 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001851 */
1852 public static final class WearableExtender implements Extender {
1853 /** Notification action extra which contains wearable extensions */
1854 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1855
Pete Gastaf6781d2014-10-07 15:17:05 -04001856 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001857 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001858 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1859 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1860 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001861
1862 // Flags bitwise-ored to mFlags
1863 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001864 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001865 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001866
1867 // Default value for flags integer
1868 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1869
1870 private int mFlags = DEFAULT_FLAGS;
1871
Pete Gastaf6781d2014-10-07 15:17:05 -04001872 private CharSequence mInProgressLabel;
1873 private CharSequence mConfirmLabel;
1874 private CharSequence mCancelLabel;
1875
Griff Hazen61a9e862014-05-22 16:05:19 -07001876 /**
1877 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1878 * options.
1879 */
1880 public WearableExtender() {
1881 }
1882
1883 /**
1884 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1885 * wearable options present in an existing notification action.
1886 * @param action the notification action to inspect.
1887 */
1888 public WearableExtender(Action action) {
1889 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1890 if (wearableBundle != null) {
1891 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001892 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1893 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1894 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001895 }
1896 }
1897
1898 /**
1899 * Apply wearable extensions to a notification action that is being built. This is
1900 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1901 * method of {@link android.app.Notification.Action.Builder}.
1902 */
1903 @Override
1904 public Action.Builder extend(Action.Builder builder) {
1905 Bundle wearableBundle = new Bundle();
1906
1907 if (mFlags != DEFAULT_FLAGS) {
1908 wearableBundle.putInt(KEY_FLAGS, mFlags);
1909 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001910 if (mInProgressLabel != null) {
1911 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1912 }
1913 if (mConfirmLabel != null) {
1914 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1915 }
1916 if (mCancelLabel != null) {
1917 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1918 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001919
1920 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1921 return builder;
1922 }
1923
1924 @Override
1925 public WearableExtender clone() {
1926 WearableExtender that = new WearableExtender();
1927 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001928 that.mInProgressLabel = this.mInProgressLabel;
1929 that.mConfirmLabel = this.mConfirmLabel;
1930 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001931 return that;
1932 }
1933
1934 /**
1935 * Set whether this action is available when the wearable device is not connected to
1936 * a companion device. The user can still trigger this action when the wearable device is
1937 * offline, but a visual hint will indicate that the action may not be available.
1938 * Defaults to true.
1939 */
1940 public WearableExtender setAvailableOffline(boolean availableOffline) {
1941 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1942 return this;
1943 }
1944
1945 /**
1946 * Get whether this action is available when the wearable device is not connected to
1947 * a companion device. The user can still trigger this action when the wearable device is
1948 * offline, but a visual hint will indicate that the action may not be available.
1949 * Defaults to true.
1950 */
1951 public boolean isAvailableOffline() {
1952 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1953 }
1954
1955 private void setFlag(int mask, boolean value) {
1956 if (value) {
1957 mFlags |= mask;
1958 } else {
1959 mFlags &= ~mask;
1960 }
1961 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001962
1963 /**
1964 * Set a label to display while the wearable is preparing to automatically execute the
1965 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1966 *
1967 * @param label the label to display while the action is being prepared to execute
1968 * @return this object for method chaining
1969 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001970 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001971 public WearableExtender setInProgressLabel(CharSequence label) {
1972 mInProgressLabel = label;
1973 return this;
1974 }
1975
1976 /**
1977 * Get the label to display while the wearable is preparing to automatically execute
1978 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1979 *
1980 * @return the label to display while the action is being prepared to execute
1981 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001982 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001983 public CharSequence getInProgressLabel() {
1984 return mInProgressLabel;
1985 }
1986
1987 /**
1988 * Set a label to display to confirm that the action should be executed.
1989 * This is usually an imperative verb like "Send".
1990 *
1991 * @param label the label to confirm the action should be executed
1992 * @return this object for method chaining
1993 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001994 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001995 public WearableExtender setConfirmLabel(CharSequence label) {
1996 mConfirmLabel = label;
1997 return this;
1998 }
1999
2000 /**
2001 * Get the label to display to confirm that the action should be executed.
2002 * This is usually an imperative verb like "Send".
2003 *
2004 * @return the label to confirm the action should be executed
2005 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002006 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002007 public CharSequence getConfirmLabel() {
2008 return mConfirmLabel;
2009 }
2010
2011 /**
2012 * Set a label to display to cancel the action.
2013 * This is usually an imperative verb, like "Cancel".
2014 *
2015 * @param label the label to display to cancel the action
2016 * @return this object for method chaining
2017 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002018 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002019 public WearableExtender setCancelLabel(CharSequence label) {
2020 mCancelLabel = label;
2021 return this;
2022 }
2023
2024 /**
2025 * Get the label to display to cancel the action.
2026 * This is usually an imperative verb like "Cancel".
2027 *
2028 * @return the label to display to cancel the action
2029 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002030 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002031 public CharSequence getCancelLabel() {
2032 return mCancelLabel;
2033 }
Alex Hills9ab3a232016-04-05 14:54:56 -04002034
2035 /**
2036 * Set a hint that this Action will launch an {@link Activity} directly, telling the
2037 * platform that it can generate the appropriate transitions.
2038 * @param hintLaunchesActivity {@code true} if the content intent will launch
2039 * an activity and transitions should be generated, false otherwise.
2040 * @return this object for method chaining
2041 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04002042 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04002043 boolean hintLaunchesActivity) {
2044 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
2045 return this;
2046 }
2047
2048 /**
2049 * Get a hint that this Action will launch an {@link Activity} directly, telling the
2050 * platform that it can generate the appropriate transitions
2051 * @return {@code true} if the content intent will launch an activity and transitions
2052 * should be generated, false otherwise. The default value is {@code false} if this was
2053 * never set.
2054 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04002055 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04002056 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
2057 }
Alex Hills9f087612016-06-07 09:08:59 -04002058
2059 /**
2060 * Set a hint that this Action should be displayed inline.
2061 *
2062 * @param hintDisplayInline {@code true} if action should be displayed inline, false
2063 * otherwise
2064 * @return this object for method chaining
2065 */
2066 public WearableExtender setHintDisplayActionInline(
2067 boolean hintDisplayInline) {
2068 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
2069 return this;
2070 }
2071
2072 /**
2073 * Get a hint that this Action should be displayed inline.
2074 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08002075 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04002076 * otherwise. The default value is {@code false} if this was never set.
2077 */
2078 public boolean getHintDisplayActionInline() {
2079 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
2080 }
Griff Hazen61a9e862014-05-22 16:05:19 -07002081 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002082
2083 /**
2084 * Provides meaning to an {@link Action} that hints at what the associated
2085 * {@link PendingIntent} will do. For example, an {@link Action} with a
2086 * {@link PendingIntent} that replies to a text message notification may have the
2087 * {@link #SEMANTIC_ACTION_REPLY} {@code SemanticAction} set within it.
2088 *
2089 * @hide
2090 */
2091 @IntDef(prefix = { "SEMANTIC_ACTION_" }, value = {
2092 SEMANTIC_ACTION_NONE,
2093 SEMANTIC_ACTION_REPLY,
2094 SEMANTIC_ACTION_MARK_AS_READ,
2095 SEMANTIC_ACTION_MARK_AS_UNREAD,
2096 SEMANTIC_ACTION_DELETE,
2097 SEMANTIC_ACTION_ARCHIVE,
2098 SEMANTIC_ACTION_MUTE,
2099 SEMANTIC_ACTION_UNMUTE,
2100 SEMANTIC_ACTION_THUMBS_UP,
Kodlee Yincda5b092018-02-15 15:34:53 -08002101 SEMANTIC_ACTION_THUMBS_DOWN,
Gustav Sennton005d7a02019-01-04 13:41:32 +00002102 SEMANTIC_ACTION_CALL
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002103 })
2104 @Retention(RetentionPolicy.SOURCE)
2105 public @interface SemanticAction {}
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002106 }
2107
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002108 /**
2109 * Array of all {@link Action} structures attached to this notification by
2110 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
2111 * {@link android.service.notification.NotificationListenerService} that provide an alternative
2112 * interface for invoking actions.
2113 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05002114 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002115
2116 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002117 * Replacement version of this notification whose content will be shown
2118 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
2119 * and {@link #VISIBILITY_PUBLIC}.
2120 */
2121 public Notification publicVersion;
2122
2123 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002124 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08002125 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002126 */
2127 public Notification()
2128 {
2129 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002130 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002131 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002132 }
2133
2134 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 * @hide
2136 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002137 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002138 public Notification(Context context, int icon, CharSequence tickerText, long when,
2139 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
2140 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04002141 new Builder(context)
2142 .setWhen(when)
2143 .setSmallIcon(icon)
2144 .setTicker(tickerText)
2145 .setContentTitle(contentTitle)
2146 .setContentText(contentText)
2147 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
2148 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002149 }
2150
2151 /**
2152 * Constructs a Notification object with the information needed to
2153 * have a status bar icon without the standard expanded view.
2154 *
2155 * @param icon The resource id of the icon to put in the status bar.
2156 * @param tickerText The text that flows by in the status bar when the notification first
2157 * activates.
2158 * @param when The time to show in the time field. In the System.currentTimeMillis
2159 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08002160 *
2161 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002162 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002163 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002164 public Notification(int icon, CharSequence tickerText, long when)
2165 {
2166 this.icon = icon;
2167 this.tickerText = tickerText;
2168 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002169 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002170 }
2171
2172 /**
2173 * Unflatten the notification from a parcel.
2174 */
Svet Ganovddb94882016-06-23 19:55:24 -07002175 @SuppressWarnings("unchecked")
2176 public Notification(Parcel parcel) {
2177 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
2178 // intents in extras are always written as the last entry.
2179 readFromParcelImpl(parcel);
2180 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002181 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07002182 }
2183
2184 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002185 {
2186 int version = parcel.readInt();
2187
Adrian Roosfb921842017-10-26 14:49:56 +02002188 mWhitelistToken = parcel.readStrongBinder();
2189 if (mWhitelistToken == null) {
2190 mWhitelistToken = processWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -07002191 }
2192 // Propagate this token to all pending intents that are unmarshalled from the parcel.
Adrian Roosfb921842017-10-26 14:49:56 +02002193 parcel.setClassCookie(PendingIntent.class, mWhitelistToken);
Dianne Hackborn98305522017-05-05 17:53:53 -07002194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002195 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002196 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04002197 if (parcel.readInt() != 0) {
2198 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04002199 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
2200 icon = mSmallIcon.getResId();
2201 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002202 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002203 number = parcel.readInt();
2204 if (parcel.readInt() != 0) {
2205 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2206 }
2207 if (parcel.readInt() != 0) {
2208 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2209 }
2210 if (parcel.readInt() != 0) {
2211 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2212 }
2213 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002214 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002215 }
2216 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002217 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
2218 }
Joe Onorato561d3852010-11-20 18:09:34 -08002219 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002220 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08002221 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002222 defaults = parcel.readInt();
2223 flags = parcel.readInt();
2224 if (parcel.readInt() != 0) {
2225 sound = Uri.CREATOR.createFromParcel(parcel);
2226 }
2227
2228 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04002229 if (parcel.readInt() != 0) {
2230 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
2231 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002232 vibrate = parcel.createLongArray();
2233 ledARGB = parcel.readInt();
2234 ledOnMS = parcel.readInt();
2235 ledOffMS = parcel.readInt();
2236 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002237
2238 if (parcel.readInt() != 0) {
2239 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2240 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002241
2242 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002243
John Spurlockfd7f1e02014-03-18 16:41:57 -04002244 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002245
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002246 mGroupKey = parcel.readString();
2247
2248 mSortKey = parcel.readString();
2249
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06002250 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Robin Leead7e72a2017-12-04 15:45:46 +01002251 fixDuplicateExtras();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002252
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002253 actions = parcel.createTypedArray(Action.CREATOR); // may be null
2254
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002255 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002256 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2257 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002258
Chris Wren8fd39ec2014-02-27 17:43:26 -05002259 if (parcel.readInt() != 0) {
2260 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2261 }
2262
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002263 visibility = parcel.readInt();
2264
2265 if (parcel.readInt() != 0) {
2266 publicVersion = Notification.CREATOR.createFromParcel(parcel);
2267 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002268
2269 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002270
2271 if (parcel.readInt() != 0) {
2272 mChannelId = parcel.readString();
2273 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002274 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05002275
2276 if (parcel.readInt() != 0) {
2277 mShortcutId = parcel.readString();
2278 }
2279
Felipe Leme90205ef2019-03-05 09:59:52 -08002280 if (parcel.readInt() != 0) {
2281 mLocusId = LocusId.CREATOR.createFromParcel(parcel);
2282 }
2283
Julia Reynolds13d898c2017-02-02 12:22:05 -05002284 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04002285
2286 if (parcel.readInt() != 0) {
2287 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2288 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002289
2290 mGroupAlertBehavior = parcel.readInt();
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002291 if (parcel.readInt() != 0) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002292 mBubbleMetadata = BubbleMetadata.CREATOR.createFromParcel(parcel);
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002293 }
Gustav Sennton761884c2018-11-19 17:40:19 +00002294
2295 mAllowSystemGeneratedContextualActions = parcel.readBoolean();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296 }
2297
Andy Stadler110988c2010-12-03 14:29:16 -08002298 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07002299 public Notification clone() {
2300 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04002301 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002302 return that;
2303 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002304
Daniel Sandler1a497d32013-04-18 14:52:45 -04002305 /**
2306 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
2307 * of this into that.
2308 * @hide
2309 */
2310 public void cloneInto(Notification that, boolean heavy) {
Adrian Roosfb921842017-10-26 14:49:56 +02002311 that.mWhitelistToken = this.mWhitelistToken;
Joe Onorato18e69df2010-05-17 22:26:12 -07002312 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002313 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002314 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07002315 that.number = this.number;
2316
2317 // PendingIntents are global, so there's no reason (or way) to clone them.
2318 that.contentIntent = this.contentIntent;
2319 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002320 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07002321
2322 if (this.tickerText != null) {
2323 that.tickerText = this.tickerText.toString();
2324 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002325 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002326 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04002327 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002328 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07002329 that.contentView = this.contentView.clone();
2330 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002331 if (heavy && this.mLargeIcon != null) {
2332 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08002333 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01002334 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07002335 that.sound = this.sound; // android.net.Uri is immutable
2336 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002337 if (this.audioAttributes != null) {
2338 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
2339 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002340
2341 final long[] vibrate = this.vibrate;
2342 if (vibrate != null) {
2343 final int N = vibrate.length;
2344 final long[] vib = that.vibrate = new long[N];
2345 System.arraycopy(vibrate, 0, vib, 0, N);
2346 }
2347
2348 that.ledARGB = this.ledARGB;
2349 that.ledOnMS = this.ledOnMS;
2350 that.ledOffMS = this.ledOffMS;
2351 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002352
Joe Onorato18e69df2010-05-17 22:26:12 -07002353 that.flags = this.flags;
2354
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002355 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08002356
John Spurlockfd7f1e02014-03-18 16:41:57 -04002357 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002358
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002359 that.mGroupKey = this.mGroupKey;
2360
2361 that.mSortKey = this.mSortKey;
2362
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002363 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002364 try {
2365 that.extras = new Bundle(this.extras);
2366 // will unparcel
2367 that.extras.size();
2368 } catch (BadParcelableException e) {
2369 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
2370 that.extras = null;
2371 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002372 }
2373
Felipe Lemedd85da62016-06-28 11:29:54 -07002374 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2375 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002376 }
2377
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002378 if (this.actions != null) {
2379 that.actions = new Action[this.actions.length];
2380 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002381 if ( this.actions[i] != null) {
2382 that.actions[i] = this.actions[i].clone();
2383 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002384 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002385 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002386
Daniel Sandler1a497d32013-04-18 14:52:45 -04002387 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002388 that.bigContentView = this.bigContentView.clone();
2389 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002390
Chris Wren8fd39ec2014-02-27 17:43:26 -05002391 if (heavy && this.headsUpContentView != null) {
2392 that.headsUpContentView = this.headsUpContentView.clone();
2393 }
2394
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002395 that.visibility = this.visibility;
2396
2397 if (this.publicVersion != null) {
2398 that.publicVersion = new Notification();
2399 this.publicVersion.cloneInto(that.publicVersion, heavy);
2400 }
2401
Dan Sandler26e81cf2014-05-06 10:01:27 -04002402 that.color = this.color;
2403
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002404 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002405 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002406 that.mShortcutId = this.mShortcutId;
Felipe Leme90205ef2019-03-05 09:59:52 -08002407 that.mLocusId = this.mLocusId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002408 that.mBadgeIcon = this.mBadgeIcon;
2409 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002410 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002411 that.mBubbleMetadata = this.mBubbleMetadata;
Gustav Sennton761884c2018-11-19 17:40:19 +00002412 that.mAllowSystemGeneratedContextualActions = this.mAllowSystemGeneratedContextualActions;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002413
Daniel Sandler1a497d32013-04-18 14:52:45 -04002414 if (!heavy) {
2415 that.lightenPayload(); // will clean out extras
2416 }
2417 }
2418
2419 /**
Jeff Sharkey23b31182018-04-18 21:32:12 -06002420 * Note all {@link Uri} that are referenced internally, with the expectation
2421 * that Uri permission grants will need to be issued to ensure the recipient
2422 * of this object is able to render its contents.
2423 *
2424 * @hide
2425 */
2426 public void visitUris(@NonNull Consumer<Uri> visitor) {
2427 visitor.accept(sound);
2428
2429 if (tickerView != null) tickerView.visitUris(visitor);
2430 if (contentView != null) contentView.visitUris(visitor);
2431 if (bigContentView != null) bigContentView.visitUris(visitor);
2432 if (headsUpContentView != null) headsUpContentView.visitUris(visitor);
2433
2434 if (extras != null) {
2435 visitor.accept(extras.getParcelable(EXTRA_AUDIO_CONTENTS_URI));
Robin Leed107af62018-04-27 13:55:56 +02002436 if (extras.containsKey(EXTRA_BACKGROUND_IMAGE_URI)) {
2437 visitor.accept(Uri.parse(extras.getString(EXTRA_BACKGROUND_IMAGE_URI)));
2438 }
Jeff Sharkey23b31182018-04-18 21:32:12 -06002439 }
2440
2441 if (MessagingStyle.class.equals(getNotificationStyle()) && extras != null) {
2442 final Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
2443 if (!ArrayUtils.isEmpty(messages)) {
2444 for (MessagingStyle.Message message : MessagingStyle.Message
2445 .getMessagesFromBundleArray(messages)) {
2446 visitor.accept(message.getDataUri());
2447 }
2448 }
2449
2450 final Parcelable[] historic = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
2451 if (!ArrayUtils.isEmpty(historic)) {
2452 for (MessagingStyle.Message message : MessagingStyle.Message
2453 .getMessagesFromBundleArray(historic)) {
2454 visitor.accept(message.getDataUri());
2455 }
2456 }
2457 }
2458 }
2459
2460 /**
Daniel Sandler1a497d32013-04-18 14:52:45 -04002461 * Removes heavyweight parts of the Notification object for archival or for sending to
2462 * listeners when the full contents are not necessary.
2463 * @hide
2464 */
2465 public final void lightenPayload() {
2466 tickerView = null;
2467 contentView = null;
2468 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002469 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002470 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002471 if (extras != null && !extras.isEmpty()) {
2472 final Set<String> keyset = extras.keySet();
2473 final int N = keyset.size();
2474 final String[] keys = keyset.toArray(new String[N]);
2475 for (int i=0; i<N; i++) {
2476 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002477 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2478 continue;
2479 }
Dan Sandler50128532015-12-08 15:42:41 -05002480 final Object obj = extras.get(key);
2481 if (obj != null &&
2482 ( obj instanceof Parcelable
2483 || obj instanceof Parcelable[]
2484 || obj instanceof SparseArray
2485 || obj instanceof ArrayList)) {
2486 extras.remove(key);
2487 }
2488 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002489 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002490 }
2491
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002492 /**
2493 * Make sure this CharSequence is safe to put into a bundle, which basically
2494 * means it had better not be some custom Parcelable implementation.
2495 * @hide
2496 */
2497 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002498 if (cs == null) return cs;
2499 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2500 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2501 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002502 if (cs instanceof Parcelable) {
2503 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2504 + " instance is a custom Parcelable and not allowed in Notification");
2505 return cs.toString();
2506 }
Selim Cinek60a54252016-02-26 17:03:25 -08002507 return removeTextSizeSpans(cs);
2508 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002509
Selim Cinek60a54252016-02-26 17:03:25 -08002510 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2511 if (charSequence instanceof Spanned) {
2512 Spanned ss = (Spanned) charSequence;
2513 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2514 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2515 for (Object span : spans) {
2516 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002517 if (resultSpan instanceof CharacterStyle) {
2518 resultSpan = ((CharacterStyle) span).getUnderlying();
2519 }
2520 if (resultSpan instanceof TextAppearanceSpan) {
2521 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002522 resultSpan = new TextAppearanceSpan(
2523 originalSpan.getFamily(),
2524 originalSpan.getTextStyle(),
2525 -1,
2526 originalSpan.getTextColor(),
2527 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002528 } else if (resultSpan instanceof RelativeSizeSpan
2529 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002530 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002531 } else {
2532 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002533 }
2534 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2535 ss.getSpanFlags(span));
2536 }
2537 return builder;
2538 }
2539 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002540 }
2541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002542 public int describeContents() {
2543 return 0;
2544 }
2545
2546 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002547 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002548 */
Svet Ganovddb94882016-06-23 19:55:24 -07002549 public void writeToParcel(Parcel parcel, int flags) {
2550 // We need to mark all pending intents getting into the notification
2551 // system as being put there to later allow the notification ranker
2552 // to launch them and by doing so add the app to the battery saver white
2553 // list for a short period of time. The problem is that the system
2554 // cannot look into the extras as there may be parcelables there that
2555 // the platform does not know how to handle. To go around that we have
2556 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002557 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002558 if (collectPendingIntents) {
2559 PendingIntent.setOnMarshaledListener(
2560 (PendingIntent intent, Parcel out, int outFlags) -> {
2561 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002562 if (allPendingIntents == null) {
2563 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002564 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002565 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002566 }
2567 });
2568 }
2569 try {
2570 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002571 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002572 writeToParcelImpl(parcel, flags);
2573 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002574 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002575 } finally {
2576 if (collectPendingIntents) {
2577 PendingIntent.setOnMarshaledListener(null);
2578 }
2579 }
2580 }
2581
2582 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002583 parcel.writeInt(1);
2584
Adrian Roosfb921842017-10-26 14:49:56 +02002585 parcel.writeStrongBinder(mWhitelistToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002586 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002587 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002588 if (mSmallIcon == null && icon != 0) {
2589 // you snuck an icon in here without using the builder; let's try to keep it
2590 mSmallIcon = Icon.createWithResource("", icon);
2591 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002592 if (mSmallIcon != null) {
2593 parcel.writeInt(1);
2594 mSmallIcon.writeToParcel(parcel, 0);
2595 } else {
2596 parcel.writeInt(0);
2597 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002598 parcel.writeInt(number);
2599 if (contentIntent != null) {
2600 parcel.writeInt(1);
2601 contentIntent.writeToParcel(parcel, 0);
2602 } else {
2603 parcel.writeInt(0);
2604 }
2605 if (deleteIntent != null) {
2606 parcel.writeInt(1);
2607 deleteIntent.writeToParcel(parcel, 0);
2608 } else {
2609 parcel.writeInt(0);
2610 }
2611 if (tickerText != null) {
2612 parcel.writeInt(1);
2613 TextUtils.writeToParcel(tickerText, parcel, flags);
2614 } else {
2615 parcel.writeInt(0);
2616 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002617 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002618 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002619 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002620 } else {
2621 parcel.writeInt(0);
2622 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002623 if (contentView != null) {
2624 parcel.writeInt(1);
2625 contentView.writeToParcel(parcel, 0);
2626 } else {
2627 parcel.writeInt(0);
2628 }
Selim Cinek279fa862016-06-14 10:57:25 -07002629 if (mLargeIcon == null && largeIcon != null) {
2630 // you snuck an icon in here without using the builder; let's try to keep it
2631 mLargeIcon = Icon.createWithBitmap(largeIcon);
2632 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002633 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002634 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002635 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002636 } else {
2637 parcel.writeInt(0);
2638 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002639
2640 parcel.writeInt(defaults);
2641 parcel.writeInt(this.flags);
2642
2643 if (sound != null) {
2644 parcel.writeInt(1);
2645 sound.writeToParcel(parcel, 0);
2646 } else {
2647 parcel.writeInt(0);
2648 }
2649 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002650
2651 if (audioAttributes != null) {
2652 parcel.writeInt(1);
2653 audioAttributes.writeToParcel(parcel, 0);
2654 } else {
2655 parcel.writeInt(0);
2656 }
2657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002658 parcel.writeLongArray(vibrate);
2659 parcel.writeInt(ledARGB);
2660 parcel.writeInt(ledOnMS);
2661 parcel.writeInt(ledOffMS);
2662 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002663
2664 if (fullScreenIntent != null) {
2665 parcel.writeInt(1);
2666 fullScreenIntent.writeToParcel(parcel, 0);
2667 } else {
2668 parcel.writeInt(0);
2669 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002670
2671 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002672
John Spurlockfd7f1e02014-03-18 16:41:57 -04002673 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002674
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002675 parcel.writeString(mGroupKey);
2676
2677 parcel.writeString(mSortKey);
2678
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002679 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002680
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002681 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002682
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002683 if (bigContentView != null) {
2684 parcel.writeInt(1);
2685 bigContentView.writeToParcel(parcel, 0);
2686 } else {
2687 parcel.writeInt(0);
2688 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002689
Chris Wren8fd39ec2014-02-27 17:43:26 -05002690 if (headsUpContentView != null) {
2691 parcel.writeInt(1);
2692 headsUpContentView.writeToParcel(parcel, 0);
2693 } else {
2694 parcel.writeInt(0);
2695 }
2696
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002697 parcel.writeInt(visibility);
2698
2699 if (publicVersion != null) {
2700 parcel.writeInt(1);
2701 publicVersion.writeToParcel(parcel, 0);
2702 } else {
2703 parcel.writeInt(0);
2704 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002705
2706 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002707
2708 if (mChannelId != null) {
2709 parcel.writeInt(1);
2710 parcel.writeString(mChannelId);
2711 } else {
2712 parcel.writeInt(0);
2713 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002714 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002715
2716 if (mShortcutId != null) {
2717 parcel.writeInt(1);
2718 parcel.writeString(mShortcutId);
2719 } else {
2720 parcel.writeInt(0);
2721 }
2722
Felipe Leme90205ef2019-03-05 09:59:52 -08002723 if (mLocusId != null) {
2724 parcel.writeInt(1);
2725 mLocusId.writeToParcel(parcel, 0);
2726 } else {
2727 parcel.writeInt(0);
2728 }
2729
Julia Reynolds13d898c2017-02-02 12:22:05 -05002730 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002731
2732 if (mSettingsText != null) {
2733 parcel.writeInt(1);
2734 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2735 } else {
2736 parcel.writeInt(0);
2737 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002738
2739 parcel.writeInt(mGroupAlertBehavior);
Julia Reynoldsfc640012018-02-21 12:25:27 -05002740
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002741 if (mBubbleMetadata != null) {
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002742 parcel.writeInt(1);
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002743 mBubbleMetadata.writeToParcel(parcel, 0);
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002744 } else {
2745 parcel.writeInt(0);
2746 }
2747
Gustav Sennton761884c2018-11-19 17:40:19 +00002748 parcel.writeBoolean(mAllowSystemGeneratedContextualActions);
2749
Julia Reynoldsfc640012018-02-21 12:25:27 -05002750 // mUsesStandardHeader is not written because it should be recomputed in listeners
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002751 }
2752
2753 /**
2754 * Parcelable.Creator that instantiates Notification objects
2755 */
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07002756 public static final @android.annotation.NonNull Parcelable.Creator<Notification> CREATOR
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002757 = new Parcelable.Creator<Notification>()
2758 {
2759 public Notification createFromParcel(Parcel parcel)
2760 {
2761 return new Notification(parcel);
2762 }
2763
2764 public Notification[] newArray(int size)
2765 {
2766 return new Notification[size];
2767 }
2768 };
2769
2770 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05002771 * @hide
2772 */
2773 public static boolean areActionsVisiblyDifferent(Notification first, Notification second) {
2774 Notification.Action[] firstAs = first.actions;
2775 Notification.Action[] secondAs = second.actions;
2776 if (firstAs == null && secondAs != null || firstAs != null && secondAs == null) {
2777 return true;
2778 }
2779 if (firstAs != null && secondAs != null) {
2780 if (firstAs.length != secondAs.length) {
2781 return true;
2782 }
2783 for (int i = 0; i < firstAs.length; i++) {
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -04002784 if (!Objects.equals(String.valueOf(firstAs[i].title),
2785 String.valueOf(secondAs[i].title))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05002786 return true;
2787 }
2788 RemoteInput[] firstRs = firstAs[i].getRemoteInputs();
2789 RemoteInput[] secondRs = secondAs[i].getRemoteInputs();
2790 if (firstRs == null) {
2791 firstRs = new RemoteInput[0];
2792 }
2793 if (secondRs == null) {
2794 secondRs = new RemoteInput[0];
2795 }
2796 if (firstRs.length != secondRs.length) {
2797 return true;
2798 }
2799 for (int j = 0; j < firstRs.length; j++) {
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -04002800 if (!Objects.equals(String.valueOf(firstRs[j].getLabel()),
2801 String.valueOf(secondRs[j].getLabel()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05002802 return true;
2803 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05002804 }
2805 }
2806 }
2807 return false;
2808 }
2809
2810 /**
2811 * @hide
2812 */
2813 public static boolean areStyledNotificationsVisiblyDifferent(Builder first, Builder second) {
2814 if (first.getStyle() == null) {
2815 return second.getStyle() != null;
2816 }
2817 if (second.getStyle() == null) {
2818 return true;
2819 }
2820 return first.getStyle().areNotificationsVisiblyDifferent(second.getStyle());
2821 }
2822
2823 /**
2824 * @hide
2825 */
2826 public static boolean areRemoteViewsChanged(Builder first, Builder second) {
Julia Reynoldse5c60452018-04-30 14:41:36 -04002827 if (!Objects.equals(first.usesStandardHeader(), second.usesStandardHeader())) {
2828 return true;
2829 }
2830
2831 if (areRemoteViewsChanged(first.mN.contentView, second.mN.contentView)) {
2832 return true;
2833 }
2834 if (areRemoteViewsChanged(first.mN.bigContentView, second.mN.bigContentView)) {
2835 return true;
2836 }
2837 if (areRemoteViewsChanged(first.mN.headsUpContentView, second.mN.headsUpContentView)) {
2838 return true;
2839 }
2840
2841 return false;
2842 }
2843
2844 private static boolean areRemoteViewsChanged(RemoteViews first, RemoteViews second) {
2845 if (first == null && second == null) {
2846 return false;
2847 }
2848 if (first == null && second != null || first != null && second == null) {
2849 return true;
2850 }
2851
2852 if (!Objects.equals(first.getLayoutId(), second.getLayoutId())) {
2853 return true;
2854 }
2855
2856 if (!Objects.equals(first.getSequenceNumber(), second.getSequenceNumber())) {
2857 return true;
2858 }
2859
2860 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05002861 }
2862
2863 /**
Robin Leead7e72a2017-12-04 15:45:46 +01002864 * Parcelling creates multiple copies of objects in {@code extras}. Fix them.
2865 * <p>
2866 * For backwards compatibility {@code extras} holds some references to "real" member data such
2867 * as {@link getLargeIcon()} which is mirrored by {@link #EXTRA_LARGE_ICON}. This is mostly
2868 * fine as long as the object stays in one process.
2869 * <p>
2870 * However, once the notification goes into a parcel each reference gets marshalled separately,
2871 * wasting memory. Especially with large images on Auto and TV, this is worth fixing.
2872 */
2873 private void fixDuplicateExtras() {
2874 if (extras != null) {
Robin Leead7e72a2017-12-04 15:45:46 +01002875 fixDuplicateExtra(mLargeIcon, EXTRA_LARGE_ICON);
2876 }
2877 }
2878
2879 /**
2880 * If we find an extra that's exactly the same as one of the "real" fields but refers to a
2881 * separate object, replace it with the field's version to avoid holding duplicate copies.
2882 */
2883 private void fixDuplicateExtra(@Nullable Parcelable original, @NonNull String extraName) {
2884 if (original != null && extras.getParcelable(extraName) != null) {
2885 extras.putParcelable(extraName, original);
2886 }
2887 }
2888
2889 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002890 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2891 * layout.
2892 *
2893 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2894 * in the view.</p>
2895 * @param context The context for your application / activity.
2896 * @param contentTitle The title that goes in the expanded entry.
2897 * @param contentText The text that goes in the expanded entry.
2898 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2899 * If this is an activity, it must include the
2900 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002901 * that you take care of task management as described in the
2902 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2903 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002904 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002905 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002906 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002907 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002908 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002909 public void setLatestEventInfo(Context context,
2910 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002911 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2912 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2913 new Throwable());
2914 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002915
Selim Cinek4ac6f602016-06-13 15:47:03 -07002916 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2917 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2918 }
2919
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002920 // ensure that any information already set directly is preserved
2921 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002922
2923 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002924 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002925 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002926 }
2927 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002928 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002929 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002930 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002931
2932 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002933 }
2934
Julia Reynoldsda303542015-11-23 14:00:20 -05002935 /**
2936 * @hide
2937 */
2938 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002939 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002940 }
2941
2942 /**
2943 * @hide
2944 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002945 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002946 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002947 }
2948
Yi Jin6b514142017-10-30 14:54:12 -07002949 /**
2950 * @hide
2951 */
2952 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2953 long token = proto.start(fieldId);
2954 proto.write(NotificationProto.CHANNEL_ID, getChannelId());
2955 proto.write(NotificationProto.HAS_TICKER_TEXT, this.tickerText != null);
2956 proto.write(NotificationProto.FLAGS, this.flags);
2957 proto.write(NotificationProto.COLOR, this.color);
2958 proto.write(NotificationProto.CATEGORY, this.category);
2959 proto.write(NotificationProto.GROUP_KEY, this.mGroupKey);
2960 proto.write(NotificationProto.SORT_KEY, this.mSortKey);
2961 if (this.actions != null) {
2962 proto.write(NotificationProto.ACTION_LENGTH, this.actions.length);
2963 }
2964 if (this.visibility >= VISIBILITY_SECRET && this.visibility <= VISIBILITY_PUBLIC) {
2965 proto.write(NotificationProto.VISIBILITY, this.visibility);
2966 }
2967 if (publicVersion != null) {
2968 publicVersion.writeToProto(proto, NotificationProto.PUBLIC_VERSION);
2969 }
2970 proto.end(token);
2971 }
2972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002973 @Override
2974 public String toString() {
2975 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002976 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04002977 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002978 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002979 sb.append(priority);
2980 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002981 if (contentView != null) {
2982 sb.append(contentView.getPackage());
2983 sb.append("/0x");
2984 sb.append(Integer.toHexString(contentView.getLayoutId()));
2985 } else {
2986 sb.append("null");
2987 }
2988 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002989 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2990 sb.append("default");
2991 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002992 int N = this.vibrate.length-1;
2993 sb.append("[");
2994 for (int i=0; i<N; i++) {
2995 sb.append(this.vibrate[i]);
2996 sb.append(',');
2997 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002998 if (N != -1) {
2999 sb.append(this.vibrate[N]);
3000 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003001 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003002 } else {
3003 sb.append("null");
3004 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003005 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05003006 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003007 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05003008 } else if (this.sound != null) {
3009 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003010 } else {
3011 sb.append("null");
3012 }
Chris Wren365b6d32015-07-16 10:39:26 -04003013 if (this.tickerText != null) {
3014 sb.append(" tick");
3015 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003016 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003017 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003018 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04003019 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04003020 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003021 if (this.category != null) {
3022 sb.append(" category=");
3023 sb.append(this.category);
3024 }
3025 if (this.mGroupKey != null) {
3026 sb.append(" groupKey=");
3027 sb.append(this.mGroupKey);
3028 }
3029 if (this.mSortKey != null) {
3030 sb.append(" sortKey=");
3031 sb.append(this.mSortKey);
3032 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003033 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04003034 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003035 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04003036 }
3037 sb.append(" vis=");
3038 sb.append(visibilityToString(this.visibility));
3039 if (this.publicVersion != null) {
3040 sb.append(" publicVersion=");
3041 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003042 }
Felipe Leme90205ef2019-03-05 09:59:52 -08003043 if (this.mLocusId != null) {
3044 sb.append(" locusId=");
3045 sb.append(this.mLocusId); // LocusId.toString() is PII safe.
3046 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003047 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003048 return sb.toString();
3049 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003050
Dan Sandler1b718782014-07-18 12:43:45 -04003051 /**
3052 * {@hide}
3053 */
3054 public static String visibilityToString(int vis) {
3055 switch (vis) {
3056 case VISIBILITY_PRIVATE:
3057 return "PRIVATE";
3058 case VISIBILITY_PUBLIC:
3059 return "PUBLIC";
3060 case VISIBILITY_SECRET:
3061 return "SECRET";
3062 default:
3063 return "UNKNOWN(" + String.valueOf(vis) + ")";
3064 }
3065 }
3066
Joe Onoratocb109a02011-01-18 17:57:41 -08003067 /**
John Spurlock1d881a12015-03-18 19:21:54 -04003068 * {@hide}
3069 */
3070 public static String priorityToString(@Priority int pri) {
3071 switch (pri) {
3072 case PRIORITY_MIN:
3073 return "MIN";
3074 case PRIORITY_LOW:
3075 return "LOW";
3076 case PRIORITY_DEFAULT:
3077 return "DEFAULT";
3078 case PRIORITY_HIGH:
3079 return "HIGH";
3080 case PRIORITY_MAX:
3081 return "MAX";
3082 default:
3083 return "UNKNOWN(" + String.valueOf(pri) + ")";
3084 }
3085 }
3086
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04003087 /**
3088 * @hide
3089 */
3090 public boolean hasCompletedProgress() {
3091 // not a progress notification; can't be complete
3092 if (!extras.containsKey(EXTRA_PROGRESS)
3093 || !extras.containsKey(EXTRA_PROGRESS_MAX)) {
3094 return false;
3095 }
3096 // many apps use max 0 for 'indeterminate'; not complete
3097 if (extras.getInt(EXTRA_PROGRESS_MAX) == 0) {
3098 return false;
3099 }
3100 return extras.getInt(EXTRA_PROGRESS) == extras.getInt(EXTRA_PROGRESS_MAX);
3101 }
3102
Jeff Sharkey000ce802017-04-29 13:13:27 -06003103 /** @removed */
3104 @Deprecated
Julia Reynolds37856052016-11-11 09:20:07 -05003105 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003106 return mChannelId;
3107 }
3108
3109 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003110 * Returns the id of the channel this notification posts to.
3111 */
3112 public String getChannelId() {
3113 return mChannelId;
3114 }
3115
Jeff Sharkey000ce802017-04-29 13:13:27 -06003116 /** @removed */
3117 @Deprecated
Julia Reynolds2a128742016-11-28 14:29:25 -05003118 public long getTimeout() {
3119 return mTimeout;
3120 }
3121
3122 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003123 * Returns the duration from posting after which this notification should be canceled by the
3124 * system, if it's not canceled already.
3125 */
3126 public long getTimeoutAfter() {
3127 return mTimeout;
3128 }
3129
3130 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003131 * Returns what icon should be shown for this notification if it is being displayed in a
3132 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
3133 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
3134 */
3135 public int getBadgeIconType() {
3136 return mBadgeIcon;
3137 }
3138
3139 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003140 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04003141 *
3142 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
3143 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003144 */
3145 public String getShortcutId() {
3146 return mShortcutId;
3147 }
3148
Felipe Leme90205ef2019-03-05 09:59:52 -08003149 /**
3150 * Gets the {@link LocusId} associated with this notification.
3151 *
3152 * <p>Used by the device's intelligence services to correlate objects (such as
3153 * {@link ShortcutInfo} and {@link ContentCaptureContext}) that are correlated.
3154 */
3155 @Nullable
3156 public LocusId getLocusId() {
3157 return mLocusId;
3158 }
Julia Reynolds3aedded2017-03-31 14:42:09 -04003159
3160 /**
3161 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
3162 */
3163 public CharSequence getSettingsText() {
3164 return mSettingsText;
3165 }
3166
Julia Reynolds13d898c2017-02-02 12:22:05 -05003167 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003168 * Returns which type of notifications in a group are responsible for audibly alerting the
3169 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
3170 * {@link #GROUP_ALERT_SUMMARY}.
3171 */
3172 public @GroupAlertBehavior int getGroupAlertBehavior() {
3173 return mGroupAlertBehavior;
3174 }
3175
3176 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003177 * Returns the bubble metadata that will be used to display app content in a floating window
3178 * over the existing foreground activity.
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003179 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08003180 @Nullable
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003181 public BubbleMetadata getBubbleMetadata() {
3182 return mBubbleMetadata;
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003183 }
3184
Gustav Sennton79ebf4b2018-12-19 10:31:51 +00003185 /**
3186 * Returns whether the platform is allowed (by the app developer) to generate contextual actions
3187 * for this notification.
3188 */
Gustav Sennton761884c2018-11-19 17:40:19 +00003189 public boolean getAllowSystemGeneratedContextualActions() {
3190 return mAllowSystemGeneratedContextualActions;
3191 }
3192
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003193 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003194 * The small icon representing this notification in the status bar and content view.
3195 *
3196 * @return the small icon representing this notification.
3197 *
3198 * @see Builder#getSmallIcon()
3199 * @see Builder#setSmallIcon(Icon)
3200 */
3201 public Icon getSmallIcon() {
3202 return mSmallIcon;
3203 }
3204
3205 /**
3206 * Used when notifying to clean up legacy small icons.
3207 * @hide
3208 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003209 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04003210 public void setSmallIcon(Icon icon) {
3211 mSmallIcon = icon;
3212 }
3213
3214 /**
3215 * The large icon shown in this notification's content view.
3216 * @see Builder#getLargeIcon()
3217 * @see Builder#setLargeIcon(Icon)
3218 */
3219 public Icon getLargeIcon() {
3220 return mLargeIcon;
3221 }
3222
3223 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02003224 * @hide
3225 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003226 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003227 public boolean isGroupSummary() {
3228 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
3229 }
3230
3231 /**
3232 * @hide
3233 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003234 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003235 public boolean isGroupChild() {
3236 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
3237 }
3238
3239 /**
Julia Reynolds30203152017-05-26 13:36:31 -04003240 * @hide
3241 */
3242 public boolean suppressAlertingDueToGrouping() {
3243 if (isGroupSummary()
3244 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
3245 return true;
3246 } else if (isGroupChild()
3247 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
3248 return true;
3249 }
3250 return false;
3251 }
3252
Tony Mak638430e2018-10-08 19:19:10 +01003253
3254 /**
3255 * Finds and returns a remote input and its corresponding action.
3256 *
3257 * @param requiresFreeform requires the remoteinput to allow freeform or not.
3258 * @return the result pair, {@code null} if no result is found.
3259 *
3260 * @hide
3261 */
3262 @Nullable
3263 public Pair<RemoteInput, Action> findRemoteInputActionPair(boolean requiresFreeform) {
3264 if (actions == null) {
3265 return null;
3266 }
3267 for (Notification.Action action : actions) {
3268 if (action.getRemoteInputs() == null) {
3269 continue;
3270 }
3271 RemoteInput resultRemoteInput = null;
3272 for (RemoteInput remoteInput : action.getRemoteInputs()) {
3273 if (remoteInput.getAllowFreeFormInput() || !requiresFreeform) {
3274 resultRemoteInput = remoteInput;
3275 }
3276 }
3277 if (resultRemoteInput != null) {
3278 return Pair.create(resultRemoteInput, action);
3279 }
3280 }
3281 return null;
3282 }
3283
Julia Reynolds30203152017-05-26 13:36:31 -04003284 /**
Gustav Sennton005d7a02019-01-04 13:41:32 +00003285 * Returns the actions that are contextual out of the actions in this notification.
Gustav Senntoneab53682018-11-01 16:30:23 +00003286 *
3287 * @hide
3288 */
3289 public List<Notification.Action> getContextualActions() {
3290 if (actions == null) return Collections.emptyList();
3291
3292 List<Notification.Action> contextualActions = new ArrayList<>();
3293 for (Notification.Action action : actions) {
Gustav Sennton005d7a02019-01-04 13:41:32 +00003294 if (action.isContextual()) {
Gustav Senntoneab53682018-11-01 16:30:23 +00003295 contextualActions.add(action);
3296 }
3297 }
3298 return contextualActions;
3299 }
3300
3301 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003302 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08003303 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003304 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07003305 * content views using the platform's notification layout template. If your app supports
3306 * versions of Android as old as API level 4, you can instead use
3307 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
3308 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
3309 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08003310 *
Scott Main183bf112012-08-13 19:12:13 -07003311 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08003312 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003313 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07003314 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003315 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3316 * .setContentText(subject)
3317 * .setSmallIcon(R.drawable.new_mail)
3318 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003319 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003320 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08003321 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003322 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003323 /**
3324 * @hide
3325 */
3326 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
3327 "android.rebuild.contentViewActionCount";
3328 /**
3329 * @hide
3330 */
3331 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
3332 = "android.rebuild.bigViewActionCount";
3333 /**
3334 * @hide
3335 */
3336 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
3337 = "android.rebuild.hudViewActionCount";
3338
Selim Cinek6743c0b2017-01-18 18:24:01 -08003339 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
3340 SystemProperties.getBoolean("notifications.only_title", true);
3341
Selim Cinek389edcd2017-05-11 19:16:44 -07003342 /**
3343 * The lightness difference that has to be added to the primary text color to obtain the
3344 * secondary text color when the background is light.
3345 */
3346 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
3347
3348 /**
3349 * The lightness difference that has to be added to the primary text color to obtain the
3350 * secondary text color when the background is dark.
3351 * A bit less then the above value, since it looks better on dark backgrounds.
3352 */
3353 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
3354
Joe Onorato46439ce2010-11-19 13:56:21 -08003355 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003356 private Notification mN;
3357 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003358 private Style mStyle;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003359 @UnsupportedAppUsage
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003360 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Selim Cineke7238dd2017-12-14 17:48:32 -08003361 private ArrayList<Person> mPersonList = new ArrayList<>();
Lucas Dupina291d192018-06-07 13:59:42 -07003362 private ContrastColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08003363 private boolean mIsLegacy;
3364 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02003365
3366 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08003367 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
3368 */
3369 private int mCachedContrastColor = COLOR_INVALID;
3370 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08003371 /**
Selim Cinek4717d862018-04-19 09:19:15 +08003372 * Caches a ambient version of {@link #mCachedAmbientColorIsFor}.
Adrian Roos487374f2017-01-11 15:48:14 -08003373 */
3374 private int mCachedAmbientColor = COLOR_INVALID;
3375 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Selim Cinek4717d862018-04-19 09:19:15 +08003376 /**
3377 * A neutral color color that can be used for icons.
3378 */
3379 private int mNeutralColor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08003380
3381 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08003382 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
3383 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
3384 */
3385 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08003386 private int mTextColorsAreForBackground = COLOR_INVALID;
3387 private int mPrimaryTextColor = COLOR_INVALID;
3388 private int mSecondaryTextColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07003389 private int mBackgroundColor = COLOR_INVALID;
3390 private int mForegroundColor = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07003391 /**
3392 * A temporary location where actions are stored. If != null the view originally has action
3393 * but doesn't have any for this inflation.
3394 */
3395 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07003396 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003397
Anthony Chenad4d1582017-04-10 16:07:58 -07003398 private boolean mTintActionButtons;
3399 private boolean mInNightMode;
3400
Adrian Roos70d7aa32017-01-11 15:39:06 -08003401 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003402 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08003403 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003404 * @param context
3405 * A {@link Context} that will be used by the Builder to construct the
3406 * RemoteViews. The Context will not be held past the lifetime of this Builder
3407 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003408 * @param channelId
3409 * The constructed Notification will be posted on this
3410 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
3411 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003412 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003413 public Builder(Context context, String channelId) {
3414 this(context, (Notification) null);
3415 mN.mChannelId = channelId;
3416 }
3417
3418 /**
3419 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
3420 * instead. All posted Notifications must specify a NotificationChannel Id.
3421 */
3422 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003423 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003424 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003425 }
3426
Joe Onoratocb109a02011-01-18 17:57:41 -08003427 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003428 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02003429 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003430 public Builder(Context context, Notification toAdopt) {
3431 mContext = context;
Anthony Chenad4d1582017-04-10 16:07:58 -07003432 Resources res = mContext.getResources();
3433 mTintActionButtons = res.getBoolean(R.bool.config_tintNotificationActionButtons);
3434
3435 if (res.getBoolean(R.bool.config_enableNightMode)) {
3436 Configuration currentConfig = res.getConfiguration();
3437 mInNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
3438 == Configuration.UI_MODE_NIGHT_YES;
3439 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003440
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003441 if (toAdopt == null) {
3442 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003443 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
3444 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
3445 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003446 mN.priority = PRIORITY_DEFAULT;
3447 mN.visibility = VISIBILITY_PRIVATE;
3448 } else {
3449 mN = toAdopt;
3450 if (mN.actions != null) {
3451 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003452 }
3453
Selim Cineke7238dd2017-12-14 17:48:32 -08003454 if (mN.extras.containsKey(EXTRA_PEOPLE_LIST)) {
3455 ArrayList<Person> people = mN.extras.getParcelableArrayList(EXTRA_PEOPLE_LIST);
3456 mPersonList.addAll(people);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003457 }
3458
Selim Cinek4ac6f602016-06-13 15:47:03 -07003459 if (mN.getSmallIcon() == null && mN.icon != 0) {
3460 setSmallIcon(mN.icon);
3461 }
3462
3463 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
3464 setLargeIcon(mN.largeIcon);
3465 }
3466
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003467 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
3468 if (!TextUtils.isEmpty(templateClass)) {
3469 final Class<? extends Style> styleClass
3470 = getNotificationStyleClass(templateClass);
3471 if (styleClass == null) {
3472 Log.d(TAG, "Unknown style class: " + templateClass);
3473 } else {
3474 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003475 final Constructor<? extends Style> ctor =
3476 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003477 ctor.setAccessible(true);
3478 final Style style = ctor.newInstance();
3479 style.restoreFromExtras(mN.extras);
3480
3481 if (style != null) {
3482 setStyle(style);
3483 }
3484 } catch (Throwable t) {
3485 Log.e(TAG, "Could not create Style", t);
3486 }
3487 }
3488 }
3489
3490 }
3491 }
3492
Lucas Dupina291d192018-06-07 13:59:42 -07003493 private ContrastColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08003494 if (mColorUtil == null) {
Lucas Dupina291d192018-06-07 13:59:42 -07003495 mColorUtil = ContrastColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003496 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003497 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003498 }
3499
3500 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003501 * If this notification is duplicative of a Launcher shortcut, sets the
3502 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
3503 * the shortcut.
3504 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04003505 * This field will be ignored by Launchers that don't support badging, don't show
3506 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003507 *
3508 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
3509 * supersedes
3510 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003511 @NonNull
Julia Reynolds13d898c2017-02-02 12:22:05 -05003512 public Builder setShortcutId(String shortcutId) {
3513 mN.mShortcutId = shortcutId;
3514 return this;
3515 }
3516
3517 /**
Felipe Leme90205ef2019-03-05 09:59:52 -08003518 * Sets the {@link LocusId} associated with this notification.
3519 *
3520 * <p>This method should be called when the {@link LocusId} is used in other places (such
3521 * as {@link ShortcutInfo} and {@link ContentCaptureContext}) so the device's intelligence
3522 * services can correlate them.
3523 */
3524 @NonNull
3525 public Builder setLocusId(@Nullable LocusId locusId) {
3526 mN.mLocusId = locusId;
3527 return this;
3528 }
3529
3530 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003531 * Sets which icon to display as a badge for this notification.
3532 *
3533 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
3534 * {@link #BADGE_ICON_LARGE}.
3535 *
3536 * Note: This value might be ignored, for launchers that don't support badge icons.
3537 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003538 @NonNull
Julia Reynolds612beb22017-03-30 10:48:30 -04003539 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04003540 mN.mBadgeIcon = icon;
3541 return this;
3542 }
3543
3544 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003545 * Sets the group alert behavior for this notification. Use this method to mute this
3546 * notification if alerts for this notification's group should be handled by a different
3547 * notification. This is only applicable for notifications that belong to a
Julia Reynolds399d9bf2017-08-11 12:52:14 -04003548 * {@link #setGroup(String) group}. This must be called on all notifications you want to
3549 * mute. For example, if you want only the summary of your group to make noise, all
3550 * children in the group should have the group alert behavior {@link #GROUP_ALERT_SUMMARY}.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003551 *
3552 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
3553 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003554 @NonNull
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003555 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
3556 mN.mGroupAlertBehavior = groupAlertBehavior;
3557 return this;
3558 }
3559
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003560 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003561 * Sets the {@link BubbleMetadata} that will be used to display app content in a floating
3562 * window over the existing foreground activity.
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003563 *
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003564 * <p>This data will be ignored unless the notification is posted to a channel that
3565 * allows {@link NotificationChannel#canBubble() bubbles}.</p>
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003566 *
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003567 * <b>Notifications with a valid and allowed bubble metadata will display in collapsed state
3568 * outside of the notification shade on unlocked devices. When a user interacts with the
3569 * collapsed state, the bubble intent will be invoked and displayed.</b>
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003570 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003571 @NonNull
Mady Mellorcf8f1b22019-03-07 14:08:21 -08003572 public Builder setBubbleMetadata(@Nullable BubbleMetadata data) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003573 mN.mBubbleMetadata = data;
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003574 return this;
3575 }
3576
Jeff Sharkey000ce802017-04-29 13:13:27 -06003577 /** @removed */
3578 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003579 public Builder setChannel(String channelId) {
3580 mN.mChannelId = channelId;
3581 return this;
3582 }
3583
3584 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003585 * Specifies the channel the notification should be delivered on.
3586 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003587 @NonNull
Julia Reynoldsbad42972017-04-25 13:52:49 -04003588 public Builder setChannelId(String channelId) {
3589 mN.mChannelId = channelId;
3590 return this;
3591 }
3592
Jeff Sharkey000ce802017-04-29 13:13:27 -06003593 /** @removed */
3594 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05003595 public Builder setTimeout(long durationMs) {
3596 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05003597 return this;
3598 }
3599
3600 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003601 * Specifies a duration in milliseconds after which this notification should be canceled,
3602 * if it is not already canceled.
3603 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003604 @NonNull
Julia Reynoldsbad42972017-04-25 13:52:49 -04003605 public Builder setTimeoutAfter(long durationMs) {
3606 mN.mTimeout = durationMs;
3607 return this;
3608 }
3609
3610 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003611 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003612 *
3613 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
3614 * shown anymore by default and must be opted into by using
3615 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003616 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003617 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08003618 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003619 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003620 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003621 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08003622 return this;
3623 }
3624
Joe Onoratocb109a02011-01-18 17:57:41 -08003625 /**
Griff Hazen50c11652014-05-16 09:46:31 -07003626 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07003627 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003628 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
3629 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07003630 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003631 @NonNull
Daniel Sandler0c890492012-09-12 17:23:10 -07003632 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003633 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07003634 return this;
3635 }
3636
3637 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003638 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08003639 *
3640 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003641 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003642 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003643 * Useful when showing an elapsed time (like an ongoing phone call).
3644 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08003645 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07003646 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08003647 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003648 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003649 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07003650 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003651 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003652 @NonNull
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003653 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003654 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003655 return this;
3656 }
3657
3658 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08003659 * Sets the Chronometer to count down instead of counting up.
3660 *
3661 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
3662 * If it isn't set the chronometer will count up.
3663 *
3664 * @see #setUsesChronometer(boolean)
3665 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003666 @NonNull
Adrian Roos96b7e202016-05-17 13:50:38 -07003667 public Builder setChronometerCountDown(boolean countDown) {
3668 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003669 return this;
3670 }
3671
3672 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003673 * Set the small icon resource, which will be used to represent the notification in the
3674 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003675 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003676
3677 * The platform template for the expanded view will draw this icon in the left, unless a
3678 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3679 * icon will be moved to the right-hand side.
3680 *
3681
3682 * @param icon
3683 * A resource ID in the application's package of the drawable to use.
3684 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003685 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003686 @NonNull
Tor Norbye7b9c9122013-05-30 16:48:33 -07003687 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003688 return setSmallIcon(icon != 0
3689 ? Icon.createWithResource(mContext, icon)
3690 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003691 }
3692
Joe Onoratocb109a02011-01-18 17:57:41 -08003693 /**
3694 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3695 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3696 * LevelListDrawable}.
3697 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003698 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003699 * @param level The level to use for the icon.
3700 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003701 * @see Notification#icon
3702 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003703 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003704 @NonNull
Tor Norbye7b9c9122013-05-30 16:48:33 -07003705 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003706 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003707 return setSmallIcon(icon);
3708 }
3709
3710 /**
3711 * Set the small icon, which will be used to represent the notification in the
koprivadebd4ee2018-09-13 10:59:46 -07003712 * status bar and content view (unless overridden there by a
Dan Sandlerd63f9322015-05-06 15:18:49 -04003713 * {@link #setLargeIcon(Bitmap) large icon}).
3714 *
3715 * @param icon An Icon object to use.
3716 * @see Notification#icon
3717 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003718 @NonNull
Dan Sandlerd63f9322015-05-06 15:18:49 -04003719 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003720 mN.setSmallIcon(icon);
3721 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3722 mN.icon = icon.getResId();
3723 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003724 return this;
3725 }
3726
Joe Onoratocb109a02011-01-18 17:57:41 -08003727 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003728 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003729 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003730 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003731 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003732 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003733 return this;
3734 }
3735
Joe Onoratocb109a02011-01-18 17:57:41 -08003736 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003737 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003738 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003739 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003740 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003741 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003742 return this;
3743 }
3744
Joe Onoratocb109a02011-01-18 17:57:41 -08003745 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003746 * This provides some additional information that is displayed in the notification. No
3747 * guarantees are given where exactly it is displayed.
3748 *
3749 * <p>This information should only be provided if it provides an essential
3750 * benefit to the understanding of the notification. The more text you provide the
3751 * less readable it becomes. For example, an email client should only provide the account
3752 * name here if more than one email account has been added.</p>
3753 *
3754 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3755 * notification header area.
3756 *
3757 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3758 * this will be shown in the third line of text in the platform notification template.
3759 * You should not be using {@link #setProgress(int, int, boolean)} at the
3760 * same time on those versions; they occupy the same place.
3761 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003762 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003763 @NonNull
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003764 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003765 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003766 return this;
3767 }
3768
3769 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003770 * Provides text that will appear as a link to your application's settings.
3771 *
3772 * <p>This text does not appear within notification {@link Style templates} but may
3773 * appear when the user uses an affordance to learn more about the notification.
3774 * Additionally, this text will not appear unless you provide a valid link target by
3775 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3776 *
3777 * <p>This text is meant to be concise description about what the user can customize
3778 * when they click on this link. The recommended maximum length is 40 characters.
3779 * @param text
3780 * @return
3781 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003782 @NonNull
Julia Reynolds3aedded2017-03-31 14:42:09 -04003783 public Builder setSettingsText(CharSequence text) {
3784 mN.mSettingsText = safeCharSequence(text);
3785 return this;
3786 }
3787
3788 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003789 * Set the remote input history.
3790 *
3791 * This should be set to the most recent inputs that have been sent
3792 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3793 * longer relevant (e.g. for chat notifications once the other party has responded).
3794 *
3795 * The most recent input must be stored at the 0 index, the second most recent at the
3796 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3797 * and how much of each individual input is shown.
3798 *
3799 * <p>Note: The reply text will only be shown on notifications that have least one action
3800 * with a {@code RemoteInput}.</p>
3801 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003802 @NonNull
Adrian Roose458aa82015-12-08 16:17:19 -08003803 public Builder setRemoteInputHistory(CharSequence[] text) {
3804 if (text == null) {
3805 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3806 } else {
3807 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3808 CharSequence[] safe = new CharSequence[N];
3809 for (int i = 0; i < N; i++) {
3810 safe[i] = safeCharSequence(text[i]);
3811 }
3812 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3813 }
3814 return this;
3815 }
3816
3817 /**
Kenny Guya0f6de82018-04-06 16:20:16 +01003818 * Sets whether remote history entries view should have a spinner.
3819 * @hide
3820 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003821 @NonNull
Kenny Guya0f6de82018-04-06 16:20:16 +01003822 public Builder setShowRemoteInputSpinner(boolean showSpinner) {
3823 mN.extras.putBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER, showSpinner);
3824 return this;
3825 }
3826
3827 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +01003828 * Sets whether smart reply buttons should be hidden.
3829 * @hide
3830 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003831 @NonNull
Kenny Guy8cc15d22018-05-09 09:50:55 +01003832 public Builder setHideSmartReplies(boolean hideSmartReplies) {
3833 mN.extras.putBoolean(EXTRA_HIDE_SMART_REPLIES, hideSmartReplies);
3834 return this;
3835 }
3836
3837 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003838 * Sets the number of items this notification represents. May be displayed as a badge count
3839 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003840 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003841 @NonNull
Joe Onorato8595a3d2010-11-19 18:12:07 -08003842 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003843 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003844 return this;
3845 }
3846
Joe Onoratocb109a02011-01-18 17:57:41 -08003847 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003848 * A small piece of additional information pertaining to this notification.
3849 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003850 * The platform template will draw this on the last line of the notification, at the far
3851 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003852 *
3853 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3854 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3855 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003856 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003857 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003858 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003859 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003860 return this;
3861 }
3862
Joe Onoratocb109a02011-01-18 17:57:41 -08003863 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003864 * Set the progress this notification represents.
3865 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003866 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003867 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003868 @NonNull
Jeff Sharkey1c400132011-08-05 14:50:13 -07003869 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003870 mN.extras.putInt(EXTRA_PROGRESS, progress);
3871 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3872 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003873 return this;
3874 }
3875
3876 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003877 * Supply a custom RemoteViews to use instead of the platform template.
3878 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003879 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003880 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003881 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003882 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003883 return setCustomContentView(views);
3884 }
3885
3886 /**
3887 * Supply custom RemoteViews to use instead of the platform template.
3888 *
3889 * This will override the layout that would otherwise be constructed by this Builder
3890 * object.
3891 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003892 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003893 public Builder setCustomContentView(RemoteViews contentView) {
3894 mN.contentView = contentView;
3895 return this;
3896 }
3897
3898 /**
3899 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3900 *
3901 * This will override the expanded layout that would otherwise be constructed by this
3902 * Builder object.
3903 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003904 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003905 public Builder setCustomBigContentView(RemoteViews contentView) {
3906 mN.bigContentView = contentView;
3907 return this;
3908 }
3909
3910 /**
3911 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3912 *
3913 * This will override the heads-up layout that would otherwise be constructed by this
3914 * Builder object.
3915 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003916 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003917 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3918 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003919 return this;
3920 }
3921
Joe Onoratocb109a02011-01-18 17:57:41 -08003922 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003923 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3924 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003925 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3926 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3927 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003928 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003929 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003930 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003931 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003932 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003933 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003934 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003935 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003936 return this;
3937 }
3938
Joe Onoratocb109a02011-01-18 17:57:41 -08003939 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003940 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3941 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003942 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003943 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003944 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003945 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003946 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003947 return this;
3948 }
3949
Joe Onoratocb109a02011-01-18 17:57:41 -08003950 /**
3951 * An intent to launch instead of posting the notification to the status bar.
3952 * Only for use with extremely high-priority notifications demanding the user's
3953 * <strong>immediate</strong> attention, such as an incoming phone call or
3954 * alarm clock that the user has explicitly set to a particular time.
3955 * If this facility is used for something else, please give the user an option
3956 * to turn it off and use a normal notification, as this can be extremely
3957 * disruptive.
3958 *
Chris Wren47c20a12014-06-18 17:27:29 -04003959 * <p>
3960 * The system UI may choose to display a heads-up notification, instead of
3961 * launching this intent, while the user is using the device.
3962 * </p>
Julia Reynolds47fd15f2018-11-28 10:16:00 -05003963 * <p>Apps targeting {@link Build.VERSION_CODES#Q} and above will have to request
3964 * a permission ({@link android.Manifest.permission#USE_FULL_SCREEN_INTENT}) in order to
3965 * use full screen intents.</p>
Chris Wren47c20a12014-06-18 17:27:29 -04003966 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003967 * @param intent The pending intent to launch.
3968 * @param highPriority Passing true will cause this notification to be sent
3969 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003970 *
3971 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003972 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003973 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003974 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003975 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003976 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3977 return this;
3978 }
3979
Joe Onoratocb109a02011-01-18 17:57:41 -08003980 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003981 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003982 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003983 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003984 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003985 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003986 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003987 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003988 return this;
3989 }
3990
Joe Onoratocb109a02011-01-18 17:57:41 -08003991 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003992 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003993 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003994 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003995 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003996 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003997 setTicker(tickerText);
3998 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003999 return this;
4000 }
4001
Joe Onoratocb109a02011-01-18 17:57:41 -08004002 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04004003 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004004 *
4005 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04004006 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
4007 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04004008 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004009 @NonNull
Dan Sandlerd63f9322015-05-06 15:18:49 -04004010 public Builder setLargeIcon(Bitmap b) {
4011 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
4012 }
4013
4014 /**
4015 * Add a large icon to the notification content view.
4016 *
4017 * In the platform template, this image will be shown on the left of the notification view
4018 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
4019 * badge atop the large icon).
4020 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004021 @NonNull
Dan Sandlerd63f9322015-05-06 15:18:49 -04004022 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004023 mN.mLargeIcon = icon;
4024 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08004025 return this;
4026 }
4027
Joe Onoratocb109a02011-01-18 17:57:41 -08004028 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004029 * Set the sound to play.
4030 *
John Spurlockc0650f022014-07-19 13:22:39 -04004031 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
4032 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004033 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004034 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08004035 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004036 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08004037 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004038 mN.sound = sound;
4039 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08004040 return this;
4041 }
4042
Joe Onoratocb109a02011-01-18 17:57:41 -08004043 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004044 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08004045 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004046 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
4047 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004048 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08004049 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07004050 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004051 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08004052 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004053 mN.sound = sound;
4054 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08004055 return this;
4056 }
4057
Joe Onoratocb109a02011-01-18 17:57:41 -08004058 /**
John Spurlockc0650f022014-07-19 13:22:39 -04004059 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
4060 * use during playback.
4061 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004062 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04004063 * @see Notification#sound
4064 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004065 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04004066 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004067 mN.sound = sound;
4068 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04004069 return this;
4070 }
4071
4072 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08004073 * Set the vibration pattern to use.
4074 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004075 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
4076 * <code>pattern</code> parameter.
4077 *
Chris Wren47c20a12014-06-18 17:27:29 -04004078 * <p>
4079 * A notification that vibrates is more likely to be presented as a heads-up notification.
4080 * </p>
4081 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004082 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004083 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08004084 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004085 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004086 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004087 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08004088 return this;
4089 }
4090
Joe Onoratocb109a02011-01-18 17:57:41 -08004091 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004092 * Set the desired color for the indicator LED on the device, as well as the
4093 * blink duty cycle (specified in milliseconds).
4094 *
4095
4096 * Not all devices will honor all (or even any) of these values.
4097 *
Julia Reynolds529e3322017-02-06 08:33:01 -05004098 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004099 * @see Notification#ledARGB
4100 * @see Notification#ledOnMS
4101 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08004102 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004103 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08004104 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004105 mN.ledARGB = argb;
4106 mN.ledOnMS = onMs;
4107 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004108 if (onMs != 0 || offMs != 0) {
4109 mN.flags |= FLAG_SHOW_LIGHTS;
4110 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004111 return this;
4112 }
4113
Joe Onoratocb109a02011-01-18 17:57:41 -08004114 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004115 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08004116 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004117
4118 * Ongoing notifications cannot be dismissed by the user, so your application or service
4119 * must take care of canceling them.
4120 *
4121
4122 * They are typically used to indicate a background task that the user is actively engaged
4123 * with (e.g., playing music) or is pending in some way and therefore occupying the device
4124 * (e.g., a file download, sync operation, active network connection).
4125 *
4126
4127 * @see Notification#FLAG_ONGOING_EVENT
Joe Onoratocb109a02011-01-18 17:57:41 -08004128 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004129 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08004130 public Builder setOngoing(boolean ongoing) {
4131 setFlag(FLAG_ONGOING_EVENT, ongoing);
4132 return this;
4133 }
4134
Joe Onoratocb109a02011-01-18 17:57:41 -08004135 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08004136 * Set whether this notification should be colorized. When set, the color set with
4137 * {@link #setColor(int)} will be used as the background color of this notification.
4138 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08004139 * This should only be used for high priority ongoing tasks like navigation, an ongoing
4140 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08004141 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07004142 * For most styles, the coloring will only be applied if the notification is for a
4143 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08004144 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07004145 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08004146 *
Aurimas Liutikas7f695332018-05-31 21:07:32 -07004147 * @see #setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08004148 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08004149 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004150 @NonNull
Selim Cinek7b9605b2017-01-19 17:36:00 -08004151 public Builder setColorized(boolean colorize) {
4152 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
4153 return this;
4154 }
4155
4156 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08004157 * Set this flag if you would only like the sound, vibrate
4158 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004159 *
4160 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08004161 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004162 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08004163 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
4164 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
4165 return this;
4166 }
4167
Joe Onoratocb109a02011-01-18 17:57:41 -08004168 /**
Julia Reynolds04499532016-09-13 14:04:53 -04004169 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004170 *
4171 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08004172 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004173 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08004174 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08004175 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08004176 return this;
4177 }
4178
Joe Onoratocb109a02011-01-18 17:57:41 -08004179 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08004180 * Set whether or not this notification should not bridge to other devices.
4181 *
4182 * <p>Some notifications can be bridged to other devices for remote display.
4183 * This hint can be set to recommend this notification not be bridged.
4184 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004185 @NonNull
Griff Hazendfcb0802014-02-11 12:00:00 -08004186 public Builder setLocalOnly(boolean localOnly) {
4187 setFlag(FLAG_LOCAL_ONLY, localOnly);
4188 return this;
4189 }
4190
4191 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004192 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08004193 * <p>
4194 * The value should be one or more of the following fields combined with
4195 * bitwise-or:
4196 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
4197 * <p>
4198 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004199 *
4200 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05004201 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004202 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08004203 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004204 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004205 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004206 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08004207 return this;
4208 }
4209
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004210 /**
4211 * Set the priority of this notification.
4212 *
4213 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004214 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004215 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004216 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07004217 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004218 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004219 return this;
4220 }
Joe Malin8d40d042012-11-05 11:36:40 -08004221
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004222 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04004223 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08004224 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04004225 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004226 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004227 @NonNull
John Spurlockfd7f1e02014-03-18 16:41:57 -04004228 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004229 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004230 return this;
4231 }
4232
4233 /**
Chris Wrendde75302014-03-26 17:24:15 -04004234 * Add a person that is relevant to this notification.
4235 *
Chris Wrene6c48932014-09-29 17:19:27 -04004236 * <P>
4237 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04004238 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4239 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4240 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04004241 * </P>
4242 *
4243 * <P>
4244 * The person should be specified by the {@code String} representation of a
4245 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
4246 * </P>
4247 *
4248 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
4249 * URIs. The path part of these URIs must exist in the contacts database, in the
4250 * appropriate column, or the reference will be discarded as invalid. Telephone schema
4251 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
Selim Cineke7238dd2017-12-14 17:48:32 -08004252 * It is also possible to provide a URI with the schema {@code name:} in order to uniquely
4253 * identify a person without an entry in the contacts database.
Chris Wrene6c48932014-09-29 17:19:27 -04004254 * </P>
4255 *
4256 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04004257 * @see Notification#EXTRA_PEOPLE
Selim Cineke7238dd2017-12-14 17:48:32 -08004258 * @deprecated use {@link #addPerson(Person)}
Chris Wrendde75302014-03-26 17:24:15 -04004259 */
Chris Wrene6c48932014-09-29 17:19:27 -04004260 public Builder addPerson(String uri) {
Selim Cinek9acd6732018-03-23 16:39:02 -07004261 addPerson(new Person.Builder().setUri(uri).build());
Selim Cineke7238dd2017-12-14 17:48:32 -08004262 return this;
4263 }
4264
4265 /**
4266 * Add a person that is relevant to this notification.
4267 *
4268 * <P>
4269 * Depending on user preferences, this annotation may allow the notification to pass
4270 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4271 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4272 * appear more prominently in the user interface.
4273 * </P>
4274 *
4275 * <P>
4276 * A person should usually contain a uri in order to benefit from the ranking boost.
4277 * However, even if no uri is provided, it's beneficial to provide other people in the
4278 * notification, such that listeners and voice only devices can announce and handle them
4279 * properly.
4280 * </P>
4281 *
4282 * @param person the person to add.
4283 * @see Notification#EXTRA_PEOPLE_LIST
4284 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004285 @NonNull
Selim Cineke7238dd2017-12-14 17:48:32 -08004286 public Builder addPerson(Person person) {
4287 mPersonList.add(person);
Chris Wrendde75302014-03-26 17:24:15 -04004288 return this;
4289 }
4290
4291 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004292 * Set this notification to be part of a group of notifications sharing the same key.
4293 * Grouped notifications may display in a cluster or stack on devices which
4294 * support such rendering.
4295 *
4296 * <p>To make this notification the summary for its group, also call
4297 * {@link #setGroupSummary}. A sort order can be specified for group members by using
4298 * {@link #setSortKey}.
4299 * @param groupKey The group key of the group.
4300 * @return this object for method chaining
4301 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004302 @NonNull
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004303 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004304 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004305 return this;
4306 }
4307
4308 /**
4309 * Set this notification to be the group summary for a group of notifications.
4310 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04004311 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
4312 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004313 * @param isGroupSummary Whether this notification should be a group summary.
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 setGroupSummary(boolean isGroupSummary) {
4318 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
4319 return this;
4320 }
4321
4322 /**
4323 * Set a sort key that orders this notification among other notifications from the
4324 * same package. This can be useful if an external sort was already applied and an app
4325 * would like to preserve this. Notifications will be sorted lexicographically using this
4326 * value, although providing different priorities in addition to providing sort key may
4327 * cause this value to be ignored.
4328 *
4329 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07004330 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004331 *
4332 * @see String#compareTo(String)
4333 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004334 @NonNull
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004335 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004336 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004337 return this;
4338 }
4339
4340 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004341 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004342 *
Griff Hazen720042b2014-02-24 15:46:56 -08004343 * <p>Values within the Bundle will replace existing extras values in this Builder.
4344 *
4345 * @see Notification#extras
4346 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004347 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07004348 public Builder addExtras(Bundle extras) {
4349 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004350 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08004351 }
4352 return this;
4353 }
4354
4355 /**
4356 * Set metadata for this notification.
4357 *
4358 * <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 -04004359 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004360 * called.
4361 *
Griff Hazen720042b2014-02-24 15:46:56 -08004362 * <p>Replaces any existing extras values with those from the provided Bundle.
4363 * Use {@link #addExtras} to merge in metadata instead.
4364 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004365 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004366 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004367 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07004368 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004369 if (extras != null) {
4370 mUserExtras = extras;
4371 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004372 return this;
4373 }
4374
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004375 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004376 * Get the current metadata Bundle used by this notification Builder.
4377 *
4378 * <p>The returned Bundle is shared with this Builder.
4379 *
4380 * <p>The current contents of this Bundle are copied into the Notification each time
4381 * {@link #build()} is called.
4382 *
4383 * @see Notification#extras
4384 */
4385 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004386 return mUserExtras;
4387 }
4388
4389 private Bundle getAllExtras() {
4390 final Bundle saveExtras = (Bundle) mUserExtras.clone();
4391 saveExtras.putAll(mN.extras);
4392 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08004393 }
4394
4395 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004396 * Add an action to this notification. Actions are typically displayed by
4397 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004398 * <p>
4399 * Every action must have an icon (32dp square and matching the
4400 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4401 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4402 * <p>
4403 * A notification in its expanded form can display up to 3 actions, from left to right in
4404 * the order they were added. Actions will not be displayed when the notification is
4405 * collapsed, however, so be sure that any essential functions may be accessed by the user
4406 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004407 *
4408 * @param icon Resource ID of a drawable that represents the action.
4409 * @param title Text describing the action.
4410 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04004411 *
4412 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004413 */
Dan Sandler86647982015-05-13 23:41:13 -04004414 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004415 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004416 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004417 return this;
4418 }
4419
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004420 /**
Griff Hazen959591e2014-05-15 22:26:18 -07004421 * Add an action to this notification. Actions are typically displayed by
4422 * the system as a button adjacent to the notification content.
4423 * <p>
4424 * Every action must have an icon (32dp square and matching the
4425 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4426 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4427 * <p>
4428 * A notification in its expanded form can display up to 3 actions, from left to right in
4429 * the order they were added. Actions will not be displayed when the notification is
4430 * collapsed, however, so be sure that any essential functions may be accessed by the user
4431 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
4432 *
4433 * @param action The action to add.
4434 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004435 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07004436 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08004437 if (action != null) {
4438 mActions.add(action);
4439 }
Griff Hazen959591e2014-05-15 22:26:18 -07004440 return this;
4441 }
4442
4443 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004444 * Alter the complete list of actions attached to this notification.
4445 * @see #addAction(Action).
4446 *
4447 * @param actions
4448 * @return
4449 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004450 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004451 public Builder setActions(Action... actions) {
4452 mActions.clear();
4453 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08004454 if (actions[i] != null) {
4455 mActions.add(actions[i]);
4456 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004457 }
4458 return this;
4459 }
4460
4461 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004462 * Add a rich notification style to be applied at build time.
4463 *
4464 * @param style Object responsible for modifying the notification style.
4465 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004466 @NonNull
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004467 public Builder setStyle(Style style) {
4468 if (mStyle != style) {
4469 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004470 if (mStyle != null) {
4471 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004472 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
4473 } else {
4474 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004475 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004476 }
4477 return this;
4478 }
4479
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004480 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05004481 * Returns the style set by {@link #setStyle(Style)}.
4482 */
4483 public Style getStyle() {
4484 return mStyle;
4485 }
4486
4487 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004488 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07004489 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004490 * @return The same Builder.
4491 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004492 @NonNull
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06004493 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004494 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004495 return this;
4496 }
4497
4498 /**
4499 * Supply a replacement Notification whose contents should be shown in insecure contexts
4500 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
4501 * @param n A replacement notification, presumably with some or all info redacted.
4502 * @return The same Builder.
4503 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004504 @NonNull
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004505 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004506 if (n != null) {
4507 mN.publicVersion = new Notification();
4508 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
4509 } else {
4510 mN.publicVersion = null;
4511 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004512 return this;
4513 }
4514
Griff Hazenb720abe2014-05-20 13:15:30 -07004515 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004516 * Apply an extender to this notification builder. Extenders may be used to add
4517 * metadata or change options on this builder.
4518 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004519 @NonNull
Griff Hazen61a9e862014-05-22 16:05:19 -07004520 public Builder extend(Extender extender) {
4521 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004522 return this;
4523 }
4524
Dan Sandler4e787062015-06-17 15:09:48 -04004525 /**
4526 * @hide
4527 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004528 @NonNull
Julia Reynoldse46bb372016-03-17 11:05:58 -04004529 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08004530 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004531 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004532 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004533 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004534 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04004535 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08004536 }
4537
Dan Sandler26e81cf2014-05-06 10:01:27 -04004538 /**
4539 * Sets {@link Notification#color}.
4540 *
4541 * @param argb The accent color to use
4542 *
4543 * @return The same Builder.
4544 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004545 @NonNull
Tor Norbye80756e32015-03-02 09:39:27 -08004546 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004547 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004548 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04004549 return this;
4550 }
4551
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004552 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04004553 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
4554 // This user can never be a badged profile,
4555 // and also includes USER_ALL system notifications.
4556 return null;
4557 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02004558 // Note: This assumes that the current user can read the profile badge of the
4559 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08004560 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05004561 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004562 }
4563
4564 private Bitmap getProfileBadge() {
4565 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01004566 if (badge == null) {
4567 return null;
4568 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004569 final int size = mContext.getResources().getDimensionPixelSize(
4570 R.dimen.notification_badge_size);
4571 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004572 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004573 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004574 badge.draw(canvas);
4575 return bitmap;
4576 }
4577
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004578 private void bindProfileBadge(RemoteViews contentView, StandardTemplateParams p) {
Kenny Guy98193ea2014-07-24 19:54:37 +01004579 Bitmap profileBadge = getProfileBadge();
4580
Kenny Guy98193ea2014-07-24 19:54:37 +01004581 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004582 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
4583 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004584 if (isColorized(p)) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004585 contentView.setDrawableTint(R.id.profile_badge, false,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004586 getPrimaryTextColor(p), PorterDuff.Mode.SRC_ATOP);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004587 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004588 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004589 }
4590
Gus Prevasbae5ba32018-11-28 15:45:13 -05004591 private void bindAlertedIcon(RemoteViews contentView, StandardTemplateParams p) {
4592 contentView.setDrawableTint(
4593 R.id.alerted_icon,
4594 false /* targetBackground */,
4595 getNeutralColor(p),
4596 PorterDuff.Mode.SRC_ATOP);
4597 }
4598
Julia Reynoldsfc640012018-02-21 12:25:27 -05004599 /**
4600 * @hide
4601 */
4602 public boolean usesStandardHeader() {
4603 if (mN.mUsesStandardHeader) {
4604 return true;
4605 }
4606 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.N) {
4607 if (mN.contentView == null && mN.bigContentView == null) {
4608 return true;
4609 }
4610 }
4611 boolean contentViewUsesHeader = mN.contentView == null
4612 || STANDARD_LAYOUTS.contains(mN.contentView.getLayoutId());
4613 boolean bigContentViewUsesHeader = mN.bigContentView == null
4614 || STANDARD_LAYOUTS.contains(mN.bigContentView.getLayoutId());
4615 return contentViewUsesHeader && bigContentViewUsesHeader;
4616 }
4617
Christoph Studerfe718432014-09-01 18:21:18 +02004618 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004619 resetNotificationHeader(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02004620 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08004621 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004622 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08004623 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004624 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08004625 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08004626 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004627 }
4628
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004629 /**
4630 * Resets the notification header to its original state
4631 */
4632 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07004633 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
4634 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08004635 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004636 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02004637 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004638 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07004639 contentView.setTextViewText(R.id.header_text, null);
Selim Cinekafeed292017-12-12 17:32:44 -08004640 contentView.setViewVisibility(R.id.header_text_secondary, View.GONE);
4641 contentView.setTextViewText(R.id.header_text_secondary, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004642 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinekafeed292017-12-12 17:32:44 -08004643 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004644 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004645 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004646 contentView.setImageViewIcon(R.id.profile_badge, null);
4647 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Gus Prevasa3226492018-10-23 11:10:09 -04004648 contentView.setViewVisibility(R.id.alerted_icon, View.GONE);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004649 mN.mUsesStandardHeader = false;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004650 }
4651
Selim Cinek384804b2018-04-18 14:31:07 +08004652 private RemoteViews applyStandardTemplate(int resId, TemplateBindResult result) {
4653 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this),
4654 result);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004655 }
4656
Selim Cinek384804b2018-04-18 14:31:07 +08004657 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p,
4658 TemplateBindResult result) {
Kenny Guy77320062014-08-27 21:37:15 +01004659 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04004660
Christoph Studerfe718432014-09-01 18:21:18 +02004661 resetStandardTemplate(contentView);
4662
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004663 final Bundle ex = mN.extras;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004664 updateBackgroundColor(contentView, p);
4665 bindNotificationHeader(contentView, p);
Selim Cinek384804b2018-04-18 14:31:07 +08004666 bindLargeIconAndReply(contentView, p, result);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004667 boolean showProgress = handleProgressBar(contentView, ex, p);
Adrian Roos70d7aa32017-01-11 15:39:06 -08004668 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08004669 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004670 contentView.setTextViewText(R.id.title, processTextSpans(p.title));
Lucas Dupin00be88f2019-01-03 17:50:52 -08004671 setTextViewColorPrimary(contentView, R.id.title, p);
Selim Cinek954cc232016-05-20 13:29:23 -07004672 contentView.setViewLayoutWidth(R.id.title, showProgress
4673 ? ViewGroup.LayoutParams.WRAP_CONTENT
4674 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08004675 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08004676 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08004677 int textId = showProgress ? com.android.internal.R.id.text_line_1
4678 : com.android.internal.R.id.text;
Selim Cinek48f66b72017-08-18 16:17:51 -07004679 contentView.setTextViewText(textId, processTextSpans(p.text));
Lucas Dupin00be88f2019-01-03 17:50:52 -08004680 setTextViewColorSecondary(contentView, textId, p);
Selim Cinek41598732016-01-11 16:58:37 -08004681 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08004682 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08004683
Selim Cinek279fa862016-06-14 10:57:25 -07004684 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004685
Selim Cinek29603462015-11-17 19:04:39 -08004686 return contentView;
4687 }
4688
Selim Cinek48f66b72017-08-18 16:17:51 -07004689 private CharSequence processTextSpans(CharSequence text) {
Lucas Dupind3c99322018-09-27 10:22:29 -07004690 if (hasForegroundColor() || mInNightMode) {
Lucas Dupina291d192018-06-07 13:59:42 -07004691 return ContrastColorUtil.clearColorSpans(text);
Selim Cinek48f66b72017-08-18 16:17:51 -07004692 }
4693 return text;
4694 }
4695
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004696 private void setTextViewColorPrimary(RemoteViews contentView, int id,
4697 StandardTemplateParams p) {
4698 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004699 contentView.setTextColor(id, mPrimaryTextColor);
4700 }
4701
Selim Cinek48f66b72017-08-18 16:17:51 -07004702 private boolean hasForegroundColor() {
4703 return mForegroundColor != COLOR_INVALID;
4704 }
4705
Selim Cinek389edcd2017-05-11 19:16:44 -07004706 /**
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004707 * Return the primary text color using the existing template params
Selim Cinek389edcd2017-05-11 19:16:44 -07004708 * @hide
4709 */
4710 @VisibleForTesting
4711 public int getPrimaryTextColor() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004712 return getPrimaryTextColor(mParams);
4713 }
4714
4715 /**
4716 * @param p the template params to inflate this with
4717 * @return the primary text color
4718 * @hide
4719 */
4720 @VisibleForTesting
4721 public int getPrimaryTextColor(StandardTemplateParams p) {
4722 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004723 return mPrimaryTextColor;
4724 }
4725
Selim Cinek389edcd2017-05-11 19:16:44 -07004726 /**
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004727 * Return the secondary text color using the existing template params
Selim Cinek389edcd2017-05-11 19:16:44 -07004728 * @hide
4729 */
4730 @VisibleForTesting
4731 public int getSecondaryTextColor() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004732 return getSecondaryTextColor(mParams);
4733 }
4734
4735 /**
4736 * @param p the template params to inflate this with
4737 * @return the secondary text color
4738 * @hide
4739 */
4740 @VisibleForTesting
4741 public int getSecondaryTextColor(StandardTemplateParams p) {
4742 ensureColors(p);
Selim Cinek389edcd2017-05-11 19:16:44 -07004743 return mSecondaryTextColor;
4744 }
4745
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004746 private void setTextViewColorSecondary(RemoteViews contentView, int id,
4747 StandardTemplateParams p) {
4748 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004749 contentView.setTextColor(id, mSecondaryTextColor);
4750 }
4751
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004752 private void ensureColors(StandardTemplateParams p) {
4753 int backgroundColor = getBackgroundColor(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004754 if (mPrimaryTextColor == COLOR_INVALID
4755 || mSecondaryTextColor == COLOR_INVALID
Selim Cinek7b9605b2017-01-19 17:36:00 -08004756 || mTextColorsAreForBackground != backgroundColor) {
4757 mTextColorsAreForBackground = backgroundColor;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004758 if (!hasForegroundColor() || !isColorized(p)) {
Lucas Dupina291d192018-06-07 13:59:42 -07004759 mPrimaryTextColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004760 backgroundColor, mInNightMode);
Lucas Dupina291d192018-06-07 13:59:42 -07004761 mSecondaryTextColor = ContrastColorUtil.resolveSecondaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004762 backgroundColor, mInNightMode);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004763 if (backgroundColor != COLOR_DEFAULT && isColorized(p)) {
Lucas Dupina291d192018-06-07 13:59:42 -07004764 mPrimaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004765 mPrimaryTextColor, backgroundColor, 4.5);
Lucas Dupina291d192018-06-07 13:59:42 -07004766 mSecondaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004767 mSecondaryTextColor, backgroundColor, 4.5);
4768 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07004769 } else {
Lucas Dupina291d192018-06-07 13:59:42 -07004770 double backLum = ContrastColorUtil.calculateLuminance(backgroundColor);
4771 double textLum = ContrastColorUtil.calculateLuminance(mForegroundColor);
4772 double contrast = ContrastColorUtil.calculateContrast(mForegroundColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004773 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07004774 // We only respect the given colors if worst case Black or White still has
4775 // contrast
4776 boolean backgroundLight = backLum > textLum
4777 && satisfiesTextContrast(backgroundColor, Color.BLACK)
4778 || backLum <= textLum
4779 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004780 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07004781 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004782 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004783 mForegroundColor,
4784 backgroundColor,
4785 true /* findFG */,
4786 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004787 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004788 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004789 } else {
4790 mSecondaryTextColor =
Lucas Dupina291d192018-06-07 13:59:42 -07004791 ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004792 mForegroundColor,
4793 backgroundColor,
4794 true /* findFG */,
4795 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004796 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004797 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004798 }
4799 } else {
4800 mPrimaryTextColor = mForegroundColor;
Lucas Dupina291d192018-06-07 13:59:42 -07004801 mSecondaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004802 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4803 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Lucas Dupina291d192018-06-07 13:59:42 -07004804 if (ContrastColorUtil.calculateContrast(mSecondaryTextColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004805 backgroundColor) < 4.5f) {
4806 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07004807 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004808 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004809 mSecondaryTextColor,
4810 backgroundColor,
4811 true /* findFG */,
4812 4.5f);
4813 } else {
4814 mSecondaryTextColor
Lucas Dupina291d192018-06-07 13:59:42 -07004815 = ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004816 mSecondaryTextColor,
4817 backgroundColor,
4818 true /* findFG */,
4819 4.5f);
4820 }
Lucas Dupina291d192018-06-07 13:59:42 -07004821 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004822 mSecondaryTextColor, backgroundLight
4823 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4824 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004825 }
4826 }
4827 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004828 }
4829 }
4830
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004831 private void updateBackgroundColor(RemoteViews contentView,
4832 StandardTemplateParams p) {
4833 if (isColorized(p)) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004834 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004835 getBackgroundColor(p));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004836 } else {
4837 // Clear it!
4838 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4839 0);
4840 }
4841 }
4842
Selim Cinek860b6da2015-12-16 19:02:19 -08004843 /**
4844 * @param remoteView the remote view to update the minheight in
4845 * @param hasMinHeight does it have a mimHeight
4846 * @hide
4847 */
4848 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4849 int minHeight = 0;
4850 if (hasMinHeight) {
4851 // we need to set the minHeight of the notification
4852 minHeight = mContext.getResources().getDimensionPixelSize(
4853 com.android.internal.R.dimen.notification_min_content_height);
4854 }
4855 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4856 }
4857
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004858 private boolean handleProgressBar(RemoteViews contentView, Bundle ex,
4859 StandardTemplateParams p) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004860 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4861 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4862 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004863 if (p.hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004864 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004865 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004866 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004867 contentView.setProgressBackgroundTintList(
4868 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4869 R.color.notification_progress_background_color)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004870 if (getRawColor(p) != COLOR_DEFAULT) {
4871 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor(p));
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004872 contentView.setProgressTintList(R.id.progress, colorStateList);
4873 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004874 }
Selim Cinek29603462015-11-17 19:04:39 -08004875 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004876 } else {
4877 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004878 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004879 }
Joe Onorato561d3852010-11-20 18:09:34 -08004880 }
4881
Selim Cinek384804b2018-04-18 14:31:07 +08004882 private void bindLargeIconAndReply(RemoteViews contentView, StandardTemplateParams p,
4883 TemplateBindResult result) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004884 boolean largeIconShown = bindLargeIcon(contentView, p);
4885 boolean replyIconShown = bindReplyIcon(contentView, p);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004886 boolean iconContainerVisible = largeIconShown || replyIconShown;
Selim Cinek384804b2018-04-18 14:31:07 +08004887 contentView.setViewVisibility(R.id.right_icon_container,
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004888 iconContainerVisible ? View.VISIBLE : View.GONE);
Selim Cinek1c72fa02018-04-23 18:00:54 +08004889 int marginEnd = calculateMarginEnd(largeIconShown, replyIconShown);
4890 contentView.setViewLayoutMarginEnd(R.id.line1, marginEnd);
4891 contentView.setViewLayoutMarginEnd(R.id.text, marginEnd);
4892 contentView.setViewLayoutMarginEnd(R.id.progress, marginEnd);
Selim Cinek384804b2018-04-18 14:31:07 +08004893 if (result != null) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08004894 result.setIconMarginEnd(marginEnd);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004895 result.setRightIconContainerVisible(iconContainerVisible);
Selim Cinek384804b2018-04-18 14:31:07 +08004896 }
4897 }
4898
Selim Cinek1c72fa02018-04-23 18:00:54 +08004899 private int calculateMarginEnd(boolean largeIconShown, boolean replyIconShown) {
4900 int marginEnd = 0;
4901 int contentMargin = mContext.getResources().getDimensionPixelSize(
4902 R.dimen.notification_content_margin_end);
4903 int iconSize = mContext.getResources().getDimensionPixelSize(
4904 R.dimen.notification_right_icon_size);
4905 if (replyIconShown) {
4906 // The size of the reply icon
4907 marginEnd += iconSize;
4908
4909 int replyInset = mContext.getResources().getDimensionPixelSize(
4910 R.dimen.notification_reply_inset);
4911 // We're subtracting the inset of the reply icon to make sure it's
4912 // aligned nicely on the right, and remove it from the following padding
4913 marginEnd -= replyInset * 2;
4914 }
4915 if (largeIconShown) {
4916 // adding size of the right icon
4917 marginEnd += iconSize;
4918
4919 if (replyIconShown) {
4920 // We also add some padding to the reply icon if it's around
4921 marginEnd += contentMargin;
4922 }
4923 }
4924 if (replyIconShown || largeIconShown) {
4925 // The padding to the content
4926 marginEnd += contentMargin;
4927 }
4928 return marginEnd;
4929 }
4930
Selim Cinek384804b2018-04-18 14:31:07 +08004931 /**
4932 * Bind the large icon.
4933 * @return if the largeIcon is visible
4934 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004935 private boolean bindLargeIcon(RemoteViews contentView, StandardTemplateParams p) {
Selim Cinek279fa862016-06-14 10:57:25 -07004936 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4937 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4938 }
Lucas Dupin00be88f2019-01-03 17:50:52 -08004939 boolean showLargeIcon = mN.mLargeIcon != null && !p.hideLargeIcon;
Selim Cinek88188f22017-09-19 16:46:56 -07004940 if (showLargeIcon) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004941 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4942 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004943 processLargeLegacyIcon(mN.mLargeIcon, contentView, p);
Selim Cinek88188f22017-09-19 16:46:56 -07004944 }
Selim Cinek384804b2018-04-18 14:31:07 +08004945 return showLargeIcon;
4946 }
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004947
Selim Cinek384804b2018-04-18 14:31:07 +08004948 /**
4949 * Bind the reply icon.
4950 * @return if the reply icon is visible
4951 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004952 private boolean bindReplyIcon(RemoteViews contentView, StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08004953 boolean actionVisible = !p.hideReplyIcon;
Selim Cinek384804b2018-04-18 14:31:07 +08004954 Action action = null;
Selim Cinek88188f22017-09-19 16:46:56 -07004955 if (actionVisible) {
Selim Cinek384804b2018-04-18 14:31:07 +08004956 action = findReplyAction();
4957 actionVisible = action != null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004958 }
Selim Cinek384804b2018-04-18 14:31:07 +08004959 if (actionVisible) {
4960 contentView.setViewVisibility(R.id.reply_icon_action, View.VISIBLE);
4961 contentView.setDrawableTint(R.id.reply_icon_action,
4962 false /* targetBackground */,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004963 getNeutralColor(p),
Selim Cinek384804b2018-04-18 14:31:07 +08004964 PorterDuff.Mode.SRC_ATOP);
4965 contentView.setOnClickPendingIntent(R.id.reply_icon_action, action.actionIntent);
4966 contentView.setRemoteInputs(R.id.reply_icon_action, action.mRemoteInputs);
4967 } else {
4968 contentView.setRemoteInputs(R.id.reply_icon_action, null);
4969 }
4970 contentView.setViewVisibility(R.id.reply_icon_action,
4971 actionVisible ? View.VISIBLE : View.GONE);
4972 return actionVisible;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004973 }
4974
4975 private Action findReplyAction() {
4976 ArrayList<Action> actions = mActions;
4977 if (mOriginalActions != null) {
4978 actions = mOriginalActions;
4979 }
4980 int numActions = actions.size();
4981 for (int i = 0; i < numActions; i++) {
4982 Action action = actions.get(i);
4983 if (hasValidRemoteInput(action)) {
4984 return action;
4985 }
4986 }
4987 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004988 }
4989
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004990 private void bindNotificationHeader(RemoteViews contentView, StandardTemplateParams p) {
4991 bindSmallIcon(contentView, p);
4992 bindHeaderAppName(contentView, p);
Lucas Dupin00be88f2019-01-03 17:50:52 -08004993 bindHeaderText(contentView, p);
4994 bindHeaderTextSecondary(contentView, p);
4995 bindHeaderChronometerAndTime(contentView, p);
4996 bindProfileBadge(contentView, p);
4997 bindAlertedIcon(contentView, p);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004998 bindActivePermissions(contentView, p);
4999 bindExpandButton(contentView, p);
Julia Reynoldsfc640012018-02-21 12:25:27 -05005000 mN.mUsesStandardHeader = true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005001 }
5002
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005003 private void bindActivePermissions(RemoteViews contentView, StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005004 int color = getNeutralColor(p);
Julia Reynoldsb887b232018-04-10 16:38:08 -04005005 contentView.setDrawableTint(R.id.camera, false, color, PorterDuff.Mode.SRC_ATOP);
5006 contentView.setDrawableTint(R.id.mic, false, color, PorterDuff.Mode.SRC_ATOP);
5007 contentView.setDrawableTint(R.id.overlay, false, color, PorterDuff.Mode.SRC_ATOP);
5008 }
5009
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005010 private void bindExpandButton(RemoteViews contentView, StandardTemplateParams p) {
5011 int color = isColorized(p) ? getPrimaryTextColor(p) : getSecondaryTextColor(p);
Sunny Goyal5b153922017-09-21 21:00:36 -07005012 contentView.setDrawableTint(R.id.expand_button, false, color,
5013 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005014 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08005015 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005016 }
5017
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005018 private void bindHeaderChronometerAndTime(RemoteViews contentView,
5019 StandardTemplateParams p) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005020 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08005021 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005022 setTextViewColorSecondary(contentView, R.id.time_divider, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005023 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
5024 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
5025 contentView.setLong(R.id.chronometer, "setBase",
5026 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
5027 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07005028 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07005029 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005030 setTextViewColorSecondary(contentView, R.id.chronometer, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005031 } else {
5032 contentView.setViewVisibility(R.id.time, View.VISIBLE);
5033 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005034 setTextViewColorSecondary(contentView, R.id.time, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005035 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005036 } else {
5037 // We still want a time to be set but gone, such that we can show and hide it
5038 // on demand in case it's a child notification without anything in the header
5039 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005040 }
5041 }
5042
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005043 private void bindHeaderText(RemoteViews contentView, StandardTemplateParams p) {
5044 CharSequence summaryText = p.summaryText;
5045 if (summaryText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05005046 && mStyle.hasSummaryInHeader()) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005047 summaryText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05005048 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005049 if (summaryText == null
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07005050 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
5051 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005052 summaryText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07005053 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005054 if (summaryText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005055 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek48f66b72017-08-18 16:17:51 -07005056 contentView.setTextViewText(R.id.header_text, processTextSpans(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005057 processLegacyText(summaryText)));
5058 setTextViewColorSecondary(contentView, R.id.header_text, p);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07005059 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
5060 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005061 setTextViewColorSecondary(contentView, R.id.header_text_divider, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005062 }
5063 }
5064
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005065 private void bindHeaderTextSecondary(RemoteViews contentView, StandardTemplateParams p) {
5066 if (!TextUtils.isEmpty(p.headerTextSecondary)) {
Selim Cinekafeed292017-12-12 17:32:44 -08005067 contentView.setTextViewText(R.id.header_text_secondary, processTextSpans(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005068 processLegacyText(p.headerTextSecondary)));
5069 setTextViewColorSecondary(contentView, R.id.header_text_secondary, p);
Selim Cinekafeed292017-12-12 17:32:44 -08005070 contentView.setViewVisibility(R.id.header_text_secondary, View.VISIBLE);
5071 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005072 setTextViewColorSecondary(contentView, R.id.header_text_secondary_divider, p);
Selim Cinekafeed292017-12-12 17:32:44 -08005073 }
5074 }
5075
Adrian Rooseba05822016-04-22 17:09:27 -07005076 /**
5077 * @hide
5078 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005079 @UnsupportedAppUsage
Adrian Rooseba05822016-04-22 17:09:27 -07005080 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04005081 CharSequence name = null;
5082 final PackageManager pm = mContext.getPackageManager();
5083 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
5084 // only system packages which lump together a bunch of unrelated stuff
5085 // may substitute a different name to make the purpose of the
5086 // notification more clear. the correct package label should always
5087 // be accessible via SystemUI.
5088 final String pkg = mContext.getPackageName();
5089 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
5090 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
5091 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
5092 name = subName;
5093 } else {
5094 Log.w(TAG, "warning: pkg "
5095 + pkg + " attempting to substitute app name '" + subName
5096 + "' without holding perm "
5097 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
5098 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005099 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04005100 if (TextUtils.isEmpty(name)) {
5101 name = pm.getApplicationLabel(mContext.getApplicationInfo());
5102 }
5103 if (TextUtils.isEmpty(name)) {
5104 // still nothing?
5105 return null;
5106 }
5107
5108 return String.valueOf(name);
5109 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005110 private void bindHeaderAppName(RemoteViews contentView, StandardTemplateParams p) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04005111 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005112 if (isColorized(p)) {
5113 setTextViewColorPrimary(contentView, R.id.app_name_text, p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005114 } else {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005115 contentView.setTextColor(R.id.app_name_text, getSecondaryTextColor(p));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005116 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005117 }
5118
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005119 private boolean isColorized(StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005120 return p.allowColorization && mN.isColorized();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005121 }
5122
5123 private void bindSmallIcon(RemoteViews contentView, StandardTemplateParams p) {
Selim Cinek279fa862016-06-14 10:57:25 -07005124 if (mN.mSmallIcon == null && mN.icon != 0) {
5125 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
5126 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005127 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Sunny Goyal5b153922017-09-21 21:00:36 -07005128 contentView.setInt(R.id.icon, "setImageLevel", mN.iconLevel);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005129 processSmallIconColor(mN.mSmallIcon, contentView, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005130 }
5131
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005132 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005133 * @return true if the built notification will show the time or the chronometer; false
5134 * otherwise
5135 */
5136 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07005137 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005138 }
5139
Christoph Studerfe718432014-09-01 18:21:18 +02005140 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07005141 // actions_container is only reset when there are no actions to avoid focus issues with
5142 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02005143 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02005144 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08005145
5146 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
5147 big.setTextViewText(R.id.notification_material_reply_text_1, null);
Kenny Guya0f6de82018-04-06 16:20:16 +01005148 big.setViewVisibility(R.id.notification_material_reply_text_1_container, View.GONE);
5149 big.setViewVisibility(R.id.notification_material_reply_progress, View.GONE);
Adrian Roose458aa82015-12-08 16:17:19 -08005150
5151 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
5152 big.setTextViewText(R.id.notification_material_reply_text_2, null);
5153 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
5154 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07005155
Selim Cineked64a142018-02-06 18:06:01 -08005156 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
5157 R.dimen.notification_content_margin);
Christoph Studerfe718432014-09-01 18:21:18 +02005158 }
5159
Selim Cinek384804b2018-04-18 14:31:07 +08005160 private RemoteViews applyStandardTemplateWithActions(int layoutId,
5161 TemplateBindResult result) {
5162 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this),
5163 result);
Adrian Roos48d746a2016-04-12 14:57:28 -07005164 }
5165
Gustav Sennton1463d832018-11-06 16:12:48 +00005166 private static List<Notification.Action> filterOutContextualActions(
5167 List<Notification.Action> actions) {
5168 List<Notification.Action> nonContextualActions = new ArrayList<>();
5169 for (Notification.Action action : actions) {
Gustav Sennton005d7a02019-01-04 13:41:32 +00005170 if (!action.isContextual()) {
Gustav Sennton1463d832018-11-06 16:12:48 +00005171 nonContextualActions.add(action);
5172 }
5173 }
5174 return nonContextualActions;
5175 }
5176
Adrian Roos70d7aa32017-01-11 15:39:06 -08005177 private RemoteViews applyStandardTemplateWithActions(int layoutId,
Selim Cinek384804b2018-04-18 14:31:07 +08005178 StandardTemplateParams p, TemplateBindResult result) {
5179 RemoteViews big = applyStandardTemplate(layoutId, p, result);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005180
Christoph Studerfe718432014-09-01 18:21:18 +02005181 resetStandardTemplateWithActions(big);
5182
Adrian Roose458aa82015-12-08 16:17:19 -08005183 boolean validRemoteInput = false;
5184
Gustav Sennton1463d832018-11-06 16:12:48 +00005185 // In the UI contextual actions appear separately from the standard actions, so we
5186 // filter them out here.
5187 List<Notification.Action> nonContextualActions = filterOutContextualActions(mActions);
5188
5189 int N = nonContextualActions.size();
Lucas Dupin00be88f2019-01-03 17:50:52 -08005190 boolean emphazisedMode = mN.fullScreenIntent != null;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005191 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005192 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08005193 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005194 big.setViewVisibility(R.id.actions, View.VISIBLE);
Selim Cineked64a142018-02-06 18:06:01 -08005195 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Daniel Sandler8680bf82012-05-15 16:52:52 -04005196 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005197 for (int i=0; i<N; i++) {
Gustav Sennton1463d832018-11-06 16:12:48 +00005198 Action action = nonContextualActions.get(i);
5199
Selim Cinekffcc7cf2018-02-06 17:43:51 -08005200 boolean actionHasValidInput = hasValidRemoteInput(action);
5201 validRemoteInput |= actionHasValidInput;
Adrian Roose458aa82015-12-08 16:17:19 -08005202
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005203 final RemoteViews button = generateActionButton(action, emphazisedMode, p);
Selim Cinek396caca2018-04-10 17:46:46 -07005204 if (actionHasValidInput && !emphazisedMode) {
Selim Cinekffcc7cf2018-02-06 17:43:51 -08005205 // Clear the drawable
5206 button.setInt(R.id.action0, "setBackgroundResource", 0);
5207 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005208 big.addView(R.id.actions, button);
5209 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07005210 } else {
5211 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005212 }
Adrian Roose458aa82015-12-08 16:17:19 -08005213
5214 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Lucas Dupin00be88f2019-01-03 17:50:52 -08005215 if (validRemoteInput && replyText != null
Selim Cinekbee4e072018-05-21 22:06:43 -07005216 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])
5217 && p.maxRemoteInputHistory > 0) {
Kenny Guya0f6de82018-04-06 16:20:16 +01005218 boolean showSpinner = mN.extras.getBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER);
Adrian Roose458aa82015-12-08 16:17:19 -08005219 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Kenny Guya0f6de82018-04-06 16:20:16 +01005220 big.setViewVisibility(R.id.notification_material_reply_text_1_container,
5221 View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005222 big.setTextViewText(R.id.notification_material_reply_text_1,
5223 processTextSpans(replyText[0]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005224 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1, p);
Kenny Guya0f6de82018-04-06 16:20:16 +01005225 big.setViewVisibility(R.id.notification_material_reply_progress,
5226 showSpinner ? View.VISIBLE : View.GONE);
5227 big.setProgressIndeterminateTintList(
5228 R.id.notification_material_reply_progress,
5229 ColorStateList.valueOf(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005230 isColorized(p) ? getPrimaryTextColor(p) : resolveContrastColor(p)));
Adrian Roose458aa82015-12-08 16:17:19 -08005231
Selim Cinekbee4e072018-05-21 22:06:43 -07005232 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])
5233 && p.maxRemoteInputHistory > 1) {
Adrian Roose458aa82015-12-08 16:17:19 -08005234 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005235 big.setTextViewText(R.id.notification_material_reply_text_2,
5236 processTextSpans(replyText[1]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005237 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2, p);
Adrian Roose458aa82015-12-08 16:17:19 -08005238
Selim Cinekbee4e072018-05-21 22:06:43 -07005239 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])
5240 && p.maxRemoteInputHistory > 2) {
Adrian Roose458aa82015-12-08 16:17:19 -08005241 big.setViewVisibility(
5242 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005243 big.setTextViewText(R.id.notification_material_reply_text_3,
5244 processTextSpans(replyText[2]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005245 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3, p);
Adrian Roose458aa82015-12-08 16:17:19 -08005246 }
5247 }
5248 }
5249
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005250 return big;
5251 }
5252
Adrian Roose458aa82015-12-08 16:17:19 -08005253 private boolean hasValidRemoteInput(Action action) {
5254 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
5255 // Weird actions
5256 return false;
5257 }
5258
5259 RemoteInput[] remoteInputs = action.getRemoteInputs();
5260 if (remoteInputs == null) {
5261 return false;
5262 }
5263
5264 for (RemoteInput r : remoteInputs) {
5265 CharSequence[] choices = r.getChoices();
5266 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
5267 return true;
5268 }
5269 }
5270 return false;
5271 }
5272
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005273 /**
5274 * Construct a RemoteViews for the final 1U notification layout. In order:
5275 * 1. Custom contentView from the caller
5276 * 2. Style's proposed content view
5277 * 3. Standard template view
5278 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005279 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005280 return createContentView(false /* increasedheight */ );
5281 }
5282
5283 /**
5284 * Construct a RemoteViews for the smaller content view.
5285 *
5286 * @param increasedHeight true if this layout be created with an increased height. Some
5287 * styles may support showing more then just that basic 1U size
5288 * and the system may decide to render important notifications
5289 * slightly bigger even when collapsed.
5290 *
5291 * @hide
5292 */
5293 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005294 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005295 return mN.contentView;
5296 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005297 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005298 if (styleView != null) {
5299 return styleView;
5300 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005301 }
Selim Cinek384804b2018-04-18 14:31:07 +08005302 return applyStandardTemplate(getBaseLayoutResource(), null /* result */);
Joe Onorato46439ce2010-11-19 13:56:21 -08005303 }
5304
Selim Cineka7679b62017-05-10 16:33:25 -07005305 private boolean useExistingRemoteView() {
5306 return mStyle == null || (!mStyle.displayCustomViewInline()
5307 && !mRebuildStyledRemoteViews);
5308 }
5309
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005310 /**
5311 * Construct a RemoteViews for the final big notification layout.
5312 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005313 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05005314 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07005315 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005316 return mN.bigContentView;
5317 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05005318 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08005319 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005320 } else if (mActions.size() != 0) {
Selim Cinek384804b2018-04-18 14:31:07 +08005321 result = applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5322 null /* result */);
Selim Cinek850a8542015-11-11 11:48:36 -05005323 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08005324 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05005325 return result;
5326 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005327
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005328 /**
Selim Cinek414ad332017-02-24 19:06:12 -08005329 * Construct a RemoteViews for the final notification header only. This will not be
5330 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005331 *
5332 * @hide
5333 */
Lucas Dupin00be88f2019-01-03 17:50:52 -08005334 public RemoteViews makeNotificationHeader() {
5335 return makeNotificationHeader(mParams.reset().fillTextsFrom(this));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005336 }
5337
5338 /**
5339 * Construct a RemoteViews for the final notification header only. This will not be
5340 * colorized.
5341 *
5342 * @param p the template params to inflate this with
5343 */
5344 private RemoteViews makeNotificationHeader(StandardTemplateParams p) {
5345 // Headers on their own are never colorized
5346 p.disallowColorization();
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005347 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Lucas Dupin00be88f2019-01-03 17:50:52 -08005348 R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005349 resetNotificationHeader(header);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005350 bindNotificationHeader(header, p);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005351 return header;
5352 }
5353
Adrian Roos487374f2017-01-11 15:48:14 -08005354 /**
5355 * Construct a RemoteViews for the ambient version of the notification.
5356 *
5357 * @hide
5358 */
5359 public RemoteViews makeAmbientNotification() {
Selim Cinek0e069942019-01-24 16:09:41 -08005360 RemoteViews headsUpContentView = createHeadsUpContentView(false /* increasedHeight */);
5361 if (headsUpContentView != null) {
5362 return headsUpContentView;
5363 }
5364 return createContentView();
Adrian Roos487374f2017-01-11 15:48:14 -08005365 }
5366
Selim Cinek29603462015-11-17 19:04:39 -08005367 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005368 if (result != null) {
5369 result.setViewVisibility(R.id.text_line_1, View.GONE);
5370 }
Selim Cinek29603462015-11-17 19:04:39 -08005371 }
5372
Selim Cinek6743c0b2017-01-18 18:24:01 -08005373 /**
5374 * Adapt the Notification header if this view is used as an expanded view.
5375 *
5376 * @hide
5377 */
5378 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005379 if (result != null) {
5380 result.setBoolean(R.id.notification_header, "setExpanded", true);
5381 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005382 }
5383
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005384 /**
5385 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005386 *
5387 * @param increasedHeight true if this layout be created with an increased height. Some
5388 * styles may support showing more then just that basic 1U size
5389 * and the system may decide to render important notifications
5390 * slightly bigger even when collapsed.
5391 *
5392 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005393 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005394 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005395 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005396 return mN.headsUpContentView;
5397 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08005398 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
5399 if (styleView != null) {
5400 return styleView;
5401 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05005402 } else if (mActions.size() == 0) {
5403 return null;
5404 }
5405
Selim Cinekbee4e072018-05-21 22:06:43 -07005406 // We only want at most a single remote input history to be shown here, otherwise
5407 // the content would become squished.
5408 StandardTemplateParams p = mParams.reset().fillTextsFrom(this)
5409 .setMaxRemoteInputHistory(1);
5410 return applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5411 p,
5412 null /* result */);
Chris Wren8fd39ec2014-02-27 17:43:26 -05005413 }
5414
Selim Cinek624c02db2015-12-14 21:00:02 -08005415 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08005416 * Construct a RemoteViews for the final heads-up notification layout.
5417 */
5418 public RemoteViews createHeadsUpContentView() {
5419 return createHeadsUpContentView(false /* useIncreasedHeight */);
5420 }
5421
5422 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08005423 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5424 *
5425 * @hide
5426 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005427 @UnsupportedAppUsage
Selim Cinek624c02db2015-12-14 21:00:02 -08005428 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005429 return makePublicView(false /* ambient */);
5430 }
5431
5432 /**
5433 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5434 *
5435 * @hide
5436 */
5437 public RemoteViews makePublicAmbientNotification() {
5438 return makePublicView(true /* ambient */);
5439 }
5440
5441 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08005442 if (mN.publicVersion != null) {
5443 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005444 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08005445 }
5446 Bundle savedBundle = mN.extras;
5447 Style style = mStyle;
5448 mStyle = null;
5449 Icon largeIcon = mN.mLargeIcon;
5450 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07005451 Bitmap largeIconLegacy = mN.largeIcon;
5452 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005453 ArrayList<Action> actions = mActions;
5454 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08005455 Bundle publicExtras = new Bundle();
5456 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
5457 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
5458 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
5459 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07005460 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
5461 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cineked07b962018-04-30 14:39:35 -07005462 String appName = savedBundle.getString(EXTRA_SUBSTITUTE_APP_NAME);
5463 if (appName != null) {
5464 publicExtras.putString(EXTRA_SUBSTITUTE_APP_NAME, appName);
5465 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005466 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07005467 RemoteViews view;
Lucas Dupin00be88f2019-01-03 17:50:52 -08005468 view = makeNotificationHeader();
5469 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
Selim Cinek624c02db2015-12-14 21:00:02 -08005470 mN.extras = savedBundle;
5471 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07005472 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005473 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08005474 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005475 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08005476 }
5477
Selim Cinek6743c0b2017-01-18 18:24:01 -08005478 /**
5479 * Construct a content view for the display when low - priority
5480 *
5481 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
5482 * a new subtext is created consisting of the content of the
5483 * notification.
5484 * @hide
5485 */
5486 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005487 StandardTemplateParams p = mParams.reset()
5488 .forceDefaultColor()
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005489 .fillTextsFrom(this);
5490 if (!useRegularSubtext || TextUtils.isEmpty(mParams.summaryText)) {
5491 p.summaryText(createSummaryText());
Selim Cinek6743c0b2017-01-18 18:24:01 -08005492 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005493 RemoteViews header = makeNotificationHeader(p);
Selim Cinek1b554392017-02-28 17:22:49 -08005494 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08005495 return header;
5496 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005497
Selim Cinek6743c0b2017-01-18 18:24:01 -08005498 private CharSequence createSummaryText() {
5499 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
5500 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
5501 return titleText;
5502 }
5503 SpannableStringBuilder summary = new SpannableStringBuilder();
5504 if (titleText == null) {
5505 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
5506 }
5507 BidiFormatter bidi = BidiFormatter.getInstance();
5508 if (titleText != null) {
5509 summary.append(bidi.unicodeWrap(titleText));
5510 }
5511 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
5512 if (titleText != null && contentText != null) {
5513 summary.append(bidi.unicodeWrap(mContext.getText(
5514 R.string.notification_header_divider_symbol_with_spaces)));
5515 }
5516 if (contentText != null) {
5517 summary.append(bidi.unicodeWrap(contentText));
5518 }
5519 return summary;
5520 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05005521
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005522 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005523 StandardTemplateParams p) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04005524 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07005525 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005526 emphazisedMode ? getEmphasizedActionLayoutResource()
5527 : tombstone ? getActionTombstoneLayoutResource()
5528 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04005529 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04005530 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04005531 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005532 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005533 if (action.mRemoteInputs != null) {
5534 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
5535 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005536 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07005537 // change the background bgColor
Selim Cinek981962e2016-07-20 20:41:58 -07005538 CharSequence title = action.title;
5539 ColorStateList[] outResultColor = null;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005540 int background = resolveBackgroundColor(p);
Selim Cinek981962e2016-07-20 20:41:58 -07005541 if (isLegacy()) {
Lucas Dupina291d192018-06-07 13:59:42 -07005542 title = ContrastColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07005543 } else {
5544 outResultColor = new ColorStateList[1];
Selim Cinek396caca2018-04-10 17:46:46 -07005545 title = ensureColorSpanContrast(title, background, outResultColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005546 }
Selim Cinek48f66b72017-08-18 16:17:51 -07005547 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005548 setTextViewColorPrimary(button, R.id.action0, p);
Selim Cinek396caca2018-04-10 17:46:46 -07005549 int rippleColor;
5550 boolean hasColorOverride = outResultColor != null && outResultColor[0] != null;
5551 if (hasColorOverride) {
5552 // There's a span spanning the full text, let's take it and use it as the
5553 // background color
5554 background = outResultColor[0].getDefaultColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005555 int textColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07005556 background, mInNightMode);
Selim Cinek396caca2018-04-10 17:46:46 -07005557 button.setTextColor(R.id.action0, textColor);
5558 rippleColor = textColor;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005559 } else if (getRawColor(p) != COLOR_DEFAULT && !isColorized(p)
5560 && mTintActionButtons) {
5561 rippleColor = resolveContrastColor(p);
Selim Cinek396caca2018-04-10 17:46:46 -07005562 button.setTextColor(R.id.action0, rippleColor);
5563 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005564 rippleColor = getPrimaryTextColor(p);
Selim Cinek981962e2016-07-20 20:41:58 -07005565 }
Selim Cinek396caca2018-04-10 17:46:46 -07005566 // We only want about 20% alpha for the ripple
5567 rippleColor = (rippleColor & 0x00ffffff) | 0x33000000;
5568 button.setColorStateList(R.id.action0, "setRippleColor",
5569 ColorStateList.valueOf(rippleColor));
5570 button.setColorStateList(R.id.action0, "setButtonBackground",
5571 ColorStateList.valueOf(background));
5572 button.setBoolean(R.id.action0, "setHasStroke", !hasColorOverride);
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005573 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07005574 button.setTextViewText(R.id.action0, processTextSpans(
5575 processLegacyText(action.title)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005576 if (isColorized(p)) {
5577 setTextViewColorPrimary(button, R.id.action0, p);
5578 } else if (getRawColor(p) != COLOR_DEFAULT && mTintActionButtons) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005579 button.setTextColor(R.id.action0, resolveContrastColor(p));
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005580 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005581 }
Tony Mak7d4b3a52018-11-27 17:29:36 +00005582 button.setIntTag(R.id.action0, R.id.notification_action_index_tag,
5583 mActions.indexOf(action));
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005584 return button;
5585 }
5586
Joe Onoratocb109a02011-01-18 17:57:41 -08005587 /**
Selim Cinek981962e2016-07-20 20:41:58 -07005588 * Ensures contrast on color spans against a background color. also returns the color of the
5589 * text if a span was found that spans over the whole text.
5590 *
5591 * @param charSequence the charSequence on which the spans are
5592 * @param background the background color to ensure the contrast against
5593 * @param outResultColor an array in which a color will be returned as the first element if
5594 * there exists a full length color span.
5595 * @return the contrasted charSequence
5596 */
5597 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
5598 ColorStateList[] outResultColor) {
5599 if (charSequence instanceof Spanned) {
5600 Spanned ss = (Spanned) charSequence;
5601 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
5602 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
5603 for (Object span : spans) {
5604 Object resultSpan = span;
5605 int spanStart = ss.getSpanStart(span);
5606 int spanEnd = ss.getSpanEnd(span);
5607 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
5608 if (resultSpan instanceof CharacterStyle) {
5609 resultSpan = ((CharacterStyle) span).getUnderlying();
5610 }
5611 if (resultSpan instanceof TextAppearanceSpan) {
5612 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
5613 ColorStateList textColor = originalSpan.getTextColor();
5614 if (textColor != null) {
5615 int[] colors = textColor.getColors();
5616 int[] newColors = new int[colors.length];
5617 for (int i = 0; i < newColors.length; i++) {
Lucas Dupina291d192018-06-07 13:59:42 -07005618 newColors[i] = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005619 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005620 }
5621 textColor = new ColorStateList(textColor.getStates().clone(),
5622 newColors);
Selim Cinek396caca2018-04-10 17:46:46 -07005623 if (fullLength) {
5624 outResultColor[0] = textColor;
5625 // Let's drop the color from the span
5626 textColor = null;
5627 }
Selim Cinek981962e2016-07-20 20:41:58 -07005628 resultSpan = new TextAppearanceSpan(
5629 originalSpan.getFamily(),
5630 originalSpan.getTextStyle(),
5631 originalSpan.getTextSize(),
5632 textColor,
5633 originalSpan.getLinkTextColor());
Selim Cinek981962e2016-07-20 20:41:58 -07005634 }
5635 } else if (resultSpan instanceof ForegroundColorSpan) {
5636 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
5637 int foregroundColor = originalSpan.getForegroundColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005638 foregroundColor = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005639 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005640 if (fullLength) {
5641 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
Selim Cinek396caca2018-04-10 17:46:46 -07005642 resultSpan = null;
5643 } else {
5644 resultSpan = new ForegroundColorSpan(foregroundColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005645 }
5646 } else {
5647 resultSpan = span;
5648 }
Selim Cinek396caca2018-04-10 17:46:46 -07005649 if (resultSpan != null) {
5650 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
5651 }
Selim Cinek981962e2016-07-20 20:41:58 -07005652 }
5653 return builder;
5654 }
5655 return charSequence;
5656 }
5657
5658 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005659 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07005660 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005661 */
5662 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005663 if (!mIsLegacyInitialized) {
5664 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
5665 < Build.VERSION_CODES.LOLLIPOP;
5666 mIsLegacyInitialized = true;
5667 }
5668 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005669 }
5670
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005671 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08005672 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
Lucas Dupin00be88f2019-01-03 17:50:52 -08005673 if (isAlreadyLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005674 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005675 } else {
5676 return charSequence;
5677 }
5678 }
5679
Dan Sandler26e81cf2014-05-06 10:01:27 -04005680 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005681 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04005682 */
Adrian Roos487374f2017-01-11 15:48:14 -08005683 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005684 StandardTemplateParams p) {
Selim Cinekea4bef72015-12-02 15:51:10 -08005685 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005686 int color;
Lucas Dupin00be88f2019-01-03 17:50:52 -08005687 if (isColorized(p)) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005688 color = getPrimaryTextColor(p);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005689 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005690 color = resolveContrastColor(p);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005691 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005692 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07005693 contentView.setDrawableTint(R.id.icon, false, color,
5694 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005695
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005696 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005697 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08005698 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005699 }
5700
Dan Sandler26e81cf2014-05-06 10:01:27 -04005701 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005702 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04005703 * if it's grayscale).
5704 */
5705 // TODO: also check bounds, transparency, that sort of thing.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005706 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView,
5707 StandardTemplateParams p) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04005708 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005709 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005710 // resolve color will fall back to the default when legacy
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005711 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(p),
Sunny Goyal5b153922017-09-21 21:00:36 -07005712 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01005713 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005714 }
5715
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05005716 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005717 if (mN.color != COLOR_DEFAULT) {
5718 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02005719 }
Jorim Jaggi74419312014-06-10 20:57:21 +02005720 }
5721
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005722 int resolveContrastColor(StandardTemplateParams p) {
5723 int rawColor = getRawColor(p);
5724 if (mCachedContrastColorIsFor == rawColor && mCachedContrastColor != COLOR_INVALID) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08005725 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005726 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005727
Selim Cinekac5f0272017-05-02 16:05:41 -07005728 int color;
Selim Cinekc7f5a822018-03-20 19:32:06 -07005729 int background = mContext.getColor(
5730 com.android.internal.R.color.notification_material_background_color);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005731 if (rawColor == COLOR_DEFAULT) {
5732 ensureColors(p);
Lucas Dupinf03e7522018-06-25 16:21:13 -07005733 color = ContrastColorUtil.resolveDefaultColor(mContext, background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005734 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005735 color = ContrastColorUtil.resolveContrastColor(mContext, rawColor,
Anthony Chenad4d1582017-04-10 16:07:58 -07005736 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005737 }
5738 if (Color.alpha(color) < 255) {
5739 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005740 color = ContrastColorUtil.compositeColors(color, background);
Selim Cinekac5f0272017-05-02 16:05:41 -07005741 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005742 mCachedContrastColorIsFor = rawColor;
Selim Cinekac5f0272017-05-02 16:05:41 -07005743 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005744 }
5745
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005746 /**
5747 * Return the raw color of this Notification, which doesn't necessarily satisfy contrast.
5748 *
5749 * @see #resolveContrastColor(StandardTemplateParams) for the contrasted color
5750 * @param p the template params to inflate this with
5751 */
5752 private int getRawColor(StandardTemplateParams p) {
5753 if (p.forceDefaultColor) {
5754 return COLOR_DEFAULT;
5755 }
5756 return mN.color;
5757 }
5758
Selim Cinek4717d862018-04-19 09:19:15 +08005759 int resolveNeutralColor() {
5760 if (mNeutralColor != COLOR_INVALID) {
5761 return mNeutralColor;
5762 }
5763 int background = mContext.getColor(
5764 com.android.internal.R.color.notification_material_background_color);
Lucas Dupinf03e7522018-06-25 16:21:13 -07005765 mNeutralColor = ContrastColorUtil.resolveDefaultColor(mContext, background,
5766 mInNightMode);
Selim Cinek4717d862018-04-19 09:19:15 +08005767 if (Color.alpha(mNeutralColor) < 255) {
5768 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005769 mNeutralColor = ContrastColorUtil.compositeColors(mNeutralColor, background);
Selim Cinek4717d862018-04-19 09:19:15 +08005770 }
5771 return mNeutralColor;
5772 }
5773
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005774 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005775 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005776 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08005777 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005778 @NonNull
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005779 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005780 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005781 mN.actions = new Action[mActions.size()];
5782 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005783 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005784 if (!mPersonList.isEmpty()) {
Selim Cineke7238dd2017-12-14 17:48:32 -08005785 mN.extras.putParcelableArrayList(EXTRA_PEOPLE_LIST, mPersonList);
Dan Sandler0bf2ed82013-12-21 23:33:41 -06005786 }
Selim Cinek247fa012016-02-18 09:50:48 -08005787 if (mN.bigContentView != null || mN.contentView != null
5788 || mN.headsUpContentView != null) {
5789 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
5790 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005791 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08005792 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005793
Julia Reynolds3b848122016-02-26 10:45:32 -05005794 /**
5795 * Creates a Builder from an existing notification so further changes can be made.
5796 * @param context The context for your application / activity.
5797 * @param n The notification to create a Builder from.
5798 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005799 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005800 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005801 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005802 ApplicationInfo applicationInfo = n.extras.getParcelable(
5803 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005804 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05005805 if (applicationInfo != null) {
5806 try {
5807 builderContext = context.createApplicationContext(applicationInfo,
5808 Context.CONTEXT_RESTRICTED);
5809 } catch (NameNotFoundException e) {
5810 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
5811 builderContext = context; // try with our context
5812 }
5813 } else {
5814 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02005815 }
5816
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005817 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005818 }
5819
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005820 /**
Gustav Sennton761884c2018-11-19 17:40:19 +00005821 * Determines whether the platform can generate contextual actions for a notification.
5822 * By default this is true.
5823 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005824 @NonNull
Gustav Sennton761884c2018-11-19 17:40:19 +00005825 public Builder setAllowSystemGeneratedContextualActions(boolean allowed) {
5826 mN.mAllowSystemGeneratedContextualActions = allowed;
5827 return this;
5828 }
5829
5830 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005831 * @deprecated Use {@link #build()} instead.
5832 */
5833 @Deprecated
5834 public Notification getNotification() {
5835 return build();
5836 }
5837
5838 /**
5839 * Combine all of the options that have been set and return a new {@link Notification}
5840 * object.
5841 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005842 @NonNull
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005843 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005844 // first, add any extras from the calling code
5845 if (mUserExtras != null) {
5846 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005847 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005848
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005849 mN.creationTime = System.currentTimeMillis();
5850
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005851 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05005852 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02005853
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005854 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005855
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005856 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02005857 mStyle.reduceImageSizes(mContext);
5858 mStyle.purgeResources();
Selim Cinek90343862018-02-01 11:07:11 -08005859 mStyle.validate(mContext);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005860 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005861 }
Selim Cinekd0426622017-07-11 13:19:59 +02005862 mN.reduceImageSizes(mContext);
5863
Adrian Roos5081c0d2016-02-26 16:04:19 -08005864 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07005865 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005866 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005867 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005868 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
5869 mN.contentView.getSequenceNumber());
5870 }
5871 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005872 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005873 if (mN.bigContentView != null) {
5874 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
5875 mN.bigContentView.getSequenceNumber());
5876 }
5877 }
5878 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005879 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005880 if (mN.headsUpContentView != null) {
5881 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
5882 mN.headsUpContentView.getSequenceNumber());
5883 }
5884 }
5885 }
5886
Julia Reynolds4c0c2022016-02-02 15:11:59 -05005887 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
5888 mN.flags |= FLAG_SHOW_LIGHTS;
5889 }
5890
Adrian Roosfb921842017-10-26 14:49:56 +02005891 mN.allPendingIntents = null;
5892
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005893 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005894 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005895
5896 /**
5897 * Apply this Builder to an existing {@link Notification} object.
5898 *
5899 * @hide
5900 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005901 @NonNull
5902 public Notification buildInto(@NonNull Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005903 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005904 return n;
5905 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005906
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005907 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005908 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005909 * change. Also removes extenders on low ram devices, as
5910 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005911 *
5912 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5913 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005914 * @hide
5915 */
Kristian Monsen05f34792018-04-09 10:27:16 +02005916 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam,
5917 Context context) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005918 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005919
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005920 // Only strip views for known Styles because we won't know how to
5921 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005922 if (!isLowRam
5923 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005924 && getNotificationStyleClass(templateClass) == null) {
5925 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005926 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005927
5928 // Only strip unmodified BuilderRemoteViews.
5929 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005930 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005931 n.contentView.getSequenceNumber();
5932 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005933 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005934 n.bigContentView.getSequenceNumber();
5935 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005936 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005937 n.headsUpContentView.getSequenceNumber();
5938
5939 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005940 if (!isLowRam
5941 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005942 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005943 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005944
5945 Notification clone = n.clone();
5946 if (stripContentView) {
5947 clone.contentView = null;
5948 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5949 }
5950 if (stripBigContentView) {
5951 clone.bigContentView = null;
5952 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5953 }
5954 if (stripHeadsUpContentView) {
5955 clone.headsUpContentView = null;
5956 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5957 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005958 if (isLowRam) {
Kristian Monsen05f34792018-04-09 10:27:16 +02005959 String[] allowedServices = context.getResources().getStringArray(
5960 R.array.config_allowedManagedServicesOnLowRamDevices);
5961 if (allowedServices.length == 0) {
5962 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5963 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5964 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5965 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005966 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005967 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005968 }
5969
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005970 @UnsupportedAppUsage
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005971 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005972 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005973 }
5974
5975 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005976 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005977 }
5978
5979 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005980 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005981 }
5982
5983 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005984 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005985 }
5986
5987 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005988 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005989 }
5990
Adrian Roosc1a80b02016-04-05 14:54:55 -07005991 private int getMessagingLayoutResource() {
5992 return R.layout.notification_template_material_messaging;
5993 }
5994
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005995 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005996 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005997 }
5998
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005999 private int getEmphasizedActionLayoutResource() {
6000 return R.layout.notification_material_action_emphasized;
6001 }
6002
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006003 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07006004 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006005 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08006006
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006007 private int getBackgroundColor(StandardTemplateParams p) {
6008 if (isColorized(p)) {
6009 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : getRawColor(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08006010 } else {
Selim Cinekc7f5a822018-03-20 19:32:06 -07006011 return COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08006012 }
6013 }
6014
Selim Cinek396caca2018-04-10 17:46:46 -07006015 /**
Selim Cinek4717d862018-04-19 09:19:15 +08006016 * Gets a neutral color that can be used for icons or similar that should not stand out.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006017 * @param p the template params to inflate this with
Selim Cinek4717d862018-04-19 09:19:15 +08006018 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006019 private int getNeutralColor(StandardTemplateParams p) {
6020 if (isColorized(p)) {
6021 return getSecondaryTextColor(p);
Selim Cinek4717d862018-04-19 09:19:15 +08006022 } else {
6023 return resolveNeutralColor();
6024 }
6025 }
6026
6027 /**
Selim Cinek396caca2018-04-10 17:46:46 -07006028 * Same as getBackgroundColor but also resolved the default color to the background.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006029 * @param p the template params to inflate this with
Selim Cinek396caca2018-04-10 17:46:46 -07006030 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006031 private int resolveBackgroundColor(StandardTemplateParams p) {
6032 int backgroundColor = getBackgroundColor(p);
Selim Cinek396caca2018-04-10 17:46:46 -07006033 if (backgroundColor == COLOR_DEFAULT) {
6034 backgroundColor = mContext.getColor(
6035 com.android.internal.R.color.notification_material_background_color);
6036 }
6037 return backgroundColor;
6038 }
6039
Anthony Chenad4d1582017-04-10 16:07:58 -07006040 private boolean shouldTintActionButtons() {
6041 return mTintActionButtons;
6042 }
6043
Selim Cinek99104832017-01-25 14:47:33 -08006044 private boolean textColorsNeedInversion() {
6045 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
6046 return false;
6047 }
6048 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
6049 return targetSdkVersion > Build.VERSION_CODES.M
6050 && targetSdkVersion < Build.VERSION_CODES.O;
6051 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07006052
6053 /**
6054 * Set a color palette to be used as the background and textColors
6055 *
6056 * @param backgroundColor the color to be used as the background
6057 * @param foregroundColor the color to be used as the foreground
6058 *
6059 * @hide
6060 */
6061 public void setColorPalette(int backgroundColor, int foregroundColor) {
6062 mBackgroundColor = backgroundColor;
6063 mForegroundColor = foregroundColor;
6064 mTextColorsAreForBackground = COLOR_INVALID;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006065 ensureColors(mParams.reset().fillTextsFrom(this));
Selim Cinek5fb73f82017-04-20 16:55:38 -07006066 }
Selim Cinekac5f0272017-05-02 16:05:41 -07006067
6068 /**
Selim Cineka7679b62017-05-10 16:33:25 -07006069 * Forces all styled remoteViews to be built from scratch and not use any cached
6070 * RemoteViews.
6071 * This is needed for legacy apps that are baking in their remoteviews into the
6072 * notification.
6073 *
6074 * @hide
6075 */
6076 public void setRebuildStyledRemoteViews(boolean rebuild) {
6077 mRebuildStyledRemoteViews = rebuild;
6078 }
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006079
6080 /**
6081 * Get the text that should be displayed in the statusBar when heads upped. This is
6082 * usually just the app name, but may be different depending on the style.
6083 *
6084 * @param publicMode If true, return a text that is safe to display in public.
6085 *
6086 * @hide
6087 */
6088 public CharSequence getHeadsUpStatusBarText(boolean publicMode) {
6089 if (mStyle != null && !publicMode) {
6090 CharSequence text = mStyle.getHeadsUpStatusBarText();
6091 if (!TextUtils.isEmpty(text)) {
6092 return text;
6093 }
6094 }
6095 return loadHeaderAppName();
6096 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08006097 }
6098
6099 /**
Selim Cinekd0426622017-07-11 13:19:59 +02006100 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
6101 * remote views.
6102 *
6103 * @hide
6104 */
6105 void reduceImageSizes(Context context) {
6106 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
6107 return;
6108 }
Selim Cineka8cb1262017-08-15 16:53:44 -07006109 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006110 if (mLargeIcon != null || largeIcon != null) {
6111 Resources resources = context.getResources();
6112 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07006113 int maxWidth = resources.getDimensionPixelSize(isLowRam
6114 ? R.dimen.notification_right_icon_size_low_ram
6115 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006116 int maxHeight = maxWidth;
6117 if (MediaStyle.class.equals(style)
6118 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006119 maxHeight = resources.getDimensionPixelSize(isLowRam
6120 ? R.dimen.notification_media_image_max_height_low_ram
6121 : R.dimen.notification_media_image_max_height);
6122 maxWidth = resources.getDimensionPixelSize(isLowRam
6123 ? R.dimen.notification_media_image_max_width_low_ram
6124 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006125 }
6126 if (mLargeIcon != null) {
6127 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
6128 }
6129 if (largeIcon != null) {
6130 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
6131 }
6132 }
Selim Cineka8cb1262017-08-15 16:53:44 -07006133 reduceImageSizesForRemoteView(contentView, context, isLowRam);
6134 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
6135 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02006136 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
6137 }
6138
Selim Cineka8cb1262017-08-15 16:53:44 -07006139 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
6140 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02006141 if (remoteView != null) {
6142 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006143 int maxWidth = resources.getDimensionPixelSize(isLowRam
6144 ? R.dimen.notification_custom_view_max_image_width_low_ram
6145 : R.dimen.notification_custom_view_max_image_width);
6146 int maxHeight = resources.getDimensionPixelSize(isLowRam
6147 ? R.dimen.notification_custom_view_max_image_height_low_ram
6148 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02006149 remoteView.reduceImageSizes(maxWidth, maxHeight);
6150 }
6151 }
6152
6153 /**
Selim Cinek22714f12017-04-13 16:23:53 -07006154 * @return whether this notification is a foreground service notification
Gus Prevasb0c1a462018-11-05 11:06:15 -05006155 * @hide
Selim Cinek7b9605b2017-01-19 17:36:00 -08006156 */
Gus Prevasb0c1a462018-11-05 11:06:15 -05006157 public boolean isForegroundService() {
Selim Cinek22714f12017-04-13 16:23:53 -07006158 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08006159 }
6160
6161 /**
Selim Cinek99104832017-01-25 14:47:33 -08006162 * @return whether this notification has a media session attached
6163 * @hide
6164 */
6165 public boolean hasMediaSession() {
6166 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
6167 }
6168
6169 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006170 * @return the style class of this notification
6171 * @hide
6172 */
6173 public Class<? extends Notification.Style> getNotificationStyle() {
6174 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
6175
6176 if (!TextUtils.isEmpty(templateClass)) {
6177 return Notification.getNotificationStyleClass(templateClass);
6178 }
6179 return null;
6180 }
6181
6182 /**
Selim Cinek22714f12017-04-13 16:23:53 -07006183 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08006184 *
6185 * @hide
6186 */
6187 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07006188 if (isColorizedMedia()) {
6189 return true;
6190 }
Julia Reynolds4db59552017-06-30 13:34:01 -04006191 return extras.getBoolean(EXTRA_COLORIZED)
6192 && (hasColorizedPermission() || isForegroundService());
6193 }
6194
6195 /**
6196 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
6197 * permission. The permission is checked when a notification is enqueued.
6198 */
6199 private boolean hasColorizedPermission() {
6200 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07006201 }
6202
6203 /**
6204 * @return true if this notification is colorized and it is a media notification
6205 *
6206 * @hide
6207 */
6208 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08006209 Class<? extends Style> style = getNotificationStyle();
6210 if (MediaStyle.class.equals(style)) {
6211 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
6212 if ((colorized == null || colorized) && hasMediaSession()) {
6213 return true;
6214 }
6215 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6216 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
6217 return true;
6218 }
6219 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07006220 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006221 }
6222
Selim Cinek0847acd2017-04-24 19:48:29 -07006223
6224 /**
6225 * @return true if this is a media notification
6226 *
6227 * @hide
6228 */
6229 public boolean isMediaNotification() {
6230 Class<? extends Style> style = getNotificationStyle();
6231 if (MediaStyle.class.equals(style)) {
6232 return true;
6233 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6234 return true;
6235 }
6236 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006237 }
6238
Selim Cinek279fa862016-06-14 10:57:25 -07006239 private boolean hasLargeIcon() {
6240 return mLargeIcon != null || largeIcon != null;
6241 }
6242
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006243 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006244 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006245 * @hide
6246 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07006247 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006248 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
6249 }
6250
6251 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006252 * @return true if the notification will show a chronometer; false otherwise
6253 * @hide
6254 */
6255 public boolean showsChronometer() {
6256 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
6257 }
6258
6259 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04006260 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05006261 */
6262 @SystemApi
6263 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
6264 Class<? extends Style>[] classes = new Class[] {
6265 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
6266 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
6267 MessagingStyle.class };
6268 for (Class<? extends Style> innerClass : classes) {
6269 if (templateClass.equals(innerClass.getName())) {
6270 return innerClass;
6271 }
6272 }
6273 return null;
6274 }
6275
6276 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006277 * An object that can apply a rich notification style to a {@link Notification.Builder}
6278 * object.
6279 */
Griff Hazendfcb0802014-02-11 12:00:00 -08006280 public static abstract class Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07006281
6282 /**
6283 * The number of items allowed simulatanously in the remote input history.
6284 * @hide
6285 */
6286 static final int MAX_REMOTE_INPUT_HISTORY_LINES = 3;
Chris Wrend6297db2012-05-03 16:20:13 -04006287 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006288
6289 /**
6290 * @hide
6291 */
6292 protected CharSequence mSummaryText = null;
6293
6294 /**
6295 * @hide
6296 */
6297 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04006298
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006299 protected Builder mBuilder;
6300
Chris Wrend6297db2012-05-03 16:20:13 -04006301 /**
6302 * Overrides ContentTitle in the big form of the template.
6303 * This defaults to the value passed to setContentTitle().
6304 */
6305 protected void internalSetBigContentTitle(CharSequence title) {
6306 mBigContentTitle = title;
6307 }
6308
6309 /**
6310 * Set the first line of text after the detail section in the big form of the template.
6311 */
6312 protected void internalSetSummaryText(CharSequence cs) {
6313 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04006314 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04006315 }
6316
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006317 public void setBuilder(Builder builder) {
6318 if (mBuilder != builder) {
6319 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07006320 if (mBuilder != null) {
6321 mBuilder.setStyle(this);
6322 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006323 }
6324 }
6325
Chris Wrend6297db2012-05-03 16:20:13 -04006326 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006327 if (mBuilder == null) {
6328 throw new IllegalArgumentException("Style requires a valid Builder object");
6329 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006330 }
Chris Wrend6297db2012-05-03 16:20:13 -04006331
6332 protected RemoteViews getStandardView(int layoutId) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006333 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder);
6334 return getStandardView(layoutId, p, null);
Selim Cinek384804b2018-04-18 14:31:07 +08006335 }
6336
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006337
Selim Cinek384804b2018-04-18 14:31:07 +08006338 /**
6339 * Get the standard view for this style.
6340 *
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006341 * @param layoutId The layout id to use.
6342 * @param p the params for this inflation.
Selim Cinek384804b2018-04-18 14:31:07 +08006343 * @param result The result where template bind information is saved.
6344 * @return A remoteView for this style.
6345 * @hide
6346 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006347 protected RemoteViews getStandardView(int layoutId, StandardTemplateParams p,
6348 TemplateBindResult result) {
Chris Wrend6297db2012-05-03 16:20:13 -04006349 checkBuilder();
6350
6351 if (mBigContentTitle != null) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006352 p.title = mBigContentTitle;
Chris Wrend6297db2012-05-03 16:20:13 -04006353 }
6354
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006355 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId, p,
6356 result);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006357
Chris Wrend6297db2012-05-03 16:20:13 -04006358 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
6359 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04006360 } else {
6361 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04006362 }
6363
Chris Wrend6297db2012-05-03 16:20:13 -04006364 return contentView;
6365 }
6366
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006367 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006368 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006369 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08006370 *
6371 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006372 * @hide
6373 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08006374 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006375 return null;
6376 }
6377
6378 /**
6379 * Construct a Style-specific RemoteViews for the final big notification layout.
6380 * @hide
6381 */
6382 public RemoteViews makeBigContentView() {
6383 return null;
6384 }
6385
6386 /**
6387 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08006388 *
6389 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006390 * @hide
6391 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006392 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006393 return null;
6394 }
6395
6396 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006397 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006398 * @hide
6399 */
6400 public void addExtras(Bundle extras) {
6401 if (mSummaryTextSet) {
6402 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
6403 }
6404 if (mBigContentTitle != null) {
6405 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
6406 }
Chris Wren91ad5632013-06-05 15:05:57 -04006407 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006408 }
6409
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006410 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006411 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006412 * @hide
6413 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02006414 protected void restoreFromExtras(Bundle extras) {
6415 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
6416 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
6417 mSummaryTextSet = true;
6418 }
6419 if (extras.containsKey(EXTRA_TITLE_BIG)) {
6420 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
6421 }
6422 }
6423
6424
6425 /**
6426 * @hide
6427 */
6428 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006429 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006430 return wip;
6431 }
6432
Daniel Sandler0ec46202015-06-24 01:27:05 -04006433 /**
6434 * @hide
6435 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006436 public void purgeResources() {}
6437
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006438 /**
6439 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
6440 * attached to.
6441 *
6442 * @return the fully constructed Notification.
6443 */
6444 public Notification build() {
6445 checkBuilder();
6446 return mBuilder.build();
6447 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006448
6449 /**
6450 * @hide
6451 * @return true if the style positions the progress bar on the second line; false if the
6452 * style hides the progress bar
6453 */
6454 protected boolean hasProgress() {
6455 return true;
6456 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006457
6458 /**
6459 * @hide
6460 * @return Whether we should put the summary be put into the notification header
6461 */
6462 public boolean hasSummaryInHeader() {
6463 return true;
6464 }
Selim Cinek593610c2016-02-16 18:42:57 -08006465
6466 /**
6467 * @hide
6468 * @return Whether custom content views are displayed inline in the style
6469 */
6470 public boolean displayCustomViewInline() {
6471 return false;
6472 }
Selim Cinekd0426622017-07-11 13:19:59 +02006473
6474 /**
6475 * Reduces the image sizes contained in this style.
6476 *
6477 * @hide
6478 */
6479 public void reduceImageSizes(Context context) {
6480 }
Selim Cinek90343862018-02-01 11:07:11 -08006481
6482 /**
6483 * Validate that this style was properly composed. This is called at build time.
6484 * @hide
6485 */
6486 public void validate(Context context) {
6487 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006488
6489 /**
6490 * @hide
6491 */
6492 public abstract boolean areNotificationsVisiblyDifferent(Style other);
Selim Cinekc7f5a822018-03-20 19:32:06 -07006493
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006494 /**
koprivaa1a78482018-10-09 10:09:23 -07006495 * @return the text that should be displayed in the statusBar when heads-upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006496 * If {@code null} is returned, the default implementation will be used.
6497 *
6498 * @hide
6499 */
6500 public CharSequence getHeadsUpStatusBarText() {
6501 return null;
6502 }
Joe Onorato46439ce2010-11-19 13:56:21 -08006503 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006504
6505 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006506 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08006507 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006508 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006509 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006510 * Notification notif = new Notification.Builder(mContext)
6511 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
6512 * .setContentText(subject)
6513 * .setSmallIcon(R.drawable.new_post)
6514 * .setLargeIcon(aBitmap)
6515 * .setStyle(new Notification.BigPictureStyle()
6516 * .bigPicture(aBigBitmap))
6517 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006518 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006519 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006520 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006521 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006522 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006523 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006524 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006525 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006526
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006527 public BigPictureStyle() {
6528 }
6529
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006530 /**
6531 * @deprecated use {@code BigPictureStyle()}.
6532 */
6533 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006534 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006535 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006536 }
6537
Chris Wrend6297db2012-05-03 16:20:13 -04006538 /**
6539 * Overrides ContentTitle in the big form of the template.
6540 * This defaults to the value passed to setContentTitle().
6541 */
6542 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006543 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006544 return this;
6545 }
6546
6547 /**
6548 * Set the first line of text after the detail section in the big form of the template.
6549 */
6550 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006551 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006552 return this;
6553 }
6554
Chris Wren0bd664d2012-08-01 13:56:56 -04006555 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05006556 * @hide
6557 */
6558 public Bitmap getBigPicture() {
6559 return mPicture;
6560 }
6561
6562 /**
Chris Wren0bd664d2012-08-01 13:56:56 -04006563 * Provide the bitmap to be used as the payload for the BigPicture notification.
6564 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006565 public BigPictureStyle bigPicture(Bitmap b) {
6566 mPicture = b;
6567 return this;
6568 }
6569
Chris Wren3745a3d2012-05-22 15:11:52 -04006570 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04006571 * Override the large icon when the big notification is shown.
6572 */
6573 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04006574 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
6575 }
6576
6577 /**
6578 * Override the large icon when the big notification is shown.
6579 */
6580 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04006581 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006582 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006583 return this;
6584 }
6585
Riley Andrews0394a0c2015-11-03 23:36:52 -08006586 /** @hide */
6587 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
6588
Daniel Sandler0ec46202015-06-24 01:27:05 -04006589 /**
6590 * @hide
6591 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006592 @Override
6593 public void purgeResources() {
6594 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08006595 if (mPicture != null &&
6596 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08006597 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006598 mPicture = mPicture.createAshmemBitmap();
6599 }
6600 if (mBigLargeIcon != null) {
6601 mBigLargeIcon.convertToAshmem();
6602 }
6603 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01006604
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006605 /**
6606 * @hide
6607 */
Selim Cinekd0426622017-07-11 13:19:59 +02006608 @Override
6609 public void reduceImageSizes(Context context) {
6610 super.reduceImageSizes(context);
6611 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006612 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006613 if (mPicture != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006614 int maxPictureWidth = resources.getDimensionPixelSize(isLowRam
6615 ? R.dimen.notification_big_picture_max_height_low_ram
6616 : R.dimen.notification_big_picture_max_height);
6617 int maxPictureHeight = resources.getDimensionPixelSize(isLowRam
6618 ? R.dimen.notification_big_picture_max_width_low_ram
6619 : R.dimen.notification_big_picture_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006620 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
6621 }
6622 if (mBigLargeIcon != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006623 int rightIconSize = resources.getDimensionPixelSize(isLowRam
6624 ? R.dimen.notification_right_icon_size_low_ram
6625 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006626 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
6627 }
6628 }
6629
6630 /**
6631 * @hide
6632 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006633 public RemoteViews makeBigContentView() {
6634 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01006635 // This covers the following cases:
6636 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006637 // mN.mLargeIcon
6638 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04006639 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07006640 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006641 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006642 oldLargeIcon = mBuilder.mN.mLargeIcon;
6643 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006644 // The legacy largeIcon might not allow us to clear the image, as it's taken in
6645 // replacement if the other one is null. Because we're restoring these legacy icons
6646 // for old listeners, this is in general non-null.
6647 largeIconLegacy = mBuilder.mN.largeIcon;
6648 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006649 }
6650
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006651 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder);
Selim Cinek384804b2018-04-18 14:31:07 +08006652 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource(),
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006653 p, null /* result */);
Selim Cinek03d0d652015-11-13 13:18:09 -05006654 if (mSummaryTextSet) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006655 contentView.setTextViewText(R.id.text, mBuilder.processTextSpans(
6656 mBuilder.processLegacyText(mSummaryText)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006657 mBuilder.setTextViewColorSecondary(contentView, R.id.text, p);
Selim Cinekc848c3a2016-01-13 15:27:30 -08006658 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05006659 }
Selim Cinek279fa862016-06-14 10:57:25 -07006660 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08006661
Christoph Studer5c510ee2014-12-15 16:32:27 +01006662 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006663 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006664 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006665 }
6666
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006667 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006668 return contentView;
6669 }
6670
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006671 /**
6672 * @hide
6673 */
6674 public void addExtras(Bundle extras) {
6675 super.addExtras(extras);
6676
6677 if (mBigLargeIconSet) {
6678 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
6679 }
6680 extras.putParcelable(EXTRA_PICTURE, mPicture);
6681 }
6682
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006683 /**
6684 * @hide
6685 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006686 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02006687 protected void restoreFromExtras(Bundle extras) {
6688 super.restoreFromExtras(extras);
6689
6690 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01006691 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02006692 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04006693 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006694 mPicture = extras.getParcelable(EXTRA_PICTURE);
6695 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006696
6697 /**
6698 * @hide
6699 */
6700 @Override
6701 public boolean hasSummaryInHeader() {
6702 return false;
6703 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006704
6705 /**
6706 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006707 * Note that we aren't actually comparing the contents of the bitmaps here, so this
6708 * is only doing a cursory inspection. Bitmaps of equal size will appear the same.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006709 */
6710 @Override
6711 public boolean areNotificationsVisiblyDifferent(Style other) {
6712 if (other == null || getClass() != other.getClass()) {
6713 return true;
6714 }
6715 BigPictureStyle otherS = (BigPictureStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006716 return areBitmapsObviouslyDifferent(getBigPicture(), otherS.getBigPicture());
6717 }
6718
6719 private static boolean areBitmapsObviouslyDifferent(Bitmap a, Bitmap b) {
6720 if (a == b) {
6721 return false;
6722 }
6723 if (a == null || b == null) {
6724 return true;
6725 }
6726 return a.getWidth() != b.getWidth()
6727 || a.getHeight() != b.getHeight()
6728 || a.getConfig() != b.getConfig()
6729 || a.getGenerationId() != b.getGenerationId();
Julia Reynolds7217dc92018-03-07 12:12:09 -05006730 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006731 }
6732
6733 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006734 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08006735 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006736 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006737 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006738 * Notification notif = new Notification.Builder(mContext)
6739 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
6740 * .setContentText(subject)
6741 * .setSmallIcon(R.drawable.new_mail)
6742 * .setLargeIcon(aBitmap)
6743 * .setStyle(new Notification.BigTextStyle()
6744 * .bigText(aVeryLongString))
6745 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006746 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006747 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006748 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006749 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006750 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006751
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006752 private CharSequence mBigText;
6753
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006754 public BigTextStyle() {
6755 }
6756
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006757 /**
6758 * @deprecated use {@code BigTextStyle()}.
6759 */
6760 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006761 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006762 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006763 }
6764
Chris Wrend6297db2012-05-03 16:20:13 -04006765 /**
6766 * Overrides ContentTitle in the big form of the template.
6767 * This defaults to the value passed to setContentTitle().
6768 */
6769 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006770 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006771 return this;
6772 }
6773
6774 /**
6775 * Set the first line of text after the detail section in the big form of the template.
6776 */
6777 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006778 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006779 return this;
6780 }
6781
Chris Wren0bd664d2012-08-01 13:56:56 -04006782 /**
6783 * Provide the longer text to be displayed in the big form of the
6784 * template in place of the content text.
6785 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006786 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006787 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006788 return this;
6789 }
6790
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006791 /**
6792 * @hide
6793 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05006794 public CharSequence getBigText() {
6795 return mBigText;
6796 }
6797
6798 /**
6799 * @hide
6800 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006801 public void addExtras(Bundle extras) {
6802 super.addExtras(extras);
6803
Christoph Studer4600f9b2014-07-22 22:44:43 +02006804 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
6805 }
6806
6807 /**
6808 * @hide
6809 */
6810 @Override
6811 protected void restoreFromExtras(Bundle extras) {
6812 super.restoreFromExtras(extras);
6813
6814 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006815 }
6816
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006817 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006818 * @param increasedHeight true if this layout be created with an increased height.
6819 *
6820 * @hide
6821 */
6822 @Override
6823 public RemoteViews makeContentView(boolean increasedHeight) {
6824 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006825 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006826 mBuilder.mActions = new ArrayList<>();
6827 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006828 mBuilder.mActions = mBuilder.mOriginalActions;
6829 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006830 return remoteViews;
6831 }
6832 return super.makeContentView(increasedHeight);
6833 }
6834
6835 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006836 * @hide
6837 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006838 @Override
6839 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
6840 if (increasedHeight && mBuilder.mActions.size() > 0) {
6841 return makeBigContentView();
6842 }
6843 return super.makeHeadsUpContentView(increasedHeight);
6844 }
6845
6846 /**
6847 * @hide
6848 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006849 public RemoteViews makeBigContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006850 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder).text(null);
Selim Cinek384804b2018-04-18 14:31:07 +08006851 TemplateBindResult result = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006852 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource(), p,
6853 result);
Selim Cinek1c72fa02018-04-23 18:00:54 +08006854 contentView.setInt(R.id.big_text, "setImageEndMargin", result.getIconMarginEnd());
Joe Malin8d40d042012-11-05 11:36:40 -08006855
Selim Cinek3a2c4b92015-12-17 17:01:17 -08006856 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07006857 if (TextUtils.isEmpty(bigTextText)) {
6858 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
6859 // experience
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006860 bigTextText = mBuilder.processLegacyText(
6861 mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT));
Selim Cinek75998782016-04-26 10:39:17 -07006862 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006863 contentView.setTextViewText(R.id.big_text, mBuilder.processTextSpans(bigTextText));
6864 mBuilder.setTextViewColorSecondary(contentView, R.id.big_text, p);
6865 contentView.setViewVisibility(R.id.big_text,
6866 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08006867 contentView.setBoolean(R.id.big_text, "setHasImage",
6868 result.isRightIconContainerVisible());
Selim Cinek4fb12d32015-11-19 18:10:48 -08006869
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006870 return contentView;
6871 }
6872
Julia Reynolds7217dc92018-03-07 12:12:09 -05006873 /**
6874 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006875 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006876 */
6877 @Override
6878 public boolean areNotificationsVisiblyDifferent(Style other) {
6879 if (other == null || getClass() != other.getClass()) {
6880 return true;
6881 }
6882 BigTextStyle newS = (BigTextStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006883 return !Objects.equals(String.valueOf(getBigText()), String.valueOf(newS.getBigText()));
Julia Reynolds7217dc92018-03-07 12:12:09 -05006884 }
6885
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006886 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006887
6888 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006889 * Helper class for generating large-format notifications that include multiple back-and-forth
6890 * messages of varying types between any number of people.
6891 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006892 * <p>
Alex Hillsfc737de2016-03-23 17:33:02 -04006893 * If the platform does not provide large-format notifications, this method has no effect. The
6894 * user will always see the normal notification view.
Selim Cinekce8794f2018-05-23 16:46:05 -07006895 *
6896 * <p>
6897 * If the app is targeting Android P and above, it is required to use the {@link Person}
6898 * class in order to get an optimal rendering of the notification and its avatars. For
6899 * conversations involving multiple people, the app should also make sure that it marks the
6900 * conversation as a group with {@link #setGroupConversation(boolean)}.
6901 *
6902 * <p>
6903 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior.
6904 * Here's an example of how this may be used:
Alex Hillsfc737de2016-03-23 17:33:02 -04006905 * <pre class="prettyprint">
6906 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006907 * Person user = new Person.Builder().setIcon(userIcon).setName(userName).build();
6908 * MessagingStyle style = new MessagingStyle(user)
6909 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getPerson())
6910 * .addMessage(messages[2].getText(), messages[2].getTime(), messages[2].getPerson())
6911 * .setGroupConversation(hasMultiplePeople());
6912 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006913 * Notification noti = new Notification.Builder()
Selim Cinekce8794f2018-05-23 16:46:05 -07006914 * .setContentTitle(&quot;2 new messages with &quot; + sender.toString())
Alex Hillsfc737de2016-03-23 17:33:02 -04006915 * .setContentText(subject)
6916 * .setSmallIcon(R.drawable.new_message)
6917 * .setLargeIcon(aBitmap)
Selim Cinekce8794f2018-05-23 16:46:05 -07006918 * .setStyle(style)
Alex Hillsfc737de2016-03-23 17:33:02 -04006919 * .build();
6920 * </pre>
6921 */
6922 public static class MessagingStyle extends Style {
6923
6924 /**
6925 * The maximum number of messages that will be retained in the Notification itself (the
6926 * number displayed is up to the platform).
6927 */
6928 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
6929
Selim Cinekcb8b9852017-12-15 18:01:52 -08006930 @NonNull Person mUser;
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006931 @Nullable CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04006932 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08006933 List<Message> mHistoricMessages = new ArrayList<>();
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006934 boolean mIsGroupConversation;
Alex Hillsfc737de2016-03-23 17:33:02 -04006935
6936 MessagingStyle() {
6937 }
6938
6939 /**
Alex Hillsfd590442016-10-07 09:52:44 -04006940 * @param userDisplayName Required - the name to be displayed for any replies sent by the
6941 * user before the posting app reposts the notification with those messages after they've
6942 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04006943 * {@link #addMessage(Notification.MessagingStyle.Message)}
Selim Cinekcb8b9852017-12-15 18:01:52 -08006944 *
6945 * @deprecated use {@code MessagingStyle(Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006946 */
Alex Hillsfd590442016-10-07 09:52:44 -04006947 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Selim Cinek9acd6732018-03-23 16:39:02 -07006948 this(new Person.Builder().setName(userDisplayName).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006949 }
6950
6951 /**
6952 * @param user Required - The person displayed for any messages that are sent by the
6953 * user. Any messages added with {@link #addMessage(Notification.MessagingStyle.Message)}
6954 * who don't have a Person associated with it will be displayed as if they were sent
Selim Cinek90343862018-02-01 11:07:11 -08006955 * by this user. The user also needs to have a valid name associated with it, which will
6956 * be enforced starting in Android P.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006957 */
6958 public MessagingStyle(@NonNull Person user) {
6959 mUser = user;
Selim Cinek90343862018-02-01 11:07:11 -08006960 }
6961
6962 /**
6963 * Validate that this style was properly composed. This is called at build time.
6964 * @hide
6965 */
6966 @Override
6967 public void validate(Context context) {
6968 super.validate(context);
6969 if (context.getApplicationInfo().targetSdkVersion
6970 >= Build.VERSION_CODES.P && (mUser == null || mUser.getName() == null)) {
6971 throw new RuntimeException("User must be valid and have a name.");
Selim Cinekcb8b9852017-12-15 18:01:52 -08006972 }
6973 }
6974
6975 /**
koprivaa1a78482018-10-09 10:09:23 -07006976 * @return the text that should be displayed in the statusBar when heads upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006977 * If {@code null} is returned, the default implementation will be used.
6978 *
6979 * @hide
6980 */
6981 @Override
6982 public CharSequence getHeadsUpStatusBarText() {
6983 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
6984 ? super.mBigContentTitle
6985 : mConversationTitle;
6986 if (!TextUtils.isEmpty(conversationTitle) && !hasOnlyWhiteSpaceSenders()) {
6987 return conversationTitle;
6988 }
6989 return null;
6990 }
6991
6992 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08006993 * @return the user to be displayed for any replies sent by the user
6994 */
Selim Cinekeb53c222018-04-06 12:56:09 -07006995 @NonNull
Selim Cinekcb8b9852017-12-15 18:01:52 -08006996 public Person getUser() {
6997 return mUser;
Alex Hillsfc737de2016-03-23 17:33:02 -04006998 }
6999
7000 /**
7001 * Returns the name to be displayed for any replies sent by the user
Selim Cinekcb8b9852017-12-15 18:01:52 -08007002 *
7003 * @deprecated use {@link #getUser()} instead
Alex Hillsfc737de2016-03-23 17:33:02 -04007004 */
7005 public CharSequence getUserDisplayName() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007006 return mUser.getName();
Alex Hillsfc737de2016-03-23 17:33:02 -04007007 }
7008
7009 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007010 * Sets the title to be displayed on this conversation. May be set to {@code null}.
7011 *
Kodlee Yin14656422017-12-22 17:00:46 -08007012 * <p>This API's behavior was changed in SDK version {@link Build.VERSION_CODES#P}. If your
7013 * application's target version is less than {@link Build.VERSION_CODES#P}, setting a
7014 * conversation title to a non-null value will make {@link #isGroupConversation()} return
7015 * {@code true} and passing {@code null} will make it return {@code false}. In
7016 * {@link Build.VERSION_CODES#P} and beyond, use {@link #setGroupConversation(boolean)}
7017 * to set group conversation status.
7018 *
7019 * @param conversationTitle Title displayed for this conversation
7020 * @return this object for method chaining
Alex Hillsfc737de2016-03-23 17:33:02 -04007021 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007022 public MessagingStyle setConversationTitle(@Nullable CharSequence conversationTitle) {
Alex Hillsfc737de2016-03-23 17:33:02 -04007023 mConversationTitle = conversationTitle;
7024 return this;
7025 }
7026
7027 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007028 * Return the title to be displayed on this conversation. May return {@code null}.
Alex Hillsfc737de2016-03-23 17:33:02 -04007029 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007030 @Nullable
Alex Hillsfc737de2016-03-23 17:33:02 -04007031 public CharSequence getConversationTitle() {
7032 return mConversationTitle;
7033 }
7034
7035 /**
7036 * Adds a message for display by this notification. Convenience call for a simple
7037 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
7038 * @param text A {@link CharSequence} to be displayed as the message content
7039 * @param timestamp Time at which the message arrived
7040 * @param sender A {@link CharSequence} to be used for displaying the name of the
7041 * sender. Should be <code>null</code> for messages by the current user, in which case
7042 * the platform will insert {@link #getUserDisplayName()}.
7043 * Should be unique amongst all individuals in the conversation, and should be
7044 * consistent during re-posts of the notification.
7045 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07007046 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Alex Hillsfc737de2016-03-23 17:33:02 -04007047 *
7048 * @return this object for method chaining
Selim Cinekcb8b9852017-12-15 18:01:52 -08007049 *
7050 * @deprecated use {@link #addMessage(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04007051 */
7052 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007053 return addMessage(text, timestamp,
Selim Cinek9acd6732018-03-23 16:39:02 -07007054 sender == null ? null : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08007055 }
7056
7057 /**
7058 * Adds a message for display by this notification. Convenience call for a simple
7059 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
7060 * @param text A {@link CharSequence} to be displayed as the message content
7061 * @param timestamp Time at which the message arrived
7062 * @param sender The {@link Person} who sent the message.
7063 * Should be <code>null</code> for messages by the current user, in which case
7064 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7065 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07007066 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Selim Cinekcb8b9852017-12-15 18:01:52 -08007067 *
7068 * @return this object for method chaining
7069 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007070 public MessagingStyle addMessage(@NonNull CharSequence text, long timestamp,
7071 @Nullable Person sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08007072 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04007073 }
7074
7075 /**
7076 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08007077 *
7078 * <p>The messages should be added in chronologic order, i.e. the oldest first,
7079 * the newest last.
7080 *
Alex Hillsfc737de2016-03-23 17:33:02 -04007081 * @param message The {@link Message} to be displayed
7082 * @return this object for method chaining
7083 */
7084 public MessagingStyle addMessage(Message message) {
7085 mMessages.add(message);
7086 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
7087 mMessages.remove(0);
7088 }
7089 return this;
7090 }
7091
7092 /**
Adrian Roos437cd562017-01-18 15:47:03 -08007093 * Adds a {@link Message} for historic context in this notification.
7094 *
7095 * <p>Messages should be added as historic if they are not the main subject of the
7096 * notification but may give context to a conversation. The system may choose to present
7097 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
7098 *
7099 * <p>The messages should be added in chronologic order, i.e. the oldest first,
7100 * the newest last.
7101 *
7102 * @param message The historic {@link Message} to be added
7103 * @return this object for method chaining
7104 */
7105 public MessagingStyle addHistoricMessage(Message message) {
7106 mHistoricMessages.add(message);
7107 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
7108 mHistoricMessages.remove(0);
7109 }
7110 return this;
7111 }
7112
7113 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007114 * Gets the list of {@code Message} objects that represent the notification
7115 */
7116 public List<Message> getMessages() {
7117 return mMessages;
7118 }
7119
7120 /**
Adrian Roos437cd562017-01-18 15:47:03 -08007121 * Gets the list of historic {@code Message}s in the notification.
7122 */
7123 public List<Message> getHistoricMessages() {
7124 return mHistoricMessages;
7125 }
7126
7127 /**
Selim Cinekce8794f2018-05-23 16:46:05 -07007128 * Sets whether this conversation notification represents a group. If the app is targeting
7129 * Android P, this is required if the app wants to display the largeIcon set with
7130 * {@link Notification.Builder#setLargeIcon(Bitmap)}, otherwise it will be hidden.
Kodlee Yin14656422017-12-22 17:00:46 -08007131 *
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007132 * @param isGroupConversation {@code true} if the conversation represents a group,
7133 * {@code false} otherwise.
7134 * @return this object for method chaining
7135 */
7136 public MessagingStyle setGroupConversation(boolean isGroupConversation) {
7137 mIsGroupConversation = isGroupConversation;
7138 return this;
7139 }
7140
7141 /**
Kodlee Yin14656422017-12-22 17:00:46 -08007142 * Returns {@code true} if this notification represents a group conversation, otherwise
7143 * {@code false}.
7144 *
7145 * <p> If the application that generated this {@link MessagingStyle} targets an SDK version
7146 * less than {@link Build.VERSION_CODES#P}, this method becomes dependent on whether or
7147 * not the conversation title is set; returning {@code true} if the conversation title is
7148 * a non-null value, or {@code false} otherwise. From {@link Build.VERSION_CODES#P} forward,
7149 * this method returns what's set by {@link #setGroupConversation(boolean)} allowing for
7150 * named, non-group conversations.
7151 *
7152 * @see #setConversationTitle(CharSequence)
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007153 */
7154 public boolean isGroupConversation() {
Kodlee Yin14656422017-12-22 17:00:46 -08007155 // When target SDK version is < P, a non-null conversation title dictates if this is
7156 // as group conversation.
7157 if (mBuilder != null
7158 && mBuilder.mContext.getApplicationInfo().targetSdkVersion
7159 < Build.VERSION_CODES.P) {
7160 return mConversationTitle != null;
7161 }
7162
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007163 return mIsGroupConversation;
7164 }
7165
7166 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007167 * @hide
7168 */
7169 @Override
7170 public void addExtras(Bundle extras) {
7171 super.addExtras(extras);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007172 if (mUser != null) {
7173 // For legacy usages
7174 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUser.getName());
7175 extras.putParcelable(EXTRA_MESSAGING_PERSON, mUser);
Alex Hillsfc737de2016-03-23 17:33:02 -04007176 }
7177 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007178 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04007179 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007180 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
7181 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04007182 }
Adrian Roos437cd562017-01-18 15:47:03 -08007183 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
7184 Message.getBundleArrayForMessages(mHistoricMessages));
7185 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007186
7187 fixTitleAndTextExtras(extras);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007188 extras.putBoolean(EXTRA_IS_GROUP_CONVERSATION, mIsGroupConversation);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007189 }
7190
7191 private void fixTitleAndTextExtras(Bundle extras) {
7192 Message m = findLatestIncomingMessage();
7193 CharSequence text = (m == null) ? null : m.mText;
7194 CharSequence sender = m == null ? null
Selim Cinekcb8b9852017-12-15 18:01:52 -08007195 : m.mSender == null || TextUtils.isEmpty(m.mSender.getName())
7196 ? mUser.getName() : m.mSender.getName();
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007197 CharSequence title;
7198 if (!TextUtils.isEmpty(mConversationTitle)) {
7199 if (!TextUtils.isEmpty(sender)) {
7200 BidiFormatter bidi = BidiFormatter.getInstance();
7201 title = mBuilder.mContext.getString(
7202 com.android.internal.R.string.notification_messaging_title_template,
Selim Cinekcb8b9852017-12-15 18:01:52 -08007203 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(sender));
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007204 } else {
7205 title = mConversationTitle;
7206 }
7207 } else {
7208 title = sender;
7209 }
7210
7211 if (title != null) {
7212 extras.putCharSequence(EXTRA_TITLE, title);
7213 }
7214 if (text != null) {
7215 extras.putCharSequence(EXTRA_TEXT, text);
7216 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007217 }
7218
7219 /**
7220 * @hide
7221 */
7222 @Override
7223 protected void restoreFromExtras(Bundle extras) {
7224 super.restoreFromExtras(extras);
7225
Selim Cinekcb8b9852017-12-15 18:01:52 -08007226 mUser = extras.getParcelable(EXTRA_MESSAGING_PERSON);
7227 if (mUser == null) {
7228 CharSequence displayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
Selim Cinek9acd6732018-03-23 16:39:02 -07007229 mUser = new Person.Builder().setName(displayName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007230 }
Adrian Roos96b7e202016-05-17 13:50:38 -07007231 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08007232 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007233 mMessages = Message.getMessagesFromBundleArray(messages);
Adrian Roos437cd562017-01-18 15:47:03 -08007234 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007235 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007236 mIsGroupConversation = extras.getBoolean(EXTRA_IS_GROUP_CONVERSATION);
Alex Hillsfc737de2016-03-23 17:33:02 -04007237 }
7238
7239 /**
7240 * @hide
7241 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07007242 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007243 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07007244 mBuilder.mOriginalActions = mBuilder.mActions;
7245 mBuilder.mActions = new ArrayList<>();
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007246 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007247 false /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007248 mBuilder.mActions = mBuilder.mOriginalActions;
7249 mBuilder.mOriginalActions = null;
7250 return remoteViews;
Adrian Roosc1a80b02016-04-05 14:54:55 -07007251 }
7252
Julia Reynolds7217dc92018-03-07 12:12:09 -05007253 /**
7254 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04007255 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05007256 */
7257 @Override
7258 public boolean areNotificationsVisiblyDifferent(Style other) {
7259 if (other == null || getClass() != other.getClass()) {
7260 return true;
7261 }
7262 MessagingStyle newS = (MessagingStyle) other;
7263 List<MessagingStyle.Message> oldMs = getMessages();
7264 List<MessagingStyle.Message> newMs = newS.getMessages();
7265
Dan Sandler7d67bd42018-05-15 14:06:38 -04007266 if (oldMs == null || newMs == null) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007267 newMs = new ArrayList<>();
7268 }
7269
7270 int n = oldMs.size();
7271 if (n != newMs.size()) {
7272 return true;
7273 }
7274 for (int i = 0; i < n; i++) {
7275 MessagingStyle.Message oldM = oldMs.get(i);
7276 MessagingStyle.Message newM = newMs.get(i);
Dan Sandler7d67bd42018-05-15 14:06:38 -04007277 if (!Objects.equals(
7278 String.valueOf(oldM.getText()),
7279 String.valueOf(newM.getText()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007280 return true;
7281 }
7282 if (!Objects.equals(oldM.getDataUri(), newM.getDataUri())) {
7283 return true;
7284 }
Dan Sandler7d67bd42018-05-15 14:06:38 -04007285 String oldSender = String.valueOf(oldM.getSenderPerson() == null
7286 ? oldM.getSender()
7287 : oldM.getSenderPerson().getName());
7288 String newSender = String.valueOf(newM.getSenderPerson() == null
7289 ? newM.getSender()
7290 : newM.getSenderPerson().getName());
Julia Reynolds7217dc92018-03-07 12:12:09 -05007291 if (!Objects.equals(oldSender, newSender)) {
7292 return true;
7293 }
7294
7295 String oldKey = oldM.getSenderPerson() == null
7296 ? null : oldM.getSenderPerson().getKey();
7297 String newKey = newM.getSenderPerson() == null
7298 ? null : newM.getSenderPerson().getKey();
7299 if (!Objects.equals(oldKey, newKey)) {
7300 return true;
7301 }
7302 // Other fields (like timestamp) intentionally excluded
7303 }
7304 return false;
7305 }
7306
Adrian Roosc1a80b02016-04-05 14:54:55 -07007307 private Message findLatestIncomingMessage() {
Selim Cinek88188f22017-09-19 16:46:56 -07007308 return findLatestIncomingMessage(mMessages);
7309 }
7310
7311 /**
7312 * @hide
7313 */
7314 @Nullable
7315 public static Message findLatestIncomingMessage(
7316 List<Message> messages) {
7317 for (int i = messages.size() - 1; i >= 0; i--) {
7318 Message m = messages.get(i);
Adrian Roosc1a80b02016-04-05 14:54:55 -07007319 // Incoming messages have a non-empty sender.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007320 if (m.mSender != null && !TextUtils.isEmpty(m.mSender.getName())) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07007321 return m;
7322 }
7323 }
Selim Cinek88188f22017-09-19 16:46:56 -07007324 if (!messages.isEmpty()) {
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007325 // No incoming messages, fall back to outgoing message
Selim Cinek88188f22017-09-19 16:46:56 -07007326 return messages.get(messages.size() - 1);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007327 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07007328 return null;
7329 }
7330
7331 /**
7332 * @hide
7333 */
7334 @Override
7335 public RemoteViews makeBigContentView() {
Selim Cinek384804b2018-04-18 14:31:07 +08007336 return makeMessagingView(false /* displayImagesAtEnd */, true /* hideLargeIcon */);
Selim Cinekafeed292017-12-12 17:32:44 -08007337 }
7338
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007339 /**
7340 * Create a messaging layout.
7341 *
7342 * @param displayImagesAtEnd should images be displayed at the end of the content instead
7343 * of inline.
Selim Cinek384804b2018-04-18 14:31:07 +08007344 * @param hideRightIcons Should the reply affordance be shown at the end of the notification
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007345 * @return the created remoteView.
7346 */
Selim Cinekafeed292017-12-12 17:32:44 -08007347 @NonNull
Selim Cinek384804b2018-04-18 14:31:07 +08007348 private RemoteViews makeMessagingView(boolean displayImagesAtEnd, boolean hideRightIcons) {
Selim Cinek88188f22017-09-19 16:46:56 -07007349 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
Adrian Roosc1a80b02016-04-05 14:54:55 -07007350 ? super.mBigContentTitle
7351 : mConversationTitle;
Selim Cinekce8794f2018-05-23 16:46:05 -07007352 boolean atLeastP = mBuilder.mContext.getApplicationInfo().targetSdkVersion
7353 >= Build.VERSION_CODES.P;
7354 boolean isOneToOne;
Selim Cinek2dd3e722018-01-19 11:06:06 -08007355 CharSequence nameReplacement = null;
Selim Cinekce8794f2018-05-23 16:46:05 -07007356 Icon avatarReplacement = null;
7357 if (!atLeastP) {
7358 isOneToOne = TextUtils.isEmpty(conversationTitle);
7359 avatarReplacement = mBuilder.mN.mLargeIcon;
7360 if (hasOnlyWhiteSpaceSenders()) {
7361 isOneToOne = true;
7362 nameReplacement = conversationTitle;
7363 conversationTitle = null;
7364 }
7365 } else {
7366 isOneToOne = !isGroupConversation();
Adrian Roosb1f427c2016-05-26 12:27:15 -07007367 }
Selim Cinek384804b2018-04-18 14:31:07 +08007368 TemplateBindResult bindResult = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007369 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).title(
7370 conversationTitle).text(null)
7371 .hideLargeIcon(hideRightIcons || isOneToOne)
7372 .hideReplyIcon(hideRightIcons)
7373 .headerTextSecondary(conversationTitle);
Adrian Roos48d746a2016-04-12 14:57:28 -07007374 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07007375 mBuilder.getMessagingLayoutResource(),
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007376 p,
Selim Cinek384804b2018-04-18 14:31:07 +08007377 bindResult);
Selim Cinek88188f22017-09-19 16:46:56 -07007378 addExtras(mBuilder.mN.extras);
Selim Cinekafeed292017-12-12 17:32:44 -08007379 // also update the end margin if there is an image
Selim Cinek1c72fa02018-04-23 18:00:54 +08007380 contentView.setViewLayoutMarginEnd(R.id.notification_messaging,
7381 bindResult.getIconMarginEnd());
Selim Cinek88188f22017-09-19 16:46:56 -07007382 contentView.setInt(R.id.status_bar_latest_event_content, "setLayoutColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007383 mBuilder.isColorized(p) ? mBuilder.getPrimaryTextColor(p)
7384 : mBuilder.resolveContrastColor(p));
Kenny Guy14d035c2018-05-02 19:10:36 +01007385 contentView.setInt(R.id.status_bar_latest_event_content, "setSenderTextColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007386 mBuilder.getPrimaryTextColor(p));
Kenny Guy14d035c2018-05-02 19:10:36 +01007387 contentView.setInt(R.id.status_bar_latest_event_content, "setMessageTextColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007388 mBuilder.getSecondaryTextColor(p));
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007389 contentView.setBoolean(R.id.status_bar_latest_event_content, "setDisplayImagesAtEnd",
7390 displayImagesAtEnd);
Selim Cinekce8794f2018-05-23 16:46:05 -07007391 contentView.setIcon(R.id.status_bar_latest_event_content, "setAvatarReplacement",
7392 avatarReplacement);
Selim Cinek2dd3e722018-01-19 11:06:06 -08007393 contentView.setCharSequence(R.id.status_bar_latest_event_content, "setNameReplacement",
7394 nameReplacement);
Selim Cinek88188f22017-09-19 16:46:56 -07007395 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsOneToOne",
7396 isOneToOne);
7397 contentView.setBundle(R.id.status_bar_latest_event_content, "setData",
7398 mBuilder.mN.extras);
Alex Hillsfc737de2016-03-23 17:33:02 -04007399 return contentView;
7400 }
7401
Selim Cinekf7409db2017-10-24 16:17:14 -07007402 private boolean hasOnlyWhiteSpaceSenders() {
7403 for (int i = 0; i < mMessages.size(); i++) {
7404 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007405 Person sender = m.getSenderPerson();
7406 if (sender != null && !isWhiteSpace(sender.getName())) {
Selim Cinekf7409db2017-10-24 16:17:14 -07007407 return false;
7408 }
7409 }
7410 return true;
7411 }
7412
7413 private boolean isWhiteSpace(CharSequence sender) {
7414 if (TextUtils.isEmpty(sender)) {
7415 return true;
7416 }
7417 if (sender.toString().matches("^\\s*$")) {
7418 return true;
7419 }
7420 // Let's check if we only have 0 whitespace chars. Some apps did this as a workaround
7421 // For the presentation that we had.
7422 for (int i = 0; i < sender.length(); i++) {
7423 char c = sender.charAt(i);
7424 if (c != '\u200B') {
7425 return false;
7426 }
7427 }
7428 return true;
7429 }
7430
Selim Cinek88188f22017-09-19 16:46:56 -07007431 private CharSequence createConversationTitleFromMessages() {
7432 ArraySet<CharSequence> names = new ArraySet<>();
7433 for (int i = 0; i < mMessages.size(); i++) {
7434 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007435 Person sender = m.getSenderPerson();
Selim Cinek88188f22017-09-19 16:46:56 -07007436 if (sender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007437 names.add(sender.getName());
Selim Cinek88188f22017-09-19 16:46:56 -07007438 }
7439 }
7440 SpannableStringBuilder title = new SpannableStringBuilder();
7441 int size = names.size();
7442 for (int i = 0; i < size; i++) {
7443 CharSequence name = names.valueAt(i);
7444 if (!TextUtils.isEmpty(title)) {
7445 title.append(", ");
7446 }
7447 title.append(BidiFormatter.getInstance().unicodeWrap(name));
7448 }
7449 return title;
7450 }
7451
Adrian Roosdedd1df2016-04-26 16:38:47 -07007452 /**
7453 * @hide
7454 */
7455 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007456 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007457 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007458 true /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007459 remoteViews.setInt(R.id.notification_messaging, "setMaxDisplayedLines", 1);
7460 return remoteViews;
Adrian Roosdedd1df2016-04-26 16:38:47 -07007461 }
7462
Adrian Roosc1a80b02016-04-05 14:54:55 -07007463 private static TextAppearanceSpan makeFontColorSpan(int color) {
7464 return new TextAppearanceSpan(null, 0, 0,
7465 ColorStateList.valueOf(color), null);
7466 }
7467
Alex Hillsd9b04d92016-04-11 16:38:16 -04007468 public static final class Message {
Tony Mak09db2ea2018-06-27 18:12:48 +01007469 /** @hide */
7470 public static final String KEY_TEXT = "text";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007471 static final String KEY_TIMESTAMP = "time";
7472 static final String KEY_SENDER = "sender";
Selim Cinekcb8b9852017-12-15 18:01:52 -08007473 static final String KEY_SENDER_PERSON = "sender_person";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007474 static final String KEY_DATA_MIME_TYPE = "type";
7475 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08007476 static final String KEY_EXTRAS_BUNDLE = "extras";
Kenny Guya0f6de82018-04-06 16:20:16 +01007477 static final String KEY_REMOTE_INPUT_HISTORY = "remote_input_history";
Alex Hillsfc737de2016-03-23 17:33:02 -04007478
7479 private final CharSequence mText;
7480 private final long mTimestamp;
Selim Cinekcb8b9852017-12-15 18:01:52 -08007481 @Nullable
7482 private final Person mSender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007483 /** True if this message was generated from the extra
7484 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}
7485 */
7486 private final boolean mRemoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007487
Shane Brennan5a871862017-03-11 13:14:17 -08007488 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007489 private String mDataMimeType;
7490 private Uri mDataUri;
7491
7492 /**
7493 * Constructor
7494 * @param text A {@link CharSequence} to be displayed as the message content
7495 * @param timestamp Time at which the message arrived
7496 * @param sender A {@link CharSequence} to be used for displaying the name of the
7497 * sender. Should be <code>null</code> for messages by the current user, in which case
7498 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
7499 * Should be unique amongst all individuals in the conversation, and should be
7500 * consistent during re-posts of the notification.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007501 *
7502 * @deprecated use {@code Message(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04007503 */
7504 public Message(CharSequence text, long timestamp, CharSequence sender){
Selim Cinek9acd6732018-03-23 16:39:02 -07007505 this(text, timestamp, sender == null ? null
7506 : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08007507 }
7508
7509 /**
7510 * Constructor
7511 * @param text A {@link CharSequence} to be displayed as the message content
7512 * @param timestamp Time at which the message arrived
7513 * @param sender The {@link Person} who sent the message.
7514 * Should be <code>null</code> for messages by the current user, in which case
7515 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7516 * <p>
Selim Cinek9acd6732018-03-23 16:39:02 -07007517 * The person provided should contain an Icon, set with
7518 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7519 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7520 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7521 * to differentiate between the different users.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007522 * </p>
7523 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007524 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender) {
Kenny Guya0f6de82018-04-06 16:20:16 +01007525 this(text, timestamp, sender, false /* remoteHistory */);
7526 }
7527
7528 /**
7529 * Constructor
7530 * @param text A {@link CharSequence} to be displayed as the message content
7531 * @param timestamp Time at which the message arrived
7532 * @param sender The {@link Person} who sent the message.
7533 * Should be <code>null</code> for messages by the current user, in which case
7534 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7535 * @param remoteInputHistory True if the messages was generated from the extra
7536 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7537 * <p>
7538 * The person provided should contain an Icon, set with
7539 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7540 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7541 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7542 * to differentiate between the different users.
7543 * </p>
7544 * @hide
7545 */
7546 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender,
7547 boolean remoteInputHistory) {
Alex Hillsfc737de2016-03-23 17:33:02 -04007548 mText = text;
7549 mTimestamp = timestamp;
7550 mSender = sender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007551 mRemoteInputHistory = remoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007552 }
7553
7554 /**
7555 * Sets a binary blob of data and an associated MIME type for a message. In the case
7556 * where the platform doesn't support the MIME type, the original text provided in the
7557 * constructor will be used.
7558 * @param dataMimeType The MIME type of the content. See
7559 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
7560 * types on Android and Android Wear.
7561 * @param dataUri The uri containing the content whose type is given by the MIME type.
7562 * <p class="note">
7563 * <ol>
7564 * <li>Notification Listeners including the System UI need permission to access the
7565 * data the Uri points to. The recommended ways to do this are:</li>
7566 * <li>Store the data in your own ContentProvider, making sure that other apps have
7567 * the correct permission to access your provider. The preferred mechanism for
7568 * providing access is to use per-URI permissions which are temporary and only
7569 * grant access to the receiving application. An easy way to create a
7570 * ContentProvider like this is to use the FileProvider helper class.</li>
7571 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
7572 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
7573 * also store non-media types (see MediaStore.Files for more info). Files can be
7574 * inserted into the MediaStore using scanFile() after which a content:// style
7575 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
7576 * Note that once added to the system MediaStore the content is accessible to any
7577 * app on the device.</li>
7578 * </ol>
7579 * @return this object for method chaining
7580 */
7581 public Message setData(String dataMimeType, Uri dataUri) {
7582 mDataMimeType = dataMimeType;
7583 mDataUri = dataUri;
7584 return this;
7585 }
7586
Alex Hillsfc737de2016-03-23 17:33:02 -04007587 /**
7588 * Get the text to be used for this message, or the fallback text if a type and content
7589 * Uri have been set
7590 */
7591 public CharSequence getText() {
7592 return mText;
7593 }
7594
7595 /**
7596 * Get the time at which this message arrived
7597 */
7598 public long getTimestamp() {
7599 return mTimestamp;
7600 }
7601
7602 /**
Shane Brennan5a871862017-03-11 13:14:17 -08007603 * Get the extras Bundle for this message.
7604 */
7605 public Bundle getExtras() {
7606 return mExtras;
7607 }
7608
7609 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007610 * Get the text used to display the contact's name in the messaging experience
Selim Cinekcb8b9852017-12-15 18:01:52 -08007611 *
7612 * @deprecated use {@link #getSenderPerson()}
Alex Hillsfc737de2016-03-23 17:33:02 -04007613 */
7614 public CharSequence getSender() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007615 return mSender == null ? null : mSender.getName();
7616 }
7617
7618 /**
7619 * Get the sender associated with this message.
7620 */
7621 @Nullable
7622 public Person getSenderPerson() {
Alex Hillsfc737de2016-03-23 17:33:02 -04007623 return mSender;
7624 }
7625
7626 /**
7627 * Get the MIME type of the data pointed to by the Uri
7628 */
7629 public String getDataMimeType() {
7630 return mDataMimeType;
7631 }
7632
7633 /**
koprivaa1a78482018-10-09 10:09:23 -07007634 * Get the Uri pointing to the content of the message. Can be null, in which case
Alex Hillsfc737de2016-03-23 17:33:02 -04007635 * {@see #getText()} is used.
7636 */
7637 public Uri getDataUri() {
7638 return mDataUri;
7639 }
7640
Kenny Guya0f6de82018-04-06 16:20:16 +01007641 /**
7642 * @return True if the message was generated from
7643 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7644 * @hide
7645 */
7646 public boolean isRemoteInputHistory() {
7647 return mRemoteInputHistory;
7648 }
7649
Beverlye98937a2018-11-15 10:18:57 -05007650 /**
7651 * @hide
7652 */
7653 @VisibleForTesting
7654 public Bundle toBundle() {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007655 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007656 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007657 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04007658 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007659 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04007660 if (mSender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007661 // Legacy listeners need this
7662 bundle.putCharSequence(KEY_SENDER, mSender.getName());
7663 bundle.putParcelable(KEY_SENDER_PERSON, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04007664 }
7665 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007666 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04007667 }
7668 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007669 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04007670 }
Shane Brennan5a871862017-03-11 13:14:17 -08007671 if (mExtras != null) {
7672 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
7673 }
Kenny Guya0f6de82018-04-06 16:20:16 +01007674 if (mRemoteInputHistory) {
7675 bundle.putBoolean(KEY_REMOTE_INPUT_HISTORY, mRemoteInputHistory);
7676 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007677 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04007678 }
7679
Alex Hillsd9b04d92016-04-11 16:38:16 -04007680 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
7681 Bundle[] bundles = new Bundle[messages.size()];
7682 final int N = messages.size();
7683 for (int i = 0; i < N; i++) {
7684 bundles[i] = messages.get(i).toBundle();
7685 }
7686 return bundles;
7687 }
7688
Selim Cinek88188f22017-09-19 16:46:56 -07007689 /**
7690 * @return A list of messages read from the bundles.
7691 *
7692 * @hide
7693 */
7694 public static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
7695 if (bundles == null) {
7696 return new ArrayList<>();
7697 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007698 List<Message> messages = new ArrayList<>(bundles.length);
7699 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07007700 if (bundles[i] instanceof Bundle) {
7701 Message message = getMessageFromBundle((Bundle)bundles[i]);
7702 if (message != null) {
7703 messages.add(message);
7704 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007705 }
7706 }
7707 return messages;
7708 }
7709
Selim Cinekb0dc61b2018-05-22 18:49:36 -07007710 /**
7711 * @return The message that is stored in the bundle or null if the message couldn't be
7712 * resolved.
7713 *
7714 * @hide
7715 */
7716 @Nullable
7717 public static Message getMessageFromBundle(Bundle bundle) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007718 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07007719 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007720 return null;
7721 } else {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007722
7723 Person senderPerson = bundle.getParcelable(KEY_SENDER_PERSON);
7724 if (senderPerson == null) {
7725 // Legacy apps that use compat don't actually provide the sender objects
7726 // We need to fix the compat version to provide people / use
7727 // the native api instead
7728 CharSequence senderName = bundle.getCharSequence(KEY_SENDER);
7729 if (senderName != null) {
Selim Cinek9acd6732018-03-23 16:39:02 -07007730 senderPerson = new Person.Builder().setName(senderName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007731 }
7732 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007733 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
Selim Cinekcb8b9852017-12-15 18:01:52 -08007734 bundle.getLong(KEY_TIMESTAMP),
Kenny Guya0f6de82018-04-06 16:20:16 +01007735 senderPerson,
7736 bundle.getBoolean(KEY_REMOTE_INPUT_HISTORY, false));
Alex Hillsd9b04d92016-04-11 16:38:16 -04007737 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
7738 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007739 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
7740 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04007741 }
Shane Brennan5a871862017-03-11 13:14:17 -08007742 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
7743 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
7744 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007745 return message;
7746 }
7747 } catch (ClassCastException e) {
7748 return null;
7749 }
7750 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007751 }
7752 }
7753
7754 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04007755 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08007756 *
Robert Ly91c5ce32014-06-08 15:37:00 -07007757 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04007758 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07007759 * Notification notif = new Notification.Builder(mContext)
7760 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
7761 * .setContentText(subject)
7762 * .setSmallIcon(R.drawable.new_mail)
7763 * .setLargeIcon(aBitmap)
7764 * .setStyle(new Notification.InboxStyle()
7765 * .addLine(str1)
7766 * .addLine(str2)
7767 * .setContentTitle(&quot;&quot;)
7768 * .setSummaryText(&quot;+3 more&quot;))
7769 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04007770 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08007771 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04007772 * @see Notification#bigContentView
7773 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007774 public static class InboxStyle extends Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07007775
7776 /**
7777 * The number of lines of remote input history allowed until we start reducing lines.
7778 */
7779 private static final int NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION = 1;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007780 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
7781
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007782 public InboxStyle() {
7783 }
7784
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007785 /**
7786 * @deprecated use {@code InboxStyle()}.
7787 */
7788 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04007789 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007790 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04007791 }
7792
Chris Wrend6297db2012-05-03 16:20:13 -04007793 /**
7794 * Overrides ContentTitle in the big form of the template.
7795 * This defaults to the value passed to setContentTitle().
7796 */
7797 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007798 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04007799 return this;
7800 }
7801
7802 /**
7803 * Set the first line of text after the detail section in the big form of the template.
7804 */
7805 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007806 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04007807 return this;
7808 }
7809
Chris Wren0bd664d2012-08-01 13:56:56 -04007810 /**
7811 * Append a line to the digest section of the Inbox notification.
7812 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04007813 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007814 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04007815 return this;
7816 }
7817
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007818 /**
7819 * @hide
7820 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05007821 public ArrayList<CharSequence> getLines() {
7822 return mTexts;
7823 }
7824
7825 /**
7826 * @hide
7827 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007828 public void addExtras(Bundle extras) {
7829 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007830
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007831 CharSequence[] a = new CharSequence[mTexts.size()];
7832 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
7833 }
7834
Christoph Studer4600f9b2014-07-22 22:44:43 +02007835 /**
7836 * @hide
7837 */
7838 @Override
7839 protected void restoreFromExtras(Bundle extras) {
7840 super.restoreFromExtras(extras);
7841
7842 mTexts.clear();
7843 if (extras.containsKey(EXTRA_TEXT_LINES)) {
7844 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
7845 }
7846 }
7847
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007848 /**
7849 * @hide
7850 */
7851 public RemoteViews makeBigContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007852 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder).text(null);
Selim Cinek384804b2018-04-18 14:31:07 +08007853 TemplateBindResult result = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007854 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource(), p, result);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007855
Chris Wrend6297db2012-05-03 16:20:13 -04007856 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 -04007857 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04007858
Chris Wren4ed80d52012-05-17 09:30:03 -04007859 // Make sure all rows are gone in case we reuse a view.
7860 for (int rowId : rowIds) {
7861 contentView.setViewVisibility(rowId, View.GONE);
7862 }
7863
Daniel Sandler879c5e02012-04-17 16:46:51 -04007864 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07007865 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7866 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08007867 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07007868 int onlyViewId = 0;
7869 int maxRows = rowIds.length;
7870 if (mBuilder.mActions.size() > 0) {
7871 maxRows--;
7872 }
Selim Cinekbee4e072018-05-21 22:06:43 -07007873 CharSequence[] remoteInputHistory = mBuilder.mN.extras.getCharSequenceArray(
7874 EXTRA_REMOTE_INPUT_HISTORY);
7875 if (remoteInputHistory != null
7876 && remoteInputHistory.length > NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION) {
7877 // Let's remove some messages to make room for the remote input history.
7878 // 1 is always able to fit, but let's remove them if they are 2 or 3
7879 int numRemoteInputs = Math.min(remoteInputHistory.length,
7880 MAX_REMOTE_INPUT_HISTORY_LINES);
7881 int totalNumRows = mTexts.size() + numRemoteInputs
7882 - NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION;
7883 if (totalNumRows > maxRows) {
7884 int overflow = totalNumRows - maxRows;
7885 if (mTexts.size() > maxRows) {
7886 // Heuristic: if the Texts don't fit anyway, we'll rather drop the last
7887 // few messages, even with the remote input
7888 maxRows -= overflow;
7889 } else {
7890 // otherwise we drop the first messages
7891 i = overflow;
7892 }
7893 }
7894 }
Selim Cinek07c80172016-04-21 16:40:47 -07007895 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007896 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07007897 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007898 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07007899 contentView.setTextViewText(rowIds[i],
7900 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007901 mBuilder.setTextViewColorSecondary(contentView, rowIds[i], p);
Selim Cinek07c80172016-04-21 16:40:47 -07007902 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek384804b2018-04-18 14:31:07 +08007903 handleInboxImageMargin(contentView, rowIds[i], first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007904 result.getIconMarginEnd());
Selim Cinek07c80172016-04-21 16:40:47 -07007905 if (first) {
7906 onlyViewId = rowIds[i];
7907 } else {
7908 onlyViewId = 0;
7909 }
Selim Cinek247fa012016-02-18 09:50:48 -08007910 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007911 }
7912 i++;
7913 }
Selim Cinek07c80172016-04-21 16:40:47 -07007914 if (onlyViewId != 0) {
7915 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
7916 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7917 R.dimen.notification_text_margin_top);
7918 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
7919 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007920
Daniel Sandler879c5e02012-04-17 16:46:51 -04007921 return contentView;
7922 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007923
Julia Reynolds7217dc92018-03-07 12:12:09 -05007924 /**
7925 * @hide
7926 */
7927 @Override
7928 public boolean areNotificationsVisiblyDifferent(Style other) {
7929 if (other == null || getClass() != other.getClass()) {
7930 return true;
7931 }
7932 InboxStyle newS = (InboxStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04007933
7934 final ArrayList<CharSequence> myLines = getLines();
7935 final ArrayList<CharSequence> newLines = newS.getLines();
7936 final int n = myLines.size();
7937 if (n != newLines.size()) {
7938 return true;
7939 }
7940
7941 for (int i = 0; i < n; i++) {
7942 if (!Objects.equals(
7943 String.valueOf(myLines.get(i)),
7944 String.valueOf(newLines.get(i)))) {
7945 return true;
7946 }
7947 }
7948 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05007949 }
7950
Selim Cinek384804b2018-04-18 14:31:07 +08007951 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007952 int marginEndValue) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08007953 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08007954 if (first) {
7955 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
7956 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
7957 boolean hasProgress = max != 0 || ind;
Selim Cinek384804b2018-04-18 14:31:07 +08007958 if (!hasProgress) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08007959 endMargin = marginEndValue;
Selim Cinek247fa012016-02-18 09:50:48 -08007960 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007961 }
Selim Cinek1c72fa02018-04-23 18:00:54 +08007962 contentView.setViewLayoutMarginEnd(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08007963 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04007964 }
Dan Sandler842dd772014-05-15 09:36:47 -04007965
7966 /**
7967 * Notification style for media playback notifications.
7968 *
7969 * In the expanded form, {@link Notification#bigContentView}, up to 5
7970 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04007971 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04007972 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
7973 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
7974 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08007975 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04007976 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
7977 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01007978 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04007979 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08007980 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007981 * Notifications created with MediaStyle will have their category set to
7982 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
7983 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08007984 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07007985 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
7986 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04007987 * the System UI can identify this as a notification representing an active media session
7988 * and respond accordingly (by showing album artwork in the lockscreen, for example).
7989 *
Selim Cinek99104832017-01-25 14:47:33 -08007990 * <p>
7991 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
7992 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
7993 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
7994 * <p>
7995 *
Dan Sandler842dd772014-05-15 09:36:47 -04007996 * To use this style with your Notification, feed it to
7997 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7998 * <pre class="prettyprint">
7999 * Notification noti = new Notification.Builder()
8000 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01008001 * .setContentTitle(&quot;Track title&quot;)
8002 * .setContentText(&quot;Artist - Album&quot;)
8003 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01008004 * .setStyle(<b>new Notification.MediaStyle()</b>
8005 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04008006 * .build();
8007 * </pre>
8008 *
8009 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08008010 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04008011 */
8012 public static class MediaStyle extends Style {
Gus Prevas9cc96602018-10-11 11:24:23 -04008013 // Changing max media buttons requires also changing templates
8014 // (notification_template_material_media and notification_template_material_big_media).
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008015 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04008016 static final int MAX_MEDIA_BUTTONS = 5;
Gus Prevas9cc96602018-10-11 11:24:23 -04008017 @IdRes private static final int[] MEDIA_BUTTON_IDS = {
8018 R.id.action0,
8019 R.id.action1,
8020 R.id.action2,
8021 R.id.action3,
8022 R.id.action4,
8023 };
Dan Sandler842dd772014-05-15 09:36:47 -04008024
8025 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07008026 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04008027
8028 public MediaStyle() {
8029 }
8030
Adrian Roosf5faf9d2016-05-23 13:56:15 -07008031 /**
8032 * @deprecated use {@code MediaStyle()}.
8033 */
8034 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04008035 public MediaStyle(Builder builder) {
8036 setBuilder(builder);
8037 }
8038
8039 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008040 * 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 -04008041 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008042 *
8043 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04008044 */
8045 public MediaStyle setShowActionsInCompactView(int...actions) {
8046 mActionsToShowInCompact = actions;
8047 return this;
8048 }
8049
8050 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07008051 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
8052 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04008053 */
Jeff Browndba34ba2014-06-24 20:46:03 -07008054 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04008055 mToken = token;
8056 return this;
8057 }
8058
Christoph Studer4600f9b2014-07-22 22:44:43 +02008059 /**
8060 * @hide
8061 */
Dan Sandler842dd772014-05-15 09:36:47 -04008062 @Override
Mathew Inwood61e8ae62018-08-14 14:17:44 +01008063 @UnsupportedAppUsage
Dan Sandler842dd772014-05-15 09:36:47 -04008064 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02008065 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01008066 if (wip.category == null) {
8067 wip.category = Notification.CATEGORY_TRANSPORT;
8068 }
Dan Sandler842dd772014-05-15 09:36:47 -04008069 return wip;
8070 }
8071
Christoph Studer4600f9b2014-07-22 22:44:43 +02008072 /**
8073 * @hide
8074 */
8075 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008076 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04008077 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02008078 }
8079
8080 /**
8081 * @hide
8082 */
8083 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04008084 public RemoteViews makeBigContentView() {
8085 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02008086 }
8087
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008088 /**
8089 * @hide
8090 */
8091 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008092 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008093 RemoteViews expanded = makeMediaBigContentView();
8094 return expanded != null ? expanded : makeMediaContentView();
8095 }
8096
Dan Sandler842dd772014-05-15 09:36:47 -04008097 /** @hide */
8098 @Override
8099 public void addExtras(Bundle extras) {
8100 super.addExtras(extras);
8101
8102 if (mToken != null) {
8103 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
8104 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01008105 if (mActionsToShowInCompact != null) {
8106 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
8107 }
Dan Sandler842dd772014-05-15 09:36:47 -04008108 }
8109
Christoph Studer4600f9b2014-07-22 22:44:43 +02008110 /**
8111 * @hide
8112 */
8113 @Override
8114 protected void restoreFromExtras(Bundle extras) {
8115 super.restoreFromExtras(extras);
8116
8117 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
8118 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
8119 }
8120 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
8121 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
8122 }
8123 }
8124
Julia Reynolds7217dc92018-03-07 12:12:09 -05008125 /**
8126 * @hide
8127 */
8128 @Override
8129 public boolean areNotificationsVisiblyDifferent(Style other) {
8130 if (other == null || getClass() != other.getClass()) {
8131 return true;
8132 }
8133 // All fields to compare are on the Notification object
8134 return false;
8135 }
8136
Gus Prevas9cc96602018-10-11 11:24:23 -04008137 private void bindMediaActionButton(RemoteViews container, @IdRes int buttonId,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008138 Action action, StandardTemplateParams p) {
Dan Sandler842dd772014-05-15 09:36:47 -04008139 final boolean tombstone = (action.actionIntent == null);
Gus Prevas9cc96602018-10-11 11:24:23 -04008140 container.setViewVisibility(buttonId, View.VISIBLE);
8141 container.setImageViewIcon(buttonId, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07008142
8143 // If the action buttons should not be tinted, then just use the default
8144 // notification color. Otherwise, just use the passed-in color.
Gus Prevas9cc96602018-10-11 11:24:23 -04008145 Resources resources = mBuilder.mContext.getResources();
8146 Configuration currentConfig = resources.getConfiguration();
Lucas Dupinf03e7522018-06-25 16:21:13 -07008147 boolean inNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
8148 == Configuration.UI_MODE_NIGHT_YES;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008149 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized(p)
8150 ? getActionColor(p)
Lucas Dupina291d192018-06-07 13:59:42 -07008151 : ContrastColorUtil.resolveColor(mBuilder.mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07008152 Notification.COLOR_DEFAULT, inNightMode);
Anthony Chenad4d1582017-04-10 16:07:58 -07008153
Gus Prevas9cc96602018-10-11 11:24:23 -04008154 container.setDrawableTint(buttonId, false, tintColor,
Sunny Goyal5b153922017-09-21 21:00:36 -07008155 PorterDuff.Mode.SRC_ATOP);
Gus Prevas9cc96602018-10-11 11:24:23 -04008156
8157 final TypedArray typedArray = mBuilder.mContext.obtainStyledAttributes(
8158 new int[]{ android.R.attr.colorControlHighlight });
8159 int rippleAlpha = Color.alpha(typedArray.getColor(0, 0));
8160 typedArray.recycle();
8161 int rippleColor = Color.argb(rippleAlpha, Color.red(tintColor), Color.green(tintColor),
8162 Color.blue(tintColor));
8163 container.setRippleDrawableColor(buttonId, ColorStateList.valueOf(rippleColor));
8164
Dan Sandler842dd772014-05-15 09:36:47 -04008165 if (!tombstone) {
Gus Prevas9cc96602018-10-11 11:24:23 -04008166 container.setOnClickPendingIntent(buttonId, action.actionIntent);
Dan Sandler842dd772014-05-15 09:36:47 -04008167 }
Gus Prevas9cc96602018-10-11 11:24:23 -04008168 container.setContentDescription(buttonId, action.title);
Dan Sandler842dd772014-05-15 09:36:47 -04008169 }
8170
8171 private RemoteViews makeMediaContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008172 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).fillTextsFrom(
8173 mBuilder);
Dan Sandler842dd772014-05-15 09:36:47 -04008174 RemoteViews view = mBuilder.applyStandardTemplate(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008175 R.layout.notification_template_material_media, p,
Selim Cinek384804b2018-04-18 14:31:07 +08008176 null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04008177
8178 final int numActions = mBuilder.mActions.size();
Gus Prevas9cc96602018-10-11 11:24:23 -04008179 final int numActionsToShow = mActionsToShowInCompact == null
Dan Sandler842dd772014-05-15 09:36:47 -04008180 ? 0
8181 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Gus Prevas9cc96602018-10-11 11:24:23 -04008182 if (numActionsToShow > numActions) {
8183 throw new IllegalArgumentException(String.format(
8184 "setShowActionsInCompactView: action %d out of bounds (max %d)",
8185 numActions, numActions - 1));
8186 }
8187 for (int i = 0; i < MAX_MEDIA_BUTTONS_IN_COMPACT; i++) {
8188 if (i < numActionsToShow) {
Dan Sandler842dd772014-05-15 09:36:47 -04008189 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008190 bindMediaActionButton(view, MEDIA_BUTTON_IDS[i], action, p);
Gus Prevas9cc96602018-10-11 11:24:23 -04008191 } else {
8192 view.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04008193 }
8194 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08008195 handleImage(view);
8196 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008197 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07008198 if (mBuilder.mN.hasLargeIcon()) {
Selim Cinek384804b2018-04-18 14:31:07 +08008199 endMargin = R.dimen.notification_media_image_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08008200 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008201 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04008202 return view;
8203 }
8204
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008205 private int getActionColor(StandardTemplateParams p) {
8206 return mBuilder.isColorized(p) ? mBuilder.getPrimaryTextColor(p)
8207 : mBuilder.resolveContrastColor(p);
Selim Cinek99104832017-01-25 14:47:33 -08008208 }
8209
Dan Sandler842dd772014-05-15 09:36:47 -04008210 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008211 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008212 // Dont add an expanded view if there is no more content to be revealed
8213 int actionsInCompact = mActionsToShowInCompact == null
8214 ? 0
8215 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07008216 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008217 return null;
8218 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008219 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).fillTextsFrom(
8220 mBuilder);
Selim Cinek5bf069a2015-11-10 19:14:27 -05008221 RemoteViews big = mBuilder.applyStandardTemplate(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008222 R.layout.notification_template_material_big_media, p , null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04008223
Gus Prevas9cc96602018-10-11 11:24:23 -04008224 for (int i = 0; i < MAX_MEDIA_BUTTONS; i++) {
8225 if (i < actionCount) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008226 bindMediaActionButton(big, MEDIA_BUTTON_IDS[i], mBuilder.mActions.get(i), p);
Gus Prevas9cc96602018-10-11 11:24:23 -04008227 } else {
8228 big.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04008229 }
8230 }
linanson2bcd4032019-01-14 15:22:04 +08008231 bindMediaActionButton(big, R.id.media_seamless, new Action(R.drawable.ic_media_seamless,
8232 mBuilder.mContext.getString(
8233 com.android.internal.R.string.ext_media_seamless_action), null), p);
8234 big.setViewVisibility(R.id.media_seamless, View.GONE);
Selim Cinek5bf069a2015-11-10 19:14:27 -05008235 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04008236 return big;
8237 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008238
Selim Cinek5bf069a2015-11-10 19:14:27 -05008239 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07008240 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008241 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
8242 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008243 }
8244 }
8245
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008246 /**
8247 * @hide
8248 */
8249 @Override
8250 protected boolean hasProgress() {
8251 return false;
8252 }
Dan Sandler842dd772014-05-15 09:36:47 -04008253 }
Griff Hazen61a9e862014-05-22 16:05:19 -07008254
Selim Cinek593610c2016-02-16 18:42:57 -08008255 /**
8256 * Notification style for custom views that are decorated by the system
8257 *
8258 * <p>Instead of providing a notification that is completely custom, a developer can set this
8259 * style and still obtain system decorations like the notification header with the expand
8260 * affordance and actions.
8261 *
8262 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8263 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8264 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8265 * corresponding custom views to display.
8266 *
8267 * To use this style with your Notification, feed it to
8268 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8269 * <pre class="prettyprint">
8270 * Notification noti = new Notification.Builder()
8271 * .setSmallIcon(R.drawable.ic_stat_player)
8272 * .setLargeIcon(albumArtBitmap))
8273 * .setCustomContentView(contentView);
8274 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
8275 * .build();
8276 * </pre>
8277 */
8278 public static class DecoratedCustomViewStyle extends Style {
8279
8280 public DecoratedCustomViewStyle() {
8281 }
8282
Selim Cinek593610c2016-02-16 18:42:57 -08008283 /**
8284 * @hide
8285 */
8286 public boolean displayCustomViewInline() {
8287 return true;
8288 }
8289
8290 /**
8291 * @hide
8292 */
8293 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008294 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008295 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
8296 }
8297
8298 /**
8299 * @hide
8300 */
8301 @Override
8302 public RemoteViews makeBigContentView() {
8303 return makeDecoratedBigContentView();
8304 }
8305
8306 /**
8307 * @hide
8308 */
8309 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008310 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008311 return makeDecoratedHeadsUpContentView();
8312 }
8313
Selim Cinek593610c2016-02-16 18:42:57 -08008314 private RemoteViews makeDecoratedHeadsUpContentView() {
8315 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
8316 ? mBuilder.mN.contentView
8317 : mBuilder.mN.headsUpContentView;
8318 if (mBuilder.mActions.size() == 0) {
8319 return makeStandardTemplateWithCustomContent(headsUpContentView);
8320 }
Selim Cinek384804b2018-04-18 14:31:07 +08008321 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008322 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008323 mBuilder.getBigBaseLayoutResource(), result);
8324 buildIntoRemoteViewContent(remoteViews, headsUpContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008325 return remoteViews;
8326 }
8327
Selim Cinek593610c2016-02-16 18:42:57 -08008328 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
Selim Cinek384804b2018-04-18 14:31:07 +08008329 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008330 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08008331 mBuilder.getBaseLayoutResource(), result);
8332 buildIntoRemoteViewContent(remoteViews, customContent, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008333 return remoteViews;
8334 }
8335
Selim Cinek593610c2016-02-16 18:42:57 -08008336 private RemoteViews makeDecoratedBigContentView() {
8337 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
8338 ? mBuilder.mN.contentView
8339 : mBuilder.mN.bigContentView;
8340 if (mBuilder.mActions.size() == 0) {
8341 return makeStandardTemplateWithCustomContent(bigContentView);
8342 }
Selim Cinek384804b2018-04-18 14:31:07 +08008343 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008344 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008345 mBuilder.getBigBaseLayoutResource(), result);
8346 buildIntoRemoteViewContent(remoteViews, bigContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008347 return remoteViews;
8348 }
Selim Cinek247fa012016-02-18 09:50:48 -08008349
8350 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
Selim Cinek384804b2018-04-18 14:31:07 +08008351 RemoteViews customContent, TemplateBindResult result) {
Selim Cinek6fe4a102019-01-25 14:38:24 -08008352 int childIndex = -1;
Adrian Roos5081c0d2016-02-26 16:04:19 -08008353 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008354 // Need to clone customContent before adding, because otherwise it can no longer be
8355 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008356 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07008357 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
8358 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Sunny Goyalc12d31c2018-11-12 16:29:18 -08008359 remoteViews.addFlags(RemoteViews.FLAG_REAPPLY_DISALLOWED);
Selim Cinek6fe4a102019-01-25 14:38:24 -08008360 childIndex = 0;
Adrian Roos5081c0d2016-02-26 16:04:19 -08008361 }
Selim Cinek6fe4a102019-01-25 14:38:24 -08008362 remoteViews.setIntTag(R.id.notification_main_column,
8363 com.android.internal.R.id.notification_custom_view_index_tag,
8364 childIndex);
Selim Cinek247fa012016-02-18 09:50:48 -08008365 // also update the end margin if there is an image
Selim Cinek384804b2018-04-18 14:31:07 +08008366 Resources resources = mBuilder.mContext.getResources();
8367 int endMargin = resources.getDimensionPixelSize(
Selim Cinek1c72fa02018-04-23 18:00:54 +08008368 R.dimen.notification_content_margin_end) + result.getIconMarginEnd();
Selim Cinek384804b2018-04-18 14:31:07 +08008369 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08008370 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05008371
8372 /**
8373 * @hide
8374 */
8375 @Override
8376 public boolean areNotificationsVisiblyDifferent(Style other) {
8377 if (other == null || getClass() != other.getClass()) {
8378 return true;
8379 }
8380 // Comparison done for all custom RemoteViews, independent of style
8381 return false;
8382 }
Selim Cinek593610c2016-02-16 18:42:57 -08008383 }
8384
Selim Cinek03eb3b72016-02-18 10:39:45 -08008385 /**
8386 * Notification style for media custom views that are decorated by the system
8387 *
8388 * <p>Instead of providing a media notification that is completely custom, a developer can set
8389 * this style and still obtain system decorations like the notification header with the expand
8390 * affordance and actions.
8391 *
8392 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8393 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8394 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8395 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08008396 * <p>
8397 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
8398 * notification by using {@link Notification.Builder#setColorized(boolean)}.
8399 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08008400 * To use this style with your Notification, feed it to
8401 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8402 * <pre class="prettyprint">
8403 * Notification noti = new Notification.Builder()
8404 * .setSmallIcon(R.drawable.ic_stat_player)
8405 * .setLargeIcon(albumArtBitmap))
8406 * .setCustomContentView(contentView);
8407 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
8408 * .setMediaSession(mySession))
8409 * .build();
8410 * </pre>
8411 *
8412 * @see android.app.Notification.DecoratedCustomViewStyle
8413 * @see android.app.Notification.MediaStyle
8414 */
8415 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
8416
8417 public DecoratedMediaCustomViewStyle() {
8418 }
8419
Selim Cinek03eb3b72016-02-18 10:39:45 -08008420 /**
8421 * @hide
8422 */
8423 public boolean displayCustomViewInline() {
8424 return true;
8425 }
8426
8427 /**
8428 * @hide
8429 */
8430 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008431 public RemoteViews makeContentView(boolean increasedHeight) {
8432 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008433 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8434 mBuilder.mN.contentView);
8435 }
8436
8437 /**
8438 * @hide
8439 */
8440 @Override
8441 public RemoteViews makeBigContentView() {
8442 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
8443 ? mBuilder.mN.bigContentView
8444 : mBuilder.mN.contentView;
8445 return makeBigContentViewWithCustomContent(customRemoteView);
8446 }
8447
8448 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
8449 RemoteViews remoteViews = super.makeBigContentView();
8450 if (remoteViews != null) {
8451 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
8452 customRemoteView);
8453 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08008454 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008455 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8456 customRemoteView);
8457 } else {
8458 return null;
8459 }
8460 }
8461
8462 /**
8463 * @hide
8464 */
8465 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008466 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08008467 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
8468 ? mBuilder.mN.headsUpContentView
8469 : mBuilder.mN.contentView;
8470 return makeBigContentViewWithCustomContent(customRemoteView);
8471 }
8472
Julia Reynolds7217dc92018-03-07 12:12:09 -05008473 /**
8474 * @hide
8475 */
8476 @Override
8477 public boolean areNotificationsVisiblyDifferent(Style other) {
8478 if (other == null || getClass() != other.getClass()) {
8479 return true;
8480 }
8481 // Comparison done for all custom RemoteViews, independent of style
8482 return false;
8483 }
8484
Selim Cinek03eb3b72016-02-18 10:39:45 -08008485 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
8486 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08008487 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008488 // Need to clone customContent before adding, because otherwise it can no longer be
8489 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008490 customContent = customContent.clone();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008491 customContent.overrideTextColors(mBuilder.getPrimaryTextColor(mBuilder.mParams));
Selim Cinekf91017e2016-03-14 12:25:09 -07008492 remoteViews.removeAllViews(id);
8493 remoteViews.addView(id, customContent);
Sunny Goyalc12d31c2018-11-12 16:29:18 -08008494 remoteViews.addFlags(RemoteViews.FLAG_REAPPLY_DISALLOWED);
Adrian Roos5081c0d2016-02-26 16:04:19 -08008495 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08008496 return remoteViews;
8497 }
8498 }
8499
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008500 /**
8501 * Encapsulates the information needed to display a notification as a bubble.
8502 *
8503 * <p>A bubble is used to display app content in a floating window over the existing
8504 * foreground activity. A bubble has a collapsed state represented by an icon,
8505 * {@link BubbleMetadata.Builder#setIcon(Icon)} and an expanded state which is populated
8506 * via {@link BubbleMetadata.Builder#setIntent(PendingIntent)}.</p>
8507 *
8508 * <b>Notifications with a valid and allowed bubble will display in collapsed state
8509 * outside of the notification shade on unlocked devices. When a user interacts with the
8510 * collapsed bubble, the bubble intent will be invoked and displayed.</b>
8511 *
8512 * @see Notification.Builder#setBubbleMetadata(BubbleMetadata)
8513 */
8514 public static final class BubbleMetadata implements Parcelable {
8515
8516 private PendingIntent mPendingIntent;
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008517 private PendingIntent mDeleteIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008518 private Icon mIcon;
8519 private int mDesiredHeight;
Mady Mellor0157ff22019-01-24 15:31:48 -08008520 private int mFlags;
8521
8522 /**
8523 * If set and the app creating the bubble is in the foreground, the bubble will be posted
8524 * in its expanded state, with the contents of {@link #getIntent()} in a floating window.
8525 *
8526 * <p>If the app creating the bubble is not in the foreground this flag has no effect.</p>
8527 *
8528 * <p>Generally this flag should only be set if the user has performed an action to request
8529 * or create a bubble.</p>
8530 */
8531 private static final int FLAG_AUTO_EXPAND_BUBBLE = 0x00000001;
8532
8533 /**
8534 * If set and the app creating the bubble is in the foreground, the bubble will be posted
8535 * <b>without</b> the associated notification in the notification shade. Subsequent update
8536 * notifications to this bubble will post a notification in the shade.
8537 *
8538 * <p>If the app creating the bubble is not in the foreground this flag has no effect.</p>
8539 *
8540 * <p>Generally this flag should only be set if the user has performed an action to request
8541 * or create a bubble.</p>
8542 */
8543 private static final int FLAG_SUPPRESS_INITIAL_NOTIFICATION = 0x00000002;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008544
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008545 private BubbleMetadata(PendingIntent expandIntent, PendingIntent deleteIntent,
Mady Mellor138f4c92019-02-20 16:12:17 -08008546 Icon icon, int height) {
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008547 mPendingIntent = expandIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008548 mIcon = icon;
8549 mDesiredHeight = height;
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008550 mDeleteIntent = deleteIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008551 }
8552
8553 private BubbleMetadata(Parcel in) {
8554 mPendingIntent = PendingIntent.CREATOR.createFromParcel(in);
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008555 mIcon = Icon.CREATOR.createFromParcel(in);
8556 mDesiredHeight = in.readInt();
Mady Mellor0157ff22019-01-24 15:31:48 -08008557 mFlags = in.readInt();
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008558 if (in.readInt() != 0) {
8559 mDeleteIntent = PendingIntent.CREATOR.createFromParcel(in);
8560 }
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008561 }
8562
8563 /**
8564 * @return the pending intent used to populate the floating window for this bubble.
8565 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008566 @NonNull
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008567 public PendingIntent getIntent() {
8568 return mPendingIntent;
8569 }
8570
8571 /**
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008572 * @return the pending intent to send when the bubble is dismissed by a user, if one exists.
8573 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008574 @Nullable
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008575 public PendingIntent getDeleteIntent() {
8576 return mDeleteIntent;
8577 }
8578
8579 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008580 * @return the title that will appear along with the app content defined by
8581 * {@link #getIntent()} for this bubble.
Mady Mellor138f4c92019-02-20 16:12:17 -08008582 *
8583 * @deprecated titles are no longer required or shown.
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008584 */
Mady Mellor138f4c92019-02-20 16:12:17 -08008585 @Deprecated
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008586 public CharSequence getTitle() {
Mady Mellor138f4c92019-02-20 16:12:17 -08008587 return "";
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008588 }
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008589
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008590 /**
8591 * @return the icon that will be displayed for this bubble when it is collapsed.
8592 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008593 @NonNull
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008594 public Icon getIcon() {
8595 return mIcon;
8596 }
8597
8598 /**
8599 * @return the ideal height for the floating window that app content defined by
8600 * {@link #getIntent()} for this bubble.
8601 */
8602 public int getDesiredHeight() {
8603 return mDesiredHeight;
8604 }
8605
Mady Mellor0157ff22019-01-24 15:31:48 -08008606 /**
8607 * @return whether this bubble should auto expand when it is posted.
8608 *
8609 * @see BubbleMetadata.Builder#setAutoExpandBubble(boolean)
8610 */
8611 public boolean getAutoExpandBubble() {
8612 return (mFlags & FLAG_AUTO_EXPAND_BUBBLE) != 0;
8613 }
8614
8615 /**
8616 * @return whether this bubble should suppress the initial notification when it is posted.
8617 *
8618 * @see BubbleMetadata.Builder#setSuppressInitialNotification(boolean)
8619 */
8620 public boolean getSuppressInitialNotification() {
8621 return (mFlags & FLAG_SUPPRESS_INITIAL_NOTIFICATION) != 0;
8622 }
8623
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07008624 public static final @android.annotation.NonNull Parcelable.Creator<BubbleMetadata> CREATOR =
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008625 new Parcelable.Creator<BubbleMetadata>() {
8626
8627 @Override
8628 public BubbleMetadata createFromParcel(Parcel source) {
8629 return new BubbleMetadata(source);
8630 }
8631
8632 @Override
8633 public BubbleMetadata[] newArray(int size) {
8634 return new BubbleMetadata[size];
8635 }
8636 };
8637
8638 @Override
8639 public int describeContents() {
8640 return 0;
8641 }
8642
8643 @Override
8644 public void writeToParcel(Parcel out, int flags) {
8645 mPendingIntent.writeToParcel(out, 0);
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008646 mIcon.writeToParcel(out, 0);
8647 out.writeInt(mDesiredHeight);
Mady Mellor0157ff22019-01-24 15:31:48 -08008648 out.writeInt(mFlags);
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008649 out.writeInt(mDeleteIntent != null ? 1 : 0);
8650 if (mDeleteIntent != null) {
8651 mDeleteIntent.writeToParcel(out, 0);
8652 }
Mady Mellor0157ff22019-01-24 15:31:48 -08008653 }
8654
8655 private void setFlags(int flags) {
8656 mFlags = flags;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008657 }
8658
8659 /**
8660 * Builder to construct a {@link BubbleMetadata} object.
8661 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008662 public static final class Builder {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008663
8664 private PendingIntent mPendingIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008665 private Icon mIcon;
8666 private int mDesiredHeight;
Mady Mellor0157ff22019-01-24 15:31:48 -08008667 private int mFlags;
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008668 private PendingIntent mDeleteIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008669
8670 /**
8671 * Constructs a new builder object.
8672 */
8673 public Builder() {
8674 }
8675
8676 /**
8677 * Sets the intent that will be used when the bubble is expanded. This will display the
8678 * app content in a floating window over the existing foreground activity.
8679 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008680 @NonNull
8681 public BubbleMetadata.Builder setIntent(@NonNull PendingIntent intent) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008682 if (intent == null) {
8683 throw new IllegalArgumentException("Bubble requires non-null pending intent");
8684 }
8685 mPendingIntent = intent;
8686 return this;
8687 }
8688
8689 /**
8690 * Sets the title that will appear along with the app content for this bubble.
8691 *
8692 * <p>A title is required and should expect to fit on a single line and make sense when
8693 * shown with the content defined by {@link #setIntent(PendingIntent)}.</p>
Mady Mellor138f4c92019-02-20 16:12:17 -08008694 *
8695 * @deprecated titles are no longer required or shown.
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008696 */
Mady Mellor138f4c92019-02-20 16:12:17 -08008697 @Deprecated
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008698 public BubbleMetadata.Builder setTitle(CharSequence title) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008699 return this;
8700 }
8701
8702 /**
8703 * Sets the icon that will represent the bubble when it is collapsed.
8704 *
8705 * <p>An icon is required and should be representative of the content within the bubble.
8706 * If your app produces multiple bubbles, the image should be unique for each of them.
8707 * </p>
8708 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008709 @NonNull
8710 public BubbleMetadata.Builder setIcon(@NonNull Icon icon) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008711 if (icon == null) {
8712 throw new IllegalArgumentException("Bubbles require non-null icon");
8713 }
8714 mIcon = icon;
8715 return this;
8716 }
8717
8718 /**
8719 * Sets the desired height for the app content defined by
8720 * {@link #setIntent(PendingIntent)}, this height may not be respected if there is not
8721 * enough space on the screen or if the provided height is too small to be useful.
8722 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008723 @NonNull
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008724 public BubbleMetadata.Builder setDesiredHeight(int height) {
8725 mDesiredHeight = Math.max(height, 0);
8726 return this;
8727 }
8728
8729 /**
Mady Mellor0157ff22019-01-24 15:31:48 -08008730 * If set and the app creating the bubble is in the foreground, the bubble will be
8731 * posted in its expanded state, with the contents of {@link #getIntent()} in a
8732 * floating window.
8733 *
8734 * <p>If the app creating the bubble is not in the foreground this flag has no effect.
8735 * </p>
8736 *
8737 * <p>Generally this flag should only be set if the user has performed an action to
8738 * request or create a bubble.</p>
8739 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008740 @NonNull
Mady Mellor0157ff22019-01-24 15:31:48 -08008741 public BubbleMetadata.Builder setAutoExpandBubble(boolean shouldExpand) {
8742 setFlag(FLAG_AUTO_EXPAND_BUBBLE, shouldExpand);
8743 return this;
8744 }
8745
8746 /**
8747 * If set and the app creating the bubble is in the foreground, the bubble will be
8748 * posted <b>without</b> the associated notification in the notification shade.
8749 * Subsequent update notifications to this bubble will post a notification in the shade.
8750 *
8751 * <p>If the app creating the bubble is not in the foreground this flag has no effect.
8752 * </p>
8753 *
8754 * <p>Generally this flag should only be set if the user has performed an action to
8755 * request or create a bubble.</p>
8756 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008757 @NonNull
Mady Mellor0157ff22019-01-24 15:31:48 -08008758 public BubbleMetadata.Builder setSuppressInitialNotification(
8759 boolean shouldSupressNotif) {
8760 setFlag(FLAG_SUPPRESS_INITIAL_NOTIFICATION, shouldSupressNotif);
8761 return this;
8762 }
8763
8764 /**
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008765 * Sets an optional intent to send when this bubble is explicitly removed by the user.
8766 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008767 @NonNull
8768 public BubbleMetadata.Builder setDeleteIntent(@Nullable PendingIntent deleteIntent) {
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008769 mDeleteIntent = deleteIntent;
8770 return this;
8771 }
8772
8773 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008774 * Creates the {@link BubbleMetadata} defined by this builder.
8775 * <p>Will throw {@link IllegalStateException} if required fields have not been set
8776 * on this builder.</p>
8777 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008778 @NonNull
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008779 public BubbleMetadata build() {
8780 if (mPendingIntent == null) {
8781 throw new IllegalStateException("Must supply pending intent to bubble");
8782 }
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008783 if (mIcon == null) {
8784 throw new IllegalStateException("Must supply an icon for the bubble");
8785 }
Mady Mellor138f4c92019-02-20 16:12:17 -08008786 BubbleMetadata data = new BubbleMetadata(mPendingIntent, mDeleteIntent,
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008787 mIcon, mDesiredHeight);
Mady Mellor0157ff22019-01-24 15:31:48 -08008788 data.setFlags(mFlags);
8789 return data;
8790 }
8791
8792 /**
8793 * @hide
8794 */
8795 public BubbleMetadata.Builder setFlag(int mask, boolean value) {
8796 if (value) {
8797 mFlags |= mask;
8798 } else {
8799 mFlags &= ~mask;
8800 }
8801 return this;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008802 }
8803 }
8804 }
8805
8806
Christoph Studer4600f9b2014-07-22 22:44:43 +02008807 // When adding a new Style subclass here, don't forget to update
8808 // Builder.getNotificationStyleClass.
8809
Griff Hazen61a9e862014-05-22 16:05:19 -07008810 /**
8811 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
8812 * metadata or change options on a notification builder.
8813 */
8814 public interface Extender {
8815 /**
8816 * Apply this extender to a notification builder.
8817 * @param builder the builder to be modified.
8818 * @return the build object for chaining.
8819 */
8820 public Builder extend(Builder builder);
8821 }
8822
8823 /**
8824 * Helper class to add wearable extensions to notifications.
8825 * <p class="note"> See
8826 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
8827 * for Android Wear</a> for more information on how to use this class.
8828 * <p>
8829 * To create a notification with wearable extensions:
8830 * <ol>
8831 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
8832 * properties.
8833 * <li>Create a {@link android.app.Notification.WearableExtender}.
8834 * <li>Set wearable-specific properties using the
8835 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
8836 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
8837 * notification.
8838 * <li>Post the notification to the notification system with the
8839 * {@code NotificationManager.notify(...)} methods.
8840 * </ol>
8841 *
8842 * <pre class="prettyprint">
8843 * Notification notif = new Notification.Builder(mContext)
8844 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
8845 * .setContentText(subject)
8846 * .setSmallIcon(R.drawable.new_mail)
8847 * .extend(new Notification.WearableExtender()
8848 * .setContentIcon(R.drawable.new_mail))
8849 * .build();
8850 * NotificationManager notificationManger =
8851 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
8852 * notificationManger.notify(0, notif);</pre>
8853 *
8854 * <p>Wearable extensions can be accessed on an existing notification by using the
8855 * {@code WearableExtender(Notification)} constructor,
8856 * and then using the {@code get} methods to access values.
8857 *
8858 * <pre class="prettyprint">
8859 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
8860 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07008861 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008862 */
8863 public static final class WearableExtender implements Extender {
8864 /**
8865 * Sentinel value for an action index that is unset.
8866 */
8867 public static final int UNSET_ACTION_INDEX = -1;
8868
8869 /**
8870 * Size value for use with {@link #setCustomSizePreset} to show this notification with
8871 * default sizing.
8872 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07008873 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07008874 * on their content.
Gus Prevasd7363752018-09-18 14:35:15 -04008875 *
8876 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008877 */
Gus Prevasd7363752018-09-18 14:35:15 -04008878 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008879 public static final int SIZE_DEFAULT = 0;
8880
8881 /**
8882 * Size value for use with {@link #setCustomSizePreset} to show this notification
8883 * with an extra small size.
8884 * <p>This value is only applicable for custom display notifications created using
8885 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008886 *
8887 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008888 */
Gus Prevasd7363752018-09-18 14:35:15 -04008889 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008890 public static final int SIZE_XSMALL = 1;
8891
8892 /**
8893 * Size value for use with {@link #setCustomSizePreset} to show this notification
8894 * with a small size.
8895 * <p>This value is only applicable for custom display notifications created using
8896 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008897 *
8898 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008899 */
Gus Prevasd7363752018-09-18 14:35:15 -04008900 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008901 public static final int SIZE_SMALL = 2;
8902
8903 /**
8904 * Size value for use with {@link #setCustomSizePreset} to show this notification
8905 * with a medium size.
8906 * <p>This value is only applicable for custom display notifications created using
8907 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008908 *
8909 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008910 */
Gus Prevasd7363752018-09-18 14:35:15 -04008911 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008912 public static final int SIZE_MEDIUM = 3;
8913
8914 /**
8915 * Size value for use with {@link #setCustomSizePreset} to show this notification
8916 * with a large size.
8917 * <p>This value is only applicable for custom display notifications created using
8918 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008919 *
8920 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008921 */
Gus Prevasd7363752018-09-18 14:35:15 -04008922 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008923 public static final int SIZE_LARGE = 4;
8924
Griff Hazend5f11f92014-05-27 15:40:09 -07008925 /**
8926 * Size value for use with {@link #setCustomSizePreset} to show this notification
8927 * full screen.
8928 * <p>This value is only applicable for custom display notifications created using
8929 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008930 *
8931 * @deprecated Display intents are no longer supported.
Griff Hazend5f11f92014-05-27 15:40:09 -07008932 */
Gus Prevasd7363752018-09-18 14:35:15 -04008933 @Deprecated
Griff Hazend5f11f92014-05-27 15:40:09 -07008934 public static final int SIZE_FULL_SCREEN = 5;
8935
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008936 /**
8937 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
8938 * short amount of time when this notification is displayed on the screen. This
8939 * is the default value.
Gus Prevasd7363752018-09-18 14:35:15 -04008940 *
8941 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008942 */
Gus Prevasd7363752018-09-18 14:35:15 -04008943 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008944 public static final int SCREEN_TIMEOUT_SHORT = 0;
8945
8946 /**
8947 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
8948 * for a longer amount of time when this notification is displayed on the screen.
Gus Prevasd7363752018-09-18 14:35:15 -04008949 *
8950 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008951 */
Gus Prevasd7363752018-09-18 14:35:15 -04008952 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008953 public static final int SCREEN_TIMEOUT_LONG = -1;
8954
Griff Hazen61a9e862014-05-22 16:05:19 -07008955 /** Notification extra which contains wearable extensions */
8956 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
8957
Pete Gastaf6781d2014-10-07 15:17:05 -04008958 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07008959 private static final String KEY_ACTIONS = "actions";
8960 private static final String KEY_FLAGS = "flags";
8961 private static final String KEY_DISPLAY_INTENT = "displayIntent";
8962 private static final String KEY_PAGES = "pages";
8963 private static final String KEY_BACKGROUND = "background";
8964 private static final String KEY_CONTENT_ICON = "contentIcon";
8965 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
8966 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
8967 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
8968 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
8969 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008970 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04008971 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008972 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07008973
8974 // Flags bitwise-ored to mFlags
8975 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
8976 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
8977 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
8978 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008979 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04008980 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04008981 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07008982
8983 // Default value for flags integer
8984 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
8985
8986 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
8987 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
8988
8989 private ArrayList<Action> mActions = new ArrayList<Action>();
8990 private int mFlags = DEFAULT_FLAGS;
8991 private PendingIntent mDisplayIntent;
8992 private ArrayList<Notification> mPages = new ArrayList<Notification>();
8993 private Bitmap mBackground;
8994 private int mContentIcon;
8995 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
8996 private int mContentActionIndex = UNSET_ACTION_INDEX;
8997 private int mCustomSizePreset = SIZE_DEFAULT;
8998 private int mCustomContentHeight;
8999 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009000 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04009001 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009002 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07009003
9004 /**
9005 * Create a {@link android.app.Notification.WearableExtender} with default
9006 * options.
9007 */
9008 public WearableExtender() {
9009 }
9010
9011 public WearableExtender(Notification notif) {
9012 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
9013 if (wearableBundle != null) {
9014 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
9015 if (actions != null) {
9016 mActions.addAll(actions);
9017 }
9018
9019 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
9020 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
9021
9022 Notification[] pages = getNotificationArrayFromBundle(
9023 wearableBundle, KEY_PAGES);
9024 if (pages != null) {
9025 Collections.addAll(mPages, pages);
9026 }
9027
9028 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
9029 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
9030 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
9031 DEFAULT_CONTENT_ICON_GRAVITY);
9032 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
9033 UNSET_ACTION_INDEX);
9034 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
9035 SIZE_DEFAULT);
9036 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
9037 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009038 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04009039 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009040 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07009041 }
9042 }
9043
9044 /**
9045 * Apply wearable extensions to a notification that is being built. This is typically
9046 * called by the {@link android.app.Notification.Builder#extend} method of
9047 * {@link android.app.Notification.Builder}.
9048 */
9049 @Override
9050 public Notification.Builder extend(Notification.Builder builder) {
9051 Bundle wearableBundle = new Bundle();
9052
9053 if (!mActions.isEmpty()) {
9054 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
9055 }
9056 if (mFlags != DEFAULT_FLAGS) {
9057 wearableBundle.putInt(KEY_FLAGS, mFlags);
9058 }
9059 if (mDisplayIntent != null) {
9060 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
9061 }
9062 if (!mPages.isEmpty()) {
9063 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
9064 new Notification[mPages.size()]));
9065 }
9066 if (mBackground != null) {
9067 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
9068 }
9069 if (mContentIcon != 0) {
9070 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
9071 }
9072 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
9073 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
9074 }
9075 if (mContentActionIndex != UNSET_ACTION_INDEX) {
9076 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
9077 mContentActionIndex);
9078 }
9079 if (mCustomSizePreset != SIZE_DEFAULT) {
9080 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
9081 }
9082 if (mCustomContentHeight != 0) {
9083 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
9084 }
9085 if (mGravity != DEFAULT_GRAVITY) {
9086 wearableBundle.putInt(KEY_GRAVITY, mGravity);
9087 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009088 if (mHintScreenTimeout != 0) {
9089 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
9090 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04009091 if (mDismissalId != null) {
9092 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
9093 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009094 if (mBridgeTag != null) {
9095 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
9096 }
Griff Hazen61a9e862014-05-22 16:05:19 -07009097
9098 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
9099 return builder;
9100 }
9101
9102 @Override
9103 public WearableExtender clone() {
9104 WearableExtender that = new WearableExtender();
9105 that.mActions = new ArrayList<Action>(this.mActions);
9106 that.mFlags = this.mFlags;
9107 that.mDisplayIntent = this.mDisplayIntent;
9108 that.mPages = new ArrayList<Notification>(this.mPages);
9109 that.mBackground = this.mBackground;
9110 that.mContentIcon = this.mContentIcon;
9111 that.mContentIconGravity = this.mContentIconGravity;
9112 that.mContentActionIndex = this.mContentActionIndex;
9113 that.mCustomSizePreset = this.mCustomSizePreset;
9114 that.mCustomContentHeight = this.mCustomContentHeight;
9115 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009116 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04009117 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009118 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07009119 return that;
9120 }
9121
9122 /**
9123 * Add a wearable action to this notification.
9124 *
9125 * <p>When wearable actions are added using this method, the set of actions that
9126 * show on a wearable device splits from devices that only show actions added
9127 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
9128 * of which actions display on different devices.
9129 *
9130 * @param action the action to add to this notification
9131 * @return this object for method chaining
9132 * @see android.app.Notification.Action
9133 */
9134 public WearableExtender addAction(Action action) {
9135 mActions.add(action);
9136 return this;
9137 }
9138
9139 /**
9140 * Adds wearable actions to this notification.
9141 *
9142 * <p>When wearable actions are added using this method, the set of actions that
9143 * show on a wearable device splits from devices that only show actions added
9144 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
9145 * of which actions display on different devices.
9146 *
9147 * @param actions the actions to add to this notification
9148 * @return this object for method chaining
9149 * @see android.app.Notification.Action
9150 */
9151 public WearableExtender addActions(List<Action> actions) {
9152 mActions.addAll(actions);
9153 return this;
9154 }
9155
9156 /**
9157 * Clear all wearable actions present on this builder.
9158 * @return this object for method chaining.
9159 * @see #addAction
9160 */
9161 public WearableExtender clearActions() {
9162 mActions.clear();
9163 return this;
9164 }
9165
9166 /**
9167 * Get the wearable actions present on this notification.
9168 */
9169 public List<Action> getActions() {
9170 return mActions;
9171 }
9172
9173 /**
9174 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07009175 * this notification. The {@link PendingIntent} provided should be for an activity.
9176 *
9177 * <pre class="prettyprint">
9178 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
9179 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
9180 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
9181 * Notification notif = new Notification.Builder(context)
9182 * .extend(new Notification.WearableExtender()
9183 * .setDisplayIntent(displayPendingIntent)
9184 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
9185 * .build();</pre>
9186 *
9187 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07009188 * should have an empty task affinity. It is also recommended to use the device
9189 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07009190 *
9191 * <p>Example AndroidManifest.xml entry:
9192 * <pre class="prettyprint">
9193 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
9194 * android:exported=&quot;true&quot;
9195 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07009196 * android:taskAffinity=&quot;&quot;
9197 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07009198 *
9199 * @param intent the {@link PendingIntent} for an activity
9200 * @return this object for method chaining
9201 * @see android.app.Notification.WearableExtender#getDisplayIntent
Gus Prevasd7363752018-09-18 14:35:15 -04009202 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009203 */
Gus Prevasd7363752018-09-18 14:35:15 -04009204 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009205 public WearableExtender setDisplayIntent(PendingIntent intent) {
9206 mDisplayIntent = intent;
9207 return this;
9208 }
9209
9210 /**
9211 * Get the intent to launch inside of an activity view when displaying this
9212 * notification. This {@code PendingIntent} should be for an activity.
Gus Prevasd7363752018-09-18 14:35:15 -04009213 *
9214 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009215 */
Gus Prevasd7363752018-09-18 14:35:15 -04009216 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009217 public PendingIntent getDisplayIntent() {
9218 return mDisplayIntent;
9219 }
9220
9221 /**
9222 * Add an additional page of content to display with this notification. The current
9223 * notification forms the first page, and pages added using this function form
9224 * subsequent pages. This field can be used to separate a notification into multiple
9225 * sections.
9226 *
9227 * @param page the notification to add as another page
9228 * @return this object for method chaining
9229 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04009230 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009231 */
Gus Prevasd7363752018-09-18 14:35:15 -04009232 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009233 public WearableExtender addPage(Notification page) {
9234 mPages.add(page);
9235 return this;
9236 }
9237
9238 /**
9239 * Add additional pages of content to display with this notification. The current
9240 * notification forms the first page, and pages added using this function form
9241 * subsequent pages. This field can be used to separate a notification into multiple
9242 * sections.
9243 *
9244 * @param pages a list of notifications
9245 * @return this object for method chaining
9246 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04009247 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009248 */
Gus Prevasd7363752018-09-18 14:35:15 -04009249 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009250 public WearableExtender addPages(List<Notification> pages) {
9251 mPages.addAll(pages);
9252 return this;
9253 }
9254
9255 /**
9256 * Clear all additional pages present on this builder.
9257 * @return this object for method chaining.
9258 * @see #addPage
Gus Prevasd7363752018-09-18 14:35:15 -04009259 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009260 */
Gus Prevasd7363752018-09-18 14:35:15 -04009261 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009262 public WearableExtender clearPages() {
9263 mPages.clear();
9264 return this;
9265 }
9266
9267 /**
9268 * Get the array of additional pages of content for displaying this notification. The
9269 * current notification forms the first page, and elements within this array form
9270 * subsequent pages. This field can be used to separate a notification into multiple
9271 * sections.
9272 * @return the pages for this notification
Gus Prevasd7363752018-09-18 14:35:15 -04009273 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009274 */
Gus Prevasd7363752018-09-18 14:35:15 -04009275 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009276 public List<Notification> getPages() {
9277 return mPages;
9278 }
9279
9280 /**
9281 * Set a background image to be displayed behind the notification content.
9282 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
9283 * will work with any notification style.
9284 *
9285 * @param background the background bitmap
9286 * @return this object for method chaining
9287 * @see android.app.Notification.WearableExtender#getBackground
Gus Prevasd7363752018-09-18 14:35:15 -04009288 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009289 */
Gus Prevasd7363752018-09-18 14:35:15 -04009290 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009291 public WearableExtender setBackground(Bitmap background) {
9292 mBackground = background;
9293 return this;
9294 }
9295
9296 /**
9297 * Get a background image to be displayed behind the notification content.
9298 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
9299 * will work with any notification style.
9300 *
9301 * @return the background image
9302 * @see android.app.Notification.WearableExtender#setBackground
Gus Prevasd7363752018-09-18 14:35:15 -04009303 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009304 */
Gus Prevasd7363752018-09-18 14:35:15 -04009305 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009306 public Bitmap getBackground() {
9307 return mBackground;
9308 }
9309
9310 /**
9311 * Set an icon that goes with the content of this notification.
9312 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009313 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009314 public WearableExtender setContentIcon(int icon) {
9315 mContentIcon = icon;
9316 return this;
9317 }
9318
9319 /**
9320 * Get an icon that goes with the content of this notification.
9321 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009322 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009323 public int getContentIcon() {
9324 return mContentIcon;
9325 }
9326
9327 /**
9328 * Set the gravity that the content icon should have within the notification display.
9329 * Supported values include {@link android.view.Gravity#START} and
9330 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
9331 * @see #setContentIcon
9332 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009333 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009334 public WearableExtender setContentIconGravity(int contentIconGravity) {
9335 mContentIconGravity = contentIconGravity;
9336 return this;
9337 }
9338
9339 /**
9340 * Get the gravity that the content icon should have within the notification display.
9341 * Supported values include {@link android.view.Gravity#START} and
9342 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
9343 * @see #getContentIcon
9344 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009345 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009346 public int getContentIconGravity() {
9347 return mContentIconGravity;
9348 }
9349
9350 /**
Gus Prevasd7363752018-09-18 14:35:15 -04009351 * Set an action from this notification's actions as the primary action. If the action has a
9352 * {@link RemoteInput} associated with it, shortcuts to the options for that input are shown
9353 * directly on the notification.
Griff Hazen14f57992014-05-26 09:07:14 -07009354 *
Gus Prevasd7363752018-09-18 14:35:15 -04009355 * @param actionIndex The index of the primary action.
Griff Hazen14f57992014-05-26 09:07:14 -07009356 * If wearable actions were added to the main notification, this index
9357 * will apply to that list, otherwise it will apply to the regular
9358 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07009359 */
9360 public WearableExtender setContentAction(int actionIndex) {
9361 mContentActionIndex = actionIndex;
9362 return this;
9363 }
9364
9365 /**
Gus Prevasd7363752018-09-18 14:35:15 -04009366 * Get the index of the notification action, if any, that was specified as the primary
9367 * action.
Griff Hazen14f57992014-05-26 09:07:14 -07009368 *
9369 * <p>If wearable specific actions were added to the main notification, this index will
9370 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07009371 *
9372 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07009373 */
9374 public int getContentAction() {
9375 return mContentActionIndex;
9376 }
9377
9378 /**
9379 * Set the gravity that this notification should have within the available viewport space.
9380 * Supported values include {@link android.view.Gravity#TOP},
9381 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
9382 * The default value is {@link android.view.Gravity#BOTTOM}.
9383 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009384 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009385 public WearableExtender setGravity(int gravity) {
9386 mGravity = gravity;
9387 return this;
9388 }
9389
9390 /**
9391 * Get the gravity that this notification should have within the available viewport space.
9392 * Supported values include {@link android.view.Gravity#TOP},
9393 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
9394 * The default value is {@link android.view.Gravity#BOTTOM}.
9395 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009396 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009397 public int getGravity() {
9398 return mGravity;
9399 }
9400
9401 /**
9402 * Set the custom size preset for the display of this notification out of the available
9403 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
9404 * {@link #SIZE_LARGE}.
9405 * <p>Some custom size presets are only applicable for custom display notifications created
9406 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
9407 * documentation for the preset in question. See also
9408 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
9409 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009410 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009411 public WearableExtender setCustomSizePreset(int sizePreset) {
9412 mCustomSizePreset = sizePreset;
9413 return this;
9414 }
9415
9416 /**
9417 * Get the custom size preset for the display of this notification out of the available
9418 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
9419 * {@link #SIZE_LARGE}.
9420 * <p>Some custom size presets are only applicable for custom display notifications created
9421 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
9422 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
9423 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009424 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009425 public int getCustomSizePreset() {
9426 return mCustomSizePreset;
9427 }
9428
9429 /**
9430 * Set the custom height in pixels for the display of this notification's content.
9431 * <p>This option is only available for custom display notifications created
9432 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
9433 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
9434 * {@link #getCustomContentHeight}.
9435 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009436 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009437 public WearableExtender setCustomContentHeight(int height) {
9438 mCustomContentHeight = height;
9439 return this;
9440 }
9441
9442 /**
9443 * Get the custom height in pixels for the display of this notification's content.
9444 * <p>This option is only available for custom display notifications created
9445 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
9446 * {@link #setCustomContentHeight}.
9447 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009448 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009449 public int getCustomContentHeight() {
9450 return mCustomContentHeight;
9451 }
9452
9453 /**
9454 * Set whether the scrolling position for the contents of this notification should start
9455 * at the bottom of the contents instead of the top when the contents are too long to
9456 * display within the screen. Default is false (start scroll at the top).
9457 */
9458 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
9459 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
9460 return this;
9461 }
9462
9463 /**
9464 * Get whether the scrolling position for the contents of this notification should start
9465 * at the bottom of the contents instead of the top when the contents are too long to
9466 * display within the screen. Default is false (start scroll at the top).
9467 */
9468 public boolean getStartScrollBottom() {
9469 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
9470 }
9471
9472 /**
9473 * Set whether the content intent is available when the wearable device is not connected
9474 * to a companion device. The user can still trigger this intent when the wearable device
9475 * is offline, but a visual hint will indicate that the content intent may not be available.
9476 * Defaults to true.
9477 */
9478 public WearableExtender setContentIntentAvailableOffline(
9479 boolean contentIntentAvailableOffline) {
9480 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
9481 return this;
9482 }
9483
9484 /**
9485 * Get whether the content intent is available when the wearable device is not connected
9486 * to a companion device. The user can still trigger this intent when the wearable device
9487 * is offline, but a visual hint will indicate that the content intent may not be available.
9488 * Defaults to true.
9489 */
9490 public boolean getContentIntentAvailableOffline() {
9491 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
9492 }
9493
9494 /**
9495 * Set a hint that this notification's icon should not be displayed.
9496 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
9497 * @return this object for method chaining
9498 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009499 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009500 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
9501 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
9502 return this;
9503 }
9504
9505 /**
9506 * Get a hint that this notification's icon should not be displayed.
9507 * @return {@code true} if this icon should not be displayed, false otherwise.
9508 * The default value is {@code false} if this was never set.
9509 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009510 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009511 public boolean getHintHideIcon() {
9512 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
9513 }
9514
9515 /**
9516 * Set a visual hint that only the background image of this notification should be
9517 * displayed, and other semantic content should be hidden. This hint is only applicable
9518 * to sub-pages added using {@link #addPage}.
9519 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009520 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009521 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
9522 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
9523 return this;
9524 }
9525
9526 /**
9527 * Get a visual hint that only the background image of this notification should be
9528 * displayed, and other semantic content should be hidden. This hint is only applicable
9529 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
9530 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009531 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009532 public boolean getHintShowBackgroundOnly() {
9533 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
9534 }
9535
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009536 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009537 * Set a hint that this notification's background should not be clipped if possible,
9538 * and should instead be resized to fully display on the screen, retaining the aspect
9539 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009540 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
9541 * @return this object for method chaining
9542 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009543 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009544 public WearableExtender setHintAvoidBackgroundClipping(
9545 boolean hintAvoidBackgroundClipping) {
9546 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
9547 return this;
9548 }
9549
9550 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009551 * Get a hint that this notification's background should not be clipped if possible,
9552 * and should instead be resized to fully display on the screen, retaining the aspect
9553 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009554 * @return {@code true} if it's ok if the background is clipped on the screen, false
9555 * otherwise. The default value is {@code false} if this was never set.
9556 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009557 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009558 public boolean getHintAvoidBackgroundClipping() {
9559 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
9560 }
9561
9562 /**
9563 * Set a hint that the screen should remain on for at least this duration when
9564 * this notification is displayed on the screen.
9565 * @param timeout The requested screen timeout in milliseconds. Can also be either
9566 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9567 * @return this object for method chaining
9568 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009569 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009570 public WearableExtender setHintScreenTimeout(int timeout) {
9571 mHintScreenTimeout = timeout;
9572 return this;
9573 }
9574
9575 /**
9576 * Get the duration, in milliseconds, that the screen should remain on for
9577 * when this notification is displayed.
9578 * @return the duration in milliseconds if > 0, or either one of the sentinel values
9579 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9580 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009581 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009582 public int getHintScreenTimeout() {
9583 return mHintScreenTimeout;
9584 }
9585
Alex Hills9ab3a232016-04-05 14:54:56 -04009586 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04009587 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
9588 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9589 * qr codes, as well as other simple black-and-white tickets.
9590 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
9591 * @return this object for method chaining
Gus Prevasd7363752018-09-18 14:35:15 -04009592 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009593 */
Gus Prevasd7363752018-09-18 14:35:15 -04009594 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009595 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
9596 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
9597 return this;
9598 }
9599
9600 /**
9601 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
9602 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9603 * qr codes, as well as other simple black-and-white tickets.
9604 * @return {@code true} if it should be displayed in ambient, false otherwise
9605 * otherwise. The default value is {@code false} if this was never set.
Gus Prevasd7363752018-09-18 14:35:15 -04009606 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009607 */
Gus Prevasd7363752018-09-18 14:35:15 -04009608 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009609 public boolean getHintAmbientBigPicture() {
9610 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
9611 }
9612
9613 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04009614 * Set a hint that this notification's content intent will launch an {@link Activity}
9615 * directly, telling the platform that it can generate the appropriate transitions.
9616 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
9617 * an activity and transitions should be generated, false otherwise.
9618 * @return this object for method chaining
9619 */
9620 public WearableExtender setHintContentIntentLaunchesActivity(
9621 boolean hintContentIntentLaunchesActivity) {
9622 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
9623 return this;
9624 }
9625
9626 /**
9627 * Get a hint that this notification's content intent will launch an {@link Activity}
9628 * directly, telling the platform that it can generate the appropriate transitions
9629 * @return {@code true} if the content intent will launch an activity and transitions should
9630 * be generated, false otherwise. The default value is {@code false} if this was never set.
9631 */
9632 public boolean getHintContentIntentLaunchesActivity() {
9633 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
9634 }
9635
Nadia Benbernou948627e2016-04-14 14:41:08 -04009636 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009637 * Sets the dismissal id for this notification. If a notification is posted with a
9638 * dismissal id, then when that notification is canceled, notifications on other wearables
9639 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04009640 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009641 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04009642 * @param dismissalId the dismissal id of the notification.
9643 * @return this object for method chaining
9644 */
9645 public WearableExtender setDismissalId(String dismissalId) {
9646 mDismissalId = dismissalId;
9647 return this;
9648 }
9649
9650 /**
9651 * Returns the dismissal id of the notification.
9652 * @return the dismissal id of the notification or null if it has not been set.
9653 */
9654 public String getDismissalId() {
9655 return mDismissalId;
9656 }
9657
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009658 /**
9659 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
9660 * posted from a phone to provide finer-grained control on what notifications are bridged
9661 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
9662 * Features to Notifications</a> for more information.
9663 * @param bridgeTag the bridge tag of the notification.
9664 * @return this object for method chaining
9665 */
9666 public WearableExtender setBridgeTag(String bridgeTag) {
9667 mBridgeTag = bridgeTag;
9668 return this;
9669 }
9670
9671 /**
9672 * Returns the bridge tag of the notification.
9673 * @return the bridge tag or null if not present.
9674 */
9675 public String getBridgeTag() {
9676 return mBridgeTag;
9677 }
9678
Griff Hazen61a9e862014-05-22 16:05:19 -07009679 private void setFlag(int mask, boolean value) {
9680 if (value) {
9681 mFlags |= mask;
9682 } else {
9683 mFlags &= ~mask;
9684 }
9685 }
9686 }
9687
9688 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009689 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
9690 * with car extensions:
9691 *
9692 * <ol>
9693 * <li>Create an {@link Notification.Builder}, setting any desired
9694 * properties.
9695 * <li>Create a {@link CarExtender}.
9696 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
9697 * {@link CarExtender}.
9698 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9699 * to apply the extensions to a notification.
9700 * </ol>
9701 *
9702 * <pre class="prettyprint">
9703 * Notification notification = new Notification.Builder(context)
9704 * ...
9705 * .extend(new CarExtender()
9706 * .set*(...))
9707 * .build();
9708 * </pre>
9709 *
9710 * <p>Car extensions can be accessed on an existing notification by using the
9711 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
9712 * to access values.
9713 */
9714 public static final class CarExtender implements Extender {
9715 private static final String TAG = "CarExtender";
9716
9717 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
9718 private static final String EXTRA_LARGE_ICON = "large_icon";
9719 private static final String EXTRA_CONVERSATION = "car_conversation";
9720 private static final String EXTRA_COLOR = "app_color";
9721
9722 private Bitmap mLargeIcon;
9723 private UnreadConversation mUnreadConversation;
9724 private int mColor = Notification.COLOR_DEFAULT;
9725
9726 /**
9727 * Create a {@link CarExtender} with default options.
9728 */
9729 public CarExtender() {
9730 }
9731
9732 /**
9733 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
9734 *
9735 * @param notif The notification from which to copy options.
9736 */
9737 public CarExtender(Notification notif) {
9738 Bundle carBundle = notif.extras == null ?
9739 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
9740 if (carBundle != null) {
9741 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
9742 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
9743
9744 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
9745 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
9746 }
9747 }
9748
9749 /**
9750 * Apply car extensions to a notification that is being built. This is typically called by
9751 * the {@link Notification.Builder#extend(Notification.Extender)}
9752 * method of {@link Notification.Builder}.
9753 */
9754 @Override
9755 public Notification.Builder extend(Notification.Builder builder) {
9756 Bundle carExtensions = new Bundle();
9757
9758 if (mLargeIcon != null) {
9759 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
9760 }
9761 if (mColor != Notification.COLOR_DEFAULT) {
9762 carExtensions.putInt(EXTRA_COLOR, mColor);
9763 }
9764
9765 if (mUnreadConversation != null) {
9766 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
9767 carExtensions.putBundle(EXTRA_CONVERSATION, b);
9768 }
9769
9770 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
9771 return builder;
9772 }
9773
9774 /**
9775 * Sets the accent color to use when Android Auto presents the notification.
9776 *
9777 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
9778 * to accent the displayed notification. However, not all colors are acceptable in an
9779 * automotive setting. This method can be used to override the color provided in the
9780 * notification in such a situation.
9781 */
Tor Norbye80756e32015-03-02 09:39:27 -08009782 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009783 mColor = color;
9784 return this;
9785 }
9786
9787 /**
9788 * Gets the accent color.
9789 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04009790 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009791 */
Tor Norbye80756e32015-03-02 09:39:27 -08009792 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009793 public int getColor() {
9794 return mColor;
9795 }
9796
9797 /**
9798 * Sets the large icon of the car notification.
9799 *
9800 * If no large icon is set in the extender, Android Auto will display the icon
9801 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
9802 *
9803 * @param largeIcon The large icon to use in the car notification.
9804 * @return This object for method chaining.
9805 */
9806 public CarExtender setLargeIcon(Bitmap largeIcon) {
9807 mLargeIcon = largeIcon;
9808 return this;
9809 }
9810
9811 /**
9812 * Gets the large icon used in this car notification, or null if no icon has been set.
9813 *
9814 * @return The large icon for the car notification.
9815 * @see CarExtender#setLargeIcon
9816 */
9817 public Bitmap getLargeIcon() {
9818 return mLargeIcon;
9819 }
9820
9821 /**
9822 * Sets the unread conversation in a message notification.
9823 *
9824 * @param unreadConversation The unread part of the conversation this notification conveys.
9825 * @return This object for method chaining.
9826 */
9827 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
9828 mUnreadConversation = unreadConversation;
9829 return this;
9830 }
9831
9832 /**
9833 * Returns the unread conversation conveyed by this notification.
9834 * @see #setUnreadConversation(UnreadConversation)
9835 */
9836 public UnreadConversation getUnreadConversation() {
9837 return mUnreadConversation;
9838 }
9839
9840 /**
9841 * A class which holds the unread messages from a conversation.
9842 */
9843 public static class UnreadConversation {
9844 private static final String KEY_AUTHOR = "author";
9845 private static final String KEY_TEXT = "text";
9846 private static final String KEY_MESSAGES = "messages";
9847 private static final String KEY_REMOTE_INPUT = "remote_input";
9848 private static final String KEY_ON_REPLY = "on_reply";
9849 private static final String KEY_ON_READ = "on_read";
9850 private static final String KEY_PARTICIPANTS = "participants";
9851 private static final String KEY_TIMESTAMP = "timestamp";
9852
9853 private final String[] mMessages;
9854 private final RemoteInput mRemoteInput;
9855 private final PendingIntent mReplyPendingIntent;
9856 private final PendingIntent mReadPendingIntent;
9857 private final String[] mParticipants;
9858 private final long mLatestTimestamp;
9859
9860 UnreadConversation(String[] messages, RemoteInput remoteInput,
9861 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
9862 String[] participants, long latestTimestamp) {
9863 mMessages = messages;
9864 mRemoteInput = remoteInput;
9865 mReadPendingIntent = readPendingIntent;
9866 mReplyPendingIntent = replyPendingIntent;
9867 mParticipants = participants;
9868 mLatestTimestamp = latestTimestamp;
9869 }
9870
9871 /**
9872 * Gets the list of messages conveyed by this notification.
9873 */
9874 public String[] getMessages() {
9875 return mMessages;
9876 }
9877
9878 /**
9879 * Gets the remote input that will be used to convey the response to a message list, or
9880 * null if no such remote input exists.
9881 */
9882 public RemoteInput getRemoteInput() {
9883 return mRemoteInput;
9884 }
9885
9886 /**
9887 * Gets the pending intent that will be triggered when the user replies to this
9888 * notification.
9889 */
9890 public PendingIntent getReplyPendingIntent() {
9891 return mReplyPendingIntent;
9892 }
9893
9894 /**
9895 * Gets the pending intent that Android Auto will send after it reads aloud all messages
9896 * in this object's message list.
9897 */
9898 public PendingIntent getReadPendingIntent() {
9899 return mReadPendingIntent;
9900 }
9901
9902 /**
9903 * Gets the participants in the conversation.
9904 */
9905 public String[] getParticipants() {
9906 return mParticipants;
9907 }
9908
9909 /**
9910 * Gets the firs participant in the conversation.
9911 */
9912 public String getParticipant() {
9913 return mParticipants.length > 0 ? mParticipants[0] : null;
9914 }
9915
9916 /**
9917 * Gets the timestamp of the conversation.
9918 */
9919 public long getLatestTimestamp() {
9920 return mLatestTimestamp;
9921 }
9922
9923 Bundle getBundleForUnreadConversation() {
9924 Bundle b = new Bundle();
9925 String author = null;
9926 if (mParticipants != null && mParticipants.length > 1) {
9927 author = mParticipants[0];
9928 }
9929 Parcelable[] messages = new Parcelable[mMessages.length];
9930 for (int i = 0; i < messages.length; i++) {
9931 Bundle m = new Bundle();
9932 m.putString(KEY_TEXT, mMessages[i]);
9933 m.putString(KEY_AUTHOR, author);
9934 messages[i] = m;
9935 }
9936 b.putParcelableArray(KEY_MESSAGES, messages);
9937 if (mRemoteInput != null) {
9938 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
9939 }
9940 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
9941 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
9942 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
9943 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
9944 return b;
9945 }
9946
9947 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
9948 if (b == null) {
9949 return null;
9950 }
9951 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
9952 String[] messages = null;
9953 if (parcelableMessages != null) {
9954 String[] tmp = new String[parcelableMessages.length];
9955 boolean success = true;
9956 for (int i = 0; i < tmp.length; i++) {
9957 if (!(parcelableMessages[i] instanceof Bundle)) {
9958 success = false;
9959 break;
9960 }
9961 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
9962 if (tmp[i] == null) {
9963 success = false;
9964 break;
9965 }
9966 }
9967 if (success) {
9968 messages = tmp;
9969 } else {
9970 return null;
9971 }
9972 }
9973
9974 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
9975 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
9976
9977 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
9978
9979 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
9980 if (participants == null || participants.length != 1) {
9981 return null;
9982 }
9983
9984 return new UnreadConversation(messages,
9985 remoteInput,
9986 onReply,
9987 onRead,
9988 participants, b.getLong(KEY_TIMESTAMP));
9989 }
9990 };
9991
9992 /**
9993 * Builder class for {@link CarExtender.UnreadConversation} objects.
9994 */
9995 public static class Builder {
9996 private final List<String> mMessages = new ArrayList<String>();
9997 private final String mParticipant;
9998 private RemoteInput mRemoteInput;
9999 private PendingIntent mReadPendingIntent;
10000 private PendingIntent mReplyPendingIntent;
10001 private long mLatestTimestamp;
10002
10003 /**
10004 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
10005 *
10006 * @param name The name of the other participant in the conversation.
10007 */
10008 public Builder(String name) {
10009 mParticipant = name;
10010 }
10011
10012 /**
10013 * Appends a new unread message to the list of messages for this conversation.
10014 *
10015 * The messages should be added from oldest to newest.
10016 *
10017 * @param message The text of the new unread message.
10018 * @return This object for method chaining.
10019 */
10020 public Builder addMessage(String message) {
10021 mMessages.add(message);
10022 return this;
10023 }
10024
10025 /**
10026 * Sets the pending intent and remote input which will convey the reply to this
10027 * notification.
10028 *
10029 * @param pendingIntent The pending intent which will be triggered on a reply.
10030 * @param remoteInput The remote input parcelable which will carry the reply.
10031 * @return This object for method chaining.
10032 *
10033 * @see CarExtender.UnreadConversation#getRemoteInput
10034 * @see CarExtender.UnreadConversation#getReplyPendingIntent
10035 */
10036 public Builder setReplyAction(
10037 PendingIntent pendingIntent, RemoteInput remoteInput) {
10038 mRemoteInput = remoteInput;
10039 mReplyPendingIntent = pendingIntent;
10040
10041 return this;
10042 }
10043
10044 /**
10045 * Sets the pending intent that will be sent once the messages in this notification
10046 * are read.
10047 *
10048 * @param pendingIntent The pending intent to use.
10049 * @return This object for method chaining.
10050 */
10051 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
10052 mReadPendingIntent = pendingIntent;
10053 return this;
10054 }
10055
10056 /**
10057 * Sets the timestamp of the most recent message in an unread conversation.
10058 *
10059 * If a messaging notification has been posted by your application and has not
10060 * yet been cancelled, posting a later notification with the same id and tag
10061 * but without a newer timestamp may result in Android Auto not displaying a
10062 * heads up notification for the later notification.
10063 *
10064 * @param timestamp The timestamp of the most recent message in the conversation.
10065 * @return This object for method chaining.
10066 */
10067 public Builder setLatestTimestamp(long timestamp) {
10068 mLatestTimestamp = timestamp;
10069 return this;
10070 }
10071
10072 /**
10073 * Builds a new unread conversation object.
10074 *
10075 * @return The new unread conversation object.
10076 */
10077 public UnreadConversation build() {
10078 String[] messages = mMessages.toArray(new String[mMessages.size()]);
10079 String[] participants = { mParticipant };
10080 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
10081 mReadPendingIntent, participants, mLatestTimestamp);
10082 }
10083 }
10084 }
10085
10086 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010087 * <p>Helper class to add Android TV extensions to notifications. To create a notification
10088 * with a TV extension:
10089 *
10090 * <ol>
10091 * <li>Create an {@link Notification.Builder}, setting any desired properties.
10092 * <li>Create a {@link TvExtender}.
10093 * <li>Set TV-specific properties using the {@code set} methods of
10094 * {@link TvExtender}.
10095 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
10096 * to apply the extension to a notification.
10097 * </ol>
10098 *
10099 * <pre class="prettyprint">
10100 * Notification notification = new Notification.Builder(context)
10101 * ...
10102 * .extend(new TvExtender()
10103 * .set*(...))
10104 * .build();
10105 * </pre>
10106 *
10107 * <p>TV extensions can be accessed on an existing notification by using the
10108 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
10109 * to access values.
10110 *
10111 * @hide
10112 */
10113 @SystemApi
10114 public static final class TvExtender implements Extender {
10115 private static final String TAG = "TvExtender";
10116
10117 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
10118 private static final String EXTRA_FLAGS = "flags";
10119 private static final String EXTRA_CONTENT_INTENT = "content_intent";
10120 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010121 private static final String EXTRA_CHANNEL_ID = "channel_id";
Rhiannon Malia1a083932018-01-24 15:02:30 -080010122 private static final String EXTRA_SUPPRESS_SHOW_OVER_APPS = "suppressShowOverApps";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010123
10124 // Flags bitwise-ored to mFlags
10125 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
10126
10127 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010128 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010129 private PendingIntent mContentIntent;
10130 private PendingIntent mDeleteIntent;
Rhiannon Malia1a083932018-01-24 15:02:30 -080010131 private boolean mSuppressShowOverApps;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010132
10133 /**
10134 * Create a {@link TvExtender} with default options.
10135 */
10136 public TvExtender() {
10137 mFlags = FLAG_AVAILABLE_ON_TV;
10138 }
10139
10140 /**
10141 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
10142 *
10143 * @param notif The notification from which to copy options.
10144 */
10145 public TvExtender(Notification notif) {
10146 Bundle bundle = notif.extras == null ?
10147 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
10148 if (bundle != null) {
10149 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010150 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Rhiannon Malia1a083932018-01-24 15:02:30 -080010151 mSuppressShowOverApps = bundle.getBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010152 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
10153 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
10154 }
10155 }
10156
10157 /**
10158 * Apply a TV extension to a notification that is being built. This is typically called by
10159 * the {@link Notification.Builder#extend(Notification.Extender)}
10160 * method of {@link Notification.Builder}.
10161 */
10162 @Override
10163 public Notification.Builder extend(Notification.Builder builder) {
10164 Bundle bundle = new Bundle();
10165
10166 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010167 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Rhiannon Malia1a083932018-01-24 15:02:30 -080010168 bundle.putBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS, mSuppressShowOverApps);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010169 if (mContentIntent != null) {
10170 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
10171 }
10172
10173 if (mDeleteIntent != null) {
10174 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
10175 }
10176
10177 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
10178 return builder;
10179 }
10180
10181 /**
10182 * Returns true if this notification should be shown on TV. This method return true
10183 * if the notification was extended with a TvExtender.
10184 */
10185 public boolean isAvailableOnTv() {
10186 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
10187 }
10188
10189 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010190 * Specifies the channel the notification should be delivered on when shown on TV.
10191 * It can be different from the channel that the notification is delivered to when
10192 * posting on a non-TV device.
10193 */
10194 public TvExtender setChannel(String channelId) {
10195 mChannelId = channelId;
10196 return this;
10197 }
10198
10199 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -040010200 * Specifies the channel the notification should be delivered on when shown on TV.
10201 * It can be different from the channel that the notification is delivered to when
10202 * posting on a non-TV device.
10203 */
10204 public TvExtender setChannelId(String channelId) {
10205 mChannelId = channelId;
10206 return this;
10207 }
10208
Jeff Sharkey000ce802017-04-29 13:13:27 -060010209 /** @removed */
10210 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010211 public String getChannel() {
10212 return mChannelId;
10213 }
10214
10215 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -040010216 * Returns the id of the channel this notification posts to on TV.
10217 */
10218 public String getChannelId() {
10219 return mChannelId;
10220 }
10221
10222 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010223 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
10224 * If provided, it is used instead of the content intent specified
10225 * at the level of Notification.
10226 */
10227 public TvExtender setContentIntent(PendingIntent intent) {
10228 mContentIntent = intent;
10229 return this;
10230 }
10231
10232 /**
10233 * Returns the TV-specific content intent. If this method returns null, the
10234 * main content intent on the notification should be used.
10235 *
10236 * @see {@link Notification#contentIntent}
10237 */
10238 public PendingIntent getContentIntent() {
10239 return mContentIntent;
10240 }
10241
10242 /**
10243 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
10244 * by the user on TV. If provided, it is used instead of the delete intent specified
10245 * at the level of Notification.
10246 */
10247 public TvExtender setDeleteIntent(PendingIntent intent) {
10248 mDeleteIntent = intent;
10249 return this;
10250 }
10251
10252 /**
10253 * Returns the TV-specific delete intent. If this method returns null, the
10254 * main delete intent on the notification should be used.
10255 *
10256 * @see {@link Notification#deleteIntent}
10257 */
10258 public PendingIntent getDeleteIntent() {
10259 return mDeleteIntent;
10260 }
Rhiannon Malia1a083932018-01-24 15:02:30 -080010261
10262 /**
10263 * Specifies whether this notification should suppress showing a message over top of apps
10264 * outside of the launcher.
10265 */
10266 public TvExtender setSuppressShowOverApps(boolean suppress) {
10267 mSuppressShowOverApps = suppress;
10268 return this;
10269 }
10270
10271 /**
10272 * Returns true if this notification should not show messages over top of apps
10273 * outside of the launcher.
10274 */
10275 public boolean getSuppressShowOverApps() {
10276 return mSuppressShowOverApps;
10277 }
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010278 }
10279
10280 /**
Griff Hazen61a9e862014-05-22 16:05:19 -070010281 * Get an array of Notification objects from a parcelable array bundle field.
10282 * Update the bundle to have a typed array so fetches in the future don't need
10283 * to do an array copy.
10284 */
10285 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
10286 Parcelable[] array = bundle.getParcelableArray(key);
10287 if (array instanceof Notification[] || array == null) {
10288 return (Notification[]) array;
10289 }
10290 Notification[] typedArray = Arrays.copyOf(array, array.length,
10291 Notification[].class);
10292 bundle.putParcelableArray(key, typedArray);
10293 return typedArray;
10294 }
Christoph Studer4600f9b2014-07-22 22:44:43 +020010295
10296 private static class BuilderRemoteViews extends RemoteViews {
10297 public BuilderRemoteViews(Parcel parcel) {
10298 super(parcel);
10299 }
10300
Kenny Guy77320062014-08-27 21:37:15 +010010301 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
10302 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +020010303 }
10304
10305 @Override
10306 public BuilderRemoteViews clone() {
10307 Parcel p = Parcel.obtain();
10308 writeToParcel(p, 0);
10309 p.setDataPosition(0);
10310 BuilderRemoteViews brv = new BuilderRemoteViews(p);
10311 p.recycle();
10312 return brv;
10313 }
10314 }
Adrian Roos70d7aa32017-01-11 15:39:06 -080010315
Selim Cinek384804b2018-04-18 14:31:07 +080010316 /**
10317 * A result object where information about the template that was created is saved.
10318 */
10319 private static class TemplateBindResult {
Selim Cinek1c72fa02018-04-23 18:00:54 +080010320 int mIconMarginEnd;
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010321 boolean mRightIconContainerVisible;
Selim Cinek384804b2018-04-18 14:31:07 +080010322
10323 /**
Selim Cinek1c72fa02018-04-23 18:00:54 +080010324 * Get the margin end that needs to be added to any fields that may overlap
Selim Cinek384804b2018-04-18 14:31:07 +080010325 * with the right actions.
10326 */
Selim Cinek1c72fa02018-04-23 18:00:54 +080010327 public int getIconMarginEnd() {
10328 return mIconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +080010329 }
10330
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010331 /**
10332 * Is the icon container visible on the right size because of the reply button or the
10333 * right icon.
10334 */
10335 public boolean isRightIconContainerVisible() {
10336 return mRightIconContainerVisible;
10337 }
10338
Selim Cinek1c72fa02018-04-23 18:00:54 +080010339 public void setIconMarginEnd(int iconMarginEnd) {
10340 this.mIconMarginEnd = iconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +080010341 }
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010342
10343 public void setRightIconContainerVisible(boolean iconContainerVisible) {
10344 mRightIconContainerVisible = iconContainerVisible;
10345 }
Selim Cinek384804b2018-04-18 14:31:07 +080010346 }
10347
Adrian Roos70d7aa32017-01-11 15:39:06 -080010348 private static class StandardTemplateParams {
10349 boolean hasProgress = true;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010350 CharSequence title;
10351 CharSequence text;
Selim Cinekafeed292017-12-12 17:32:44 -080010352 CharSequence headerTextSecondary;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010353 CharSequence summaryText;
Selim Cinekbee4e072018-05-21 22:06:43 -070010354 int maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek88188f22017-09-19 16:46:56 -070010355 boolean hideLargeIcon;
Selim Cinek384804b2018-04-18 14:31:07 +080010356 boolean hideReplyIcon;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010357 boolean allowColorization = true;
10358 boolean forceDefaultColor = false;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010359
10360 final StandardTemplateParams reset() {
10361 hasProgress = true;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010362 title = null;
10363 text = null;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010364 summaryText = null;
Selim Cinekafeed292017-12-12 17:32:44 -080010365 headerTextSecondary = null;
Selim Cinekbee4e072018-05-21 22:06:43 -070010366 maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010367 allowColorization = true;
10368 forceDefaultColor = false;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010369 return this;
10370 }
10371
10372 final StandardTemplateParams hasProgress(boolean hasProgress) {
10373 this.hasProgress = hasProgress;
10374 return this;
10375 }
10376
10377 final StandardTemplateParams title(CharSequence title) {
10378 this.title = title;
10379 return this;
10380 }
10381
10382 final StandardTemplateParams text(CharSequence text) {
10383 this.text = text;
10384 return this;
10385 }
10386
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010387 final StandardTemplateParams summaryText(CharSequence text) {
10388 this.summaryText = text;
10389 return this;
10390 }
10391
Selim Cinekafeed292017-12-12 17:32:44 -080010392 final StandardTemplateParams headerTextSecondary(CharSequence text) {
10393 this.headerTextSecondary = text;
10394 return this;
10395 }
10396
Selim Cinek384804b2018-04-18 14:31:07 +080010397 final StandardTemplateParams hideLargeIcon(boolean hideLargeIcon) {
10398 this.hideLargeIcon = hideLargeIcon;
Selim Cinek88188f22017-09-19 16:46:56 -070010399 return this;
10400 }
10401
Selim Cinek384804b2018-04-18 14:31:07 +080010402 final StandardTemplateParams hideReplyIcon(boolean hideReplyIcon) {
10403 this.hideReplyIcon = hideReplyIcon;
Selim Cinek88188f22017-09-19 16:46:56 -070010404 return this;
10405 }
10406
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010407 final StandardTemplateParams disallowColorization() {
10408 this.allowColorization = false;
10409 return this;
10410 }
10411
10412 final StandardTemplateParams forceDefaultColor() {
10413 this.forceDefaultColor = true;
10414 return this;
10415 }
10416
Adrian Roos70d7aa32017-01-11 15:39:06 -080010417 final StandardTemplateParams fillTextsFrom(Builder b) {
10418 Bundle extras = b.mN.extras;
Lucas Dupin00be88f2019-01-03 17:50:52 -080010419 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE));
Lucas Dupin06c5e642017-09-13 16:34:58 -070010420
Lucas Dupin06c5e642017-09-13 16:34:58 -070010421 CharSequence text = extras.getCharSequence(EXTRA_BIG_TEXT);
Lucas Dupin00be88f2019-01-03 17:50:52 -080010422 if (TextUtils.isEmpty(text)) {
Lucas Dupin06c5e642017-09-13 16:34:58 -070010423 text = extras.getCharSequence(EXTRA_TEXT);
10424 }
Lucas Dupin00be88f2019-01-03 17:50:52 -080010425 this.text = b.processLegacyText(text);
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010426 this.summaryText = extras.getCharSequence(EXTRA_SUB_TEXT);
Adrian Roos70d7aa32017-01-11 15:39:06 -080010427 return this;
10428 }
Selim Cinekbee4e072018-05-21 22:06:43 -070010429
10430 /**
10431 * Set the maximum lines of remote input history lines allowed.
10432 * @param maxRemoteInputHistory The number of lines.
10433 * @return The builder for method chaining.
10434 */
10435 public StandardTemplateParams setMaxRemoteInputHistory(int maxRemoteInputHistory) {
10436 this.maxRemoteInputHistory = maxRemoteInputHistory;
10437 return this;
10438 }
Adrian Roos70d7aa32017-01-11 15:39:06 -080010439 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010440}