blob: 03806faaeaa8b1ffb13b5d727f7822bc868c8e4b [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 Mellorc39b4ae2019-01-09 17:11:37 -08003180 public BubbleMetadata getBubbleMetadata() {
3181 return mBubbleMetadata;
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003182 }
3183
Gustav Sennton79ebf4b2018-12-19 10:31:51 +00003184 /**
3185 * Returns whether the platform is allowed (by the app developer) to generate contextual actions
3186 * for this notification.
3187 */
Gustav Sennton761884c2018-11-19 17:40:19 +00003188 public boolean getAllowSystemGeneratedContextualActions() {
3189 return mAllowSystemGeneratedContextualActions;
3190 }
3191
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003192 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003193 * The small icon representing this notification in the status bar and content view.
3194 *
3195 * @return the small icon representing this notification.
3196 *
3197 * @see Builder#getSmallIcon()
3198 * @see Builder#setSmallIcon(Icon)
3199 */
3200 public Icon getSmallIcon() {
3201 return mSmallIcon;
3202 }
3203
3204 /**
3205 * Used when notifying to clean up legacy small icons.
3206 * @hide
3207 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003208 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04003209 public void setSmallIcon(Icon icon) {
3210 mSmallIcon = icon;
3211 }
3212
3213 /**
3214 * The large icon shown in this notification's content view.
3215 * @see Builder#getLargeIcon()
3216 * @see Builder#setLargeIcon(Icon)
3217 */
3218 public Icon getLargeIcon() {
3219 return mLargeIcon;
3220 }
3221
3222 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02003223 * @hide
3224 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003225 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003226 public boolean isGroupSummary() {
3227 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
3228 }
3229
3230 /**
3231 * @hide
3232 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003233 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003234 public boolean isGroupChild() {
3235 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
3236 }
3237
3238 /**
Julia Reynolds30203152017-05-26 13:36:31 -04003239 * @hide
3240 */
3241 public boolean suppressAlertingDueToGrouping() {
3242 if (isGroupSummary()
3243 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
3244 return true;
3245 } else if (isGroupChild()
3246 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
3247 return true;
3248 }
3249 return false;
3250 }
3251
Tony Mak638430e2018-10-08 19:19:10 +01003252
3253 /**
3254 * Finds and returns a remote input and its corresponding action.
3255 *
3256 * @param requiresFreeform requires the remoteinput to allow freeform or not.
3257 * @return the result pair, {@code null} if no result is found.
3258 *
3259 * @hide
3260 */
3261 @Nullable
3262 public Pair<RemoteInput, Action> findRemoteInputActionPair(boolean requiresFreeform) {
3263 if (actions == null) {
3264 return null;
3265 }
3266 for (Notification.Action action : actions) {
3267 if (action.getRemoteInputs() == null) {
3268 continue;
3269 }
3270 RemoteInput resultRemoteInput = null;
3271 for (RemoteInput remoteInput : action.getRemoteInputs()) {
3272 if (remoteInput.getAllowFreeFormInput() || !requiresFreeform) {
3273 resultRemoteInput = remoteInput;
3274 }
3275 }
3276 if (resultRemoteInput != null) {
3277 return Pair.create(resultRemoteInput, action);
3278 }
3279 }
3280 return null;
3281 }
3282
Julia Reynolds30203152017-05-26 13:36:31 -04003283 /**
Gustav Sennton005d7a02019-01-04 13:41:32 +00003284 * Returns the actions that are contextual out of the actions in this notification.
Gustav Senntoneab53682018-11-01 16:30:23 +00003285 *
3286 * @hide
3287 */
3288 public List<Notification.Action> getContextualActions() {
3289 if (actions == null) return Collections.emptyList();
3290
3291 List<Notification.Action> contextualActions = new ArrayList<>();
3292 for (Notification.Action action : actions) {
Gustav Sennton005d7a02019-01-04 13:41:32 +00003293 if (action.isContextual()) {
Gustav Senntoneab53682018-11-01 16:30:23 +00003294 contextualActions.add(action);
3295 }
3296 }
3297 return contextualActions;
3298 }
3299
3300 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003301 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08003302 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003303 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07003304 * content views using the platform's notification layout template. If your app supports
3305 * versions of Android as old as API level 4, you can instead use
3306 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
3307 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
3308 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08003309 *
Scott Main183bf112012-08-13 19:12:13 -07003310 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08003311 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003312 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07003313 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003314 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3315 * .setContentText(subject)
3316 * .setSmallIcon(R.drawable.new_mail)
3317 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003318 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003319 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08003320 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003321 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003322 /**
3323 * @hide
3324 */
3325 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
3326 "android.rebuild.contentViewActionCount";
3327 /**
3328 * @hide
3329 */
3330 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
3331 = "android.rebuild.bigViewActionCount";
3332 /**
3333 * @hide
3334 */
3335 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
3336 = "android.rebuild.hudViewActionCount";
3337
Selim Cinek6743c0b2017-01-18 18:24:01 -08003338 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
3339 SystemProperties.getBoolean("notifications.only_title", true);
3340
Selim Cinek389edcd2017-05-11 19:16:44 -07003341 /**
3342 * The lightness difference that has to be added to the primary text color to obtain the
3343 * secondary text color when the background is light.
3344 */
3345 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
3346
3347 /**
3348 * The lightness difference that has to be added to the primary text color to obtain the
3349 * secondary text color when the background is dark.
3350 * A bit less then the above value, since it looks better on dark backgrounds.
3351 */
3352 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
3353
Joe Onorato46439ce2010-11-19 13:56:21 -08003354 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003355 private Notification mN;
3356 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003357 private Style mStyle;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003358 @UnsupportedAppUsage
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003359 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Selim Cineke7238dd2017-12-14 17:48:32 -08003360 private ArrayList<Person> mPersonList = new ArrayList<>();
Lucas Dupina291d192018-06-07 13:59:42 -07003361 private ContrastColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08003362 private boolean mIsLegacy;
3363 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02003364
3365 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08003366 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
3367 */
3368 private int mCachedContrastColor = COLOR_INVALID;
3369 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08003370 /**
Selim Cinek4717d862018-04-19 09:19:15 +08003371 * Caches a ambient version of {@link #mCachedAmbientColorIsFor}.
Adrian Roos487374f2017-01-11 15:48:14 -08003372 */
3373 private int mCachedAmbientColor = COLOR_INVALID;
3374 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Selim Cinek4717d862018-04-19 09:19:15 +08003375 /**
3376 * A neutral color color that can be used for icons.
3377 */
3378 private int mNeutralColor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08003379
3380 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08003381 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
3382 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
3383 */
3384 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08003385 private int mTextColorsAreForBackground = COLOR_INVALID;
3386 private int mPrimaryTextColor = COLOR_INVALID;
3387 private int mSecondaryTextColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07003388 private int mBackgroundColor = COLOR_INVALID;
3389 private int mForegroundColor = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07003390 /**
3391 * A temporary location where actions are stored. If != null the view originally has action
3392 * but doesn't have any for this inflation.
3393 */
3394 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07003395 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003396
Anthony Chenad4d1582017-04-10 16:07:58 -07003397 private boolean mTintActionButtons;
3398 private boolean mInNightMode;
3399
Adrian Roos70d7aa32017-01-11 15:39:06 -08003400 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003401 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08003402 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003403 * @param context
3404 * A {@link Context} that will be used by the Builder to construct the
3405 * RemoteViews. The Context will not be held past the lifetime of this Builder
3406 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003407 * @param channelId
3408 * The constructed Notification will be posted on this
3409 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
3410 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003411 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003412 public Builder(Context context, String channelId) {
3413 this(context, (Notification) null);
3414 mN.mChannelId = channelId;
3415 }
3416
3417 /**
3418 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
3419 * instead. All posted Notifications must specify a NotificationChannel Id.
3420 */
3421 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003422 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003423 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003424 }
3425
Joe Onoratocb109a02011-01-18 17:57:41 -08003426 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003427 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02003428 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003429 public Builder(Context context, Notification toAdopt) {
3430 mContext = context;
Anthony Chenad4d1582017-04-10 16:07:58 -07003431 Resources res = mContext.getResources();
3432 mTintActionButtons = res.getBoolean(R.bool.config_tintNotificationActionButtons);
3433
3434 if (res.getBoolean(R.bool.config_enableNightMode)) {
3435 Configuration currentConfig = res.getConfiguration();
3436 mInNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
3437 == Configuration.UI_MODE_NIGHT_YES;
3438 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003439
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003440 if (toAdopt == null) {
3441 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003442 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
3443 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
3444 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003445 mN.priority = PRIORITY_DEFAULT;
3446 mN.visibility = VISIBILITY_PRIVATE;
3447 } else {
3448 mN = toAdopt;
3449 if (mN.actions != null) {
3450 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003451 }
3452
Selim Cineke7238dd2017-12-14 17:48:32 -08003453 if (mN.extras.containsKey(EXTRA_PEOPLE_LIST)) {
3454 ArrayList<Person> people = mN.extras.getParcelableArrayList(EXTRA_PEOPLE_LIST);
3455 mPersonList.addAll(people);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003456 }
3457
Selim Cinek4ac6f602016-06-13 15:47:03 -07003458 if (mN.getSmallIcon() == null && mN.icon != 0) {
3459 setSmallIcon(mN.icon);
3460 }
3461
3462 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
3463 setLargeIcon(mN.largeIcon);
3464 }
3465
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003466 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
3467 if (!TextUtils.isEmpty(templateClass)) {
3468 final Class<? extends Style> styleClass
3469 = getNotificationStyleClass(templateClass);
3470 if (styleClass == null) {
3471 Log.d(TAG, "Unknown style class: " + templateClass);
3472 } else {
3473 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003474 final Constructor<? extends Style> ctor =
3475 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003476 ctor.setAccessible(true);
3477 final Style style = ctor.newInstance();
3478 style.restoreFromExtras(mN.extras);
3479
3480 if (style != null) {
3481 setStyle(style);
3482 }
3483 } catch (Throwable t) {
3484 Log.e(TAG, "Could not create Style", t);
3485 }
3486 }
3487 }
3488
3489 }
3490 }
3491
Lucas Dupina291d192018-06-07 13:59:42 -07003492 private ContrastColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08003493 if (mColorUtil == null) {
Lucas Dupina291d192018-06-07 13:59:42 -07003494 mColorUtil = ContrastColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003495 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003496 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003497 }
3498
3499 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003500 * If this notification is duplicative of a Launcher shortcut, sets the
3501 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
3502 * the shortcut.
3503 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04003504 * This field will be ignored by Launchers that don't support badging, don't show
3505 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003506 *
3507 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
3508 * supersedes
3509 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003510 @NonNull
Julia Reynolds13d898c2017-02-02 12:22:05 -05003511 public Builder setShortcutId(String shortcutId) {
3512 mN.mShortcutId = shortcutId;
3513 return this;
3514 }
3515
3516 /**
Felipe Leme90205ef2019-03-05 09:59:52 -08003517 * Sets the {@link LocusId} associated with this notification.
3518 *
3519 * <p>This method should be called when the {@link LocusId} is used in other places (such
3520 * as {@link ShortcutInfo} and {@link ContentCaptureContext}) so the device's intelligence
3521 * services can correlate them.
3522 */
3523 @NonNull
3524 public Builder setLocusId(@Nullable LocusId locusId) {
3525 mN.mLocusId = locusId;
3526 return this;
3527 }
3528
3529 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003530 * Sets which icon to display as a badge for this notification.
3531 *
3532 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
3533 * {@link #BADGE_ICON_LARGE}.
3534 *
3535 * Note: This value might be ignored, for launchers that don't support badge icons.
3536 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003537 @NonNull
Julia Reynolds612beb22017-03-30 10:48:30 -04003538 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04003539 mN.mBadgeIcon = icon;
3540 return this;
3541 }
3542
3543 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003544 * Sets the group alert behavior for this notification. Use this method to mute this
3545 * notification if alerts for this notification's group should be handled by a different
3546 * notification. This is only applicable for notifications that belong to a
Julia Reynolds399d9bf2017-08-11 12:52:14 -04003547 * {@link #setGroup(String) group}. This must be called on all notifications you want to
3548 * mute. For example, if you want only the summary of your group to make noise, all
3549 * children in the group should have the group alert behavior {@link #GROUP_ALERT_SUMMARY}.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003550 *
3551 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
3552 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003553 @NonNull
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003554 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
3555 mN.mGroupAlertBehavior = groupAlertBehavior;
3556 return this;
3557 }
3558
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003559 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003560 * Sets the {@link BubbleMetadata} that will be used to display app content in a floating
3561 * window over the existing foreground activity.
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003562 *
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003563 * <p>This data will be ignored unless the notification is posted to a channel that
3564 * allows {@link NotificationChannel#canBubble() bubbles}.</p>
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003565 *
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003566 * <b>Notifications with a valid and allowed bubble metadata will display in collapsed state
3567 * outside of the notification shade on unlocked devices. When a user interacts with the
3568 * collapsed state, the bubble intent will be invoked and displayed.</b>
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003569 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003570 @NonNull
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003571 public Builder setBubbleMetadata(BubbleMetadata data) {
3572 mN.mBubbleMetadata = data;
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003573 return this;
3574 }
3575
Jeff Sharkey000ce802017-04-29 13:13:27 -06003576 /** @removed */
3577 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003578 public Builder setChannel(String channelId) {
3579 mN.mChannelId = channelId;
3580 return this;
3581 }
3582
3583 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003584 * Specifies the channel the notification should be delivered on.
3585 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003586 @NonNull
Julia Reynoldsbad42972017-04-25 13:52:49 -04003587 public Builder setChannelId(String channelId) {
3588 mN.mChannelId = channelId;
3589 return this;
3590 }
3591
Jeff Sharkey000ce802017-04-29 13:13:27 -06003592 /** @removed */
3593 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05003594 public Builder setTimeout(long durationMs) {
3595 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05003596 return this;
3597 }
3598
3599 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003600 * Specifies a duration in milliseconds after which this notification should be canceled,
3601 * if it is not already canceled.
3602 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003603 @NonNull
Julia Reynoldsbad42972017-04-25 13:52:49 -04003604 public Builder setTimeoutAfter(long durationMs) {
3605 mN.mTimeout = durationMs;
3606 return this;
3607 }
3608
3609 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003610 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003611 *
3612 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
3613 * shown anymore by default and must be opted into by using
3614 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003615 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003616 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08003617 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003618 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003619 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003620 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08003621 return this;
3622 }
3623
Joe Onoratocb109a02011-01-18 17:57:41 -08003624 /**
Griff Hazen50c11652014-05-16 09:46:31 -07003625 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07003626 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003627 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
3628 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07003629 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003630 @NonNull
Daniel Sandler0c890492012-09-12 17:23:10 -07003631 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003632 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07003633 return this;
3634 }
3635
3636 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003637 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08003638 *
3639 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003640 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003641 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003642 * Useful when showing an elapsed time (like an ongoing phone call).
3643 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08003644 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07003645 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08003646 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003647 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003648 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07003649 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003650 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003651 @NonNull
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003652 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003653 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003654 return this;
3655 }
3656
3657 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08003658 * Sets the Chronometer to count down instead of counting up.
3659 *
3660 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
3661 * If it isn't set the chronometer will count up.
3662 *
3663 * @see #setUsesChronometer(boolean)
3664 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003665 @NonNull
Adrian Roos96b7e202016-05-17 13:50:38 -07003666 public Builder setChronometerCountDown(boolean countDown) {
3667 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003668 return this;
3669 }
3670
3671 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003672 * Set the small icon resource, which will be used to represent the notification in the
3673 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003674 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003675
3676 * The platform template for the expanded view will draw this icon in the left, unless a
3677 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3678 * icon will be moved to the right-hand side.
3679 *
3680
3681 * @param icon
3682 * A resource ID in the application's package of the drawable to use.
3683 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003684 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003685 @NonNull
Tor Norbye7b9c9122013-05-30 16:48:33 -07003686 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003687 return setSmallIcon(icon != 0
3688 ? Icon.createWithResource(mContext, icon)
3689 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003690 }
3691
Joe Onoratocb109a02011-01-18 17:57:41 -08003692 /**
3693 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3694 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3695 * LevelListDrawable}.
3696 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003697 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003698 * @param level The level to use for the icon.
3699 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003700 * @see Notification#icon
3701 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003702 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003703 @NonNull
Tor Norbye7b9c9122013-05-30 16:48:33 -07003704 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003705 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003706 return setSmallIcon(icon);
3707 }
3708
3709 /**
3710 * Set the small icon, which will be used to represent the notification in the
koprivadebd4ee2018-09-13 10:59:46 -07003711 * status bar and content view (unless overridden there by a
Dan Sandlerd63f9322015-05-06 15:18:49 -04003712 * {@link #setLargeIcon(Bitmap) large icon}).
3713 *
3714 * @param icon An Icon object to use.
3715 * @see Notification#icon
3716 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003717 @NonNull
Dan Sandlerd63f9322015-05-06 15:18:49 -04003718 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003719 mN.setSmallIcon(icon);
3720 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3721 mN.icon = icon.getResId();
3722 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003723 return this;
3724 }
3725
Joe Onoratocb109a02011-01-18 17:57:41 -08003726 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003727 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003728 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003729 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003730 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003731 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003732 return this;
3733 }
3734
Joe Onoratocb109a02011-01-18 17:57:41 -08003735 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003736 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003737 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003738 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003739 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003740 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003741 return this;
3742 }
3743
Joe Onoratocb109a02011-01-18 17:57:41 -08003744 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003745 * This provides some additional information that is displayed in the notification. No
3746 * guarantees are given where exactly it is displayed.
3747 *
3748 * <p>This information should only be provided if it provides an essential
3749 * benefit to the understanding of the notification. The more text you provide the
3750 * less readable it becomes. For example, an email client should only provide the account
3751 * name here if more than one email account has been added.</p>
3752 *
3753 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3754 * notification header area.
3755 *
3756 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3757 * this will be shown in the third line of text in the platform notification template.
3758 * You should not be using {@link #setProgress(int, int, boolean)} at the
3759 * same time on those versions; they occupy the same place.
3760 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003761 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003762 @NonNull
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003763 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003764 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003765 return this;
3766 }
3767
3768 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003769 * Provides text that will appear as a link to your application's settings.
3770 *
3771 * <p>This text does not appear within notification {@link Style templates} but may
3772 * appear when the user uses an affordance to learn more about the notification.
3773 * Additionally, this text will not appear unless you provide a valid link target by
3774 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3775 *
3776 * <p>This text is meant to be concise description about what the user can customize
3777 * when they click on this link. The recommended maximum length is 40 characters.
3778 * @param text
3779 * @return
3780 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003781 @NonNull
Julia Reynolds3aedded2017-03-31 14:42:09 -04003782 public Builder setSettingsText(CharSequence text) {
3783 mN.mSettingsText = safeCharSequence(text);
3784 return this;
3785 }
3786
3787 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003788 * Set the remote input history.
3789 *
3790 * This should be set to the most recent inputs that have been sent
3791 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3792 * longer relevant (e.g. for chat notifications once the other party has responded).
3793 *
3794 * The most recent input must be stored at the 0 index, the second most recent at the
3795 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3796 * and how much of each individual input is shown.
3797 *
3798 * <p>Note: The reply text will only be shown on notifications that have least one action
3799 * with a {@code RemoteInput}.</p>
3800 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003801 @NonNull
Adrian Roose458aa82015-12-08 16:17:19 -08003802 public Builder setRemoteInputHistory(CharSequence[] text) {
3803 if (text == null) {
3804 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3805 } else {
3806 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3807 CharSequence[] safe = new CharSequence[N];
3808 for (int i = 0; i < N; i++) {
3809 safe[i] = safeCharSequence(text[i]);
3810 }
3811 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3812 }
3813 return this;
3814 }
3815
3816 /**
Kenny Guya0f6de82018-04-06 16:20:16 +01003817 * Sets whether remote history entries view should have a spinner.
3818 * @hide
3819 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003820 @NonNull
Kenny Guya0f6de82018-04-06 16:20:16 +01003821 public Builder setShowRemoteInputSpinner(boolean showSpinner) {
3822 mN.extras.putBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER, showSpinner);
3823 return this;
3824 }
3825
3826 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +01003827 * Sets whether smart reply buttons should be hidden.
3828 * @hide
3829 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003830 @NonNull
Kenny Guy8cc15d22018-05-09 09:50:55 +01003831 public Builder setHideSmartReplies(boolean hideSmartReplies) {
3832 mN.extras.putBoolean(EXTRA_HIDE_SMART_REPLIES, hideSmartReplies);
3833 return this;
3834 }
3835
3836 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003837 * Sets the number of items this notification represents. May be displayed as a badge count
3838 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003839 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003840 @NonNull
Joe Onorato8595a3d2010-11-19 18:12:07 -08003841 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003842 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003843 return this;
3844 }
3845
Joe Onoratocb109a02011-01-18 17:57:41 -08003846 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003847 * A small piece of additional information pertaining to this notification.
3848 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003849 * The platform template will draw this on the last line of the notification, at the far
3850 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003851 *
3852 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3853 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3854 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003855 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003856 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003857 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003858 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003859 return this;
3860 }
3861
Joe Onoratocb109a02011-01-18 17:57:41 -08003862 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003863 * Set the progress this notification represents.
3864 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003865 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003866 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003867 @NonNull
Jeff Sharkey1c400132011-08-05 14:50:13 -07003868 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003869 mN.extras.putInt(EXTRA_PROGRESS, progress);
3870 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3871 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003872 return this;
3873 }
3874
3875 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003876 * Supply a custom RemoteViews to use instead of the platform template.
3877 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003878 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003879 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003880 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003881 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003882 return setCustomContentView(views);
3883 }
3884
3885 /**
3886 * Supply custom RemoteViews to use instead of the platform template.
3887 *
3888 * This will override the layout that would otherwise be constructed by this Builder
3889 * object.
3890 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003891 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003892 public Builder setCustomContentView(RemoteViews contentView) {
3893 mN.contentView = contentView;
3894 return this;
3895 }
3896
3897 /**
3898 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3899 *
3900 * This will override the expanded layout that would otherwise be constructed by this
3901 * Builder object.
3902 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003903 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003904 public Builder setCustomBigContentView(RemoteViews contentView) {
3905 mN.bigContentView = contentView;
3906 return this;
3907 }
3908
3909 /**
3910 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3911 *
3912 * This will override the heads-up layout that would otherwise be constructed by this
3913 * Builder object.
3914 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003915 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003916 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3917 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003918 return this;
3919 }
3920
Joe Onoratocb109a02011-01-18 17:57:41 -08003921 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003922 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3923 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003924 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3925 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3926 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003927 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003928 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003929 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003930 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003931 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003932 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003933 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003934 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003935 return this;
3936 }
3937
Joe Onoratocb109a02011-01-18 17:57:41 -08003938 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003939 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3940 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003941 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003942 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003943 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003944 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003945 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003946 return this;
3947 }
3948
Joe Onoratocb109a02011-01-18 17:57:41 -08003949 /**
3950 * An intent to launch instead of posting the notification to the status bar.
3951 * Only for use with extremely high-priority notifications demanding the user's
3952 * <strong>immediate</strong> attention, such as an incoming phone call or
3953 * alarm clock that the user has explicitly set to a particular time.
3954 * If this facility is used for something else, please give the user an option
3955 * to turn it off and use a normal notification, as this can be extremely
3956 * disruptive.
3957 *
Chris Wren47c20a12014-06-18 17:27:29 -04003958 * <p>
3959 * The system UI may choose to display a heads-up notification, instead of
3960 * launching this intent, while the user is using the device.
3961 * </p>
Julia Reynolds47fd15f2018-11-28 10:16:00 -05003962 * <p>Apps targeting {@link Build.VERSION_CODES#Q} and above will have to request
3963 * a permission ({@link android.Manifest.permission#USE_FULL_SCREEN_INTENT}) in order to
3964 * use full screen intents.</p>
Chris Wren47c20a12014-06-18 17:27:29 -04003965 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003966 * @param intent The pending intent to launch.
3967 * @param highPriority Passing true will cause this notification to be sent
3968 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003969 *
3970 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003971 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003972 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003973 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003974 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003975 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3976 return this;
3977 }
3978
Joe Onoratocb109a02011-01-18 17:57:41 -08003979 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003980 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003981 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003982 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003983 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003984 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003985 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003986 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003987 return this;
3988 }
3989
Joe Onoratocb109a02011-01-18 17:57:41 -08003990 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003991 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003992 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003993 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003994 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003995 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003996 setTicker(tickerText);
3997 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003998 return this;
3999 }
4000
Joe Onoratocb109a02011-01-18 17:57:41 -08004001 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04004002 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004003 *
4004 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04004005 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
4006 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04004007 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004008 @NonNull
Dan Sandlerd63f9322015-05-06 15:18:49 -04004009 public Builder setLargeIcon(Bitmap b) {
4010 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
4011 }
4012
4013 /**
4014 * Add a large icon to the notification content view.
4015 *
4016 * In the platform template, this image will be shown on the left of the notification view
4017 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
4018 * badge atop the large icon).
4019 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004020 @NonNull
Dan Sandlerd63f9322015-05-06 15:18:49 -04004021 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004022 mN.mLargeIcon = icon;
4023 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08004024 return this;
4025 }
4026
Joe Onoratocb109a02011-01-18 17:57:41 -08004027 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004028 * Set the sound to play.
4029 *
John Spurlockc0650f022014-07-19 13:22:39 -04004030 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
4031 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004032 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004033 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08004034 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004035 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08004036 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004037 mN.sound = sound;
4038 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08004039 return this;
4040 }
4041
Joe Onoratocb109a02011-01-18 17:57:41 -08004042 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004043 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08004044 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004045 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
4046 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004047 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08004048 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07004049 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004050 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08004051 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004052 mN.sound = sound;
4053 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08004054 return this;
4055 }
4056
Joe Onoratocb109a02011-01-18 17:57:41 -08004057 /**
John Spurlockc0650f022014-07-19 13:22:39 -04004058 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
4059 * use during playback.
4060 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004061 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04004062 * @see Notification#sound
4063 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004064 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04004065 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004066 mN.sound = sound;
4067 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04004068 return this;
4069 }
4070
4071 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08004072 * Set the vibration pattern to use.
4073 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004074 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
4075 * <code>pattern</code> parameter.
4076 *
Chris Wren47c20a12014-06-18 17:27:29 -04004077 * <p>
4078 * A notification that vibrates is more likely to be presented as a heads-up notification.
4079 * </p>
4080 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004081 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004082 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08004083 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004084 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004085 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004086 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08004087 return this;
4088 }
4089
Joe Onoratocb109a02011-01-18 17:57:41 -08004090 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004091 * Set the desired color for the indicator LED on the device, as well as the
4092 * blink duty cycle (specified in milliseconds).
4093 *
4094
4095 * Not all devices will honor all (or even any) of these values.
4096 *
Julia Reynolds529e3322017-02-06 08:33:01 -05004097 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004098 * @see Notification#ledARGB
4099 * @see Notification#ledOnMS
4100 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08004101 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004102 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08004103 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004104 mN.ledARGB = argb;
4105 mN.ledOnMS = onMs;
4106 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004107 if (onMs != 0 || offMs != 0) {
4108 mN.flags |= FLAG_SHOW_LIGHTS;
4109 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004110 return this;
4111 }
4112
Joe Onoratocb109a02011-01-18 17:57:41 -08004113 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004114 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08004115 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004116
4117 * Ongoing notifications cannot be dismissed by the user, so your application or service
4118 * must take care of canceling them.
4119 *
4120
4121 * They are typically used to indicate a background task that the user is actively engaged
4122 * with (e.g., playing music) or is pending in some way and therefore occupying the device
4123 * (e.g., a file download, sync operation, active network connection).
4124 *
4125
4126 * @see Notification#FLAG_ONGOING_EVENT
Joe Onoratocb109a02011-01-18 17:57:41 -08004127 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004128 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08004129 public Builder setOngoing(boolean ongoing) {
4130 setFlag(FLAG_ONGOING_EVENT, ongoing);
4131 return this;
4132 }
4133
Joe Onoratocb109a02011-01-18 17:57:41 -08004134 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08004135 * Set whether this notification should be colorized. When set, the color set with
4136 * {@link #setColor(int)} will be used as the background color of this notification.
4137 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08004138 * This should only be used for high priority ongoing tasks like navigation, an ongoing
4139 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08004140 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07004141 * For most styles, the coloring will only be applied if the notification is for a
4142 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08004143 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07004144 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08004145 *
Aurimas Liutikas7f695332018-05-31 21:07:32 -07004146 * @see #setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08004147 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08004148 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004149 @NonNull
Selim Cinek7b9605b2017-01-19 17:36:00 -08004150 public Builder setColorized(boolean colorize) {
4151 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
4152 return this;
4153 }
4154
4155 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08004156 * Set this flag if you would only like the sound, vibrate
4157 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004158 *
4159 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08004160 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004161 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08004162 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
4163 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
4164 return this;
4165 }
4166
Joe Onoratocb109a02011-01-18 17:57:41 -08004167 /**
Julia Reynolds04499532016-09-13 14:04:53 -04004168 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004169 *
4170 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08004171 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004172 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08004173 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08004174 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08004175 return this;
4176 }
4177
Joe Onoratocb109a02011-01-18 17:57:41 -08004178 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08004179 * Set whether or not this notification should not bridge to other devices.
4180 *
4181 * <p>Some notifications can be bridged to other devices for remote display.
4182 * This hint can be set to recommend this notification not be bridged.
4183 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004184 @NonNull
Griff Hazendfcb0802014-02-11 12:00:00 -08004185 public Builder setLocalOnly(boolean localOnly) {
4186 setFlag(FLAG_LOCAL_ONLY, localOnly);
4187 return this;
4188 }
4189
4190 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004191 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08004192 * <p>
4193 * The value should be one or more of the following fields combined with
4194 * bitwise-or:
4195 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
4196 * <p>
4197 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004198 *
4199 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05004200 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004201 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08004202 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004203 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004204 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004205 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08004206 return this;
4207 }
4208
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004209 /**
4210 * Set the priority of this notification.
4211 *
4212 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004213 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004214 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004215 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07004216 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004217 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004218 return this;
4219 }
Joe Malin8d40d042012-11-05 11:36:40 -08004220
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004221 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04004222 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08004223 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04004224 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004225 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004226 @NonNull
John Spurlockfd7f1e02014-03-18 16:41:57 -04004227 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004228 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004229 return this;
4230 }
4231
4232 /**
Chris Wrendde75302014-03-26 17:24:15 -04004233 * Add a person that is relevant to this notification.
4234 *
Chris Wrene6c48932014-09-29 17:19:27 -04004235 * <P>
4236 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04004237 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4238 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4239 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04004240 * </P>
4241 *
4242 * <P>
4243 * The person should be specified by the {@code String} representation of a
4244 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
4245 * </P>
4246 *
4247 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
4248 * URIs. The path part of these URIs must exist in the contacts database, in the
4249 * appropriate column, or the reference will be discarded as invalid. Telephone schema
4250 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
Selim Cineke7238dd2017-12-14 17:48:32 -08004251 * It is also possible to provide a URI with the schema {@code name:} in order to uniquely
4252 * identify a person without an entry in the contacts database.
Chris Wrene6c48932014-09-29 17:19:27 -04004253 * </P>
4254 *
4255 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04004256 * @see Notification#EXTRA_PEOPLE
Selim Cineke7238dd2017-12-14 17:48:32 -08004257 * @deprecated use {@link #addPerson(Person)}
Chris Wrendde75302014-03-26 17:24:15 -04004258 */
Chris Wrene6c48932014-09-29 17:19:27 -04004259 public Builder addPerson(String uri) {
Selim Cinek9acd6732018-03-23 16:39:02 -07004260 addPerson(new Person.Builder().setUri(uri).build());
Selim Cineke7238dd2017-12-14 17:48:32 -08004261 return this;
4262 }
4263
4264 /**
4265 * Add a person that is relevant to this notification.
4266 *
4267 * <P>
4268 * Depending on user preferences, this annotation may allow the notification to pass
4269 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4270 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4271 * appear more prominently in the user interface.
4272 * </P>
4273 *
4274 * <P>
4275 * A person should usually contain a uri in order to benefit from the ranking boost.
4276 * However, even if no uri is provided, it's beneficial to provide other people in the
4277 * notification, such that listeners and voice only devices can announce and handle them
4278 * properly.
4279 * </P>
4280 *
4281 * @param person the person to add.
4282 * @see Notification#EXTRA_PEOPLE_LIST
4283 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004284 @NonNull
Selim Cineke7238dd2017-12-14 17:48:32 -08004285 public Builder addPerson(Person person) {
4286 mPersonList.add(person);
Chris Wrendde75302014-03-26 17:24:15 -04004287 return this;
4288 }
4289
4290 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004291 * Set this notification to be part of a group of notifications sharing the same key.
4292 * Grouped notifications may display in a cluster or stack on devices which
4293 * support such rendering.
4294 *
4295 * <p>To make this notification the summary for its group, also call
4296 * {@link #setGroupSummary}. A sort order can be specified for group members by using
4297 * {@link #setSortKey}.
4298 * @param groupKey The group key of the group.
4299 * @return this object for method chaining
4300 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004301 @NonNull
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004302 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004303 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004304 return this;
4305 }
4306
4307 /**
4308 * Set this notification to be the group summary for a group of notifications.
4309 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04004310 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
4311 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004312 * @param isGroupSummary Whether this notification should be a group summary.
4313 * @return this object for method chaining
4314 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004315 @NonNull
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004316 public Builder setGroupSummary(boolean isGroupSummary) {
4317 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
4318 return this;
4319 }
4320
4321 /**
4322 * Set a sort key that orders this notification among other notifications from the
4323 * same package. This can be useful if an external sort was already applied and an app
4324 * would like to preserve this. Notifications will be sorted lexicographically using this
4325 * value, although providing different priorities in addition to providing sort key may
4326 * cause this value to be ignored.
4327 *
4328 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07004329 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004330 *
4331 * @see String#compareTo(String)
4332 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004333 @NonNull
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004334 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004335 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004336 return this;
4337 }
4338
4339 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004340 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004341 *
Griff Hazen720042b2014-02-24 15:46:56 -08004342 * <p>Values within the Bundle will replace existing extras values in this Builder.
4343 *
4344 * @see Notification#extras
4345 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004346 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07004347 public Builder addExtras(Bundle extras) {
4348 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004349 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08004350 }
4351 return this;
4352 }
4353
4354 /**
4355 * Set metadata for this notification.
4356 *
4357 * <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 -04004358 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004359 * called.
4360 *
Griff Hazen720042b2014-02-24 15:46:56 -08004361 * <p>Replaces any existing extras values with those from the provided Bundle.
4362 * Use {@link #addExtras} to merge in metadata instead.
4363 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004364 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004365 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004366 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07004367 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004368 if (extras != null) {
4369 mUserExtras = extras;
4370 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004371 return this;
4372 }
4373
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004374 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004375 * Get the current metadata Bundle used by this notification Builder.
4376 *
4377 * <p>The returned Bundle is shared with this Builder.
4378 *
4379 * <p>The current contents of this Bundle are copied into the Notification each time
4380 * {@link #build()} is called.
4381 *
4382 * @see Notification#extras
4383 */
4384 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004385 return mUserExtras;
4386 }
4387
4388 private Bundle getAllExtras() {
4389 final Bundle saveExtras = (Bundle) mUserExtras.clone();
4390 saveExtras.putAll(mN.extras);
4391 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08004392 }
4393
4394 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004395 * Add an action to this notification. Actions are typically displayed by
4396 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004397 * <p>
4398 * Every action must have an icon (32dp square and matching the
4399 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4400 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4401 * <p>
4402 * A notification in its expanded form can display up to 3 actions, from left to right in
4403 * the order they were added. Actions will not be displayed when the notification is
4404 * collapsed, however, so be sure that any essential functions may be accessed by the user
4405 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004406 *
4407 * @param icon Resource ID of a drawable that represents the action.
4408 * @param title Text describing the action.
4409 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04004410 *
4411 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004412 */
Dan Sandler86647982015-05-13 23:41:13 -04004413 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004414 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004415 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004416 return this;
4417 }
4418
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004419 /**
Griff Hazen959591e2014-05-15 22:26:18 -07004420 * Add an action to this notification. Actions are typically displayed by
4421 * the system as a button adjacent to the notification content.
4422 * <p>
4423 * Every action must have an icon (32dp square and matching the
4424 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4425 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4426 * <p>
4427 * A notification in its expanded form can display up to 3 actions, from left to right in
4428 * the order they were added. Actions will not be displayed when the notification is
4429 * collapsed, however, so be sure that any essential functions may be accessed by the user
4430 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
4431 *
4432 * @param action The action to add.
4433 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004434 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07004435 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08004436 if (action != null) {
4437 mActions.add(action);
4438 }
Griff Hazen959591e2014-05-15 22:26:18 -07004439 return this;
4440 }
4441
4442 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004443 * Alter the complete list of actions attached to this notification.
4444 * @see #addAction(Action).
4445 *
4446 * @param actions
4447 * @return
4448 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004449 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004450 public Builder setActions(Action... actions) {
4451 mActions.clear();
4452 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08004453 if (actions[i] != null) {
4454 mActions.add(actions[i]);
4455 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004456 }
4457 return this;
4458 }
4459
4460 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004461 * Add a rich notification style to be applied at build time.
4462 *
4463 * @param style Object responsible for modifying the notification style.
4464 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004465 @NonNull
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004466 public Builder setStyle(Style style) {
4467 if (mStyle != style) {
4468 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004469 if (mStyle != null) {
4470 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004471 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
4472 } else {
4473 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004474 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004475 }
4476 return this;
4477 }
4478
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004479 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05004480 * Returns the style set by {@link #setStyle(Style)}.
4481 */
4482 public Style getStyle() {
4483 return mStyle;
4484 }
4485
4486 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004487 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07004488 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004489 * @return The same Builder.
4490 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004491 @NonNull
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06004492 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004493 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004494 return this;
4495 }
4496
4497 /**
4498 * Supply a replacement Notification whose contents should be shown in insecure contexts
4499 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
4500 * @param n A replacement notification, presumably with some or all info redacted.
4501 * @return The same Builder.
4502 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004503 @NonNull
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004504 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004505 if (n != null) {
4506 mN.publicVersion = new Notification();
4507 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
4508 } else {
4509 mN.publicVersion = null;
4510 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004511 return this;
4512 }
4513
Griff Hazenb720abe2014-05-20 13:15:30 -07004514 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004515 * Apply an extender to this notification builder. Extenders may be used to add
4516 * metadata or change options on this builder.
4517 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004518 @NonNull
Griff Hazen61a9e862014-05-22 16:05:19 -07004519 public Builder extend(Extender extender) {
4520 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004521 return this;
4522 }
4523
Dan Sandler4e787062015-06-17 15:09:48 -04004524 /**
4525 * @hide
4526 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004527 @NonNull
Julia Reynoldse46bb372016-03-17 11:05:58 -04004528 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08004529 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004530 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004531 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004532 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004533 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04004534 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08004535 }
4536
Dan Sandler26e81cf2014-05-06 10:01:27 -04004537 /**
4538 * Sets {@link Notification#color}.
4539 *
4540 * @param argb The accent color to use
4541 *
4542 * @return The same Builder.
4543 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004544 @NonNull
Tor Norbye80756e32015-03-02 09:39:27 -08004545 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004546 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004547 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04004548 return this;
4549 }
4550
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004551 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04004552 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
4553 // This user can never be a badged profile,
4554 // and also includes USER_ALL system notifications.
4555 return null;
4556 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02004557 // Note: This assumes that the current user can read the profile badge of the
4558 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08004559 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05004560 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004561 }
4562
4563 private Bitmap getProfileBadge() {
4564 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01004565 if (badge == null) {
4566 return null;
4567 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004568 final int size = mContext.getResources().getDimensionPixelSize(
4569 R.dimen.notification_badge_size);
4570 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004571 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004572 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004573 badge.draw(canvas);
4574 return bitmap;
4575 }
4576
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004577 private void bindProfileBadge(RemoteViews contentView, StandardTemplateParams p) {
Kenny Guy98193ea2014-07-24 19:54:37 +01004578 Bitmap profileBadge = getProfileBadge();
4579
Kenny Guy98193ea2014-07-24 19:54:37 +01004580 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004581 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
4582 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004583 if (isColorized(p)) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004584 contentView.setDrawableTint(R.id.profile_badge, false,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004585 getPrimaryTextColor(p), PorterDuff.Mode.SRC_ATOP);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004586 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004587 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004588 }
4589
Gus Prevasbae5ba32018-11-28 15:45:13 -05004590 private void bindAlertedIcon(RemoteViews contentView, StandardTemplateParams p) {
4591 contentView.setDrawableTint(
4592 R.id.alerted_icon,
4593 false /* targetBackground */,
4594 getNeutralColor(p),
4595 PorterDuff.Mode.SRC_ATOP);
4596 }
4597
Julia Reynoldsfc640012018-02-21 12:25:27 -05004598 /**
4599 * @hide
4600 */
4601 public boolean usesStandardHeader() {
4602 if (mN.mUsesStandardHeader) {
4603 return true;
4604 }
4605 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.N) {
4606 if (mN.contentView == null && mN.bigContentView == null) {
4607 return true;
4608 }
4609 }
4610 boolean contentViewUsesHeader = mN.contentView == null
4611 || STANDARD_LAYOUTS.contains(mN.contentView.getLayoutId());
4612 boolean bigContentViewUsesHeader = mN.bigContentView == null
4613 || STANDARD_LAYOUTS.contains(mN.bigContentView.getLayoutId());
4614 return contentViewUsesHeader && bigContentViewUsesHeader;
4615 }
4616
Christoph Studerfe718432014-09-01 18:21:18 +02004617 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004618 resetNotificationHeader(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02004619 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08004620 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004621 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08004622 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004623 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08004624 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08004625 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004626 }
4627
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004628 /**
4629 * Resets the notification header to its original state
4630 */
4631 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07004632 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
4633 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08004634 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004635 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02004636 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004637 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07004638 contentView.setTextViewText(R.id.header_text, null);
Selim Cinekafeed292017-12-12 17:32:44 -08004639 contentView.setViewVisibility(R.id.header_text_secondary, View.GONE);
4640 contentView.setTextViewText(R.id.header_text_secondary, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004641 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinekafeed292017-12-12 17:32:44 -08004642 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004643 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004644 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004645 contentView.setImageViewIcon(R.id.profile_badge, null);
4646 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Gus Prevasa3226492018-10-23 11:10:09 -04004647 contentView.setViewVisibility(R.id.alerted_icon, View.GONE);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004648 mN.mUsesStandardHeader = false;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004649 }
4650
Selim Cinek384804b2018-04-18 14:31:07 +08004651 private RemoteViews applyStandardTemplate(int resId, TemplateBindResult result) {
4652 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this),
4653 result);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004654 }
4655
Selim Cinek384804b2018-04-18 14:31:07 +08004656 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p,
4657 TemplateBindResult result) {
Kenny Guy77320062014-08-27 21:37:15 +01004658 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04004659
Christoph Studerfe718432014-09-01 18:21:18 +02004660 resetStandardTemplate(contentView);
4661
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004662 final Bundle ex = mN.extras;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004663 updateBackgroundColor(contentView, p);
4664 bindNotificationHeader(contentView, p);
Selim Cinek384804b2018-04-18 14:31:07 +08004665 bindLargeIconAndReply(contentView, p, result);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004666 boolean showProgress = handleProgressBar(contentView, ex, p);
Adrian Roos70d7aa32017-01-11 15:39:06 -08004667 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08004668 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004669 contentView.setTextViewText(R.id.title, processTextSpans(p.title));
Lucas Dupin00be88f2019-01-03 17:50:52 -08004670 setTextViewColorPrimary(contentView, R.id.title, p);
Selim Cinek954cc232016-05-20 13:29:23 -07004671 contentView.setViewLayoutWidth(R.id.title, showProgress
4672 ? ViewGroup.LayoutParams.WRAP_CONTENT
4673 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08004674 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08004675 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08004676 int textId = showProgress ? com.android.internal.R.id.text_line_1
4677 : com.android.internal.R.id.text;
Selim Cinek48f66b72017-08-18 16:17:51 -07004678 contentView.setTextViewText(textId, processTextSpans(p.text));
Lucas Dupin00be88f2019-01-03 17:50:52 -08004679 setTextViewColorSecondary(contentView, textId, p);
Selim Cinek41598732016-01-11 16:58:37 -08004680 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08004681 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08004682
Selim Cinek279fa862016-06-14 10:57:25 -07004683 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004684
Selim Cinek29603462015-11-17 19:04:39 -08004685 return contentView;
4686 }
4687
Selim Cinek48f66b72017-08-18 16:17:51 -07004688 private CharSequence processTextSpans(CharSequence text) {
Lucas Dupind3c99322018-09-27 10:22:29 -07004689 if (hasForegroundColor() || mInNightMode) {
Lucas Dupina291d192018-06-07 13:59:42 -07004690 return ContrastColorUtil.clearColorSpans(text);
Selim Cinek48f66b72017-08-18 16:17:51 -07004691 }
4692 return text;
4693 }
4694
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004695 private void setTextViewColorPrimary(RemoteViews contentView, int id,
4696 StandardTemplateParams p) {
4697 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004698 contentView.setTextColor(id, mPrimaryTextColor);
4699 }
4700
Selim Cinek48f66b72017-08-18 16:17:51 -07004701 private boolean hasForegroundColor() {
4702 return mForegroundColor != COLOR_INVALID;
4703 }
4704
Selim Cinek389edcd2017-05-11 19:16:44 -07004705 /**
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004706 * Return the primary text color using the existing template params
Selim Cinek389edcd2017-05-11 19:16:44 -07004707 * @hide
4708 */
4709 @VisibleForTesting
4710 public int getPrimaryTextColor() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004711 return getPrimaryTextColor(mParams);
4712 }
4713
4714 /**
4715 * @param p the template params to inflate this with
4716 * @return the primary text color
4717 * @hide
4718 */
4719 @VisibleForTesting
4720 public int getPrimaryTextColor(StandardTemplateParams p) {
4721 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004722 return mPrimaryTextColor;
4723 }
4724
Selim Cinek389edcd2017-05-11 19:16:44 -07004725 /**
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004726 * Return the secondary text color using the existing template params
Selim Cinek389edcd2017-05-11 19:16:44 -07004727 * @hide
4728 */
4729 @VisibleForTesting
4730 public int getSecondaryTextColor() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004731 return getSecondaryTextColor(mParams);
4732 }
4733
4734 /**
4735 * @param p the template params to inflate this with
4736 * @return the secondary text color
4737 * @hide
4738 */
4739 @VisibleForTesting
4740 public int getSecondaryTextColor(StandardTemplateParams p) {
4741 ensureColors(p);
Selim Cinek389edcd2017-05-11 19:16:44 -07004742 return mSecondaryTextColor;
4743 }
4744
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004745 private void setTextViewColorSecondary(RemoteViews contentView, int id,
4746 StandardTemplateParams p) {
4747 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004748 contentView.setTextColor(id, mSecondaryTextColor);
4749 }
4750
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004751 private void ensureColors(StandardTemplateParams p) {
4752 int backgroundColor = getBackgroundColor(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004753 if (mPrimaryTextColor == COLOR_INVALID
4754 || mSecondaryTextColor == COLOR_INVALID
Selim Cinek7b9605b2017-01-19 17:36:00 -08004755 || mTextColorsAreForBackground != backgroundColor) {
4756 mTextColorsAreForBackground = backgroundColor;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004757 if (!hasForegroundColor() || !isColorized(p)) {
Lucas Dupina291d192018-06-07 13:59:42 -07004758 mPrimaryTextColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004759 backgroundColor, mInNightMode);
Lucas Dupina291d192018-06-07 13:59:42 -07004760 mSecondaryTextColor = ContrastColorUtil.resolveSecondaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004761 backgroundColor, mInNightMode);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004762 if (backgroundColor != COLOR_DEFAULT && isColorized(p)) {
Lucas Dupina291d192018-06-07 13:59:42 -07004763 mPrimaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004764 mPrimaryTextColor, backgroundColor, 4.5);
Lucas Dupina291d192018-06-07 13:59:42 -07004765 mSecondaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004766 mSecondaryTextColor, backgroundColor, 4.5);
4767 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07004768 } else {
Lucas Dupina291d192018-06-07 13:59:42 -07004769 double backLum = ContrastColorUtil.calculateLuminance(backgroundColor);
4770 double textLum = ContrastColorUtil.calculateLuminance(mForegroundColor);
4771 double contrast = ContrastColorUtil.calculateContrast(mForegroundColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004772 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07004773 // We only respect the given colors if worst case Black or White still has
4774 // contrast
4775 boolean backgroundLight = backLum > textLum
4776 && satisfiesTextContrast(backgroundColor, Color.BLACK)
4777 || backLum <= textLum
4778 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004779 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07004780 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004781 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004782 mForegroundColor,
4783 backgroundColor,
4784 true /* findFG */,
4785 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004786 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004787 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004788 } else {
4789 mSecondaryTextColor =
Lucas Dupina291d192018-06-07 13:59:42 -07004790 ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004791 mForegroundColor,
4792 backgroundColor,
4793 true /* findFG */,
4794 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004795 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004796 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004797 }
4798 } else {
4799 mPrimaryTextColor = mForegroundColor;
Lucas Dupina291d192018-06-07 13:59:42 -07004800 mSecondaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004801 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4802 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Lucas Dupina291d192018-06-07 13:59:42 -07004803 if (ContrastColorUtil.calculateContrast(mSecondaryTextColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004804 backgroundColor) < 4.5f) {
4805 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07004806 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004807 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004808 mSecondaryTextColor,
4809 backgroundColor,
4810 true /* findFG */,
4811 4.5f);
4812 } else {
4813 mSecondaryTextColor
Lucas Dupina291d192018-06-07 13:59:42 -07004814 = ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004815 mSecondaryTextColor,
4816 backgroundColor,
4817 true /* findFG */,
4818 4.5f);
4819 }
Lucas Dupina291d192018-06-07 13:59:42 -07004820 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004821 mSecondaryTextColor, backgroundLight
4822 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4823 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004824 }
4825 }
4826 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004827 }
4828 }
4829
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004830 private void updateBackgroundColor(RemoteViews contentView,
4831 StandardTemplateParams p) {
4832 if (isColorized(p)) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004833 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004834 getBackgroundColor(p));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004835 } else {
4836 // Clear it!
4837 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4838 0);
4839 }
4840 }
4841
Selim Cinek860b6da2015-12-16 19:02:19 -08004842 /**
4843 * @param remoteView the remote view to update the minheight in
4844 * @param hasMinHeight does it have a mimHeight
4845 * @hide
4846 */
4847 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4848 int minHeight = 0;
4849 if (hasMinHeight) {
4850 // we need to set the minHeight of the notification
4851 minHeight = mContext.getResources().getDimensionPixelSize(
4852 com.android.internal.R.dimen.notification_min_content_height);
4853 }
4854 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4855 }
4856
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004857 private boolean handleProgressBar(RemoteViews contentView, Bundle ex,
4858 StandardTemplateParams p) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004859 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4860 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4861 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004862 if (p.hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004863 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004864 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004865 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004866 contentView.setProgressBackgroundTintList(
4867 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4868 R.color.notification_progress_background_color)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004869 if (getRawColor(p) != COLOR_DEFAULT) {
4870 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor(p));
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004871 contentView.setProgressTintList(R.id.progress, colorStateList);
4872 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004873 }
Selim Cinek29603462015-11-17 19:04:39 -08004874 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004875 } else {
4876 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004877 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004878 }
Joe Onorato561d3852010-11-20 18:09:34 -08004879 }
4880
Selim Cinek384804b2018-04-18 14:31:07 +08004881 private void bindLargeIconAndReply(RemoteViews contentView, StandardTemplateParams p,
4882 TemplateBindResult result) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004883 boolean largeIconShown = bindLargeIcon(contentView, p);
4884 boolean replyIconShown = bindReplyIcon(contentView, p);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004885 boolean iconContainerVisible = largeIconShown || replyIconShown;
Selim Cinek384804b2018-04-18 14:31:07 +08004886 contentView.setViewVisibility(R.id.right_icon_container,
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004887 iconContainerVisible ? View.VISIBLE : View.GONE);
Selim Cinek1c72fa02018-04-23 18:00:54 +08004888 int marginEnd = calculateMarginEnd(largeIconShown, replyIconShown);
4889 contentView.setViewLayoutMarginEnd(R.id.line1, marginEnd);
4890 contentView.setViewLayoutMarginEnd(R.id.text, marginEnd);
4891 contentView.setViewLayoutMarginEnd(R.id.progress, marginEnd);
Selim Cinek384804b2018-04-18 14:31:07 +08004892 if (result != null) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08004893 result.setIconMarginEnd(marginEnd);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004894 result.setRightIconContainerVisible(iconContainerVisible);
Selim Cinek384804b2018-04-18 14:31:07 +08004895 }
4896 }
4897
Selim Cinek1c72fa02018-04-23 18:00:54 +08004898 private int calculateMarginEnd(boolean largeIconShown, boolean replyIconShown) {
4899 int marginEnd = 0;
4900 int contentMargin = mContext.getResources().getDimensionPixelSize(
4901 R.dimen.notification_content_margin_end);
4902 int iconSize = mContext.getResources().getDimensionPixelSize(
4903 R.dimen.notification_right_icon_size);
4904 if (replyIconShown) {
4905 // The size of the reply icon
4906 marginEnd += iconSize;
4907
4908 int replyInset = mContext.getResources().getDimensionPixelSize(
4909 R.dimen.notification_reply_inset);
4910 // We're subtracting the inset of the reply icon to make sure it's
4911 // aligned nicely on the right, and remove it from the following padding
4912 marginEnd -= replyInset * 2;
4913 }
4914 if (largeIconShown) {
4915 // adding size of the right icon
4916 marginEnd += iconSize;
4917
4918 if (replyIconShown) {
4919 // We also add some padding to the reply icon if it's around
4920 marginEnd += contentMargin;
4921 }
4922 }
4923 if (replyIconShown || largeIconShown) {
4924 // The padding to the content
4925 marginEnd += contentMargin;
4926 }
4927 return marginEnd;
4928 }
4929
Selim Cinek384804b2018-04-18 14:31:07 +08004930 /**
4931 * Bind the large icon.
4932 * @return if the largeIcon is visible
4933 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004934 private boolean bindLargeIcon(RemoteViews contentView, StandardTemplateParams p) {
Selim Cinek279fa862016-06-14 10:57:25 -07004935 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4936 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4937 }
Lucas Dupin00be88f2019-01-03 17:50:52 -08004938 boolean showLargeIcon = mN.mLargeIcon != null && !p.hideLargeIcon;
Selim Cinek88188f22017-09-19 16:46:56 -07004939 if (showLargeIcon) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004940 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4941 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004942 processLargeLegacyIcon(mN.mLargeIcon, contentView, p);
Selim Cinek88188f22017-09-19 16:46:56 -07004943 }
Selim Cinek384804b2018-04-18 14:31:07 +08004944 return showLargeIcon;
4945 }
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004946
Selim Cinek384804b2018-04-18 14:31:07 +08004947 /**
4948 * Bind the reply icon.
4949 * @return if the reply icon is visible
4950 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004951 private boolean bindReplyIcon(RemoteViews contentView, StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08004952 boolean actionVisible = !p.hideReplyIcon;
Selim Cinek384804b2018-04-18 14:31:07 +08004953 Action action = null;
Selim Cinek88188f22017-09-19 16:46:56 -07004954 if (actionVisible) {
Selim Cinek384804b2018-04-18 14:31:07 +08004955 action = findReplyAction();
4956 actionVisible = action != null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004957 }
Selim Cinek384804b2018-04-18 14:31:07 +08004958 if (actionVisible) {
4959 contentView.setViewVisibility(R.id.reply_icon_action, View.VISIBLE);
4960 contentView.setDrawableTint(R.id.reply_icon_action,
4961 false /* targetBackground */,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004962 getNeutralColor(p),
Selim Cinek384804b2018-04-18 14:31:07 +08004963 PorterDuff.Mode.SRC_ATOP);
4964 contentView.setOnClickPendingIntent(R.id.reply_icon_action, action.actionIntent);
4965 contentView.setRemoteInputs(R.id.reply_icon_action, action.mRemoteInputs);
4966 } else {
4967 contentView.setRemoteInputs(R.id.reply_icon_action, null);
4968 }
4969 contentView.setViewVisibility(R.id.reply_icon_action,
4970 actionVisible ? View.VISIBLE : View.GONE);
4971 return actionVisible;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004972 }
4973
4974 private Action findReplyAction() {
4975 ArrayList<Action> actions = mActions;
4976 if (mOriginalActions != null) {
4977 actions = mOriginalActions;
4978 }
4979 int numActions = actions.size();
4980 for (int i = 0; i < numActions; i++) {
4981 Action action = actions.get(i);
4982 if (hasValidRemoteInput(action)) {
4983 return action;
4984 }
4985 }
4986 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004987 }
4988
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004989 private void bindNotificationHeader(RemoteViews contentView, StandardTemplateParams p) {
4990 bindSmallIcon(contentView, p);
4991 bindHeaderAppName(contentView, p);
Lucas Dupin00be88f2019-01-03 17:50:52 -08004992 bindHeaderText(contentView, p);
4993 bindHeaderTextSecondary(contentView, p);
4994 bindHeaderChronometerAndTime(contentView, p);
4995 bindProfileBadge(contentView, p);
4996 bindAlertedIcon(contentView, p);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004997 bindActivePermissions(contentView, p);
4998 bindExpandButton(contentView, p);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004999 mN.mUsesStandardHeader = true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005000 }
5001
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005002 private void bindActivePermissions(RemoteViews contentView, StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005003 int color = getNeutralColor(p);
Julia Reynoldsb887b232018-04-10 16:38:08 -04005004 contentView.setDrawableTint(R.id.camera, false, color, PorterDuff.Mode.SRC_ATOP);
5005 contentView.setDrawableTint(R.id.mic, false, color, PorterDuff.Mode.SRC_ATOP);
5006 contentView.setDrawableTint(R.id.overlay, false, color, PorterDuff.Mode.SRC_ATOP);
5007 }
5008
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005009 private void bindExpandButton(RemoteViews contentView, StandardTemplateParams p) {
5010 int color = isColorized(p) ? getPrimaryTextColor(p) : getSecondaryTextColor(p);
Sunny Goyal5b153922017-09-21 21:00:36 -07005011 contentView.setDrawableTint(R.id.expand_button, false, color,
5012 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005013 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08005014 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005015 }
5016
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005017 private void bindHeaderChronometerAndTime(RemoteViews contentView,
5018 StandardTemplateParams p) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005019 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08005020 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005021 setTextViewColorSecondary(contentView, R.id.time_divider, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005022 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
5023 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
5024 contentView.setLong(R.id.chronometer, "setBase",
5025 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
5026 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07005027 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07005028 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005029 setTextViewColorSecondary(contentView, R.id.chronometer, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005030 } else {
5031 contentView.setViewVisibility(R.id.time, View.VISIBLE);
5032 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005033 setTextViewColorSecondary(contentView, R.id.time, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005034 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005035 } else {
5036 // We still want a time to be set but gone, such that we can show and hide it
5037 // on demand in case it's a child notification without anything in the header
5038 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005039 }
5040 }
5041
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005042 private void bindHeaderText(RemoteViews contentView, StandardTemplateParams p) {
5043 CharSequence summaryText = p.summaryText;
5044 if (summaryText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05005045 && mStyle.hasSummaryInHeader()) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005046 summaryText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05005047 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005048 if (summaryText == null
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07005049 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
5050 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005051 summaryText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07005052 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005053 if (summaryText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005054 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek48f66b72017-08-18 16:17:51 -07005055 contentView.setTextViewText(R.id.header_text, processTextSpans(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005056 processLegacyText(summaryText)));
5057 setTextViewColorSecondary(contentView, R.id.header_text, p);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07005058 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
5059 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005060 setTextViewColorSecondary(contentView, R.id.header_text_divider, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005061 }
5062 }
5063
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005064 private void bindHeaderTextSecondary(RemoteViews contentView, StandardTemplateParams p) {
5065 if (!TextUtils.isEmpty(p.headerTextSecondary)) {
Selim Cinekafeed292017-12-12 17:32:44 -08005066 contentView.setTextViewText(R.id.header_text_secondary, processTextSpans(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005067 processLegacyText(p.headerTextSecondary)));
5068 setTextViewColorSecondary(contentView, R.id.header_text_secondary, p);
Selim Cinekafeed292017-12-12 17:32:44 -08005069 contentView.setViewVisibility(R.id.header_text_secondary, View.VISIBLE);
5070 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005071 setTextViewColorSecondary(contentView, R.id.header_text_secondary_divider, p);
Selim Cinekafeed292017-12-12 17:32:44 -08005072 }
5073 }
5074
Adrian Rooseba05822016-04-22 17:09:27 -07005075 /**
5076 * @hide
5077 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005078 @UnsupportedAppUsage
Adrian Rooseba05822016-04-22 17:09:27 -07005079 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04005080 CharSequence name = null;
5081 final PackageManager pm = mContext.getPackageManager();
5082 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
5083 // only system packages which lump together a bunch of unrelated stuff
5084 // may substitute a different name to make the purpose of the
5085 // notification more clear. the correct package label should always
5086 // be accessible via SystemUI.
5087 final String pkg = mContext.getPackageName();
5088 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
5089 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
5090 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
5091 name = subName;
5092 } else {
5093 Log.w(TAG, "warning: pkg "
5094 + pkg + " attempting to substitute app name '" + subName
5095 + "' without holding perm "
5096 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
5097 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005098 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04005099 if (TextUtils.isEmpty(name)) {
5100 name = pm.getApplicationLabel(mContext.getApplicationInfo());
5101 }
5102 if (TextUtils.isEmpty(name)) {
5103 // still nothing?
5104 return null;
5105 }
5106
5107 return String.valueOf(name);
5108 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005109 private void bindHeaderAppName(RemoteViews contentView, StandardTemplateParams p) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04005110 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005111 if (isColorized(p)) {
5112 setTextViewColorPrimary(contentView, R.id.app_name_text, p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005113 } else {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005114 contentView.setTextColor(R.id.app_name_text, getSecondaryTextColor(p));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005115 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005116 }
5117
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005118 private boolean isColorized(StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005119 return p.allowColorization && mN.isColorized();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005120 }
5121
5122 private void bindSmallIcon(RemoteViews contentView, StandardTemplateParams p) {
Selim Cinek279fa862016-06-14 10:57:25 -07005123 if (mN.mSmallIcon == null && mN.icon != 0) {
5124 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
5125 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005126 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Sunny Goyal5b153922017-09-21 21:00:36 -07005127 contentView.setInt(R.id.icon, "setImageLevel", mN.iconLevel);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005128 processSmallIconColor(mN.mSmallIcon, contentView, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005129 }
5130
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005131 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005132 * @return true if the built notification will show the time or the chronometer; false
5133 * otherwise
5134 */
5135 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07005136 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005137 }
5138
Christoph Studerfe718432014-09-01 18:21:18 +02005139 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07005140 // actions_container is only reset when there are no actions to avoid focus issues with
5141 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02005142 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02005143 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08005144
5145 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
5146 big.setTextViewText(R.id.notification_material_reply_text_1, null);
Kenny Guya0f6de82018-04-06 16:20:16 +01005147 big.setViewVisibility(R.id.notification_material_reply_text_1_container, View.GONE);
5148 big.setViewVisibility(R.id.notification_material_reply_progress, View.GONE);
Adrian Roose458aa82015-12-08 16:17:19 -08005149
5150 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
5151 big.setTextViewText(R.id.notification_material_reply_text_2, null);
5152 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
5153 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07005154
Selim Cineked64a142018-02-06 18:06:01 -08005155 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
5156 R.dimen.notification_content_margin);
Christoph Studerfe718432014-09-01 18:21:18 +02005157 }
5158
Selim Cinek384804b2018-04-18 14:31:07 +08005159 private RemoteViews applyStandardTemplateWithActions(int layoutId,
5160 TemplateBindResult result) {
5161 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this),
5162 result);
Adrian Roos48d746a2016-04-12 14:57:28 -07005163 }
5164
Gustav Sennton1463d832018-11-06 16:12:48 +00005165 private static List<Notification.Action> filterOutContextualActions(
5166 List<Notification.Action> actions) {
5167 List<Notification.Action> nonContextualActions = new ArrayList<>();
5168 for (Notification.Action action : actions) {
Gustav Sennton005d7a02019-01-04 13:41:32 +00005169 if (!action.isContextual()) {
Gustav Sennton1463d832018-11-06 16:12:48 +00005170 nonContextualActions.add(action);
5171 }
5172 }
5173 return nonContextualActions;
5174 }
5175
Adrian Roos70d7aa32017-01-11 15:39:06 -08005176 private RemoteViews applyStandardTemplateWithActions(int layoutId,
Selim Cinek384804b2018-04-18 14:31:07 +08005177 StandardTemplateParams p, TemplateBindResult result) {
5178 RemoteViews big = applyStandardTemplate(layoutId, p, result);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005179
Christoph Studerfe718432014-09-01 18:21:18 +02005180 resetStandardTemplateWithActions(big);
5181
Adrian Roose458aa82015-12-08 16:17:19 -08005182 boolean validRemoteInput = false;
5183
Gustav Sennton1463d832018-11-06 16:12:48 +00005184 // In the UI contextual actions appear separately from the standard actions, so we
5185 // filter them out here.
5186 List<Notification.Action> nonContextualActions = filterOutContextualActions(mActions);
5187
5188 int N = nonContextualActions.size();
Lucas Dupin00be88f2019-01-03 17:50:52 -08005189 boolean emphazisedMode = mN.fullScreenIntent != null;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005190 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005191 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08005192 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005193 big.setViewVisibility(R.id.actions, View.VISIBLE);
Selim Cineked64a142018-02-06 18:06:01 -08005194 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Daniel Sandler8680bf82012-05-15 16:52:52 -04005195 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005196 for (int i=0; i<N; i++) {
Gustav Sennton1463d832018-11-06 16:12:48 +00005197 Action action = nonContextualActions.get(i);
5198
Selim Cinekffcc7cf2018-02-06 17:43:51 -08005199 boolean actionHasValidInput = hasValidRemoteInput(action);
5200 validRemoteInput |= actionHasValidInput;
Adrian Roose458aa82015-12-08 16:17:19 -08005201
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005202 final RemoteViews button = generateActionButton(action, emphazisedMode, p);
Selim Cinek396caca2018-04-10 17:46:46 -07005203 if (actionHasValidInput && !emphazisedMode) {
Selim Cinekffcc7cf2018-02-06 17:43:51 -08005204 // Clear the drawable
5205 button.setInt(R.id.action0, "setBackgroundResource", 0);
5206 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005207 big.addView(R.id.actions, button);
5208 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07005209 } else {
5210 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005211 }
Adrian Roose458aa82015-12-08 16:17:19 -08005212
5213 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Lucas Dupin00be88f2019-01-03 17:50:52 -08005214 if (validRemoteInput && replyText != null
Selim Cinekbee4e072018-05-21 22:06:43 -07005215 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])
5216 && p.maxRemoteInputHistory > 0) {
Kenny Guya0f6de82018-04-06 16:20:16 +01005217 boolean showSpinner = mN.extras.getBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER);
Adrian Roose458aa82015-12-08 16:17:19 -08005218 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Kenny Guya0f6de82018-04-06 16:20:16 +01005219 big.setViewVisibility(R.id.notification_material_reply_text_1_container,
5220 View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005221 big.setTextViewText(R.id.notification_material_reply_text_1,
5222 processTextSpans(replyText[0]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005223 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1, p);
Kenny Guya0f6de82018-04-06 16:20:16 +01005224 big.setViewVisibility(R.id.notification_material_reply_progress,
5225 showSpinner ? View.VISIBLE : View.GONE);
5226 big.setProgressIndeterminateTintList(
5227 R.id.notification_material_reply_progress,
5228 ColorStateList.valueOf(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005229 isColorized(p) ? getPrimaryTextColor(p) : resolveContrastColor(p)));
Adrian Roose458aa82015-12-08 16:17:19 -08005230
Selim Cinekbee4e072018-05-21 22:06:43 -07005231 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])
5232 && p.maxRemoteInputHistory > 1) {
Adrian Roose458aa82015-12-08 16:17:19 -08005233 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005234 big.setTextViewText(R.id.notification_material_reply_text_2,
5235 processTextSpans(replyText[1]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005236 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2, p);
Adrian Roose458aa82015-12-08 16:17:19 -08005237
Selim Cinekbee4e072018-05-21 22:06:43 -07005238 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])
5239 && p.maxRemoteInputHistory > 2) {
Adrian Roose458aa82015-12-08 16:17:19 -08005240 big.setViewVisibility(
5241 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005242 big.setTextViewText(R.id.notification_material_reply_text_3,
5243 processTextSpans(replyText[2]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005244 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3, p);
Adrian Roose458aa82015-12-08 16:17:19 -08005245 }
5246 }
5247 }
5248
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005249 return big;
5250 }
5251
Adrian Roose458aa82015-12-08 16:17:19 -08005252 private boolean hasValidRemoteInput(Action action) {
5253 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
5254 // Weird actions
5255 return false;
5256 }
5257
5258 RemoteInput[] remoteInputs = action.getRemoteInputs();
5259 if (remoteInputs == null) {
5260 return false;
5261 }
5262
5263 for (RemoteInput r : remoteInputs) {
5264 CharSequence[] choices = r.getChoices();
5265 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
5266 return true;
5267 }
5268 }
5269 return false;
5270 }
5271
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005272 /**
5273 * Construct a RemoteViews for the final 1U notification layout. In order:
5274 * 1. Custom contentView from the caller
5275 * 2. Style's proposed content view
5276 * 3. Standard template view
5277 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005278 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005279 return createContentView(false /* increasedheight */ );
5280 }
5281
5282 /**
5283 * Construct a RemoteViews for the smaller content view.
5284 *
5285 * @param increasedHeight true if this layout be created with an increased height. Some
5286 * styles may support showing more then just that basic 1U size
5287 * and the system may decide to render important notifications
5288 * slightly bigger even when collapsed.
5289 *
5290 * @hide
5291 */
5292 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005293 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005294 return mN.contentView;
5295 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005296 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005297 if (styleView != null) {
5298 return styleView;
5299 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005300 }
Selim Cinek384804b2018-04-18 14:31:07 +08005301 return applyStandardTemplate(getBaseLayoutResource(), null /* result */);
Joe Onorato46439ce2010-11-19 13:56:21 -08005302 }
5303
Selim Cineka7679b62017-05-10 16:33:25 -07005304 private boolean useExistingRemoteView() {
5305 return mStyle == null || (!mStyle.displayCustomViewInline()
5306 && !mRebuildStyledRemoteViews);
5307 }
5308
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005309 /**
5310 * Construct a RemoteViews for the final big notification layout.
5311 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005312 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05005313 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07005314 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005315 return mN.bigContentView;
5316 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05005317 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08005318 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005319 } else if (mActions.size() != 0) {
Selim Cinek384804b2018-04-18 14:31:07 +08005320 result = applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5321 null /* result */);
Selim Cinek850a8542015-11-11 11:48:36 -05005322 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08005323 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05005324 return result;
5325 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005326
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005327 /**
Selim Cinek414ad332017-02-24 19:06:12 -08005328 * Construct a RemoteViews for the final notification header only. This will not be
5329 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005330 *
5331 * @hide
5332 */
Lucas Dupin00be88f2019-01-03 17:50:52 -08005333 public RemoteViews makeNotificationHeader() {
5334 return makeNotificationHeader(mParams.reset().fillTextsFrom(this));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005335 }
5336
5337 /**
5338 * Construct a RemoteViews for the final notification header only. This will not be
5339 * colorized.
5340 *
5341 * @param p the template params to inflate this with
5342 */
5343 private RemoteViews makeNotificationHeader(StandardTemplateParams p) {
5344 // Headers on their own are never colorized
5345 p.disallowColorization();
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005346 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Lucas Dupin00be88f2019-01-03 17:50:52 -08005347 R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005348 resetNotificationHeader(header);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005349 bindNotificationHeader(header, p);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005350 return header;
5351 }
5352
Adrian Roos487374f2017-01-11 15:48:14 -08005353 /**
5354 * Construct a RemoteViews for the ambient version of the notification.
5355 *
5356 * @hide
5357 */
5358 public RemoteViews makeAmbientNotification() {
Selim Cinek0e069942019-01-24 16:09:41 -08005359 RemoteViews headsUpContentView = createHeadsUpContentView(false /* increasedHeight */);
5360 if (headsUpContentView != null) {
5361 return headsUpContentView;
5362 }
5363 return createContentView();
Adrian Roos487374f2017-01-11 15:48:14 -08005364 }
5365
Selim Cinek29603462015-11-17 19:04:39 -08005366 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005367 if (result != null) {
5368 result.setViewVisibility(R.id.text_line_1, View.GONE);
5369 }
Selim Cinek29603462015-11-17 19:04:39 -08005370 }
5371
Selim Cinek6743c0b2017-01-18 18:24:01 -08005372 /**
5373 * Adapt the Notification header if this view is used as an expanded view.
5374 *
5375 * @hide
5376 */
5377 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005378 if (result != null) {
5379 result.setBoolean(R.id.notification_header, "setExpanded", true);
5380 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005381 }
5382
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005383 /**
5384 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005385 *
5386 * @param increasedHeight true if this layout be created with an increased height. Some
5387 * styles may support showing more then just that basic 1U size
5388 * and the system may decide to render important notifications
5389 * slightly bigger even when collapsed.
5390 *
5391 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005392 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005393 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005394 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005395 return mN.headsUpContentView;
5396 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08005397 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
5398 if (styleView != null) {
5399 return styleView;
5400 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05005401 } else if (mActions.size() == 0) {
5402 return null;
5403 }
5404
Selim Cinekbee4e072018-05-21 22:06:43 -07005405 // We only want at most a single remote input history to be shown here, otherwise
5406 // the content would become squished.
5407 StandardTemplateParams p = mParams.reset().fillTextsFrom(this)
5408 .setMaxRemoteInputHistory(1);
5409 return applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5410 p,
5411 null /* result */);
Chris Wren8fd39ec2014-02-27 17:43:26 -05005412 }
5413
Selim Cinek624c02db2015-12-14 21:00:02 -08005414 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08005415 * Construct a RemoteViews for the final heads-up notification layout.
5416 */
5417 public RemoteViews createHeadsUpContentView() {
5418 return createHeadsUpContentView(false /* useIncreasedHeight */);
5419 }
5420
5421 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08005422 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5423 *
5424 * @hide
5425 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005426 @UnsupportedAppUsage
Selim Cinek624c02db2015-12-14 21:00:02 -08005427 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005428 return makePublicView(false /* ambient */);
5429 }
5430
5431 /**
5432 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5433 *
5434 * @hide
5435 */
5436 public RemoteViews makePublicAmbientNotification() {
5437 return makePublicView(true /* ambient */);
5438 }
5439
5440 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08005441 if (mN.publicVersion != null) {
5442 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005443 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08005444 }
5445 Bundle savedBundle = mN.extras;
5446 Style style = mStyle;
5447 mStyle = null;
5448 Icon largeIcon = mN.mLargeIcon;
5449 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07005450 Bitmap largeIconLegacy = mN.largeIcon;
5451 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005452 ArrayList<Action> actions = mActions;
5453 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08005454 Bundle publicExtras = new Bundle();
5455 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
5456 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
5457 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
5458 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07005459 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
5460 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cineked07b962018-04-30 14:39:35 -07005461 String appName = savedBundle.getString(EXTRA_SUBSTITUTE_APP_NAME);
5462 if (appName != null) {
5463 publicExtras.putString(EXTRA_SUBSTITUTE_APP_NAME, appName);
5464 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005465 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07005466 RemoteViews view;
Lucas Dupin00be88f2019-01-03 17:50:52 -08005467 view = makeNotificationHeader();
5468 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
Selim Cinek624c02db2015-12-14 21:00:02 -08005469 mN.extras = savedBundle;
5470 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07005471 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005472 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08005473 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005474 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08005475 }
5476
Selim Cinek6743c0b2017-01-18 18:24:01 -08005477 /**
5478 * Construct a content view for the display when low - priority
5479 *
5480 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
5481 * a new subtext is created consisting of the content of the
5482 * notification.
5483 * @hide
5484 */
5485 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005486 StandardTemplateParams p = mParams.reset()
5487 .forceDefaultColor()
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005488 .fillTextsFrom(this);
5489 if (!useRegularSubtext || TextUtils.isEmpty(mParams.summaryText)) {
5490 p.summaryText(createSummaryText());
Selim Cinek6743c0b2017-01-18 18:24:01 -08005491 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005492 RemoteViews header = makeNotificationHeader(p);
Selim Cinek1b554392017-02-28 17:22:49 -08005493 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08005494 return header;
5495 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005496
Selim Cinek6743c0b2017-01-18 18:24:01 -08005497 private CharSequence createSummaryText() {
5498 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
5499 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
5500 return titleText;
5501 }
5502 SpannableStringBuilder summary = new SpannableStringBuilder();
5503 if (titleText == null) {
5504 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
5505 }
5506 BidiFormatter bidi = BidiFormatter.getInstance();
5507 if (titleText != null) {
5508 summary.append(bidi.unicodeWrap(titleText));
5509 }
5510 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
5511 if (titleText != null && contentText != null) {
5512 summary.append(bidi.unicodeWrap(mContext.getText(
5513 R.string.notification_header_divider_symbol_with_spaces)));
5514 }
5515 if (contentText != null) {
5516 summary.append(bidi.unicodeWrap(contentText));
5517 }
5518 return summary;
5519 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05005520
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005521 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005522 StandardTemplateParams p) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04005523 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07005524 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005525 emphazisedMode ? getEmphasizedActionLayoutResource()
5526 : tombstone ? getActionTombstoneLayoutResource()
5527 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04005528 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04005529 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04005530 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005531 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005532 if (action.mRemoteInputs != null) {
5533 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
5534 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005535 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07005536 // change the background bgColor
Selim Cinek981962e2016-07-20 20:41:58 -07005537 CharSequence title = action.title;
5538 ColorStateList[] outResultColor = null;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005539 int background = resolveBackgroundColor(p);
Selim Cinek981962e2016-07-20 20:41:58 -07005540 if (isLegacy()) {
Lucas Dupina291d192018-06-07 13:59:42 -07005541 title = ContrastColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07005542 } else {
5543 outResultColor = new ColorStateList[1];
Selim Cinek396caca2018-04-10 17:46:46 -07005544 title = ensureColorSpanContrast(title, background, outResultColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005545 }
Selim Cinek48f66b72017-08-18 16:17:51 -07005546 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005547 setTextViewColorPrimary(button, R.id.action0, p);
Selim Cinek396caca2018-04-10 17:46:46 -07005548 int rippleColor;
5549 boolean hasColorOverride = outResultColor != null && outResultColor[0] != null;
5550 if (hasColorOverride) {
5551 // There's a span spanning the full text, let's take it and use it as the
5552 // background color
5553 background = outResultColor[0].getDefaultColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005554 int textColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07005555 background, mInNightMode);
Selim Cinek396caca2018-04-10 17:46:46 -07005556 button.setTextColor(R.id.action0, textColor);
5557 rippleColor = textColor;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005558 } else if (getRawColor(p) != COLOR_DEFAULT && !isColorized(p)
5559 && mTintActionButtons) {
5560 rippleColor = resolveContrastColor(p);
Selim Cinek396caca2018-04-10 17:46:46 -07005561 button.setTextColor(R.id.action0, rippleColor);
5562 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005563 rippleColor = getPrimaryTextColor(p);
Selim Cinek981962e2016-07-20 20:41:58 -07005564 }
Selim Cinek396caca2018-04-10 17:46:46 -07005565 // We only want about 20% alpha for the ripple
5566 rippleColor = (rippleColor & 0x00ffffff) | 0x33000000;
5567 button.setColorStateList(R.id.action0, "setRippleColor",
5568 ColorStateList.valueOf(rippleColor));
5569 button.setColorStateList(R.id.action0, "setButtonBackground",
5570 ColorStateList.valueOf(background));
5571 button.setBoolean(R.id.action0, "setHasStroke", !hasColorOverride);
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005572 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07005573 button.setTextViewText(R.id.action0, processTextSpans(
5574 processLegacyText(action.title)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005575 if (isColorized(p)) {
5576 setTextViewColorPrimary(button, R.id.action0, p);
5577 } else if (getRawColor(p) != COLOR_DEFAULT && mTintActionButtons) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005578 button.setTextColor(R.id.action0, resolveContrastColor(p));
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005579 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005580 }
Tony Mak7d4b3a52018-11-27 17:29:36 +00005581 button.setIntTag(R.id.action0, R.id.notification_action_index_tag,
5582 mActions.indexOf(action));
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005583 return button;
5584 }
5585
Joe Onoratocb109a02011-01-18 17:57:41 -08005586 /**
Selim Cinek981962e2016-07-20 20:41:58 -07005587 * Ensures contrast on color spans against a background color. also returns the color of the
5588 * text if a span was found that spans over the whole text.
5589 *
5590 * @param charSequence the charSequence on which the spans are
5591 * @param background the background color to ensure the contrast against
5592 * @param outResultColor an array in which a color will be returned as the first element if
5593 * there exists a full length color span.
5594 * @return the contrasted charSequence
5595 */
5596 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
5597 ColorStateList[] outResultColor) {
5598 if (charSequence instanceof Spanned) {
5599 Spanned ss = (Spanned) charSequence;
5600 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
5601 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
5602 for (Object span : spans) {
5603 Object resultSpan = span;
5604 int spanStart = ss.getSpanStart(span);
5605 int spanEnd = ss.getSpanEnd(span);
5606 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
5607 if (resultSpan instanceof CharacterStyle) {
5608 resultSpan = ((CharacterStyle) span).getUnderlying();
5609 }
5610 if (resultSpan instanceof TextAppearanceSpan) {
5611 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
5612 ColorStateList textColor = originalSpan.getTextColor();
5613 if (textColor != null) {
5614 int[] colors = textColor.getColors();
5615 int[] newColors = new int[colors.length];
5616 for (int i = 0; i < newColors.length; i++) {
Lucas Dupina291d192018-06-07 13:59:42 -07005617 newColors[i] = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005618 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005619 }
5620 textColor = new ColorStateList(textColor.getStates().clone(),
5621 newColors);
Selim Cinek396caca2018-04-10 17:46:46 -07005622 if (fullLength) {
5623 outResultColor[0] = textColor;
5624 // Let's drop the color from the span
5625 textColor = null;
5626 }
Selim Cinek981962e2016-07-20 20:41:58 -07005627 resultSpan = new TextAppearanceSpan(
5628 originalSpan.getFamily(),
5629 originalSpan.getTextStyle(),
5630 originalSpan.getTextSize(),
5631 textColor,
5632 originalSpan.getLinkTextColor());
Selim Cinek981962e2016-07-20 20:41:58 -07005633 }
5634 } else if (resultSpan instanceof ForegroundColorSpan) {
5635 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
5636 int foregroundColor = originalSpan.getForegroundColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005637 foregroundColor = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005638 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005639 if (fullLength) {
5640 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
Selim Cinek396caca2018-04-10 17:46:46 -07005641 resultSpan = null;
5642 } else {
5643 resultSpan = new ForegroundColorSpan(foregroundColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005644 }
5645 } else {
5646 resultSpan = span;
5647 }
Selim Cinek396caca2018-04-10 17:46:46 -07005648 if (resultSpan != null) {
5649 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
5650 }
Selim Cinek981962e2016-07-20 20:41:58 -07005651 }
5652 return builder;
5653 }
5654 return charSequence;
5655 }
5656
5657 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005658 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07005659 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005660 */
5661 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005662 if (!mIsLegacyInitialized) {
5663 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
5664 < Build.VERSION_CODES.LOLLIPOP;
5665 mIsLegacyInitialized = true;
5666 }
5667 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005668 }
5669
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005670 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08005671 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
Lucas Dupin00be88f2019-01-03 17:50:52 -08005672 if (isAlreadyLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005673 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005674 } else {
5675 return charSequence;
5676 }
5677 }
5678
Dan Sandler26e81cf2014-05-06 10:01:27 -04005679 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005680 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04005681 */
Adrian Roos487374f2017-01-11 15:48:14 -08005682 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005683 StandardTemplateParams p) {
Selim Cinekea4bef72015-12-02 15:51:10 -08005684 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005685 int color;
Lucas Dupin00be88f2019-01-03 17:50:52 -08005686 if (isColorized(p)) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005687 color = getPrimaryTextColor(p);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005688 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005689 color = resolveContrastColor(p);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005690 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005691 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07005692 contentView.setDrawableTint(R.id.icon, false, color,
5693 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005694
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005695 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005696 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08005697 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005698 }
5699
Dan Sandler26e81cf2014-05-06 10:01:27 -04005700 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005701 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04005702 * if it's grayscale).
5703 */
5704 // TODO: also check bounds, transparency, that sort of thing.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005705 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView,
5706 StandardTemplateParams p) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04005707 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005708 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005709 // resolve color will fall back to the default when legacy
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005710 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(p),
Sunny Goyal5b153922017-09-21 21:00:36 -07005711 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01005712 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005713 }
5714
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05005715 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005716 if (mN.color != COLOR_DEFAULT) {
5717 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02005718 }
Jorim Jaggi74419312014-06-10 20:57:21 +02005719 }
5720
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005721 int resolveContrastColor(StandardTemplateParams p) {
5722 int rawColor = getRawColor(p);
5723 if (mCachedContrastColorIsFor == rawColor && mCachedContrastColor != COLOR_INVALID) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08005724 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005725 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005726
Selim Cinekac5f0272017-05-02 16:05:41 -07005727 int color;
Selim Cinekc7f5a822018-03-20 19:32:06 -07005728 int background = mContext.getColor(
5729 com.android.internal.R.color.notification_material_background_color);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005730 if (rawColor == COLOR_DEFAULT) {
5731 ensureColors(p);
Lucas Dupinf03e7522018-06-25 16:21:13 -07005732 color = ContrastColorUtil.resolveDefaultColor(mContext, background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005733 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005734 color = ContrastColorUtil.resolveContrastColor(mContext, rawColor,
Anthony Chenad4d1582017-04-10 16:07:58 -07005735 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005736 }
5737 if (Color.alpha(color) < 255) {
5738 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005739 color = ContrastColorUtil.compositeColors(color, background);
Selim Cinekac5f0272017-05-02 16:05:41 -07005740 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005741 mCachedContrastColorIsFor = rawColor;
Selim Cinekac5f0272017-05-02 16:05:41 -07005742 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005743 }
5744
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005745 /**
5746 * Return the raw color of this Notification, which doesn't necessarily satisfy contrast.
5747 *
5748 * @see #resolveContrastColor(StandardTemplateParams) for the contrasted color
5749 * @param p the template params to inflate this with
5750 */
5751 private int getRawColor(StandardTemplateParams p) {
5752 if (p.forceDefaultColor) {
5753 return COLOR_DEFAULT;
5754 }
5755 return mN.color;
5756 }
5757
Selim Cinek4717d862018-04-19 09:19:15 +08005758 int resolveNeutralColor() {
5759 if (mNeutralColor != COLOR_INVALID) {
5760 return mNeutralColor;
5761 }
5762 int background = mContext.getColor(
5763 com.android.internal.R.color.notification_material_background_color);
Lucas Dupinf03e7522018-06-25 16:21:13 -07005764 mNeutralColor = ContrastColorUtil.resolveDefaultColor(mContext, background,
5765 mInNightMode);
Selim Cinek4717d862018-04-19 09:19:15 +08005766 if (Color.alpha(mNeutralColor) < 255) {
5767 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005768 mNeutralColor = ContrastColorUtil.compositeColors(mNeutralColor, background);
Selim Cinek4717d862018-04-19 09:19:15 +08005769 }
5770 return mNeutralColor;
5771 }
5772
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005773 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005774 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005775 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08005776 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005777 @NonNull
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005778 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005779 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005780 mN.actions = new Action[mActions.size()];
5781 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005782 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005783 if (!mPersonList.isEmpty()) {
Selim Cineke7238dd2017-12-14 17:48:32 -08005784 mN.extras.putParcelableArrayList(EXTRA_PEOPLE_LIST, mPersonList);
Dan Sandler0bf2ed82013-12-21 23:33:41 -06005785 }
Selim Cinek247fa012016-02-18 09:50:48 -08005786 if (mN.bigContentView != null || mN.contentView != null
5787 || mN.headsUpContentView != null) {
5788 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
5789 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005790 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08005791 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005792
Julia Reynolds3b848122016-02-26 10:45:32 -05005793 /**
5794 * Creates a Builder from an existing notification so further changes can be made.
5795 * @param context The context for your application / activity.
5796 * @param n The notification to create a Builder from.
5797 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005798 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005799 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005800 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005801 ApplicationInfo applicationInfo = n.extras.getParcelable(
5802 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005803 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05005804 if (applicationInfo != null) {
5805 try {
5806 builderContext = context.createApplicationContext(applicationInfo,
5807 Context.CONTEXT_RESTRICTED);
5808 } catch (NameNotFoundException e) {
5809 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
5810 builderContext = context; // try with our context
5811 }
5812 } else {
5813 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02005814 }
5815
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005816 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005817 }
5818
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005819 /**
Gustav Sennton761884c2018-11-19 17:40:19 +00005820 * Determines whether the platform can generate contextual actions for a notification.
5821 * By default this is true.
5822 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005823 @NonNull
Gustav Sennton761884c2018-11-19 17:40:19 +00005824 public Builder setAllowSystemGeneratedContextualActions(boolean allowed) {
5825 mN.mAllowSystemGeneratedContextualActions = allowed;
5826 return this;
5827 }
5828
5829 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005830 * @deprecated Use {@link #build()} instead.
5831 */
5832 @Deprecated
5833 public Notification getNotification() {
5834 return build();
5835 }
5836
5837 /**
5838 * Combine all of the options that have been set and return a new {@link Notification}
5839 * object.
5840 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005841 @NonNull
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005842 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005843 // first, add any extras from the calling code
5844 if (mUserExtras != null) {
5845 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005846 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005847
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005848 mN.creationTime = System.currentTimeMillis();
5849
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005850 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05005851 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02005852
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005853 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005854
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005855 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02005856 mStyle.reduceImageSizes(mContext);
5857 mStyle.purgeResources();
Selim Cinek90343862018-02-01 11:07:11 -08005858 mStyle.validate(mContext);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005859 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005860 }
Selim Cinekd0426622017-07-11 13:19:59 +02005861 mN.reduceImageSizes(mContext);
5862
Adrian Roos5081c0d2016-02-26 16:04:19 -08005863 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07005864 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005865 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005866 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005867 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
5868 mN.contentView.getSequenceNumber());
5869 }
5870 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005871 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005872 if (mN.bigContentView != null) {
5873 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
5874 mN.bigContentView.getSequenceNumber());
5875 }
5876 }
5877 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005878 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005879 if (mN.headsUpContentView != null) {
5880 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
5881 mN.headsUpContentView.getSequenceNumber());
5882 }
5883 }
5884 }
5885
Julia Reynolds4c0c2022016-02-02 15:11:59 -05005886 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
5887 mN.flags |= FLAG_SHOW_LIGHTS;
5888 }
5889
Adrian Roosfb921842017-10-26 14:49:56 +02005890 mN.allPendingIntents = null;
5891
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005892 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005893 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005894
5895 /**
5896 * Apply this Builder to an existing {@link Notification} object.
5897 *
5898 * @hide
5899 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005900 @NonNull
5901 public Notification buildInto(@NonNull Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005902 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005903 return n;
5904 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005905
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005906 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005907 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005908 * change. Also removes extenders on low ram devices, as
5909 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005910 *
5911 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5912 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005913 * @hide
5914 */
Kristian Monsen05f34792018-04-09 10:27:16 +02005915 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam,
5916 Context context) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005917 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005918
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005919 // Only strip views for known Styles because we won't know how to
5920 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005921 if (!isLowRam
5922 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005923 && getNotificationStyleClass(templateClass) == null) {
5924 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005925 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005926
5927 // Only strip unmodified BuilderRemoteViews.
5928 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005929 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005930 n.contentView.getSequenceNumber();
5931 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005932 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005933 n.bigContentView.getSequenceNumber();
5934 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005935 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005936 n.headsUpContentView.getSequenceNumber();
5937
5938 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005939 if (!isLowRam
5940 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005941 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005942 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005943
5944 Notification clone = n.clone();
5945 if (stripContentView) {
5946 clone.contentView = null;
5947 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5948 }
5949 if (stripBigContentView) {
5950 clone.bigContentView = null;
5951 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5952 }
5953 if (stripHeadsUpContentView) {
5954 clone.headsUpContentView = null;
5955 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5956 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005957 if (isLowRam) {
Kristian Monsen05f34792018-04-09 10:27:16 +02005958 String[] allowedServices = context.getResources().getStringArray(
5959 R.array.config_allowedManagedServicesOnLowRamDevices);
5960 if (allowedServices.length == 0) {
5961 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5962 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5963 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5964 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005965 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005966 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005967 }
5968
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005969 @UnsupportedAppUsage
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005970 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005971 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005972 }
5973
5974 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005975 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005976 }
5977
5978 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005979 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005980 }
5981
5982 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005983 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005984 }
5985
5986 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005987 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005988 }
5989
Adrian Roosc1a80b02016-04-05 14:54:55 -07005990 private int getMessagingLayoutResource() {
5991 return R.layout.notification_template_material_messaging;
5992 }
5993
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005994 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005995 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005996 }
5997
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005998 private int getEmphasizedActionLayoutResource() {
5999 return R.layout.notification_material_action_emphasized;
6000 }
6001
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006002 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07006003 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006004 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08006005
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006006 private int getBackgroundColor(StandardTemplateParams p) {
6007 if (isColorized(p)) {
6008 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : getRawColor(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08006009 } else {
Selim Cinekc7f5a822018-03-20 19:32:06 -07006010 return COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08006011 }
6012 }
6013
Selim Cinek396caca2018-04-10 17:46:46 -07006014 /**
Selim Cinek4717d862018-04-19 09:19:15 +08006015 * Gets a neutral color that can be used for icons or similar that should not stand out.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006016 * @param p the template params to inflate this with
Selim Cinek4717d862018-04-19 09:19:15 +08006017 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006018 private int getNeutralColor(StandardTemplateParams p) {
6019 if (isColorized(p)) {
6020 return getSecondaryTextColor(p);
Selim Cinek4717d862018-04-19 09:19:15 +08006021 } else {
6022 return resolveNeutralColor();
6023 }
6024 }
6025
6026 /**
Selim Cinek396caca2018-04-10 17:46:46 -07006027 * Same as getBackgroundColor but also resolved the default color to the background.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006028 * @param p the template params to inflate this with
Selim Cinek396caca2018-04-10 17:46:46 -07006029 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006030 private int resolveBackgroundColor(StandardTemplateParams p) {
6031 int backgroundColor = getBackgroundColor(p);
Selim Cinek396caca2018-04-10 17:46:46 -07006032 if (backgroundColor == COLOR_DEFAULT) {
6033 backgroundColor = mContext.getColor(
6034 com.android.internal.R.color.notification_material_background_color);
6035 }
6036 return backgroundColor;
6037 }
6038
Anthony Chenad4d1582017-04-10 16:07:58 -07006039 private boolean shouldTintActionButtons() {
6040 return mTintActionButtons;
6041 }
6042
Selim Cinek99104832017-01-25 14:47:33 -08006043 private boolean textColorsNeedInversion() {
6044 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
6045 return false;
6046 }
6047 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
6048 return targetSdkVersion > Build.VERSION_CODES.M
6049 && targetSdkVersion < Build.VERSION_CODES.O;
6050 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07006051
6052 /**
6053 * Set a color palette to be used as the background and textColors
6054 *
6055 * @param backgroundColor the color to be used as the background
6056 * @param foregroundColor the color to be used as the foreground
6057 *
6058 * @hide
6059 */
6060 public void setColorPalette(int backgroundColor, int foregroundColor) {
6061 mBackgroundColor = backgroundColor;
6062 mForegroundColor = foregroundColor;
6063 mTextColorsAreForBackground = COLOR_INVALID;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006064 ensureColors(mParams.reset().fillTextsFrom(this));
Selim Cinek5fb73f82017-04-20 16:55:38 -07006065 }
Selim Cinekac5f0272017-05-02 16:05:41 -07006066
6067 /**
Selim Cineka7679b62017-05-10 16:33:25 -07006068 * Forces all styled remoteViews to be built from scratch and not use any cached
6069 * RemoteViews.
6070 * This is needed for legacy apps that are baking in their remoteviews into the
6071 * notification.
6072 *
6073 * @hide
6074 */
6075 public void setRebuildStyledRemoteViews(boolean rebuild) {
6076 mRebuildStyledRemoteViews = rebuild;
6077 }
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006078
6079 /**
6080 * Get the text that should be displayed in the statusBar when heads upped. This is
6081 * usually just the app name, but may be different depending on the style.
6082 *
6083 * @param publicMode If true, return a text that is safe to display in public.
6084 *
6085 * @hide
6086 */
6087 public CharSequence getHeadsUpStatusBarText(boolean publicMode) {
6088 if (mStyle != null && !publicMode) {
6089 CharSequence text = mStyle.getHeadsUpStatusBarText();
6090 if (!TextUtils.isEmpty(text)) {
6091 return text;
6092 }
6093 }
6094 return loadHeaderAppName();
6095 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08006096 }
6097
6098 /**
Selim Cinekd0426622017-07-11 13:19:59 +02006099 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
6100 * remote views.
6101 *
6102 * @hide
6103 */
6104 void reduceImageSizes(Context context) {
6105 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
6106 return;
6107 }
Selim Cineka8cb1262017-08-15 16:53:44 -07006108 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006109 if (mLargeIcon != null || largeIcon != null) {
6110 Resources resources = context.getResources();
6111 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07006112 int maxWidth = resources.getDimensionPixelSize(isLowRam
6113 ? R.dimen.notification_right_icon_size_low_ram
6114 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006115 int maxHeight = maxWidth;
6116 if (MediaStyle.class.equals(style)
6117 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006118 maxHeight = resources.getDimensionPixelSize(isLowRam
6119 ? R.dimen.notification_media_image_max_height_low_ram
6120 : R.dimen.notification_media_image_max_height);
6121 maxWidth = resources.getDimensionPixelSize(isLowRam
6122 ? R.dimen.notification_media_image_max_width_low_ram
6123 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006124 }
6125 if (mLargeIcon != null) {
6126 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
6127 }
6128 if (largeIcon != null) {
6129 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
6130 }
6131 }
Selim Cineka8cb1262017-08-15 16:53:44 -07006132 reduceImageSizesForRemoteView(contentView, context, isLowRam);
6133 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
6134 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02006135 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
6136 }
6137
Selim Cineka8cb1262017-08-15 16:53:44 -07006138 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
6139 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02006140 if (remoteView != null) {
6141 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006142 int maxWidth = resources.getDimensionPixelSize(isLowRam
6143 ? R.dimen.notification_custom_view_max_image_width_low_ram
6144 : R.dimen.notification_custom_view_max_image_width);
6145 int maxHeight = resources.getDimensionPixelSize(isLowRam
6146 ? R.dimen.notification_custom_view_max_image_height_low_ram
6147 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02006148 remoteView.reduceImageSizes(maxWidth, maxHeight);
6149 }
6150 }
6151
6152 /**
Selim Cinek22714f12017-04-13 16:23:53 -07006153 * @return whether this notification is a foreground service notification
Gus Prevasb0c1a462018-11-05 11:06:15 -05006154 * @hide
Selim Cinek7b9605b2017-01-19 17:36:00 -08006155 */
Gus Prevasb0c1a462018-11-05 11:06:15 -05006156 public boolean isForegroundService() {
Selim Cinek22714f12017-04-13 16:23:53 -07006157 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08006158 }
6159
6160 /**
Selim Cinek99104832017-01-25 14:47:33 -08006161 * @return whether this notification has a media session attached
6162 * @hide
6163 */
6164 public boolean hasMediaSession() {
6165 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
6166 }
6167
6168 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006169 * @return the style class of this notification
6170 * @hide
6171 */
6172 public Class<? extends Notification.Style> getNotificationStyle() {
6173 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
6174
6175 if (!TextUtils.isEmpty(templateClass)) {
6176 return Notification.getNotificationStyleClass(templateClass);
6177 }
6178 return null;
6179 }
6180
6181 /**
Selim Cinek22714f12017-04-13 16:23:53 -07006182 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08006183 *
6184 * @hide
6185 */
6186 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07006187 if (isColorizedMedia()) {
6188 return true;
6189 }
Julia Reynolds4db59552017-06-30 13:34:01 -04006190 return extras.getBoolean(EXTRA_COLORIZED)
6191 && (hasColorizedPermission() || isForegroundService());
6192 }
6193
6194 /**
6195 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
6196 * permission. The permission is checked when a notification is enqueued.
6197 */
6198 private boolean hasColorizedPermission() {
6199 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07006200 }
6201
6202 /**
6203 * @return true if this notification is colorized and it is a media notification
6204 *
6205 * @hide
6206 */
6207 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08006208 Class<? extends Style> style = getNotificationStyle();
6209 if (MediaStyle.class.equals(style)) {
6210 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
6211 if ((colorized == null || colorized) && hasMediaSession()) {
6212 return true;
6213 }
6214 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6215 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
6216 return true;
6217 }
6218 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07006219 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006220 }
6221
Selim Cinek0847acd2017-04-24 19:48:29 -07006222
6223 /**
6224 * @return true if this is a media notification
6225 *
6226 * @hide
6227 */
6228 public boolean isMediaNotification() {
6229 Class<? extends Style> style = getNotificationStyle();
6230 if (MediaStyle.class.equals(style)) {
6231 return true;
6232 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6233 return true;
6234 }
6235 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006236 }
6237
Selim Cinek279fa862016-06-14 10:57:25 -07006238 private boolean hasLargeIcon() {
6239 return mLargeIcon != null || largeIcon != null;
6240 }
6241
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006242 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006243 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006244 * @hide
6245 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07006246 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006247 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
6248 }
6249
6250 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006251 * @return true if the notification will show a chronometer; false otherwise
6252 * @hide
6253 */
6254 public boolean showsChronometer() {
6255 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
6256 }
6257
6258 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04006259 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05006260 */
6261 @SystemApi
6262 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
6263 Class<? extends Style>[] classes = new Class[] {
6264 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
6265 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
6266 MessagingStyle.class };
6267 for (Class<? extends Style> innerClass : classes) {
6268 if (templateClass.equals(innerClass.getName())) {
6269 return innerClass;
6270 }
6271 }
6272 return null;
6273 }
6274
6275 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006276 * An object that can apply a rich notification style to a {@link Notification.Builder}
6277 * object.
6278 */
Griff Hazendfcb0802014-02-11 12:00:00 -08006279 public static abstract class Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07006280
6281 /**
6282 * The number of items allowed simulatanously in the remote input history.
6283 * @hide
6284 */
6285 static final int MAX_REMOTE_INPUT_HISTORY_LINES = 3;
Chris Wrend6297db2012-05-03 16:20:13 -04006286 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006287
6288 /**
6289 * @hide
6290 */
6291 protected CharSequence mSummaryText = null;
6292
6293 /**
6294 * @hide
6295 */
6296 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04006297
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006298 protected Builder mBuilder;
6299
Chris Wrend6297db2012-05-03 16:20:13 -04006300 /**
6301 * Overrides ContentTitle in the big form of the template.
6302 * This defaults to the value passed to setContentTitle().
6303 */
6304 protected void internalSetBigContentTitle(CharSequence title) {
6305 mBigContentTitle = title;
6306 }
6307
6308 /**
6309 * Set the first line of text after the detail section in the big form of the template.
6310 */
6311 protected void internalSetSummaryText(CharSequence cs) {
6312 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04006313 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04006314 }
6315
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006316 public void setBuilder(Builder builder) {
6317 if (mBuilder != builder) {
6318 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07006319 if (mBuilder != null) {
6320 mBuilder.setStyle(this);
6321 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006322 }
6323 }
6324
Chris Wrend6297db2012-05-03 16:20:13 -04006325 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006326 if (mBuilder == null) {
6327 throw new IllegalArgumentException("Style requires a valid Builder object");
6328 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006329 }
Chris Wrend6297db2012-05-03 16:20:13 -04006330
6331 protected RemoteViews getStandardView(int layoutId) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006332 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder);
6333 return getStandardView(layoutId, p, null);
Selim Cinek384804b2018-04-18 14:31:07 +08006334 }
6335
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006336
Selim Cinek384804b2018-04-18 14:31:07 +08006337 /**
6338 * Get the standard view for this style.
6339 *
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006340 * @param layoutId The layout id to use.
6341 * @param p the params for this inflation.
Selim Cinek384804b2018-04-18 14:31:07 +08006342 * @param result The result where template bind information is saved.
6343 * @return A remoteView for this style.
6344 * @hide
6345 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006346 protected RemoteViews getStandardView(int layoutId, StandardTemplateParams p,
6347 TemplateBindResult result) {
Chris Wrend6297db2012-05-03 16:20:13 -04006348 checkBuilder();
6349
6350 if (mBigContentTitle != null) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006351 p.title = mBigContentTitle;
Chris Wrend6297db2012-05-03 16:20:13 -04006352 }
6353
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006354 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId, p,
6355 result);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006356
Chris Wrend6297db2012-05-03 16:20:13 -04006357 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
6358 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04006359 } else {
6360 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04006361 }
6362
Chris Wrend6297db2012-05-03 16:20:13 -04006363 return contentView;
6364 }
6365
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006366 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006367 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006368 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08006369 *
6370 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006371 * @hide
6372 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08006373 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006374 return null;
6375 }
6376
6377 /**
6378 * Construct a Style-specific RemoteViews for the final big notification layout.
6379 * @hide
6380 */
6381 public RemoteViews makeBigContentView() {
6382 return null;
6383 }
6384
6385 /**
6386 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08006387 *
6388 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006389 * @hide
6390 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006391 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006392 return null;
6393 }
6394
6395 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006396 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006397 * @hide
6398 */
6399 public void addExtras(Bundle extras) {
6400 if (mSummaryTextSet) {
6401 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
6402 }
6403 if (mBigContentTitle != null) {
6404 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
6405 }
Chris Wren91ad5632013-06-05 15:05:57 -04006406 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006407 }
6408
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006409 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006410 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006411 * @hide
6412 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02006413 protected void restoreFromExtras(Bundle extras) {
6414 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
6415 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
6416 mSummaryTextSet = true;
6417 }
6418 if (extras.containsKey(EXTRA_TITLE_BIG)) {
6419 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
6420 }
6421 }
6422
6423
6424 /**
6425 * @hide
6426 */
6427 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006428 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006429 return wip;
6430 }
6431
Daniel Sandler0ec46202015-06-24 01:27:05 -04006432 /**
6433 * @hide
6434 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006435 public void purgeResources() {}
6436
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006437 /**
6438 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
6439 * attached to.
6440 *
6441 * @return the fully constructed Notification.
6442 */
6443 public Notification build() {
6444 checkBuilder();
6445 return mBuilder.build();
6446 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006447
6448 /**
6449 * @hide
6450 * @return true if the style positions the progress bar on the second line; false if the
6451 * style hides the progress bar
6452 */
6453 protected boolean hasProgress() {
6454 return true;
6455 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006456
6457 /**
6458 * @hide
6459 * @return Whether we should put the summary be put into the notification header
6460 */
6461 public boolean hasSummaryInHeader() {
6462 return true;
6463 }
Selim Cinek593610c2016-02-16 18:42:57 -08006464
6465 /**
6466 * @hide
6467 * @return Whether custom content views are displayed inline in the style
6468 */
6469 public boolean displayCustomViewInline() {
6470 return false;
6471 }
Selim Cinekd0426622017-07-11 13:19:59 +02006472
6473 /**
6474 * Reduces the image sizes contained in this style.
6475 *
6476 * @hide
6477 */
6478 public void reduceImageSizes(Context context) {
6479 }
Selim Cinek90343862018-02-01 11:07:11 -08006480
6481 /**
6482 * Validate that this style was properly composed. This is called at build time.
6483 * @hide
6484 */
6485 public void validate(Context context) {
6486 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006487
6488 /**
6489 * @hide
6490 */
6491 public abstract boolean areNotificationsVisiblyDifferent(Style other);
Selim Cinekc7f5a822018-03-20 19:32:06 -07006492
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006493 /**
koprivaa1a78482018-10-09 10:09:23 -07006494 * @return the text that should be displayed in the statusBar when heads-upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006495 * If {@code null} is returned, the default implementation will be used.
6496 *
6497 * @hide
6498 */
6499 public CharSequence getHeadsUpStatusBarText() {
6500 return null;
6501 }
Joe Onorato46439ce2010-11-19 13:56:21 -08006502 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006503
6504 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006505 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08006506 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006507 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006508 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006509 * Notification notif = new Notification.Builder(mContext)
6510 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
6511 * .setContentText(subject)
6512 * .setSmallIcon(R.drawable.new_post)
6513 * .setLargeIcon(aBitmap)
6514 * .setStyle(new Notification.BigPictureStyle()
6515 * .bigPicture(aBigBitmap))
6516 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006517 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006518 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006519 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006520 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006521 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006522 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006523 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006524 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006525
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006526 public BigPictureStyle() {
6527 }
6528
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006529 /**
6530 * @deprecated use {@code BigPictureStyle()}.
6531 */
6532 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006533 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006534 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006535 }
6536
Chris Wrend6297db2012-05-03 16:20:13 -04006537 /**
6538 * Overrides ContentTitle in the big form of the template.
6539 * This defaults to the value passed to setContentTitle().
6540 */
6541 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006542 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006543 return this;
6544 }
6545
6546 /**
6547 * Set the first line of text after the detail section in the big form of the template.
6548 */
6549 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006550 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006551 return this;
6552 }
6553
Chris Wren0bd664d2012-08-01 13:56:56 -04006554 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05006555 * @hide
6556 */
6557 public Bitmap getBigPicture() {
6558 return mPicture;
6559 }
6560
6561 /**
Chris Wren0bd664d2012-08-01 13:56:56 -04006562 * Provide the bitmap to be used as the payload for the BigPicture notification.
6563 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006564 public BigPictureStyle bigPicture(Bitmap b) {
6565 mPicture = b;
6566 return this;
6567 }
6568
Chris Wren3745a3d2012-05-22 15:11:52 -04006569 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04006570 * Override the large icon when the big notification is shown.
6571 */
6572 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04006573 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
6574 }
6575
6576 /**
6577 * Override the large icon when the big notification is shown.
6578 */
6579 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04006580 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006581 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006582 return this;
6583 }
6584
Riley Andrews0394a0c2015-11-03 23:36:52 -08006585 /** @hide */
6586 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
6587
Daniel Sandler0ec46202015-06-24 01:27:05 -04006588 /**
6589 * @hide
6590 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006591 @Override
6592 public void purgeResources() {
6593 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08006594 if (mPicture != null &&
6595 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08006596 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006597 mPicture = mPicture.createAshmemBitmap();
6598 }
6599 if (mBigLargeIcon != null) {
6600 mBigLargeIcon.convertToAshmem();
6601 }
6602 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01006603
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006604 /**
6605 * @hide
6606 */
Selim Cinekd0426622017-07-11 13:19:59 +02006607 @Override
6608 public void reduceImageSizes(Context context) {
6609 super.reduceImageSizes(context);
6610 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006611 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006612 if (mPicture != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006613 int maxPictureWidth = resources.getDimensionPixelSize(isLowRam
6614 ? R.dimen.notification_big_picture_max_height_low_ram
6615 : R.dimen.notification_big_picture_max_height);
6616 int maxPictureHeight = resources.getDimensionPixelSize(isLowRam
6617 ? R.dimen.notification_big_picture_max_width_low_ram
6618 : R.dimen.notification_big_picture_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006619 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
6620 }
6621 if (mBigLargeIcon != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006622 int rightIconSize = resources.getDimensionPixelSize(isLowRam
6623 ? R.dimen.notification_right_icon_size_low_ram
6624 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006625 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
6626 }
6627 }
6628
6629 /**
6630 * @hide
6631 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006632 public RemoteViews makeBigContentView() {
6633 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01006634 // This covers the following cases:
6635 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006636 // mN.mLargeIcon
6637 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04006638 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07006639 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006640 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006641 oldLargeIcon = mBuilder.mN.mLargeIcon;
6642 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006643 // The legacy largeIcon might not allow us to clear the image, as it's taken in
6644 // replacement if the other one is null. Because we're restoring these legacy icons
6645 // for old listeners, this is in general non-null.
6646 largeIconLegacy = mBuilder.mN.largeIcon;
6647 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006648 }
6649
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006650 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder);
Selim Cinek384804b2018-04-18 14:31:07 +08006651 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource(),
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006652 p, null /* result */);
Selim Cinek03d0d652015-11-13 13:18:09 -05006653 if (mSummaryTextSet) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006654 contentView.setTextViewText(R.id.text, mBuilder.processTextSpans(
6655 mBuilder.processLegacyText(mSummaryText)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006656 mBuilder.setTextViewColorSecondary(contentView, R.id.text, p);
Selim Cinekc848c3a2016-01-13 15:27:30 -08006657 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05006658 }
Selim Cinek279fa862016-06-14 10:57:25 -07006659 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08006660
Christoph Studer5c510ee2014-12-15 16:32:27 +01006661 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006662 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006663 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006664 }
6665
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006666 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006667 return contentView;
6668 }
6669
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006670 /**
6671 * @hide
6672 */
6673 public void addExtras(Bundle extras) {
6674 super.addExtras(extras);
6675
6676 if (mBigLargeIconSet) {
6677 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
6678 }
6679 extras.putParcelable(EXTRA_PICTURE, mPicture);
6680 }
6681
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006682 /**
6683 * @hide
6684 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006685 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02006686 protected void restoreFromExtras(Bundle extras) {
6687 super.restoreFromExtras(extras);
6688
6689 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01006690 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02006691 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04006692 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006693 mPicture = extras.getParcelable(EXTRA_PICTURE);
6694 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006695
6696 /**
6697 * @hide
6698 */
6699 @Override
6700 public boolean hasSummaryInHeader() {
6701 return false;
6702 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006703
6704 /**
6705 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006706 * Note that we aren't actually comparing the contents of the bitmaps here, so this
6707 * is only doing a cursory inspection. Bitmaps of equal size will appear the same.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006708 */
6709 @Override
6710 public boolean areNotificationsVisiblyDifferent(Style other) {
6711 if (other == null || getClass() != other.getClass()) {
6712 return true;
6713 }
6714 BigPictureStyle otherS = (BigPictureStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006715 return areBitmapsObviouslyDifferent(getBigPicture(), otherS.getBigPicture());
6716 }
6717
6718 private static boolean areBitmapsObviouslyDifferent(Bitmap a, Bitmap b) {
6719 if (a == b) {
6720 return false;
6721 }
6722 if (a == null || b == null) {
6723 return true;
6724 }
6725 return a.getWidth() != b.getWidth()
6726 || a.getHeight() != b.getHeight()
6727 || a.getConfig() != b.getConfig()
6728 || a.getGenerationId() != b.getGenerationId();
Julia Reynolds7217dc92018-03-07 12:12:09 -05006729 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006730 }
6731
6732 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006733 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08006734 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006735 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006736 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006737 * Notification notif = new Notification.Builder(mContext)
6738 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
6739 * .setContentText(subject)
6740 * .setSmallIcon(R.drawable.new_mail)
6741 * .setLargeIcon(aBitmap)
6742 * .setStyle(new Notification.BigTextStyle()
6743 * .bigText(aVeryLongString))
6744 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006745 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006746 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006747 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006748 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006749 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006750
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006751 private CharSequence mBigText;
6752
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006753 public BigTextStyle() {
6754 }
6755
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006756 /**
6757 * @deprecated use {@code BigTextStyle()}.
6758 */
6759 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006760 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006761 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006762 }
6763
Chris Wrend6297db2012-05-03 16:20:13 -04006764 /**
6765 * Overrides ContentTitle in the big form of the template.
6766 * This defaults to the value passed to setContentTitle().
6767 */
6768 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006769 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006770 return this;
6771 }
6772
6773 /**
6774 * Set the first line of text after the detail section in the big form of the template.
6775 */
6776 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006777 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006778 return this;
6779 }
6780
Chris Wren0bd664d2012-08-01 13:56:56 -04006781 /**
6782 * Provide the longer text to be displayed in the big form of the
6783 * template in place of the content text.
6784 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006785 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006786 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006787 return this;
6788 }
6789
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006790 /**
6791 * @hide
6792 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05006793 public CharSequence getBigText() {
6794 return mBigText;
6795 }
6796
6797 /**
6798 * @hide
6799 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006800 public void addExtras(Bundle extras) {
6801 super.addExtras(extras);
6802
Christoph Studer4600f9b2014-07-22 22:44:43 +02006803 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
6804 }
6805
6806 /**
6807 * @hide
6808 */
6809 @Override
6810 protected void restoreFromExtras(Bundle extras) {
6811 super.restoreFromExtras(extras);
6812
6813 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006814 }
6815
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006816 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006817 * @param increasedHeight true if this layout be created with an increased height.
6818 *
6819 * @hide
6820 */
6821 @Override
6822 public RemoteViews makeContentView(boolean increasedHeight) {
6823 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006824 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006825 mBuilder.mActions = new ArrayList<>();
6826 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006827 mBuilder.mActions = mBuilder.mOriginalActions;
6828 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006829 return remoteViews;
6830 }
6831 return super.makeContentView(increasedHeight);
6832 }
6833
6834 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006835 * @hide
6836 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006837 @Override
6838 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
6839 if (increasedHeight && mBuilder.mActions.size() > 0) {
6840 return makeBigContentView();
6841 }
6842 return super.makeHeadsUpContentView(increasedHeight);
6843 }
6844
6845 /**
6846 * @hide
6847 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006848 public RemoteViews makeBigContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006849 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder).text(null);
Selim Cinek384804b2018-04-18 14:31:07 +08006850 TemplateBindResult result = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006851 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource(), p,
6852 result);
Selim Cinek1c72fa02018-04-23 18:00:54 +08006853 contentView.setInt(R.id.big_text, "setImageEndMargin", result.getIconMarginEnd());
Joe Malin8d40d042012-11-05 11:36:40 -08006854
Selim Cinek3a2c4b92015-12-17 17:01:17 -08006855 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07006856 if (TextUtils.isEmpty(bigTextText)) {
6857 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
6858 // experience
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006859 bigTextText = mBuilder.processLegacyText(
6860 mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT));
Selim Cinek75998782016-04-26 10:39:17 -07006861 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006862 contentView.setTextViewText(R.id.big_text, mBuilder.processTextSpans(bigTextText));
6863 mBuilder.setTextViewColorSecondary(contentView, R.id.big_text, p);
6864 contentView.setViewVisibility(R.id.big_text,
6865 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08006866 contentView.setBoolean(R.id.big_text, "setHasImage",
6867 result.isRightIconContainerVisible());
Selim Cinek4fb12d32015-11-19 18:10:48 -08006868
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006869 return contentView;
6870 }
6871
Julia Reynolds7217dc92018-03-07 12:12:09 -05006872 /**
6873 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006874 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006875 */
6876 @Override
6877 public boolean areNotificationsVisiblyDifferent(Style other) {
6878 if (other == null || getClass() != other.getClass()) {
6879 return true;
6880 }
6881 BigTextStyle newS = (BigTextStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006882 return !Objects.equals(String.valueOf(getBigText()), String.valueOf(newS.getBigText()));
Julia Reynolds7217dc92018-03-07 12:12:09 -05006883 }
6884
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006885 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006886
6887 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006888 * Helper class for generating large-format notifications that include multiple back-and-forth
6889 * messages of varying types between any number of people.
6890 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006891 * <p>
Alex Hillsfc737de2016-03-23 17:33:02 -04006892 * If the platform does not provide large-format notifications, this method has no effect. The
6893 * user will always see the normal notification view.
Selim Cinekce8794f2018-05-23 16:46:05 -07006894 *
6895 * <p>
6896 * If the app is targeting Android P and above, it is required to use the {@link Person}
6897 * class in order to get an optimal rendering of the notification and its avatars. For
6898 * conversations involving multiple people, the app should also make sure that it marks the
6899 * conversation as a group with {@link #setGroupConversation(boolean)}.
6900 *
6901 * <p>
6902 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior.
6903 * Here's an example of how this may be used:
Alex Hillsfc737de2016-03-23 17:33:02 -04006904 * <pre class="prettyprint">
6905 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006906 * Person user = new Person.Builder().setIcon(userIcon).setName(userName).build();
6907 * MessagingStyle style = new MessagingStyle(user)
6908 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getPerson())
6909 * .addMessage(messages[2].getText(), messages[2].getTime(), messages[2].getPerson())
6910 * .setGroupConversation(hasMultiplePeople());
6911 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006912 * Notification noti = new Notification.Builder()
Selim Cinekce8794f2018-05-23 16:46:05 -07006913 * .setContentTitle(&quot;2 new messages with &quot; + sender.toString())
Alex Hillsfc737de2016-03-23 17:33:02 -04006914 * .setContentText(subject)
6915 * .setSmallIcon(R.drawable.new_message)
6916 * .setLargeIcon(aBitmap)
Selim Cinekce8794f2018-05-23 16:46:05 -07006917 * .setStyle(style)
Alex Hillsfc737de2016-03-23 17:33:02 -04006918 * .build();
6919 * </pre>
6920 */
6921 public static class MessagingStyle extends Style {
6922
6923 /**
6924 * The maximum number of messages that will be retained in the Notification itself (the
6925 * number displayed is up to the platform).
6926 */
6927 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
6928
Selim Cinekcb8b9852017-12-15 18:01:52 -08006929 @NonNull Person mUser;
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006930 @Nullable CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04006931 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08006932 List<Message> mHistoricMessages = new ArrayList<>();
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006933 boolean mIsGroupConversation;
Alex Hillsfc737de2016-03-23 17:33:02 -04006934
6935 MessagingStyle() {
6936 }
6937
6938 /**
Alex Hillsfd590442016-10-07 09:52:44 -04006939 * @param userDisplayName Required - the name to be displayed for any replies sent by the
6940 * user before the posting app reposts the notification with those messages after they've
6941 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04006942 * {@link #addMessage(Notification.MessagingStyle.Message)}
Selim Cinekcb8b9852017-12-15 18:01:52 -08006943 *
6944 * @deprecated use {@code MessagingStyle(Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006945 */
Alex Hillsfd590442016-10-07 09:52:44 -04006946 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Selim Cinek9acd6732018-03-23 16:39:02 -07006947 this(new Person.Builder().setName(userDisplayName).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006948 }
6949
6950 /**
6951 * @param user Required - The person displayed for any messages that are sent by the
6952 * user. Any messages added with {@link #addMessage(Notification.MessagingStyle.Message)}
6953 * who don't have a Person associated with it will be displayed as if they were sent
Selim Cinek90343862018-02-01 11:07:11 -08006954 * by this user. The user also needs to have a valid name associated with it, which will
6955 * be enforced starting in Android P.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006956 */
6957 public MessagingStyle(@NonNull Person user) {
6958 mUser = user;
Selim Cinek90343862018-02-01 11:07:11 -08006959 }
6960
6961 /**
6962 * Validate that this style was properly composed. This is called at build time.
6963 * @hide
6964 */
6965 @Override
6966 public void validate(Context context) {
6967 super.validate(context);
6968 if (context.getApplicationInfo().targetSdkVersion
6969 >= Build.VERSION_CODES.P && (mUser == null || mUser.getName() == null)) {
6970 throw new RuntimeException("User must be valid and have a name.");
Selim Cinekcb8b9852017-12-15 18:01:52 -08006971 }
6972 }
6973
6974 /**
koprivaa1a78482018-10-09 10:09:23 -07006975 * @return the text that should be displayed in the statusBar when heads upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006976 * If {@code null} is returned, the default implementation will be used.
6977 *
6978 * @hide
6979 */
6980 @Override
6981 public CharSequence getHeadsUpStatusBarText() {
6982 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
6983 ? super.mBigContentTitle
6984 : mConversationTitle;
6985 if (!TextUtils.isEmpty(conversationTitle) && !hasOnlyWhiteSpaceSenders()) {
6986 return conversationTitle;
6987 }
6988 return null;
6989 }
6990
6991 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08006992 * @return the user to be displayed for any replies sent by the user
6993 */
Selim Cinekeb53c222018-04-06 12:56:09 -07006994 @NonNull
Selim Cinekcb8b9852017-12-15 18:01:52 -08006995 public Person getUser() {
6996 return mUser;
Alex Hillsfc737de2016-03-23 17:33:02 -04006997 }
6998
6999 /**
7000 * Returns the name to be displayed for any replies sent by the user
Selim Cinekcb8b9852017-12-15 18:01:52 -08007001 *
7002 * @deprecated use {@link #getUser()} instead
Alex Hillsfc737de2016-03-23 17:33:02 -04007003 */
7004 public CharSequence getUserDisplayName() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007005 return mUser.getName();
Alex Hillsfc737de2016-03-23 17:33:02 -04007006 }
7007
7008 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007009 * Sets the title to be displayed on this conversation. May be set to {@code null}.
7010 *
Kodlee Yin14656422017-12-22 17:00:46 -08007011 * <p>This API's behavior was changed in SDK version {@link Build.VERSION_CODES#P}. If your
7012 * application's target version is less than {@link Build.VERSION_CODES#P}, setting a
7013 * conversation title to a non-null value will make {@link #isGroupConversation()} return
7014 * {@code true} and passing {@code null} will make it return {@code false}. In
7015 * {@link Build.VERSION_CODES#P} and beyond, use {@link #setGroupConversation(boolean)}
7016 * to set group conversation status.
7017 *
7018 * @param conversationTitle Title displayed for this conversation
7019 * @return this object for method chaining
Alex Hillsfc737de2016-03-23 17:33:02 -04007020 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007021 public MessagingStyle setConversationTitle(@Nullable CharSequence conversationTitle) {
Alex Hillsfc737de2016-03-23 17:33:02 -04007022 mConversationTitle = conversationTitle;
7023 return this;
7024 }
7025
7026 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007027 * Return the title to be displayed on this conversation. May return {@code null}.
Alex Hillsfc737de2016-03-23 17:33:02 -04007028 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007029 @Nullable
Alex Hillsfc737de2016-03-23 17:33:02 -04007030 public CharSequence getConversationTitle() {
7031 return mConversationTitle;
7032 }
7033
7034 /**
7035 * Adds a message for display by this notification. Convenience call for a simple
7036 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
7037 * @param text A {@link CharSequence} to be displayed as the message content
7038 * @param timestamp Time at which the message arrived
7039 * @param sender A {@link CharSequence} to be used for displaying the name of the
7040 * sender. Should be <code>null</code> for messages by the current user, in which case
7041 * the platform will insert {@link #getUserDisplayName()}.
7042 * Should be unique amongst all individuals in the conversation, and should be
7043 * consistent during re-posts of the notification.
7044 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07007045 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Alex Hillsfc737de2016-03-23 17:33:02 -04007046 *
7047 * @return this object for method chaining
Selim Cinekcb8b9852017-12-15 18:01:52 -08007048 *
7049 * @deprecated use {@link #addMessage(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04007050 */
7051 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007052 return addMessage(text, timestamp,
Selim Cinek9acd6732018-03-23 16:39:02 -07007053 sender == null ? null : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08007054 }
7055
7056 /**
7057 * Adds a message for display by this notification. Convenience call for a simple
7058 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
7059 * @param text A {@link CharSequence} to be displayed as the message content
7060 * @param timestamp Time at which the message arrived
7061 * @param sender The {@link Person} who sent the message.
7062 * Should be <code>null</code> for messages by the current user, in which case
7063 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7064 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07007065 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Selim Cinekcb8b9852017-12-15 18:01:52 -08007066 *
7067 * @return this object for method chaining
7068 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007069 public MessagingStyle addMessage(@NonNull CharSequence text, long timestamp,
7070 @Nullable Person sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08007071 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04007072 }
7073
7074 /**
7075 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08007076 *
7077 * <p>The messages should be added in chronologic order, i.e. the oldest first,
7078 * the newest last.
7079 *
Alex Hillsfc737de2016-03-23 17:33:02 -04007080 * @param message The {@link Message} to be displayed
7081 * @return this object for method chaining
7082 */
7083 public MessagingStyle addMessage(Message message) {
7084 mMessages.add(message);
7085 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
7086 mMessages.remove(0);
7087 }
7088 return this;
7089 }
7090
7091 /**
Adrian Roos437cd562017-01-18 15:47:03 -08007092 * Adds a {@link Message} for historic context in this notification.
7093 *
7094 * <p>Messages should be added as historic if they are not the main subject of the
7095 * notification but may give context to a conversation. The system may choose to present
7096 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
7097 *
7098 * <p>The messages should be added in chronologic order, i.e. the oldest first,
7099 * the newest last.
7100 *
7101 * @param message The historic {@link Message} to be added
7102 * @return this object for method chaining
7103 */
7104 public MessagingStyle addHistoricMessage(Message message) {
7105 mHistoricMessages.add(message);
7106 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
7107 mHistoricMessages.remove(0);
7108 }
7109 return this;
7110 }
7111
7112 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007113 * Gets the list of {@code Message} objects that represent the notification
7114 */
7115 public List<Message> getMessages() {
7116 return mMessages;
7117 }
7118
7119 /**
Adrian Roos437cd562017-01-18 15:47:03 -08007120 * Gets the list of historic {@code Message}s in the notification.
7121 */
7122 public List<Message> getHistoricMessages() {
7123 return mHistoricMessages;
7124 }
7125
7126 /**
Selim Cinekce8794f2018-05-23 16:46:05 -07007127 * Sets whether this conversation notification represents a group. If the app is targeting
7128 * Android P, this is required if the app wants to display the largeIcon set with
7129 * {@link Notification.Builder#setLargeIcon(Bitmap)}, otherwise it will be hidden.
Kodlee Yin14656422017-12-22 17:00:46 -08007130 *
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007131 * @param isGroupConversation {@code true} if the conversation represents a group,
7132 * {@code false} otherwise.
7133 * @return this object for method chaining
7134 */
7135 public MessagingStyle setGroupConversation(boolean isGroupConversation) {
7136 mIsGroupConversation = isGroupConversation;
7137 return this;
7138 }
7139
7140 /**
Kodlee Yin14656422017-12-22 17:00:46 -08007141 * Returns {@code true} if this notification represents a group conversation, otherwise
7142 * {@code false}.
7143 *
7144 * <p> If the application that generated this {@link MessagingStyle} targets an SDK version
7145 * less than {@link Build.VERSION_CODES#P}, this method becomes dependent on whether or
7146 * not the conversation title is set; returning {@code true} if the conversation title is
7147 * a non-null value, or {@code false} otherwise. From {@link Build.VERSION_CODES#P} forward,
7148 * this method returns what's set by {@link #setGroupConversation(boolean)} allowing for
7149 * named, non-group conversations.
7150 *
7151 * @see #setConversationTitle(CharSequence)
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007152 */
7153 public boolean isGroupConversation() {
Kodlee Yin14656422017-12-22 17:00:46 -08007154 // When target SDK version is < P, a non-null conversation title dictates if this is
7155 // as group conversation.
7156 if (mBuilder != null
7157 && mBuilder.mContext.getApplicationInfo().targetSdkVersion
7158 < Build.VERSION_CODES.P) {
7159 return mConversationTitle != null;
7160 }
7161
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007162 return mIsGroupConversation;
7163 }
7164
7165 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007166 * @hide
7167 */
7168 @Override
7169 public void addExtras(Bundle extras) {
7170 super.addExtras(extras);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007171 if (mUser != null) {
7172 // For legacy usages
7173 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUser.getName());
7174 extras.putParcelable(EXTRA_MESSAGING_PERSON, mUser);
Alex Hillsfc737de2016-03-23 17:33:02 -04007175 }
7176 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007177 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04007178 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007179 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
7180 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04007181 }
Adrian Roos437cd562017-01-18 15:47:03 -08007182 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
7183 Message.getBundleArrayForMessages(mHistoricMessages));
7184 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007185
7186 fixTitleAndTextExtras(extras);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007187 extras.putBoolean(EXTRA_IS_GROUP_CONVERSATION, mIsGroupConversation);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007188 }
7189
7190 private void fixTitleAndTextExtras(Bundle extras) {
7191 Message m = findLatestIncomingMessage();
7192 CharSequence text = (m == null) ? null : m.mText;
7193 CharSequence sender = m == null ? null
Selim Cinekcb8b9852017-12-15 18:01:52 -08007194 : m.mSender == null || TextUtils.isEmpty(m.mSender.getName())
7195 ? mUser.getName() : m.mSender.getName();
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007196 CharSequence title;
7197 if (!TextUtils.isEmpty(mConversationTitle)) {
7198 if (!TextUtils.isEmpty(sender)) {
7199 BidiFormatter bidi = BidiFormatter.getInstance();
7200 title = mBuilder.mContext.getString(
7201 com.android.internal.R.string.notification_messaging_title_template,
Selim Cinekcb8b9852017-12-15 18:01:52 -08007202 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(sender));
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007203 } else {
7204 title = mConversationTitle;
7205 }
7206 } else {
7207 title = sender;
7208 }
7209
7210 if (title != null) {
7211 extras.putCharSequence(EXTRA_TITLE, title);
7212 }
7213 if (text != null) {
7214 extras.putCharSequence(EXTRA_TEXT, text);
7215 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007216 }
7217
7218 /**
7219 * @hide
7220 */
7221 @Override
7222 protected void restoreFromExtras(Bundle extras) {
7223 super.restoreFromExtras(extras);
7224
Selim Cinekcb8b9852017-12-15 18:01:52 -08007225 mUser = extras.getParcelable(EXTRA_MESSAGING_PERSON);
7226 if (mUser == null) {
7227 CharSequence displayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
Selim Cinek9acd6732018-03-23 16:39:02 -07007228 mUser = new Person.Builder().setName(displayName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007229 }
Adrian Roos96b7e202016-05-17 13:50:38 -07007230 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08007231 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007232 mMessages = Message.getMessagesFromBundleArray(messages);
Adrian Roos437cd562017-01-18 15:47:03 -08007233 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007234 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007235 mIsGroupConversation = extras.getBoolean(EXTRA_IS_GROUP_CONVERSATION);
Alex Hillsfc737de2016-03-23 17:33:02 -04007236 }
7237
7238 /**
7239 * @hide
7240 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07007241 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007242 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07007243 mBuilder.mOriginalActions = mBuilder.mActions;
7244 mBuilder.mActions = new ArrayList<>();
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007245 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007246 false /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007247 mBuilder.mActions = mBuilder.mOriginalActions;
7248 mBuilder.mOriginalActions = null;
7249 return remoteViews;
Adrian Roosc1a80b02016-04-05 14:54:55 -07007250 }
7251
Julia Reynolds7217dc92018-03-07 12:12:09 -05007252 /**
7253 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04007254 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05007255 */
7256 @Override
7257 public boolean areNotificationsVisiblyDifferent(Style other) {
7258 if (other == null || getClass() != other.getClass()) {
7259 return true;
7260 }
7261 MessagingStyle newS = (MessagingStyle) other;
7262 List<MessagingStyle.Message> oldMs = getMessages();
7263 List<MessagingStyle.Message> newMs = newS.getMessages();
7264
Dan Sandler7d67bd42018-05-15 14:06:38 -04007265 if (oldMs == null || newMs == null) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007266 newMs = new ArrayList<>();
7267 }
7268
7269 int n = oldMs.size();
7270 if (n != newMs.size()) {
7271 return true;
7272 }
7273 for (int i = 0; i < n; i++) {
7274 MessagingStyle.Message oldM = oldMs.get(i);
7275 MessagingStyle.Message newM = newMs.get(i);
Dan Sandler7d67bd42018-05-15 14:06:38 -04007276 if (!Objects.equals(
7277 String.valueOf(oldM.getText()),
7278 String.valueOf(newM.getText()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007279 return true;
7280 }
7281 if (!Objects.equals(oldM.getDataUri(), newM.getDataUri())) {
7282 return true;
7283 }
Dan Sandler7d67bd42018-05-15 14:06:38 -04007284 String oldSender = String.valueOf(oldM.getSenderPerson() == null
7285 ? oldM.getSender()
7286 : oldM.getSenderPerson().getName());
7287 String newSender = String.valueOf(newM.getSenderPerson() == null
7288 ? newM.getSender()
7289 : newM.getSenderPerson().getName());
Julia Reynolds7217dc92018-03-07 12:12:09 -05007290 if (!Objects.equals(oldSender, newSender)) {
7291 return true;
7292 }
7293
7294 String oldKey = oldM.getSenderPerson() == null
7295 ? null : oldM.getSenderPerson().getKey();
7296 String newKey = newM.getSenderPerson() == null
7297 ? null : newM.getSenderPerson().getKey();
7298 if (!Objects.equals(oldKey, newKey)) {
7299 return true;
7300 }
7301 // Other fields (like timestamp) intentionally excluded
7302 }
7303 return false;
7304 }
7305
Adrian Roosc1a80b02016-04-05 14:54:55 -07007306 private Message findLatestIncomingMessage() {
Selim Cinek88188f22017-09-19 16:46:56 -07007307 return findLatestIncomingMessage(mMessages);
7308 }
7309
7310 /**
7311 * @hide
7312 */
7313 @Nullable
7314 public static Message findLatestIncomingMessage(
7315 List<Message> messages) {
7316 for (int i = messages.size() - 1; i >= 0; i--) {
7317 Message m = messages.get(i);
Adrian Roosc1a80b02016-04-05 14:54:55 -07007318 // Incoming messages have a non-empty sender.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007319 if (m.mSender != null && !TextUtils.isEmpty(m.mSender.getName())) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07007320 return m;
7321 }
7322 }
Selim Cinek88188f22017-09-19 16:46:56 -07007323 if (!messages.isEmpty()) {
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007324 // No incoming messages, fall back to outgoing message
Selim Cinek88188f22017-09-19 16:46:56 -07007325 return messages.get(messages.size() - 1);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007326 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07007327 return null;
7328 }
7329
7330 /**
7331 * @hide
7332 */
7333 @Override
7334 public RemoteViews makeBigContentView() {
Selim Cinek384804b2018-04-18 14:31:07 +08007335 return makeMessagingView(false /* displayImagesAtEnd */, true /* hideLargeIcon */);
Selim Cinekafeed292017-12-12 17:32:44 -08007336 }
7337
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007338 /**
7339 * Create a messaging layout.
7340 *
7341 * @param displayImagesAtEnd should images be displayed at the end of the content instead
7342 * of inline.
Selim Cinek384804b2018-04-18 14:31:07 +08007343 * @param hideRightIcons Should the reply affordance be shown at the end of the notification
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007344 * @return the created remoteView.
7345 */
Selim Cinekafeed292017-12-12 17:32:44 -08007346 @NonNull
Selim Cinek384804b2018-04-18 14:31:07 +08007347 private RemoteViews makeMessagingView(boolean displayImagesAtEnd, boolean hideRightIcons) {
Selim Cinek88188f22017-09-19 16:46:56 -07007348 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
Adrian Roosc1a80b02016-04-05 14:54:55 -07007349 ? super.mBigContentTitle
7350 : mConversationTitle;
Selim Cinekce8794f2018-05-23 16:46:05 -07007351 boolean atLeastP = mBuilder.mContext.getApplicationInfo().targetSdkVersion
7352 >= Build.VERSION_CODES.P;
7353 boolean isOneToOne;
Selim Cinek2dd3e722018-01-19 11:06:06 -08007354 CharSequence nameReplacement = null;
Selim Cinekce8794f2018-05-23 16:46:05 -07007355 Icon avatarReplacement = null;
7356 if (!atLeastP) {
7357 isOneToOne = TextUtils.isEmpty(conversationTitle);
7358 avatarReplacement = mBuilder.mN.mLargeIcon;
7359 if (hasOnlyWhiteSpaceSenders()) {
7360 isOneToOne = true;
7361 nameReplacement = conversationTitle;
7362 conversationTitle = null;
7363 }
7364 } else {
7365 isOneToOne = !isGroupConversation();
Adrian Roosb1f427c2016-05-26 12:27:15 -07007366 }
Selim Cinek384804b2018-04-18 14:31:07 +08007367 TemplateBindResult bindResult = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007368 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).title(
7369 conversationTitle).text(null)
7370 .hideLargeIcon(hideRightIcons || isOneToOne)
7371 .hideReplyIcon(hideRightIcons)
7372 .headerTextSecondary(conversationTitle);
Adrian Roos48d746a2016-04-12 14:57:28 -07007373 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07007374 mBuilder.getMessagingLayoutResource(),
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007375 p,
Selim Cinek384804b2018-04-18 14:31:07 +08007376 bindResult);
Selim Cinek88188f22017-09-19 16:46:56 -07007377 addExtras(mBuilder.mN.extras);
Selim Cinekafeed292017-12-12 17:32:44 -08007378 // also update the end margin if there is an image
Selim Cinek1c72fa02018-04-23 18:00:54 +08007379 contentView.setViewLayoutMarginEnd(R.id.notification_messaging,
7380 bindResult.getIconMarginEnd());
Selim Cinek88188f22017-09-19 16:46:56 -07007381 contentView.setInt(R.id.status_bar_latest_event_content, "setLayoutColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007382 mBuilder.isColorized(p) ? mBuilder.getPrimaryTextColor(p)
7383 : mBuilder.resolveContrastColor(p));
Kenny Guy14d035c2018-05-02 19:10:36 +01007384 contentView.setInt(R.id.status_bar_latest_event_content, "setSenderTextColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007385 mBuilder.getPrimaryTextColor(p));
Kenny Guy14d035c2018-05-02 19:10:36 +01007386 contentView.setInt(R.id.status_bar_latest_event_content, "setMessageTextColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007387 mBuilder.getSecondaryTextColor(p));
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007388 contentView.setBoolean(R.id.status_bar_latest_event_content, "setDisplayImagesAtEnd",
7389 displayImagesAtEnd);
Selim Cinekce8794f2018-05-23 16:46:05 -07007390 contentView.setIcon(R.id.status_bar_latest_event_content, "setAvatarReplacement",
7391 avatarReplacement);
Selim Cinek2dd3e722018-01-19 11:06:06 -08007392 contentView.setCharSequence(R.id.status_bar_latest_event_content, "setNameReplacement",
7393 nameReplacement);
Selim Cinek88188f22017-09-19 16:46:56 -07007394 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsOneToOne",
7395 isOneToOne);
7396 contentView.setBundle(R.id.status_bar_latest_event_content, "setData",
7397 mBuilder.mN.extras);
Alex Hillsfc737de2016-03-23 17:33:02 -04007398 return contentView;
7399 }
7400
Selim Cinekf7409db2017-10-24 16:17:14 -07007401 private boolean hasOnlyWhiteSpaceSenders() {
7402 for (int i = 0; i < mMessages.size(); i++) {
7403 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007404 Person sender = m.getSenderPerson();
7405 if (sender != null && !isWhiteSpace(sender.getName())) {
Selim Cinekf7409db2017-10-24 16:17:14 -07007406 return false;
7407 }
7408 }
7409 return true;
7410 }
7411
7412 private boolean isWhiteSpace(CharSequence sender) {
7413 if (TextUtils.isEmpty(sender)) {
7414 return true;
7415 }
7416 if (sender.toString().matches("^\\s*$")) {
7417 return true;
7418 }
7419 // Let's check if we only have 0 whitespace chars. Some apps did this as a workaround
7420 // For the presentation that we had.
7421 for (int i = 0; i < sender.length(); i++) {
7422 char c = sender.charAt(i);
7423 if (c != '\u200B') {
7424 return false;
7425 }
7426 }
7427 return true;
7428 }
7429
Selim Cinek88188f22017-09-19 16:46:56 -07007430 private CharSequence createConversationTitleFromMessages() {
7431 ArraySet<CharSequence> names = new ArraySet<>();
7432 for (int i = 0; i < mMessages.size(); i++) {
7433 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007434 Person sender = m.getSenderPerson();
Selim Cinek88188f22017-09-19 16:46:56 -07007435 if (sender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007436 names.add(sender.getName());
Selim Cinek88188f22017-09-19 16:46:56 -07007437 }
7438 }
7439 SpannableStringBuilder title = new SpannableStringBuilder();
7440 int size = names.size();
7441 for (int i = 0; i < size; i++) {
7442 CharSequence name = names.valueAt(i);
7443 if (!TextUtils.isEmpty(title)) {
7444 title.append(", ");
7445 }
7446 title.append(BidiFormatter.getInstance().unicodeWrap(name));
7447 }
7448 return title;
7449 }
7450
Adrian Roosdedd1df2016-04-26 16:38:47 -07007451 /**
7452 * @hide
7453 */
7454 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007455 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007456 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007457 true /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007458 remoteViews.setInt(R.id.notification_messaging, "setMaxDisplayedLines", 1);
7459 return remoteViews;
Adrian Roosdedd1df2016-04-26 16:38:47 -07007460 }
7461
Adrian Roosc1a80b02016-04-05 14:54:55 -07007462 private static TextAppearanceSpan makeFontColorSpan(int color) {
7463 return new TextAppearanceSpan(null, 0, 0,
7464 ColorStateList.valueOf(color), null);
7465 }
7466
Alex Hillsd9b04d92016-04-11 16:38:16 -04007467 public static final class Message {
Tony Mak09db2ea2018-06-27 18:12:48 +01007468 /** @hide */
7469 public static final String KEY_TEXT = "text";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007470 static final String KEY_TIMESTAMP = "time";
7471 static final String KEY_SENDER = "sender";
Selim Cinekcb8b9852017-12-15 18:01:52 -08007472 static final String KEY_SENDER_PERSON = "sender_person";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007473 static final String KEY_DATA_MIME_TYPE = "type";
7474 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08007475 static final String KEY_EXTRAS_BUNDLE = "extras";
Kenny Guya0f6de82018-04-06 16:20:16 +01007476 static final String KEY_REMOTE_INPUT_HISTORY = "remote_input_history";
Alex Hillsfc737de2016-03-23 17:33:02 -04007477
7478 private final CharSequence mText;
7479 private final long mTimestamp;
Selim Cinekcb8b9852017-12-15 18:01:52 -08007480 @Nullable
7481 private final Person mSender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007482 /** True if this message was generated from the extra
7483 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}
7484 */
7485 private final boolean mRemoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007486
Shane Brennan5a871862017-03-11 13:14:17 -08007487 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007488 private String mDataMimeType;
7489 private Uri mDataUri;
7490
7491 /**
7492 * Constructor
7493 * @param text A {@link CharSequence} to be displayed as the message content
7494 * @param timestamp Time at which the message arrived
7495 * @param sender A {@link CharSequence} to be used for displaying the name of the
7496 * sender. Should be <code>null</code> for messages by the current user, in which case
7497 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
7498 * Should be unique amongst all individuals in the conversation, and should be
7499 * consistent during re-posts of the notification.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007500 *
7501 * @deprecated use {@code Message(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04007502 */
7503 public Message(CharSequence text, long timestamp, CharSequence sender){
Selim Cinek9acd6732018-03-23 16:39:02 -07007504 this(text, timestamp, sender == null ? null
7505 : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08007506 }
7507
7508 /**
7509 * Constructor
7510 * @param text A {@link CharSequence} to be displayed as the message content
7511 * @param timestamp Time at which the message arrived
7512 * @param sender The {@link Person} who sent the message.
7513 * Should be <code>null</code> for messages by the current user, in which case
7514 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7515 * <p>
Selim Cinek9acd6732018-03-23 16:39:02 -07007516 * The person provided should contain an Icon, set with
7517 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7518 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7519 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7520 * to differentiate between the different users.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007521 * </p>
7522 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007523 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender) {
Kenny Guya0f6de82018-04-06 16:20:16 +01007524 this(text, timestamp, sender, false /* remoteHistory */);
7525 }
7526
7527 /**
7528 * Constructor
7529 * @param text A {@link CharSequence} to be displayed as the message content
7530 * @param timestamp Time at which the message arrived
7531 * @param sender The {@link Person} who sent the message.
7532 * Should be <code>null</code> for messages by the current user, in which case
7533 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7534 * @param remoteInputHistory True if the messages was generated from the extra
7535 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7536 * <p>
7537 * The person provided should contain an Icon, set with
7538 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7539 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7540 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7541 * to differentiate between the different users.
7542 * </p>
7543 * @hide
7544 */
7545 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender,
7546 boolean remoteInputHistory) {
Alex Hillsfc737de2016-03-23 17:33:02 -04007547 mText = text;
7548 mTimestamp = timestamp;
7549 mSender = sender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007550 mRemoteInputHistory = remoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007551 }
7552
7553 /**
7554 * Sets a binary blob of data and an associated MIME type for a message. In the case
7555 * where the platform doesn't support the MIME type, the original text provided in the
7556 * constructor will be used.
7557 * @param dataMimeType The MIME type of the content. See
7558 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
7559 * types on Android and Android Wear.
7560 * @param dataUri The uri containing the content whose type is given by the MIME type.
7561 * <p class="note">
7562 * <ol>
7563 * <li>Notification Listeners including the System UI need permission to access the
7564 * data the Uri points to. The recommended ways to do this are:</li>
7565 * <li>Store the data in your own ContentProvider, making sure that other apps have
7566 * the correct permission to access your provider. The preferred mechanism for
7567 * providing access is to use per-URI permissions which are temporary and only
7568 * grant access to the receiving application. An easy way to create a
7569 * ContentProvider like this is to use the FileProvider helper class.</li>
7570 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
7571 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
7572 * also store non-media types (see MediaStore.Files for more info). Files can be
7573 * inserted into the MediaStore using scanFile() after which a content:// style
7574 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
7575 * Note that once added to the system MediaStore the content is accessible to any
7576 * app on the device.</li>
7577 * </ol>
7578 * @return this object for method chaining
7579 */
7580 public Message setData(String dataMimeType, Uri dataUri) {
7581 mDataMimeType = dataMimeType;
7582 mDataUri = dataUri;
7583 return this;
7584 }
7585
Alex Hillsfc737de2016-03-23 17:33:02 -04007586 /**
7587 * Get the text to be used for this message, or the fallback text if a type and content
7588 * Uri have been set
7589 */
7590 public CharSequence getText() {
7591 return mText;
7592 }
7593
7594 /**
7595 * Get the time at which this message arrived
7596 */
7597 public long getTimestamp() {
7598 return mTimestamp;
7599 }
7600
7601 /**
Shane Brennan5a871862017-03-11 13:14:17 -08007602 * Get the extras Bundle for this message.
7603 */
7604 public Bundle getExtras() {
7605 return mExtras;
7606 }
7607
7608 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007609 * Get the text used to display the contact's name in the messaging experience
Selim Cinekcb8b9852017-12-15 18:01:52 -08007610 *
7611 * @deprecated use {@link #getSenderPerson()}
Alex Hillsfc737de2016-03-23 17:33:02 -04007612 */
7613 public CharSequence getSender() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007614 return mSender == null ? null : mSender.getName();
7615 }
7616
7617 /**
7618 * Get the sender associated with this message.
7619 */
7620 @Nullable
7621 public Person getSenderPerson() {
Alex Hillsfc737de2016-03-23 17:33:02 -04007622 return mSender;
7623 }
7624
7625 /**
7626 * Get the MIME type of the data pointed to by the Uri
7627 */
7628 public String getDataMimeType() {
7629 return mDataMimeType;
7630 }
7631
7632 /**
koprivaa1a78482018-10-09 10:09:23 -07007633 * Get the Uri pointing to the content of the message. Can be null, in which case
Alex Hillsfc737de2016-03-23 17:33:02 -04007634 * {@see #getText()} is used.
7635 */
7636 public Uri getDataUri() {
7637 return mDataUri;
7638 }
7639
Kenny Guya0f6de82018-04-06 16:20:16 +01007640 /**
7641 * @return True if the message was generated from
7642 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7643 * @hide
7644 */
7645 public boolean isRemoteInputHistory() {
7646 return mRemoteInputHistory;
7647 }
7648
Beverlye98937a2018-11-15 10:18:57 -05007649 /**
7650 * @hide
7651 */
7652 @VisibleForTesting
7653 public Bundle toBundle() {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007654 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007655 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007656 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04007657 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007658 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04007659 if (mSender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007660 // Legacy listeners need this
7661 bundle.putCharSequence(KEY_SENDER, mSender.getName());
7662 bundle.putParcelable(KEY_SENDER_PERSON, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04007663 }
7664 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007665 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04007666 }
7667 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007668 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04007669 }
Shane Brennan5a871862017-03-11 13:14:17 -08007670 if (mExtras != null) {
7671 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
7672 }
Kenny Guya0f6de82018-04-06 16:20:16 +01007673 if (mRemoteInputHistory) {
7674 bundle.putBoolean(KEY_REMOTE_INPUT_HISTORY, mRemoteInputHistory);
7675 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007676 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04007677 }
7678
Alex Hillsd9b04d92016-04-11 16:38:16 -04007679 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
7680 Bundle[] bundles = new Bundle[messages.size()];
7681 final int N = messages.size();
7682 for (int i = 0; i < N; i++) {
7683 bundles[i] = messages.get(i).toBundle();
7684 }
7685 return bundles;
7686 }
7687
Selim Cinek88188f22017-09-19 16:46:56 -07007688 /**
7689 * @return A list of messages read from the bundles.
7690 *
7691 * @hide
7692 */
7693 public static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
7694 if (bundles == null) {
7695 return new ArrayList<>();
7696 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007697 List<Message> messages = new ArrayList<>(bundles.length);
7698 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07007699 if (bundles[i] instanceof Bundle) {
7700 Message message = getMessageFromBundle((Bundle)bundles[i]);
7701 if (message != null) {
7702 messages.add(message);
7703 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007704 }
7705 }
7706 return messages;
7707 }
7708
Selim Cinekb0dc61b2018-05-22 18:49:36 -07007709 /**
7710 * @return The message that is stored in the bundle or null if the message couldn't be
7711 * resolved.
7712 *
7713 * @hide
7714 */
7715 @Nullable
7716 public static Message getMessageFromBundle(Bundle bundle) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007717 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07007718 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007719 return null;
7720 } else {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007721
7722 Person senderPerson = bundle.getParcelable(KEY_SENDER_PERSON);
7723 if (senderPerson == null) {
7724 // Legacy apps that use compat don't actually provide the sender objects
7725 // We need to fix the compat version to provide people / use
7726 // the native api instead
7727 CharSequence senderName = bundle.getCharSequence(KEY_SENDER);
7728 if (senderName != null) {
Selim Cinek9acd6732018-03-23 16:39:02 -07007729 senderPerson = new Person.Builder().setName(senderName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007730 }
7731 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007732 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
Selim Cinekcb8b9852017-12-15 18:01:52 -08007733 bundle.getLong(KEY_TIMESTAMP),
Kenny Guya0f6de82018-04-06 16:20:16 +01007734 senderPerson,
7735 bundle.getBoolean(KEY_REMOTE_INPUT_HISTORY, false));
Alex Hillsd9b04d92016-04-11 16:38:16 -04007736 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
7737 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007738 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
7739 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04007740 }
Shane Brennan5a871862017-03-11 13:14:17 -08007741 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
7742 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
7743 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007744 return message;
7745 }
7746 } catch (ClassCastException e) {
7747 return null;
7748 }
7749 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007750 }
7751 }
7752
7753 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04007754 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08007755 *
Robert Ly91c5ce32014-06-08 15:37:00 -07007756 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04007757 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07007758 * Notification notif = new Notification.Builder(mContext)
7759 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
7760 * .setContentText(subject)
7761 * .setSmallIcon(R.drawable.new_mail)
7762 * .setLargeIcon(aBitmap)
7763 * .setStyle(new Notification.InboxStyle()
7764 * .addLine(str1)
7765 * .addLine(str2)
7766 * .setContentTitle(&quot;&quot;)
7767 * .setSummaryText(&quot;+3 more&quot;))
7768 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04007769 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08007770 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04007771 * @see Notification#bigContentView
7772 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007773 public static class InboxStyle extends Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07007774
7775 /**
7776 * The number of lines of remote input history allowed until we start reducing lines.
7777 */
7778 private static final int NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION = 1;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007779 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
7780
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007781 public InboxStyle() {
7782 }
7783
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007784 /**
7785 * @deprecated use {@code InboxStyle()}.
7786 */
7787 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04007788 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007789 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04007790 }
7791
Chris Wrend6297db2012-05-03 16:20:13 -04007792 /**
7793 * Overrides ContentTitle in the big form of the template.
7794 * This defaults to the value passed to setContentTitle().
7795 */
7796 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007797 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04007798 return this;
7799 }
7800
7801 /**
7802 * Set the first line of text after the detail section in the big form of the template.
7803 */
7804 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007805 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04007806 return this;
7807 }
7808
Chris Wren0bd664d2012-08-01 13:56:56 -04007809 /**
7810 * Append a line to the digest section of the Inbox notification.
7811 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04007812 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007813 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04007814 return this;
7815 }
7816
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007817 /**
7818 * @hide
7819 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05007820 public ArrayList<CharSequence> getLines() {
7821 return mTexts;
7822 }
7823
7824 /**
7825 * @hide
7826 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007827 public void addExtras(Bundle extras) {
7828 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007829
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007830 CharSequence[] a = new CharSequence[mTexts.size()];
7831 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
7832 }
7833
Christoph Studer4600f9b2014-07-22 22:44:43 +02007834 /**
7835 * @hide
7836 */
7837 @Override
7838 protected void restoreFromExtras(Bundle extras) {
7839 super.restoreFromExtras(extras);
7840
7841 mTexts.clear();
7842 if (extras.containsKey(EXTRA_TEXT_LINES)) {
7843 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
7844 }
7845 }
7846
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007847 /**
7848 * @hide
7849 */
7850 public RemoteViews makeBigContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007851 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder).text(null);
Selim Cinek384804b2018-04-18 14:31:07 +08007852 TemplateBindResult result = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007853 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource(), p, result);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007854
Chris Wrend6297db2012-05-03 16:20:13 -04007855 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 -04007856 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04007857
Chris Wren4ed80d52012-05-17 09:30:03 -04007858 // Make sure all rows are gone in case we reuse a view.
7859 for (int rowId : rowIds) {
7860 contentView.setViewVisibility(rowId, View.GONE);
7861 }
7862
Daniel Sandler879c5e02012-04-17 16:46:51 -04007863 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07007864 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7865 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08007866 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07007867 int onlyViewId = 0;
7868 int maxRows = rowIds.length;
7869 if (mBuilder.mActions.size() > 0) {
7870 maxRows--;
7871 }
Selim Cinekbee4e072018-05-21 22:06:43 -07007872 CharSequence[] remoteInputHistory = mBuilder.mN.extras.getCharSequenceArray(
7873 EXTRA_REMOTE_INPUT_HISTORY);
7874 if (remoteInputHistory != null
7875 && remoteInputHistory.length > NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION) {
7876 // Let's remove some messages to make room for the remote input history.
7877 // 1 is always able to fit, but let's remove them if they are 2 or 3
7878 int numRemoteInputs = Math.min(remoteInputHistory.length,
7879 MAX_REMOTE_INPUT_HISTORY_LINES);
7880 int totalNumRows = mTexts.size() + numRemoteInputs
7881 - NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION;
7882 if (totalNumRows > maxRows) {
7883 int overflow = totalNumRows - maxRows;
7884 if (mTexts.size() > maxRows) {
7885 // Heuristic: if the Texts don't fit anyway, we'll rather drop the last
7886 // few messages, even with the remote input
7887 maxRows -= overflow;
7888 } else {
7889 // otherwise we drop the first messages
7890 i = overflow;
7891 }
7892 }
7893 }
Selim Cinek07c80172016-04-21 16:40:47 -07007894 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007895 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07007896 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007897 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07007898 contentView.setTextViewText(rowIds[i],
7899 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007900 mBuilder.setTextViewColorSecondary(contentView, rowIds[i], p);
Selim Cinek07c80172016-04-21 16:40:47 -07007901 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek384804b2018-04-18 14:31:07 +08007902 handleInboxImageMargin(contentView, rowIds[i], first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007903 result.getIconMarginEnd());
Selim Cinek07c80172016-04-21 16:40:47 -07007904 if (first) {
7905 onlyViewId = rowIds[i];
7906 } else {
7907 onlyViewId = 0;
7908 }
Selim Cinek247fa012016-02-18 09:50:48 -08007909 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007910 }
7911 i++;
7912 }
Selim Cinek07c80172016-04-21 16:40:47 -07007913 if (onlyViewId != 0) {
7914 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
7915 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7916 R.dimen.notification_text_margin_top);
7917 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
7918 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007919
Daniel Sandler879c5e02012-04-17 16:46:51 -04007920 return contentView;
7921 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007922
Julia Reynolds7217dc92018-03-07 12:12:09 -05007923 /**
7924 * @hide
7925 */
7926 @Override
7927 public boolean areNotificationsVisiblyDifferent(Style other) {
7928 if (other == null || getClass() != other.getClass()) {
7929 return true;
7930 }
7931 InboxStyle newS = (InboxStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04007932
7933 final ArrayList<CharSequence> myLines = getLines();
7934 final ArrayList<CharSequence> newLines = newS.getLines();
7935 final int n = myLines.size();
7936 if (n != newLines.size()) {
7937 return true;
7938 }
7939
7940 for (int i = 0; i < n; i++) {
7941 if (!Objects.equals(
7942 String.valueOf(myLines.get(i)),
7943 String.valueOf(newLines.get(i)))) {
7944 return true;
7945 }
7946 }
7947 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05007948 }
7949
Selim Cinek384804b2018-04-18 14:31:07 +08007950 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007951 int marginEndValue) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08007952 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08007953 if (first) {
7954 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
7955 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
7956 boolean hasProgress = max != 0 || ind;
Selim Cinek384804b2018-04-18 14:31:07 +08007957 if (!hasProgress) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08007958 endMargin = marginEndValue;
Selim Cinek247fa012016-02-18 09:50:48 -08007959 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007960 }
Selim Cinek1c72fa02018-04-23 18:00:54 +08007961 contentView.setViewLayoutMarginEnd(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08007962 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04007963 }
Dan Sandler842dd772014-05-15 09:36:47 -04007964
7965 /**
7966 * Notification style for media playback notifications.
7967 *
7968 * In the expanded form, {@link Notification#bigContentView}, up to 5
7969 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04007970 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04007971 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
7972 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
7973 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08007974 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04007975 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
7976 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01007977 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04007978 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08007979 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007980 * Notifications created with MediaStyle will have their category set to
7981 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
7982 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08007983 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07007984 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
7985 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04007986 * the System UI can identify this as a notification representing an active media session
7987 * and respond accordingly (by showing album artwork in the lockscreen, for example).
7988 *
Selim Cinek99104832017-01-25 14:47:33 -08007989 * <p>
7990 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
7991 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
7992 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
7993 * <p>
7994 *
Dan Sandler842dd772014-05-15 09:36:47 -04007995 * To use this style with your Notification, feed it to
7996 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7997 * <pre class="prettyprint">
7998 * Notification noti = new Notification.Builder()
7999 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01008000 * .setContentTitle(&quot;Track title&quot;)
8001 * .setContentText(&quot;Artist - Album&quot;)
8002 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01008003 * .setStyle(<b>new Notification.MediaStyle()</b>
8004 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04008005 * .build();
8006 * </pre>
8007 *
8008 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08008009 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04008010 */
8011 public static class MediaStyle extends Style {
Gus Prevas9cc96602018-10-11 11:24:23 -04008012 // Changing max media buttons requires also changing templates
8013 // (notification_template_material_media and notification_template_material_big_media).
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008014 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04008015 static final int MAX_MEDIA_BUTTONS = 5;
Gus Prevas9cc96602018-10-11 11:24:23 -04008016 @IdRes private static final int[] MEDIA_BUTTON_IDS = {
8017 R.id.action0,
8018 R.id.action1,
8019 R.id.action2,
8020 R.id.action3,
8021 R.id.action4,
8022 };
Dan Sandler842dd772014-05-15 09:36:47 -04008023
8024 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07008025 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04008026
8027 public MediaStyle() {
8028 }
8029
Adrian Roosf5faf9d2016-05-23 13:56:15 -07008030 /**
8031 * @deprecated use {@code MediaStyle()}.
8032 */
8033 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04008034 public MediaStyle(Builder builder) {
8035 setBuilder(builder);
8036 }
8037
8038 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008039 * 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 -04008040 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008041 *
8042 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04008043 */
8044 public MediaStyle setShowActionsInCompactView(int...actions) {
8045 mActionsToShowInCompact = actions;
8046 return this;
8047 }
8048
8049 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07008050 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
8051 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04008052 */
Jeff Browndba34ba2014-06-24 20:46:03 -07008053 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04008054 mToken = token;
8055 return this;
8056 }
8057
Christoph Studer4600f9b2014-07-22 22:44:43 +02008058 /**
8059 * @hide
8060 */
Dan Sandler842dd772014-05-15 09:36:47 -04008061 @Override
Mathew Inwood61e8ae62018-08-14 14:17:44 +01008062 @UnsupportedAppUsage
Dan Sandler842dd772014-05-15 09:36:47 -04008063 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02008064 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01008065 if (wip.category == null) {
8066 wip.category = Notification.CATEGORY_TRANSPORT;
8067 }
Dan Sandler842dd772014-05-15 09:36:47 -04008068 return wip;
8069 }
8070
Christoph Studer4600f9b2014-07-22 22:44:43 +02008071 /**
8072 * @hide
8073 */
8074 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008075 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04008076 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02008077 }
8078
8079 /**
8080 * @hide
8081 */
8082 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04008083 public RemoteViews makeBigContentView() {
8084 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02008085 }
8086
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008087 /**
8088 * @hide
8089 */
8090 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008091 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008092 RemoteViews expanded = makeMediaBigContentView();
8093 return expanded != null ? expanded : makeMediaContentView();
8094 }
8095
Dan Sandler842dd772014-05-15 09:36:47 -04008096 /** @hide */
8097 @Override
8098 public void addExtras(Bundle extras) {
8099 super.addExtras(extras);
8100
8101 if (mToken != null) {
8102 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
8103 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01008104 if (mActionsToShowInCompact != null) {
8105 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
8106 }
Dan Sandler842dd772014-05-15 09:36:47 -04008107 }
8108
Christoph Studer4600f9b2014-07-22 22:44:43 +02008109 /**
8110 * @hide
8111 */
8112 @Override
8113 protected void restoreFromExtras(Bundle extras) {
8114 super.restoreFromExtras(extras);
8115
8116 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
8117 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
8118 }
8119 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
8120 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
8121 }
8122 }
8123
Julia Reynolds7217dc92018-03-07 12:12:09 -05008124 /**
8125 * @hide
8126 */
8127 @Override
8128 public boolean areNotificationsVisiblyDifferent(Style other) {
8129 if (other == null || getClass() != other.getClass()) {
8130 return true;
8131 }
8132 // All fields to compare are on the Notification object
8133 return false;
8134 }
8135
Gus Prevas9cc96602018-10-11 11:24:23 -04008136 private void bindMediaActionButton(RemoteViews container, @IdRes int buttonId,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008137 Action action, StandardTemplateParams p) {
Dan Sandler842dd772014-05-15 09:36:47 -04008138 final boolean tombstone = (action.actionIntent == null);
Gus Prevas9cc96602018-10-11 11:24:23 -04008139 container.setViewVisibility(buttonId, View.VISIBLE);
8140 container.setImageViewIcon(buttonId, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07008141
8142 // If the action buttons should not be tinted, then just use the default
8143 // notification color. Otherwise, just use the passed-in color.
Gus Prevas9cc96602018-10-11 11:24:23 -04008144 Resources resources = mBuilder.mContext.getResources();
8145 Configuration currentConfig = resources.getConfiguration();
Lucas Dupinf03e7522018-06-25 16:21:13 -07008146 boolean inNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
8147 == Configuration.UI_MODE_NIGHT_YES;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008148 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized(p)
8149 ? getActionColor(p)
Lucas Dupina291d192018-06-07 13:59:42 -07008150 : ContrastColorUtil.resolveColor(mBuilder.mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07008151 Notification.COLOR_DEFAULT, inNightMode);
Anthony Chenad4d1582017-04-10 16:07:58 -07008152
Gus Prevas9cc96602018-10-11 11:24:23 -04008153 container.setDrawableTint(buttonId, false, tintColor,
Sunny Goyal5b153922017-09-21 21:00:36 -07008154 PorterDuff.Mode.SRC_ATOP);
Gus Prevas9cc96602018-10-11 11:24:23 -04008155
8156 final TypedArray typedArray = mBuilder.mContext.obtainStyledAttributes(
8157 new int[]{ android.R.attr.colorControlHighlight });
8158 int rippleAlpha = Color.alpha(typedArray.getColor(0, 0));
8159 typedArray.recycle();
8160 int rippleColor = Color.argb(rippleAlpha, Color.red(tintColor), Color.green(tintColor),
8161 Color.blue(tintColor));
8162 container.setRippleDrawableColor(buttonId, ColorStateList.valueOf(rippleColor));
8163
Dan Sandler842dd772014-05-15 09:36:47 -04008164 if (!tombstone) {
Gus Prevas9cc96602018-10-11 11:24:23 -04008165 container.setOnClickPendingIntent(buttonId, action.actionIntent);
Dan Sandler842dd772014-05-15 09:36:47 -04008166 }
Gus Prevas9cc96602018-10-11 11:24:23 -04008167 container.setContentDescription(buttonId, action.title);
Dan Sandler842dd772014-05-15 09:36:47 -04008168 }
8169
8170 private RemoteViews makeMediaContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008171 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).fillTextsFrom(
8172 mBuilder);
Dan Sandler842dd772014-05-15 09:36:47 -04008173 RemoteViews view = mBuilder.applyStandardTemplate(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008174 R.layout.notification_template_material_media, p,
Selim Cinek384804b2018-04-18 14:31:07 +08008175 null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04008176
8177 final int numActions = mBuilder.mActions.size();
Gus Prevas9cc96602018-10-11 11:24:23 -04008178 final int numActionsToShow = mActionsToShowInCompact == null
Dan Sandler842dd772014-05-15 09:36:47 -04008179 ? 0
8180 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Gus Prevas9cc96602018-10-11 11:24:23 -04008181 if (numActionsToShow > numActions) {
8182 throw new IllegalArgumentException(String.format(
8183 "setShowActionsInCompactView: action %d out of bounds (max %d)",
8184 numActions, numActions - 1));
8185 }
8186 for (int i = 0; i < MAX_MEDIA_BUTTONS_IN_COMPACT; i++) {
8187 if (i < numActionsToShow) {
Dan Sandler842dd772014-05-15 09:36:47 -04008188 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008189 bindMediaActionButton(view, MEDIA_BUTTON_IDS[i], action, p);
Gus Prevas9cc96602018-10-11 11:24:23 -04008190 } else {
8191 view.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04008192 }
8193 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08008194 handleImage(view);
8195 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008196 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07008197 if (mBuilder.mN.hasLargeIcon()) {
Selim Cinek384804b2018-04-18 14:31:07 +08008198 endMargin = R.dimen.notification_media_image_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08008199 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008200 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04008201 return view;
8202 }
8203
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008204 private int getActionColor(StandardTemplateParams p) {
8205 return mBuilder.isColorized(p) ? mBuilder.getPrimaryTextColor(p)
8206 : mBuilder.resolveContrastColor(p);
Selim Cinek99104832017-01-25 14:47:33 -08008207 }
8208
Dan Sandler842dd772014-05-15 09:36:47 -04008209 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008210 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008211 // Dont add an expanded view if there is no more content to be revealed
8212 int actionsInCompact = mActionsToShowInCompact == null
8213 ? 0
8214 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07008215 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008216 return null;
8217 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008218 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).fillTextsFrom(
8219 mBuilder);
Selim Cinek5bf069a2015-11-10 19:14:27 -05008220 RemoteViews big = mBuilder.applyStandardTemplate(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008221 R.layout.notification_template_material_big_media, p , null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04008222
Gus Prevas9cc96602018-10-11 11:24:23 -04008223 for (int i = 0; i < MAX_MEDIA_BUTTONS; i++) {
8224 if (i < actionCount) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008225 bindMediaActionButton(big, MEDIA_BUTTON_IDS[i], mBuilder.mActions.get(i), p);
Gus Prevas9cc96602018-10-11 11:24:23 -04008226 } else {
8227 big.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04008228 }
8229 }
linanson2bcd4032019-01-14 15:22:04 +08008230 bindMediaActionButton(big, R.id.media_seamless, new Action(R.drawable.ic_media_seamless,
8231 mBuilder.mContext.getString(
8232 com.android.internal.R.string.ext_media_seamless_action), null), p);
8233 big.setViewVisibility(R.id.media_seamless, View.GONE);
Selim Cinek5bf069a2015-11-10 19:14:27 -05008234 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04008235 return big;
8236 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008237
Selim Cinek5bf069a2015-11-10 19:14:27 -05008238 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07008239 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008240 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
8241 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008242 }
8243 }
8244
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008245 /**
8246 * @hide
8247 */
8248 @Override
8249 protected boolean hasProgress() {
8250 return false;
8251 }
Dan Sandler842dd772014-05-15 09:36:47 -04008252 }
Griff Hazen61a9e862014-05-22 16:05:19 -07008253
Selim Cinek593610c2016-02-16 18:42:57 -08008254 /**
8255 * Notification style for custom views that are decorated by the system
8256 *
8257 * <p>Instead of providing a notification that is completely custom, a developer can set this
8258 * style and still obtain system decorations like the notification header with the expand
8259 * affordance and actions.
8260 *
8261 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8262 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8263 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8264 * corresponding custom views to display.
8265 *
8266 * To use this style with your Notification, feed it to
8267 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8268 * <pre class="prettyprint">
8269 * Notification noti = new Notification.Builder()
8270 * .setSmallIcon(R.drawable.ic_stat_player)
8271 * .setLargeIcon(albumArtBitmap))
8272 * .setCustomContentView(contentView);
8273 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
8274 * .build();
8275 * </pre>
8276 */
8277 public static class DecoratedCustomViewStyle extends Style {
8278
8279 public DecoratedCustomViewStyle() {
8280 }
8281
Selim Cinek593610c2016-02-16 18:42:57 -08008282 /**
8283 * @hide
8284 */
8285 public boolean displayCustomViewInline() {
8286 return true;
8287 }
8288
8289 /**
8290 * @hide
8291 */
8292 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008293 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008294 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
8295 }
8296
8297 /**
8298 * @hide
8299 */
8300 @Override
8301 public RemoteViews makeBigContentView() {
8302 return makeDecoratedBigContentView();
8303 }
8304
8305 /**
8306 * @hide
8307 */
8308 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008309 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008310 return makeDecoratedHeadsUpContentView();
8311 }
8312
Selim Cinek593610c2016-02-16 18:42:57 -08008313 private RemoteViews makeDecoratedHeadsUpContentView() {
8314 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
8315 ? mBuilder.mN.contentView
8316 : mBuilder.mN.headsUpContentView;
8317 if (mBuilder.mActions.size() == 0) {
8318 return makeStandardTemplateWithCustomContent(headsUpContentView);
8319 }
Selim Cinek384804b2018-04-18 14:31:07 +08008320 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008321 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008322 mBuilder.getBigBaseLayoutResource(), result);
8323 buildIntoRemoteViewContent(remoteViews, headsUpContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008324 return remoteViews;
8325 }
8326
Selim Cinek593610c2016-02-16 18:42:57 -08008327 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
Selim Cinek384804b2018-04-18 14:31:07 +08008328 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008329 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08008330 mBuilder.getBaseLayoutResource(), result);
8331 buildIntoRemoteViewContent(remoteViews, customContent, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008332 return remoteViews;
8333 }
8334
Selim Cinek593610c2016-02-16 18:42:57 -08008335 private RemoteViews makeDecoratedBigContentView() {
8336 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
8337 ? mBuilder.mN.contentView
8338 : mBuilder.mN.bigContentView;
8339 if (mBuilder.mActions.size() == 0) {
8340 return makeStandardTemplateWithCustomContent(bigContentView);
8341 }
Selim Cinek384804b2018-04-18 14:31:07 +08008342 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008343 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008344 mBuilder.getBigBaseLayoutResource(), result);
8345 buildIntoRemoteViewContent(remoteViews, bigContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008346 return remoteViews;
8347 }
Selim Cinek247fa012016-02-18 09:50:48 -08008348
8349 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
Selim Cinek384804b2018-04-18 14:31:07 +08008350 RemoteViews customContent, TemplateBindResult result) {
Selim Cinek6fe4a102019-01-25 14:38:24 -08008351 int childIndex = -1;
Adrian Roos5081c0d2016-02-26 16:04:19 -08008352 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008353 // Need to clone customContent before adding, because otherwise it can no longer be
8354 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008355 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07008356 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
8357 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Sunny Goyalc12d31c2018-11-12 16:29:18 -08008358 remoteViews.addFlags(RemoteViews.FLAG_REAPPLY_DISALLOWED);
Selim Cinek6fe4a102019-01-25 14:38:24 -08008359 childIndex = 0;
Adrian Roos5081c0d2016-02-26 16:04:19 -08008360 }
Selim Cinek6fe4a102019-01-25 14:38:24 -08008361 remoteViews.setIntTag(R.id.notification_main_column,
8362 com.android.internal.R.id.notification_custom_view_index_tag,
8363 childIndex);
Selim Cinek247fa012016-02-18 09:50:48 -08008364 // also update the end margin if there is an image
Selim Cinek384804b2018-04-18 14:31:07 +08008365 Resources resources = mBuilder.mContext.getResources();
8366 int endMargin = resources.getDimensionPixelSize(
Selim Cinek1c72fa02018-04-23 18:00:54 +08008367 R.dimen.notification_content_margin_end) + result.getIconMarginEnd();
Selim Cinek384804b2018-04-18 14:31:07 +08008368 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08008369 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05008370
8371 /**
8372 * @hide
8373 */
8374 @Override
8375 public boolean areNotificationsVisiblyDifferent(Style other) {
8376 if (other == null || getClass() != other.getClass()) {
8377 return true;
8378 }
8379 // Comparison done for all custom RemoteViews, independent of style
8380 return false;
8381 }
Selim Cinek593610c2016-02-16 18:42:57 -08008382 }
8383
Selim Cinek03eb3b72016-02-18 10:39:45 -08008384 /**
8385 * Notification style for media custom views that are decorated by the system
8386 *
8387 * <p>Instead of providing a media notification that is completely custom, a developer can set
8388 * this style and still obtain system decorations like the notification header with the expand
8389 * affordance and actions.
8390 *
8391 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8392 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8393 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8394 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08008395 * <p>
8396 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
8397 * notification by using {@link Notification.Builder#setColorized(boolean)}.
8398 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08008399 * To use this style with your Notification, feed it to
8400 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8401 * <pre class="prettyprint">
8402 * Notification noti = new Notification.Builder()
8403 * .setSmallIcon(R.drawable.ic_stat_player)
8404 * .setLargeIcon(albumArtBitmap))
8405 * .setCustomContentView(contentView);
8406 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
8407 * .setMediaSession(mySession))
8408 * .build();
8409 * </pre>
8410 *
8411 * @see android.app.Notification.DecoratedCustomViewStyle
8412 * @see android.app.Notification.MediaStyle
8413 */
8414 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
8415
8416 public DecoratedMediaCustomViewStyle() {
8417 }
8418
Selim Cinek03eb3b72016-02-18 10:39:45 -08008419 /**
8420 * @hide
8421 */
8422 public boolean displayCustomViewInline() {
8423 return true;
8424 }
8425
8426 /**
8427 * @hide
8428 */
8429 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008430 public RemoteViews makeContentView(boolean increasedHeight) {
8431 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008432 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8433 mBuilder.mN.contentView);
8434 }
8435
8436 /**
8437 * @hide
8438 */
8439 @Override
8440 public RemoteViews makeBigContentView() {
8441 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
8442 ? mBuilder.mN.bigContentView
8443 : mBuilder.mN.contentView;
8444 return makeBigContentViewWithCustomContent(customRemoteView);
8445 }
8446
8447 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
8448 RemoteViews remoteViews = super.makeBigContentView();
8449 if (remoteViews != null) {
8450 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
8451 customRemoteView);
8452 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08008453 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008454 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8455 customRemoteView);
8456 } else {
8457 return null;
8458 }
8459 }
8460
8461 /**
8462 * @hide
8463 */
8464 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008465 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08008466 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
8467 ? mBuilder.mN.headsUpContentView
8468 : mBuilder.mN.contentView;
8469 return makeBigContentViewWithCustomContent(customRemoteView);
8470 }
8471
Julia Reynolds7217dc92018-03-07 12:12:09 -05008472 /**
8473 * @hide
8474 */
8475 @Override
8476 public boolean areNotificationsVisiblyDifferent(Style other) {
8477 if (other == null || getClass() != other.getClass()) {
8478 return true;
8479 }
8480 // Comparison done for all custom RemoteViews, independent of style
8481 return false;
8482 }
8483
Selim Cinek03eb3b72016-02-18 10:39:45 -08008484 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
8485 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08008486 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008487 // Need to clone customContent before adding, because otherwise it can no longer be
8488 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008489 customContent = customContent.clone();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008490 customContent.overrideTextColors(mBuilder.getPrimaryTextColor(mBuilder.mParams));
Selim Cinekf91017e2016-03-14 12:25:09 -07008491 remoteViews.removeAllViews(id);
8492 remoteViews.addView(id, customContent);
Sunny Goyalc12d31c2018-11-12 16:29:18 -08008493 remoteViews.addFlags(RemoteViews.FLAG_REAPPLY_DISALLOWED);
Adrian Roos5081c0d2016-02-26 16:04:19 -08008494 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08008495 return remoteViews;
8496 }
8497 }
8498
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008499 /**
8500 * Encapsulates the information needed to display a notification as a bubble.
8501 *
8502 * <p>A bubble is used to display app content in a floating window over the existing
8503 * foreground activity. A bubble has a collapsed state represented by an icon,
8504 * {@link BubbleMetadata.Builder#setIcon(Icon)} and an expanded state which is populated
8505 * via {@link BubbleMetadata.Builder#setIntent(PendingIntent)}.</p>
8506 *
8507 * <b>Notifications with a valid and allowed bubble will display in collapsed state
8508 * outside of the notification shade on unlocked devices. When a user interacts with the
8509 * collapsed bubble, the bubble intent will be invoked and displayed.</b>
8510 *
8511 * @see Notification.Builder#setBubbleMetadata(BubbleMetadata)
8512 */
8513 public static final class BubbleMetadata implements Parcelable {
8514
8515 private PendingIntent mPendingIntent;
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008516 private PendingIntent mDeleteIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008517 private Icon mIcon;
8518 private int mDesiredHeight;
Mady Mellor0157ff22019-01-24 15:31:48 -08008519 private int mFlags;
8520
8521 /**
8522 * If set and the app creating the bubble is in the foreground, the bubble will be posted
8523 * in its expanded state, with the contents of {@link #getIntent()} in a floating window.
8524 *
8525 * <p>If the app creating the bubble is not in the foreground this flag has no effect.</p>
8526 *
8527 * <p>Generally this flag should only be set if the user has performed an action to request
8528 * or create a bubble.</p>
8529 */
8530 private static final int FLAG_AUTO_EXPAND_BUBBLE = 0x00000001;
8531
8532 /**
8533 * If set and the app creating the bubble is in the foreground, the bubble will be posted
8534 * <b>without</b> the associated notification in the notification shade. Subsequent update
8535 * notifications to this bubble will post a notification in the shade.
8536 *
8537 * <p>If the app creating the bubble is not in the foreground this flag has no effect.</p>
8538 *
8539 * <p>Generally this flag should only be set if the user has performed an action to request
8540 * or create a bubble.</p>
8541 */
8542 private static final int FLAG_SUPPRESS_INITIAL_NOTIFICATION = 0x00000002;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008543
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008544 private BubbleMetadata(PendingIntent expandIntent, PendingIntent deleteIntent,
Mady Mellor138f4c92019-02-20 16:12:17 -08008545 Icon icon, int height) {
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008546 mPendingIntent = expandIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008547 mIcon = icon;
8548 mDesiredHeight = height;
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008549 mDeleteIntent = deleteIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008550 }
8551
8552 private BubbleMetadata(Parcel in) {
8553 mPendingIntent = PendingIntent.CREATOR.createFromParcel(in);
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008554 mIcon = Icon.CREATOR.createFromParcel(in);
8555 mDesiredHeight = in.readInt();
Mady Mellor0157ff22019-01-24 15:31:48 -08008556 mFlags = in.readInt();
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008557 if (in.readInt() != 0) {
8558 mDeleteIntent = PendingIntent.CREATOR.createFromParcel(in);
8559 }
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008560 }
8561
8562 /**
8563 * @return the pending intent used to populate the floating window for this bubble.
8564 */
8565 public PendingIntent getIntent() {
8566 return mPendingIntent;
8567 }
8568
8569 /**
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008570 * @return the pending intent to send when the bubble is dismissed by a user, if one exists.
8571 */
8572 public PendingIntent getDeleteIntent() {
8573 return mDeleteIntent;
8574 }
8575
8576 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008577 * @return the title that will appear along with the app content defined by
8578 * {@link #getIntent()} for this bubble.
Mady Mellor138f4c92019-02-20 16:12:17 -08008579 *
8580 * @deprecated titles are no longer required or shown.
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008581 */
Mady Mellor138f4c92019-02-20 16:12:17 -08008582 @Deprecated
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008583 public CharSequence getTitle() {
Mady Mellor138f4c92019-02-20 16:12:17 -08008584 return "";
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008585 }
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008586 /**
8587 * @return the icon that will be displayed for this bubble when it is collapsed.
8588 */
8589 public Icon getIcon() {
8590 return mIcon;
8591 }
8592
8593 /**
8594 * @return the ideal height for the floating window that app content defined by
8595 * {@link #getIntent()} for this bubble.
8596 */
8597 public int getDesiredHeight() {
8598 return mDesiredHeight;
8599 }
8600
Mady Mellor0157ff22019-01-24 15:31:48 -08008601 /**
8602 * @return whether this bubble should auto expand when it is posted.
8603 *
8604 * @see BubbleMetadata.Builder#setAutoExpandBubble(boolean)
8605 */
8606 public boolean getAutoExpandBubble() {
8607 return (mFlags & FLAG_AUTO_EXPAND_BUBBLE) != 0;
8608 }
8609
8610 /**
8611 * @return whether this bubble should suppress the initial notification when it is posted.
8612 *
8613 * @see BubbleMetadata.Builder#setSuppressInitialNotification(boolean)
8614 */
8615 public boolean getSuppressInitialNotification() {
8616 return (mFlags & FLAG_SUPPRESS_INITIAL_NOTIFICATION) != 0;
8617 }
8618
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07008619 public static final @android.annotation.NonNull Parcelable.Creator<BubbleMetadata> CREATOR =
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008620 new Parcelable.Creator<BubbleMetadata>() {
8621
8622 @Override
8623 public BubbleMetadata createFromParcel(Parcel source) {
8624 return new BubbleMetadata(source);
8625 }
8626
8627 @Override
8628 public BubbleMetadata[] newArray(int size) {
8629 return new BubbleMetadata[size];
8630 }
8631 };
8632
8633 @Override
8634 public int describeContents() {
8635 return 0;
8636 }
8637
8638 @Override
8639 public void writeToParcel(Parcel out, int flags) {
8640 mPendingIntent.writeToParcel(out, 0);
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008641 mIcon.writeToParcel(out, 0);
8642 out.writeInt(mDesiredHeight);
Mady Mellor0157ff22019-01-24 15:31:48 -08008643 out.writeInt(mFlags);
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008644 out.writeInt(mDeleteIntent != null ? 1 : 0);
8645 if (mDeleteIntent != null) {
8646 mDeleteIntent.writeToParcel(out, 0);
8647 }
Mady Mellor0157ff22019-01-24 15:31:48 -08008648 }
8649
8650 private void setFlags(int flags) {
8651 mFlags = flags;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008652 }
8653
8654 /**
8655 * Builder to construct a {@link BubbleMetadata} object.
8656 */
8657 public static class Builder {
8658
8659 private PendingIntent mPendingIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008660 private Icon mIcon;
8661 private int mDesiredHeight;
Mady Mellor0157ff22019-01-24 15:31:48 -08008662 private int mFlags;
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008663 private PendingIntent mDeleteIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008664
8665 /**
8666 * Constructs a new builder object.
8667 */
8668 public Builder() {
8669 }
8670
8671 /**
8672 * Sets the intent that will be used when the bubble is expanded. This will display the
8673 * app content in a floating window over the existing foreground activity.
8674 */
8675 public BubbleMetadata.Builder setIntent(PendingIntent intent) {
8676 if (intent == null) {
8677 throw new IllegalArgumentException("Bubble requires non-null pending intent");
8678 }
8679 mPendingIntent = intent;
8680 return this;
8681 }
8682
8683 /**
8684 * Sets the title that will appear along with the app content for this bubble.
8685 *
8686 * <p>A title is required and should expect to fit on a single line and make sense when
8687 * shown with the content defined by {@link #setIntent(PendingIntent)}.</p>
Mady Mellor138f4c92019-02-20 16:12:17 -08008688 *
8689 * @deprecated titles are no longer required or shown.
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008690 */
Mady Mellor138f4c92019-02-20 16:12:17 -08008691 @Deprecated
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008692 public BubbleMetadata.Builder setTitle(CharSequence title) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008693 return this;
8694 }
8695
8696 /**
8697 * Sets the icon that will represent the bubble when it is collapsed.
8698 *
8699 * <p>An icon is required and should be representative of the content within the bubble.
8700 * If your app produces multiple bubbles, the image should be unique for each of them.
8701 * </p>
8702 */
8703 public BubbleMetadata.Builder setIcon(Icon icon) {
8704 if (icon == null) {
8705 throw new IllegalArgumentException("Bubbles require non-null icon");
8706 }
8707 mIcon = icon;
8708 return this;
8709 }
8710
8711 /**
8712 * Sets the desired height for the app content defined by
8713 * {@link #setIntent(PendingIntent)}, this height may not be respected if there is not
8714 * enough space on the screen or if the provided height is too small to be useful.
8715 */
8716 public BubbleMetadata.Builder setDesiredHeight(int height) {
8717 mDesiredHeight = Math.max(height, 0);
8718 return this;
8719 }
8720
8721 /**
Mady Mellor0157ff22019-01-24 15:31:48 -08008722 * If set and the app creating the bubble is in the foreground, the bubble will be
8723 * posted in its expanded state, with the contents of {@link #getIntent()} in a
8724 * floating window.
8725 *
8726 * <p>If the app creating the bubble is not in the foreground this flag has no effect.
8727 * </p>
8728 *
8729 * <p>Generally this flag should only be set if the user has performed an action to
8730 * request or create a bubble.</p>
8731 */
8732 public BubbleMetadata.Builder setAutoExpandBubble(boolean shouldExpand) {
8733 setFlag(FLAG_AUTO_EXPAND_BUBBLE, shouldExpand);
8734 return this;
8735 }
8736
8737 /**
8738 * If set and the app creating the bubble is in the foreground, the bubble will be
8739 * posted <b>without</b> the associated notification in the notification shade.
8740 * Subsequent update notifications to this bubble will post a notification in the shade.
8741 *
8742 * <p>If the app creating the bubble is not in the foreground this flag has no effect.
8743 * </p>
8744 *
8745 * <p>Generally this flag should only be set if the user has performed an action to
8746 * request or create a bubble.</p>
8747 */
8748 public BubbleMetadata.Builder setSuppressInitialNotification(
8749 boolean shouldSupressNotif) {
8750 setFlag(FLAG_SUPPRESS_INITIAL_NOTIFICATION, shouldSupressNotif);
8751 return this;
8752 }
8753
8754 /**
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008755 * Sets an optional intent to send when this bubble is explicitly removed by the user.
8756 */
8757 public BubbleMetadata.Builder setDeleteIntent(PendingIntent deleteIntent) {
8758 mDeleteIntent = deleteIntent;
8759 return this;
8760 }
8761
8762 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008763 * Creates the {@link BubbleMetadata} defined by this builder.
8764 * <p>Will throw {@link IllegalStateException} if required fields have not been set
8765 * on this builder.</p>
8766 */
8767 public BubbleMetadata build() {
8768 if (mPendingIntent == null) {
8769 throw new IllegalStateException("Must supply pending intent to bubble");
8770 }
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008771 if (mIcon == null) {
8772 throw new IllegalStateException("Must supply an icon for the bubble");
8773 }
Mady Mellor138f4c92019-02-20 16:12:17 -08008774 BubbleMetadata data = new BubbleMetadata(mPendingIntent, mDeleteIntent,
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008775 mIcon, mDesiredHeight);
Mady Mellor0157ff22019-01-24 15:31:48 -08008776 data.setFlags(mFlags);
8777 return data;
8778 }
8779
8780 /**
8781 * @hide
8782 */
8783 public BubbleMetadata.Builder setFlag(int mask, boolean value) {
8784 if (value) {
8785 mFlags |= mask;
8786 } else {
8787 mFlags &= ~mask;
8788 }
8789 return this;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008790 }
8791 }
8792 }
8793
8794
Christoph Studer4600f9b2014-07-22 22:44:43 +02008795 // When adding a new Style subclass here, don't forget to update
8796 // Builder.getNotificationStyleClass.
8797
Griff Hazen61a9e862014-05-22 16:05:19 -07008798 /**
8799 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
8800 * metadata or change options on a notification builder.
8801 */
8802 public interface Extender {
8803 /**
8804 * Apply this extender to a notification builder.
8805 * @param builder the builder to be modified.
8806 * @return the build object for chaining.
8807 */
8808 public Builder extend(Builder builder);
8809 }
8810
8811 /**
8812 * Helper class to add wearable extensions to notifications.
8813 * <p class="note"> See
8814 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
8815 * for Android Wear</a> for more information on how to use this class.
8816 * <p>
8817 * To create a notification with wearable extensions:
8818 * <ol>
8819 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
8820 * properties.
8821 * <li>Create a {@link android.app.Notification.WearableExtender}.
8822 * <li>Set wearable-specific properties using the
8823 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
8824 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
8825 * notification.
8826 * <li>Post the notification to the notification system with the
8827 * {@code NotificationManager.notify(...)} methods.
8828 * </ol>
8829 *
8830 * <pre class="prettyprint">
8831 * Notification notif = new Notification.Builder(mContext)
8832 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
8833 * .setContentText(subject)
8834 * .setSmallIcon(R.drawable.new_mail)
8835 * .extend(new Notification.WearableExtender()
8836 * .setContentIcon(R.drawable.new_mail))
8837 * .build();
8838 * NotificationManager notificationManger =
8839 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
8840 * notificationManger.notify(0, notif);</pre>
8841 *
8842 * <p>Wearable extensions can be accessed on an existing notification by using the
8843 * {@code WearableExtender(Notification)} constructor,
8844 * and then using the {@code get} methods to access values.
8845 *
8846 * <pre class="prettyprint">
8847 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
8848 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07008849 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008850 */
8851 public static final class WearableExtender implements Extender {
8852 /**
8853 * Sentinel value for an action index that is unset.
8854 */
8855 public static final int UNSET_ACTION_INDEX = -1;
8856
8857 /**
8858 * Size value for use with {@link #setCustomSizePreset} to show this notification with
8859 * default sizing.
8860 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07008861 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07008862 * on their content.
Gus Prevasd7363752018-09-18 14:35:15 -04008863 *
8864 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008865 */
Gus Prevasd7363752018-09-18 14:35:15 -04008866 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008867 public static final int SIZE_DEFAULT = 0;
8868
8869 /**
8870 * Size value for use with {@link #setCustomSizePreset} to show this notification
8871 * with an extra small size.
8872 * <p>This value is only applicable for custom display notifications created using
8873 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008874 *
8875 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008876 */
Gus Prevasd7363752018-09-18 14:35:15 -04008877 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008878 public static final int SIZE_XSMALL = 1;
8879
8880 /**
8881 * Size value for use with {@link #setCustomSizePreset} to show this notification
8882 * with a small size.
8883 * <p>This value is only applicable for custom display notifications created using
8884 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008885 *
8886 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008887 */
Gus Prevasd7363752018-09-18 14:35:15 -04008888 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008889 public static final int SIZE_SMALL = 2;
8890
8891 /**
8892 * Size value for use with {@link #setCustomSizePreset} to show this notification
8893 * with a medium size.
8894 * <p>This value is only applicable for custom display notifications created using
8895 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008896 *
8897 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008898 */
Gus Prevasd7363752018-09-18 14:35:15 -04008899 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008900 public static final int SIZE_MEDIUM = 3;
8901
8902 /**
8903 * Size value for use with {@link #setCustomSizePreset} to show this notification
8904 * with a large size.
8905 * <p>This value is only applicable for custom display notifications created using
8906 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008907 *
8908 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008909 */
Gus Prevasd7363752018-09-18 14:35:15 -04008910 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008911 public static final int SIZE_LARGE = 4;
8912
Griff Hazend5f11f92014-05-27 15:40:09 -07008913 /**
8914 * Size value for use with {@link #setCustomSizePreset} to show this notification
8915 * full screen.
8916 * <p>This value is only applicable for custom display notifications created using
8917 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008918 *
8919 * @deprecated Display intents are no longer supported.
Griff Hazend5f11f92014-05-27 15:40:09 -07008920 */
Gus Prevasd7363752018-09-18 14:35:15 -04008921 @Deprecated
Griff Hazend5f11f92014-05-27 15:40:09 -07008922 public static final int SIZE_FULL_SCREEN = 5;
8923
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008924 /**
8925 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
8926 * short amount of time when this notification is displayed on the screen. This
8927 * is the default value.
Gus Prevasd7363752018-09-18 14:35:15 -04008928 *
8929 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008930 */
Gus Prevasd7363752018-09-18 14:35:15 -04008931 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008932 public static final int SCREEN_TIMEOUT_SHORT = 0;
8933
8934 /**
8935 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
8936 * for a longer amount of time when this notification is displayed on the screen.
Gus Prevasd7363752018-09-18 14:35:15 -04008937 *
8938 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008939 */
Gus Prevasd7363752018-09-18 14:35:15 -04008940 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008941 public static final int SCREEN_TIMEOUT_LONG = -1;
8942
Griff Hazen61a9e862014-05-22 16:05:19 -07008943 /** Notification extra which contains wearable extensions */
8944 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
8945
Pete Gastaf6781d2014-10-07 15:17:05 -04008946 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07008947 private static final String KEY_ACTIONS = "actions";
8948 private static final String KEY_FLAGS = "flags";
8949 private static final String KEY_DISPLAY_INTENT = "displayIntent";
8950 private static final String KEY_PAGES = "pages";
8951 private static final String KEY_BACKGROUND = "background";
8952 private static final String KEY_CONTENT_ICON = "contentIcon";
8953 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
8954 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
8955 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
8956 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
8957 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008958 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04008959 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008960 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07008961
8962 // Flags bitwise-ored to mFlags
8963 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
8964 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
8965 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
8966 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008967 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04008968 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04008969 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07008970
8971 // Default value for flags integer
8972 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
8973
8974 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
8975 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
8976
8977 private ArrayList<Action> mActions = new ArrayList<Action>();
8978 private int mFlags = DEFAULT_FLAGS;
8979 private PendingIntent mDisplayIntent;
8980 private ArrayList<Notification> mPages = new ArrayList<Notification>();
8981 private Bitmap mBackground;
8982 private int mContentIcon;
8983 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
8984 private int mContentActionIndex = UNSET_ACTION_INDEX;
8985 private int mCustomSizePreset = SIZE_DEFAULT;
8986 private int mCustomContentHeight;
8987 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008988 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04008989 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008990 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07008991
8992 /**
8993 * Create a {@link android.app.Notification.WearableExtender} with default
8994 * options.
8995 */
8996 public WearableExtender() {
8997 }
8998
8999 public WearableExtender(Notification notif) {
9000 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
9001 if (wearableBundle != null) {
9002 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
9003 if (actions != null) {
9004 mActions.addAll(actions);
9005 }
9006
9007 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
9008 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
9009
9010 Notification[] pages = getNotificationArrayFromBundle(
9011 wearableBundle, KEY_PAGES);
9012 if (pages != null) {
9013 Collections.addAll(mPages, pages);
9014 }
9015
9016 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
9017 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
9018 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
9019 DEFAULT_CONTENT_ICON_GRAVITY);
9020 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
9021 UNSET_ACTION_INDEX);
9022 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
9023 SIZE_DEFAULT);
9024 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
9025 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009026 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04009027 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009028 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07009029 }
9030 }
9031
9032 /**
9033 * Apply wearable extensions to a notification that is being built. This is typically
9034 * called by the {@link android.app.Notification.Builder#extend} method of
9035 * {@link android.app.Notification.Builder}.
9036 */
9037 @Override
9038 public Notification.Builder extend(Notification.Builder builder) {
9039 Bundle wearableBundle = new Bundle();
9040
9041 if (!mActions.isEmpty()) {
9042 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
9043 }
9044 if (mFlags != DEFAULT_FLAGS) {
9045 wearableBundle.putInt(KEY_FLAGS, mFlags);
9046 }
9047 if (mDisplayIntent != null) {
9048 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
9049 }
9050 if (!mPages.isEmpty()) {
9051 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
9052 new Notification[mPages.size()]));
9053 }
9054 if (mBackground != null) {
9055 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
9056 }
9057 if (mContentIcon != 0) {
9058 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
9059 }
9060 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
9061 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
9062 }
9063 if (mContentActionIndex != UNSET_ACTION_INDEX) {
9064 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
9065 mContentActionIndex);
9066 }
9067 if (mCustomSizePreset != SIZE_DEFAULT) {
9068 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
9069 }
9070 if (mCustomContentHeight != 0) {
9071 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
9072 }
9073 if (mGravity != DEFAULT_GRAVITY) {
9074 wearableBundle.putInt(KEY_GRAVITY, mGravity);
9075 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009076 if (mHintScreenTimeout != 0) {
9077 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
9078 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04009079 if (mDismissalId != null) {
9080 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
9081 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009082 if (mBridgeTag != null) {
9083 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
9084 }
Griff Hazen61a9e862014-05-22 16:05:19 -07009085
9086 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
9087 return builder;
9088 }
9089
9090 @Override
9091 public WearableExtender clone() {
9092 WearableExtender that = new WearableExtender();
9093 that.mActions = new ArrayList<Action>(this.mActions);
9094 that.mFlags = this.mFlags;
9095 that.mDisplayIntent = this.mDisplayIntent;
9096 that.mPages = new ArrayList<Notification>(this.mPages);
9097 that.mBackground = this.mBackground;
9098 that.mContentIcon = this.mContentIcon;
9099 that.mContentIconGravity = this.mContentIconGravity;
9100 that.mContentActionIndex = this.mContentActionIndex;
9101 that.mCustomSizePreset = this.mCustomSizePreset;
9102 that.mCustomContentHeight = this.mCustomContentHeight;
9103 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009104 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04009105 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009106 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07009107 return that;
9108 }
9109
9110 /**
9111 * Add a wearable action to this notification.
9112 *
9113 * <p>When wearable actions are added using this method, the set of actions that
9114 * show on a wearable device splits from devices that only show actions added
9115 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
9116 * of which actions display on different devices.
9117 *
9118 * @param action the action to add to this notification
9119 * @return this object for method chaining
9120 * @see android.app.Notification.Action
9121 */
9122 public WearableExtender addAction(Action action) {
9123 mActions.add(action);
9124 return this;
9125 }
9126
9127 /**
9128 * Adds wearable actions to this notification.
9129 *
9130 * <p>When wearable actions are added using this method, the set of actions that
9131 * show on a wearable device splits from devices that only show actions added
9132 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
9133 * of which actions display on different devices.
9134 *
9135 * @param actions the actions to add to this notification
9136 * @return this object for method chaining
9137 * @see android.app.Notification.Action
9138 */
9139 public WearableExtender addActions(List<Action> actions) {
9140 mActions.addAll(actions);
9141 return this;
9142 }
9143
9144 /**
9145 * Clear all wearable actions present on this builder.
9146 * @return this object for method chaining.
9147 * @see #addAction
9148 */
9149 public WearableExtender clearActions() {
9150 mActions.clear();
9151 return this;
9152 }
9153
9154 /**
9155 * Get the wearable actions present on this notification.
9156 */
9157 public List<Action> getActions() {
9158 return mActions;
9159 }
9160
9161 /**
9162 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07009163 * this notification. The {@link PendingIntent} provided should be for an activity.
9164 *
9165 * <pre class="prettyprint">
9166 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
9167 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
9168 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
9169 * Notification notif = new Notification.Builder(context)
9170 * .extend(new Notification.WearableExtender()
9171 * .setDisplayIntent(displayPendingIntent)
9172 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
9173 * .build();</pre>
9174 *
9175 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07009176 * should have an empty task affinity. It is also recommended to use the device
9177 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07009178 *
9179 * <p>Example AndroidManifest.xml entry:
9180 * <pre class="prettyprint">
9181 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
9182 * android:exported=&quot;true&quot;
9183 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07009184 * android:taskAffinity=&quot;&quot;
9185 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07009186 *
9187 * @param intent the {@link PendingIntent} for an activity
9188 * @return this object for method chaining
9189 * @see android.app.Notification.WearableExtender#getDisplayIntent
Gus Prevasd7363752018-09-18 14:35:15 -04009190 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009191 */
Gus Prevasd7363752018-09-18 14:35:15 -04009192 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009193 public WearableExtender setDisplayIntent(PendingIntent intent) {
9194 mDisplayIntent = intent;
9195 return this;
9196 }
9197
9198 /**
9199 * Get the intent to launch inside of an activity view when displaying this
9200 * notification. This {@code PendingIntent} should be for an activity.
Gus Prevasd7363752018-09-18 14:35:15 -04009201 *
9202 * @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 PendingIntent getDisplayIntent() {
9206 return mDisplayIntent;
9207 }
9208
9209 /**
9210 * Add an additional page of content to display with this notification. The current
9211 * notification forms the first page, and pages added using this function form
9212 * subsequent pages. This field can be used to separate a notification into multiple
9213 * sections.
9214 *
9215 * @param page the notification to add as another page
9216 * @return this object for method chaining
9217 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04009218 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009219 */
Gus Prevasd7363752018-09-18 14:35:15 -04009220 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009221 public WearableExtender addPage(Notification page) {
9222 mPages.add(page);
9223 return this;
9224 }
9225
9226 /**
9227 * Add additional pages of content to display with this notification. The current
9228 * notification forms the first page, and pages added using this function form
9229 * subsequent pages. This field can be used to separate a notification into multiple
9230 * sections.
9231 *
9232 * @param pages a list of notifications
9233 * @return this object for method chaining
9234 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04009235 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009236 */
Gus Prevasd7363752018-09-18 14:35:15 -04009237 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009238 public WearableExtender addPages(List<Notification> pages) {
9239 mPages.addAll(pages);
9240 return this;
9241 }
9242
9243 /**
9244 * Clear all additional pages present on this builder.
9245 * @return this object for method chaining.
9246 * @see #addPage
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 clearPages() {
9251 mPages.clear();
9252 return this;
9253 }
9254
9255 /**
9256 * Get the array of additional pages of content for displaying this notification. The
9257 * current notification forms the first page, and elements within this array form
9258 * subsequent pages. This field can be used to separate a notification into multiple
9259 * sections.
9260 * @return the pages for this notification
Gus Prevasd7363752018-09-18 14:35:15 -04009261 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009262 */
Gus Prevasd7363752018-09-18 14:35:15 -04009263 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009264 public List<Notification> getPages() {
9265 return mPages;
9266 }
9267
9268 /**
9269 * Set a background image to be displayed behind the notification content.
9270 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
9271 * will work with any notification style.
9272 *
9273 * @param background the background bitmap
9274 * @return this object for method chaining
9275 * @see android.app.Notification.WearableExtender#getBackground
Gus Prevasd7363752018-09-18 14:35:15 -04009276 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009277 */
Gus Prevasd7363752018-09-18 14:35:15 -04009278 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009279 public WearableExtender setBackground(Bitmap background) {
9280 mBackground = background;
9281 return this;
9282 }
9283
9284 /**
9285 * Get a background image to be displayed behind the notification content.
9286 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
9287 * will work with any notification style.
9288 *
9289 * @return the background image
9290 * @see android.app.Notification.WearableExtender#setBackground
Gus Prevasd7363752018-09-18 14:35:15 -04009291 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009292 */
Gus Prevasd7363752018-09-18 14:35:15 -04009293 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009294 public Bitmap getBackground() {
9295 return mBackground;
9296 }
9297
9298 /**
9299 * Set an icon that goes with the content of this notification.
9300 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009301 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009302 public WearableExtender setContentIcon(int icon) {
9303 mContentIcon = icon;
9304 return this;
9305 }
9306
9307 /**
9308 * Get an icon that goes with the content of this notification.
9309 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009310 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009311 public int getContentIcon() {
9312 return mContentIcon;
9313 }
9314
9315 /**
9316 * Set the gravity that the content icon should have within the notification display.
9317 * Supported values include {@link android.view.Gravity#START} and
9318 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
9319 * @see #setContentIcon
9320 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009321 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009322 public WearableExtender setContentIconGravity(int contentIconGravity) {
9323 mContentIconGravity = contentIconGravity;
9324 return this;
9325 }
9326
9327 /**
9328 * Get 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 #getContentIcon
9332 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009333 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009334 public int getContentIconGravity() {
9335 return mContentIconGravity;
9336 }
9337
9338 /**
Gus Prevasd7363752018-09-18 14:35:15 -04009339 * Set an action from this notification's actions as the primary action. If the action has a
9340 * {@link RemoteInput} associated with it, shortcuts to the options for that input are shown
9341 * directly on the notification.
Griff Hazen14f57992014-05-26 09:07:14 -07009342 *
Gus Prevasd7363752018-09-18 14:35:15 -04009343 * @param actionIndex The index of the primary action.
Griff Hazen14f57992014-05-26 09:07:14 -07009344 * If wearable actions were added to the main notification, this index
9345 * will apply to that list, otherwise it will apply to the regular
9346 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07009347 */
9348 public WearableExtender setContentAction(int actionIndex) {
9349 mContentActionIndex = actionIndex;
9350 return this;
9351 }
9352
9353 /**
Gus Prevasd7363752018-09-18 14:35:15 -04009354 * Get the index of the notification action, if any, that was specified as the primary
9355 * action.
Griff Hazen14f57992014-05-26 09:07:14 -07009356 *
9357 * <p>If wearable specific actions were added to the main notification, this index will
9358 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07009359 *
9360 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07009361 */
9362 public int getContentAction() {
9363 return mContentActionIndex;
9364 }
9365
9366 /**
9367 * Set the gravity that this notification should have within the available viewport space.
9368 * Supported values include {@link android.view.Gravity#TOP},
9369 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
9370 * The default value is {@link android.view.Gravity#BOTTOM}.
9371 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009372 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009373 public WearableExtender setGravity(int gravity) {
9374 mGravity = gravity;
9375 return this;
9376 }
9377
9378 /**
9379 * Get 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 int getGravity() {
9386 return mGravity;
9387 }
9388
9389 /**
9390 * Set the custom size preset for the display of this notification out of the available
9391 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
9392 * {@link #SIZE_LARGE}.
9393 * <p>Some custom size presets are only applicable for custom display notifications created
9394 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
9395 * documentation for the preset in question. See also
9396 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
9397 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009398 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009399 public WearableExtender setCustomSizePreset(int sizePreset) {
9400 mCustomSizePreset = sizePreset;
9401 return this;
9402 }
9403
9404 /**
9405 * Get the custom size preset for the display of this notification out of the available
9406 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
9407 * {@link #SIZE_LARGE}.
9408 * <p>Some custom size presets are only applicable for custom display notifications created
9409 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
9410 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
9411 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009412 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009413 public int getCustomSizePreset() {
9414 return mCustomSizePreset;
9415 }
9416
9417 /**
9418 * Set the custom height in pixels for the display of this notification's content.
9419 * <p>This option is only available for custom display notifications created
9420 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
9421 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
9422 * {@link #getCustomContentHeight}.
9423 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009424 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009425 public WearableExtender setCustomContentHeight(int height) {
9426 mCustomContentHeight = height;
9427 return this;
9428 }
9429
9430 /**
9431 * Get the custom height in pixels for the display of this notification's content.
9432 * <p>This option is only available for custom display notifications created
9433 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
9434 * {@link #setCustomContentHeight}.
9435 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009436 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009437 public int getCustomContentHeight() {
9438 return mCustomContentHeight;
9439 }
9440
9441 /**
9442 * Set whether the scrolling position for the contents of this notification should start
9443 * at the bottom of the contents instead of the top when the contents are too long to
9444 * display within the screen. Default is false (start scroll at the top).
9445 */
9446 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
9447 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
9448 return this;
9449 }
9450
9451 /**
9452 * Get whether the scrolling position for the contents of this notification should start
9453 * at the bottom of the contents instead of the top when the contents are too long to
9454 * display within the screen. Default is false (start scroll at the top).
9455 */
9456 public boolean getStartScrollBottom() {
9457 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
9458 }
9459
9460 /**
9461 * Set whether the content intent is available when the wearable device is not connected
9462 * to a companion device. The user can still trigger this intent when the wearable device
9463 * is offline, but a visual hint will indicate that the content intent may not be available.
9464 * Defaults to true.
9465 */
9466 public WearableExtender setContentIntentAvailableOffline(
9467 boolean contentIntentAvailableOffline) {
9468 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
9469 return this;
9470 }
9471
9472 /**
9473 * Get 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 boolean getContentIntentAvailableOffline() {
9479 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
9480 }
9481
9482 /**
9483 * Set a hint that this notification's icon should not be displayed.
9484 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
9485 * @return this object for method chaining
9486 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009487 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009488 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
9489 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
9490 return this;
9491 }
9492
9493 /**
9494 * Get a hint that this notification's icon should not be displayed.
9495 * @return {@code true} if this icon should not be displayed, false otherwise.
9496 * The default value is {@code false} if this was never set.
9497 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009498 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009499 public boolean getHintHideIcon() {
9500 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
9501 }
9502
9503 /**
9504 * Set a visual hint that only the background image of this notification should be
9505 * displayed, and other semantic content should be hidden. This hint is only applicable
9506 * to sub-pages added using {@link #addPage}.
9507 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009508 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009509 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
9510 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
9511 return this;
9512 }
9513
9514 /**
9515 * Get a visual hint that only the background image of this notification should be
9516 * displayed, and other semantic content should be hidden. This hint is only applicable
9517 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
9518 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009519 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009520 public boolean getHintShowBackgroundOnly() {
9521 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
9522 }
9523
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009524 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009525 * Set a hint that this notification's background should not be clipped if possible,
9526 * and should instead be resized to fully display on the screen, retaining the aspect
9527 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009528 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
9529 * @return this object for method chaining
9530 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009531 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009532 public WearableExtender setHintAvoidBackgroundClipping(
9533 boolean hintAvoidBackgroundClipping) {
9534 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
9535 return this;
9536 }
9537
9538 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009539 * Get a hint that this notification's background should not be clipped if possible,
9540 * and should instead be resized to fully display on the screen, retaining the aspect
9541 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009542 * @return {@code true} if it's ok if the background is clipped on the screen, false
9543 * otherwise. The default value is {@code false} if this was never set.
9544 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009545 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009546 public boolean getHintAvoidBackgroundClipping() {
9547 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
9548 }
9549
9550 /**
9551 * Set a hint that the screen should remain on for at least this duration when
9552 * this notification is displayed on the screen.
9553 * @param timeout The requested screen timeout in milliseconds. Can also be either
9554 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9555 * @return this object for method chaining
9556 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009557 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009558 public WearableExtender setHintScreenTimeout(int timeout) {
9559 mHintScreenTimeout = timeout;
9560 return this;
9561 }
9562
9563 /**
9564 * Get the duration, in milliseconds, that the screen should remain on for
9565 * when this notification is displayed.
9566 * @return the duration in milliseconds if > 0, or either one of the sentinel values
9567 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9568 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009569 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009570 public int getHintScreenTimeout() {
9571 return mHintScreenTimeout;
9572 }
9573
Alex Hills9ab3a232016-04-05 14:54:56 -04009574 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04009575 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
9576 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9577 * qr codes, as well as other simple black-and-white tickets.
9578 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
9579 * @return this object for method chaining
Gus Prevasd7363752018-09-18 14:35:15 -04009580 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009581 */
Gus Prevasd7363752018-09-18 14:35:15 -04009582 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009583 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
9584 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
9585 return this;
9586 }
9587
9588 /**
9589 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
9590 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9591 * qr codes, as well as other simple black-and-white tickets.
9592 * @return {@code true} if it should be displayed in ambient, false otherwise
9593 * otherwise. The default value is {@code false} if this was never set.
Gus Prevasd7363752018-09-18 14:35:15 -04009594 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009595 */
Gus Prevasd7363752018-09-18 14:35:15 -04009596 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009597 public boolean getHintAmbientBigPicture() {
9598 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
9599 }
9600
9601 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04009602 * Set a hint that this notification's content intent will launch an {@link Activity}
9603 * directly, telling the platform that it can generate the appropriate transitions.
9604 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
9605 * an activity and transitions should be generated, false otherwise.
9606 * @return this object for method chaining
9607 */
9608 public WearableExtender setHintContentIntentLaunchesActivity(
9609 boolean hintContentIntentLaunchesActivity) {
9610 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
9611 return this;
9612 }
9613
9614 /**
9615 * Get a hint that this notification's content intent will launch an {@link Activity}
9616 * directly, telling the platform that it can generate the appropriate transitions
9617 * @return {@code true} if the content intent will launch an activity and transitions should
9618 * be generated, false otherwise. The default value is {@code false} if this was never set.
9619 */
9620 public boolean getHintContentIntentLaunchesActivity() {
9621 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
9622 }
9623
Nadia Benbernou948627e2016-04-14 14:41:08 -04009624 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009625 * Sets the dismissal id for this notification. If a notification is posted with a
9626 * dismissal id, then when that notification is canceled, notifications on other wearables
9627 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04009628 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009629 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04009630 * @param dismissalId the dismissal id of the notification.
9631 * @return this object for method chaining
9632 */
9633 public WearableExtender setDismissalId(String dismissalId) {
9634 mDismissalId = dismissalId;
9635 return this;
9636 }
9637
9638 /**
9639 * Returns the dismissal id of the notification.
9640 * @return the dismissal id of the notification or null if it has not been set.
9641 */
9642 public String getDismissalId() {
9643 return mDismissalId;
9644 }
9645
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009646 /**
9647 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
9648 * posted from a phone to provide finer-grained control on what notifications are bridged
9649 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
9650 * Features to Notifications</a> for more information.
9651 * @param bridgeTag the bridge tag of the notification.
9652 * @return this object for method chaining
9653 */
9654 public WearableExtender setBridgeTag(String bridgeTag) {
9655 mBridgeTag = bridgeTag;
9656 return this;
9657 }
9658
9659 /**
9660 * Returns the bridge tag of the notification.
9661 * @return the bridge tag or null if not present.
9662 */
9663 public String getBridgeTag() {
9664 return mBridgeTag;
9665 }
9666
Griff Hazen61a9e862014-05-22 16:05:19 -07009667 private void setFlag(int mask, boolean value) {
9668 if (value) {
9669 mFlags |= mask;
9670 } else {
9671 mFlags &= ~mask;
9672 }
9673 }
9674 }
9675
9676 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009677 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
9678 * with car extensions:
9679 *
9680 * <ol>
9681 * <li>Create an {@link Notification.Builder}, setting any desired
9682 * properties.
9683 * <li>Create a {@link CarExtender}.
9684 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
9685 * {@link CarExtender}.
9686 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9687 * to apply the extensions to a notification.
9688 * </ol>
9689 *
9690 * <pre class="prettyprint">
9691 * Notification notification = new Notification.Builder(context)
9692 * ...
9693 * .extend(new CarExtender()
9694 * .set*(...))
9695 * .build();
9696 * </pre>
9697 *
9698 * <p>Car extensions can be accessed on an existing notification by using the
9699 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
9700 * to access values.
9701 */
9702 public static final class CarExtender implements Extender {
9703 private static final String TAG = "CarExtender";
9704
9705 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
9706 private static final String EXTRA_LARGE_ICON = "large_icon";
9707 private static final String EXTRA_CONVERSATION = "car_conversation";
9708 private static final String EXTRA_COLOR = "app_color";
9709
9710 private Bitmap mLargeIcon;
9711 private UnreadConversation mUnreadConversation;
9712 private int mColor = Notification.COLOR_DEFAULT;
9713
9714 /**
9715 * Create a {@link CarExtender} with default options.
9716 */
9717 public CarExtender() {
9718 }
9719
9720 /**
9721 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
9722 *
9723 * @param notif The notification from which to copy options.
9724 */
9725 public CarExtender(Notification notif) {
9726 Bundle carBundle = notif.extras == null ?
9727 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
9728 if (carBundle != null) {
9729 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
9730 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
9731
9732 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
9733 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
9734 }
9735 }
9736
9737 /**
9738 * Apply car extensions to a notification that is being built. This is typically called by
9739 * the {@link Notification.Builder#extend(Notification.Extender)}
9740 * method of {@link Notification.Builder}.
9741 */
9742 @Override
9743 public Notification.Builder extend(Notification.Builder builder) {
9744 Bundle carExtensions = new Bundle();
9745
9746 if (mLargeIcon != null) {
9747 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
9748 }
9749 if (mColor != Notification.COLOR_DEFAULT) {
9750 carExtensions.putInt(EXTRA_COLOR, mColor);
9751 }
9752
9753 if (mUnreadConversation != null) {
9754 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
9755 carExtensions.putBundle(EXTRA_CONVERSATION, b);
9756 }
9757
9758 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
9759 return builder;
9760 }
9761
9762 /**
9763 * Sets the accent color to use when Android Auto presents the notification.
9764 *
9765 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
9766 * to accent the displayed notification. However, not all colors are acceptable in an
9767 * automotive setting. This method can be used to override the color provided in the
9768 * notification in such a situation.
9769 */
Tor Norbye80756e32015-03-02 09:39:27 -08009770 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009771 mColor = color;
9772 return this;
9773 }
9774
9775 /**
9776 * Gets the accent color.
9777 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04009778 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009779 */
Tor Norbye80756e32015-03-02 09:39:27 -08009780 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009781 public int getColor() {
9782 return mColor;
9783 }
9784
9785 /**
9786 * Sets the large icon of the car notification.
9787 *
9788 * If no large icon is set in the extender, Android Auto will display the icon
9789 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
9790 *
9791 * @param largeIcon The large icon to use in the car notification.
9792 * @return This object for method chaining.
9793 */
9794 public CarExtender setLargeIcon(Bitmap largeIcon) {
9795 mLargeIcon = largeIcon;
9796 return this;
9797 }
9798
9799 /**
9800 * Gets the large icon used in this car notification, or null if no icon has been set.
9801 *
9802 * @return The large icon for the car notification.
9803 * @see CarExtender#setLargeIcon
9804 */
9805 public Bitmap getLargeIcon() {
9806 return mLargeIcon;
9807 }
9808
9809 /**
9810 * Sets the unread conversation in a message notification.
9811 *
9812 * @param unreadConversation The unread part of the conversation this notification conveys.
9813 * @return This object for method chaining.
9814 */
9815 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
9816 mUnreadConversation = unreadConversation;
9817 return this;
9818 }
9819
9820 /**
9821 * Returns the unread conversation conveyed by this notification.
9822 * @see #setUnreadConversation(UnreadConversation)
9823 */
9824 public UnreadConversation getUnreadConversation() {
9825 return mUnreadConversation;
9826 }
9827
9828 /**
9829 * A class which holds the unread messages from a conversation.
9830 */
9831 public static class UnreadConversation {
9832 private static final String KEY_AUTHOR = "author";
9833 private static final String KEY_TEXT = "text";
9834 private static final String KEY_MESSAGES = "messages";
9835 private static final String KEY_REMOTE_INPUT = "remote_input";
9836 private static final String KEY_ON_REPLY = "on_reply";
9837 private static final String KEY_ON_READ = "on_read";
9838 private static final String KEY_PARTICIPANTS = "participants";
9839 private static final String KEY_TIMESTAMP = "timestamp";
9840
9841 private final String[] mMessages;
9842 private final RemoteInput mRemoteInput;
9843 private final PendingIntent mReplyPendingIntent;
9844 private final PendingIntent mReadPendingIntent;
9845 private final String[] mParticipants;
9846 private final long mLatestTimestamp;
9847
9848 UnreadConversation(String[] messages, RemoteInput remoteInput,
9849 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
9850 String[] participants, long latestTimestamp) {
9851 mMessages = messages;
9852 mRemoteInput = remoteInput;
9853 mReadPendingIntent = readPendingIntent;
9854 mReplyPendingIntent = replyPendingIntent;
9855 mParticipants = participants;
9856 mLatestTimestamp = latestTimestamp;
9857 }
9858
9859 /**
9860 * Gets the list of messages conveyed by this notification.
9861 */
9862 public String[] getMessages() {
9863 return mMessages;
9864 }
9865
9866 /**
9867 * Gets the remote input that will be used to convey the response to a message list, or
9868 * null if no such remote input exists.
9869 */
9870 public RemoteInput getRemoteInput() {
9871 return mRemoteInput;
9872 }
9873
9874 /**
9875 * Gets the pending intent that will be triggered when the user replies to this
9876 * notification.
9877 */
9878 public PendingIntent getReplyPendingIntent() {
9879 return mReplyPendingIntent;
9880 }
9881
9882 /**
9883 * Gets the pending intent that Android Auto will send after it reads aloud all messages
9884 * in this object's message list.
9885 */
9886 public PendingIntent getReadPendingIntent() {
9887 return mReadPendingIntent;
9888 }
9889
9890 /**
9891 * Gets the participants in the conversation.
9892 */
9893 public String[] getParticipants() {
9894 return mParticipants;
9895 }
9896
9897 /**
9898 * Gets the firs participant in the conversation.
9899 */
9900 public String getParticipant() {
9901 return mParticipants.length > 0 ? mParticipants[0] : null;
9902 }
9903
9904 /**
9905 * Gets the timestamp of the conversation.
9906 */
9907 public long getLatestTimestamp() {
9908 return mLatestTimestamp;
9909 }
9910
9911 Bundle getBundleForUnreadConversation() {
9912 Bundle b = new Bundle();
9913 String author = null;
9914 if (mParticipants != null && mParticipants.length > 1) {
9915 author = mParticipants[0];
9916 }
9917 Parcelable[] messages = new Parcelable[mMessages.length];
9918 for (int i = 0; i < messages.length; i++) {
9919 Bundle m = new Bundle();
9920 m.putString(KEY_TEXT, mMessages[i]);
9921 m.putString(KEY_AUTHOR, author);
9922 messages[i] = m;
9923 }
9924 b.putParcelableArray(KEY_MESSAGES, messages);
9925 if (mRemoteInput != null) {
9926 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
9927 }
9928 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
9929 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
9930 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
9931 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
9932 return b;
9933 }
9934
9935 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
9936 if (b == null) {
9937 return null;
9938 }
9939 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
9940 String[] messages = null;
9941 if (parcelableMessages != null) {
9942 String[] tmp = new String[parcelableMessages.length];
9943 boolean success = true;
9944 for (int i = 0; i < tmp.length; i++) {
9945 if (!(parcelableMessages[i] instanceof Bundle)) {
9946 success = false;
9947 break;
9948 }
9949 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
9950 if (tmp[i] == null) {
9951 success = false;
9952 break;
9953 }
9954 }
9955 if (success) {
9956 messages = tmp;
9957 } else {
9958 return null;
9959 }
9960 }
9961
9962 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
9963 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
9964
9965 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
9966
9967 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
9968 if (participants == null || participants.length != 1) {
9969 return null;
9970 }
9971
9972 return new UnreadConversation(messages,
9973 remoteInput,
9974 onReply,
9975 onRead,
9976 participants, b.getLong(KEY_TIMESTAMP));
9977 }
9978 };
9979
9980 /**
9981 * Builder class for {@link CarExtender.UnreadConversation} objects.
9982 */
9983 public static class Builder {
9984 private final List<String> mMessages = new ArrayList<String>();
9985 private final String mParticipant;
9986 private RemoteInput mRemoteInput;
9987 private PendingIntent mReadPendingIntent;
9988 private PendingIntent mReplyPendingIntent;
9989 private long mLatestTimestamp;
9990
9991 /**
9992 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
9993 *
9994 * @param name The name of the other participant in the conversation.
9995 */
9996 public Builder(String name) {
9997 mParticipant = name;
9998 }
9999
10000 /**
10001 * Appends a new unread message to the list of messages for this conversation.
10002 *
10003 * The messages should be added from oldest to newest.
10004 *
10005 * @param message The text of the new unread message.
10006 * @return This object for method chaining.
10007 */
10008 public Builder addMessage(String message) {
10009 mMessages.add(message);
10010 return this;
10011 }
10012
10013 /**
10014 * Sets the pending intent and remote input which will convey the reply to this
10015 * notification.
10016 *
10017 * @param pendingIntent The pending intent which will be triggered on a reply.
10018 * @param remoteInput The remote input parcelable which will carry the reply.
10019 * @return This object for method chaining.
10020 *
10021 * @see CarExtender.UnreadConversation#getRemoteInput
10022 * @see CarExtender.UnreadConversation#getReplyPendingIntent
10023 */
10024 public Builder setReplyAction(
10025 PendingIntent pendingIntent, RemoteInput remoteInput) {
10026 mRemoteInput = remoteInput;
10027 mReplyPendingIntent = pendingIntent;
10028
10029 return this;
10030 }
10031
10032 /**
10033 * Sets the pending intent that will be sent once the messages in this notification
10034 * are read.
10035 *
10036 * @param pendingIntent The pending intent to use.
10037 * @return This object for method chaining.
10038 */
10039 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
10040 mReadPendingIntent = pendingIntent;
10041 return this;
10042 }
10043
10044 /**
10045 * Sets the timestamp of the most recent message in an unread conversation.
10046 *
10047 * If a messaging notification has been posted by your application and has not
10048 * yet been cancelled, posting a later notification with the same id and tag
10049 * but without a newer timestamp may result in Android Auto not displaying a
10050 * heads up notification for the later notification.
10051 *
10052 * @param timestamp The timestamp of the most recent message in the conversation.
10053 * @return This object for method chaining.
10054 */
10055 public Builder setLatestTimestamp(long timestamp) {
10056 mLatestTimestamp = timestamp;
10057 return this;
10058 }
10059
10060 /**
10061 * Builds a new unread conversation object.
10062 *
10063 * @return The new unread conversation object.
10064 */
10065 public UnreadConversation build() {
10066 String[] messages = mMessages.toArray(new String[mMessages.size()]);
10067 String[] participants = { mParticipant };
10068 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
10069 mReadPendingIntent, participants, mLatestTimestamp);
10070 }
10071 }
10072 }
10073
10074 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010075 * <p>Helper class to add Android TV extensions to notifications. To create a notification
10076 * with a TV extension:
10077 *
10078 * <ol>
10079 * <li>Create an {@link Notification.Builder}, setting any desired properties.
10080 * <li>Create a {@link TvExtender}.
10081 * <li>Set TV-specific properties using the {@code set} methods of
10082 * {@link TvExtender}.
10083 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
10084 * to apply the extension to a notification.
10085 * </ol>
10086 *
10087 * <pre class="prettyprint">
10088 * Notification notification = new Notification.Builder(context)
10089 * ...
10090 * .extend(new TvExtender()
10091 * .set*(...))
10092 * .build();
10093 * </pre>
10094 *
10095 * <p>TV extensions can be accessed on an existing notification by using the
10096 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
10097 * to access values.
10098 *
10099 * @hide
10100 */
10101 @SystemApi
10102 public static final class TvExtender implements Extender {
10103 private static final String TAG = "TvExtender";
10104
10105 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
10106 private static final String EXTRA_FLAGS = "flags";
10107 private static final String EXTRA_CONTENT_INTENT = "content_intent";
10108 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010109 private static final String EXTRA_CHANNEL_ID = "channel_id";
Rhiannon Malia1a083932018-01-24 15:02:30 -080010110 private static final String EXTRA_SUPPRESS_SHOW_OVER_APPS = "suppressShowOverApps";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010111
10112 // Flags bitwise-ored to mFlags
10113 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
10114
10115 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010116 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010117 private PendingIntent mContentIntent;
10118 private PendingIntent mDeleteIntent;
Rhiannon Malia1a083932018-01-24 15:02:30 -080010119 private boolean mSuppressShowOverApps;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010120
10121 /**
10122 * Create a {@link TvExtender} with default options.
10123 */
10124 public TvExtender() {
10125 mFlags = FLAG_AVAILABLE_ON_TV;
10126 }
10127
10128 /**
10129 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
10130 *
10131 * @param notif The notification from which to copy options.
10132 */
10133 public TvExtender(Notification notif) {
10134 Bundle bundle = notif.extras == null ?
10135 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
10136 if (bundle != null) {
10137 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010138 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Rhiannon Malia1a083932018-01-24 15:02:30 -080010139 mSuppressShowOverApps = bundle.getBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010140 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
10141 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
10142 }
10143 }
10144
10145 /**
10146 * Apply a TV extension to a notification that is being built. This is typically called by
10147 * the {@link Notification.Builder#extend(Notification.Extender)}
10148 * method of {@link Notification.Builder}.
10149 */
10150 @Override
10151 public Notification.Builder extend(Notification.Builder builder) {
10152 Bundle bundle = new Bundle();
10153
10154 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010155 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Rhiannon Malia1a083932018-01-24 15:02:30 -080010156 bundle.putBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS, mSuppressShowOverApps);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010157 if (mContentIntent != null) {
10158 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
10159 }
10160
10161 if (mDeleteIntent != null) {
10162 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
10163 }
10164
10165 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
10166 return builder;
10167 }
10168
10169 /**
10170 * Returns true if this notification should be shown on TV. This method return true
10171 * if the notification was extended with a TvExtender.
10172 */
10173 public boolean isAvailableOnTv() {
10174 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
10175 }
10176
10177 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010178 * Specifies the channel the notification should be delivered on when shown on TV.
10179 * It can be different from the channel that the notification is delivered to when
10180 * posting on a non-TV device.
10181 */
10182 public TvExtender setChannel(String channelId) {
10183 mChannelId = channelId;
10184 return this;
10185 }
10186
10187 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -040010188 * Specifies the channel the notification should be delivered on when shown on TV.
10189 * It can be different from the channel that the notification is delivered to when
10190 * posting on a non-TV device.
10191 */
10192 public TvExtender setChannelId(String channelId) {
10193 mChannelId = channelId;
10194 return this;
10195 }
10196
Jeff Sharkey000ce802017-04-29 13:13:27 -060010197 /** @removed */
10198 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010199 public String getChannel() {
10200 return mChannelId;
10201 }
10202
10203 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -040010204 * Returns the id of the channel this notification posts to on TV.
10205 */
10206 public String getChannelId() {
10207 return mChannelId;
10208 }
10209
10210 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010211 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
10212 * If provided, it is used instead of the content intent specified
10213 * at the level of Notification.
10214 */
10215 public TvExtender setContentIntent(PendingIntent intent) {
10216 mContentIntent = intent;
10217 return this;
10218 }
10219
10220 /**
10221 * Returns the TV-specific content intent. If this method returns null, the
10222 * main content intent on the notification should be used.
10223 *
10224 * @see {@link Notification#contentIntent}
10225 */
10226 public PendingIntent getContentIntent() {
10227 return mContentIntent;
10228 }
10229
10230 /**
10231 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
10232 * by the user on TV. If provided, it is used instead of the delete intent specified
10233 * at the level of Notification.
10234 */
10235 public TvExtender setDeleteIntent(PendingIntent intent) {
10236 mDeleteIntent = intent;
10237 return this;
10238 }
10239
10240 /**
10241 * Returns the TV-specific delete intent. If this method returns null, the
10242 * main delete intent on the notification should be used.
10243 *
10244 * @see {@link Notification#deleteIntent}
10245 */
10246 public PendingIntent getDeleteIntent() {
10247 return mDeleteIntent;
10248 }
Rhiannon Malia1a083932018-01-24 15:02:30 -080010249
10250 /**
10251 * Specifies whether this notification should suppress showing a message over top of apps
10252 * outside of the launcher.
10253 */
10254 public TvExtender setSuppressShowOverApps(boolean suppress) {
10255 mSuppressShowOverApps = suppress;
10256 return this;
10257 }
10258
10259 /**
10260 * Returns true if this notification should not show messages over top of apps
10261 * outside of the launcher.
10262 */
10263 public boolean getSuppressShowOverApps() {
10264 return mSuppressShowOverApps;
10265 }
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010266 }
10267
10268 /**
Griff Hazen61a9e862014-05-22 16:05:19 -070010269 * Get an array of Notification objects from a parcelable array bundle field.
10270 * Update the bundle to have a typed array so fetches in the future don't need
10271 * to do an array copy.
10272 */
10273 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
10274 Parcelable[] array = bundle.getParcelableArray(key);
10275 if (array instanceof Notification[] || array == null) {
10276 return (Notification[]) array;
10277 }
10278 Notification[] typedArray = Arrays.copyOf(array, array.length,
10279 Notification[].class);
10280 bundle.putParcelableArray(key, typedArray);
10281 return typedArray;
10282 }
Christoph Studer4600f9b2014-07-22 22:44:43 +020010283
10284 private static class BuilderRemoteViews extends RemoteViews {
10285 public BuilderRemoteViews(Parcel parcel) {
10286 super(parcel);
10287 }
10288
Kenny Guy77320062014-08-27 21:37:15 +010010289 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
10290 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +020010291 }
10292
10293 @Override
10294 public BuilderRemoteViews clone() {
10295 Parcel p = Parcel.obtain();
10296 writeToParcel(p, 0);
10297 p.setDataPosition(0);
10298 BuilderRemoteViews brv = new BuilderRemoteViews(p);
10299 p.recycle();
10300 return brv;
10301 }
10302 }
Adrian Roos70d7aa32017-01-11 15:39:06 -080010303
Selim Cinek384804b2018-04-18 14:31:07 +080010304 /**
10305 * A result object where information about the template that was created is saved.
10306 */
10307 private static class TemplateBindResult {
Selim Cinek1c72fa02018-04-23 18:00:54 +080010308 int mIconMarginEnd;
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010309 boolean mRightIconContainerVisible;
Selim Cinek384804b2018-04-18 14:31:07 +080010310
10311 /**
Selim Cinek1c72fa02018-04-23 18:00:54 +080010312 * Get the margin end that needs to be added to any fields that may overlap
Selim Cinek384804b2018-04-18 14:31:07 +080010313 * with the right actions.
10314 */
Selim Cinek1c72fa02018-04-23 18:00:54 +080010315 public int getIconMarginEnd() {
10316 return mIconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +080010317 }
10318
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010319 /**
10320 * Is the icon container visible on the right size because of the reply button or the
10321 * right icon.
10322 */
10323 public boolean isRightIconContainerVisible() {
10324 return mRightIconContainerVisible;
10325 }
10326
Selim Cinek1c72fa02018-04-23 18:00:54 +080010327 public void setIconMarginEnd(int iconMarginEnd) {
10328 this.mIconMarginEnd = iconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +080010329 }
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010330
10331 public void setRightIconContainerVisible(boolean iconContainerVisible) {
10332 mRightIconContainerVisible = iconContainerVisible;
10333 }
Selim Cinek384804b2018-04-18 14:31:07 +080010334 }
10335
Adrian Roos70d7aa32017-01-11 15:39:06 -080010336 private static class StandardTemplateParams {
10337 boolean hasProgress = true;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010338 CharSequence title;
10339 CharSequence text;
Selim Cinekafeed292017-12-12 17:32:44 -080010340 CharSequence headerTextSecondary;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010341 CharSequence summaryText;
Selim Cinekbee4e072018-05-21 22:06:43 -070010342 int maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek88188f22017-09-19 16:46:56 -070010343 boolean hideLargeIcon;
Selim Cinek384804b2018-04-18 14:31:07 +080010344 boolean hideReplyIcon;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010345 boolean allowColorization = true;
10346 boolean forceDefaultColor = false;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010347
10348 final StandardTemplateParams reset() {
10349 hasProgress = true;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010350 title = null;
10351 text = null;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010352 summaryText = null;
Selim Cinekafeed292017-12-12 17:32:44 -080010353 headerTextSecondary = null;
Selim Cinekbee4e072018-05-21 22:06:43 -070010354 maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010355 allowColorization = true;
10356 forceDefaultColor = false;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010357 return this;
10358 }
10359
10360 final StandardTemplateParams hasProgress(boolean hasProgress) {
10361 this.hasProgress = hasProgress;
10362 return this;
10363 }
10364
10365 final StandardTemplateParams title(CharSequence title) {
10366 this.title = title;
10367 return this;
10368 }
10369
10370 final StandardTemplateParams text(CharSequence text) {
10371 this.text = text;
10372 return this;
10373 }
10374
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010375 final StandardTemplateParams summaryText(CharSequence text) {
10376 this.summaryText = text;
10377 return this;
10378 }
10379
Selim Cinekafeed292017-12-12 17:32:44 -080010380 final StandardTemplateParams headerTextSecondary(CharSequence text) {
10381 this.headerTextSecondary = text;
10382 return this;
10383 }
10384
Selim Cinek384804b2018-04-18 14:31:07 +080010385 final StandardTemplateParams hideLargeIcon(boolean hideLargeIcon) {
10386 this.hideLargeIcon = hideLargeIcon;
Selim Cinek88188f22017-09-19 16:46:56 -070010387 return this;
10388 }
10389
Selim Cinek384804b2018-04-18 14:31:07 +080010390 final StandardTemplateParams hideReplyIcon(boolean hideReplyIcon) {
10391 this.hideReplyIcon = hideReplyIcon;
Selim Cinek88188f22017-09-19 16:46:56 -070010392 return this;
10393 }
10394
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010395 final StandardTemplateParams disallowColorization() {
10396 this.allowColorization = false;
10397 return this;
10398 }
10399
10400 final StandardTemplateParams forceDefaultColor() {
10401 this.forceDefaultColor = true;
10402 return this;
10403 }
10404
Adrian Roos70d7aa32017-01-11 15:39:06 -080010405 final StandardTemplateParams fillTextsFrom(Builder b) {
10406 Bundle extras = b.mN.extras;
Lucas Dupin00be88f2019-01-03 17:50:52 -080010407 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE));
Lucas Dupin06c5e642017-09-13 16:34:58 -070010408
Lucas Dupin06c5e642017-09-13 16:34:58 -070010409 CharSequence text = extras.getCharSequence(EXTRA_BIG_TEXT);
Lucas Dupin00be88f2019-01-03 17:50:52 -080010410 if (TextUtils.isEmpty(text)) {
Lucas Dupin06c5e642017-09-13 16:34:58 -070010411 text = extras.getCharSequence(EXTRA_TEXT);
10412 }
Lucas Dupin00be88f2019-01-03 17:50:52 -080010413 this.text = b.processLegacyText(text);
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010414 this.summaryText = extras.getCharSequence(EXTRA_SUB_TEXT);
Adrian Roos70d7aa32017-01-11 15:39:06 -080010415 return this;
10416 }
Selim Cinekbee4e072018-05-21 22:06:43 -070010417
10418 /**
10419 * Set the maximum lines of remote input history lines allowed.
10420 * @param maxRemoteInputHistory The number of lines.
10421 * @return The builder for method chaining.
10422 */
10423 public StandardTemplateParams setMaxRemoteInputHistory(int maxRemoteInputHistory) {
10424 this.maxRemoteInputHistory = maxRemoteInputHistory;
10425 return this;
10426 }
Adrian Roos70d7aa32017-01-11 15:39:06 -080010427 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010428}