blob: b8d748dce9e6d127e3a65f94945e8d6221ce0b04 [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;
Kenny Guy77320062014-08-27 21:37:15 +010034import android.content.pm.ApplicationInfo;
Dan Sandler732bd6c2016-04-12 14:20:32 -040035import android.content.pm.PackageManager;
Christoph Studer4600f9b2014-07-22 22:44:43 +020036import android.content.pm.PackageManager.NameNotFoundException;
Julia Reynolds13d898c2017-02-02 12:22:05 -050037import android.content.pm.ShortcutInfo;
Jorim Jaggief72a192014-08-26 21:57:46 +020038import android.content.res.ColorStateList;
Anthony Chenad4d1582017-04-10 16:07:58 -070039import android.content.res.Configuration;
40import android.content.res.Resources;
Gus Prevas9cc96602018-10-11 11:24:23 -040041import android.content.res.TypedArray;
Joe Onoratoef1e7762010-09-17 18:38:38 -040042import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010043import android.graphics.Canvas;
Adrian Roosc1a80b02016-04-05 14:54:55 -070044import android.graphics.Color;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010045import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010046import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040047import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040048import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070049import android.media.AudioManager;
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -080050import android.media.PlayerBase;
Jeff Browndba34ba2014-06-24 20:46:03 -070051import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040053import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020054import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050055import android.os.Bundle;
Dianne Hackborn98305522017-05-05 17:53:53 -070056import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.os.Parcel;
58import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040059import android.os.SystemClock;
Selim Cinek6743c0b2017-01-18 18:24:01 -080060import android.os.SystemProperties;
Jeff Sharkey6d515712012-09-20 16:06:08 -070061import android.os.UserHandle;
Adrian Roosc1a80b02016-04-05 14:54:55 -070062import android.text.BidiFormatter;
Selim Cinek60a54252016-02-26 17:03:25 -080063import android.text.SpannableStringBuilder;
64import android.text.Spanned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.text.TextUtils;
Selim Cinek60a54252016-02-26 17:03:25 -080066import android.text.style.AbsoluteSizeSpan;
Selim Cinek89991a22016-03-07 19:51:54 -080067import android.text.style.CharacterStyle;
Selim Cinek981962e2016-07-20 20:41:58 -070068import android.text.style.ForegroundColorSpan;
Selim Cinek60a54252016-02-26 17:03:25 -080069import android.text.style.RelativeSizeSpan;
70import android.text.style.TextAppearanceSpan;
Svet Ganovddb94882016-06-23 19:55:24 -070071import android.util.ArraySet;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040072import android.util.Log;
Tony Mak638430e2018-10-08 19:19:10 +010073import android.util.Pair;
Dan Sandler50128532015-12-08 15:42:41 -050074import android.util.SparseArray;
Yi Jin6b514142017-10-30 14:54:12 -070075import android.util.proto.ProtoOutputStream;
Griff Hazen61a9e862014-05-22 16:05:19 -070076import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080077import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080078import android.view.View;
Selim Cinek954cc232016-05-20 13:29:23 -070079import android.view.ViewGroup;
Jeff Sharkey1c400132011-08-05 14:50:13 -070080import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import android.widget.RemoteViews;
82
Griff Hazen959591e2014-05-15 22:26:18 -070083import com.android.internal.R;
Selim Cinek389edcd2017-05-11 19:16:44 -070084import com.android.internal.annotations.VisibleForTesting;
Svet Ganovddb94882016-06-23 19:55:24 -070085import com.android.internal.util.ArrayUtils;
Lucas Dupina291d192018-06-07 13:59:42 -070086import com.android.internal.util.ContrastColorUtil;
Adrian Roos0bc3f6a2017-03-06 11:54:05 -080087import com.android.internal.util.Preconditions;
Griff Hazen959591e2014-05-15 22:26:18 -070088
Tor Norbyed9273d62013-05-30 15:59:53 -070089import java.lang.annotation.Retention;
90import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020091import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050092import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070093import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070094import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070095import java.util.List;
Julia Reynolds7217dc92018-03-07 12:12:09 -050096import java.util.Objects;
Dan Sandler50128532015-12-08 15:42:41 -050097import java.util.Set;
Jeff Sharkey23b31182018-04-18 21:32:12 -060098import java.util.function.Consumer;
Joe Onorato561d3852010-11-20 18:09:34 -080099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100/**
101 * A class that represents how a persistent notification is to be presented to
102 * the user using the {@link android.app.NotificationManager}.
103 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800104 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
105 * easier to construct Notifications.</p>
106 *
Joe Fernandez558459f2011-10-13 16:47:36 -0700107 * <div class="special reference">
108 * <h3>Developer Guides</h3>
109 * <p>For a guide to creating notifications, read the
110 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
111 * developer guide.</p>
112 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 */
114public class Notification implements Parcelable
115{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -0400116 private static final String TAG = "Notification";
117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -0400119 * An activity that provides a user interface for adjusting notification preferences for its
Julia Reynolds3aedded2017-03-31 14:42:09 -0400120 * containing application.
Daniel Sandler01df1c62014-06-09 10:54:01 -0400121 */
122 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
123 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
124 = "android.intent.category.NOTIFICATION_PREFERENCES";
125
126 /**
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500127 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
128 * contain a {@link NotificationChannel#getId() channel id} that can be used to narrow down
Julia Reynolds3aedded2017-03-31 14:42:09 -0400129 * what settings should be shown in the target app.
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500130 */
131 public static final String EXTRA_CHANNEL_ID = "android.intent.extra.CHANNEL_ID";
132
133 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -0400134 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds005c8b92017-08-24 10:35:53 -0400135 * contain a {@link NotificationChannelGroup#getId() group id} that can be used to narrow down
136 * what settings should be shown in the target app.
137 */
138 public static final String EXTRA_CHANNEL_GROUP_ID = "android.intent.extra.CHANNEL_GROUP_ID";
139
140 /**
141 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds3aedded2017-03-31 14:42:09 -0400142 * contain the tag provided to {@link NotificationManager#notify(String, int, Notification)}
143 * that can be used to narrow down what settings should be shown in the target app.
144 */
145 public static final String EXTRA_NOTIFICATION_TAG = "android.intent.extra.NOTIFICATION_TAG";
146
147 /**
148 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
149 * contain the id provided to {@link NotificationManager#notify(String, int, Notification)}
150 * that can be used to narrow down what settings should be shown in the target app.
151 */
152 public static final String EXTRA_NOTIFICATION_ID = "android.intent.extra.NOTIFICATION_ID";
153
154 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 * Use all default values (where applicable).
156 */
157 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 /**
160 * Use the default notification sound. This will ignore any given
161 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500162 *
Chris Wren47c20a12014-06-18 17:27:29 -0400163 * <p>
164 * A notification that is noisy is more likely to be presented as a heads-up notification.
165 * </p>
166 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500168 */
169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 public static final int DEFAULT_SOUND = 1;
171
172 /**
173 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500174 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700175 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500176 *
Chris Wren47c20a12014-06-18 17:27:29 -0400177 * <p>
178 * A notification that vibrates is more likely to be presented as a heads-up notification.
179 * </p>
180 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500182 */
183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 /**
187 * Use the default notification lights. This will ignore the
188 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
189 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500190 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500192 */
193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200197 * Maximum length of CharSequences accepted by Builder and friends.
198 *
199 * <p>
200 * Avoids spamming the system with overly large strings such as full e-mails.
201 */
202 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
203
204 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800205 * Maximum entries of reply text that are accepted by Builder and friends.
206 */
207 private static final int MAX_REPLY_HISTORY = 5;
208
Tony Mak09db2ea2018-06-27 18:12:48 +0100209 /**
Gustav Sennton1463d832018-11-06 16:12:48 +0000210 * Maximum number of (generic) action buttons in a notification (contextual action buttons are
211 * handled separately).
Tony Mak09db2ea2018-06-27 18:12:48 +0100212 * @hide
213 */
214 public static final int MAX_ACTION_BUTTONS = 3;
Selim Cinekde4de0e2018-01-24 16:21:07 -0800215
216 /**
217 * If the notification contained an unsent draft for a RemoteInput when the user clicked on it,
218 * we're adding the draft as a String extra to the {@link #contentIntent} using this key.
219 *
220 * <p>Apps may use this extra to prepopulate text fields in the app, where the user usually
221 * sends messages.</p>
222 */
223 public static final String EXTRA_REMOTE_INPUT_DRAFT = "android.remoteInputDraft";
224
Adrian Roose458aa82015-12-08 16:17:19 -0800225 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500226 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800227 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500228 * Default value: {@link System#currentTimeMillis() Now}.
229 *
230 * Choose a timestamp that will be most relevant to the user. For most finite events, this
231 * corresponds to the time the event happened (or will happen, in the case of events that have
232 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800233 * timestamped according to when the activity began.
234 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500235 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800236 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500237 * <ul>
238 * <li>Notification of a new chat message should be stamped when the message was received.</li>
239 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
240 * <li>Notification of a completed file download should be stamped when the download finished.</li>
241 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
242 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
243 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800244 * </ul>
245 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700246 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
247 * anymore by default and must be opted into by using
248 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 */
250 public long when;
251
252 /**
Selim Cinekb85f36fd2016-04-20 18:46:36 -0700253 * The creation time of the notification
254 */
255 private long creationTime;
256
257 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400259 *
260 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 */
Dan Sandler86647982015-05-13 23:41:13 -0400262 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700263 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 public int icon;
265
266 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800267 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
268 * leave it at its default value of 0.
269 *
270 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700271 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800272 */
273 public int iconLevel;
274
275 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500276 * The number of events that this notification represents. For example, in a new mail
277 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800278 *
Julia Reynolds30331982017-04-27 10:12:50 -0400279 * The system may or may not use this field to modify the appearance of the notification.
Julia Reynolds13d898c2017-02-02 12:22:05 -0500280 * Starting with {@link android.os.Build.VERSION_CODES#O}, the number may be displayed as a
281 * badge icon in Launchers that support badging.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 */
Julia Reynolds30331982017-04-27 10:12:50 -0400283 public int number = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284
285 /**
286 * The intent to execute when the expanded status entry is clicked. If
287 * this is an activity, it must include the
288 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800289 * that you take care of task management as described in the
290 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800291 * Stack</a> document. In particular, make sure to read the notification section
292 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
293 * Notifications</a> for the correct ways to launch an application from a
294 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 */
296 public PendingIntent contentIntent;
297
298 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500299 * The intent to execute when the notification is explicitly dismissed by the user, either with
300 * the "Clear All" button or by swiping it away individually.
301 *
302 * This probably shouldn't be launching an activity since several of those will be sent
303 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 */
305 public PendingIntent deleteIntent;
306
307 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700308 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800309 *
Chris Wren47c20a12014-06-18 17:27:29 -0400310 * <p>
311 * The system UI may choose to display a heads-up notification, instead of
312 * launching this intent, while the user is using the device.
313 * </p>
314 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800315 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400316 */
317 public PendingIntent fullScreenIntent;
318
319 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400320 * Text that summarizes this notification for accessibility services.
321 *
322 * As of the L release, this text is no longer shown on screen, but it is still useful to
323 * accessibility services (where it serves as an audible announcement of the notification's
324 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400325 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800326 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 */
328 public CharSequence tickerText;
329
330 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400331 * Formerly, a view showing the {@link #tickerText}.
332 *
333 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400334 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400335 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800336 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400337
338 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400339 * The view that will represent this notification in the notification list (which is pulled
340 * down from the status bar).
341 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500342 * As of N, this field may be null. The notification view is determined by the inputs
343 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400344 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400346 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 public RemoteViews contentView;
348
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400349 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400350 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400351 * opportunity to show more detail. The system UI may choose to show this
352 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400353 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500354 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400355 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
356 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400357 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400358 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400359 public RemoteViews bigContentView;
360
Chris Wren8fd39ec2014-02-27 17:43:26 -0500361
362 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400363 * A medium-format version of {@link #contentView}, providing the Notification an
364 * opportunity to add action buttons to contentView. At its discretion, the system UI may
365 * choose to show this as a heads-up notification, which will pop up so the user can see
366 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400367 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500368 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400369 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
370 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500371 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400372 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500373 public RemoteViews headsUpContentView;
374
Julia Reynoldsfc640012018-02-21 12:25:27 -0500375 private boolean mUsesStandardHeader;
376
377 private static final ArraySet<Integer> STANDARD_LAYOUTS = new ArraySet<>();
378 static {
379 STANDARD_LAYOUTS.add(R.layout.notification_template_material_base);
380 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_base);
381 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_picture);
382 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_text);
383 STANDARD_LAYOUTS.add(R.layout.notification_template_material_inbox);
384 STANDARD_LAYOUTS.add(R.layout.notification_template_material_messaging);
385 STANDARD_LAYOUTS.add(R.layout.notification_template_material_media);
386 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_media);
Julia Reynoldsfc640012018-02-21 12:25:27 -0500387 STANDARD_LAYOUTS.add(R.layout.notification_template_header);
Julia Reynoldsfc640012018-02-21 12:25:27 -0500388 }
389
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400390 /**
Dan Sandler86647982015-05-13 23:41:13 -0400391 * A large bitmap to be shown in the notification content area.
392 *
393 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 */
Dan Sandler86647982015-05-13 23:41:13 -0400395 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800396 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397
398 /**
399 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500400 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400402 * A notification that is noisy is more likely to be presented as a heads-up notification.
403 * </p>
404 *
405 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500406 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500408 * @deprecated use {@link NotificationChannel#getSound()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500410 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 public Uri sound;
412
413 /**
414 * Use this constant as the value for audioStreamType to request that
415 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700416 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400417 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500418 * @deprecated Use {@link NotificationChannel#getAudioAttributes()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700420 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 public static final int STREAM_DEFAULT = -1;
422
423 /**
424 * The audio stream type to use when playing the sound.
425 * Should be one of the STREAM_ constants from
426 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400427 *
428 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700430 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 public int audioStreamType = STREAM_DEFAULT;
432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400434 * The default value of {@link #audioAttributes}.
435 */
436 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
437 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
438 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
439 .build();
440
441 /**
442 * The {@link AudioAttributes audio attributes} to use when playing the sound.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500443 *
444 * @deprecated use {@link NotificationChannel#getAudioAttributes()} instead.
John Spurlockc0650f022014-07-19 13:22:39 -0400445 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500446 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -0400447 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
448
449 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500450 * The pattern with which to vibrate.
451 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 * <p>
453 * To vibrate the default pattern, see {@link #defaults}.
454 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500455 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 * @see android.os.Vibrator#vibrate(long[],int)
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500457 * @deprecated use {@link NotificationChannel#getVibrationPattern()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500459 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 public long[] vibrate;
461
462 /**
463 * The color of the led. The hardware will do its best approximation.
464 *
465 * @see #FLAG_SHOW_LIGHTS
466 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500467 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 */
Tor Norbye80756e32015-03-02 09:39:27 -0800469 @ColorInt
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500470 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 public int ledARGB;
472
473 /**
474 * The number of milliseconds for the LED to be on while it's flashing.
475 * The hardware will do its best approximation.
476 *
477 * @see #FLAG_SHOW_LIGHTS
478 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500479 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500481 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 public int ledOnMS;
483
484 /**
485 * The number of milliseconds for the LED to be off while it's flashing.
486 * The hardware will do its best approximation.
487 *
488 * @see #FLAG_SHOW_LIGHTS
489 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500490 *
491 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500493 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 public int ledOffMS;
495
496 /**
497 * Specifies which values should be taken from the defaults.
498 * <p>
499 * To set, OR the desired from {@link #DEFAULT_SOUND},
500 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
501 * values, use {@link #DEFAULT_ALL}.
502 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500503 *
504 * @deprecated use {@link NotificationChannel#getSound()} and
505 * {@link NotificationChannel#shouldShowLights()} and
506 * {@link NotificationChannel#shouldVibrate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500508 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 public int defaults;
510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 /**
512 * Bit to be bitwise-ored into the {@link #flags} field that should be
513 * set if you want the LED on for this notification.
514 * <ul>
515 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
516 * or 0 for both ledOnMS and ledOffMS.</li>
517 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
518 * <li>To flash the LED, pass the number of milliseconds that it should
519 * be on and off to ledOnMS and ledOffMS.</li>
520 * </ul>
521 * <p>
522 * Since hardware varies, you are not guaranteed that any of the values
Ricardo Loo Forondaf8f6d0a2018-01-25 08:42:43 -0800523 * you pass are honored exactly. Use the system defaults if possible
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 * because they will be set to values that work on any given hardware.
525 * <p>
526 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500527 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500528 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500530 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
532
533 /**
534 * Bit to be bitwise-ored into the {@link #flags} field that should be
535 * set if this notification is in reference to something that is ongoing,
536 * like a phone call. It should not be set if this notification is in
537 * reference to something that happened at a particular point in time,
538 * like a missed phone call.
539 */
540 public static final int FLAG_ONGOING_EVENT = 0x00000002;
541
542 /**
543 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700544 * the audio will be repeated until the notification is
545 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 */
547 public static final int FLAG_INSISTENT = 0x00000004;
548
549 /**
550 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700551 * set if you would only like the sound, vibrate and ticker to be played
552 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 */
554 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
555
556 /**
557 * Bit to be bitwise-ored into the {@link #flags} field that should be
558 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500559 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 */
561 public static final int FLAG_AUTO_CANCEL = 0x00000010;
562
563 /**
564 * Bit to be bitwise-ored into the {@link #flags} field that should be
565 * set if the notification should not be canceled when the user clicks
566 * the Clear all button.
567 */
568 public static final int FLAG_NO_CLEAR = 0x00000020;
569
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700570 /**
571 * Bit to be bitwise-ored into the {@link #flags} field that should be
572 * set if this notification represents a currently running service. This
573 * will normally be set for you by {@link Service#startForeground}.
574 */
575 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
576
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400577 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500578 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800579 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500580 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400581 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700582 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500583 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400584
Griff Hazendfcb0802014-02-11 12:00:00 -0800585 /**
586 * Bit to be bitswise-ored into the {@link #flags} field that should be
587 * set if this notification is relevant to the current device only
588 * and it is not recommended that it bridge to other devices.
589 */
590 public static final int FLAG_LOCAL_ONLY = 0x00000100;
591
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700592 /**
593 * Bit to be bitswise-ored into the {@link #flags} field that should be
594 * set if this notification is the group summary for a group of notifications.
595 * Grouped notifications may display in a cluster or stack on devices which
596 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
597 */
598 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
599
Julia Reynoldse46bb372016-03-17 11:05:58 -0400600 /**
601 * Bit to be bitswise-ored into the {@link #flags} field that should be
602 * set if this notification is the group summary for an auto-group of notifications.
603 *
604 * @hide
605 */
606 @SystemApi
607 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
608
Julia Reynolds4db59552017-06-30 13:34:01 -0400609 /**
610 * @hide
611 */
612 public static final int FLAG_CAN_COLORIZE = 0x00000800;
613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 public int flags;
615
Tor Norbyed9273d62013-05-30 15:59:53 -0700616 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700617 @IntDef(prefix = { "PRIORITY_" }, value = {
618 PRIORITY_DEFAULT,
619 PRIORITY_LOW,
620 PRIORITY_MIN,
621 PRIORITY_HIGH,
622 PRIORITY_MAX
623 })
Tor Norbyed9273d62013-05-30 15:59:53 -0700624 @Retention(RetentionPolicy.SOURCE)
625 public @interface Priority {}
626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500628 * Default notification {@link #priority}. If your application does not prioritize its own
629 * notifications, use this value for all notifications.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500630 *
631 * @deprecated use {@link NotificationManager#IMPORTANCE_DEFAULT} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500632 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500633 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500634 public static final int PRIORITY_DEFAULT = 0;
635
636 /**
637 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
638 * items smaller, or at a different position in the list, compared with your app's
639 * {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500640 *
641 * @deprecated use {@link NotificationManager#IMPORTANCE_LOW} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500642 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500643 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500644 public static final int PRIORITY_LOW = -1;
645
646 /**
647 * Lowest {@link #priority}; these items might not be shown to the user except under special
648 * circumstances, such as detailed notification logs.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500649 *
650 * @deprecated use {@link NotificationManager#IMPORTANCE_MIN} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500651 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500652 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500653 public static final int PRIORITY_MIN = -2;
654
655 /**
656 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
657 * show these items larger, or at a different position in notification lists, compared with
658 * your app's {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500659 *
660 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500661 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500662 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500663 public static final int PRIORITY_HIGH = 1;
664
665 /**
666 * Highest {@link #priority}, for your application's most important items that require the
667 * user's prompt attention or input.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500668 *
669 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500670 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500671 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500672 public static final int PRIORITY_MAX = 2;
673
674 /**
675 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800676 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500677 * Priority is an indication of how much of the user's valuable attention should be consumed by
678 * this notification. Low-priority notifications may be hidden from the user in certain
679 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500680 * system will make a determination about how to interpret this priority when presenting
681 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400682 *
683 * <p>
684 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
685 * as a heads-up notification.
686 * </p>
687 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500688 * @deprecated use {@link NotificationChannel#getImportance()} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500689 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700690 @Priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500691 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500692 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800693
Dan Sandler26e81cf2014-05-06 10:01:27 -0400694 /**
695 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
696 * to be applied by the standard Style templates when presenting this notification.
697 *
698 * The current template design constructs a colorful header image by overlaying the
699 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
700 * ignored.
701 */
Tor Norbye80756e32015-03-02 09:39:27 -0800702 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400703 public int color = COLOR_DEFAULT;
704
705 /**
706 * Special value of {@link #color} telling the system not to decorate this notification with
707 * any special color but instead use default colors when presenting this notification.
708 */
Tor Norbye80756e32015-03-02 09:39:27 -0800709 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400710 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600711
712 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800713 * Special value of {@link #color} used as a place holder for an invalid color.
Selim Cinek99104832017-01-25 14:47:33 -0800714 * @hide
Adrian Roos4ff3b122016-02-01 12:26:13 -0800715 */
716 @ColorInt
Selim Cinek99104832017-01-25 14:47:33 -0800717 public static final int COLOR_INVALID = 1;
Adrian Roos4ff3b122016-02-01 12:26:13 -0800718
719 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700720 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
721 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600722 * lockscreen).
723 *
724 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
725 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
726 * shown in all situations, but the contents are only available if the device is unlocked for
727 * the appropriate user.
728 *
729 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
730 * can be read even in an "insecure" context (that is, above a secure lockscreen).
731 * To modify the public version of this notification—for example, to redact some portions—see
732 * {@link Builder#setPublicVersion(Notification)}.
733 *
734 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
735 * and ticker until the user has bypassed the lockscreen.
736 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600737 public @Visibility int visibility;
738
739 /** @hide */
740 @IntDef(prefix = { "VISIBILITY_" }, value = {
741 VISIBILITY_PUBLIC,
742 VISIBILITY_PRIVATE,
743 VISIBILITY_SECRET,
744 })
745 @Retention(RetentionPolicy.SOURCE)
746 public @interface Visibility {}
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600747
Griff Hazenfc3922d2014-08-20 11:56:44 -0700748 /**
749 * Notification visibility: Show this notification in its entirety on all lockscreens.
750 *
751 * {@see #visibility}
752 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600753 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700754
755 /**
756 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
757 * private information on secure lockscreens.
758 *
759 * {@see #visibility}
760 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600761 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700762
763 /**
764 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
765 *
766 * {@see #visibility}
767 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600768 public static final int VISIBILITY_SECRET = -1;
769
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500770 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400771 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500772 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400773 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500774
775 /**
Adora Zhangaa90e872018-03-12 14:07:50 -0700776 * Notification category: map turn-by-turn navigation.
777 */
778 public static final String CATEGORY_NAVIGATION = "navigation";
779
780 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400781 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500782 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400783 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500784
785 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400786 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500787 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400788 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500789
790 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400791 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500792 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400793 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500794
795 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400796 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500797 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400798 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500799
800 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400801 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500802 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400803 public static final String CATEGORY_ALARM = "alarm";
804
805 /**
806 * Notification category: progress of a long-running background operation.
807 */
808 public static final String CATEGORY_PROGRESS = "progress";
809
810 /**
811 * Notification category: social network or sharing update.
812 */
813 public static final String CATEGORY_SOCIAL = "social";
814
815 /**
816 * Notification category: error in background operation or authentication status.
817 */
818 public static final String CATEGORY_ERROR = "err";
819
820 /**
821 * Notification category: media transport control for playback.
822 */
823 public static final String CATEGORY_TRANSPORT = "transport";
824
825 /**
826 * Notification category: system or device status update. Reserved for system use.
827 */
828 public static final String CATEGORY_SYSTEM = "sys";
829
830 /**
831 * Notification category: indication of running background service.
832 */
833 public static final String CATEGORY_SERVICE = "service";
834
835 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400836 * Notification category: a specific, timely recommendation for a single thing.
837 * For example, a news app might want to recommend a news story it believes the user will
838 * want to read next.
839 */
840 public static final String CATEGORY_RECOMMENDATION = "recommendation";
841
842 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400843 * Notification category: ongoing information about device or contextual status.
844 */
845 public static final String CATEGORY_STATUS = "status";
846
847 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400848 * Notification category: user-scheduled reminder.
849 */
850 public static final String CATEGORY_REMINDER = "reminder";
851
852 /**
Adora Zhangaa90e872018-03-12 14:07:50 -0700853 * Notification category: extreme car emergencies.
854 * @hide
855 */
856 @SystemApi
857 public static final String CATEGORY_CAR_EMERGENCY = "car_emergency";
858
859 /**
860 * Notification category: car warnings.
861 * @hide
862 */
863 @SystemApi
864 public static final String CATEGORY_CAR_WARNING = "car_warning";
865
866 /**
867 * Notification category: general car system information.
868 * @hide
869 */
870 @SystemApi
871 public static final String CATEGORY_CAR_INFORMATION = "car_information";
872
873 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400874 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
875 * that best describes this Notification. May be used by the system for ranking and filtering.
876 */
877 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500878
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100879 @UnsupportedAppUsage
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700880 private String mGroupKey;
881
882 /**
883 * Get the key used to group this notification into a cluster or stack
884 * with other notifications on devices which support such rendering.
885 */
886 public String getGroup() {
887 return mGroupKey;
888 }
889
890 private String mSortKey;
891
892 /**
893 * Get a sort key that orders this notification among other notifications from the
894 * same package. This can be useful if an external sort was already applied and an app
895 * would like to preserve this. Notifications will be sorted lexicographically using this
896 * value, although providing different priorities in addition to providing sort key may
897 * cause this value to be ignored.
898 *
899 * <p>This sort key can also be used to order members of a notification group. See
900 * {@link Builder#setGroup}.
901 *
902 * @see String#compareTo(String)
903 */
904 public String getSortKey() {
905 return mSortKey;
906 }
907
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500908 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400909 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400910 * <p>
911 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
912 * APIs, and are intended to be used by
913 * {@link android.service.notification.NotificationListenerService} implementations to extract
914 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500915 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400916 public Bundle extras = new Bundle();
917
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400918 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700919 * All pending intents in the notification as the system needs to be able to access them but
920 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700921 * custom parcelable objects.
922 *
923 * @hide
924 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100925 @UnsupportedAppUsage
Felipe Lemedd85da62016-06-28 11:29:54 -0700926 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700927
928 /**
Dianne Hackborn98305522017-05-05 17:53:53 -0700929 * Token identifying the notification that is applying doze/bgcheck whitelisting to the
930 * pending intents inside of it, so only those will get the behavior.
931 *
932 * @hide
933 */
Adrian Roosfb921842017-10-26 14:49:56 +0200934 private IBinder mWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -0700935
936 /**
937 * Must be set by a process to start associating tokens with Notification objects
938 * coming in to it. This is set by NotificationManagerService.
939 *
940 * @hide
941 */
942 static public IBinder processWhitelistToken;
943
944 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400945 * {@link #extras} key: this is the title of the notification,
946 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
947 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500948 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400949
950 /**
951 * {@link #extras} key: this is the title of the notification when shown in expanded form,
952 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
953 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400954 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400955
956 /**
957 * {@link #extras} key: this is the main text payload, as supplied to
958 * {@link Builder#setContentText(CharSequence)}.
959 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500960 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400961
962 /**
963 * {@link #extras} key: this is a third line of text, as supplied to
964 * {@link Builder#setSubText(CharSequence)}.
965 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400966 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400967
968 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800969 * {@link #extras} key: this is the remote input history, as supplied to
970 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700971 *
972 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
973 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
974 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
975 * notifications once the other party has responded).
976 *
977 * The extra with this key is of type CharSequence[] and contains the most recent entry at
978 * the 0 index, the second most recent at the 1 index, etc.
979 *
980 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800981 */
982 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
983
984 /**
Kenny Guya0f6de82018-04-06 16:20:16 +0100985 * {@link #extras} key: boolean as supplied to
986 * {@link Builder#setShowRemoteInputSpinner(boolean)}.
987 *
988 * If set to true, then the view displaying the remote input history from
989 * {@link Builder#setRemoteInputHistory(CharSequence[])} will have a progress spinner.
990 *
991 * @see Builder#setShowRemoteInputSpinner(boolean)
992 * @hide
993 */
994 public static final String EXTRA_SHOW_REMOTE_INPUT_SPINNER = "android.remoteInputSpinner";
995
996 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +0100997 * {@link #extras} key: boolean as supplied to
998 * {@link Builder#setHideSmartReplies(boolean)}.
999 *
1000 * If set to true, then any smart reply buttons will be hidden.
1001 *
1002 * @see Builder#setHideSmartReplies(boolean)
1003 * @hide
1004 */
1005 public static final String EXTRA_HIDE_SMART_REPLIES = "android.hideSmartReplies";
1006
1007 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001008 * {@link #extras} key: this is a small piece of additional text as supplied to
1009 * {@link Builder#setContentInfo(CharSequence)}.
1010 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001011 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001012
1013 /**
1014 * {@link #extras} key: this is a line of summary information intended to be shown
1015 * alongside expanded notifications, as supplied to (e.g.)
1016 * {@link BigTextStyle#setSummaryText(CharSequence)}.
1017 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001018 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001019
1020 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02001021 * {@link #extras} key: this is the longer text shown in the big form of a
1022 * {@link BigTextStyle} notification, as supplied to
1023 * {@link BigTextStyle#bigText(CharSequence)}.
1024 */
1025 public static final String EXTRA_BIG_TEXT = "android.bigText";
1026
1027 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001028 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
1029 * supplied to {@link Builder#setSmallIcon(int)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -04001030 *
1031 * @deprecated Use {@link #getSmallIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001032 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04001033 @Deprecated
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001034 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001035
1036 /**
1037 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
1038 * notification payload, as
1039 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -04001040 *
1041 * @deprecated Use {@link #getLargeIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001042 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04001043 @Deprecated
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001044 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001045
1046 /**
1047 * {@link #extras} key: this is a bitmap to be used instead of the one from
1048 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
1049 * shown in its expanded form, as supplied to
1050 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
1051 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001052 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001053
1054 /**
1055 * {@link #extras} key: this is the progress value supplied to
1056 * {@link Builder#setProgress(int, int, boolean)}.
1057 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001058 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001059
1060 /**
1061 * {@link #extras} key: this is the maximum value supplied to
1062 * {@link Builder#setProgress(int, int, boolean)}.
1063 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001064 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001065
1066 /**
1067 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
1068 * {@link Builder#setProgress(int, int, boolean)}.
1069 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001070 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001071
1072 /**
1073 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
1074 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
1075 * {@link Builder#setUsesChronometer(boolean)}.
1076 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001077 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001078
1079 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08001080 * {@link #extras} key: whether the chronometer set on the notification should count down
1081 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -07001082 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -08001083 */
Adrian Roos96b7e202016-05-17 13:50:38 -07001084 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -08001085
1086 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001087 * {@link #extras} key: whether {@link #when} should be shown,
1088 * as supplied to {@link Builder#setShowWhen(boolean)}.
1089 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001090 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001091
1092 /**
1093 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
1094 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
1095 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001096 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001097
1098 /**
1099 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
1100 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
1101 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001102 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -04001103
1104 /**
1105 * {@link #extras} key: A string representing the name of the specific
1106 * {@link android.app.Notification.Style} used to create this notification.
1107 */
Chris Wren91ad5632013-06-05 15:05:57 -04001108 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001109
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001110 /**
Chris Wrene6c48932014-09-29 17:19:27 -04001111 * {@link #extras} key: A String array containing the people that this notification relates to,
1112 * each of which was supplied to {@link Builder#addPerson(String)}.
Selim Cineke7238dd2017-12-14 17:48:32 -08001113 *
1114 * @deprecated the actual objects are now in {@link #EXTRA_PEOPLE_LIST}
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001115 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001116 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001117
1118 /**
Selim Cineke7238dd2017-12-14 17:48:32 -08001119 * {@link #extras} key: An arrayList of {@link Person} objects containing the people that
1120 * this notification relates to.
1121 */
1122 public static final String EXTRA_PEOPLE_LIST = "android.people.list";
1123
1124 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04001125 * Allow certain system-generated notifications to appear before the device is provisioned.
1126 * Only available to notifications coming from the android package.
1127 * @hide
1128 */
Maurice Lam96c10032017-03-29 15:42:38 -07001129 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001130 @RequiresPermission(android.Manifest.permission.NOTIFICATION_DURING_SETUP)
John Spurlockfd7f1e02014-03-18 16:41:57 -04001131 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
1132
1133 /**
Robin Leed107af62018-04-27 13:55:56 +02001134 * {@link #extras} key:
1135 * flat {@link String} representation of a {@link android.content.ContentUris content URI}
1136 * pointing to an image that can be displayed in the background when the notification is
1137 * selected. Used on television platforms. The URI must point to an image stream suitable for
1138 * passing into {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001139 * BitmapFactory.decodeStream}; all other content types will be ignored.
Jose Limae9e3b3b2014-05-18 23:44:50 -07001140 */
1141 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
1142
1143 /**
Dan Sandler842dd772014-05-15 09:36:47 -04001144 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -07001145 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -04001146 * {@link android.app.Notification.MediaStyle} notification.
1147 */
1148 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
1149
1150 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +01001151 * {@link #extras} key: the indices of actions to be shown in the compact view,
1152 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
1153 */
1154 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
1155
Christoph Studer943aa672014-08-03 20:31:16 +02001156 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04001157 * {@link #extras} key: the username to be displayed for all messages sent by the user including
1158 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -07001159 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1160 * {@link CharSequence}
Selim Cinekcb8b9852017-12-15 18:01:52 -08001161 *
1162 * @deprecated use {@link #EXTRA_MESSAGING_PERSON}
Alex Hillsfc737de2016-03-23 17:33:02 -04001163 */
1164 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
1165
1166 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08001167 * {@link #extras} key: the person to be displayed for all messages sent by the user including
1168 * direct replies
1169 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1170 * {@link Person}
1171 */
1172 public static final String EXTRA_MESSAGING_PERSON = "android.messagingUser";
1173
1174 /**
Adrian Roos96b7e202016-05-17 13:50:38 -07001175 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -04001176 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -04001177 */
Alex Hillsd9b04d92016-04-11 16:38:16 -04001178 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -04001179
1180 /**
1181 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
1182 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -07001183 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1184 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -04001185 */
1186 public static final String EXTRA_MESSAGES = "android.messages";
1187
1188 /**
Adrian Roos437cd562017-01-18 15:47:03 -08001189 * {@link #extras} key: an array of
1190 * {@link android.app.Notification.MessagingStyle#addHistoricMessage historic}
1191 * {@link android.app.Notification.MessagingStyle.Message} bundles provided by a
1192 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1193 * array of bundles.
1194 */
1195 public static final String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
1196
1197 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08001198 * {@link #extras} key: whether the {@link android.app.Notification.MessagingStyle} notification
1199 * represents a group conversation.
1200 */
1201 public static final String EXTRA_IS_GROUP_CONVERSATION = "android.isGroupConversation";
1202
1203 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08001204 * {@link #extras} key: whether the notification should be colorized as
1205 * supplied to {@link Builder#setColorized(boolean)}}.
1206 */
1207 public static final String EXTRA_COLORIZED = "android.colorized";
1208
1209 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001210 * @hide
1211 */
1212 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
1213
Selim Cinek247fa012016-02-18 09:50:48 -08001214 /**
1215 * @hide
1216 */
1217 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
1218
Shane Brennan472a3b32016-12-12 15:28:10 -08001219 /**
Selim Cinekd0426622017-07-11 13:19:59 +02001220 * @hide
1221 */
1222 public static final String EXTRA_REDUCED_IMAGES = "android.reduced.images";
1223
1224 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001225 * {@link #extras} key: the audio contents of this notification.
1226 *
1227 * This is for use when rendering the notification on an audio-focused interface;
1228 * the audio contents are a complete sound sample that contains the contents/body of the
1229 * notification. This may be used in substitute of a Text-to-Speech reading of the
1230 * notification. For example if the notification represents a voice message this should point
1231 * to the audio of that message.
1232 *
1233 * The data stored under this key should be a String representation of a Uri that contains the
1234 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1235 *
1236 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1237 * has a field for holding data URI. That field can be used for audio.
1238 * See {@code Message#setData}.
1239 *
1240 * Example usage:
1241 * <pre>
1242 * {@code
1243 * Notification.Builder myBuilder = (build your Notification as normal);
1244 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1245 * }
1246 * </pre>
1247 */
1248 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1249
Dan Sandler80eaa592016-04-14 23:34:54 -04001250 /** @hide */
1251 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001252 @RequiresPermission(android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME)
Dan Sandler732bd6c2016-04-12 14:20:32 -04001253 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1254
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001255 /**
Julia Reynolds3c7de112018-03-28 09:33:13 -04001256 * This is set on the notifications shown by system_server about apps running foreground
1257 * services. It indicates that the notification should be shown
1258 * only if any of the given apps do not already have a properly tagged
1259 * {@link #FLAG_FOREGROUND_SERVICE} notification currently visible to the user.
1260 * This is a string array of all package names of the apps.
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001261 * @hide
1262 */
1263 public static final String EXTRA_FOREGROUND_APPS = "android.foregroundApps";
1264
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001265 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04001266 private Icon mSmallIcon;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001267 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04001268 private Icon mLargeIcon;
1269
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001270 @UnsupportedAppUsage
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001271 private String mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001272 private long mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001273
Julia Reynolds13d898c2017-02-02 12:22:05 -05001274 private String mShortcutId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04001275 private CharSequence mSettingsText;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001276
Mady Mellorc39b4ae2019-01-09 17:11:37 -08001277 private BubbleMetadata mBubbleMetadata;
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04001278
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001279 /** @hide */
1280 @IntDef(prefix = { "GROUP_ALERT_" }, value = {
1281 GROUP_ALERT_ALL, GROUP_ALERT_CHILDREN, GROUP_ALERT_SUMMARY
1282 })
1283 @Retention(RetentionPolicy.SOURCE)
1284 public @interface GroupAlertBehavior {}
1285
1286 /**
1287 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all notifications in a
1288 * group with sound or vibration ought to make sound or vibrate (respectively), so this
1289 * notification will not be muted when it is in a group.
1290 */
1291 public static final int GROUP_ALERT_ALL = 0;
1292
1293 /**
1294 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all children
1295 * notification in a group should be silenced (no sound or vibration) even if they are posted
1296 * to a {@link NotificationChannel} that has sound and/or vibration. Use this constant to
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001297 * mute this notification if this notification is a group child. This must be applied to all
1298 * children notifications you want to mute.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001299 *
1300 * <p> For example, you might want to use this constant if you post a number of children
1301 * notifications at once (say, after a periodic sync), and only need to notify the user
1302 * audibly once.
1303 */
1304 public static final int GROUP_ALERT_SUMMARY = 1;
1305
1306 /**
1307 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that the summary
1308 * notification in a group should be silenced (no sound or vibration) even if they are
1309 * posted to a {@link NotificationChannel} that has sound and/or vibration. Use this constant
1310 * to mute this notification if this notification is a group summary.
1311 *
1312 * <p>For example, you might want to use this constant if only the children notifications
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001313 * in your group have content and the summary is only used to visually group notifications
1314 * rather than to alert the user that new information is available.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001315 */
1316 public static final int GROUP_ALERT_CHILDREN = 2;
1317
Julia Reynolds2f431e22017-06-07 14:12:09 +00001318 private int mGroupAlertBehavior = GROUP_ALERT_ALL;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001319
Julia Reynolds13d898c2017-02-02 12:22:05 -05001320 /**
1321 * If this notification is being shown as a badge, always show as a number.
1322 */
1323 public static final int BADGE_ICON_NONE = 0;
1324
1325 /**
1326 * If this notification is being shown as a badge, use the {@link #getSmallIcon()} to
1327 * represent this notification.
1328 */
1329 public static final int BADGE_ICON_SMALL = 1;
1330
1331 /**
1332 * If this notification is being shown as a badge, use the {@link #getLargeIcon()} to
1333 * represent this notification.
1334 */
1335 public static final int BADGE_ICON_LARGE = 2;
Julia Reynolds7d5b4da2017-03-20 10:18:49 -04001336 private int mBadgeIcon = BADGE_ICON_NONE;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001337
Chris Wren51c75102013-07-16 20:49:17 -04001338 /**
Gustav Sennton761884c2018-11-19 17:40:19 +00001339 * Determines whether the platform can generate contextual actions for a notification.
1340 */
1341 private boolean mAllowSystemGeneratedContextualActions = true;
1342
1343 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001344 * Structure to encapsulate a named action that can be shown as part of this notification.
1345 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1346 * selected by the user.
1347 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001348 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1349 * or {@link Notification.Builder#addAction(Notification.Action)}
1350 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001351 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001352 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001353 /**
1354 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1355 * {@link RemoteInput}s.
1356 *
1357 * This is intended for {@link RemoteInput}s that only accept data, meaning
1358 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
1359 * is null or empty, and {@link RemoteInput#getAllowedDataTypes is non-null and not
1360 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1361 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1362 *
1363 * You can test if a RemoteInput matches these constraints using
1364 * {@link RemoteInput#isDataOnly}.
1365 */
1366 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1367
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001368 /**
1369 * {@link }: No semantic action defined.
1370 */
1371 public static final int SEMANTIC_ACTION_NONE = 0;
1372
1373 /**
1374 * {@code SemanticAction}: Reply to a conversation, chat, group, or wherever replies
1375 * may be appropriate.
1376 */
1377 public static final int SEMANTIC_ACTION_REPLY = 1;
1378
1379 /**
1380 * {@code SemanticAction}: Mark content as read.
1381 */
1382 public static final int SEMANTIC_ACTION_MARK_AS_READ = 2;
1383
1384 /**
1385 * {@code SemanticAction}: Mark content as unread.
1386 */
1387 public static final int SEMANTIC_ACTION_MARK_AS_UNREAD = 3;
1388
1389 /**
1390 * {@code SemanticAction}: Delete the content associated with the notification. This
1391 * could mean deleting an email, message, etc.
1392 */
1393 public static final int SEMANTIC_ACTION_DELETE = 4;
1394
1395 /**
1396 * {@code SemanticAction}: Archive the content associated with the notification. This
1397 * could mean archiving an email, message, etc.
1398 */
1399 public static final int SEMANTIC_ACTION_ARCHIVE = 5;
1400
1401 /**
1402 * {@code SemanticAction}: Mute the content associated with the notification. This could
1403 * mean silencing a conversation or currently playing media.
1404 */
1405 public static final int SEMANTIC_ACTION_MUTE = 6;
1406
1407 /**
1408 * {@code SemanticAction}: Unmute the content associated with the notification. This could
1409 * mean un-silencing a conversation or currently playing media.
1410 */
1411 public static final int SEMANTIC_ACTION_UNMUTE = 7;
1412
1413 /**
1414 * {@code SemanticAction}: Mark content with a thumbs up.
1415 */
1416 public static final int SEMANTIC_ACTION_THUMBS_UP = 8;
1417
1418 /**
1419 * {@code SemanticAction}: Mark content with a thumbs down.
1420 */
1421 public static final int SEMANTIC_ACTION_THUMBS_DOWN = 9;
1422
Kodlee Yincda5b092018-02-15 15:34:53 -08001423 /**
1424 * {@code SemanticAction}: Call a contact, group, etc.
1425 */
1426 public static final int SEMANTIC_ACTION_CALL = 10;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001427
Griff Hazen959591e2014-05-15 22:26:18 -07001428 private final Bundle mExtras;
Mathew Inwood8c854f82018-09-14 12:35:36 +01001429 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Dan Sandler86647982015-05-13 23:41:13 -04001430 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001431 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001432 private boolean mAllowGeneratedReplies = true;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001433 private final @SemanticAction int mSemanticAction;
Gustav Sennton005d7a02019-01-04 13:41:32 +00001434 private final boolean mIsContextual;
Griff Hazen959591e2014-05-15 22:26:18 -07001435
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001436 /**
1437 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001438 *
1439 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001440 */
Dan Sandler86647982015-05-13 23:41:13 -04001441 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001442 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001443
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001444 /**
1445 * Title of the action.
1446 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001447 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001448
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001449 /**
1450 * Intent to send when the user invokes this action. May be null, in which case the action
1451 * may be rendered in a disabled presentation by the system UI.
1452 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001453 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001454
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001455 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001456 if (in.readInt() != 0) {
1457 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001458 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1459 icon = mIcon.getResId();
1460 }
Dan Sandler86647982015-05-13 23:41:13 -04001461 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001462 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1463 if (in.readInt() == 1) {
1464 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1465 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001466 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001467 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001468 mAllowGeneratedReplies = in.readInt() == 1;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001469 mSemanticAction = in.readInt();
Gustav Sennton005d7a02019-01-04 13:41:32 +00001470 mIsContextual = in.readInt() == 1;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001471 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001472
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001473 /**
Dan Sandler86647982015-05-13 23:41:13 -04001474 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001475 */
Dan Sandler86647982015-05-13 23:41:13 -04001476 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001477 public Action(int icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001478 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true,
Gustav Sennton005d7a02019-01-04 13:41:32 +00001479 SEMANTIC_ACTION_NONE, false /* isContextual */);
Griff Hazen959591e2014-05-15 22:26:18 -07001480 }
1481
Adrian Roos7af53622016-10-12 13:44:05 -07001482 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001483 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001484 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
Gustav Sennton005d7a02019-01-04 13:41:32 +00001485 @SemanticAction int semanticAction, boolean isContextual) {
Dan Sandler86647982015-05-13 23:41:13 -04001486 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001487 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1488 this.icon = icon.getResId();
1489 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001490 this.title = title;
1491 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001492 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001493 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001494 this.mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001495 this.mSemanticAction = semanticAction;
Gustav Sennton005d7a02019-01-04 13:41:32 +00001496 this.mIsContextual = isContextual;
Griff Hazen959591e2014-05-15 22:26:18 -07001497 }
1498
1499 /**
Dan Sandler86647982015-05-13 23:41:13 -04001500 * Return an icon representing the action.
1501 */
1502 public Icon getIcon() {
1503 if (mIcon == null && icon != 0) {
1504 // you snuck an icon in here without using the builder; let's try to keep it
1505 mIcon = Icon.createWithResource("", icon);
1506 }
1507 return mIcon;
1508 }
1509
1510 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001511 * Get additional metadata carried around with this Action.
1512 */
1513 public Bundle getExtras() {
1514 return mExtras;
1515 }
1516
1517 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001518 * Return whether the platform should automatically generate possible replies for this
1519 * {@link Action}
1520 */
1521 public boolean getAllowGeneratedReplies() {
1522 return mAllowGeneratedReplies;
1523 }
1524
1525 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001526 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001527 * May return null if no remote inputs were added. Only returns inputs which accept
1528 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001529 */
1530 public RemoteInput[] getRemoteInputs() {
1531 return mRemoteInputs;
1532 }
1533
1534 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001535 * Returns the {@code SemanticAction} associated with this {@link Action}. A
1536 * {@code SemanticAction} denotes what an {@link Action}'s {@link PendingIntent} will do
1537 * (eg. reply, mark as read, delete, etc).
1538 */
1539 public @SemanticAction int getSemanticAction() {
1540 return mSemanticAction;
1541 }
1542
1543 /**
Gustav Sennton005d7a02019-01-04 13:41:32 +00001544 * Returns whether this is a contextual Action, i.e. whether the action is dependent on the
1545 * notification message body. An example of a contextual action could be an action opening a
1546 * map application with an address shown in the notification.
1547 */
1548 public boolean isContextual() {
1549 return mIsContextual;
1550 }
1551
1552 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001553 * Get the list of inputs to be collected from the user that ONLY accept data when this
1554 * action is sent. These remote inputs are guaranteed to return true on a call to
1555 * {@link RemoteInput#isDataOnly}.
1556 *
Shane Brennanf670d6c2017-04-25 13:05:45 -07001557 * Returns null if there are no data-only remote inputs.
Shane Brennan472a3b32016-12-12 15:28:10 -08001558 *
1559 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1560 * of non-textual RemoteInputs do not access these remote inputs.
1561 */
1562 public RemoteInput[] getDataOnlyRemoteInputs() {
1563 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1564 }
1565
1566 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001567 * Builder class for {@link Action} objects.
1568 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001569 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001570 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001571 private final CharSequence mTitle;
1572 private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001573 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001574 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001575 private ArrayList<RemoteInput> mRemoteInputs;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001576 private @SemanticAction int mSemanticAction;
Gustav Sennton005d7a02019-01-04 13:41:32 +00001577 private boolean mIsContextual;
Griff Hazen959591e2014-05-15 22:26:18 -07001578
1579 /**
1580 * Construct a new builder for {@link Action} object.
1581 * @param icon icon to show for this action
1582 * @param title the title of the action
1583 * @param intent the {@link PendingIntent} to fire when users trigger this action
1584 */
Dan Sandler86647982015-05-13 23:41:13 -04001585 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001586 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001587 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001588 }
1589
1590 /**
1591 * Construct a new builder for {@link Action} object.
1592 * @param icon icon to show for this action
1593 * @param title the title of the action
1594 * @param intent the {@link PendingIntent} to fire when users trigger this action
1595 */
1596 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001597 this(icon, title, intent, new Bundle(), null, true, SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001598 }
1599
1600 /**
1601 * Construct a new builder for {@link Action} object using the fields from an
1602 * {@link Action}.
1603 * @param action the action to read fields from.
1604 */
1605 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001606 this(action.getIcon(), action.title, action.actionIntent,
1607 new Bundle(action.mExtras), action.getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001608 action.getAllowGeneratedReplies(), action.getSemanticAction());
Griff Hazen959591e2014-05-15 22:26:18 -07001609 }
1610
Dan Sandler86647982015-05-13 23:41:13 -04001611 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001612 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1613 @SemanticAction int semanticAction) {
Griff Hazen959591e2014-05-15 22:26:18 -07001614 mIcon = icon;
1615 mTitle = title;
1616 mIntent = intent;
1617 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001618 if (remoteInputs != null) {
1619 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1620 Collections.addAll(mRemoteInputs, remoteInputs);
1621 }
Adrian Roos7af53622016-10-12 13:44:05 -07001622 mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001623 mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001624 }
1625
1626 /**
1627 * Merge additional metadata into this builder.
1628 *
1629 * <p>Values within the Bundle will replace existing extras values in this Builder.
1630 *
1631 * @see Notification.Action#extras
1632 */
1633 public Builder addExtras(Bundle extras) {
1634 if (extras != null) {
1635 mExtras.putAll(extras);
1636 }
1637 return this;
1638 }
1639
1640 /**
1641 * Get the metadata Bundle used by this Builder.
1642 *
1643 * <p>The returned Bundle is shared with this Builder.
1644 */
1645 public Bundle getExtras() {
1646 return mExtras;
1647 }
1648
1649 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001650 * Add an input to be collected from the user when this action is sent.
1651 * Response values can be retrieved from the fired intent by using the
1652 * {@link RemoteInput#getResultsFromIntent} function.
1653 * @param remoteInput a {@link RemoteInput} to add to the action
1654 * @return this object for method chaining
1655 */
1656 public Builder addRemoteInput(RemoteInput remoteInput) {
1657 if (mRemoteInputs == null) {
1658 mRemoteInputs = new ArrayList<RemoteInput>();
1659 }
1660 mRemoteInputs.add(remoteInput);
1661 return this;
1662 }
1663
1664 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001665 * Set whether the platform should automatically generate possible replies to add to
1666 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1667 * {@link RemoteInput}, this has no effect.
1668 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1669 * otherwise
1670 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001671 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001672 */
1673 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1674 mAllowGeneratedReplies = allowGeneratedReplies;
1675 return this;
1676 }
1677
1678 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001679 * Sets the {@code SemanticAction} for this {@link Action}. A
1680 * {@code SemanticAction} denotes what an {@link Action}'s
1681 * {@link PendingIntent} will do (eg. reply, mark as read, delete, etc).
1682 * @param semanticAction a SemanticAction defined within {@link Action} with
1683 * {@code SEMANTIC_ACTION_} prefixes
1684 * @return this object for method chaining
1685 */
1686 public Builder setSemanticAction(@SemanticAction int semanticAction) {
1687 mSemanticAction = semanticAction;
1688 return this;
1689 }
1690
1691 /**
Gustav Sennton005d7a02019-01-04 13:41:32 +00001692 * Sets whether this {@link Action} is a contextual action, i.e. whether the action is
1693 * dependent on the notification message body. An example of a contextual action could
1694 * be an action opening a map application with an address shown in the notification.
1695 */
1696 public Builder setContextual(boolean isContextual) {
1697 mIsContextual = isContextual;
1698 return this;
1699 }
1700
1701 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001702 * Apply an extender to this action builder. Extenders may be used to add
1703 * metadata or change options on this builder.
1704 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001705 public Builder extend(Extender extender) {
1706 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001707 return this;
1708 }
1709
1710 /**
Gustav Senntonc98b1632018-11-23 12:26:15 +00001711 * Throws an NPE if we are building a contextual action missing one of the fields
1712 * necessary to display the action.
1713 */
1714 private void checkContextualActionNullFields() {
Gustav Sennton005d7a02019-01-04 13:41:32 +00001715 if (!mIsContextual) return;
Gustav Senntonc98b1632018-11-23 12:26:15 +00001716
1717 if (mIcon == null) {
1718 throw new NullPointerException("Contextual Actions must contain a valid icon");
1719 }
1720
1721 if (mIntent == null) {
1722 throw new NullPointerException(
1723 "Contextual Actions must contain a valid PendingIntent");
1724 }
1725 }
1726
1727 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001728 * Combine all of the options that have been set and return a new {@link Action}
1729 * object.
1730 * @return the built action
1731 */
1732 public Action build() {
Gustav Senntonc98b1632018-11-23 12:26:15 +00001733 checkContextualActionNullFields();
1734
Shane Brennan472a3b32016-12-12 15:28:10 -08001735 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1736 RemoteInput[] previousDataInputs =
1737 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001738 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001739 for (RemoteInput input : previousDataInputs) {
1740 dataOnlyInputs.add(input);
1741 }
1742 }
1743 List<RemoteInput> textInputs = new ArrayList<>();
1744 if (mRemoteInputs != null) {
1745 for (RemoteInput input : mRemoteInputs) {
1746 if (input.isDataOnly()) {
1747 dataOnlyInputs.add(input);
1748 } else {
1749 textInputs.add(input);
1750 }
1751 }
1752 }
1753 if (!dataOnlyInputs.isEmpty()) {
1754 RemoteInput[] dataInputsArr =
1755 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1756 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1757 }
1758 RemoteInput[] textInputsArr = textInputs.isEmpty()
1759 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1760 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Gustav Sennton005d7a02019-01-04 13:41:32 +00001761 mAllowGeneratedReplies, mSemanticAction, mIsContextual);
Griff Hazen959591e2014-05-15 22:26:18 -07001762 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001763 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001764
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001765 @Override
1766 public Action clone() {
1767 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001768 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001769 title,
1770 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001771 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001772 getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001773 getAllowGeneratedReplies(),
Gustav Sennton005d7a02019-01-04 13:41:32 +00001774 getSemanticAction(),
1775 isContextual());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001776 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001777
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001778 @Override
1779 public int describeContents() {
1780 return 0;
1781 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001782
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001783 @Override
1784 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001785 final Icon ic = getIcon();
1786 if (ic != null) {
1787 out.writeInt(1);
1788 ic.writeToParcel(out, 0);
1789 } else {
1790 out.writeInt(0);
1791 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001792 TextUtils.writeToParcel(title, out, flags);
1793 if (actionIntent != null) {
1794 out.writeInt(1);
1795 actionIntent.writeToParcel(out, flags);
1796 } else {
1797 out.writeInt(0);
1798 }
Griff Hazen959591e2014-05-15 22:26:18 -07001799 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001800 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001801 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001802 out.writeInt(mSemanticAction);
Gustav Sennton005d7a02019-01-04 13:41:32 +00001803 out.writeInt(mIsContextual ? 1 : 0);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001804 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001805
Griff Hazen959591e2014-05-15 22:26:18 -07001806 public static final Parcelable.Creator<Action> CREATOR =
1807 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001808 public Action createFromParcel(Parcel in) {
1809 return new Action(in);
1810 }
1811 public Action[] newArray(int size) {
1812 return new Action[size];
1813 }
1814 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001815
1816 /**
1817 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1818 * metadata or change options on an action builder.
1819 */
1820 public interface Extender {
1821 /**
1822 * Apply this extender to a notification action builder.
1823 * @param builder the builder to be modified.
1824 * @return the build object for chaining.
1825 */
1826 public Builder extend(Builder builder);
1827 }
1828
1829 /**
1830 * Wearable extender for notification actions. To add extensions to an action,
1831 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1832 * the {@code WearableExtender()} constructor and apply it to a
1833 * {@link android.app.Notification.Action.Builder} using
1834 * {@link android.app.Notification.Action.Builder#extend}.
1835 *
1836 * <pre class="prettyprint">
1837 * Notification.Action action = new Notification.Action.Builder(
1838 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001839 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001840 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001841 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001842 */
1843 public static final class WearableExtender implements Extender {
1844 /** Notification action extra which contains wearable extensions */
1845 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1846
Pete Gastaf6781d2014-10-07 15:17:05 -04001847 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001848 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001849 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1850 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1851 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001852
1853 // Flags bitwise-ored to mFlags
1854 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001855 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001856 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001857
1858 // Default value for flags integer
1859 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1860
1861 private int mFlags = DEFAULT_FLAGS;
1862
Pete Gastaf6781d2014-10-07 15:17:05 -04001863 private CharSequence mInProgressLabel;
1864 private CharSequence mConfirmLabel;
1865 private CharSequence mCancelLabel;
1866
Griff Hazen61a9e862014-05-22 16:05:19 -07001867 /**
1868 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1869 * options.
1870 */
1871 public WearableExtender() {
1872 }
1873
1874 /**
1875 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1876 * wearable options present in an existing notification action.
1877 * @param action the notification action to inspect.
1878 */
1879 public WearableExtender(Action action) {
1880 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1881 if (wearableBundle != null) {
1882 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001883 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1884 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1885 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001886 }
1887 }
1888
1889 /**
1890 * Apply wearable extensions to a notification action that is being built. This is
1891 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1892 * method of {@link android.app.Notification.Action.Builder}.
1893 */
1894 @Override
1895 public Action.Builder extend(Action.Builder builder) {
1896 Bundle wearableBundle = new Bundle();
1897
1898 if (mFlags != DEFAULT_FLAGS) {
1899 wearableBundle.putInt(KEY_FLAGS, mFlags);
1900 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001901 if (mInProgressLabel != null) {
1902 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1903 }
1904 if (mConfirmLabel != null) {
1905 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1906 }
1907 if (mCancelLabel != null) {
1908 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1909 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001910
1911 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1912 return builder;
1913 }
1914
1915 @Override
1916 public WearableExtender clone() {
1917 WearableExtender that = new WearableExtender();
1918 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001919 that.mInProgressLabel = this.mInProgressLabel;
1920 that.mConfirmLabel = this.mConfirmLabel;
1921 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001922 return that;
1923 }
1924
1925 /**
1926 * Set whether this action is available when the wearable device is not connected to
1927 * a companion device. The user can still trigger this action when the wearable device is
1928 * offline, but a visual hint will indicate that the action may not be available.
1929 * Defaults to true.
1930 */
1931 public WearableExtender setAvailableOffline(boolean availableOffline) {
1932 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1933 return this;
1934 }
1935
1936 /**
1937 * Get whether this action is available when the wearable device is not connected to
1938 * a companion device. The user can still trigger this action when the wearable device is
1939 * offline, but a visual hint will indicate that the action may not be available.
1940 * Defaults to true.
1941 */
1942 public boolean isAvailableOffline() {
1943 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1944 }
1945
1946 private void setFlag(int mask, boolean value) {
1947 if (value) {
1948 mFlags |= mask;
1949 } else {
1950 mFlags &= ~mask;
1951 }
1952 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001953
1954 /**
1955 * Set a label to display while the wearable is preparing to automatically execute the
1956 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1957 *
1958 * @param label the label to display while the action is being prepared to execute
1959 * @return this object for method chaining
1960 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001961 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001962 public WearableExtender setInProgressLabel(CharSequence label) {
1963 mInProgressLabel = label;
1964 return this;
1965 }
1966
1967 /**
1968 * Get the label to display while the wearable is preparing to automatically execute
1969 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1970 *
1971 * @return the label to display while the action is being prepared to execute
1972 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001973 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001974 public CharSequence getInProgressLabel() {
1975 return mInProgressLabel;
1976 }
1977
1978 /**
1979 * Set a label to display to confirm that the action should be executed.
1980 * This is usually an imperative verb like "Send".
1981 *
1982 * @param label the label to confirm the action should be executed
1983 * @return this object for method chaining
1984 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001985 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001986 public WearableExtender setConfirmLabel(CharSequence label) {
1987 mConfirmLabel = label;
1988 return this;
1989 }
1990
1991 /**
1992 * Get the label to display to confirm that the action should be executed.
1993 * This is usually an imperative verb like "Send".
1994 *
1995 * @return the label to confirm the action should be executed
1996 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001997 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001998 public CharSequence getConfirmLabel() {
1999 return mConfirmLabel;
2000 }
2001
2002 /**
2003 * Set a label to display to cancel the action.
2004 * This is usually an imperative verb, like "Cancel".
2005 *
2006 * @param label the label to display to cancel the action
2007 * @return this object for method chaining
2008 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002009 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002010 public WearableExtender setCancelLabel(CharSequence label) {
2011 mCancelLabel = label;
2012 return this;
2013 }
2014
2015 /**
2016 * Get the label to display to cancel the action.
2017 * This is usually an imperative verb like "Cancel".
2018 *
2019 * @return the label to display to cancel the action
2020 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002021 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002022 public CharSequence getCancelLabel() {
2023 return mCancelLabel;
2024 }
Alex Hills9ab3a232016-04-05 14:54:56 -04002025
2026 /**
2027 * Set a hint that this Action will launch an {@link Activity} directly, telling the
2028 * platform that it can generate the appropriate transitions.
2029 * @param hintLaunchesActivity {@code true} if the content intent will launch
2030 * an activity and transitions should be generated, false otherwise.
2031 * @return this object for method chaining
2032 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04002033 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04002034 boolean hintLaunchesActivity) {
2035 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
2036 return this;
2037 }
2038
2039 /**
2040 * Get a hint that this Action will launch an {@link Activity} directly, telling the
2041 * platform that it can generate the appropriate transitions
2042 * @return {@code true} if the content intent will launch an activity and transitions
2043 * should be generated, false otherwise. The default value is {@code false} if this was
2044 * never set.
2045 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04002046 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04002047 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
2048 }
Alex Hills9f087612016-06-07 09:08:59 -04002049
2050 /**
2051 * Set a hint that this Action should be displayed inline.
2052 *
2053 * @param hintDisplayInline {@code true} if action should be displayed inline, false
2054 * otherwise
2055 * @return this object for method chaining
2056 */
2057 public WearableExtender setHintDisplayActionInline(
2058 boolean hintDisplayInline) {
2059 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
2060 return this;
2061 }
2062
2063 /**
2064 * Get a hint that this Action should be displayed inline.
2065 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08002066 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04002067 * otherwise. The default value is {@code false} if this was never set.
2068 */
2069 public boolean getHintDisplayActionInline() {
2070 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
2071 }
Griff Hazen61a9e862014-05-22 16:05:19 -07002072 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002073
2074 /**
2075 * Provides meaning to an {@link Action} that hints at what the associated
2076 * {@link PendingIntent} will do. For example, an {@link Action} with a
2077 * {@link PendingIntent} that replies to a text message notification may have the
2078 * {@link #SEMANTIC_ACTION_REPLY} {@code SemanticAction} set within it.
2079 *
2080 * @hide
2081 */
2082 @IntDef(prefix = { "SEMANTIC_ACTION_" }, value = {
2083 SEMANTIC_ACTION_NONE,
2084 SEMANTIC_ACTION_REPLY,
2085 SEMANTIC_ACTION_MARK_AS_READ,
2086 SEMANTIC_ACTION_MARK_AS_UNREAD,
2087 SEMANTIC_ACTION_DELETE,
2088 SEMANTIC_ACTION_ARCHIVE,
2089 SEMANTIC_ACTION_MUTE,
2090 SEMANTIC_ACTION_UNMUTE,
2091 SEMANTIC_ACTION_THUMBS_UP,
Kodlee Yincda5b092018-02-15 15:34:53 -08002092 SEMANTIC_ACTION_THUMBS_DOWN,
Gustav Sennton005d7a02019-01-04 13:41:32 +00002093 SEMANTIC_ACTION_CALL
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002094 })
2095 @Retention(RetentionPolicy.SOURCE)
2096 public @interface SemanticAction {}
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002097 }
2098
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002099 /**
2100 * Array of all {@link Action} structures attached to this notification by
2101 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
2102 * {@link android.service.notification.NotificationListenerService} that provide an alternative
2103 * interface for invoking actions.
2104 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05002105 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002106
2107 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002108 * Replacement version of this notification whose content will be shown
2109 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
2110 * and {@link #VISIBILITY_PUBLIC}.
2111 */
2112 public Notification publicVersion;
2113
2114 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002115 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08002116 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002117 */
2118 public Notification()
2119 {
2120 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002121 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002122 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002123 }
2124
2125 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002126 * @hide
2127 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002128 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002129 public Notification(Context context, int icon, CharSequence tickerText, long when,
2130 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
2131 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04002132 new Builder(context)
2133 .setWhen(when)
2134 .setSmallIcon(icon)
2135 .setTicker(tickerText)
2136 .setContentTitle(contentTitle)
2137 .setContentText(contentText)
2138 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
2139 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002140 }
2141
2142 /**
2143 * Constructs a Notification object with the information needed to
2144 * have a status bar icon without the standard expanded view.
2145 *
2146 * @param icon The resource id of the icon to put in the status bar.
2147 * @param tickerText The text that flows by in the status bar when the notification first
2148 * activates.
2149 * @param when The time to show in the time field. In the System.currentTimeMillis
2150 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08002151 *
2152 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002153 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002154 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002155 public Notification(int icon, CharSequence tickerText, long when)
2156 {
2157 this.icon = icon;
2158 this.tickerText = tickerText;
2159 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002160 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002161 }
2162
2163 /**
2164 * Unflatten the notification from a parcel.
2165 */
Svet Ganovddb94882016-06-23 19:55:24 -07002166 @SuppressWarnings("unchecked")
2167 public Notification(Parcel parcel) {
2168 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
2169 // intents in extras are always written as the last entry.
2170 readFromParcelImpl(parcel);
2171 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002172 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07002173 }
2174
2175 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002176 {
2177 int version = parcel.readInt();
2178
Adrian Roosfb921842017-10-26 14:49:56 +02002179 mWhitelistToken = parcel.readStrongBinder();
2180 if (mWhitelistToken == null) {
2181 mWhitelistToken = processWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -07002182 }
2183 // Propagate this token to all pending intents that are unmarshalled from the parcel.
Adrian Roosfb921842017-10-26 14:49:56 +02002184 parcel.setClassCookie(PendingIntent.class, mWhitelistToken);
Dianne Hackborn98305522017-05-05 17:53:53 -07002185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002186 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002187 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04002188 if (parcel.readInt() != 0) {
2189 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04002190 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
2191 icon = mSmallIcon.getResId();
2192 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002193 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002194 number = parcel.readInt();
2195 if (parcel.readInt() != 0) {
2196 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2197 }
2198 if (parcel.readInt() != 0) {
2199 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2200 }
2201 if (parcel.readInt() != 0) {
2202 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2203 }
2204 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002205 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002206 }
2207 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002208 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
2209 }
Joe Onorato561d3852010-11-20 18:09:34 -08002210 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002211 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08002212 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002213 defaults = parcel.readInt();
2214 flags = parcel.readInt();
2215 if (parcel.readInt() != 0) {
2216 sound = Uri.CREATOR.createFromParcel(parcel);
2217 }
2218
2219 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04002220 if (parcel.readInt() != 0) {
2221 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
2222 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002223 vibrate = parcel.createLongArray();
2224 ledARGB = parcel.readInt();
2225 ledOnMS = parcel.readInt();
2226 ledOffMS = parcel.readInt();
2227 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002228
2229 if (parcel.readInt() != 0) {
2230 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2231 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002232
2233 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002234
John Spurlockfd7f1e02014-03-18 16:41:57 -04002235 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002236
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002237 mGroupKey = parcel.readString();
2238
2239 mSortKey = parcel.readString();
2240
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06002241 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Robin Leead7e72a2017-12-04 15:45:46 +01002242 fixDuplicateExtras();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002243
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002244 actions = parcel.createTypedArray(Action.CREATOR); // may be null
2245
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002246 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002247 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2248 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002249
Chris Wren8fd39ec2014-02-27 17:43:26 -05002250 if (parcel.readInt() != 0) {
2251 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2252 }
2253
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002254 visibility = parcel.readInt();
2255
2256 if (parcel.readInt() != 0) {
2257 publicVersion = Notification.CREATOR.createFromParcel(parcel);
2258 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002259
2260 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002261
2262 if (parcel.readInt() != 0) {
2263 mChannelId = parcel.readString();
2264 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002265 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05002266
2267 if (parcel.readInt() != 0) {
2268 mShortcutId = parcel.readString();
2269 }
2270
2271 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04002272
2273 if (parcel.readInt() != 0) {
2274 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2275 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002276
2277 mGroupAlertBehavior = parcel.readInt();
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002278 if (parcel.readInt() != 0) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002279 mBubbleMetadata = BubbleMetadata.CREATOR.createFromParcel(parcel);
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002280 }
Gustav Sennton761884c2018-11-19 17:40:19 +00002281
2282 mAllowSystemGeneratedContextualActions = parcel.readBoolean();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002283 }
2284
Andy Stadler110988c2010-12-03 14:29:16 -08002285 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07002286 public Notification clone() {
2287 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04002288 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002289 return that;
2290 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002291
Daniel Sandler1a497d32013-04-18 14:52:45 -04002292 /**
2293 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
2294 * of this into that.
2295 * @hide
2296 */
2297 public void cloneInto(Notification that, boolean heavy) {
Adrian Roosfb921842017-10-26 14:49:56 +02002298 that.mWhitelistToken = this.mWhitelistToken;
Joe Onorato18e69df2010-05-17 22:26:12 -07002299 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002300 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002301 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07002302 that.number = this.number;
2303
2304 // PendingIntents are global, so there's no reason (or way) to clone them.
2305 that.contentIntent = this.contentIntent;
2306 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002307 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07002308
2309 if (this.tickerText != null) {
2310 that.tickerText = this.tickerText.toString();
2311 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002312 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002313 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04002314 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002315 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07002316 that.contentView = this.contentView.clone();
2317 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002318 if (heavy && this.mLargeIcon != null) {
2319 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08002320 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01002321 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07002322 that.sound = this.sound; // android.net.Uri is immutable
2323 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002324 if (this.audioAttributes != null) {
2325 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
2326 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002327
2328 final long[] vibrate = this.vibrate;
2329 if (vibrate != null) {
2330 final int N = vibrate.length;
2331 final long[] vib = that.vibrate = new long[N];
2332 System.arraycopy(vibrate, 0, vib, 0, N);
2333 }
2334
2335 that.ledARGB = this.ledARGB;
2336 that.ledOnMS = this.ledOnMS;
2337 that.ledOffMS = this.ledOffMS;
2338 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002339
Joe Onorato18e69df2010-05-17 22:26:12 -07002340 that.flags = this.flags;
2341
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002342 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08002343
John Spurlockfd7f1e02014-03-18 16:41:57 -04002344 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002345
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002346 that.mGroupKey = this.mGroupKey;
2347
2348 that.mSortKey = this.mSortKey;
2349
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002350 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002351 try {
2352 that.extras = new Bundle(this.extras);
2353 // will unparcel
2354 that.extras.size();
2355 } catch (BadParcelableException e) {
2356 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
2357 that.extras = null;
2358 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002359 }
2360
Felipe Lemedd85da62016-06-28 11:29:54 -07002361 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2362 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002363 }
2364
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002365 if (this.actions != null) {
2366 that.actions = new Action[this.actions.length];
2367 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002368 if ( this.actions[i] != null) {
2369 that.actions[i] = this.actions[i].clone();
2370 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002371 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002372 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002373
Daniel Sandler1a497d32013-04-18 14:52:45 -04002374 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002375 that.bigContentView = this.bigContentView.clone();
2376 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002377
Chris Wren8fd39ec2014-02-27 17:43:26 -05002378 if (heavy && this.headsUpContentView != null) {
2379 that.headsUpContentView = this.headsUpContentView.clone();
2380 }
2381
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002382 that.visibility = this.visibility;
2383
2384 if (this.publicVersion != null) {
2385 that.publicVersion = new Notification();
2386 this.publicVersion.cloneInto(that.publicVersion, heavy);
2387 }
2388
Dan Sandler26e81cf2014-05-06 10:01:27 -04002389 that.color = this.color;
2390
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002391 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002392 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002393 that.mShortcutId = this.mShortcutId;
2394 that.mBadgeIcon = this.mBadgeIcon;
2395 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002396 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002397 that.mBubbleMetadata = this.mBubbleMetadata;
Gustav Sennton761884c2018-11-19 17:40:19 +00002398 that.mAllowSystemGeneratedContextualActions = this.mAllowSystemGeneratedContextualActions;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002399
Daniel Sandler1a497d32013-04-18 14:52:45 -04002400 if (!heavy) {
2401 that.lightenPayload(); // will clean out extras
2402 }
2403 }
2404
2405 /**
Jeff Sharkey23b31182018-04-18 21:32:12 -06002406 * Note all {@link Uri} that are referenced internally, with the expectation
2407 * that Uri permission grants will need to be issued to ensure the recipient
2408 * of this object is able to render its contents.
2409 *
2410 * @hide
2411 */
2412 public void visitUris(@NonNull Consumer<Uri> visitor) {
2413 visitor.accept(sound);
2414
2415 if (tickerView != null) tickerView.visitUris(visitor);
2416 if (contentView != null) contentView.visitUris(visitor);
2417 if (bigContentView != null) bigContentView.visitUris(visitor);
2418 if (headsUpContentView != null) headsUpContentView.visitUris(visitor);
2419
2420 if (extras != null) {
2421 visitor.accept(extras.getParcelable(EXTRA_AUDIO_CONTENTS_URI));
Robin Leed107af62018-04-27 13:55:56 +02002422 if (extras.containsKey(EXTRA_BACKGROUND_IMAGE_URI)) {
2423 visitor.accept(Uri.parse(extras.getString(EXTRA_BACKGROUND_IMAGE_URI)));
2424 }
Jeff Sharkey23b31182018-04-18 21:32:12 -06002425 }
2426
2427 if (MessagingStyle.class.equals(getNotificationStyle()) && extras != null) {
2428 final Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
2429 if (!ArrayUtils.isEmpty(messages)) {
2430 for (MessagingStyle.Message message : MessagingStyle.Message
2431 .getMessagesFromBundleArray(messages)) {
2432 visitor.accept(message.getDataUri());
2433 }
2434 }
2435
2436 final Parcelable[] historic = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
2437 if (!ArrayUtils.isEmpty(historic)) {
2438 for (MessagingStyle.Message message : MessagingStyle.Message
2439 .getMessagesFromBundleArray(historic)) {
2440 visitor.accept(message.getDataUri());
2441 }
2442 }
2443 }
2444 }
2445
2446 /**
Daniel Sandler1a497d32013-04-18 14:52:45 -04002447 * Removes heavyweight parts of the Notification object for archival or for sending to
2448 * listeners when the full contents are not necessary.
2449 * @hide
2450 */
2451 public final void lightenPayload() {
2452 tickerView = null;
2453 contentView = null;
2454 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002455 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002456 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002457 if (extras != null && !extras.isEmpty()) {
2458 final Set<String> keyset = extras.keySet();
2459 final int N = keyset.size();
2460 final String[] keys = keyset.toArray(new String[N]);
2461 for (int i=0; i<N; i++) {
2462 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002463 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2464 continue;
2465 }
Dan Sandler50128532015-12-08 15:42:41 -05002466 final Object obj = extras.get(key);
2467 if (obj != null &&
2468 ( obj instanceof Parcelable
2469 || obj instanceof Parcelable[]
2470 || obj instanceof SparseArray
2471 || obj instanceof ArrayList)) {
2472 extras.remove(key);
2473 }
2474 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002475 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002476 }
2477
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002478 /**
2479 * Make sure this CharSequence is safe to put into a bundle, which basically
2480 * means it had better not be some custom Parcelable implementation.
2481 * @hide
2482 */
2483 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002484 if (cs == null) return cs;
2485 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2486 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2487 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002488 if (cs instanceof Parcelable) {
2489 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2490 + " instance is a custom Parcelable and not allowed in Notification");
2491 return cs.toString();
2492 }
Selim Cinek60a54252016-02-26 17:03:25 -08002493 return removeTextSizeSpans(cs);
2494 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002495
Selim Cinek60a54252016-02-26 17:03:25 -08002496 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2497 if (charSequence instanceof Spanned) {
2498 Spanned ss = (Spanned) charSequence;
2499 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2500 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2501 for (Object span : spans) {
2502 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002503 if (resultSpan instanceof CharacterStyle) {
2504 resultSpan = ((CharacterStyle) span).getUnderlying();
2505 }
2506 if (resultSpan instanceof TextAppearanceSpan) {
2507 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002508 resultSpan = new TextAppearanceSpan(
2509 originalSpan.getFamily(),
2510 originalSpan.getTextStyle(),
2511 -1,
2512 originalSpan.getTextColor(),
2513 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002514 } else if (resultSpan instanceof RelativeSizeSpan
2515 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002516 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002517 } else {
2518 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002519 }
2520 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2521 ss.getSpanFlags(span));
2522 }
2523 return builder;
2524 }
2525 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002526 }
2527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002528 public int describeContents() {
2529 return 0;
2530 }
2531
2532 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002533 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002534 */
Svet Ganovddb94882016-06-23 19:55:24 -07002535 public void writeToParcel(Parcel parcel, int flags) {
2536 // We need to mark all pending intents getting into the notification
2537 // system as being put there to later allow the notification ranker
2538 // to launch them and by doing so add the app to the battery saver white
2539 // list for a short period of time. The problem is that the system
2540 // cannot look into the extras as there may be parcelables there that
2541 // the platform does not know how to handle. To go around that we have
2542 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002543 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002544 if (collectPendingIntents) {
2545 PendingIntent.setOnMarshaledListener(
2546 (PendingIntent intent, Parcel out, int outFlags) -> {
2547 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002548 if (allPendingIntents == null) {
2549 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002550 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002551 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002552 }
2553 });
2554 }
2555 try {
2556 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002557 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002558 writeToParcelImpl(parcel, flags);
2559 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002560 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002561 } finally {
2562 if (collectPendingIntents) {
2563 PendingIntent.setOnMarshaledListener(null);
2564 }
2565 }
2566 }
2567
2568 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002569 parcel.writeInt(1);
2570
Adrian Roosfb921842017-10-26 14:49:56 +02002571 parcel.writeStrongBinder(mWhitelistToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002572 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002573 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002574 if (mSmallIcon == null && icon != 0) {
2575 // you snuck an icon in here without using the builder; let's try to keep it
2576 mSmallIcon = Icon.createWithResource("", icon);
2577 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002578 if (mSmallIcon != null) {
2579 parcel.writeInt(1);
2580 mSmallIcon.writeToParcel(parcel, 0);
2581 } else {
2582 parcel.writeInt(0);
2583 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002584 parcel.writeInt(number);
2585 if (contentIntent != null) {
2586 parcel.writeInt(1);
2587 contentIntent.writeToParcel(parcel, 0);
2588 } else {
2589 parcel.writeInt(0);
2590 }
2591 if (deleteIntent != null) {
2592 parcel.writeInt(1);
2593 deleteIntent.writeToParcel(parcel, 0);
2594 } else {
2595 parcel.writeInt(0);
2596 }
2597 if (tickerText != null) {
2598 parcel.writeInt(1);
2599 TextUtils.writeToParcel(tickerText, parcel, flags);
2600 } else {
2601 parcel.writeInt(0);
2602 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002603 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002604 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002605 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002606 } else {
2607 parcel.writeInt(0);
2608 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002609 if (contentView != null) {
2610 parcel.writeInt(1);
2611 contentView.writeToParcel(parcel, 0);
2612 } else {
2613 parcel.writeInt(0);
2614 }
Selim Cinek279fa862016-06-14 10:57:25 -07002615 if (mLargeIcon == null && largeIcon != null) {
2616 // you snuck an icon in here without using the builder; let's try to keep it
2617 mLargeIcon = Icon.createWithBitmap(largeIcon);
2618 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002619 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002620 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002621 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002622 } else {
2623 parcel.writeInt(0);
2624 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002625
2626 parcel.writeInt(defaults);
2627 parcel.writeInt(this.flags);
2628
2629 if (sound != null) {
2630 parcel.writeInt(1);
2631 sound.writeToParcel(parcel, 0);
2632 } else {
2633 parcel.writeInt(0);
2634 }
2635 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002636
2637 if (audioAttributes != null) {
2638 parcel.writeInt(1);
2639 audioAttributes.writeToParcel(parcel, 0);
2640 } else {
2641 parcel.writeInt(0);
2642 }
2643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002644 parcel.writeLongArray(vibrate);
2645 parcel.writeInt(ledARGB);
2646 parcel.writeInt(ledOnMS);
2647 parcel.writeInt(ledOffMS);
2648 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002649
2650 if (fullScreenIntent != null) {
2651 parcel.writeInt(1);
2652 fullScreenIntent.writeToParcel(parcel, 0);
2653 } else {
2654 parcel.writeInt(0);
2655 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002656
2657 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002658
John Spurlockfd7f1e02014-03-18 16:41:57 -04002659 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002660
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002661 parcel.writeString(mGroupKey);
2662
2663 parcel.writeString(mSortKey);
2664
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002665 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002666
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002667 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002668
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002669 if (bigContentView != null) {
2670 parcel.writeInt(1);
2671 bigContentView.writeToParcel(parcel, 0);
2672 } else {
2673 parcel.writeInt(0);
2674 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002675
Chris Wren8fd39ec2014-02-27 17:43:26 -05002676 if (headsUpContentView != null) {
2677 parcel.writeInt(1);
2678 headsUpContentView.writeToParcel(parcel, 0);
2679 } else {
2680 parcel.writeInt(0);
2681 }
2682
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002683 parcel.writeInt(visibility);
2684
2685 if (publicVersion != null) {
2686 parcel.writeInt(1);
2687 publicVersion.writeToParcel(parcel, 0);
2688 } else {
2689 parcel.writeInt(0);
2690 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002691
2692 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002693
2694 if (mChannelId != null) {
2695 parcel.writeInt(1);
2696 parcel.writeString(mChannelId);
2697 } else {
2698 parcel.writeInt(0);
2699 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002700 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002701
2702 if (mShortcutId != null) {
2703 parcel.writeInt(1);
2704 parcel.writeString(mShortcutId);
2705 } else {
2706 parcel.writeInt(0);
2707 }
2708
2709 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002710
2711 if (mSettingsText != null) {
2712 parcel.writeInt(1);
2713 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2714 } else {
2715 parcel.writeInt(0);
2716 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002717
2718 parcel.writeInt(mGroupAlertBehavior);
Julia Reynoldsfc640012018-02-21 12:25:27 -05002719
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002720 if (mBubbleMetadata != null) {
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002721 parcel.writeInt(1);
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002722 mBubbleMetadata.writeToParcel(parcel, 0);
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002723 } else {
2724 parcel.writeInt(0);
2725 }
2726
Gustav Sennton761884c2018-11-19 17:40:19 +00002727 parcel.writeBoolean(mAllowSystemGeneratedContextualActions);
2728
Julia Reynoldsfc640012018-02-21 12:25:27 -05002729 // mUsesStandardHeader is not written because it should be recomputed in listeners
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002730 }
2731
2732 /**
2733 * Parcelable.Creator that instantiates Notification objects
2734 */
2735 public static final Parcelable.Creator<Notification> CREATOR
2736 = new Parcelable.Creator<Notification>()
2737 {
2738 public Notification createFromParcel(Parcel parcel)
2739 {
2740 return new Notification(parcel);
2741 }
2742
2743 public Notification[] newArray(int size)
2744 {
2745 return new Notification[size];
2746 }
2747 };
2748
2749 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05002750 * @hide
2751 */
2752 public static boolean areActionsVisiblyDifferent(Notification first, Notification second) {
2753 Notification.Action[] firstAs = first.actions;
2754 Notification.Action[] secondAs = second.actions;
2755 if (firstAs == null && secondAs != null || firstAs != null && secondAs == null) {
2756 return true;
2757 }
2758 if (firstAs != null && secondAs != null) {
2759 if (firstAs.length != secondAs.length) {
2760 return true;
2761 }
2762 for (int i = 0; i < firstAs.length; i++) {
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -04002763 if (!Objects.equals(String.valueOf(firstAs[i].title),
2764 String.valueOf(secondAs[i].title))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05002765 return true;
2766 }
2767 RemoteInput[] firstRs = firstAs[i].getRemoteInputs();
2768 RemoteInput[] secondRs = secondAs[i].getRemoteInputs();
2769 if (firstRs == null) {
2770 firstRs = new RemoteInput[0];
2771 }
2772 if (secondRs == null) {
2773 secondRs = new RemoteInput[0];
2774 }
2775 if (firstRs.length != secondRs.length) {
2776 return true;
2777 }
2778 for (int j = 0; j < firstRs.length; j++) {
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -04002779 if (!Objects.equals(String.valueOf(firstRs[j].getLabel()),
2780 String.valueOf(secondRs[j].getLabel()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05002781 return true;
2782 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05002783 }
2784 }
2785 }
2786 return false;
2787 }
2788
2789 /**
2790 * @hide
2791 */
2792 public static boolean areStyledNotificationsVisiblyDifferent(Builder first, Builder second) {
2793 if (first.getStyle() == null) {
2794 return second.getStyle() != null;
2795 }
2796 if (second.getStyle() == null) {
2797 return true;
2798 }
2799 return first.getStyle().areNotificationsVisiblyDifferent(second.getStyle());
2800 }
2801
2802 /**
2803 * @hide
2804 */
2805 public static boolean areRemoteViewsChanged(Builder first, Builder second) {
Julia Reynoldse5c60452018-04-30 14:41:36 -04002806 if (!Objects.equals(first.usesStandardHeader(), second.usesStandardHeader())) {
2807 return true;
2808 }
2809
2810 if (areRemoteViewsChanged(first.mN.contentView, second.mN.contentView)) {
2811 return true;
2812 }
2813 if (areRemoteViewsChanged(first.mN.bigContentView, second.mN.bigContentView)) {
2814 return true;
2815 }
2816 if (areRemoteViewsChanged(first.mN.headsUpContentView, second.mN.headsUpContentView)) {
2817 return true;
2818 }
2819
2820 return false;
2821 }
2822
2823 private static boolean areRemoteViewsChanged(RemoteViews first, RemoteViews second) {
2824 if (first == null && second == null) {
2825 return false;
2826 }
2827 if (first == null && second != null || first != null && second == null) {
2828 return true;
2829 }
2830
2831 if (!Objects.equals(first.getLayoutId(), second.getLayoutId())) {
2832 return true;
2833 }
2834
2835 if (!Objects.equals(first.getSequenceNumber(), second.getSequenceNumber())) {
2836 return true;
2837 }
2838
2839 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05002840 }
2841
2842 /**
Robin Leead7e72a2017-12-04 15:45:46 +01002843 * Parcelling creates multiple copies of objects in {@code extras}. Fix them.
2844 * <p>
2845 * For backwards compatibility {@code extras} holds some references to "real" member data such
2846 * as {@link getLargeIcon()} which is mirrored by {@link #EXTRA_LARGE_ICON}. This is mostly
2847 * fine as long as the object stays in one process.
2848 * <p>
2849 * However, once the notification goes into a parcel each reference gets marshalled separately,
2850 * wasting memory. Especially with large images on Auto and TV, this is worth fixing.
2851 */
2852 private void fixDuplicateExtras() {
2853 if (extras != null) {
Robin Leead7e72a2017-12-04 15:45:46 +01002854 fixDuplicateExtra(mLargeIcon, EXTRA_LARGE_ICON);
2855 }
2856 }
2857
2858 /**
2859 * If we find an extra that's exactly the same as one of the "real" fields but refers to a
2860 * separate object, replace it with the field's version to avoid holding duplicate copies.
2861 */
2862 private void fixDuplicateExtra(@Nullable Parcelable original, @NonNull String extraName) {
2863 if (original != null && extras.getParcelable(extraName) != null) {
2864 extras.putParcelable(extraName, original);
2865 }
2866 }
2867
2868 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002869 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2870 * layout.
2871 *
2872 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2873 * in the view.</p>
2874 * @param context The context for your application / activity.
2875 * @param contentTitle The title that goes in the expanded entry.
2876 * @param contentText The text that goes in the expanded entry.
2877 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2878 * If this is an activity, it must include the
2879 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002880 * that you take care of task management as described in the
2881 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2882 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002883 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002884 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002885 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002886 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002887 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002888 public void setLatestEventInfo(Context context,
2889 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002890 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2891 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2892 new Throwable());
2893 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002894
Selim Cinek4ac6f602016-06-13 15:47:03 -07002895 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2896 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2897 }
2898
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002899 // ensure that any information already set directly is preserved
2900 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002901
2902 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002904 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002905 }
2906 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002907 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002908 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002909 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002910
2911 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002912 }
2913
Julia Reynoldsda303542015-11-23 14:00:20 -05002914 /**
2915 * @hide
2916 */
2917 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002918 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002919 }
2920
2921 /**
2922 * @hide
2923 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002924 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002925 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002926 }
2927
Yi Jin6b514142017-10-30 14:54:12 -07002928 /**
2929 * @hide
2930 */
2931 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2932 long token = proto.start(fieldId);
2933 proto.write(NotificationProto.CHANNEL_ID, getChannelId());
2934 proto.write(NotificationProto.HAS_TICKER_TEXT, this.tickerText != null);
2935 proto.write(NotificationProto.FLAGS, this.flags);
2936 proto.write(NotificationProto.COLOR, this.color);
2937 proto.write(NotificationProto.CATEGORY, this.category);
2938 proto.write(NotificationProto.GROUP_KEY, this.mGroupKey);
2939 proto.write(NotificationProto.SORT_KEY, this.mSortKey);
2940 if (this.actions != null) {
2941 proto.write(NotificationProto.ACTION_LENGTH, this.actions.length);
2942 }
2943 if (this.visibility >= VISIBILITY_SECRET && this.visibility <= VISIBILITY_PUBLIC) {
2944 proto.write(NotificationProto.VISIBILITY, this.visibility);
2945 }
2946 if (publicVersion != null) {
2947 publicVersion.writeToProto(proto, NotificationProto.PUBLIC_VERSION);
2948 }
2949 proto.end(token);
2950 }
2951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002952 @Override
2953 public String toString() {
2954 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002955 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04002956 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002957 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002958 sb.append(priority);
2959 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002960 if (contentView != null) {
2961 sb.append(contentView.getPackage());
2962 sb.append("/0x");
2963 sb.append(Integer.toHexString(contentView.getLayoutId()));
2964 } else {
2965 sb.append("null");
2966 }
2967 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002968 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2969 sb.append("default");
2970 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002971 int N = this.vibrate.length-1;
2972 sb.append("[");
2973 for (int i=0; i<N; i++) {
2974 sb.append(this.vibrate[i]);
2975 sb.append(',');
2976 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002977 if (N != -1) {
2978 sb.append(this.vibrate[N]);
2979 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002980 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002981 } else {
2982 sb.append("null");
2983 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002984 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002985 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002986 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002987 } else if (this.sound != null) {
2988 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002989 } else {
2990 sb.append("null");
2991 }
Chris Wren365b6d32015-07-16 10:39:26 -04002992 if (this.tickerText != null) {
2993 sb.append(" tick");
2994 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002995 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002996 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002997 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002998 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002999 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003000 if (this.category != null) {
3001 sb.append(" category=");
3002 sb.append(this.category);
3003 }
3004 if (this.mGroupKey != null) {
3005 sb.append(" groupKey=");
3006 sb.append(this.mGroupKey);
3007 }
3008 if (this.mSortKey != null) {
3009 sb.append(" sortKey=");
3010 sb.append(this.mSortKey);
3011 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003012 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04003013 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003014 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04003015 }
3016 sb.append(" vis=");
3017 sb.append(visibilityToString(this.visibility));
3018 if (this.publicVersion != null) {
3019 sb.append(" publicVersion=");
3020 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003021 }
3022 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003023 return sb.toString();
3024 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003025
Dan Sandler1b718782014-07-18 12:43:45 -04003026 /**
3027 * {@hide}
3028 */
3029 public static String visibilityToString(int vis) {
3030 switch (vis) {
3031 case VISIBILITY_PRIVATE:
3032 return "PRIVATE";
3033 case VISIBILITY_PUBLIC:
3034 return "PUBLIC";
3035 case VISIBILITY_SECRET:
3036 return "SECRET";
3037 default:
3038 return "UNKNOWN(" + String.valueOf(vis) + ")";
3039 }
3040 }
3041
Joe Onoratocb109a02011-01-18 17:57:41 -08003042 /**
John Spurlock1d881a12015-03-18 19:21:54 -04003043 * {@hide}
3044 */
3045 public static String priorityToString(@Priority int pri) {
3046 switch (pri) {
3047 case PRIORITY_MIN:
3048 return "MIN";
3049 case PRIORITY_LOW:
3050 return "LOW";
3051 case PRIORITY_DEFAULT:
3052 return "DEFAULT";
3053 case PRIORITY_HIGH:
3054 return "HIGH";
3055 case PRIORITY_MAX:
3056 return "MAX";
3057 default:
3058 return "UNKNOWN(" + String.valueOf(pri) + ")";
3059 }
3060 }
3061
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04003062 /**
3063 * @hide
3064 */
3065 public boolean hasCompletedProgress() {
3066 // not a progress notification; can't be complete
3067 if (!extras.containsKey(EXTRA_PROGRESS)
3068 || !extras.containsKey(EXTRA_PROGRESS_MAX)) {
3069 return false;
3070 }
3071 // many apps use max 0 for 'indeterminate'; not complete
3072 if (extras.getInt(EXTRA_PROGRESS_MAX) == 0) {
3073 return false;
3074 }
3075 return extras.getInt(EXTRA_PROGRESS) == extras.getInt(EXTRA_PROGRESS_MAX);
3076 }
3077
Jeff Sharkey000ce802017-04-29 13:13:27 -06003078 /** @removed */
3079 @Deprecated
Julia Reynolds37856052016-11-11 09:20:07 -05003080 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003081 return mChannelId;
3082 }
3083
3084 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003085 * Returns the id of the channel this notification posts to.
3086 */
3087 public String getChannelId() {
3088 return mChannelId;
3089 }
3090
Jeff Sharkey000ce802017-04-29 13:13:27 -06003091 /** @removed */
3092 @Deprecated
Julia Reynolds2a128742016-11-28 14:29:25 -05003093 public long getTimeout() {
3094 return mTimeout;
3095 }
3096
3097 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003098 * Returns the duration from posting after which this notification should be canceled by the
3099 * system, if it's not canceled already.
3100 */
3101 public long getTimeoutAfter() {
3102 return mTimeout;
3103 }
3104
3105 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003106 * Returns what icon should be shown for this notification if it is being displayed in a
3107 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
3108 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
3109 */
3110 public int getBadgeIconType() {
3111 return mBadgeIcon;
3112 }
3113
3114 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003115 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04003116 *
3117 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
3118 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003119 */
3120 public String getShortcutId() {
3121 return mShortcutId;
3122 }
3123
Julia Reynolds3aedded2017-03-31 14:42:09 -04003124
3125 /**
3126 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
3127 */
3128 public CharSequence getSettingsText() {
3129 return mSettingsText;
3130 }
3131
Julia Reynolds13d898c2017-02-02 12:22:05 -05003132 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003133 * Returns which type of notifications in a group are responsible for audibly alerting the
3134 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
3135 * {@link #GROUP_ALERT_SUMMARY}.
3136 */
3137 public @GroupAlertBehavior int getGroupAlertBehavior() {
3138 return mGroupAlertBehavior;
3139 }
3140
3141 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003142 * Returns the bubble metadata that will be used to display app content in a floating window
3143 * over the existing foreground activity.
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003144 */
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003145 public BubbleMetadata getBubbleMetadata() {
3146 return mBubbleMetadata;
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003147 }
3148
Gustav Sennton79ebf4b2018-12-19 10:31:51 +00003149 /**
3150 * Returns whether the platform is allowed (by the app developer) to generate contextual actions
3151 * for this notification.
3152 */
Gustav Sennton761884c2018-11-19 17:40:19 +00003153 public boolean getAllowSystemGeneratedContextualActions() {
3154 return mAllowSystemGeneratedContextualActions;
3155 }
3156
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003157 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003158 * The small icon representing this notification in the status bar and content view.
3159 *
3160 * @return the small icon representing this notification.
3161 *
3162 * @see Builder#getSmallIcon()
3163 * @see Builder#setSmallIcon(Icon)
3164 */
3165 public Icon getSmallIcon() {
3166 return mSmallIcon;
3167 }
3168
3169 /**
3170 * Used when notifying to clean up legacy small icons.
3171 * @hide
3172 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003173 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04003174 public void setSmallIcon(Icon icon) {
3175 mSmallIcon = icon;
3176 }
3177
3178 /**
3179 * The large icon shown in this notification's content view.
3180 * @see Builder#getLargeIcon()
3181 * @see Builder#setLargeIcon(Icon)
3182 */
3183 public Icon getLargeIcon() {
3184 return mLargeIcon;
3185 }
3186
3187 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02003188 * @hide
3189 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003190 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003191 public boolean isGroupSummary() {
3192 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
3193 }
3194
3195 /**
3196 * @hide
3197 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003198 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003199 public boolean isGroupChild() {
3200 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
3201 }
3202
3203 /**
Julia Reynolds30203152017-05-26 13:36:31 -04003204 * @hide
3205 */
3206 public boolean suppressAlertingDueToGrouping() {
3207 if (isGroupSummary()
3208 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
3209 return true;
3210 } else if (isGroupChild()
3211 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
3212 return true;
3213 }
3214 return false;
3215 }
3216
Tony Mak638430e2018-10-08 19:19:10 +01003217
3218 /**
3219 * Finds and returns a remote input and its corresponding action.
3220 *
3221 * @param requiresFreeform requires the remoteinput to allow freeform or not.
3222 * @return the result pair, {@code null} if no result is found.
3223 *
3224 * @hide
3225 */
3226 @Nullable
3227 public Pair<RemoteInput, Action> findRemoteInputActionPair(boolean requiresFreeform) {
3228 if (actions == null) {
3229 return null;
3230 }
3231 for (Notification.Action action : actions) {
3232 if (action.getRemoteInputs() == null) {
3233 continue;
3234 }
3235 RemoteInput resultRemoteInput = null;
3236 for (RemoteInput remoteInput : action.getRemoteInputs()) {
3237 if (remoteInput.getAllowFreeFormInput() || !requiresFreeform) {
3238 resultRemoteInput = remoteInput;
3239 }
3240 }
3241 if (resultRemoteInput != null) {
3242 return Pair.create(resultRemoteInput, action);
3243 }
3244 }
3245 return null;
3246 }
3247
Julia Reynolds30203152017-05-26 13:36:31 -04003248 /**
Gustav Sennton005d7a02019-01-04 13:41:32 +00003249 * Returns the actions that are contextual out of the actions in this notification.
Gustav Senntoneab53682018-11-01 16:30:23 +00003250 *
3251 * @hide
3252 */
3253 public List<Notification.Action> getContextualActions() {
3254 if (actions == null) return Collections.emptyList();
3255
3256 List<Notification.Action> contextualActions = new ArrayList<>();
3257 for (Notification.Action action : actions) {
Gustav Sennton005d7a02019-01-04 13:41:32 +00003258 if (action.isContextual()) {
Gustav Senntoneab53682018-11-01 16:30:23 +00003259 contextualActions.add(action);
3260 }
3261 }
3262 return contextualActions;
3263 }
3264
3265 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003266 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08003267 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003268 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07003269 * content views using the platform's notification layout template. If your app supports
3270 * versions of Android as old as API level 4, you can instead use
3271 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
3272 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
3273 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08003274 *
Scott Main183bf112012-08-13 19:12:13 -07003275 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08003276 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003277 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07003278 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003279 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3280 * .setContentText(subject)
3281 * .setSmallIcon(R.drawable.new_mail)
3282 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003283 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003284 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08003285 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003286 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003287 /**
3288 * @hide
3289 */
3290 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
3291 "android.rebuild.contentViewActionCount";
3292 /**
3293 * @hide
3294 */
3295 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
3296 = "android.rebuild.bigViewActionCount";
3297 /**
3298 * @hide
3299 */
3300 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
3301 = "android.rebuild.hudViewActionCount";
3302
Selim Cinek6743c0b2017-01-18 18:24:01 -08003303 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
3304 SystemProperties.getBoolean("notifications.only_title", true);
3305
Selim Cinek389edcd2017-05-11 19:16:44 -07003306 /**
3307 * The lightness difference that has to be added to the primary text color to obtain the
3308 * secondary text color when the background is light.
3309 */
3310 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
3311
3312 /**
3313 * The lightness difference that has to be added to the primary text color to obtain the
3314 * secondary text color when the background is dark.
3315 * A bit less then the above value, since it looks better on dark backgrounds.
3316 */
3317 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
3318
Joe Onorato46439ce2010-11-19 13:56:21 -08003319 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003320 private Notification mN;
3321 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003322 private Style mStyle;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003323 @UnsupportedAppUsage
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003324 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Selim Cineke7238dd2017-12-14 17:48:32 -08003325 private ArrayList<Person> mPersonList = new ArrayList<>();
Lucas Dupina291d192018-06-07 13:59:42 -07003326 private ContrastColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08003327 private boolean mIsLegacy;
3328 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02003329
3330 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08003331 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
3332 */
3333 private int mCachedContrastColor = COLOR_INVALID;
3334 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08003335 /**
Selim Cinek4717d862018-04-19 09:19:15 +08003336 * Caches a ambient version of {@link #mCachedAmbientColorIsFor}.
Adrian Roos487374f2017-01-11 15:48:14 -08003337 */
3338 private int mCachedAmbientColor = COLOR_INVALID;
3339 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Selim Cinek4717d862018-04-19 09:19:15 +08003340 /**
3341 * A neutral color color that can be used for icons.
3342 */
3343 private int mNeutralColor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08003344
3345 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08003346 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
3347 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
3348 */
3349 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08003350 private int mTextColorsAreForBackground = COLOR_INVALID;
3351 private int mPrimaryTextColor = COLOR_INVALID;
3352 private int mSecondaryTextColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07003353 private int mBackgroundColor = COLOR_INVALID;
3354 private int mForegroundColor = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07003355 /**
3356 * A temporary location where actions are stored. If != null the view originally has action
3357 * but doesn't have any for this inflation.
3358 */
3359 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07003360 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003361
Anthony Chenad4d1582017-04-10 16:07:58 -07003362 private boolean mTintActionButtons;
3363 private boolean mInNightMode;
3364
Adrian Roos70d7aa32017-01-11 15:39:06 -08003365 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003366 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08003367 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003368 * @param context
3369 * A {@link Context} that will be used by the Builder to construct the
3370 * RemoteViews. The Context will not be held past the lifetime of this Builder
3371 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003372 * @param channelId
3373 * The constructed Notification will be posted on this
3374 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
3375 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003376 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003377 public Builder(Context context, String channelId) {
3378 this(context, (Notification) null);
3379 mN.mChannelId = channelId;
3380 }
3381
3382 /**
3383 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
3384 * instead. All posted Notifications must specify a NotificationChannel Id.
3385 */
3386 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003387 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003388 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003389 }
3390
Joe Onoratocb109a02011-01-18 17:57:41 -08003391 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003392 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02003393 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003394 public Builder(Context context, Notification toAdopt) {
3395 mContext = context;
Anthony Chenad4d1582017-04-10 16:07:58 -07003396 Resources res = mContext.getResources();
3397 mTintActionButtons = res.getBoolean(R.bool.config_tintNotificationActionButtons);
3398
3399 if (res.getBoolean(R.bool.config_enableNightMode)) {
3400 Configuration currentConfig = res.getConfiguration();
3401 mInNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
3402 == Configuration.UI_MODE_NIGHT_YES;
3403 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003404
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003405 if (toAdopt == null) {
3406 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003407 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
3408 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
3409 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003410 mN.priority = PRIORITY_DEFAULT;
3411 mN.visibility = VISIBILITY_PRIVATE;
3412 } else {
3413 mN = toAdopt;
3414 if (mN.actions != null) {
3415 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003416 }
3417
Selim Cineke7238dd2017-12-14 17:48:32 -08003418 if (mN.extras.containsKey(EXTRA_PEOPLE_LIST)) {
3419 ArrayList<Person> people = mN.extras.getParcelableArrayList(EXTRA_PEOPLE_LIST);
3420 mPersonList.addAll(people);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003421 }
3422
Selim Cinek4ac6f602016-06-13 15:47:03 -07003423 if (mN.getSmallIcon() == null && mN.icon != 0) {
3424 setSmallIcon(mN.icon);
3425 }
3426
3427 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
3428 setLargeIcon(mN.largeIcon);
3429 }
3430
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003431 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
3432 if (!TextUtils.isEmpty(templateClass)) {
3433 final Class<? extends Style> styleClass
3434 = getNotificationStyleClass(templateClass);
3435 if (styleClass == null) {
3436 Log.d(TAG, "Unknown style class: " + templateClass);
3437 } else {
3438 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003439 final Constructor<? extends Style> ctor =
3440 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003441 ctor.setAccessible(true);
3442 final Style style = ctor.newInstance();
3443 style.restoreFromExtras(mN.extras);
3444
3445 if (style != null) {
3446 setStyle(style);
3447 }
3448 } catch (Throwable t) {
3449 Log.e(TAG, "Could not create Style", t);
3450 }
3451 }
3452 }
3453
3454 }
3455 }
3456
Lucas Dupina291d192018-06-07 13:59:42 -07003457 private ContrastColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08003458 if (mColorUtil == null) {
Lucas Dupina291d192018-06-07 13:59:42 -07003459 mColorUtil = ContrastColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003460 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003461 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003462 }
3463
3464 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003465 * If this notification is duplicative of a Launcher shortcut, sets the
3466 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
3467 * the shortcut.
3468 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04003469 * This field will be ignored by Launchers that don't support badging, don't show
3470 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003471 *
3472 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
3473 * supersedes
3474 */
3475 public Builder setShortcutId(String shortcutId) {
3476 mN.mShortcutId = shortcutId;
3477 return this;
3478 }
3479
3480 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003481 * Sets which icon to display as a badge for this notification.
3482 *
3483 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
3484 * {@link #BADGE_ICON_LARGE}.
3485 *
3486 * Note: This value might be ignored, for launchers that don't support badge icons.
3487 */
Julia Reynolds612beb22017-03-30 10:48:30 -04003488 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04003489 mN.mBadgeIcon = icon;
3490 return this;
3491 }
3492
3493 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003494 * Sets the group alert behavior for this notification. Use this method to mute this
3495 * notification if alerts for this notification's group should be handled by a different
3496 * notification. This is only applicable for notifications that belong to a
Julia Reynolds399d9bf2017-08-11 12:52:14 -04003497 * {@link #setGroup(String) group}. This must be called on all notifications you want to
3498 * mute. For example, if you want only the summary of your group to make noise, all
3499 * children in the group should have the group alert behavior {@link #GROUP_ALERT_SUMMARY}.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003500 *
3501 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
3502 */
3503 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
3504 mN.mGroupAlertBehavior = groupAlertBehavior;
3505 return this;
3506 }
3507
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003508 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003509 * Sets the {@link BubbleMetadata} that will be used to display app content in a floating
3510 * window over the existing foreground activity.
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003511 *
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003512 * <p>This data will be ignored unless the notification is posted to a channel that
3513 * allows {@link NotificationChannel#canBubble() bubbles}.</p>
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003514 *
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003515 * <b>Notifications with a valid and allowed bubble metadata will display in collapsed state
3516 * outside of the notification shade on unlocked devices. When a user interacts with the
3517 * collapsed state, the bubble intent will be invoked and displayed.</b>
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003518 */
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003519 public Builder setBubbleMetadata(BubbleMetadata data) {
3520 mN.mBubbleMetadata = data;
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003521 return this;
3522 }
3523
Jeff Sharkey000ce802017-04-29 13:13:27 -06003524 /** @removed */
3525 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003526 public Builder setChannel(String channelId) {
3527 mN.mChannelId = channelId;
3528 return this;
3529 }
3530
3531 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003532 * Specifies the channel the notification should be delivered on.
3533 */
3534 public Builder setChannelId(String channelId) {
3535 mN.mChannelId = channelId;
3536 return this;
3537 }
3538
Jeff Sharkey000ce802017-04-29 13:13:27 -06003539 /** @removed */
3540 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05003541 public Builder setTimeout(long durationMs) {
3542 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05003543 return this;
3544 }
3545
3546 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003547 * Specifies a duration in milliseconds after which this notification should be canceled,
3548 * if it is not already canceled.
3549 */
3550 public Builder setTimeoutAfter(long durationMs) {
3551 mN.mTimeout = durationMs;
3552 return this;
3553 }
3554
3555 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003556 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003557 *
3558 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
3559 * shown anymore by default and must be opted into by using
3560 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003561 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003562 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08003563 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003564 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003565 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08003566 return this;
3567 }
3568
Joe Onoratocb109a02011-01-18 17:57:41 -08003569 /**
Griff Hazen50c11652014-05-16 09:46:31 -07003570 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07003571 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003572 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
3573 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07003574 */
3575 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003576 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07003577 return this;
3578 }
3579
3580 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003581 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08003582 *
3583 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003584 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003585 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003586 * Useful when showing an elapsed time (like an ongoing phone call).
3587 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08003588 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07003589 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08003590 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003591 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003592 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07003593 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003594 */
3595 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003596 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003597 return this;
3598 }
3599
3600 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08003601 * Sets the Chronometer to count down instead of counting up.
3602 *
3603 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
3604 * If it isn't set the chronometer will count up.
3605 *
3606 * @see #setUsesChronometer(boolean)
3607 */
Adrian Roos96b7e202016-05-17 13:50:38 -07003608 public Builder setChronometerCountDown(boolean countDown) {
3609 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003610 return this;
3611 }
3612
3613 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003614 * Set the small icon resource, which will be used to represent the notification in the
3615 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003616 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003617
3618 * The platform template for the expanded view will draw this icon in the left, unless a
3619 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3620 * icon will be moved to the right-hand side.
3621 *
3622
3623 * @param icon
3624 * A resource ID in the application's package of the drawable to use.
3625 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003626 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003627 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003628 return setSmallIcon(icon != 0
3629 ? Icon.createWithResource(mContext, icon)
3630 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003631 }
3632
Joe Onoratocb109a02011-01-18 17:57:41 -08003633 /**
3634 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3635 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3636 * LevelListDrawable}.
3637 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003638 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003639 * @param level The level to use for the icon.
3640 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003641 * @see Notification#icon
3642 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003643 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003644 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003645 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003646 return setSmallIcon(icon);
3647 }
3648
3649 /**
3650 * Set the small icon, which will be used to represent the notification in the
koprivadebd4ee2018-09-13 10:59:46 -07003651 * status bar and content view (unless overridden there by a
Dan Sandlerd63f9322015-05-06 15:18:49 -04003652 * {@link #setLargeIcon(Bitmap) large icon}).
3653 *
3654 * @param icon An Icon object to use.
3655 * @see Notification#icon
3656 */
3657 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003658 mN.setSmallIcon(icon);
3659 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3660 mN.icon = icon.getResId();
3661 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003662 return this;
3663 }
3664
Joe Onoratocb109a02011-01-18 17:57:41 -08003665 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003666 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003667 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003668 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003669 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003670 return this;
3671 }
3672
Joe Onoratocb109a02011-01-18 17:57:41 -08003673 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003674 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003675 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003676 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003677 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003678 return this;
3679 }
3680
Joe Onoratocb109a02011-01-18 17:57:41 -08003681 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003682 * This provides some additional information that is displayed in the notification. No
3683 * guarantees are given where exactly it is displayed.
3684 *
3685 * <p>This information should only be provided if it provides an essential
3686 * benefit to the understanding of the notification. The more text you provide the
3687 * less readable it becomes. For example, an email client should only provide the account
3688 * name here if more than one email account has been added.</p>
3689 *
3690 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3691 * notification header area.
3692 *
3693 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3694 * this will be shown in the third line of text in the platform notification template.
3695 * You should not be using {@link #setProgress(int, int, boolean)} at the
3696 * same time on those versions; they occupy the same place.
3697 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003698 */
3699 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003700 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003701 return this;
3702 }
3703
3704 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003705 * Provides text that will appear as a link to your application's settings.
3706 *
3707 * <p>This text does not appear within notification {@link Style templates} but may
3708 * appear when the user uses an affordance to learn more about the notification.
3709 * Additionally, this text will not appear unless you provide a valid link target by
3710 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3711 *
3712 * <p>This text is meant to be concise description about what the user can customize
3713 * when they click on this link. The recommended maximum length is 40 characters.
3714 * @param text
3715 * @return
3716 */
3717 public Builder setSettingsText(CharSequence text) {
3718 mN.mSettingsText = safeCharSequence(text);
3719 return this;
3720 }
3721
3722 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003723 * Set the remote input history.
3724 *
3725 * This should be set to the most recent inputs that have been sent
3726 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3727 * longer relevant (e.g. for chat notifications once the other party has responded).
3728 *
3729 * The most recent input must be stored at the 0 index, the second most recent at the
3730 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3731 * and how much of each individual input is shown.
3732 *
3733 * <p>Note: The reply text will only be shown on notifications that have least one action
3734 * with a {@code RemoteInput}.</p>
3735 */
3736 public Builder setRemoteInputHistory(CharSequence[] text) {
3737 if (text == null) {
3738 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3739 } else {
3740 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3741 CharSequence[] safe = new CharSequence[N];
3742 for (int i = 0; i < N; i++) {
3743 safe[i] = safeCharSequence(text[i]);
3744 }
3745 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3746 }
3747 return this;
3748 }
3749
3750 /**
Kenny Guya0f6de82018-04-06 16:20:16 +01003751 * Sets whether remote history entries view should have a spinner.
3752 * @hide
3753 */
3754 public Builder setShowRemoteInputSpinner(boolean showSpinner) {
3755 mN.extras.putBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER, showSpinner);
3756 return this;
3757 }
3758
3759 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +01003760 * Sets whether smart reply buttons should be hidden.
3761 * @hide
3762 */
3763 public Builder setHideSmartReplies(boolean hideSmartReplies) {
3764 mN.extras.putBoolean(EXTRA_HIDE_SMART_REPLIES, hideSmartReplies);
3765 return this;
3766 }
3767
3768 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003769 * Sets the number of items this notification represents. May be displayed as a badge count
3770 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003771 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08003772 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003773 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003774 return this;
3775 }
3776
Joe Onoratocb109a02011-01-18 17:57:41 -08003777 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003778 * A small piece of additional information pertaining to this notification.
3779 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003780 * The platform template will draw this on the last line of the notification, at the far
3781 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003782 *
3783 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3784 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3785 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003786 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003787 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003788 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003789 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003790 return this;
3791 }
3792
Joe Onoratocb109a02011-01-18 17:57:41 -08003793 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003794 * Set the progress this notification represents.
3795 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003796 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003797 */
3798 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003799 mN.extras.putInt(EXTRA_PROGRESS, progress);
3800 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3801 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003802 return this;
3803 }
3804
3805 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003806 * Supply a custom RemoteViews to use instead of the platform template.
3807 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003808 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003809 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003810 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003811 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003812 return setCustomContentView(views);
3813 }
3814
3815 /**
3816 * Supply custom RemoteViews to use instead of the platform template.
3817 *
3818 * This will override the layout that would otherwise be constructed by this Builder
3819 * object.
3820 */
3821 public Builder setCustomContentView(RemoteViews contentView) {
3822 mN.contentView = contentView;
3823 return this;
3824 }
3825
3826 /**
3827 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3828 *
3829 * This will override the expanded layout that would otherwise be constructed by this
3830 * Builder object.
3831 */
3832 public Builder setCustomBigContentView(RemoteViews contentView) {
3833 mN.bigContentView = contentView;
3834 return this;
3835 }
3836
3837 /**
3838 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3839 *
3840 * This will override the heads-up layout that would otherwise be constructed by this
3841 * Builder object.
3842 */
3843 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3844 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003845 return this;
3846 }
3847
Joe Onoratocb109a02011-01-18 17:57:41 -08003848 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003849 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3850 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003851 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3852 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3853 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003854 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003855 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003856 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003857 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003858 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003859 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003860 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003861 return this;
3862 }
3863
Joe Onoratocb109a02011-01-18 17:57:41 -08003864 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003865 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3866 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003867 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003868 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003869 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003870 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003871 return this;
3872 }
3873
Joe Onoratocb109a02011-01-18 17:57:41 -08003874 /**
3875 * An intent to launch instead of posting the notification to the status bar.
3876 * Only for use with extremely high-priority notifications demanding the user's
3877 * <strong>immediate</strong> attention, such as an incoming phone call or
3878 * alarm clock that the user has explicitly set to a particular time.
3879 * If this facility is used for something else, please give the user an option
3880 * to turn it off and use a normal notification, as this can be extremely
3881 * disruptive.
3882 *
Chris Wren47c20a12014-06-18 17:27:29 -04003883 * <p>
3884 * The system UI may choose to display a heads-up notification, instead of
3885 * launching this intent, while the user is using the device.
3886 * </p>
Julia Reynolds47fd15f2018-11-28 10:16:00 -05003887 * <p>Apps targeting {@link Build.VERSION_CODES#Q} and above will have to request
3888 * a permission ({@link android.Manifest.permission#USE_FULL_SCREEN_INTENT}) in order to
3889 * use full screen intents.</p>
Chris Wren47c20a12014-06-18 17:27:29 -04003890 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003891 * @param intent The pending intent to launch.
3892 * @param highPriority Passing true will cause this notification to be sent
3893 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003894 *
3895 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003896 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003897 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003898 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003899 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3900 return this;
3901 }
3902
Joe Onoratocb109a02011-01-18 17:57:41 -08003903 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003904 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003905 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003906 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003907 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003908 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003909 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003910 return this;
3911 }
3912
Joe Onoratocb109a02011-01-18 17:57:41 -08003913 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003914 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003915 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003916 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003917 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003918 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003919 setTicker(tickerText);
3920 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003921 return this;
3922 }
3923
Joe Onoratocb109a02011-01-18 17:57:41 -08003924 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003925 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003926 *
3927 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04003928 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3929 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04003930 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003931 public Builder setLargeIcon(Bitmap b) {
3932 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3933 }
3934
3935 /**
3936 * Add a large icon to the notification content view.
3937 *
3938 * In the platform template, this image will be shown on the left of the notification view
3939 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3940 * badge atop the large icon).
3941 */
3942 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003943 mN.mLargeIcon = icon;
3944 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08003945 return this;
3946 }
3947
Joe Onoratocb109a02011-01-18 17:57:41 -08003948 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003949 * Set the sound to play.
3950 *
John Spurlockc0650f022014-07-19 13:22:39 -04003951 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
3952 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003953 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003954 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003955 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003956 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08003957 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003958 mN.sound = sound;
3959 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08003960 return this;
3961 }
3962
Joe Onoratocb109a02011-01-18 17:57:41 -08003963 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003964 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08003965 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003966 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
3967 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003968 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003969 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07003970 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003971 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08003972 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003973 mN.sound = sound;
3974 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08003975 return this;
3976 }
3977
Joe Onoratocb109a02011-01-18 17:57:41 -08003978 /**
John Spurlockc0650f022014-07-19 13:22:39 -04003979 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
3980 * use during playback.
3981 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003982 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04003983 * @see Notification#sound
3984 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003985 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04003986 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003987 mN.sound = sound;
3988 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04003989 return this;
3990 }
3991
3992 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003993 * Set the vibration pattern to use.
3994 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003995 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
3996 * <code>pattern</code> parameter.
3997 *
Chris Wren47c20a12014-06-18 17:27:29 -04003998 * <p>
3999 * A notification that vibrates is more likely to be presented as a heads-up notification.
4000 * </p>
4001 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004002 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004003 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08004004 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004005 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004006 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004007 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08004008 return this;
4009 }
4010
Joe Onoratocb109a02011-01-18 17:57:41 -08004011 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004012 * Set the desired color for the indicator LED on the device, as well as the
4013 * blink duty cycle (specified in milliseconds).
4014 *
4015
4016 * Not all devices will honor all (or even any) of these values.
4017 *
Julia Reynolds529e3322017-02-06 08:33:01 -05004018 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004019 * @see Notification#ledARGB
4020 * @see Notification#ledOnMS
4021 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08004022 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004023 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08004024 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004025 mN.ledARGB = argb;
4026 mN.ledOnMS = onMs;
4027 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004028 if (onMs != 0 || offMs != 0) {
4029 mN.flags |= FLAG_SHOW_LIGHTS;
4030 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004031 return this;
4032 }
4033
Joe Onoratocb109a02011-01-18 17:57:41 -08004034 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004035 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08004036 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004037
4038 * Ongoing notifications cannot be dismissed by the user, so your application or service
4039 * must take care of canceling them.
4040 *
4041
4042 * They are typically used to indicate a background task that the user is actively engaged
4043 * with (e.g., playing music) or is pending in some way and therefore occupying the device
4044 * (e.g., a file download, sync operation, active network connection).
4045 *
4046
4047 * @see Notification#FLAG_ONGOING_EVENT
Joe Onoratocb109a02011-01-18 17:57:41 -08004048 */
Joe Onorato46439ce2010-11-19 13:56:21 -08004049 public Builder setOngoing(boolean ongoing) {
4050 setFlag(FLAG_ONGOING_EVENT, ongoing);
4051 return this;
4052 }
4053
Joe Onoratocb109a02011-01-18 17:57:41 -08004054 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08004055 * Set whether this notification should be colorized. When set, the color set with
4056 * {@link #setColor(int)} will be used as the background color of this notification.
4057 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08004058 * This should only be used for high priority ongoing tasks like navigation, an ongoing
4059 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08004060 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07004061 * For most styles, the coloring will only be applied if the notification is for a
4062 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08004063 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07004064 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08004065 *
Aurimas Liutikas7f695332018-05-31 21:07:32 -07004066 * @see #setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08004067 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08004068 */
4069 public Builder setColorized(boolean colorize) {
4070 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
4071 return this;
4072 }
4073
4074 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08004075 * Set this flag if you would only like the sound, vibrate
4076 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004077 *
4078 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08004079 */
Joe Onorato46439ce2010-11-19 13:56:21 -08004080 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
4081 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
4082 return this;
4083 }
4084
Joe Onoratocb109a02011-01-18 17:57:41 -08004085 /**
Julia Reynolds04499532016-09-13 14:04:53 -04004086 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004087 *
4088 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08004089 */
Joe Onorato46439ce2010-11-19 13:56:21 -08004090 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08004091 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08004092 return this;
4093 }
4094
Joe Onoratocb109a02011-01-18 17:57:41 -08004095 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08004096 * Set whether or not this notification should not bridge to other devices.
4097 *
4098 * <p>Some notifications can be bridged to other devices for remote display.
4099 * This hint can be set to recommend this notification not be bridged.
4100 */
4101 public Builder setLocalOnly(boolean localOnly) {
4102 setFlag(FLAG_LOCAL_ONLY, localOnly);
4103 return this;
4104 }
4105
4106 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004107 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08004108 * <p>
4109 * The value should be one or more of the following fields combined with
4110 * bitwise-or:
4111 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
4112 * <p>
4113 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004114 *
4115 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05004116 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004117 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08004118 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004119 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004120 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004121 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08004122 return this;
4123 }
4124
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004125 /**
4126 * Set the priority of this notification.
4127 *
4128 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004129 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004130 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004131 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07004132 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004133 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004134 return this;
4135 }
Joe Malin8d40d042012-11-05 11:36:40 -08004136
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004137 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04004138 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08004139 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04004140 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004141 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04004142 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004143 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004144 return this;
4145 }
4146
4147 /**
Chris Wrendde75302014-03-26 17:24:15 -04004148 * Add a person that is relevant to this notification.
4149 *
Chris Wrene6c48932014-09-29 17:19:27 -04004150 * <P>
4151 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04004152 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4153 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4154 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04004155 * </P>
4156 *
4157 * <P>
4158 * The person should be specified by the {@code String} representation of a
4159 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
4160 * </P>
4161 *
4162 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
4163 * URIs. The path part of these URIs must exist in the contacts database, in the
4164 * appropriate column, or the reference will be discarded as invalid. Telephone schema
4165 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
Selim Cineke7238dd2017-12-14 17:48:32 -08004166 * It is also possible to provide a URI with the schema {@code name:} in order to uniquely
4167 * identify a person without an entry in the contacts database.
Chris Wrene6c48932014-09-29 17:19:27 -04004168 * </P>
4169 *
4170 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04004171 * @see Notification#EXTRA_PEOPLE
Selim Cineke7238dd2017-12-14 17:48:32 -08004172 * @deprecated use {@link #addPerson(Person)}
Chris Wrendde75302014-03-26 17:24:15 -04004173 */
Chris Wrene6c48932014-09-29 17:19:27 -04004174 public Builder addPerson(String uri) {
Selim Cinek9acd6732018-03-23 16:39:02 -07004175 addPerson(new Person.Builder().setUri(uri).build());
Selim Cineke7238dd2017-12-14 17:48:32 -08004176 return this;
4177 }
4178
4179 /**
4180 * Add a person that is relevant to this notification.
4181 *
4182 * <P>
4183 * Depending on user preferences, this annotation may allow the notification to pass
4184 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4185 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4186 * appear more prominently in the user interface.
4187 * </P>
4188 *
4189 * <P>
4190 * A person should usually contain a uri in order to benefit from the ranking boost.
4191 * However, even if no uri is provided, it's beneficial to provide other people in the
4192 * notification, such that listeners and voice only devices can announce and handle them
4193 * properly.
4194 * </P>
4195 *
4196 * @param person the person to add.
4197 * @see Notification#EXTRA_PEOPLE_LIST
4198 */
4199 public Builder addPerson(Person person) {
4200 mPersonList.add(person);
Chris Wrendde75302014-03-26 17:24:15 -04004201 return this;
4202 }
4203
4204 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004205 * Set this notification to be part of a group of notifications sharing the same key.
4206 * Grouped notifications may display in a cluster or stack on devices which
4207 * support such rendering.
4208 *
4209 * <p>To make this notification the summary for its group, also call
4210 * {@link #setGroupSummary}. A sort order can be specified for group members by using
4211 * {@link #setSortKey}.
4212 * @param groupKey The group key of the group.
4213 * @return this object for method chaining
4214 */
4215 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004216 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004217 return this;
4218 }
4219
4220 /**
4221 * Set this notification to be the group summary for a group of notifications.
4222 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04004223 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
4224 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004225 * @param isGroupSummary Whether this notification should be a group summary.
4226 * @return this object for method chaining
4227 */
4228 public Builder setGroupSummary(boolean isGroupSummary) {
4229 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
4230 return this;
4231 }
4232
4233 /**
4234 * Set a sort key that orders this notification among other notifications from the
4235 * same package. This can be useful if an external sort was already applied and an app
4236 * would like to preserve this. Notifications will be sorted lexicographically using this
4237 * value, although providing different priorities in addition to providing sort key may
4238 * cause this value to be ignored.
4239 *
4240 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07004241 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004242 *
4243 * @see String#compareTo(String)
4244 */
4245 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004246 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004247 return this;
4248 }
4249
4250 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004251 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004252 *
Griff Hazen720042b2014-02-24 15:46:56 -08004253 * <p>Values within the Bundle will replace existing extras values in this Builder.
4254 *
4255 * @see Notification#extras
4256 */
Griff Hazen959591e2014-05-15 22:26:18 -07004257 public Builder addExtras(Bundle extras) {
4258 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004259 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08004260 }
4261 return this;
4262 }
4263
4264 /**
4265 * Set metadata for this notification.
4266 *
4267 * <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 -04004268 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004269 * called.
4270 *
Griff Hazen720042b2014-02-24 15:46:56 -08004271 * <p>Replaces any existing extras values with those from the provided Bundle.
4272 * Use {@link #addExtras} to merge in metadata instead.
4273 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004274 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004275 */
Griff Hazen959591e2014-05-15 22:26:18 -07004276 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004277 if (extras != null) {
4278 mUserExtras = extras;
4279 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004280 return this;
4281 }
4282
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004283 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004284 * Get the current metadata Bundle used by this notification Builder.
4285 *
4286 * <p>The returned Bundle is shared with this Builder.
4287 *
4288 * <p>The current contents of this Bundle are copied into the Notification each time
4289 * {@link #build()} is called.
4290 *
4291 * @see Notification#extras
4292 */
4293 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004294 return mUserExtras;
4295 }
4296
4297 private Bundle getAllExtras() {
4298 final Bundle saveExtras = (Bundle) mUserExtras.clone();
4299 saveExtras.putAll(mN.extras);
4300 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08004301 }
4302
4303 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004304 * Add an action to this notification. Actions are typically displayed by
4305 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004306 * <p>
4307 * Every action must have an icon (32dp square and matching the
4308 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4309 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4310 * <p>
4311 * A notification in its expanded form can display up to 3 actions, from left to right in
4312 * the order they were added. Actions will not be displayed when the notification is
4313 * collapsed, however, so be sure that any essential functions may be accessed by the user
4314 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004315 *
4316 * @param icon Resource ID of a drawable that represents the action.
4317 * @param title Text describing the action.
4318 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04004319 *
4320 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004321 */
Dan Sandler86647982015-05-13 23:41:13 -04004322 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004323 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004324 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004325 return this;
4326 }
4327
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004328 /**
Griff Hazen959591e2014-05-15 22:26:18 -07004329 * Add an action to this notification. Actions are typically displayed by
4330 * the system as a button adjacent to the notification content.
4331 * <p>
4332 * Every action must have an icon (32dp square and matching the
4333 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4334 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4335 * <p>
4336 * A notification in its expanded form can display up to 3 actions, from left to right in
4337 * the order they were added. Actions will not be displayed when the notification is
4338 * collapsed, however, so be sure that any essential functions may be accessed by the user
4339 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
4340 *
4341 * @param action The action to add.
4342 */
4343 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08004344 if (action != null) {
4345 mActions.add(action);
4346 }
Griff Hazen959591e2014-05-15 22:26:18 -07004347 return this;
4348 }
4349
4350 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004351 * Alter the complete list of actions attached to this notification.
4352 * @see #addAction(Action).
4353 *
4354 * @param actions
4355 * @return
4356 */
4357 public Builder setActions(Action... actions) {
4358 mActions.clear();
4359 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08004360 if (actions[i] != null) {
4361 mActions.add(actions[i]);
4362 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004363 }
4364 return this;
4365 }
4366
4367 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004368 * Add a rich notification style to be applied at build time.
4369 *
4370 * @param style Object responsible for modifying the notification style.
4371 */
4372 public Builder setStyle(Style style) {
4373 if (mStyle != style) {
4374 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004375 if (mStyle != null) {
4376 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004377 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
4378 } else {
4379 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004380 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004381 }
4382 return this;
4383 }
4384
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004385 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05004386 * Returns the style set by {@link #setStyle(Style)}.
4387 */
4388 public Style getStyle() {
4389 return mStyle;
4390 }
4391
4392 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004393 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07004394 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004395 * @return The same Builder.
4396 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06004397 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004398 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004399 return this;
4400 }
4401
4402 /**
4403 * Supply a replacement Notification whose contents should be shown in insecure contexts
4404 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
4405 * @param n A replacement notification, presumably with some or all info redacted.
4406 * @return The same Builder.
4407 */
4408 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004409 if (n != null) {
4410 mN.publicVersion = new Notification();
4411 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
4412 } else {
4413 mN.publicVersion = null;
4414 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004415 return this;
4416 }
4417
Griff Hazenb720abe2014-05-20 13:15:30 -07004418 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004419 * Apply an extender to this notification builder. Extenders may be used to add
4420 * metadata or change options on this builder.
4421 */
Griff Hazen61a9e862014-05-22 16:05:19 -07004422 public Builder extend(Extender extender) {
4423 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004424 return this;
4425 }
4426
Dan Sandler4e787062015-06-17 15:09:48 -04004427 /**
4428 * @hide
4429 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04004430 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08004431 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004432 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004433 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004434 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004435 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04004436 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08004437 }
4438
Dan Sandler26e81cf2014-05-06 10:01:27 -04004439 /**
4440 * Sets {@link Notification#color}.
4441 *
4442 * @param argb The accent color to use
4443 *
4444 * @return The same Builder.
4445 */
Tor Norbye80756e32015-03-02 09:39:27 -08004446 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004447 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004448 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04004449 return this;
4450 }
4451
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004452 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04004453 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
4454 // This user can never be a badged profile,
4455 // and also includes USER_ALL system notifications.
4456 return null;
4457 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02004458 // Note: This assumes that the current user can read the profile badge of the
4459 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08004460 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05004461 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004462 }
4463
4464 private Bitmap getProfileBadge() {
4465 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01004466 if (badge == null) {
4467 return null;
4468 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004469 final int size = mContext.getResources().getDimensionPixelSize(
4470 R.dimen.notification_badge_size);
4471 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004472 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004473 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004474 badge.draw(canvas);
4475 return bitmap;
4476 }
4477
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004478 private void bindProfileBadge(RemoteViews contentView, StandardTemplateParams p) {
Kenny Guy98193ea2014-07-24 19:54:37 +01004479 Bitmap profileBadge = getProfileBadge();
4480
Kenny Guy98193ea2014-07-24 19:54:37 +01004481 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004482 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
4483 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004484 if (isColorized(p)) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004485 contentView.setDrawableTint(R.id.profile_badge, false,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004486 getPrimaryTextColor(p), PorterDuff.Mode.SRC_ATOP);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004487 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004488 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004489 }
4490
Gus Prevasbae5ba32018-11-28 15:45:13 -05004491 private void bindAlertedIcon(RemoteViews contentView, StandardTemplateParams p) {
4492 contentView.setDrawableTint(
4493 R.id.alerted_icon,
4494 false /* targetBackground */,
4495 getNeutralColor(p),
4496 PorterDuff.Mode.SRC_ATOP);
4497 }
4498
Julia Reynoldsfc640012018-02-21 12:25:27 -05004499 /**
4500 * @hide
4501 */
4502 public boolean usesStandardHeader() {
4503 if (mN.mUsesStandardHeader) {
4504 return true;
4505 }
4506 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.N) {
4507 if (mN.contentView == null && mN.bigContentView == null) {
4508 return true;
4509 }
4510 }
4511 boolean contentViewUsesHeader = mN.contentView == null
4512 || STANDARD_LAYOUTS.contains(mN.contentView.getLayoutId());
4513 boolean bigContentViewUsesHeader = mN.bigContentView == null
4514 || STANDARD_LAYOUTS.contains(mN.bigContentView.getLayoutId());
4515 return contentViewUsesHeader && bigContentViewUsesHeader;
4516 }
4517
Christoph Studerfe718432014-09-01 18:21:18 +02004518 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004519 resetNotificationHeader(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02004520 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08004521 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004522 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08004523 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004524 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08004525 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08004526 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004527 }
4528
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004529 /**
4530 * Resets the notification header to its original state
4531 */
4532 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07004533 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
4534 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08004535 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004536 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02004537 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004538 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07004539 contentView.setTextViewText(R.id.header_text, null);
Selim Cinekafeed292017-12-12 17:32:44 -08004540 contentView.setViewVisibility(R.id.header_text_secondary, View.GONE);
4541 contentView.setTextViewText(R.id.header_text_secondary, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004542 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinekafeed292017-12-12 17:32:44 -08004543 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004544 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004545 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004546 contentView.setImageViewIcon(R.id.profile_badge, null);
4547 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Gus Prevasa3226492018-10-23 11:10:09 -04004548 contentView.setViewVisibility(R.id.alerted_icon, View.GONE);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004549 mN.mUsesStandardHeader = false;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004550 }
4551
Selim Cinek384804b2018-04-18 14:31:07 +08004552 private RemoteViews applyStandardTemplate(int resId, TemplateBindResult result) {
4553 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this),
4554 result);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004555 }
4556
Selim Cinek384804b2018-04-18 14:31:07 +08004557 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p,
4558 TemplateBindResult result) {
Kenny Guy77320062014-08-27 21:37:15 +01004559 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04004560
Christoph Studerfe718432014-09-01 18:21:18 +02004561 resetStandardTemplate(contentView);
4562
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004563 final Bundle ex = mN.extras;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004564 updateBackgroundColor(contentView, p);
4565 bindNotificationHeader(contentView, p);
Selim Cinek384804b2018-04-18 14:31:07 +08004566 bindLargeIconAndReply(contentView, p, result);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004567 boolean showProgress = handleProgressBar(contentView, ex, p);
Adrian Roos70d7aa32017-01-11 15:39:06 -08004568 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08004569 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004570 contentView.setTextViewText(R.id.title, processTextSpans(p.title));
Lucas Dupin00be88f2019-01-03 17:50:52 -08004571 setTextViewColorPrimary(contentView, R.id.title, p);
Selim Cinek954cc232016-05-20 13:29:23 -07004572 contentView.setViewLayoutWidth(R.id.title, showProgress
4573 ? ViewGroup.LayoutParams.WRAP_CONTENT
4574 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08004575 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08004576 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08004577 int textId = showProgress ? com.android.internal.R.id.text_line_1
4578 : com.android.internal.R.id.text;
Selim Cinek48f66b72017-08-18 16:17:51 -07004579 contentView.setTextViewText(textId, processTextSpans(p.text));
Lucas Dupin00be88f2019-01-03 17:50:52 -08004580 setTextViewColorSecondary(contentView, textId, p);
Selim Cinek41598732016-01-11 16:58:37 -08004581 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08004582 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08004583
Selim Cinek279fa862016-06-14 10:57:25 -07004584 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004585
Selim Cinek29603462015-11-17 19:04:39 -08004586 return contentView;
4587 }
4588
Selim Cinek48f66b72017-08-18 16:17:51 -07004589 private CharSequence processTextSpans(CharSequence text) {
Lucas Dupind3c99322018-09-27 10:22:29 -07004590 if (hasForegroundColor() || mInNightMode) {
Lucas Dupina291d192018-06-07 13:59:42 -07004591 return ContrastColorUtil.clearColorSpans(text);
Selim Cinek48f66b72017-08-18 16:17:51 -07004592 }
4593 return text;
4594 }
4595
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004596 private void setTextViewColorPrimary(RemoteViews contentView, int id,
4597 StandardTemplateParams p) {
4598 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004599 contentView.setTextColor(id, mPrimaryTextColor);
4600 }
4601
Selim Cinek48f66b72017-08-18 16:17:51 -07004602 private boolean hasForegroundColor() {
4603 return mForegroundColor != COLOR_INVALID;
4604 }
4605
Selim Cinek389edcd2017-05-11 19:16:44 -07004606 /**
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004607 * Return the primary text color using the existing template params
Selim Cinek389edcd2017-05-11 19:16:44 -07004608 * @hide
4609 */
4610 @VisibleForTesting
4611 public int getPrimaryTextColor() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004612 return getPrimaryTextColor(mParams);
4613 }
4614
4615 /**
4616 * @param p the template params to inflate this with
4617 * @return the primary text color
4618 * @hide
4619 */
4620 @VisibleForTesting
4621 public int getPrimaryTextColor(StandardTemplateParams p) {
4622 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004623 return mPrimaryTextColor;
4624 }
4625
Selim Cinek389edcd2017-05-11 19:16:44 -07004626 /**
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004627 * Return the secondary text color using the existing template params
Selim Cinek389edcd2017-05-11 19:16:44 -07004628 * @hide
4629 */
4630 @VisibleForTesting
4631 public int getSecondaryTextColor() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004632 return getSecondaryTextColor(mParams);
4633 }
4634
4635 /**
4636 * @param p the template params to inflate this with
4637 * @return the secondary text color
4638 * @hide
4639 */
4640 @VisibleForTesting
4641 public int getSecondaryTextColor(StandardTemplateParams p) {
4642 ensureColors(p);
Selim Cinek389edcd2017-05-11 19:16:44 -07004643 return mSecondaryTextColor;
4644 }
4645
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004646 private void setTextViewColorSecondary(RemoteViews contentView, int id,
4647 StandardTemplateParams p) {
4648 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004649 contentView.setTextColor(id, mSecondaryTextColor);
4650 }
4651
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004652 private void ensureColors(StandardTemplateParams p) {
4653 int backgroundColor = getBackgroundColor(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004654 if (mPrimaryTextColor == COLOR_INVALID
4655 || mSecondaryTextColor == COLOR_INVALID
Selim Cinek7b9605b2017-01-19 17:36:00 -08004656 || mTextColorsAreForBackground != backgroundColor) {
4657 mTextColorsAreForBackground = backgroundColor;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004658 if (!hasForegroundColor() || !isColorized(p)) {
Lucas Dupina291d192018-06-07 13:59:42 -07004659 mPrimaryTextColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004660 backgroundColor, mInNightMode);
Lucas Dupina291d192018-06-07 13:59:42 -07004661 mSecondaryTextColor = ContrastColorUtil.resolveSecondaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004662 backgroundColor, mInNightMode);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004663 if (backgroundColor != COLOR_DEFAULT && isColorized(p)) {
Lucas Dupina291d192018-06-07 13:59:42 -07004664 mPrimaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004665 mPrimaryTextColor, backgroundColor, 4.5);
Lucas Dupina291d192018-06-07 13:59:42 -07004666 mSecondaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004667 mSecondaryTextColor, backgroundColor, 4.5);
4668 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07004669 } else {
Lucas Dupina291d192018-06-07 13:59:42 -07004670 double backLum = ContrastColorUtil.calculateLuminance(backgroundColor);
4671 double textLum = ContrastColorUtil.calculateLuminance(mForegroundColor);
4672 double contrast = ContrastColorUtil.calculateContrast(mForegroundColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004673 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07004674 // We only respect the given colors if worst case Black or White still has
4675 // contrast
4676 boolean backgroundLight = backLum > textLum
4677 && satisfiesTextContrast(backgroundColor, Color.BLACK)
4678 || backLum <= textLum
4679 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004680 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07004681 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004682 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004683 mForegroundColor,
4684 backgroundColor,
4685 true /* findFG */,
4686 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004687 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004688 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004689 } else {
4690 mSecondaryTextColor =
Lucas Dupina291d192018-06-07 13:59:42 -07004691 ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004692 mForegroundColor,
4693 backgroundColor,
4694 true /* findFG */,
4695 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004696 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004697 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004698 }
4699 } else {
4700 mPrimaryTextColor = mForegroundColor;
Lucas Dupina291d192018-06-07 13:59:42 -07004701 mSecondaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004702 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4703 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Lucas Dupina291d192018-06-07 13:59:42 -07004704 if (ContrastColorUtil.calculateContrast(mSecondaryTextColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004705 backgroundColor) < 4.5f) {
4706 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07004707 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004708 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004709 mSecondaryTextColor,
4710 backgroundColor,
4711 true /* findFG */,
4712 4.5f);
4713 } else {
4714 mSecondaryTextColor
Lucas Dupina291d192018-06-07 13:59:42 -07004715 = ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004716 mSecondaryTextColor,
4717 backgroundColor,
4718 true /* findFG */,
4719 4.5f);
4720 }
Lucas Dupina291d192018-06-07 13:59:42 -07004721 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004722 mSecondaryTextColor, backgroundLight
4723 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4724 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004725 }
4726 }
4727 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004728 }
4729 }
4730
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004731 private void updateBackgroundColor(RemoteViews contentView,
4732 StandardTemplateParams p) {
4733 if (isColorized(p)) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004734 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004735 getBackgroundColor(p));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004736 } else {
4737 // Clear it!
4738 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4739 0);
4740 }
4741 }
4742
Selim Cinek860b6da2015-12-16 19:02:19 -08004743 /**
4744 * @param remoteView the remote view to update the minheight in
4745 * @param hasMinHeight does it have a mimHeight
4746 * @hide
4747 */
4748 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4749 int minHeight = 0;
4750 if (hasMinHeight) {
4751 // we need to set the minHeight of the notification
4752 minHeight = mContext.getResources().getDimensionPixelSize(
4753 com.android.internal.R.dimen.notification_min_content_height);
4754 }
4755 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4756 }
4757
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004758 private boolean handleProgressBar(RemoteViews contentView, Bundle ex,
4759 StandardTemplateParams p) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004760 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4761 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4762 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004763 if (p.hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004764 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004765 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004766 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004767 contentView.setProgressBackgroundTintList(
4768 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4769 R.color.notification_progress_background_color)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004770 if (getRawColor(p) != COLOR_DEFAULT) {
4771 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor(p));
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004772 contentView.setProgressTintList(R.id.progress, colorStateList);
4773 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004774 }
Selim Cinek29603462015-11-17 19:04:39 -08004775 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004776 } else {
4777 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004778 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004779 }
Joe Onorato561d3852010-11-20 18:09:34 -08004780 }
4781
Selim Cinek384804b2018-04-18 14:31:07 +08004782 private void bindLargeIconAndReply(RemoteViews contentView, StandardTemplateParams p,
4783 TemplateBindResult result) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004784 boolean largeIconShown = bindLargeIcon(contentView, p);
4785 boolean replyIconShown = bindReplyIcon(contentView, p);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004786 boolean iconContainerVisible = largeIconShown || replyIconShown;
Selim Cinek384804b2018-04-18 14:31:07 +08004787 contentView.setViewVisibility(R.id.right_icon_container,
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004788 iconContainerVisible ? View.VISIBLE : View.GONE);
Selim Cinek1c72fa02018-04-23 18:00:54 +08004789 int marginEnd = calculateMarginEnd(largeIconShown, replyIconShown);
4790 contentView.setViewLayoutMarginEnd(R.id.line1, marginEnd);
4791 contentView.setViewLayoutMarginEnd(R.id.text, marginEnd);
4792 contentView.setViewLayoutMarginEnd(R.id.progress, marginEnd);
Selim Cinek384804b2018-04-18 14:31:07 +08004793 if (result != null) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08004794 result.setIconMarginEnd(marginEnd);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004795 result.setRightIconContainerVisible(iconContainerVisible);
Selim Cinek384804b2018-04-18 14:31:07 +08004796 }
4797 }
4798
Selim Cinek1c72fa02018-04-23 18:00:54 +08004799 private int calculateMarginEnd(boolean largeIconShown, boolean replyIconShown) {
4800 int marginEnd = 0;
4801 int contentMargin = mContext.getResources().getDimensionPixelSize(
4802 R.dimen.notification_content_margin_end);
4803 int iconSize = mContext.getResources().getDimensionPixelSize(
4804 R.dimen.notification_right_icon_size);
4805 if (replyIconShown) {
4806 // The size of the reply icon
4807 marginEnd += iconSize;
4808
4809 int replyInset = mContext.getResources().getDimensionPixelSize(
4810 R.dimen.notification_reply_inset);
4811 // We're subtracting the inset of the reply icon to make sure it's
4812 // aligned nicely on the right, and remove it from the following padding
4813 marginEnd -= replyInset * 2;
4814 }
4815 if (largeIconShown) {
4816 // adding size of the right icon
4817 marginEnd += iconSize;
4818
4819 if (replyIconShown) {
4820 // We also add some padding to the reply icon if it's around
4821 marginEnd += contentMargin;
4822 }
4823 }
4824 if (replyIconShown || largeIconShown) {
4825 // The padding to the content
4826 marginEnd += contentMargin;
4827 }
4828 return marginEnd;
4829 }
4830
Selim Cinek384804b2018-04-18 14:31:07 +08004831 /**
4832 * Bind the large icon.
4833 * @return if the largeIcon is visible
4834 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004835 private boolean bindLargeIcon(RemoteViews contentView, StandardTemplateParams p) {
Selim Cinek279fa862016-06-14 10:57:25 -07004836 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4837 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4838 }
Lucas Dupin00be88f2019-01-03 17:50:52 -08004839 boolean showLargeIcon = mN.mLargeIcon != null && !p.hideLargeIcon;
Selim Cinek88188f22017-09-19 16:46:56 -07004840 if (showLargeIcon) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004841 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4842 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004843 processLargeLegacyIcon(mN.mLargeIcon, contentView, p);
Selim Cinek88188f22017-09-19 16:46:56 -07004844 }
Selim Cinek384804b2018-04-18 14:31:07 +08004845 return showLargeIcon;
4846 }
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004847
Selim Cinek384804b2018-04-18 14:31:07 +08004848 /**
4849 * Bind the reply icon.
4850 * @return if the reply icon is visible
4851 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004852 private boolean bindReplyIcon(RemoteViews contentView, StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08004853 boolean actionVisible = !p.hideReplyIcon;
Selim Cinek384804b2018-04-18 14:31:07 +08004854 Action action = null;
Selim Cinek88188f22017-09-19 16:46:56 -07004855 if (actionVisible) {
Selim Cinek384804b2018-04-18 14:31:07 +08004856 action = findReplyAction();
4857 actionVisible = action != null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004858 }
Selim Cinek384804b2018-04-18 14:31:07 +08004859 if (actionVisible) {
4860 contentView.setViewVisibility(R.id.reply_icon_action, View.VISIBLE);
4861 contentView.setDrawableTint(R.id.reply_icon_action,
4862 false /* targetBackground */,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004863 getNeutralColor(p),
Selim Cinek384804b2018-04-18 14:31:07 +08004864 PorterDuff.Mode.SRC_ATOP);
4865 contentView.setOnClickPendingIntent(R.id.reply_icon_action, action.actionIntent);
4866 contentView.setRemoteInputs(R.id.reply_icon_action, action.mRemoteInputs);
4867 } else {
4868 contentView.setRemoteInputs(R.id.reply_icon_action, null);
4869 }
4870 contentView.setViewVisibility(R.id.reply_icon_action,
4871 actionVisible ? View.VISIBLE : View.GONE);
4872 return actionVisible;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004873 }
4874
4875 private Action findReplyAction() {
4876 ArrayList<Action> actions = mActions;
4877 if (mOriginalActions != null) {
4878 actions = mOriginalActions;
4879 }
4880 int numActions = actions.size();
4881 for (int i = 0; i < numActions; i++) {
4882 Action action = actions.get(i);
4883 if (hasValidRemoteInput(action)) {
4884 return action;
4885 }
4886 }
4887 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004888 }
4889
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004890 private void bindNotificationHeader(RemoteViews contentView, StandardTemplateParams p) {
4891 bindSmallIcon(contentView, p);
4892 bindHeaderAppName(contentView, p);
Lucas Dupin00be88f2019-01-03 17:50:52 -08004893 bindHeaderText(contentView, p);
4894 bindHeaderTextSecondary(contentView, p);
4895 bindHeaderChronometerAndTime(contentView, p);
4896 bindProfileBadge(contentView, p);
4897 bindAlertedIcon(contentView, p);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004898 bindActivePermissions(contentView, p);
4899 bindExpandButton(contentView, p);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004900 mN.mUsesStandardHeader = true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004901 }
4902
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004903 private void bindActivePermissions(RemoteViews contentView, StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08004904 int color = getNeutralColor(p);
Julia Reynoldsb887b232018-04-10 16:38:08 -04004905 contentView.setDrawableTint(R.id.camera, false, color, PorterDuff.Mode.SRC_ATOP);
4906 contentView.setDrawableTint(R.id.mic, false, color, PorterDuff.Mode.SRC_ATOP);
4907 contentView.setDrawableTint(R.id.overlay, false, color, PorterDuff.Mode.SRC_ATOP);
4908 }
4909
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004910 private void bindExpandButton(RemoteViews contentView, StandardTemplateParams p) {
4911 int color = isColorized(p) ? getPrimaryTextColor(p) : getSecondaryTextColor(p);
Sunny Goyal5b153922017-09-21 21:00:36 -07004912 contentView.setDrawableTint(R.id.expand_button, false, color,
4913 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08004914 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08004915 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004916 }
4917
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004918 private void bindHeaderChronometerAndTime(RemoteViews contentView,
4919 StandardTemplateParams p) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004920 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08004921 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004922 setTextViewColorSecondary(contentView, R.id.time_divider, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004923 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
4924 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
4925 contentView.setLong(R.id.chronometer, "setBase",
4926 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
4927 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07004928 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07004929 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004930 setTextViewColorSecondary(contentView, R.id.chronometer, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004931 } else {
4932 contentView.setViewVisibility(R.id.time, View.VISIBLE);
4933 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004934 setTextViewColorSecondary(contentView, R.id.time, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004935 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004936 } else {
4937 // We still want a time to be set but gone, such that we can show and hide it
4938 // on demand in case it's a child notification without anything in the header
4939 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004940 }
4941 }
4942
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004943 private void bindHeaderText(RemoteViews contentView, StandardTemplateParams p) {
4944 CharSequence summaryText = p.summaryText;
4945 if (summaryText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05004946 && mStyle.hasSummaryInHeader()) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004947 summaryText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05004948 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004949 if (summaryText == null
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004950 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4951 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004952 summaryText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004953 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004954 if (summaryText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004955 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek48f66b72017-08-18 16:17:51 -07004956 contentView.setTextViewText(R.id.header_text, processTextSpans(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004957 processLegacyText(summaryText)));
4958 setTextViewColorSecondary(contentView, R.id.header_text, p);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004959 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
4960 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004961 setTextViewColorSecondary(contentView, R.id.header_text_divider, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004962 }
4963 }
4964
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004965 private void bindHeaderTextSecondary(RemoteViews contentView, StandardTemplateParams p) {
4966 if (!TextUtils.isEmpty(p.headerTextSecondary)) {
Selim Cinekafeed292017-12-12 17:32:44 -08004967 contentView.setTextViewText(R.id.header_text_secondary, processTextSpans(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004968 processLegacyText(p.headerTextSecondary)));
4969 setTextViewColorSecondary(contentView, R.id.header_text_secondary, p);
Selim Cinekafeed292017-12-12 17:32:44 -08004970 contentView.setViewVisibility(R.id.header_text_secondary, View.VISIBLE);
4971 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004972 setTextViewColorSecondary(contentView, R.id.header_text_secondary_divider, p);
Selim Cinekafeed292017-12-12 17:32:44 -08004973 }
4974 }
4975
Adrian Rooseba05822016-04-22 17:09:27 -07004976 /**
4977 * @hide
4978 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01004979 @UnsupportedAppUsage
Adrian Rooseba05822016-04-22 17:09:27 -07004980 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004981 CharSequence name = null;
4982 final PackageManager pm = mContext.getPackageManager();
4983 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
4984 // only system packages which lump together a bunch of unrelated stuff
4985 // may substitute a different name to make the purpose of the
4986 // notification more clear. the correct package label should always
4987 // be accessible via SystemUI.
4988 final String pkg = mContext.getPackageName();
4989 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
4990 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
4991 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
4992 name = subName;
4993 } else {
4994 Log.w(TAG, "warning: pkg "
4995 + pkg + " attempting to substitute app name '" + subName
4996 + "' without holding perm "
4997 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
4998 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004999 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04005000 if (TextUtils.isEmpty(name)) {
5001 name = pm.getApplicationLabel(mContext.getApplicationInfo());
5002 }
5003 if (TextUtils.isEmpty(name)) {
5004 // still nothing?
5005 return null;
5006 }
5007
5008 return String.valueOf(name);
5009 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005010 private void bindHeaderAppName(RemoteViews contentView, StandardTemplateParams p) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04005011 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005012 if (isColorized(p)) {
5013 setTextViewColorPrimary(contentView, R.id.app_name_text, p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005014 } else {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005015 contentView.setTextColor(R.id.app_name_text, getSecondaryTextColor(p));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005016 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005017 }
5018
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005019 private boolean isColorized(StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005020 return p.allowColorization && mN.isColorized();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005021 }
5022
5023 private void bindSmallIcon(RemoteViews contentView, StandardTemplateParams p) {
Selim Cinek279fa862016-06-14 10:57:25 -07005024 if (mN.mSmallIcon == null && mN.icon != 0) {
5025 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
5026 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005027 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Sunny Goyal5b153922017-09-21 21:00:36 -07005028 contentView.setInt(R.id.icon, "setImageLevel", mN.iconLevel);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005029 processSmallIconColor(mN.mSmallIcon, contentView, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005030 }
5031
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005032 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005033 * @return true if the built notification will show the time or the chronometer; false
5034 * otherwise
5035 */
5036 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07005037 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005038 }
5039
Christoph Studerfe718432014-09-01 18:21:18 +02005040 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07005041 // actions_container is only reset when there are no actions to avoid focus issues with
5042 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02005043 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02005044 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08005045
5046 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
5047 big.setTextViewText(R.id.notification_material_reply_text_1, null);
Kenny Guya0f6de82018-04-06 16:20:16 +01005048 big.setViewVisibility(R.id.notification_material_reply_text_1_container, View.GONE);
5049 big.setViewVisibility(R.id.notification_material_reply_progress, View.GONE);
Adrian Roose458aa82015-12-08 16:17:19 -08005050
5051 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
5052 big.setTextViewText(R.id.notification_material_reply_text_2, null);
5053 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
5054 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07005055
Selim Cineked64a142018-02-06 18:06:01 -08005056 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
5057 R.dimen.notification_content_margin);
Christoph Studerfe718432014-09-01 18:21:18 +02005058 }
5059
Selim Cinek384804b2018-04-18 14:31:07 +08005060 private RemoteViews applyStandardTemplateWithActions(int layoutId,
5061 TemplateBindResult result) {
5062 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this),
5063 result);
Adrian Roos48d746a2016-04-12 14:57:28 -07005064 }
5065
Gustav Sennton1463d832018-11-06 16:12:48 +00005066 private static List<Notification.Action> filterOutContextualActions(
5067 List<Notification.Action> actions) {
5068 List<Notification.Action> nonContextualActions = new ArrayList<>();
5069 for (Notification.Action action : actions) {
Gustav Sennton005d7a02019-01-04 13:41:32 +00005070 if (!action.isContextual()) {
Gustav Sennton1463d832018-11-06 16:12:48 +00005071 nonContextualActions.add(action);
5072 }
5073 }
5074 return nonContextualActions;
5075 }
5076
Adrian Roos70d7aa32017-01-11 15:39:06 -08005077 private RemoteViews applyStandardTemplateWithActions(int layoutId,
Selim Cinek384804b2018-04-18 14:31:07 +08005078 StandardTemplateParams p, TemplateBindResult result) {
5079 RemoteViews big = applyStandardTemplate(layoutId, p, result);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005080
Christoph Studerfe718432014-09-01 18:21:18 +02005081 resetStandardTemplateWithActions(big);
5082
Adrian Roose458aa82015-12-08 16:17:19 -08005083 boolean validRemoteInput = false;
5084
Gustav Sennton1463d832018-11-06 16:12:48 +00005085 // In the UI contextual actions appear separately from the standard actions, so we
5086 // filter them out here.
5087 List<Notification.Action> nonContextualActions = filterOutContextualActions(mActions);
5088
5089 int N = nonContextualActions.size();
Lucas Dupin00be88f2019-01-03 17:50:52 -08005090 boolean emphazisedMode = mN.fullScreenIntent != null;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005091 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005092 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08005093 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005094 big.setViewVisibility(R.id.actions, View.VISIBLE);
Selim Cineked64a142018-02-06 18:06:01 -08005095 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Daniel Sandler8680bf82012-05-15 16:52:52 -04005096 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005097 for (int i=0; i<N; i++) {
Gustav Sennton1463d832018-11-06 16:12:48 +00005098 Action action = nonContextualActions.get(i);
5099
Selim Cinekffcc7cf2018-02-06 17:43:51 -08005100 boolean actionHasValidInput = hasValidRemoteInput(action);
5101 validRemoteInput |= actionHasValidInput;
Adrian Roose458aa82015-12-08 16:17:19 -08005102
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005103 final RemoteViews button = generateActionButton(action, emphazisedMode, p);
Selim Cinek396caca2018-04-10 17:46:46 -07005104 if (actionHasValidInput && !emphazisedMode) {
Selim Cinekffcc7cf2018-02-06 17:43:51 -08005105 // Clear the drawable
5106 button.setInt(R.id.action0, "setBackgroundResource", 0);
5107 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005108 big.addView(R.id.actions, button);
5109 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07005110 } else {
5111 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005112 }
Adrian Roose458aa82015-12-08 16:17:19 -08005113
5114 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Lucas Dupin00be88f2019-01-03 17:50:52 -08005115 if (validRemoteInput && replyText != null
Selim Cinekbee4e072018-05-21 22:06:43 -07005116 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])
5117 && p.maxRemoteInputHistory > 0) {
Kenny Guya0f6de82018-04-06 16:20:16 +01005118 boolean showSpinner = mN.extras.getBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER);
Adrian Roose458aa82015-12-08 16:17:19 -08005119 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Kenny Guya0f6de82018-04-06 16:20:16 +01005120 big.setViewVisibility(R.id.notification_material_reply_text_1_container,
5121 View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005122 big.setTextViewText(R.id.notification_material_reply_text_1,
5123 processTextSpans(replyText[0]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005124 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1, p);
Kenny Guya0f6de82018-04-06 16:20:16 +01005125 big.setViewVisibility(R.id.notification_material_reply_progress,
5126 showSpinner ? View.VISIBLE : View.GONE);
5127 big.setProgressIndeterminateTintList(
5128 R.id.notification_material_reply_progress,
5129 ColorStateList.valueOf(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005130 isColorized(p) ? getPrimaryTextColor(p) : resolveContrastColor(p)));
Adrian Roose458aa82015-12-08 16:17:19 -08005131
Selim Cinekbee4e072018-05-21 22:06:43 -07005132 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])
5133 && p.maxRemoteInputHistory > 1) {
Adrian Roose458aa82015-12-08 16:17:19 -08005134 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005135 big.setTextViewText(R.id.notification_material_reply_text_2,
5136 processTextSpans(replyText[1]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005137 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2, p);
Adrian Roose458aa82015-12-08 16:17:19 -08005138
Selim Cinekbee4e072018-05-21 22:06:43 -07005139 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])
5140 && p.maxRemoteInputHistory > 2) {
Adrian Roose458aa82015-12-08 16:17:19 -08005141 big.setViewVisibility(
5142 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005143 big.setTextViewText(R.id.notification_material_reply_text_3,
5144 processTextSpans(replyText[2]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005145 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3, p);
Adrian Roose458aa82015-12-08 16:17:19 -08005146 }
5147 }
5148 }
5149
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005150 return big;
5151 }
5152
Adrian Roose458aa82015-12-08 16:17:19 -08005153 private boolean hasValidRemoteInput(Action action) {
5154 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
5155 // Weird actions
5156 return false;
5157 }
5158
5159 RemoteInput[] remoteInputs = action.getRemoteInputs();
5160 if (remoteInputs == null) {
5161 return false;
5162 }
5163
5164 for (RemoteInput r : remoteInputs) {
5165 CharSequence[] choices = r.getChoices();
5166 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
5167 return true;
5168 }
5169 }
5170 return false;
5171 }
5172
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005173 /**
5174 * Construct a RemoteViews for the final 1U notification layout. In order:
5175 * 1. Custom contentView from the caller
5176 * 2. Style's proposed content view
5177 * 3. Standard template view
5178 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005179 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005180 return createContentView(false /* increasedheight */ );
5181 }
5182
5183 /**
5184 * Construct a RemoteViews for the smaller content view.
5185 *
5186 * @param increasedHeight true if this layout be created with an increased height. Some
5187 * styles may support showing more then just that basic 1U size
5188 * and the system may decide to render important notifications
5189 * slightly bigger even when collapsed.
5190 *
5191 * @hide
5192 */
5193 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005194 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005195 return mN.contentView;
5196 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005197 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005198 if (styleView != null) {
5199 return styleView;
5200 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005201 }
Selim Cinek384804b2018-04-18 14:31:07 +08005202 return applyStandardTemplate(getBaseLayoutResource(), null /* result */);
Joe Onorato46439ce2010-11-19 13:56:21 -08005203 }
5204
Selim Cineka7679b62017-05-10 16:33:25 -07005205 private boolean useExistingRemoteView() {
5206 return mStyle == null || (!mStyle.displayCustomViewInline()
5207 && !mRebuildStyledRemoteViews);
5208 }
5209
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005210 /**
5211 * Construct a RemoteViews for the final big notification layout.
5212 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005213 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05005214 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07005215 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005216 return mN.bigContentView;
5217 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05005218 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08005219 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005220 } else if (mActions.size() != 0) {
Selim Cinek384804b2018-04-18 14:31:07 +08005221 result = applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5222 null /* result */);
Selim Cinek850a8542015-11-11 11:48:36 -05005223 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08005224 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05005225 return result;
5226 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005227
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005228 /**
Selim Cinek414ad332017-02-24 19:06:12 -08005229 * Construct a RemoteViews for the final notification header only. This will not be
5230 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005231 *
5232 * @hide
5233 */
Lucas Dupin00be88f2019-01-03 17:50:52 -08005234 public RemoteViews makeNotificationHeader() {
5235 return makeNotificationHeader(mParams.reset().fillTextsFrom(this));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005236 }
5237
5238 /**
5239 * Construct a RemoteViews for the final notification header only. This will not be
5240 * colorized.
5241 *
5242 * @param p the template params to inflate this with
5243 */
5244 private RemoteViews makeNotificationHeader(StandardTemplateParams p) {
5245 // Headers on their own are never colorized
5246 p.disallowColorization();
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005247 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Lucas Dupin00be88f2019-01-03 17:50:52 -08005248 R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005249 resetNotificationHeader(header);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005250 bindNotificationHeader(header, p);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005251 return header;
5252 }
5253
Adrian Roos487374f2017-01-11 15:48:14 -08005254 /**
5255 * Construct a RemoteViews for the ambient version of the notification.
5256 *
5257 * @hide
5258 */
5259 public RemoteViews makeAmbientNotification() {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005260 return createHeadsUpContentView(false /* increasedHeight */);
Adrian Roos487374f2017-01-11 15:48:14 -08005261 }
5262
Selim Cinek29603462015-11-17 19:04:39 -08005263 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005264 if (result != null) {
5265 result.setViewVisibility(R.id.text_line_1, View.GONE);
5266 }
Selim Cinek29603462015-11-17 19:04:39 -08005267 }
5268
Selim Cinek6743c0b2017-01-18 18:24:01 -08005269 /**
5270 * Adapt the Notification header if this view is used as an expanded view.
5271 *
5272 * @hide
5273 */
5274 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005275 if (result != null) {
5276 result.setBoolean(R.id.notification_header, "setExpanded", true);
5277 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005278 }
5279
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005280 /**
5281 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005282 *
5283 * @param increasedHeight true if this layout be created with an increased height. Some
5284 * styles may support showing more then just that basic 1U size
5285 * and the system may decide to render important notifications
5286 * slightly bigger even when collapsed.
5287 *
5288 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005289 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005290 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005291 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005292 return mN.headsUpContentView;
5293 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08005294 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
5295 if (styleView != null) {
5296 return styleView;
5297 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05005298 } else if (mActions.size() == 0) {
5299 return null;
5300 }
5301
Selim Cinekbee4e072018-05-21 22:06:43 -07005302 // We only want at most a single remote input history to be shown here, otherwise
5303 // the content would become squished.
5304 StandardTemplateParams p = mParams.reset().fillTextsFrom(this)
5305 .setMaxRemoteInputHistory(1);
5306 return applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5307 p,
5308 null /* result */);
Chris Wren8fd39ec2014-02-27 17:43:26 -05005309 }
5310
Selim Cinek624c02db2015-12-14 21:00:02 -08005311 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08005312 * Construct a RemoteViews for the final heads-up notification layout.
5313 */
5314 public RemoteViews createHeadsUpContentView() {
5315 return createHeadsUpContentView(false /* useIncreasedHeight */);
5316 }
5317
5318 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08005319 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5320 *
5321 * @hide
5322 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005323 @UnsupportedAppUsage
Selim Cinek624c02db2015-12-14 21:00:02 -08005324 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005325 return makePublicView(false /* ambient */);
5326 }
5327
5328 /**
5329 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5330 *
5331 * @hide
5332 */
5333 public RemoteViews makePublicAmbientNotification() {
5334 return makePublicView(true /* ambient */);
5335 }
5336
5337 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08005338 if (mN.publicVersion != null) {
5339 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005340 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08005341 }
5342 Bundle savedBundle = mN.extras;
5343 Style style = mStyle;
5344 mStyle = null;
5345 Icon largeIcon = mN.mLargeIcon;
5346 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07005347 Bitmap largeIconLegacy = mN.largeIcon;
5348 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005349 ArrayList<Action> actions = mActions;
5350 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08005351 Bundle publicExtras = new Bundle();
5352 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
5353 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
5354 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
5355 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07005356 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
5357 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cineked07b962018-04-30 14:39:35 -07005358 String appName = savedBundle.getString(EXTRA_SUBSTITUTE_APP_NAME);
5359 if (appName != null) {
5360 publicExtras.putString(EXTRA_SUBSTITUTE_APP_NAME, appName);
5361 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005362 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07005363 RemoteViews view;
Lucas Dupin00be88f2019-01-03 17:50:52 -08005364 view = makeNotificationHeader();
5365 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
Selim Cinek624c02db2015-12-14 21:00:02 -08005366 mN.extras = savedBundle;
5367 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07005368 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005369 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08005370 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005371 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08005372 }
5373
Selim Cinek6743c0b2017-01-18 18:24:01 -08005374 /**
5375 * Construct a content view for the display when low - priority
5376 *
5377 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
5378 * a new subtext is created consisting of the content of the
5379 * notification.
5380 * @hide
5381 */
5382 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005383 StandardTemplateParams p = mParams.reset()
5384 .forceDefaultColor()
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005385 .fillTextsFrom(this);
5386 if (!useRegularSubtext || TextUtils.isEmpty(mParams.summaryText)) {
5387 p.summaryText(createSummaryText());
Selim Cinek6743c0b2017-01-18 18:24:01 -08005388 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005389 RemoteViews header = makeNotificationHeader(p);
Selim Cinek1b554392017-02-28 17:22:49 -08005390 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08005391 return header;
5392 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005393
Selim Cinek6743c0b2017-01-18 18:24:01 -08005394 private CharSequence createSummaryText() {
5395 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
5396 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
5397 return titleText;
5398 }
5399 SpannableStringBuilder summary = new SpannableStringBuilder();
5400 if (titleText == null) {
5401 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
5402 }
5403 BidiFormatter bidi = BidiFormatter.getInstance();
5404 if (titleText != null) {
5405 summary.append(bidi.unicodeWrap(titleText));
5406 }
5407 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
5408 if (titleText != null && contentText != null) {
5409 summary.append(bidi.unicodeWrap(mContext.getText(
5410 R.string.notification_header_divider_symbol_with_spaces)));
5411 }
5412 if (contentText != null) {
5413 summary.append(bidi.unicodeWrap(contentText));
5414 }
5415 return summary;
5416 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05005417
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005418 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005419 StandardTemplateParams p) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04005420 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07005421 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005422 emphazisedMode ? getEmphasizedActionLayoutResource()
5423 : tombstone ? getActionTombstoneLayoutResource()
5424 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04005425 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04005426 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04005427 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005428 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005429 if (action.mRemoteInputs != null) {
5430 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
5431 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005432 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07005433 // change the background bgColor
Selim Cinek981962e2016-07-20 20:41:58 -07005434 CharSequence title = action.title;
5435 ColorStateList[] outResultColor = null;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005436 int background = resolveBackgroundColor(p);
Selim Cinek981962e2016-07-20 20:41:58 -07005437 if (isLegacy()) {
Lucas Dupina291d192018-06-07 13:59:42 -07005438 title = ContrastColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07005439 } else {
5440 outResultColor = new ColorStateList[1];
Selim Cinek396caca2018-04-10 17:46:46 -07005441 title = ensureColorSpanContrast(title, background, outResultColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005442 }
Selim Cinek48f66b72017-08-18 16:17:51 -07005443 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005444 setTextViewColorPrimary(button, R.id.action0, p);
Selim Cinek396caca2018-04-10 17:46:46 -07005445 int rippleColor;
5446 boolean hasColorOverride = outResultColor != null && outResultColor[0] != null;
5447 if (hasColorOverride) {
5448 // There's a span spanning the full text, let's take it and use it as the
5449 // background color
5450 background = outResultColor[0].getDefaultColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005451 int textColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07005452 background, mInNightMode);
Selim Cinek396caca2018-04-10 17:46:46 -07005453 button.setTextColor(R.id.action0, textColor);
5454 rippleColor = textColor;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005455 } else if (getRawColor(p) != COLOR_DEFAULT && !isColorized(p)
5456 && mTintActionButtons) {
5457 rippleColor = resolveContrastColor(p);
Selim Cinek396caca2018-04-10 17:46:46 -07005458 button.setTextColor(R.id.action0, rippleColor);
5459 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005460 rippleColor = getPrimaryTextColor(p);
Selim Cinek981962e2016-07-20 20:41:58 -07005461 }
Selim Cinek396caca2018-04-10 17:46:46 -07005462 // We only want about 20% alpha for the ripple
5463 rippleColor = (rippleColor & 0x00ffffff) | 0x33000000;
5464 button.setColorStateList(R.id.action0, "setRippleColor",
5465 ColorStateList.valueOf(rippleColor));
5466 button.setColorStateList(R.id.action0, "setButtonBackground",
5467 ColorStateList.valueOf(background));
5468 button.setBoolean(R.id.action0, "setHasStroke", !hasColorOverride);
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005469 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07005470 button.setTextViewText(R.id.action0, processTextSpans(
5471 processLegacyText(action.title)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005472 if (isColorized(p)) {
5473 setTextViewColorPrimary(button, R.id.action0, p);
5474 } else if (getRawColor(p) != COLOR_DEFAULT && mTintActionButtons) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005475 button.setTextColor(R.id.action0, resolveContrastColor(p));
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005476 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005477 }
Tony Mak7d4b3a52018-11-27 17:29:36 +00005478 button.setIntTag(R.id.action0, R.id.notification_action_index_tag,
5479 mActions.indexOf(action));
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005480 return button;
5481 }
5482
Joe Onoratocb109a02011-01-18 17:57:41 -08005483 /**
Selim Cinek981962e2016-07-20 20:41:58 -07005484 * Ensures contrast on color spans against a background color. also returns the color of the
5485 * text if a span was found that spans over the whole text.
5486 *
5487 * @param charSequence the charSequence on which the spans are
5488 * @param background the background color to ensure the contrast against
5489 * @param outResultColor an array in which a color will be returned as the first element if
5490 * there exists a full length color span.
5491 * @return the contrasted charSequence
5492 */
5493 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
5494 ColorStateList[] outResultColor) {
5495 if (charSequence instanceof Spanned) {
5496 Spanned ss = (Spanned) charSequence;
5497 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
5498 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
5499 for (Object span : spans) {
5500 Object resultSpan = span;
5501 int spanStart = ss.getSpanStart(span);
5502 int spanEnd = ss.getSpanEnd(span);
5503 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
5504 if (resultSpan instanceof CharacterStyle) {
5505 resultSpan = ((CharacterStyle) span).getUnderlying();
5506 }
5507 if (resultSpan instanceof TextAppearanceSpan) {
5508 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
5509 ColorStateList textColor = originalSpan.getTextColor();
5510 if (textColor != null) {
5511 int[] colors = textColor.getColors();
5512 int[] newColors = new int[colors.length];
5513 for (int i = 0; i < newColors.length; i++) {
Lucas Dupina291d192018-06-07 13:59:42 -07005514 newColors[i] = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005515 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005516 }
5517 textColor = new ColorStateList(textColor.getStates().clone(),
5518 newColors);
Selim Cinek396caca2018-04-10 17:46:46 -07005519 if (fullLength) {
5520 outResultColor[0] = textColor;
5521 // Let's drop the color from the span
5522 textColor = null;
5523 }
Selim Cinek981962e2016-07-20 20:41:58 -07005524 resultSpan = new TextAppearanceSpan(
5525 originalSpan.getFamily(),
5526 originalSpan.getTextStyle(),
5527 originalSpan.getTextSize(),
5528 textColor,
5529 originalSpan.getLinkTextColor());
Selim Cinek981962e2016-07-20 20:41:58 -07005530 }
5531 } else if (resultSpan instanceof ForegroundColorSpan) {
5532 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
5533 int foregroundColor = originalSpan.getForegroundColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005534 foregroundColor = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005535 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005536 if (fullLength) {
5537 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
Selim Cinek396caca2018-04-10 17:46:46 -07005538 resultSpan = null;
5539 } else {
5540 resultSpan = new ForegroundColorSpan(foregroundColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005541 }
5542 } else {
5543 resultSpan = span;
5544 }
Selim Cinek396caca2018-04-10 17:46:46 -07005545 if (resultSpan != null) {
5546 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
5547 }
Selim Cinek981962e2016-07-20 20:41:58 -07005548 }
5549 return builder;
5550 }
5551 return charSequence;
5552 }
5553
5554 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005555 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07005556 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005557 */
5558 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005559 if (!mIsLegacyInitialized) {
5560 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
5561 < Build.VERSION_CODES.LOLLIPOP;
5562 mIsLegacyInitialized = true;
5563 }
5564 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005565 }
5566
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005567 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08005568 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
Lucas Dupin00be88f2019-01-03 17:50:52 -08005569 if (isAlreadyLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005570 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005571 } else {
5572 return charSequence;
5573 }
5574 }
5575
Dan Sandler26e81cf2014-05-06 10:01:27 -04005576 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005577 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04005578 */
Adrian Roos487374f2017-01-11 15:48:14 -08005579 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005580 StandardTemplateParams p) {
Selim Cinekea4bef72015-12-02 15:51:10 -08005581 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005582 int color;
Lucas Dupin00be88f2019-01-03 17:50:52 -08005583 if (isColorized(p)) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005584 color = getPrimaryTextColor(p);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005585 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005586 color = resolveContrastColor(p);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005587 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005588 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07005589 contentView.setDrawableTint(R.id.icon, false, color,
5590 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005591
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005592 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005593 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08005594 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005595 }
5596
Dan Sandler26e81cf2014-05-06 10:01:27 -04005597 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005598 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04005599 * if it's grayscale).
5600 */
5601 // TODO: also check bounds, transparency, that sort of thing.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005602 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView,
5603 StandardTemplateParams p) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04005604 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005605 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005606 // resolve color will fall back to the default when legacy
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005607 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(p),
Sunny Goyal5b153922017-09-21 21:00:36 -07005608 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01005609 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005610 }
5611
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05005612 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005613 if (mN.color != COLOR_DEFAULT) {
5614 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02005615 }
Jorim Jaggi74419312014-06-10 20:57:21 +02005616 }
5617
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005618 int resolveContrastColor(StandardTemplateParams p) {
5619 int rawColor = getRawColor(p);
5620 if (mCachedContrastColorIsFor == rawColor && mCachedContrastColor != COLOR_INVALID) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08005621 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005622 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005623
Selim Cinekac5f0272017-05-02 16:05:41 -07005624 int color;
Selim Cinekc7f5a822018-03-20 19:32:06 -07005625 int background = mContext.getColor(
5626 com.android.internal.R.color.notification_material_background_color);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005627 if (rawColor == COLOR_DEFAULT) {
5628 ensureColors(p);
Lucas Dupinf03e7522018-06-25 16:21:13 -07005629 color = ContrastColorUtil.resolveDefaultColor(mContext, background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005630 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005631 color = ContrastColorUtil.resolveContrastColor(mContext, rawColor,
Anthony Chenad4d1582017-04-10 16:07:58 -07005632 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005633 }
5634 if (Color.alpha(color) < 255) {
5635 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005636 color = ContrastColorUtil.compositeColors(color, background);
Selim Cinekac5f0272017-05-02 16:05:41 -07005637 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005638 mCachedContrastColorIsFor = rawColor;
Selim Cinekac5f0272017-05-02 16:05:41 -07005639 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005640 }
5641
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005642 /**
5643 * Return the raw color of this Notification, which doesn't necessarily satisfy contrast.
5644 *
5645 * @see #resolveContrastColor(StandardTemplateParams) for the contrasted color
5646 * @param p the template params to inflate this with
5647 */
5648 private int getRawColor(StandardTemplateParams p) {
5649 if (p.forceDefaultColor) {
5650 return COLOR_DEFAULT;
5651 }
5652 return mN.color;
5653 }
5654
Selim Cinek4717d862018-04-19 09:19:15 +08005655 int resolveNeutralColor() {
5656 if (mNeutralColor != COLOR_INVALID) {
5657 return mNeutralColor;
5658 }
5659 int background = mContext.getColor(
5660 com.android.internal.R.color.notification_material_background_color);
Lucas Dupinf03e7522018-06-25 16:21:13 -07005661 mNeutralColor = ContrastColorUtil.resolveDefaultColor(mContext, background,
5662 mInNightMode);
Selim Cinek4717d862018-04-19 09:19:15 +08005663 if (Color.alpha(mNeutralColor) < 255) {
5664 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005665 mNeutralColor = ContrastColorUtil.compositeColors(mNeutralColor, background);
Selim Cinek4717d862018-04-19 09:19:15 +08005666 }
5667 return mNeutralColor;
5668 }
5669
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005670 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005671 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005672 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08005673 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005674 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005675 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005676 mN.actions = new Action[mActions.size()];
5677 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005678 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005679 if (!mPersonList.isEmpty()) {
Selim Cineke7238dd2017-12-14 17:48:32 -08005680 mN.extras.putParcelableArrayList(EXTRA_PEOPLE_LIST, mPersonList);
Dan Sandler0bf2ed82013-12-21 23:33:41 -06005681 }
Selim Cinek247fa012016-02-18 09:50:48 -08005682 if (mN.bigContentView != null || mN.contentView != null
5683 || mN.headsUpContentView != null) {
5684 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
5685 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005686 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08005687 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005688
Julia Reynolds3b848122016-02-26 10:45:32 -05005689 /**
5690 * Creates a Builder from an existing notification so further changes can be made.
5691 * @param context The context for your application / activity.
5692 * @param n The notification to create a Builder from.
5693 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005694 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005695 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005696 ApplicationInfo applicationInfo = n.extras.getParcelable(
5697 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005698 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05005699 if (applicationInfo != null) {
5700 try {
5701 builderContext = context.createApplicationContext(applicationInfo,
5702 Context.CONTEXT_RESTRICTED);
5703 } catch (NameNotFoundException e) {
5704 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
5705 builderContext = context; // try with our context
5706 }
5707 } else {
5708 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02005709 }
5710
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005711 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005712 }
5713
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005714 /**
Gustav Sennton761884c2018-11-19 17:40:19 +00005715 * Determines whether the platform can generate contextual actions for a notification.
5716 * By default this is true.
5717 */
5718 public Builder setAllowSystemGeneratedContextualActions(boolean allowed) {
5719 mN.mAllowSystemGeneratedContextualActions = allowed;
5720 return this;
5721 }
5722
5723 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005724 * @deprecated Use {@link #build()} instead.
5725 */
5726 @Deprecated
5727 public Notification getNotification() {
5728 return build();
5729 }
5730
5731 /**
5732 * Combine all of the options that have been set and return a new {@link Notification}
5733 * object.
5734 */
5735 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005736 // first, add any extras from the calling code
5737 if (mUserExtras != null) {
5738 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005739 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005740
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005741 mN.creationTime = System.currentTimeMillis();
5742
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005743 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05005744 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02005745
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005746 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005747
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005748 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02005749 mStyle.reduceImageSizes(mContext);
5750 mStyle.purgeResources();
Selim Cinek90343862018-02-01 11:07:11 -08005751 mStyle.validate(mContext);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005752 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005753 }
Selim Cinekd0426622017-07-11 13:19:59 +02005754 mN.reduceImageSizes(mContext);
5755
Adrian Roos5081c0d2016-02-26 16:04:19 -08005756 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07005757 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005758 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005759 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005760 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
5761 mN.contentView.getSequenceNumber());
5762 }
5763 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005764 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005765 if (mN.bigContentView != null) {
5766 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
5767 mN.bigContentView.getSequenceNumber());
5768 }
5769 }
5770 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005771 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005772 if (mN.headsUpContentView != null) {
5773 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
5774 mN.headsUpContentView.getSequenceNumber());
5775 }
5776 }
5777 }
5778
Julia Reynolds4c0c2022016-02-02 15:11:59 -05005779 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
5780 mN.flags |= FLAG_SHOW_LIGHTS;
5781 }
5782
Adrian Roosfb921842017-10-26 14:49:56 +02005783 mN.allPendingIntents = null;
5784
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005785 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005786 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005787
5788 /**
5789 * Apply this Builder to an existing {@link Notification} object.
5790 *
5791 * @hide
5792 */
5793 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005794 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005795 return n;
5796 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005797
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005798 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005799 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005800 * change. Also removes extenders on low ram devices, as
5801 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005802 *
5803 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5804 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005805 * @hide
5806 */
Kristian Monsen05f34792018-04-09 10:27:16 +02005807 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam,
5808 Context context) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005809 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005810
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005811 // Only strip views for known Styles because we won't know how to
5812 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005813 if (!isLowRam
5814 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005815 && getNotificationStyleClass(templateClass) == null) {
5816 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005817 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005818
5819 // Only strip unmodified BuilderRemoteViews.
5820 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005821 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005822 n.contentView.getSequenceNumber();
5823 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005824 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005825 n.bigContentView.getSequenceNumber();
5826 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005827 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005828 n.headsUpContentView.getSequenceNumber();
5829
5830 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005831 if (!isLowRam
5832 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005833 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005834 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005835
5836 Notification clone = n.clone();
5837 if (stripContentView) {
5838 clone.contentView = null;
5839 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5840 }
5841 if (stripBigContentView) {
5842 clone.bigContentView = null;
5843 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5844 }
5845 if (stripHeadsUpContentView) {
5846 clone.headsUpContentView = null;
5847 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5848 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005849 if (isLowRam) {
Kristian Monsen05f34792018-04-09 10:27:16 +02005850 String[] allowedServices = context.getResources().getStringArray(
5851 R.array.config_allowedManagedServicesOnLowRamDevices);
5852 if (allowedServices.length == 0) {
5853 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5854 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5855 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5856 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005857 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005858 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005859 }
5860
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005861 @UnsupportedAppUsage
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005862 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005863 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005864 }
5865
5866 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005867 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005868 }
5869
5870 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005871 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005872 }
5873
5874 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005875 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005876 }
5877
5878 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005879 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005880 }
5881
Adrian Roosc1a80b02016-04-05 14:54:55 -07005882 private int getMessagingLayoutResource() {
5883 return R.layout.notification_template_material_messaging;
5884 }
5885
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005886 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005887 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005888 }
5889
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005890 private int getEmphasizedActionLayoutResource() {
5891 return R.layout.notification_material_action_emphasized;
5892 }
5893
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005894 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005895 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005896 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005897
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005898 private int getBackgroundColor(StandardTemplateParams p) {
5899 if (isColorized(p)) {
5900 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : getRawColor(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005901 } else {
Selim Cinekc7f5a822018-03-20 19:32:06 -07005902 return COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005903 }
5904 }
5905
Selim Cinek396caca2018-04-10 17:46:46 -07005906 /**
Selim Cinek4717d862018-04-19 09:19:15 +08005907 * Gets a neutral color that can be used for icons or similar that should not stand out.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005908 * @param p the template params to inflate this with
Selim Cinek4717d862018-04-19 09:19:15 +08005909 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005910 private int getNeutralColor(StandardTemplateParams p) {
5911 if (isColorized(p)) {
5912 return getSecondaryTextColor(p);
Selim Cinek4717d862018-04-19 09:19:15 +08005913 } else {
5914 return resolveNeutralColor();
5915 }
5916 }
5917
5918 /**
Selim Cinek396caca2018-04-10 17:46:46 -07005919 * Same as getBackgroundColor but also resolved the default color to the background.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005920 * @param p the template params to inflate this with
Selim Cinek396caca2018-04-10 17:46:46 -07005921 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005922 private int resolveBackgroundColor(StandardTemplateParams p) {
5923 int backgroundColor = getBackgroundColor(p);
Selim Cinek396caca2018-04-10 17:46:46 -07005924 if (backgroundColor == COLOR_DEFAULT) {
5925 backgroundColor = mContext.getColor(
5926 com.android.internal.R.color.notification_material_background_color);
5927 }
5928 return backgroundColor;
5929 }
5930
Anthony Chenad4d1582017-04-10 16:07:58 -07005931 private boolean shouldTintActionButtons() {
5932 return mTintActionButtons;
5933 }
5934
Selim Cinek99104832017-01-25 14:47:33 -08005935 private boolean textColorsNeedInversion() {
5936 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
5937 return false;
5938 }
5939 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
5940 return targetSdkVersion > Build.VERSION_CODES.M
5941 && targetSdkVersion < Build.VERSION_CODES.O;
5942 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005943
5944 /**
5945 * Set a color palette to be used as the background and textColors
5946 *
5947 * @param backgroundColor the color to be used as the background
5948 * @param foregroundColor the color to be used as the foreground
5949 *
5950 * @hide
5951 */
5952 public void setColorPalette(int backgroundColor, int foregroundColor) {
5953 mBackgroundColor = backgroundColor;
5954 mForegroundColor = foregroundColor;
5955 mTextColorsAreForBackground = COLOR_INVALID;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005956 ensureColors(mParams.reset().fillTextsFrom(this));
Selim Cinek5fb73f82017-04-20 16:55:38 -07005957 }
Selim Cinekac5f0272017-05-02 16:05:41 -07005958
5959 /**
Selim Cineka7679b62017-05-10 16:33:25 -07005960 * Forces all styled remoteViews to be built from scratch and not use any cached
5961 * RemoteViews.
5962 * This is needed for legacy apps that are baking in their remoteviews into the
5963 * notification.
5964 *
5965 * @hide
5966 */
5967 public void setRebuildStyledRemoteViews(boolean rebuild) {
5968 mRebuildStyledRemoteViews = rebuild;
5969 }
Selim Cinekaa9db1f2018-02-27 17:35:47 -08005970
5971 /**
5972 * Get the text that should be displayed in the statusBar when heads upped. This is
5973 * usually just the app name, but may be different depending on the style.
5974 *
5975 * @param publicMode If true, return a text that is safe to display in public.
5976 *
5977 * @hide
5978 */
5979 public CharSequence getHeadsUpStatusBarText(boolean publicMode) {
5980 if (mStyle != null && !publicMode) {
5981 CharSequence text = mStyle.getHeadsUpStatusBarText();
5982 if (!TextUtils.isEmpty(text)) {
5983 return text;
5984 }
5985 }
5986 return loadHeaderAppName();
5987 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005988 }
5989
5990 /**
Selim Cinekd0426622017-07-11 13:19:59 +02005991 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
5992 * remote views.
5993 *
5994 * @hide
5995 */
5996 void reduceImageSizes(Context context) {
5997 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
5998 return;
5999 }
Selim Cineka8cb1262017-08-15 16:53:44 -07006000 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006001 if (mLargeIcon != null || largeIcon != null) {
6002 Resources resources = context.getResources();
6003 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07006004 int maxWidth = resources.getDimensionPixelSize(isLowRam
6005 ? R.dimen.notification_right_icon_size_low_ram
6006 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006007 int maxHeight = maxWidth;
6008 if (MediaStyle.class.equals(style)
6009 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006010 maxHeight = resources.getDimensionPixelSize(isLowRam
6011 ? R.dimen.notification_media_image_max_height_low_ram
6012 : R.dimen.notification_media_image_max_height);
6013 maxWidth = resources.getDimensionPixelSize(isLowRam
6014 ? R.dimen.notification_media_image_max_width_low_ram
6015 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006016 }
6017 if (mLargeIcon != null) {
6018 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
6019 }
6020 if (largeIcon != null) {
6021 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
6022 }
6023 }
Selim Cineka8cb1262017-08-15 16:53:44 -07006024 reduceImageSizesForRemoteView(contentView, context, isLowRam);
6025 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
6026 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02006027 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
6028 }
6029
Selim Cineka8cb1262017-08-15 16:53:44 -07006030 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
6031 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02006032 if (remoteView != null) {
6033 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006034 int maxWidth = resources.getDimensionPixelSize(isLowRam
6035 ? R.dimen.notification_custom_view_max_image_width_low_ram
6036 : R.dimen.notification_custom_view_max_image_width);
6037 int maxHeight = resources.getDimensionPixelSize(isLowRam
6038 ? R.dimen.notification_custom_view_max_image_height_low_ram
6039 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02006040 remoteView.reduceImageSizes(maxWidth, maxHeight);
6041 }
6042 }
6043
6044 /**
Selim Cinek22714f12017-04-13 16:23:53 -07006045 * @return whether this notification is a foreground service notification
Gus Prevasb0c1a462018-11-05 11:06:15 -05006046 * @hide
Selim Cinek7b9605b2017-01-19 17:36:00 -08006047 */
Gus Prevasb0c1a462018-11-05 11:06:15 -05006048 public boolean isForegroundService() {
Selim Cinek22714f12017-04-13 16:23:53 -07006049 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08006050 }
6051
6052 /**
Selim Cinek99104832017-01-25 14:47:33 -08006053 * @return whether this notification has a media session attached
6054 * @hide
6055 */
6056 public boolean hasMediaSession() {
6057 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
6058 }
6059
6060 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006061 * @return the style class of this notification
6062 * @hide
6063 */
6064 public Class<? extends Notification.Style> getNotificationStyle() {
6065 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
6066
6067 if (!TextUtils.isEmpty(templateClass)) {
6068 return Notification.getNotificationStyleClass(templateClass);
6069 }
6070 return null;
6071 }
6072
6073 /**
Selim Cinek22714f12017-04-13 16:23:53 -07006074 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08006075 *
6076 * @hide
6077 */
6078 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07006079 if (isColorizedMedia()) {
6080 return true;
6081 }
Julia Reynolds4db59552017-06-30 13:34:01 -04006082 return extras.getBoolean(EXTRA_COLORIZED)
6083 && (hasColorizedPermission() || isForegroundService());
6084 }
6085
6086 /**
6087 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
6088 * permission. The permission is checked when a notification is enqueued.
6089 */
6090 private boolean hasColorizedPermission() {
6091 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07006092 }
6093
6094 /**
6095 * @return true if this notification is colorized and it is a media notification
6096 *
6097 * @hide
6098 */
6099 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08006100 Class<? extends Style> style = getNotificationStyle();
6101 if (MediaStyle.class.equals(style)) {
6102 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
6103 if ((colorized == null || colorized) && hasMediaSession()) {
6104 return true;
6105 }
6106 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6107 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
6108 return true;
6109 }
6110 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07006111 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006112 }
6113
Selim Cinek0847acd2017-04-24 19:48:29 -07006114
6115 /**
6116 * @return true if this is a media notification
6117 *
6118 * @hide
6119 */
6120 public boolean isMediaNotification() {
6121 Class<? extends Style> style = getNotificationStyle();
6122 if (MediaStyle.class.equals(style)) {
6123 return true;
6124 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6125 return true;
6126 }
6127 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006128 }
6129
Selim Cinek279fa862016-06-14 10:57:25 -07006130 private boolean hasLargeIcon() {
6131 return mLargeIcon != null || largeIcon != null;
6132 }
6133
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006134 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006135 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006136 * @hide
6137 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07006138 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006139 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
6140 }
6141
6142 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006143 * @return true if the notification will show a chronometer; false otherwise
6144 * @hide
6145 */
6146 public boolean showsChronometer() {
6147 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
6148 }
6149
6150 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04006151 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05006152 */
6153 @SystemApi
6154 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
6155 Class<? extends Style>[] classes = new Class[] {
6156 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
6157 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
6158 MessagingStyle.class };
6159 for (Class<? extends Style> innerClass : classes) {
6160 if (templateClass.equals(innerClass.getName())) {
6161 return innerClass;
6162 }
6163 }
6164 return null;
6165 }
6166
6167 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006168 * An object that can apply a rich notification style to a {@link Notification.Builder}
6169 * object.
6170 */
Griff Hazendfcb0802014-02-11 12:00:00 -08006171 public static abstract class Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07006172
6173 /**
6174 * The number of items allowed simulatanously in the remote input history.
6175 * @hide
6176 */
6177 static final int MAX_REMOTE_INPUT_HISTORY_LINES = 3;
Chris Wrend6297db2012-05-03 16:20:13 -04006178 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006179
6180 /**
6181 * @hide
6182 */
6183 protected CharSequence mSummaryText = null;
6184
6185 /**
6186 * @hide
6187 */
6188 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04006189
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006190 protected Builder mBuilder;
6191
Chris Wrend6297db2012-05-03 16:20:13 -04006192 /**
6193 * Overrides ContentTitle in the big form of the template.
6194 * This defaults to the value passed to setContentTitle().
6195 */
6196 protected void internalSetBigContentTitle(CharSequence title) {
6197 mBigContentTitle = title;
6198 }
6199
6200 /**
6201 * Set the first line of text after the detail section in the big form of the template.
6202 */
6203 protected void internalSetSummaryText(CharSequence cs) {
6204 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04006205 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04006206 }
6207
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006208 public void setBuilder(Builder builder) {
6209 if (mBuilder != builder) {
6210 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07006211 if (mBuilder != null) {
6212 mBuilder.setStyle(this);
6213 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006214 }
6215 }
6216
Chris Wrend6297db2012-05-03 16:20:13 -04006217 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006218 if (mBuilder == null) {
6219 throw new IllegalArgumentException("Style requires a valid Builder object");
6220 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006221 }
Chris Wrend6297db2012-05-03 16:20:13 -04006222
6223 protected RemoteViews getStandardView(int layoutId) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006224 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder);
6225 return getStandardView(layoutId, p, null);
Selim Cinek384804b2018-04-18 14:31:07 +08006226 }
6227
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006228
Selim Cinek384804b2018-04-18 14:31:07 +08006229 /**
6230 * Get the standard view for this style.
6231 *
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006232 * @param layoutId The layout id to use.
6233 * @param p the params for this inflation.
Selim Cinek384804b2018-04-18 14:31:07 +08006234 * @param result The result where template bind information is saved.
6235 * @return A remoteView for this style.
6236 * @hide
6237 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006238 protected RemoteViews getStandardView(int layoutId, StandardTemplateParams p,
6239 TemplateBindResult result) {
Chris Wrend6297db2012-05-03 16:20:13 -04006240 checkBuilder();
6241
6242 if (mBigContentTitle != null) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006243 p.title = mBigContentTitle;
Chris Wrend6297db2012-05-03 16:20:13 -04006244 }
6245
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006246 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId, p,
6247 result);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006248
Chris Wrend6297db2012-05-03 16:20:13 -04006249 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
6250 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04006251 } else {
6252 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04006253 }
6254
Chris Wrend6297db2012-05-03 16:20:13 -04006255 return contentView;
6256 }
6257
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006258 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006259 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006260 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08006261 *
6262 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006263 * @hide
6264 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08006265 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006266 return null;
6267 }
6268
6269 /**
6270 * Construct a Style-specific RemoteViews for the final big notification layout.
6271 * @hide
6272 */
6273 public RemoteViews makeBigContentView() {
6274 return null;
6275 }
6276
6277 /**
6278 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08006279 *
6280 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006281 * @hide
6282 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006283 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006284 return null;
6285 }
6286
6287 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006288 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006289 * @hide
6290 */
6291 public void addExtras(Bundle extras) {
6292 if (mSummaryTextSet) {
6293 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
6294 }
6295 if (mBigContentTitle != null) {
6296 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
6297 }
Chris Wren91ad5632013-06-05 15:05:57 -04006298 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006299 }
6300
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006301 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006302 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006303 * @hide
6304 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02006305 protected void restoreFromExtras(Bundle extras) {
6306 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
6307 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
6308 mSummaryTextSet = true;
6309 }
6310 if (extras.containsKey(EXTRA_TITLE_BIG)) {
6311 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
6312 }
6313 }
6314
6315
6316 /**
6317 * @hide
6318 */
6319 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006320 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006321 return wip;
6322 }
6323
Daniel Sandler0ec46202015-06-24 01:27:05 -04006324 /**
6325 * @hide
6326 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006327 public void purgeResources() {}
6328
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006329 /**
6330 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
6331 * attached to.
6332 *
6333 * @return the fully constructed Notification.
6334 */
6335 public Notification build() {
6336 checkBuilder();
6337 return mBuilder.build();
6338 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006339
6340 /**
6341 * @hide
6342 * @return true if the style positions the progress bar on the second line; false if the
6343 * style hides the progress bar
6344 */
6345 protected boolean hasProgress() {
6346 return true;
6347 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006348
6349 /**
6350 * @hide
6351 * @return Whether we should put the summary be put into the notification header
6352 */
6353 public boolean hasSummaryInHeader() {
6354 return true;
6355 }
Selim Cinek593610c2016-02-16 18:42:57 -08006356
6357 /**
6358 * @hide
6359 * @return Whether custom content views are displayed inline in the style
6360 */
6361 public boolean displayCustomViewInline() {
6362 return false;
6363 }
Selim Cinekd0426622017-07-11 13:19:59 +02006364
6365 /**
6366 * Reduces the image sizes contained in this style.
6367 *
6368 * @hide
6369 */
6370 public void reduceImageSizes(Context context) {
6371 }
Selim Cinek90343862018-02-01 11:07:11 -08006372
6373 /**
6374 * Validate that this style was properly composed. This is called at build time.
6375 * @hide
6376 */
6377 public void validate(Context context) {
6378 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006379
6380 /**
6381 * @hide
6382 */
6383 public abstract boolean areNotificationsVisiblyDifferent(Style other);
Selim Cinekc7f5a822018-03-20 19:32:06 -07006384
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006385 /**
koprivaa1a78482018-10-09 10:09:23 -07006386 * @return the text that should be displayed in the statusBar when heads-upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006387 * If {@code null} is returned, the default implementation will be used.
6388 *
6389 * @hide
6390 */
6391 public CharSequence getHeadsUpStatusBarText() {
6392 return null;
6393 }
Joe Onorato46439ce2010-11-19 13:56:21 -08006394 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006395
6396 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006397 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08006398 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006399 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006400 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006401 * Notification notif = new Notification.Builder(mContext)
6402 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
6403 * .setContentText(subject)
6404 * .setSmallIcon(R.drawable.new_post)
6405 * .setLargeIcon(aBitmap)
6406 * .setStyle(new Notification.BigPictureStyle()
6407 * .bigPicture(aBigBitmap))
6408 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006409 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006410 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006411 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006412 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006413 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006414 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006415 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006416 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006417
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006418 public BigPictureStyle() {
6419 }
6420
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006421 /**
6422 * @deprecated use {@code BigPictureStyle()}.
6423 */
6424 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006425 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006426 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006427 }
6428
Chris Wrend6297db2012-05-03 16:20:13 -04006429 /**
6430 * Overrides ContentTitle in the big form of the template.
6431 * This defaults to the value passed to setContentTitle().
6432 */
6433 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006434 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006435 return this;
6436 }
6437
6438 /**
6439 * Set the first line of text after the detail section in the big form of the template.
6440 */
6441 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006442 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006443 return this;
6444 }
6445
Chris Wren0bd664d2012-08-01 13:56:56 -04006446 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05006447 * @hide
6448 */
6449 public Bitmap getBigPicture() {
6450 return mPicture;
6451 }
6452
6453 /**
Chris Wren0bd664d2012-08-01 13:56:56 -04006454 * Provide the bitmap to be used as the payload for the BigPicture notification.
6455 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006456 public BigPictureStyle bigPicture(Bitmap b) {
6457 mPicture = b;
6458 return this;
6459 }
6460
Chris Wren3745a3d2012-05-22 15:11:52 -04006461 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04006462 * Override the large icon when the big notification is shown.
6463 */
6464 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04006465 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
6466 }
6467
6468 /**
6469 * Override the large icon when the big notification is shown.
6470 */
6471 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04006472 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006473 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006474 return this;
6475 }
6476
Riley Andrews0394a0c2015-11-03 23:36:52 -08006477 /** @hide */
6478 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
6479
Daniel Sandler0ec46202015-06-24 01:27:05 -04006480 /**
6481 * @hide
6482 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006483 @Override
6484 public void purgeResources() {
6485 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08006486 if (mPicture != null &&
6487 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08006488 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006489 mPicture = mPicture.createAshmemBitmap();
6490 }
6491 if (mBigLargeIcon != null) {
6492 mBigLargeIcon.convertToAshmem();
6493 }
6494 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01006495
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006496 /**
6497 * @hide
6498 */
Selim Cinekd0426622017-07-11 13:19:59 +02006499 @Override
6500 public void reduceImageSizes(Context context) {
6501 super.reduceImageSizes(context);
6502 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006503 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006504 if (mPicture != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006505 int maxPictureWidth = resources.getDimensionPixelSize(isLowRam
6506 ? R.dimen.notification_big_picture_max_height_low_ram
6507 : R.dimen.notification_big_picture_max_height);
6508 int maxPictureHeight = resources.getDimensionPixelSize(isLowRam
6509 ? R.dimen.notification_big_picture_max_width_low_ram
6510 : R.dimen.notification_big_picture_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006511 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
6512 }
6513 if (mBigLargeIcon != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006514 int rightIconSize = resources.getDimensionPixelSize(isLowRam
6515 ? R.dimen.notification_right_icon_size_low_ram
6516 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006517 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
6518 }
6519 }
6520
6521 /**
6522 * @hide
6523 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006524 public RemoteViews makeBigContentView() {
6525 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01006526 // This covers the following cases:
6527 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006528 // mN.mLargeIcon
6529 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04006530 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07006531 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006532 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006533 oldLargeIcon = mBuilder.mN.mLargeIcon;
6534 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006535 // The legacy largeIcon might not allow us to clear the image, as it's taken in
6536 // replacement if the other one is null. Because we're restoring these legacy icons
6537 // for old listeners, this is in general non-null.
6538 largeIconLegacy = mBuilder.mN.largeIcon;
6539 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006540 }
6541
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006542 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder);
Selim Cinek384804b2018-04-18 14:31:07 +08006543 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource(),
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006544 p, null /* result */);
Selim Cinek03d0d652015-11-13 13:18:09 -05006545 if (mSummaryTextSet) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006546 contentView.setTextViewText(R.id.text, mBuilder.processTextSpans(
6547 mBuilder.processLegacyText(mSummaryText)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006548 mBuilder.setTextViewColorSecondary(contentView, R.id.text, p);
Selim Cinekc848c3a2016-01-13 15:27:30 -08006549 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05006550 }
Selim Cinek279fa862016-06-14 10:57:25 -07006551 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08006552
Christoph Studer5c510ee2014-12-15 16:32:27 +01006553 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006554 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006555 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006556 }
6557
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006558 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006559 return contentView;
6560 }
6561
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006562 /**
6563 * @hide
6564 */
6565 public void addExtras(Bundle extras) {
6566 super.addExtras(extras);
6567
6568 if (mBigLargeIconSet) {
6569 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
6570 }
6571 extras.putParcelable(EXTRA_PICTURE, mPicture);
6572 }
6573
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006574 /**
6575 * @hide
6576 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006577 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02006578 protected void restoreFromExtras(Bundle extras) {
6579 super.restoreFromExtras(extras);
6580
6581 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01006582 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02006583 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04006584 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006585 mPicture = extras.getParcelable(EXTRA_PICTURE);
6586 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006587
6588 /**
6589 * @hide
6590 */
6591 @Override
6592 public boolean hasSummaryInHeader() {
6593 return false;
6594 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006595
6596 /**
6597 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006598 * Note that we aren't actually comparing the contents of the bitmaps here, so this
6599 * is only doing a cursory inspection. Bitmaps of equal size will appear the same.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006600 */
6601 @Override
6602 public boolean areNotificationsVisiblyDifferent(Style other) {
6603 if (other == null || getClass() != other.getClass()) {
6604 return true;
6605 }
6606 BigPictureStyle otherS = (BigPictureStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006607 return areBitmapsObviouslyDifferent(getBigPicture(), otherS.getBigPicture());
6608 }
6609
6610 private static boolean areBitmapsObviouslyDifferent(Bitmap a, Bitmap b) {
6611 if (a == b) {
6612 return false;
6613 }
6614 if (a == null || b == null) {
6615 return true;
6616 }
6617 return a.getWidth() != b.getWidth()
6618 || a.getHeight() != b.getHeight()
6619 || a.getConfig() != b.getConfig()
6620 || a.getGenerationId() != b.getGenerationId();
Julia Reynolds7217dc92018-03-07 12:12:09 -05006621 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006622 }
6623
6624 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006625 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08006626 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006627 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006628 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006629 * Notification notif = new Notification.Builder(mContext)
6630 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
6631 * .setContentText(subject)
6632 * .setSmallIcon(R.drawable.new_mail)
6633 * .setLargeIcon(aBitmap)
6634 * .setStyle(new Notification.BigTextStyle()
6635 * .bigText(aVeryLongString))
6636 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006637 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006638 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006639 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006640 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006641 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006642
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006643 private CharSequence mBigText;
6644
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006645 public BigTextStyle() {
6646 }
6647
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006648 /**
6649 * @deprecated use {@code BigTextStyle()}.
6650 */
6651 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006652 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006653 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006654 }
6655
Chris Wrend6297db2012-05-03 16:20:13 -04006656 /**
6657 * Overrides ContentTitle in the big form of the template.
6658 * This defaults to the value passed to setContentTitle().
6659 */
6660 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006661 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006662 return this;
6663 }
6664
6665 /**
6666 * Set the first line of text after the detail section in the big form of the template.
6667 */
6668 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006669 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006670 return this;
6671 }
6672
Chris Wren0bd664d2012-08-01 13:56:56 -04006673 /**
6674 * Provide the longer text to be displayed in the big form of the
6675 * template in place of the content text.
6676 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006677 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006678 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006679 return this;
6680 }
6681
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006682 /**
6683 * @hide
6684 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05006685 public CharSequence getBigText() {
6686 return mBigText;
6687 }
6688
6689 /**
6690 * @hide
6691 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006692 public void addExtras(Bundle extras) {
6693 super.addExtras(extras);
6694
Christoph Studer4600f9b2014-07-22 22:44:43 +02006695 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
6696 }
6697
6698 /**
6699 * @hide
6700 */
6701 @Override
6702 protected void restoreFromExtras(Bundle extras) {
6703 super.restoreFromExtras(extras);
6704
6705 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006706 }
6707
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006708 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006709 * @param increasedHeight true if this layout be created with an increased height.
6710 *
6711 * @hide
6712 */
6713 @Override
6714 public RemoteViews makeContentView(boolean increasedHeight) {
6715 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006716 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006717 mBuilder.mActions = new ArrayList<>();
6718 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006719 mBuilder.mActions = mBuilder.mOriginalActions;
6720 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006721 return remoteViews;
6722 }
6723 return super.makeContentView(increasedHeight);
6724 }
6725
6726 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006727 * @hide
6728 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006729 @Override
6730 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
6731 if (increasedHeight && mBuilder.mActions.size() > 0) {
6732 return makeBigContentView();
6733 }
6734 return super.makeHeadsUpContentView(increasedHeight);
6735 }
6736
6737 /**
6738 * @hide
6739 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006740 public RemoteViews makeBigContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006741 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder).text(null);
Selim Cinek384804b2018-04-18 14:31:07 +08006742 TemplateBindResult result = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006743 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource(), p,
6744 result);
Selim Cinek1c72fa02018-04-23 18:00:54 +08006745 contentView.setInt(R.id.big_text, "setImageEndMargin", result.getIconMarginEnd());
Joe Malin8d40d042012-11-05 11:36:40 -08006746
Selim Cinek3a2c4b92015-12-17 17:01:17 -08006747 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07006748 if (TextUtils.isEmpty(bigTextText)) {
6749 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
6750 // experience
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006751 bigTextText = mBuilder.processLegacyText(
6752 mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT));
Selim Cinek75998782016-04-26 10:39:17 -07006753 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006754 contentView.setTextViewText(R.id.big_text, mBuilder.processTextSpans(bigTextText));
6755 mBuilder.setTextViewColorSecondary(contentView, R.id.big_text, p);
6756 contentView.setViewVisibility(R.id.big_text,
6757 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08006758 contentView.setBoolean(R.id.big_text, "setHasImage",
6759 result.isRightIconContainerVisible());
Selim Cinek4fb12d32015-11-19 18:10:48 -08006760
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006761 return contentView;
6762 }
6763
Julia Reynolds7217dc92018-03-07 12:12:09 -05006764 /**
6765 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006766 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006767 */
6768 @Override
6769 public boolean areNotificationsVisiblyDifferent(Style other) {
6770 if (other == null || getClass() != other.getClass()) {
6771 return true;
6772 }
6773 BigTextStyle newS = (BigTextStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006774 return !Objects.equals(String.valueOf(getBigText()), String.valueOf(newS.getBigText()));
Julia Reynolds7217dc92018-03-07 12:12:09 -05006775 }
6776
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006777 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006778
6779 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006780 * Helper class for generating large-format notifications that include multiple back-and-forth
6781 * messages of varying types between any number of people.
6782 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006783 * <p>
Alex Hillsfc737de2016-03-23 17:33:02 -04006784 * If the platform does not provide large-format notifications, this method has no effect. The
6785 * user will always see the normal notification view.
Selim Cinekce8794f2018-05-23 16:46:05 -07006786 *
6787 * <p>
6788 * If the app is targeting Android P and above, it is required to use the {@link Person}
6789 * class in order to get an optimal rendering of the notification and its avatars. For
6790 * conversations involving multiple people, the app should also make sure that it marks the
6791 * conversation as a group with {@link #setGroupConversation(boolean)}.
6792 *
6793 * <p>
6794 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior.
6795 * Here's an example of how this may be used:
Alex Hillsfc737de2016-03-23 17:33:02 -04006796 * <pre class="prettyprint">
6797 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006798 * Person user = new Person.Builder().setIcon(userIcon).setName(userName).build();
6799 * MessagingStyle style = new MessagingStyle(user)
6800 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getPerson())
6801 * .addMessage(messages[2].getText(), messages[2].getTime(), messages[2].getPerson())
6802 * .setGroupConversation(hasMultiplePeople());
6803 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006804 * Notification noti = new Notification.Builder()
Selim Cinekce8794f2018-05-23 16:46:05 -07006805 * .setContentTitle(&quot;2 new messages with &quot; + sender.toString())
Alex Hillsfc737de2016-03-23 17:33:02 -04006806 * .setContentText(subject)
6807 * .setSmallIcon(R.drawable.new_message)
6808 * .setLargeIcon(aBitmap)
Selim Cinekce8794f2018-05-23 16:46:05 -07006809 * .setStyle(style)
Alex Hillsfc737de2016-03-23 17:33:02 -04006810 * .build();
6811 * </pre>
6812 */
6813 public static class MessagingStyle extends Style {
6814
6815 /**
6816 * The maximum number of messages that will be retained in the Notification itself (the
6817 * number displayed is up to the platform).
6818 */
6819 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
6820
Selim Cinekcb8b9852017-12-15 18:01:52 -08006821 @NonNull Person mUser;
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006822 @Nullable CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04006823 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08006824 List<Message> mHistoricMessages = new ArrayList<>();
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006825 boolean mIsGroupConversation;
Alex Hillsfc737de2016-03-23 17:33:02 -04006826
6827 MessagingStyle() {
6828 }
6829
6830 /**
Alex Hillsfd590442016-10-07 09:52:44 -04006831 * @param userDisplayName Required - the name to be displayed for any replies sent by the
6832 * user before the posting app reposts the notification with those messages after they've
6833 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04006834 * {@link #addMessage(Notification.MessagingStyle.Message)}
Selim Cinekcb8b9852017-12-15 18:01:52 -08006835 *
6836 * @deprecated use {@code MessagingStyle(Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006837 */
Alex Hillsfd590442016-10-07 09:52:44 -04006838 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Selim Cinek9acd6732018-03-23 16:39:02 -07006839 this(new Person.Builder().setName(userDisplayName).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006840 }
6841
6842 /**
6843 * @param user Required - The person displayed for any messages that are sent by the
6844 * user. Any messages added with {@link #addMessage(Notification.MessagingStyle.Message)}
6845 * who don't have a Person associated with it will be displayed as if they were sent
Selim Cinek90343862018-02-01 11:07:11 -08006846 * by this user. The user also needs to have a valid name associated with it, which will
6847 * be enforced starting in Android P.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006848 */
6849 public MessagingStyle(@NonNull Person user) {
6850 mUser = user;
Selim Cinek90343862018-02-01 11:07:11 -08006851 }
6852
6853 /**
6854 * Validate that this style was properly composed. This is called at build time.
6855 * @hide
6856 */
6857 @Override
6858 public void validate(Context context) {
6859 super.validate(context);
6860 if (context.getApplicationInfo().targetSdkVersion
6861 >= Build.VERSION_CODES.P && (mUser == null || mUser.getName() == null)) {
6862 throw new RuntimeException("User must be valid and have a name.");
Selim Cinekcb8b9852017-12-15 18:01:52 -08006863 }
6864 }
6865
6866 /**
koprivaa1a78482018-10-09 10:09:23 -07006867 * @return the text that should be displayed in the statusBar when heads upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006868 * If {@code null} is returned, the default implementation will be used.
6869 *
6870 * @hide
6871 */
6872 @Override
6873 public CharSequence getHeadsUpStatusBarText() {
6874 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
6875 ? super.mBigContentTitle
6876 : mConversationTitle;
6877 if (!TextUtils.isEmpty(conversationTitle) && !hasOnlyWhiteSpaceSenders()) {
6878 return conversationTitle;
6879 }
6880 return null;
6881 }
6882
6883 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08006884 * @return the user to be displayed for any replies sent by the user
6885 */
Selim Cinekeb53c222018-04-06 12:56:09 -07006886 @NonNull
Selim Cinekcb8b9852017-12-15 18:01:52 -08006887 public Person getUser() {
6888 return mUser;
Alex Hillsfc737de2016-03-23 17:33:02 -04006889 }
6890
6891 /**
6892 * Returns the name to be displayed for any replies sent by the user
Selim Cinekcb8b9852017-12-15 18:01:52 -08006893 *
6894 * @deprecated use {@link #getUser()} instead
Alex Hillsfc737de2016-03-23 17:33:02 -04006895 */
6896 public CharSequence getUserDisplayName() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006897 return mUser.getName();
Alex Hillsfc737de2016-03-23 17:33:02 -04006898 }
6899
6900 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006901 * Sets the title to be displayed on this conversation. May be set to {@code null}.
6902 *
Kodlee Yin14656422017-12-22 17:00:46 -08006903 * <p>This API's behavior was changed in SDK version {@link Build.VERSION_CODES#P}. If your
6904 * application's target version is less than {@link Build.VERSION_CODES#P}, setting a
6905 * conversation title to a non-null value will make {@link #isGroupConversation()} return
6906 * {@code true} and passing {@code null} will make it return {@code false}. In
6907 * {@link Build.VERSION_CODES#P} and beyond, use {@link #setGroupConversation(boolean)}
6908 * to set group conversation status.
6909 *
6910 * @param conversationTitle Title displayed for this conversation
6911 * @return this object for method chaining
Alex Hillsfc737de2016-03-23 17:33:02 -04006912 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006913 public MessagingStyle setConversationTitle(@Nullable CharSequence conversationTitle) {
Alex Hillsfc737de2016-03-23 17:33:02 -04006914 mConversationTitle = conversationTitle;
6915 return this;
6916 }
6917
6918 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006919 * Return the title to be displayed on this conversation. May return {@code null}.
Alex Hillsfc737de2016-03-23 17:33:02 -04006920 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006921 @Nullable
Alex Hillsfc737de2016-03-23 17:33:02 -04006922 public CharSequence getConversationTitle() {
6923 return mConversationTitle;
6924 }
6925
6926 /**
6927 * Adds a message for display by this notification. Convenience call for a simple
6928 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6929 * @param text A {@link CharSequence} to be displayed as the message content
6930 * @param timestamp Time at which the message arrived
6931 * @param sender A {@link CharSequence} to be used for displaying the name of the
6932 * sender. Should be <code>null</code> for messages by the current user, in which case
6933 * the platform will insert {@link #getUserDisplayName()}.
6934 * Should be unique amongst all individuals in the conversation, and should be
6935 * consistent during re-posts of the notification.
6936 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07006937 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Alex Hillsfc737de2016-03-23 17:33:02 -04006938 *
6939 * @return this object for method chaining
Selim Cinekcb8b9852017-12-15 18:01:52 -08006940 *
6941 * @deprecated use {@link #addMessage(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006942 */
6943 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006944 return addMessage(text, timestamp,
Selim Cinek9acd6732018-03-23 16:39:02 -07006945 sender == null ? null : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006946 }
6947
6948 /**
6949 * Adds a message for display by this notification. Convenience call for a simple
6950 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6951 * @param text A {@link CharSequence} to be displayed as the message content
6952 * @param timestamp Time at which the message arrived
6953 * @param sender The {@link Person} who sent the message.
6954 * Should be <code>null</code> for messages by the current user, in which case
6955 * the platform will insert the user set in {@code MessagingStyle(Person)}.
6956 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07006957 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Selim Cinekcb8b9852017-12-15 18:01:52 -08006958 *
6959 * @return this object for method chaining
6960 */
Selim Cinekeb53c222018-04-06 12:56:09 -07006961 public MessagingStyle addMessage(@NonNull CharSequence text, long timestamp,
6962 @Nullable Person sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08006963 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04006964 }
6965
6966 /**
6967 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08006968 *
6969 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6970 * the newest last.
6971 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006972 * @param message The {@link Message} to be displayed
6973 * @return this object for method chaining
6974 */
6975 public MessagingStyle addMessage(Message message) {
6976 mMessages.add(message);
6977 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6978 mMessages.remove(0);
6979 }
6980 return this;
6981 }
6982
6983 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006984 * Adds a {@link Message} for historic context in this notification.
6985 *
6986 * <p>Messages should be added as historic if they are not the main subject of the
6987 * notification but may give context to a conversation. The system may choose to present
6988 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
6989 *
6990 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6991 * the newest last.
6992 *
6993 * @param message The historic {@link Message} to be added
6994 * @return this object for method chaining
6995 */
6996 public MessagingStyle addHistoricMessage(Message message) {
6997 mHistoricMessages.add(message);
6998 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6999 mHistoricMessages.remove(0);
7000 }
7001 return this;
7002 }
7003
7004 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007005 * Gets the list of {@code Message} objects that represent the notification
7006 */
7007 public List<Message> getMessages() {
7008 return mMessages;
7009 }
7010
7011 /**
Adrian Roos437cd562017-01-18 15:47:03 -08007012 * Gets the list of historic {@code Message}s in the notification.
7013 */
7014 public List<Message> getHistoricMessages() {
7015 return mHistoricMessages;
7016 }
7017
7018 /**
Selim Cinekce8794f2018-05-23 16:46:05 -07007019 * Sets whether this conversation notification represents a group. If the app is targeting
7020 * Android P, this is required if the app wants to display the largeIcon set with
7021 * {@link Notification.Builder#setLargeIcon(Bitmap)}, otherwise it will be hidden.
Kodlee Yin14656422017-12-22 17:00:46 -08007022 *
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007023 * @param isGroupConversation {@code true} if the conversation represents a group,
7024 * {@code false} otherwise.
7025 * @return this object for method chaining
7026 */
7027 public MessagingStyle setGroupConversation(boolean isGroupConversation) {
7028 mIsGroupConversation = isGroupConversation;
7029 return this;
7030 }
7031
7032 /**
Kodlee Yin14656422017-12-22 17:00:46 -08007033 * Returns {@code true} if this notification represents a group conversation, otherwise
7034 * {@code false}.
7035 *
7036 * <p> If the application that generated this {@link MessagingStyle} targets an SDK version
7037 * less than {@link Build.VERSION_CODES#P}, this method becomes dependent on whether or
7038 * not the conversation title is set; returning {@code true} if the conversation title is
7039 * a non-null value, or {@code false} otherwise. From {@link Build.VERSION_CODES#P} forward,
7040 * this method returns what's set by {@link #setGroupConversation(boolean)} allowing for
7041 * named, non-group conversations.
7042 *
7043 * @see #setConversationTitle(CharSequence)
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007044 */
7045 public boolean isGroupConversation() {
Kodlee Yin14656422017-12-22 17:00:46 -08007046 // When target SDK version is < P, a non-null conversation title dictates if this is
7047 // as group conversation.
7048 if (mBuilder != null
7049 && mBuilder.mContext.getApplicationInfo().targetSdkVersion
7050 < Build.VERSION_CODES.P) {
7051 return mConversationTitle != null;
7052 }
7053
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007054 return mIsGroupConversation;
7055 }
7056
7057 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007058 * @hide
7059 */
7060 @Override
7061 public void addExtras(Bundle extras) {
7062 super.addExtras(extras);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007063 if (mUser != null) {
7064 // For legacy usages
7065 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUser.getName());
7066 extras.putParcelable(EXTRA_MESSAGING_PERSON, mUser);
Alex Hillsfc737de2016-03-23 17:33:02 -04007067 }
7068 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007069 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04007070 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007071 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
7072 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04007073 }
Adrian Roos437cd562017-01-18 15:47:03 -08007074 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
7075 Message.getBundleArrayForMessages(mHistoricMessages));
7076 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007077
7078 fixTitleAndTextExtras(extras);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007079 extras.putBoolean(EXTRA_IS_GROUP_CONVERSATION, mIsGroupConversation);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007080 }
7081
7082 private void fixTitleAndTextExtras(Bundle extras) {
7083 Message m = findLatestIncomingMessage();
7084 CharSequence text = (m == null) ? null : m.mText;
7085 CharSequence sender = m == null ? null
Selim Cinekcb8b9852017-12-15 18:01:52 -08007086 : m.mSender == null || TextUtils.isEmpty(m.mSender.getName())
7087 ? mUser.getName() : m.mSender.getName();
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007088 CharSequence title;
7089 if (!TextUtils.isEmpty(mConversationTitle)) {
7090 if (!TextUtils.isEmpty(sender)) {
7091 BidiFormatter bidi = BidiFormatter.getInstance();
7092 title = mBuilder.mContext.getString(
7093 com.android.internal.R.string.notification_messaging_title_template,
Selim Cinekcb8b9852017-12-15 18:01:52 -08007094 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(sender));
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007095 } else {
7096 title = mConversationTitle;
7097 }
7098 } else {
7099 title = sender;
7100 }
7101
7102 if (title != null) {
7103 extras.putCharSequence(EXTRA_TITLE, title);
7104 }
7105 if (text != null) {
7106 extras.putCharSequence(EXTRA_TEXT, text);
7107 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007108 }
7109
7110 /**
7111 * @hide
7112 */
7113 @Override
7114 protected void restoreFromExtras(Bundle extras) {
7115 super.restoreFromExtras(extras);
7116
Selim Cinekcb8b9852017-12-15 18:01:52 -08007117 mUser = extras.getParcelable(EXTRA_MESSAGING_PERSON);
7118 if (mUser == null) {
7119 CharSequence displayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
Selim Cinek9acd6732018-03-23 16:39:02 -07007120 mUser = new Person.Builder().setName(displayName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007121 }
Adrian Roos96b7e202016-05-17 13:50:38 -07007122 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08007123 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007124 mMessages = Message.getMessagesFromBundleArray(messages);
Adrian Roos437cd562017-01-18 15:47:03 -08007125 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007126 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007127 mIsGroupConversation = extras.getBoolean(EXTRA_IS_GROUP_CONVERSATION);
Alex Hillsfc737de2016-03-23 17:33:02 -04007128 }
7129
7130 /**
7131 * @hide
7132 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07007133 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007134 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07007135 mBuilder.mOriginalActions = mBuilder.mActions;
7136 mBuilder.mActions = new ArrayList<>();
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007137 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007138 false /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007139 mBuilder.mActions = mBuilder.mOriginalActions;
7140 mBuilder.mOriginalActions = null;
7141 return remoteViews;
Adrian Roosc1a80b02016-04-05 14:54:55 -07007142 }
7143
Julia Reynolds7217dc92018-03-07 12:12:09 -05007144 /**
7145 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04007146 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05007147 */
7148 @Override
7149 public boolean areNotificationsVisiblyDifferent(Style other) {
7150 if (other == null || getClass() != other.getClass()) {
7151 return true;
7152 }
7153 MessagingStyle newS = (MessagingStyle) other;
7154 List<MessagingStyle.Message> oldMs = getMessages();
7155 List<MessagingStyle.Message> newMs = newS.getMessages();
7156
Dan Sandler7d67bd42018-05-15 14:06:38 -04007157 if (oldMs == null || newMs == null) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007158 newMs = new ArrayList<>();
7159 }
7160
7161 int n = oldMs.size();
7162 if (n != newMs.size()) {
7163 return true;
7164 }
7165 for (int i = 0; i < n; i++) {
7166 MessagingStyle.Message oldM = oldMs.get(i);
7167 MessagingStyle.Message newM = newMs.get(i);
Dan Sandler7d67bd42018-05-15 14:06:38 -04007168 if (!Objects.equals(
7169 String.valueOf(oldM.getText()),
7170 String.valueOf(newM.getText()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007171 return true;
7172 }
7173 if (!Objects.equals(oldM.getDataUri(), newM.getDataUri())) {
7174 return true;
7175 }
Dan Sandler7d67bd42018-05-15 14:06:38 -04007176 String oldSender = String.valueOf(oldM.getSenderPerson() == null
7177 ? oldM.getSender()
7178 : oldM.getSenderPerson().getName());
7179 String newSender = String.valueOf(newM.getSenderPerson() == null
7180 ? newM.getSender()
7181 : newM.getSenderPerson().getName());
Julia Reynolds7217dc92018-03-07 12:12:09 -05007182 if (!Objects.equals(oldSender, newSender)) {
7183 return true;
7184 }
7185
7186 String oldKey = oldM.getSenderPerson() == null
7187 ? null : oldM.getSenderPerson().getKey();
7188 String newKey = newM.getSenderPerson() == null
7189 ? null : newM.getSenderPerson().getKey();
7190 if (!Objects.equals(oldKey, newKey)) {
7191 return true;
7192 }
7193 // Other fields (like timestamp) intentionally excluded
7194 }
7195 return false;
7196 }
7197
Adrian Roosc1a80b02016-04-05 14:54:55 -07007198 private Message findLatestIncomingMessage() {
Selim Cinek88188f22017-09-19 16:46:56 -07007199 return findLatestIncomingMessage(mMessages);
7200 }
7201
7202 /**
7203 * @hide
7204 */
7205 @Nullable
7206 public static Message findLatestIncomingMessage(
7207 List<Message> messages) {
7208 for (int i = messages.size() - 1; i >= 0; i--) {
7209 Message m = messages.get(i);
Adrian Roosc1a80b02016-04-05 14:54:55 -07007210 // Incoming messages have a non-empty sender.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007211 if (m.mSender != null && !TextUtils.isEmpty(m.mSender.getName())) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07007212 return m;
7213 }
7214 }
Selim Cinek88188f22017-09-19 16:46:56 -07007215 if (!messages.isEmpty()) {
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007216 // No incoming messages, fall back to outgoing message
Selim Cinek88188f22017-09-19 16:46:56 -07007217 return messages.get(messages.size() - 1);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007218 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07007219 return null;
7220 }
7221
7222 /**
7223 * @hide
7224 */
7225 @Override
7226 public RemoteViews makeBigContentView() {
Selim Cinek384804b2018-04-18 14:31:07 +08007227 return makeMessagingView(false /* displayImagesAtEnd */, true /* hideLargeIcon */);
Selim Cinekafeed292017-12-12 17:32:44 -08007228 }
7229
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007230 /**
7231 * Create a messaging layout.
7232 *
7233 * @param displayImagesAtEnd should images be displayed at the end of the content instead
7234 * of inline.
Selim Cinek384804b2018-04-18 14:31:07 +08007235 * @param hideRightIcons Should the reply affordance be shown at the end of the notification
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007236 * @return the created remoteView.
7237 */
Selim Cinekafeed292017-12-12 17:32:44 -08007238 @NonNull
Selim Cinek384804b2018-04-18 14:31:07 +08007239 private RemoteViews makeMessagingView(boolean displayImagesAtEnd, boolean hideRightIcons) {
Selim Cinek88188f22017-09-19 16:46:56 -07007240 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
Adrian Roosc1a80b02016-04-05 14:54:55 -07007241 ? super.mBigContentTitle
7242 : mConversationTitle;
Selim Cinekce8794f2018-05-23 16:46:05 -07007243 boolean atLeastP = mBuilder.mContext.getApplicationInfo().targetSdkVersion
7244 >= Build.VERSION_CODES.P;
7245 boolean isOneToOne;
Selim Cinek2dd3e722018-01-19 11:06:06 -08007246 CharSequence nameReplacement = null;
Selim Cinekce8794f2018-05-23 16:46:05 -07007247 Icon avatarReplacement = null;
7248 if (!atLeastP) {
7249 isOneToOne = TextUtils.isEmpty(conversationTitle);
7250 avatarReplacement = mBuilder.mN.mLargeIcon;
7251 if (hasOnlyWhiteSpaceSenders()) {
7252 isOneToOne = true;
7253 nameReplacement = conversationTitle;
7254 conversationTitle = null;
7255 }
7256 } else {
7257 isOneToOne = !isGroupConversation();
Adrian Roosb1f427c2016-05-26 12:27:15 -07007258 }
Selim Cinek384804b2018-04-18 14:31:07 +08007259 TemplateBindResult bindResult = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007260 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).title(
7261 conversationTitle).text(null)
7262 .hideLargeIcon(hideRightIcons || isOneToOne)
7263 .hideReplyIcon(hideRightIcons)
7264 .headerTextSecondary(conversationTitle);
Adrian Roos48d746a2016-04-12 14:57:28 -07007265 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07007266 mBuilder.getMessagingLayoutResource(),
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007267 p,
Selim Cinek384804b2018-04-18 14:31:07 +08007268 bindResult);
Selim Cinek88188f22017-09-19 16:46:56 -07007269 addExtras(mBuilder.mN.extras);
Selim Cinekafeed292017-12-12 17:32:44 -08007270 // also update the end margin if there is an image
Selim Cinek1c72fa02018-04-23 18:00:54 +08007271 contentView.setViewLayoutMarginEnd(R.id.notification_messaging,
7272 bindResult.getIconMarginEnd());
Selim Cinek88188f22017-09-19 16:46:56 -07007273 contentView.setInt(R.id.status_bar_latest_event_content, "setLayoutColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007274 mBuilder.isColorized(p) ? mBuilder.getPrimaryTextColor(p)
7275 : mBuilder.resolveContrastColor(p));
Kenny Guy14d035c2018-05-02 19:10:36 +01007276 contentView.setInt(R.id.status_bar_latest_event_content, "setSenderTextColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007277 mBuilder.getPrimaryTextColor(p));
Kenny Guy14d035c2018-05-02 19:10:36 +01007278 contentView.setInt(R.id.status_bar_latest_event_content, "setMessageTextColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007279 mBuilder.getSecondaryTextColor(p));
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007280 contentView.setBoolean(R.id.status_bar_latest_event_content, "setDisplayImagesAtEnd",
7281 displayImagesAtEnd);
Selim Cinekce8794f2018-05-23 16:46:05 -07007282 contentView.setIcon(R.id.status_bar_latest_event_content, "setAvatarReplacement",
7283 avatarReplacement);
Selim Cinek2dd3e722018-01-19 11:06:06 -08007284 contentView.setCharSequence(R.id.status_bar_latest_event_content, "setNameReplacement",
7285 nameReplacement);
Selim Cinek88188f22017-09-19 16:46:56 -07007286 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsOneToOne",
7287 isOneToOne);
7288 contentView.setBundle(R.id.status_bar_latest_event_content, "setData",
7289 mBuilder.mN.extras);
Alex Hillsfc737de2016-03-23 17:33:02 -04007290 return contentView;
7291 }
7292
Selim Cinekf7409db2017-10-24 16:17:14 -07007293 private boolean hasOnlyWhiteSpaceSenders() {
7294 for (int i = 0; i < mMessages.size(); i++) {
7295 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007296 Person sender = m.getSenderPerson();
7297 if (sender != null && !isWhiteSpace(sender.getName())) {
Selim Cinekf7409db2017-10-24 16:17:14 -07007298 return false;
7299 }
7300 }
7301 return true;
7302 }
7303
7304 private boolean isWhiteSpace(CharSequence sender) {
7305 if (TextUtils.isEmpty(sender)) {
7306 return true;
7307 }
7308 if (sender.toString().matches("^\\s*$")) {
7309 return true;
7310 }
7311 // Let's check if we only have 0 whitespace chars. Some apps did this as a workaround
7312 // For the presentation that we had.
7313 for (int i = 0; i < sender.length(); i++) {
7314 char c = sender.charAt(i);
7315 if (c != '\u200B') {
7316 return false;
7317 }
7318 }
7319 return true;
7320 }
7321
Selim Cinek88188f22017-09-19 16:46:56 -07007322 private CharSequence createConversationTitleFromMessages() {
7323 ArraySet<CharSequence> names = new ArraySet<>();
7324 for (int i = 0; i < mMessages.size(); i++) {
7325 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007326 Person sender = m.getSenderPerson();
Selim Cinek88188f22017-09-19 16:46:56 -07007327 if (sender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007328 names.add(sender.getName());
Selim Cinek88188f22017-09-19 16:46:56 -07007329 }
7330 }
7331 SpannableStringBuilder title = new SpannableStringBuilder();
7332 int size = names.size();
7333 for (int i = 0; i < size; i++) {
7334 CharSequence name = names.valueAt(i);
7335 if (!TextUtils.isEmpty(title)) {
7336 title.append(", ");
7337 }
7338 title.append(BidiFormatter.getInstance().unicodeWrap(name));
7339 }
7340 return title;
7341 }
7342
Adrian Roosdedd1df2016-04-26 16:38:47 -07007343 /**
7344 * @hide
7345 */
7346 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007347 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007348 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007349 true /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007350 remoteViews.setInt(R.id.notification_messaging, "setMaxDisplayedLines", 1);
7351 return remoteViews;
Adrian Roosdedd1df2016-04-26 16:38:47 -07007352 }
7353
Adrian Roosc1a80b02016-04-05 14:54:55 -07007354 private static TextAppearanceSpan makeFontColorSpan(int color) {
7355 return new TextAppearanceSpan(null, 0, 0,
7356 ColorStateList.valueOf(color), null);
7357 }
7358
Alex Hillsd9b04d92016-04-11 16:38:16 -04007359 public static final class Message {
Tony Mak09db2ea2018-06-27 18:12:48 +01007360 /** @hide */
7361 public static final String KEY_TEXT = "text";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007362 static final String KEY_TIMESTAMP = "time";
7363 static final String KEY_SENDER = "sender";
Selim Cinekcb8b9852017-12-15 18:01:52 -08007364 static final String KEY_SENDER_PERSON = "sender_person";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007365 static final String KEY_DATA_MIME_TYPE = "type";
7366 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08007367 static final String KEY_EXTRAS_BUNDLE = "extras";
Kenny Guya0f6de82018-04-06 16:20:16 +01007368 static final String KEY_REMOTE_INPUT_HISTORY = "remote_input_history";
Alex Hillsfc737de2016-03-23 17:33:02 -04007369
7370 private final CharSequence mText;
7371 private final long mTimestamp;
Selim Cinekcb8b9852017-12-15 18:01:52 -08007372 @Nullable
7373 private final Person mSender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007374 /** True if this message was generated from the extra
7375 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}
7376 */
7377 private final boolean mRemoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007378
Shane Brennan5a871862017-03-11 13:14:17 -08007379 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007380 private String mDataMimeType;
7381 private Uri mDataUri;
7382
7383 /**
7384 * Constructor
7385 * @param text A {@link CharSequence} to be displayed as the message content
7386 * @param timestamp Time at which the message arrived
7387 * @param sender A {@link CharSequence} to be used for displaying the name of the
7388 * sender. Should be <code>null</code> for messages by the current user, in which case
7389 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
7390 * Should be unique amongst all individuals in the conversation, and should be
7391 * consistent during re-posts of the notification.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007392 *
7393 * @deprecated use {@code Message(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04007394 */
7395 public Message(CharSequence text, long timestamp, CharSequence sender){
Selim Cinek9acd6732018-03-23 16:39:02 -07007396 this(text, timestamp, sender == null ? null
7397 : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08007398 }
7399
7400 /**
7401 * Constructor
7402 * @param text A {@link CharSequence} to be displayed as the message content
7403 * @param timestamp Time at which the message arrived
7404 * @param sender The {@link Person} who sent the message.
7405 * Should be <code>null</code> for messages by the current user, in which case
7406 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7407 * <p>
Selim Cinek9acd6732018-03-23 16:39:02 -07007408 * The person provided should contain an Icon, set with
7409 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7410 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7411 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7412 * to differentiate between the different users.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007413 * </p>
7414 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007415 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender) {
Kenny Guya0f6de82018-04-06 16:20:16 +01007416 this(text, timestamp, sender, false /* remoteHistory */);
7417 }
7418
7419 /**
7420 * Constructor
7421 * @param text A {@link CharSequence} to be displayed as the message content
7422 * @param timestamp Time at which the message arrived
7423 * @param sender The {@link Person} who sent the message.
7424 * Should be <code>null</code> for messages by the current user, in which case
7425 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7426 * @param remoteInputHistory True if the messages was generated from the extra
7427 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7428 * <p>
7429 * The person provided should contain an Icon, set with
7430 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7431 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7432 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7433 * to differentiate between the different users.
7434 * </p>
7435 * @hide
7436 */
7437 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender,
7438 boolean remoteInputHistory) {
Alex Hillsfc737de2016-03-23 17:33:02 -04007439 mText = text;
7440 mTimestamp = timestamp;
7441 mSender = sender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007442 mRemoteInputHistory = remoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007443 }
7444
7445 /**
7446 * Sets a binary blob of data and an associated MIME type for a message. In the case
7447 * where the platform doesn't support the MIME type, the original text provided in the
7448 * constructor will be used.
7449 * @param dataMimeType The MIME type of the content. See
7450 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
7451 * types on Android and Android Wear.
7452 * @param dataUri The uri containing the content whose type is given by the MIME type.
7453 * <p class="note">
7454 * <ol>
7455 * <li>Notification Listeners including the System UI need permission to access the
7456 * data the Uri points to. The recommended ways to do this are:</li>
7457 * <li>Store the data in your own ContentProvider, making sure that other apps have
7458 * the correct permission to access your provider. The preferred mechanism for
7459 * providing access is to use per-URI permissions which are temporary and only
7460 * grant access to the receiving application. An easy way to create a
7461 * ContentProvider like this is to use the FileProvider helper class.</li>
7462 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
7463 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
7464 * also store non-media types (see MediaStore.Files for more info). Files can be
7465 * inserted into the MediaStore using scanFile() after which a content:// style
7466 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
7467 * Note that once added to the system MediaStore the content is accessible to any
7468 * app on the device.</li>
7469 * </ol>
7470 * @return this object for method chaining
7471 */
7472 public Message setData(String dataMimeType, Uri dataUri) {
7473 mDataMimeType = dataMimeType;
7474 mDataUri = dataUri;
7475 return this;
7476 }
7477
Alex Hillsfc737de2016-03-23 17:33:02 -04007478 /**
7479 * Get the text to be used for this message, or the fallback text if a type and content
7480 * Uri have been set
7481 */
7482 public CharSequence getText() {
7483 return mText;
7484 }
7485
7486 /**
7487 * Get the time at which this message arrived
7488 */
7489 public long getTimestamp() {
7490 return mTimestamp;
7491 }
7492
7493 /**
Shane Brennan5a871862017-03-11 13:14:17 -08007494 * Get the extras Bundle for this message.
7495 */
7496 public Bundle getExtras() {
7497 return mExtras;
7498 }
7499
7500 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007501 * Get the text used to display the contact's name in the messaging experience
Selim Cinekcb8b9852017-12-15 18:01:52 -08007502 *
7503 * @deprecated use {@link #getSenderPerson()}
Alex Hillsfc737de2016-03-23 17:33:02 -04007504 */
7505 public CharSequence getSender() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007506 return mSender == null ? null : mSender.getName();
7507 }
7508
7509 /**
7510 * Get the sender associated with this message.
7511 */
7512 @Nullable
7513 public Person getSenderPerson() {
Alex Hillsfc737de2016-03-23 17:33:02 -04007514 return mSender;
7515 }
7516
7517 /**
7518 * Get the MIME type of the data pointed to by the Uri
7519 */
7520 public String getDataMimeType() {
7521 return mDataMimeType;
7522 }
7523
7524 /**
koprivaa1a78482018-10-09 10:09:23 -07007525 * Get the Uri pointing to the content of the message. Can be null, in which case
Alex Hillsfc737de2016-03-23 17:33:02 -04007526 * {@see #getText()} is used.
7527 */
7528 public Uri getDataUri() {
7529 return mDataUri;
7530 }
7531
Kenny Guya0f6de82018-04-06 16:20:16 +01007532 /**
7533 * @return True if the message was generated from
7534 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7535 * @hide
7536 */
7537 public boolean isRemoteInputHistory() {
7538 return mRemoteInputHistory;
7539 }
7540
Beverlye98937a2018-11-15 10:18:57 -05007541 /**
7542 * @hide
7543 */
7544 @VisibleForTesting
7545 public Bundle toBundle() {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007546 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007547 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007548 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04007549 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007550 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04007551 if (mSender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007552 // Legacy listeners need this
7553 bundle.putCharSequence(KEY_SENDER, mSender.getName());
7554 bundle.putParcelable(KEY_SENDER_PERSON, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04007555 }
7556 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007557 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04007558 }
7559 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007560 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04007561 }
Shane Brennan5a871862017-03-11 13:14:17 -08007562 if (mExtras != null) {
7563 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
7564 }
Kenny Guya0f6de82018-04-06 16:20:16 +01007565 if (mRemoteInputHistory) {
7566 bundle.putBoolean(KEY_REMOTE_INPUT_HISTORY, mRemoteInputHistory);
7567 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007568 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04007569 }
7570
Alex Hillsd9b04d92016-04-11 16:38:16 -04007571 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
7572 Bundle[] bundles = new Bundle[messages.size()];
7573 final int N = messages.size();
7574 for (int i = 0; i < N; i++) {
7575 bundles[i] = messages.get(i).toBundle();
7576 }
7577 return bundles;
7578 }
7579
Selim Cinek88188f22017-09-19 16:46:56 -07007580 /**
7581 * @return A list of messages read from the bundles.
7582 *
7583 * @hide
7584 */
7585 public static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
7586 if (bundles == null) {
7587 return new ArrayList<>();
7588 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007589 List<Message> messages = new ArrayList<>(bundles.length);
7590 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07007591 if (bundles[i] instanceof Bundle) {
7592 Message message = getMessageFromBundle((Bundle)bundles[i]);
7593 if (message != null) {
7594 messages.add(message);
7595 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007596 }
7597 }
7598 return messages;
7599 }
7600
Selim Cinekb0dc61b2018-05-22 18:49:36 -07007601 /**
7602 * @return The message that is stored in the bundle or null if the message couldn't be
7603 * resolved.
7604 *
7605 * @hide
7606 */
7607 @Nullable
7608 public static Message getMessageFromBundle(Bundle bundle) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007609 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07007610 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007611 return null;
7612 } else {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007613
7614 Person senderPerson = bundle.getParcelable(KEY_SENDER_PERSON);
7615 if (senderPerson == null) {
7616 // Legacy apps that use compat don't actually provide the sender objects
7617 // We need to fix the compat version to provide people / use
7618 // the native api instead
7619 CharSequence senderName = bundle.getCharSequence(KEY_SENDER);
7620 if (senderName != null) {
Selim Cinek9acd6732018-03-23 16:39:02 -07007621 senderPerson = new Person.Builder().setName(senderName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007622 }
7623 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007624 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
Selim Cinekcb8b9852017-12-15 18:01:52 -08007625 bundle.getLong(KEY_TIMESTAMP),
Kenny Guya0f6de82018-04-06 16:20:16 +01007626 senderPerson,
7627 bundle.getBoolean(KEY_REMOTE_INPUT_HISTORY, false));
Alex Hillsd9b04d92016-04-11 16:38:16 -04007628 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
7629 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007630 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
7631 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04007632 }
Shane Brennan5a871862017-03-11 13:14:17 -08007633 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
7634 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
7635 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007636 return message;
7637 }
7638 } catch (ClassCastException e) {
7639 return null;
7640 }
7641 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007642 }
7643 }
7644
7645 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04007646 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08007647 *
Robert Ly91c5ce32014-06-08 15:37:00 -07007648 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04007649 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07007650 * Notification notif = new Notification.Builder(mContext)
7651 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
7652 * .setContentText(subject)
7653 * .setSmallIcon(R.drawable.new_mail)
7654 * .setLargeIcon(aBitmap)
7655 * .setStyle(new Notification.InboxStyle()
7656 * .addLine(str1)
7657 * .addLine(str2)
7658 * .setContentTitle(&quot;&quot;)
7659 * .setSummaryText(&quot;+3 more&quot;))
7660 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04007661 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08007662 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04007663 * @see Notification#bigContentView
7664 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007665 public static class InboxStyle extends Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07007666
7667 /**
7668 * The number of lines of remote input history allowed until we start reducing lines.
7669 */
7670 private static final int NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION = 1;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007671 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
7672
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007673 public InboxStyle() {
7674 }
7675
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007676 /**
7677 * @deprecated use {@code InboxStyle()}.
7678 */
7679 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04007680 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007681 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04007682 }
7683
Chris Wrend6297db2012-05-03 16:20:13 -04007684 /**
7685 * Overrides ContentTitle in the big form of the template.
7686 * This defaults to the value passed to setContentTitle().
7687 */
7688 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007689 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04007690 return this;
7691 }
7692
7693 /**
7694 * Set the first line of text after the detail section in the big form of the template.
7695 */
7696 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007697 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04007698 return this;
7699 }
7700
Chris Wren0bd664d2012-08-01 13:56:56 -04007701 /**
7702 * Append a line to the digest section of the Inbox notification.
7703 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04007704 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007705 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04007706 return this;
7707 }
7708
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007709 /**
7710 * @hide
7711 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05007712 public ArrayList<CharSequence> getLines() {
7713 return mTexts;
7714 }
7715
7716 /**
7717 * @hide
7718 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007719 public void addExtras(Bundle extras) {
7720 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007721
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007722 CharSequence[] a = new CharSequence[mTexts.size()];
7723 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
7724 }
7725
Christoph Studer4600f9b2014-07-22 22:44:43 +02007726 /**
7727 * @hide
7728 */
7729 @Override
7730 protected void restoreFromExtras(Bundle extras) {
7731 super.restoreFromExtras(extras);
7732
7733 mTexts.clear();
7734 if (extras.containsKey(EXTRA_TEXT_LINES)) {
7735 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
7736 }
7737 }
7738
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007739 /**
7740 * @hide
7741 */
7742 public RemoteViews makeBigContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007743 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder).text(null);
Selim Cinek384804b2018-04-18 14:31:07 +08007744 TemplateBindResult result = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007745 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource(), p, result);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007746
Chris Wrend6297db2012-05-03 16:20:13 -04007747 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 -04007748 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04007749
Chris Wren4ed80d52012-05-17 09:30:03 -04007750 // Make sure all rows are gone in case we reuse a view.
7751 for (int rowId : rowIds) {
7752 contentView.setViewVisibility(rowId, View.GONE);
7753 }
7754
Daniel Sandler879c5e02012-04-17 16:46:51 -04007755 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07007756 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7757 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08007758 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07007759 int onlyViewId = 0;
7760 int maxRows = rowIds.length;
7761 if (mBuilder.mActions.size() > 0) {
7762 maxRows--;
7763 }
Selim Cinekbee4e072018-05-21 22:06:43 -07007764 CharSequence[] remoteInputHistory = mBuilder.mN.extras.getCharSequenceArray(
7765 EXTRA_REMOTE_INPUT_HISTORY);
7766 if (remoteInputHistory != null
7767 && remoteInputHistory.length > NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION) {
7768 // Let's remove some messages to make room for the remote input history.
7769 // 1 is always able to fit, but let's remove them if they are 2 or 3
7770 int numRemoteInputs = Math.min(remoteInputHistory.length,
7771 MAX_REMOTE_INPUT_HISTORY_LINES);
7772 int totalNumRows = mTexts.size() + numRemoteInputs
7773 - NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION;
7774 if (totalNumRows > maxRows) {
7775 int overflow = totalNumRows - maxRows;
7776 if (mTexts.size() > maxRows) {
7777 // Heuristic: if the Texts don't fit anyway, we'll rather drop the last
7778 // few messages, even with the remote input
7779 maxRows -= overflow;
7780 } else {
7781 // otherwise we drop the first messages
7782 i = overflow;
7783 }
7784 }
7785 }
Selim Cinek07c80172016-04-21 16:40:47 -07007786 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007787 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07007788 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007789 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07007790 contentView.setTextViewText(rowIds[i],
7791 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007792 mBuilder.setTextViewColorSecondary(contentView, rowIds[i], p);
Selim Cinek07c80172016-04-21 16:40:47 -07007793 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek384804b2018-04-18 14:31:07 +08007794 handleInboxImageMargin(contentView, rowIds[i], first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007795 result.getIconMarginEnd());
Selim Cinek07c80172016-04-21 16:40:47 -07007796 if (first) {
7797 onlyViewId = rowIds[i];
7798 } else {
7799 onlyViewId = 0;
7800 }
Selim Cinek247fa012016-02-18 09:50:48 -08007801 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007802 }
7803 i++;
7804 }
Selim Cinek07c80172016-04-21 16:40:47 -07007805 if (onlyViewId != 0) {
7806 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
7807 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7808 R.dimen.notification_text_margin_top);
7809 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
7810 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007811
Daniel Sandler879c5e02012-04-17 16:46:51 -04007812 return contentView;
7813 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007814
Julia Reynolds7217dc92018-03-07 12:12:09 -05007815 /**
7816 * @hide
7817 */
7818 @Override
7819 public boolean areNotificationsVisiblyDifferent(Style other) {
7820 if (other == null || getClass() != other.getClass()) {
7821 return true;
7822 }
7823 InboxStyle newS = (InboxStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04007824
7825 final ArrayList<CharSequence> myLines = getLines();
7826 final ArrayList<CharSequence> newLines = newS.getLines();
7827 final int n = myLines.size();
7828 if (n != newLines.size()) {
7829 return true;
7830 }
7831
7832 for (int i = 0; i < n; i++) {
7833 if (!Objects.equals(
7834 String.valueOf(myLines.get(i)),
7835 String.valueOf(newLines.get(i)))) {
7836 return true;
7837 }
7838 }
7839 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05007840 }
7841
Selim Cinek384804b2018-04-18 14:31:07 +08007842 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007843 int marginEndValue) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08007844 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08007845 if (first) {
7846 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
7847 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
7848 boolean hasProgress = max != 0 || ind;
Selim Cinek384804b2018-04-18 14:31:07 +08007849 if (!hasProgress) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08007850 endMargin = marginEndValue;
Selim Cinek247fa012016-02-18 09:50:48 -08007851 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007852 }
Selim Cinek1c72fa02018-04-23 18:00:54 +08007853 contentView.setViewLayoutMarginEnd(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08007854 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04007855 }
Dan Sandler842dd772014-05-15 09:36:47 -04007856
7857 /**
7858 * Notification style for media playback notifications.
7859 *
7860 * In the expanded form, {@link Notification#bigContentView}, up to 5
7861 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04007862 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04007863 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
7864 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
7865 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08007866 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04007867 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
7868 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01007869 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04007870 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08007871 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007872 * Notifications created with MediaStyle will have their category set to
7873 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
7874 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08007875 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07007876 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
7877 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04007878 * the System UI can identify this as a notification representing an active media session
7879 * and respond accordingly (by showing album artwork in the lockscreen, for example).
7880 *
Selim Cinek99104832017-01-25 14:47:33 -08007881 * <p>
7882 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
7883 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
7884 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
7885 * <p>
7886 *
Dan Sandler842dd772014-05-15 09:36:47 -04007887 * To use this style with your Notification, feed it to
7888 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7889 * <pre class="prettyprint">
7890 * Notification noti = new Notification.Builder()
7891 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01007892 * .setContentTitle(&quot;Track title&quot;)
7893 * .setContentText(&quot;Artist - Album&quot;)
7894 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007895 * .setStyle(<b>new Notification.MediaStyle()</b>
7896 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04007897 * .build();
7898 * </pre>
7899 *
7900 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08007901 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04007902 */
7903 public static class MediaStyle extends Style {
Gus Prevas9cc96602018-10-11 11:24:23 -04007904 // Changing max media buttons requires also changing templates
7905 // (notification_template_material_media and notification_template_material_big_media).
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007906 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04007907 static final int MAX_MEDIA_BUTTONS = 5;
Gus Prevas9cc96602018-10-11 11:24:23 -04007908 @IdRes private static final int[] MEDIA_BUTTON_IDS = {
7909 R.id.action0,
7910 R.id.action1,
7911 R.id.action2,
7912 R.id.action3,
7913 R.id.action4,
7914 };
Dan Sandler842dd772014-05-15 09:36:47 -04007915
7916 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07007917 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04007918
7919 public MediaStyle() {
7920 }
7921
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007922 /**
7923 * @deprecated use {@code MediaStyle()}.
7924 */
7925 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04007926 public MediaStyle(Builder builder) {
7927 setBuilder(builder);
7928 }
7929
7930 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007931 * 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 -04007932 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007933 *
7934 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04007935 */
7936 public MediaStyle setShowActionsInCompactView(int...actions) {
7937 mActionsToShowInCompact = actions;
7938 return this;
7939 }
7940
7941 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07007942 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
7943 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04007944 */
Jeff Browndba34ba2014-06-24 20:46:03 -07007945 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04007946 mToken = token;
7947 return this;
7948 }
7949
Christoph Studer4600f9b2014-07-22 22:44:43 +02007950 /**
7951 * @hide
7952 */
Dan Sandler842dd772014-05-15 09:36:47 -04007953 @Override
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007954 @UnsupportedAppUsage
Dan Sandler842dd772014-05-15 09:36:47 -04007955 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02007956 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007957 if (wip.category == null) {
7958 wip.category = Notification.CATEGORY_TRANSPORT;
7959 }
Dan Sandler842dd772014-05-15 09:36:47 -04007960 return wip;
7961 }
7962
Christoph Studer4600f9b2014-07-22 22:44:43 +02007963 /**
7964 * @hide
7965 */
7966 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007967 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007968 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007969 }
7970
7971 /**
7972 * @hide
7973 */
7974 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007975 public RemoteViews makeBigContentView() {
7976 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007977 }
7978
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007979 /**
7980 * @hide
7981 */
7982 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007983 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007984 RemoteViews expanded = makeMediaBigContentView();
7985 return expanded != null ? expanded : makeMediaContentView();
7986 }
7987
Dan Sandler842dd772014-05-15 09:36:47 -04007988 /** @hide */
7989 @Override
7990 public void addExtras(Bundle extras) {
7991 super.addExtras(extras);
7992
7993 if (mToken != null) {
7994 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
7995 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01007996 if (mActionsToShowInCompact != null) {
7997 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
7998 }
Dan Sandler842dd772014-05-15 09:36:47 -04007999 }
8000
Christoph Studer4600f9b2014-07-22 22:44:43 +02008001 /**
8002 * @hide
8003 */
8004 @Override
8005 protected void restoreFromExtras(Bundle extras) {
8006 super.restoreFromExtras(extras);
8007
8008 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
8009 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
8010 }
8011 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
8012 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
8013 }
8014 }
8015
Julia Reynolds7217dc92018-03-07 12:12:09 -05008016 /**
8017 * @hide
8018 */
8019 @Override
8020 public boolean areNotificationsVisiblyDifferent(Style other) {
8021 if (other == null || getClass() != other.getClass()) {
8022 return true;
8023 }
8024 // All fields to compare are on the Notification object
8025 return false;
8026 }
8027
Gus Prevas9cc96602018-10-11 11:24:23 -04008028 private void bindMediaActionButton(RemoteViews container, @IdRes int buttonId,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008029 Action action, StandardTemplateParams p) {
Dan Sandler842dd772014-05-15 09:36:47 -04008030 final boolean tombstone = (action.actionIntent == null);
Gus Prevas9cc96602018-10-11 11:24:23 -04008031 container.setViewVisibility(buttonId, View.VISIBLE);
8032 container.setImageViewIcon(buttonId, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07008033
8034 // If the action buttons should not be tinted, then just use the default
8035 // notification color. Otherwise, just use the passed-in color.
Gus Prevas9cc96602018-10-11 11:24:23 -04008036 Resources resources = mBuilder.mContext.getResources();
8037 Configuration currentConfig = resources.getConfiguration();
Lucas Dupinf03e7522018-06-25 16:21:13 -07008038 boolean inNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
8039 == Configuration.UI_MODE_NIGHT_YES;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008040 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized(p)
8041 ? getActionColor(p)
Lucas Dupina291d192018-06-07 13:59:42 -07008042 : ContrastColorUtil.resolveColor(mBuilder.mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07008043 Notification.COLOR_DEFAULT, inNightMode);
Anthony Chenad4d1582017-04-10 16:07:58 -07008044
Gus Prevas9cc96602018-10-11 11:24:23 -04008045 container.setDrawableTint(buttonId, false, tintColor,
Sunny Goyal5b153922017-09-21 21:00:36 -07008046 PorterDuff.Mode.SRC_ATOP);
Gus Prevas9cc96602018-10-11 11:24:23 -04008047
8048 final TypedArray typedArray = mBuilder.mContext.obtainStyledAttributes(
8049 new int[]{ android.R.attr.colorControlHighlight });
8050 int rippleAlpha = Color.alpha(typedArray.getColor(0, 0));
8051 typedArray.recycle();
8052 int rippleColor = Color.argb(rippleAlpha, Color.red(tintColor), Color.green(tintColor),
8053 Color.blue(tintColor));
8054 container.setRippleDrawableColor(buttonId, ColorStateList.valueOf(rippleColor));
8055
Dan Sandler842dd772014-05-15 09:36:47 -04008056 if (!tombstone) {
Gus Prevas9cc96602018-10-11 11:24:23 -04008057 container.setOnClickPendingIntent(buttonId, action.actionIntent);
Dan Sandler842dd772014-05-15 09:36:47 -04008058 }
Gus Prevas9cc96602018-10-11 11:24:23 -04008059 container.setContentDescription(buttonId, action.title);
Dan Sandler842dd772014-05-15 09:36:47 -04008060 }
8061
8062 private RemoteViews makeMediaContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008063 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).fillTextsFrom(
8064 mBuilder);
Dan Sandler842dd772014-05-15 09:36:47 -04008065 RemoteViews view = mBuilder.applyStandardTemplate(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008066 R.layout.notification_template_material_media, p,
Selim Cinek384804b2018-04-18 14:31:07 +08008067 null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04008068
8069 final int numActions = mBuilder.mActions.size();
Gus Prevas9cc96602018-10-11 11:24:23 -04008070 final int numActionsToShow = mActionsToShowInCompact == null
Dan Sandler842dd772014-05-15 09:36:47 -04008071 ? 0
8072 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Gus Prevas9cc96602018-10-11 11:24:23 -04008073 if (numActionsToShow > numActions) {
8074 throw new IllegalArgumentException(String.format(
8075 "setShowActionsInCompactView: action %d out of bounds (max %d)",
8076 numActions, numActions - 1));
8077 }
8078 for (int i = 0; i < MAX_MEDIA_BUTTONS_IN_COMPACT; i++) {
8079 if (i < numActionsToShow) {
Dan Sandler842dd772014-05-15 09:36:47 -04008080 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008081 bindMediaActionButton(view, MEDIA_BUTTON_IDS[i], action, p);
Gus Prevas9cc96602018-10-11 11:24:23 -04008082 } else {
8083 view.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04008084 }
8085 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08008086 handleImage(view);
8087 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008088 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07008089 if (mBuilder.mN.hasLargeIcon()) {
Selim Cinek384804b2018-04-18 14:31:07 +08008090 endMargin = R.dimen.notification_media_image_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08008091 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008092 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04008093 return view;
8094 }
8095
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008096 private int getActionColor(StandardTemplateParams p) {
8097 return mBuilder.isColorized(p) ? mBuilder.getPrimaryTextColor(p)
8098 : mBuilder.resolveContrastColor(p);
Selim Cinek99104832017-01-25 14:47:33 -08008099 }
8100
Dan Sandler842dd772014-05-15 09:36:47 -04008101 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008102 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008103 // Dont add an expanded view if there is no more content to be revealed
8104 int actionsInCompact = mActionsToShowInCompact == null
8105 ? 0
8106 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07008107 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008108 return null;
8109 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008110 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).fillTextsFrom(
8111 mBuilder);
Selim Cinek5bf069a2015-11-10 19:14:27 -05008112 RemoteViews big = mBuilder.applyStandardTemplate(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008113 R.layout.notification_template_material_big_media, p , null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04008114
Gus Prevas9cc96602018-10-11 11:24:23 -04008115 for (int i = 0; i < MAX_MEDIA_BUTTONS; i++) {
8116 if (i < actionCount) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008117 bindMediaActionButton(big, MEDIA_BUTTON_IDS[i], mBuilder.mActions.get(i), p);
Gus Prevas9cc96602018-10-11 11:24:23 -04008118 } else {
8119 big.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04008120 }
8121 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05008122 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04008123 return big;
8124 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008125
Selim Cinek5bf069a2015-11-10 19:14:27 -05008126 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07008127 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008128 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
8129 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008130 }
8131 }
8132
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008133 /**
8134 * @hide
8135 */
8136 @Override
8137 protected boolean hasProgress() {
8138 return false;
8139 }
Dan Sandler842dd772014-05-15 09:36:47 -04008140 }
Griff Hazen61a9e862014-05-22 16:05:19 -07008141
Selim Cinek593610c2016-02-16 18:42:57 -08008142 /**
8143 * Notification style for custom views that are decorated by the system
8144 *
8145 * <p>Instead of providing a notification that is completely custom, a developer can set this
8146 * style and still obtain system decorations like the notification header with the expand
8147 * affordance and actions.
8148 *
8149 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8150 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8151 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8152 * corresponding custom views to display.
8153 *
8154 * To use this style with your Notification, feed it to
8155 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8156 * <pre class="prettyprint">
8157 * Notification noti = new Notification.Builder()
8158 * .setSmallIcon(R.drawable.ic_stat_player)
8159 * .setLargeIcon(albumArtBitmap))
8160 * .setCustomContentView(contentView);
8161 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
8162 * .build();
8163 * </pre>
8164 */
8165 public static class DecoratedCustomViewStyle extends Style {
8166
8167 public DecoratedCustomViewStyle() {
8168 }
8169
Selim Cinek593610c2016-02-16 18:42:57 -08008170 /**
8171 * @hide
8172 */
8173 public boolean displayCustomViewInline() {
8174 return true;
8175 }
8176
8177 /**
8178 * @hide
8179 */
8180 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008181 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008182 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
8183 }
8184
8185 /**
8186 * @hide
8187 */
8188 @Override
8189 public RemoteViews makeBigContentView() {
8190 return makeDecoratedBigContentView();
8191 }
8192
8193 /**
8194 * @hide
8195 */
8196 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008197 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008198 return makeDecoratedHeadsUpContentView();
8199 }
8200
Selim Cinek593610c2016-02-16 18:42:57 -08008201 private RemoteViews makeDecoratedHeadsUpContentView() {
8202 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
8203 ? mBuilder.mN.contentView
8204 : mBuilder.mN.headsUpContentView;
8205 if (mBuilder.mActions.size() == 0) {
8206 return makeStandardTemplateWithCustomContent(headsUpContentView);
8207 }
Selim Cinek384804b2018-04-18 14:31:07 +08008208 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008209 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008210 mBuilder.getBigBaseLayoutResource(), result);
8211 buildIntoRemoteViewContent(remoteViews, headsUpContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008212 return remoteViews;
8213 }
8214
Selim Cinek593610c2016-02-16 18:42:57 -08008215 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
Selim Cinek384804b2018-04-18 14:31:07 +08008216 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008217 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08008218 mBuilder.getBaseLayoutResource(), result);
8219 buildIntoRemoteViewContent(remoteViews, customContent, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008220 return remoteViews;
8221 }
8222
Selim Cinek593610c2016-02-16 18:42:57 -08008223 private RemoteViews makeDecoratedBigContentView() {
8224 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
8225 ? mBuilder.mN.contentView
8226 : mBuilder.mN.bigContentView;
8227 if (mBuilder.mActions.size() == 0) {
8228 return makeStandardTemplateWithCustomContent(bigContentView);
8229 }
Selim Cinek384804b2018-04-18 14:31:07 +08008230 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008231 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008232 mBuilder.getBigBaseLayoutResource(), result);
8233 buildIntoRemoteViewContent(remoteViews, bigContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008234 return remoteViews;
8235 }
Selim Cinek247fa012016-02-18 09:50:48 -08008236
8237 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
Selim Cinek384804b2018-04-18 14:31:07 +08008238 RemoteViews customContent, TemplateBindResult result) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08008239 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008240 // Need to clone customContent before adding, because otherwise it can no longer be
8241 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008242 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07008243 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
8244 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Sunny Goyalc12d31c2018-11-12 16:29:18 -08008245 remoteViews.addFlags(RemoteViews.FLAG_REAPPLY_DISALLOWED);
Adrian Roos5081c0d2016-02-26 16:04:19 -08008246 }
Selim Cinek247fa012016-02-18 09:50:48 -08008247 // also update the end margin if there is an image
Selim Cinek384804b2018-04-18 14:31:07 +08008248 Resources resources = mBuilder.mContext.getResources();
8249 int endMargin = resources.getDimensionPixelSize(
Selim Cinek1c72fa02018-04-23 18:00:54 +08008250 R.dimen.notification_content_margin_end) + result.getIconMarginEnd();
Selim Cinek384804b2018-04-18 14:31:07 +08008251 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08008252 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05008253
8254 /**
8255 * @hide
8256 */
8257 @Override
8258 public boolean areNotificationsVisiblyDifferent(Style other) {
8259 if (other == null || getClass() != other.getClass()) {
8260 return true;
8261 }
8262 // Comparison done for all custom RemoteViews, independent of style
8263 return false;
8264 }
Selim Cinek593610c2016-02-16 18:42:57 -08008265 }
8266
Selim Cinek03eb3b72016-02-18 10:39:45 -08008267 /**
8268 * Notification style for media custom views that are decorated by the system
8269 *
8270 * <p>Instead of providing a media notification that is completely custom, a developer can set
8271 * this style and still obtain system decorations like the notification header with the expand
8272 * affordance and actions.
8273 *
8274 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8275 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8276 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8277 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08008278 * <p>
8279 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
8280 * notification by using {@link Notification.Builder#setColorized(boolean)}.
8281 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08008282 * To use this style with your Notification, feed it to
8283 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8284 * <pre class="prettyprint">
8285 * Notification noti = new Notification.Builder()
8286 * .setSmallIcon(R.drawable.ic_stat_player)
8287 * .setLargeIcon(albumArtBitmap))
8288 * .setCustomContentView(contentView);
8289 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
8290 * .setMediaSession(mySession))
8291 * .build();
8292 * </pre>
8293 *
8294 * @see android.app.Notification.DecoratedCustomViewStyle
8295 * @see android.app.Notification.MediaStyle
8296 */
8297 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
8298
8299 public DecoratedMediaCustomViewStyle() {
8300 }
8301
Selim Cinek03eb3b72016-02-18 10:39:45 -08008302 /**
8303 * @hide
8304 */
8305 public boolean displayCustomViewInline() {
8306 return true;
8307 }
8308
8309 /**
8310 * @hide
8311 */
8312 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008313 public RemoteViews makeContentView(boolean increasedHeight) {
8314 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008315 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8316 mBuilder.mN.contentView);
8317 }
8318
8319 /**
8320 * @hide
8321 */
8322 @Override
8323 public RemoteViews makeBigContentView() {
8324 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
8325 ? mBuilder.mN.bigContentView
8326 : mBuilder.mN.contentView;
8327 return makeBigContentViewWithCustomContent(customRemoteView);
8328 }
8329
8330 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
8331 RemoteViews remoteViews = super.makeBigContentView();
8332 if (remoteViews != null) {
8333 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
8334 customRemoteView);
8335 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08008336 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008337 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8338 customRemoteView);
8339 } else {
8340 return null;
8341 }
8342 }
8343
8344 /**
8345 * @hide
8346 */
8347 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008348 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08008349 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
8350 ? mBuilder.mN.headsUpContentView
8351 : mBuilder.mN.contentView;
8352 return makeBigContentViewWithCustomContent(customRemoteView);
8353 }
8354
Julia Reynolds7217dc92018-03-07 12:12:09 -05008355 /**
8356 * @hide
8357 */
8358 @Override
8359 public boolean areNotificationsVisiblyDifferent(Style other) {
8360 if (other == null || getClass() != other.getClass()) {
8361 return true;
8362 }
8363 // Comparison done for all custom RemoteViews, independent of style
8364 return false;
8365 }
8366
Selim Cinek03eb3b72016-02-18 10:39:45 -08008367 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
8368 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08008369 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008370 // Need to clone customContent before adding, because otherwise it can no longer be
8371 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008372 customContent = customContent.clone();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008373 customContent.overrideTextColors(mBuilder.getPrimaryTextColor(mBuilder.mParams));
Selim Cinekf91017e2016-03-14 12:25:09 -07008374 remoteViews.removeAllViews(id);
8375 remoteViews.addView(id, customContent);
Sunny Goyalc12d31c2018-11-12 16:29:18 -08008376 remoteViews.addFlags(RemoteViews.FLAG_REAPPLY_DISALLOWED);
Adrian Roos5081c0d2016-02-26 16:04:19 -08008377 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08008378 return remoteViews;
8379 }
8380 }
8381
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008382 /**
8383 * Encapsulates the information needed to display a notification as a bubble.
8384 *
8385 * <p>A bubble is used to display app content in a floating window over the existing
8386 * foreground activity. A bubble has a collapsed state represented by an icon,
8387 * {@link BubbleMetadata.Builder#setIcon(Icon)} and an expanded state which is populated
8388 * via {@link BubbleMetadata.Builder#setIntent(PendingIntent)}.</p>
8389 *
8390 * <b>Notifications with a valid and allowed bubble will display in collapsed state
8391 * outside of the notification shade on unlocked devices. When a user interacts with the
8392 * collapsed bubble, the bubble intent will be invoked and displayed.</b>
8393 *
8394 * @see Notification.Builder#setBubbleMetadata(BubbleMetadata)
8395 */
8396 public static final class BubbleMetadata implements Parcelable {
8397
8398 private PendingIntent mPendingIntent;
8399 private CharSequence mTitle;
8400 private Icon mIcon;
8401 private int mDesiredHeight;
8402
8403 private BubbleMetadata(PendingIntent intent, CharSequence title, Icon icon, int height) {
8404 mPendingIntent = intent;
8405 mTitle = title;
8406 mIcon = icon;
8407 mDesiredHeight = height;
8408 }
8409
8410 private BubbleMetadata(Parcel in) {
8411 mPendingIntent = PendingIntent.CREATOR.createFromParcel(in);
8412 mTitle = in.readCharSequence();
8413 mIcon = Icon.CREATOR.createFromParcel(in);
8414 mDesiredHeight = in.readInt();
8415 }
8416
8417 /**
8418 * @return the pending intent used to populate the floating window for this bubble.
8419 */
8420 public PendingIntent getIntent() {
8421 return mPendingIntent;
8422 }
8423
8424 /**
8425 * @return the title that will appear along with the app content defined by
8426 * {@link #getIntent()} for this bubble.
8427 */
8428 public CharSequence getTitle() {
8429 return mTitle;
8430 }
8431
8432 /**
8433 * @return the icon that will be displayed for this bubble when it is collapsed.
8434 */
8435 public Icon getIcon() {
8436 return mIcon;
8437 }
8438
8439 /**
8440 * @return the ideal height for the floating window that app content defined by
8441 * {@link #getIntent()} for this bubble.
8442 */
8443 public int getDesiredHeight() {
8444 return mDesiredHeight;
8445 }
8446
8447 public static final Parcelable.Creator<BubbleMetadata> CREATOR =
8448 new Parcelable.Creator<BubbleMetadata>() {
8449
8450 @Override
8451 public BubbleMetadata createFromParcel(Parcel source) {
8452 return new BubbleMetadata(source);
8453 }
8454
8455 @Override
8456 public BubbleMetadata[] newArray(int size) {
8457 return new BubbleMetadata[size];
8458 }
8459 };
8460
8461 @Override
8462 public int describeContents() {
8463 return 0;
8464 }
8465
8466 @Override
8467 public void writeToParcel(Parcel out, int flags) {
8468 mPendingIntent.writeToParcel(out, 0);
8469 out.writeCharSequence(mTitle);
8470 mIcon.writeToParcel(out, 0);
8471 out.writeInt(mDesiredHeight);
8472 }
8473
8474 /**
8475 * Builder to construct a {@link BubbleMetadata} object.
8476 */
8477 public static class Builder {
8478
8479 private PendingIntent mPendingIntent;
8480 private CharSequence mTitle;
8481 private Icon mIcon;
8482 private int mDesiredHeight;
8483
8484 /**
8485 * Constructs a new builder object.
8486 */
8487 public Builder() {
8488 }
8489
8490 /**
8491 * Sets the intent that will be used when the bubble is expanded. This will display the
8492 * app content in a floating window over the existing foreground activity.
8493 */
8494 public BubbleMetadata.Builder setIntent(PendingIntent intent) {
8495 if (intent == null) {
8496 throw new IllegalArgumentException("Bubble requires non-null pending intent");
8497 }
8498 mPendingIntent = intent;
8499 return this;
8500 }
8501
8502 /**
8503 * Sets the title that will appear along with the app content for this bubble.
8504 *
8505 * <p>A title is required and should expect to fit on a single line and make sense when
8506 * shown with the content defined by {@link #setIntent(PendingIntent)}.</p>
8507 */
8508 public BubbleMetadata.Builder setTitle(CharSequence title) {
8509 if (TextUtils.isEmpty(title)) {
8510 throw new IllegalArgumentException("Bubbles require non-null or empty title");
8511 }
8512 mTitle = title;
8513 return this;
8514 }
8515
8516 /**
8517 * Sets the icon that will represent the bubble when it is collapsed.
8518 *
8519 * <p>An icon is required and should be representative of the content within the bubble.
8520 * If your app produces multiple bubbles, the image should be unique for each of them.
8521 * </p>
8522 */
8523 public BubbleMetadata.Builder setIcon(Icon icon) {
8524 if (icon == null) {
8525 throw new IllegalArgumentException("Bubbles require non-null icon");
8526 }
8527 mIcon = icon;
8528 return this;
8529 }
8530
8531 /**
8532 * Sets the desired height for the app content defined by
8533 * {@link #setIntent(PendingIntent)}, this height may not be respected if there is not
8534 * enough space on the screen or if the provided height is too small to be useful.
8535 */
8536 public BubbleMetadata.Builder setDesiredHeight(int height) {
8537 mDesiredHeight = Math.max(height, 0);
8538 return this;
8539 }
8540
8541 /**
8542 * Creates the {@link BubbleMetadata} defined by this builder.
8543 * <p>Will throw {@link IllegalStateException} if required fields have not been set
8544 * on this builder.</p>
8545 */
8546 public BubbleMetadata build() {
8547 if (mPendingIntent == null) {
8548 throw new IllegalStateException("Must supply pending intent to bubble");
8549 }
8550 if (TextUtils.isEmpty(mTitle)) {
8551 throw new IllegalStateException("Must supply a title for the bubble");
8552 }
8553 if (mIcon == null) {
8554 throw new IllegalStateException("Must supply an icon for the bubble");
8555 }
8556 return new BubbleMetadata(mPendingIntent, mTitle, mIcon, mDesiredHeight);
8557 }
8558 }
8559 }
8560
8561
Christoph Studer4600f9b2014-07-22 22:44:43 +02008562 // When adding a new Style subclass here, don't forget to update
8563 // Builder.getNotificationStyleClass.
8564
Griff Hazen61a9e862014-05-22 16:05:19 -07008565 /**
8566 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
8567 * metadata or change options on a notification builder.
8568 */
8569 public interface Extender {
8570 /**
8571 * Apply this extender to a notification builder.
8572 * @param builder the builder to be modified.
8573 * @return the build object for chaining.
8574 */
8575 public Builder extend(Builder builder);
8576 }
8577
8578 /**
8579 * Helper class to add wearable extensions to notifications.
8580 * <p class="note"> See
8581 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
8582 * for Android Wear</a> for more information on how to use this class.
8583 * <p>
8584 * To create a notification with wearable extensions:
8585 * <ol>
8586 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
8587 * properties.
8588 * <li>Create a {@link android.app.Notification.WearableExtender}.
8589 * <li>Set wearable-specific properties using the
8590 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
8591 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
8592 * notification.
8593 * <li>Post the notification to the notification system with the
8594 * {@code NotificationManager.notify(...)} methods.
8595 * </ol>
8596 *
8597 * <pre class="prettyprint">
8598 * Notification notif = new Notification.Builder(mContext)
8599 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
8600 * .setContentText(subject)
8601 * .setSmallIcon(R.drawable.new_mail)
8602 * .extend(new Notification.WearableExtender()
8603 * .setContentIcon(R.drawable.new_mail))
8604 * .build();
8605 * NotificationManager notificationManger =
8606 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
8607 * notificationManger.notify(0, notif);</pre>
8608 *
8609 * <p>Wearable extensions can be accessed on an existing notification by using the
8610 * {@code WearableExtender(Notification)} constructor,
8611 * and then using the {@code get} methods to access values.
8612 *
8613 * <pre class="prettyprint">
8614 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
8615 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07008616 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008617 */
8618 public static final class WearableExtender implements Extender {
8619 /**
8620 * Sentinel value for an action index that is unset.
8621 */
8622 public static final int UNSET_ACTION_INDEX = -1;
8623
8624 /**
8625 * Size value for use with {@link #setCustomSizePreset} to show this notification with
8626 * default sizing.
8627 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07008628 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07008629 * on their content.
Gus Prevasd7363752018-09-18 14:35:15 -04008630 *
8631 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008632 */
Gus Prevasd7363752018-09-18 14:35:15 -04008633 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008634 public static final int SIZE_DEFAULT = 0;
8635
8636 /**
8637 * Size value for use with {@link #setCustomSizePreset} to show this notification
8638 * with an extra small size.
8639 * <p>This value is only applicable for custom display notifications created using
8640 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008641 *
8642 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008643 */
Gus Prevasd7363752018-09-18 14:35:15 -04008644 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008645 public static final int SIZE_XSMALL = 1;
8646
8647 /**
8648 * Size value for use with {@link #setCustomSizePreset} to show this notification
8649 * with a small size.
8650 * <p>This value is only applicable for custom display notifications created using
8651 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008652 *
8653 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008654 */
Gus Prevasd7363752018-09-18 14:35:15 -04008655 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008656 public static final int SIZE_SMALL = 2;
8657
8658 /**
8659 * Size value for use with {@link #setCustomSizePreset} to show this notification
8660 * with a medium size.
8661 * <p>This value is only applicable for custom display notifications created using
8662 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008663 *
8664 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008665 */
Gus Prevasd7363752018-09-18 14:35:15 -04008666 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008667 public static final int SIZE_MEDIUM = 3;
8668
8669 /**
8670 * Size value for use with {@link #setCustomSizePreset} to show this notification
8671 * with a large size.
8672 * <p>This value is only applicable for custom display notifications created using
8673 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008674 *
8675 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008676 */
Gus Prevasd7363752018-09-18 14:35:15 -04008677 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008678 public static final int SIZE_LARGE = 4;
8679
Griff Hazend5f11f92014-05-27 15:40:09 -07008680 /**
8681 * Size value for use with {@link #setCustomSizePreset} to show this notification
8682 * full screen.
8683 * <p>This value is only applicable for custom display notifications created using
8684 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008685 *
8686 * @deprecated Display intents are no longer supported.
Griff Hazend5f11f92014-05-27 15:40:09 -07008687 */
Gus Prevasd7363752018-09-18 14:35:15 -04008688 @Deprecated
Griff Hazend5f11f92014-05-27 15:40:09 -07008689 public static final int SIZE_FULL_SCREEN = 5;
8690
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008691 /**
8692 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
8693 * short amount of time when this notification is displayed on the screen. This
8694 * is the default value.
Gus Prevasd7363752018-09-18 14:35:15 -04008695 *
8696 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008697 */
Gus Prevasd7363752018-09-18 14:35:15 -04008698 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008699 public static final int SCREEN_TIMEOUT_SHORT = 0;
8700
8701 /**
8702 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
8703 * for a longer amount of time when this notification is displayed on the screen.
Gus Prevasd7363752018-09-18 14:35:15 -04008704 *
8705 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008706 */
Gus Prevasd7363752018-09-18 14:35:15 -04008707 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008708 public static final int SCREEN_TIMEOUT_LONG = -1;
8709
Griff Hazen61a9e862014-05-22 16:05:19 -07008710 /** Notification extra which contains wearable extensions */
8711 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
8712
Pete Gastaf6781d2014-10-07 15:17:05 -04008713 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07008714 private static final String KEY_ACTIONS = "actions";
8715 private static final String KEY_FLAGS = "flags";
8716 private static final String KEY_DISPLAY_INTENT = "displayIntent";
8717 private static final String KEY_PAGES = "pages";
8718 private static final String KEY_BACKGROUND = "background";
8719 private static final String KEY_CONTENT_ICON = "contentIcon";
8720 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
8721 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
8722 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
8723 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
8724 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008725 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04008726 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008727 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07008728
8729 // Flags bitwise-ored to mFlags
8730 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
8731 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
8732 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
8733 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008734 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04008735 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04008736 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07008737
8738 // Default value for flags integer
8739 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
8740
8741 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
8742 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
8743
8744 private ArrayList<Action> mActions = new ArrayList<Action>();
8745 private int mFlags = DEFAULT_FLAGS;
8746 private PendingIntent mDisplayIntent;
8747 private ArrayList<Notification> mPages = new ArrayList<Notification>();
8748 private Bitmap mBackground;
8749 private int mContentIcon;
8750 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
8751 private int mContentActionIndex = UNSET_ACTION_INDEX;
8752 private int mCustomSizePreset = SIZE_DEFAULT;
8753 private int mCustomContentHeight;
8754 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008755 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04008756 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008757 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07008758
8759 /**
8760 * Create a {@link android.app.Notification.WearableExtender} with default
8761 * options.
8762 */
8763 public WearableExtender() {
8764 }
8765
8766 public WearableExtender(Notification notif) {
8767 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
8768 if (wearableBundle != null) {
8769 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
8770 if (actions != null) {
8771 mActions.addAll(actions);
8772 }
8773
8774 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
8775 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
8776
8777 Notification[] pages = getNotificationArrayFromBundle(
8778 wearableBundle, KEY_PAGES);
8779 if (pages != null) {
8780 Collections.addAll(mPages, pages);
8781 }
8782
8783 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
8784 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
8785 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
8786 DEFAULT_CONTENT_ICON_GRAVITY);
8787 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
8788 UNSET_ACTION_INDEX);
8789 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
8790 SIZE_DEFAULT);
8791 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
8792 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008793 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04008794 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008795 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07008796 }
8797 }
8798
8799 /**
8800 * Apply wearable extensions to a notification that is being built. This is typically
8801 * called by the {@link android.app.Notification.Builder#extend} method of
8802 * {@link android.app.Notification.Builder}.
8803 */
8804 @Override
8805 public Notification.Builder extend(Notification.Builder builder) {
8806 Bundle wearableBundle = new Bundle();
8807
8808 if (!mActions.isEmpty()) {
8809 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
8810 }
8811 if (mFlags != DEFAULT_FLAGS) {
8812 wearableBundle.putInt(KEY_FLAGS, mFlags);
8813 }
8814 if (mDisplayIntent != null) {
8815 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
8816 }
8817 if (!mPages.isEmpty()) {
8818 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
8819 new Notification[mPages.size()]));
8820 }
8821 if (mBackground != null) {
8822 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
8823 }
8824 if (mContentIcon != 0) {
8825 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
8826 }
8827 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
8828 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
8829 }
8830 if (mContentActionIndex != UNSET_ACTION_INDEX) {
8831 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
8832 mContentActionIndex);
8833 }
8834 if (mCustomSizePreset != SIZE_DEFAULT) {
8835 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
8836 }
8837 if (mCustomContentHeight != 0) {
8838 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
8839 }
8840 if (mGravity != DEFAULT_GRAVITY) {
8841 wearableBundle.putInt(KEY_GRAVITY, mGravity);
8842 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008843 if (mHintScreenTimeout != 0) {
8844 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
8845 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04008846 if (mDismissalId != null) {
8847 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
8848 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008849 if (mBridgeTag != null) {
8850 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
8851 }
Griff Hazen61a9e862014-05-22 16:05:19 -07008852
8853 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
8854 return builder;
8855 }
8856
8857 @Override
8858 public WearableExtender clone() {
8859 WearableExtender that = new WearableExtender();
8860 that.mActions = new ArrayList<Action>(this.mActions);
8861 that.mFlags = this.mFlags;
8862 that.mDisplayIntent = this.mDisplayIntent;
8863 that.mPages = new ArrayList<Notification>(this.mPages);
8864 that.mBackground = this.mBackground;
8865 that.mContentIcon = this.mContentIcon;
8866 that.mContentIconGravity = this.mContentIconGravity;
8867 that.mContentActionIndex = this.mContentActionIndex;
8868 that.mCustomSizePreset = this.mCustomSizePreset;
8869 that.mCustomContentHeight = this.mCustomContentHeight;
8870 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008871 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04008872 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008873 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07008874 return that;
8875 }
8876
8877 /**
8878 * Add a wearable action to this notification.
8879 *
8880 * <p>When wearable actions are added using this method, the set of actions that
8881 * show on a wearable device splits from devices that only show actions added
8882 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
8883 * of which actions display on different devices.
8884 *
8885 * @param action the action to add to this notification
8886 * @return this object for method chaining
8887 * @see android.app.Notification.Action
8888 */
8889 public WearableExtender addAction(Action action) {
8890 mActions.add(action);
8891 return this;
8892 }
8893
8894 /**
8895 * Adds wearable actions to this notification.
8896 *
8897 * <p>When wearable actions are added using this method, the set of actions that
8898 * show on a wearable device splits from devices that only show actions added
8899 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
8900 * of which actions display on different devices.
8901 *
8902 * @param actions the actions to add to this notification
8903 * @return this object for method chaining
8904 * @see android.app.Notification.Action
8905 */
8906 public WearableExtender addActions(List<Action> actions) {
8907 mActions.addAll(actions);
8908 return this;
8909 }
8910
8911 /**
8912 * Clear all wearable actions present on this builder.
8913 * @return this object for method chaining.
8914 * @see #addAction
8915 */
8916 public WearableExtender clearActions() {
8917 mActions.clear();
8918 return this;
8919 }
8920
8921 /**
8922 * Get the wearable actions present on this notification.
8923 */
8924 public List<Action> getActions() {
8925 return mActions;
8926 }
8927
8928 /**
8929 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07008930 * this notification. The {@link PendingIntent} provided should be for an activity.
8931 *
8932 * <pre class="prettyprint">
8933 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
8934 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
8935 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
8936 * Notification notif = new Notification.Builder(context)
8937 * .extend(new Notification.WearableExtender()
8938 * .setDisplayIntent(displayPendingIntent)
8939 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
8940 * .build();</pre>
8941 *
8942 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07008943 * should have an empty task affinity. It is also recommended to use the device
8944 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07008945 *
8946 * <p>Example AndroidManifest.xml entry:
8947 * <pre class="prettyprint">
8948 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
8949 * android:exported=&quot;true&quot;
8950 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07008951 * android:taskAffinity=&quot;&quot;
8952 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008953 *
8954 * @param intent the {@link PendingIntent} for an activity
8955 * @return this object for method chaining
8956 * @see android.app.Notification.WearableExtender#getDisplayIntent
Gus Prevasd7363752018-09-18 14:35:15 -04008957 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008958 */
Gus Prevasd7363752018-09-18 14:35:15 -04008959 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008960 public WearableExtender setDisplayIntent(PendingIntent intent) {
8961 mDisplayIntent = intent;
8962 return this;
8963 }
8964
8965 /**
8966 * Get the intent to launch inside of an activity view when displaying this
8967 * notification. This {@code PendingIntent} should be for an activity.
Gus Prevasd7363752018-09-18 14:35:15 -04008968 *
8969 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008970 */
Gus Prevasd7363752018-09-18 14:35:15 -04008971 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008972 public PendingIntent getDisplayIntent() {
8973 return mDisplayIntent;
8974 }
8975
8976 /**
8977 * Add an additional page of content to display with this notification. The current
8978 * notification forms the first page, and pages added using this function form
8979 * subsequent pages. This field can be used to separate a notification into multiple
8980 * sections.
8981 *
8982 * @param page the notification to add as another page
8983 * @return this object for method chaining
8984 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04008985 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008986 */
Gus Prevasd7363752018-09-18 14:35:15 -04008987 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008988 public WearableExtender addPage(Notification page) {
8989 mPages.add(page);
8990 return this;
8991 }
8992
8993 /**
8994 * Add additional pages of content to display with this notification. The current
8995 * notification forms the first page, and pages added using this function form
8996 * subsequent pages. This field can be used to separate a notification into multiple
8997 * sections.
8998 *
8999 * @param pages a list of notifications
9000 * @return this object for method chaining
9001 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04009002 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009003 */
Gus Prevasd7363752018-09-18 14:35:15 -04009004 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009005 public WearableExtender addPages(List<Notification> pages) {
9006 mPages.addAll(pages);
9007 return this;
9008 }
9009
9010 /**
9011 * Clear all additional pages present on this builder.
9012 * @return this object for method chaining.
9013 * @see #addPage
Gus Prevasd7363752018-09-18 14:35:15 -04009014 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009015 */
Gus Prevasd7363752018-09-18 14:35:15 -04009016 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009017 public WearableExtender clearPages() {
9018 mPages.clear();
9019 return this;
9020 }
9021
9022 /**
9023 * Get the array of additional pages of content for displaying this notification. The
9024 * current notification forms the first page, and elements within this array form
9025 * subsequent pages. This field can be used to separate a notification into multiple
9026 * sections.
9027 * @return the pages for this notification
Gus Prevasd7363752018-09-18 14:35:15 -04009028 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009029 */
Gus Prevasd7363752018-09-18 14:35:15 -04009030 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009031 public List<Notification> getPages() {
9032 return mPages;
9033 }
9034
9035 /**
9036 * Set a background image to be displayed behind the notification content.
9037 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
9038 * will work with any notification style.
9039 *
9040 * @param background the background bitmap
9041 * @return this object for method chaining
9042 * @see android.app.Notification.WearableExtender#getBackground
Gus Prevasd7363752018-09-18 14:35:15 -04009043 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009044 */
Gus Prevasd7363752018-09-18 14:35:15 -04009045 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009046 public WearableExtender setBackground(Bitmap background) {
9047 mBackground = background;
9048 return this;
9049 }
9050
9051 /**
9052 * Get a background image to be displayed behind the notification content.
9053 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
9054 * will work with any notification style.
9055 *
9056 * @return the background image
9057 * @see android.app.Notification.WearableExtender#setBackground
Gus Prevasd7363752018-09-18 14:35:15 -04009058 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009059 */
Gus Prevasd7363752018-09-18 14:35:15 -04009060 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009061 public Bitmap getBackground() {
9062 return mBackground;
9063 }
9064
9065 /**
9066 * Set an icon that goes with the content of this notification.
9067 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009068 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009069 public WearableExtender setContentIcon(int icon) {
9070 mContentIcon = icon;
9071 return this;
9072 }
9073
9074 /**
9075 * Get an icon that goes with the content of this notification.
9076 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009077 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009078 public int getContentIcon() {
9079 return mContentIcon;
9080 }
9081
9082 /**
9083 * Set the gravity that the content icon should have within the notification display.
9084 * Supported values include {@link android.view.Gravity#START} and
9085 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
9086 * @see #setContentIcon
9087 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009088 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009089 public WearableExtender setContentIconGravity(int contentIconGravity) {
9090 mContentIconGravity = contentIconGravity;
9091 return this;
9092 }
9093
9094 /**
9095 * Get the gravity that the content icon should have within the notification display.
9096 * Supported values include {@link android.view.Gravity#START} and
9097 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
9098 * @see #getContentIcon
9099 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009100 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009101 public int getContentIconGravity() {
9102 return mContentIconGravity;
9103 }
9104
9105 /**
Gus Prevasd7363752018-09-18 14:35:15 -04009106 * Set an action from this notification's actions as the primary action. If the action has a
9107 * {@link RemoteInput} associated with it, shortcuts to the options for that input are shown
9108 * directly on the notification.
Griff Hazen14f57992014-05-26 09:07:14 -07009109 *
Gus Prevasd7363752018-09-18 14:35:15 -04009110 * @param actionIndex The index of the primary action.
Griff Hazen14f57992014-05-26 09:07:14 -07009111 * If wearable actions were added to the main notification, this index
9112 * will apply to that list, otherwise it will apply to the regular
9113 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07009114 */
9115 public WearableExtender setContentAction(int actionIndex) {
9116 mContentActionIndex = actionIndex;
9117 return this;
9118 }
9119
9120 /**
Gus Prevasd7363752018-09-18 14:35:15 -04009121 * Get the index of the notification action, if any, that was specified as the primary
9122 * action.
Griff Hazen14f57992014-05-26 09:07:14 -07009123 *
9124 * <p>If wearable specific actions were added to the main notification, this index will
9125 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07009126 *
9127 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07009128 */
9129 public int getContentAction() {
9130 return mContentActionIndex;
9131 }
9132
9133 /**
9134 * Set the gravity that this notification should have within the available viewport space.
9135 * Supported values include {@link android.view.Gravity#TOP},
9136 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
9137 * The default value is {@link android.view.Gravity#BOTTOM}.
9138 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009139 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009140 public WearableExtender setGravity(int gravity) {
9141 mGravity = gravity;
9142 return this;
9143 }
9144
9145 /**
9146 * Get the gravity that this notification should have within the available viewport space.
9147 * Supported values include {@link android.view.Gravity#TOP},
9148 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
9149 * The default value is {@link android.view.Gravity#BOTTOM}.
9150 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009151 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009152 public int getGravity() {
9153 return mGravity;
9154 }
9155
9156 /**
9157 * Set the custom size preset for the display of this notification out of the available
9158 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
9159 * {@link #SIZE_LARGE}.
9160 * <p>Some custom size presets are only applicable for custom display notifications created
9161 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
9162 * documentation for the preset in question. See also
9163 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
9164 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009165 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009166 public WearableExtender setCustomSizePreset(int sizePreset) {
9167 mCustomSizePreset = sizePreset;
9168 return this;
9169 }
9170
9171 /**
9172 * Get the custom size preset for the display of this notification out of the available
9173 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
9174 * {@link #SIZE_LARGE}.
9175 * <p>Some custom size presets are only applicable for custom display notifications created
9176 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
9177 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
9178 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009179 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009180 public int getCustomSizePreset() {
9181 return mCustomSizePreset;
9182 }
9183
9184 /**
9185 * Set the custom height in pixels for the display of this notification's content.
9186 * <p>This option is only available for custom display notifications created
9187 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
9188 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
9189 * {@link #getCustomContentHeight}.
9190 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009191 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009192 public WearableExtender setCustomContentHeight(int height) {
9193 mCustomContentHeight = height;
9194 return this;
9195 }
9196
9197 /**
9198 * Get the custom height in pixels for the display of this notification's content.
9199 * <p>This option is only available for custom display notifications created
9200 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
9201 * {@link #setCustomContentHeight}.
9202 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009203 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009204 public int getCustomContentHeight() {
9205 return mCustomContentHeight;
9206 }
9207
9208 /**
9209 * Set whether the scrolling position for the contents of this notification should start
9210 * at the bottom of the contents instead of the top when the contents are too long to
9211 * display within the screen. Default is false (start scroll at the top).
9212 */
9213 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
9214 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
9215 return this;
9216 }
9217
9218 /**
9219 * Get whether the scrolling position for the contents of this notification should start
9220 * at the bottom of the contents instead of the top when the contents are too long to
9221 * display within the screen. Default is false (start scroll at the top).
9222 */
9223 public boolean getStartScrollBottom() {
9224 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
9225 }
9226
9227 /**
9228 * Set whether the content intent is available when the wearable device is not connected
9229 * to a companion device. The user can still trigger this intent when the wearable device
9230 * is offline, but a visual hint will indicate that the content intent may not be available.
9231 * Defaults to true.
9232 */
9233 public WearableExtender setContentIntentAvailableOffline(
9234 boolean contentIntentAvailableOffline) {
9235 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
9236 return this;
9237 }
9238
9239 /**
9240 * Get whether the content intent is available when the wearable device is not connected
9241 * to a companion device. The user can still trigger this intent when the wearable device
9242 * is offline, but a visual hint will indicate that the content intent may not be available.
9243 * Defaults to true.
9244 */
9245 public boolean getContentIntentAvailableOffline() {
9246 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
9247 }
9248
9249 /**
9250 * Set a hint that this notification's icon should not be displayed.
9251 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
9252 * @return this object for method chaining
9253 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009254 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009255 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
9256 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
9257 return this;
9258 }
9259
9260 /**
9261 * Get a hint that this notification's icon should not be displayed.
9262 * @return {@code true} if this icon should not be displayed, false otherwise.
9263 * The default value is {@code false} if this was never set.
9264 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009265 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009266 public boolean getHintHideIcon() {
9267 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
9268 }
9269
9270 /**
9271 * Set a visual hint that only the background image of this notification should be
9272 * displayed, and other semantic content should be hidden. This hint is only applicable
9273 * to sub-pages added using {@link #addPage}.
9274 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009275 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009276 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
9277 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
9278 return this;
9279 }
9280
9281 /**
9282 * Get a visual hint that only the background image of this notification should be
9283 * displayed, and other semantic content should be hidden. This hint is only applicable
9284 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
9285 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009286 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009287 public boolean getHintShowBackgroundOnly() {
9288 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
9289 }
9290
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009291 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009292 * Set a hint that this notification's background should not be clipped if possible,
9293 * and should instead be resized to fully display on the screen, retaining the aspect
9294 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009295 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
9296 * @return this object for method chaining
9297 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009298 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009299 public WearableExtender setHintAvoidBackgroundClipping(
9300 boolean hintAvoidBackgroundClipping) {
9301 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
9302 return this;
9303 }
9304
9305 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009306 * Get a hint that this notification's background should not be clipped if possible,
9307 * and should instead be resized to fully display on the screen, retaining the aspect
9308 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009309 * @return {@code true} if it's ok if the background is clipped on the screen, false
9310 * otherwise. The default value is {@code false} if this was never set.
9311 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009312 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009313 public boolean getHintAvoidBackgroundClipping() {
9314 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
9315 }
9316
9317 /**
9318 * Set a hint that the screen should remain on for at least this duration when
9319 * this notification is displayed on the screen.
9320 * @param timeout The requested screen timeout in milliseconds. Can also be either
9321 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9322 * @return this object for method chaining
9323 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009324 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009325 public WearableExtender setHintScreenTimeout(int timeout) {
9326 mHintScreenTimeout = timeout;
9327 return this;
9328 }
9329
9330 /**
9331 * Get the duration, in milliseconds, that the screen should remain on for
9332 * when this notification is displayed.
9333 * @return the duration in milliseconds if > 0, or either one of the sentinel values
9334 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9335 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009336 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009337 public int getHintScreenTimeout() {
9338 return mHintScreenTimeout;
9339 }
9340
Alex Hills9ab3a232016-04-05 14:54:56 -04009341 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04009342 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
9343 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9344 * qr codes, as well as other simple black-and-white tickets.
9345 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
9346 * @return this object for method chaining
Gus Prevasd7363752018-09-18 14:35:15 -04009347 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009348 */
Gus Prevasd7363752018-09-18 14:35:15 -04009349 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009350 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
9351 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
9352 return this;
9353 }
9354
9355 /**
9356 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
9357 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9358 * qr codes, as well as other simple black-and-white tickets.
9359 * @return {@code true} if it should be displayed in ambient, false otherwise
9360 * otherwise. The default value is {@code false} if this was never set.
Gus Prevasd7363752018-09-18 14:35:15 -04009361 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009362 */
Gus Prevasd7363752018-09-18 14:35:15 -04009363 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009364 public boolean getHintAmbientBigPicture() {
9365 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
9366 }
9367
9368 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04009369 * Set a hint that this notification's content intent will launch an {@link Activity}
9370 * directly, telling the platform that it can generate the appropriate transitions.
9371 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
9372 * an activity and transitions should be generated, false otherwise.
9373 * @return this object for method chaining
9374 */
9375 public WearableExtender setHintContentIntentLaunchesActivity(
9376 boolean hintContentIntentLaunchesActivity) {
9377 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
9378 return this;
9379 }
9380
9381 /**
9382 * Get a hint that this notification's content intent will launch an {@link Activity}
9383 * directly, telling the platform that it can generate the appropriate transitions
9384 * @return {@code true} if the content intent will launch an activity and transitions should
9385 * be generated, false otherwise. The default value is {@code false} if this was never set.
9386 */
9387 public boolean getHintContentIntentLaunchesActivity() {
9388 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
9389 }
9390
Nadia Benbernou948627e2016-04-14 14:41:08 -04009391 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009392 * Sets the dismissal id for this notification. If a notification is posted with a
9393 * dismissal id, then when that notification is canceled, notifications on other wearables
9394 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04009395 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009396 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04009397 * @param dismissalId the dismissal id of the notification.
9398 * @return this object for method chaining
9399 */
9400 public WearableExtender setDismissalId(String dismissalId) {
9401 mDismissalId = dismissalId;
9402 return this;
9403 }
9404
9405 /**
9406 * Returns the dismissal id of the notification.
9407 * @return the dismissal id of the notification or null if it has not been set.
9408 */
9409 public String getDismissalId() {
9410 return mDismissalId;
9411 }
9412
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009413 /**
9414 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
9415 * posted from a phone to provide finer-grained control on what notifications are bridged
9416 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
9417 * Features to Notifications</a> for more information.
9418 * @param bridgeTag the bridge tag of the notification.
9419 * @return this object for method chaining
9420 */
9421 public WearableExtender setBridgeTag(String bridgeTag) {
9422 mBridgeTag = bridgeTag;
9423 return this;
9424 }
9425
9426 /**
9427 * Returns the bridge tag of the notification.
9428 * @return the bridge tag or null if not present.
9429 */
9430 public String getBridgeTag() {
9431 return mBridgeTag;
9432 }
9433
Griff Hazen61a9e862014-05-22 16:05:19 -07009434 private void setFlag(int mask, boolean value) {
9435 if (value) {
9436 mFlags |= mask;
9437 } else {
9438 mFlags &= ~mask;
9439 }
9440 }
9441 }
9442
9443 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009444 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
9445 * with car extensions:
9446 *
9447 * <ol>
9448 * <li>Create an {@link Notification.Builder}, setting any desired
9449 * properties.
9450 * <li>Create a {@link CarExtender}.
9451 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
9452 * {@link CarExtender}.
9453 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9454 * to apply the extensions to a notification.
9455 * </ol>
9456 *
9457 * <pre class="prettyprint">
9458 * Notification notification = new Notification.Builder(context)
9459 * ...
9460 * .extend(new CarExtender()
9461 * .set*(...))
9462 * .build();
9463 * </pre>
9464 *
9465 * <p>Car extensions can be accessed on an existing notification by using the
9466 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
9467 * to access values.
9468 */
9469 public static final class CarExtender implements Extender {
9470 private static final String TAG = "CarExtender";
9471
9472 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
9473 private static final String EXTRA_LARGE_ICON = "large_icon";
9474 private static final String EXTRA_CONVERSATION = "car_conversation";
9475 private static final String EXTRA_COLOR = "app_color";
9476
9477 private Bitmap mLargeIcon;
9478 private UnreadConversation mUnreadConversation;
9479 private int mColor = Notification.COLOR_DEFAULT;
9480
9481 /**
9482 * Create a {@link CarExtender} with default options.
9483 */
9484 public CarExtender() {
9485 }
9486
9487 /**
9488 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
9489 *
9490 * @param notif The notification from which to copy options.
9491 */
9492 public CarExtender(Notification notif) {
9493 Bundle carBundle = notif.extras == null ?
9494 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
9495 if (carBundle != null) {
9496 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
9497 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
9498
9499 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
9500 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
9501 }
9502 }
9503
9504 /**
9505 * Apply car extensions to a notification that is being built. This is typically called by
9506 * the {@link Notification.Builder#extend(Notification.Extender)}
9507 * method of {@link Notification.Builder}.
9508 */
9509 @Override
9510 public Notification.Builder extend(Notification.Builder builder) {
9511 Bundle carExtensions = new Bundle();
9512
9513 if (mLargeIcon != null) {
9514 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
9515 }
9516 if (mColor != Notification.COLOR_DEFAULT) {
9517 carExtensions.putInt(EXTRA_COLOR, mColor);
9518 }
9519
9520 if (mUnreadConversation != null) {
9521 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
9522 carExtensions.putBundle(EXTRA_CONVERSATION, b);
9523 }
9524
9525 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
9526 return builder;
9527 }
9528
9529 /**
9530 * Sets the accent color to use when Android Auto presents the notification.
9531 *
9532 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
9533 * to accent the displayed notification. However, not all colors are acceptable in an
9534 * automotive setting. This method can be used to override the color provided in the
9535 * notification in such a situation.
9536 */
Tor Norbye80756e32015-03-02 09:39:27 -08009537 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009538 mColor = color;
9539 return this;
9540 }
9541
9542 /**
9543 * Gets the accent color.
9544 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04009545 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009546 */
Tor Norbye80756e32015-03-02 09:39:27 -08009547 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009548 public int getColor() {
9549 return mColor;
9550 }
9551
9552 /**
9553 * Sets the large icon of the car notification.
9554 *
9555 * If no large icon is set in the extender, Android Auto will display the icon
9556 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
9557 *
9558 * @param largeIcon The large icon to use in the car notification.
9559 * @return This object for method chaining.
9560 */
9561 public CarExtender setLargeIcon(Bitmap largeIcon) {
9562 mLargeIcon = largeIcon;
9563 return this;
9564 }
9565
9566 /**
9567 * Gets the large icon used in this car notification, or null if no icon has been set.
9568 *
9569 * @return The large icon for the car notification.
9570 * @see CarExtender#setLargeIcon
9571 */
9572 public Bitmap getLargeIcon() {
9573 return mLargeIcon;
9574 }
9575
9576 /**
9577 * Sets the unread conversation in a message notification.
9578 *
9579 * @param unreadConversation The unread part of the conversation this notification conveys.
9580 * @return This object for method chaining.
9581 */
9582 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
9583 mUnreadConversation = unreadConversation;
9584 return this;
9585 }
9586
9587 /**
9588 * Returns the unread conversation conveyed by this notification.
9589 * @see #setUnreadConversation(UnreadConversation)
9590 */
9591 public UnreadConversation getUnreadConversation() {
9592 return mUnreadConversation;
9593 }
9594
9595 /**
9596 * A class which holds the unread messages from a conversation.
9597 */
9598 public static class UnreadConversation {
9599 private static final String KEY_AUTHOR = "author";
9600 private static final String KEY_TEXT = "text";
9601 private static final String KEY_MESSAGES = "messages";
9602 private static final String KEY_REMOTE_INPUT = "remote_input";
9603 private static final String KEY_ON_REPLY = "on_reply";
9604 private static final String KEY_ON_READ = "on_read";
9605 private static final String KEY_PARTICIPANTS = "participants";
9606 private static final String KEY_TIMESTAMP = "timestamp";
9607
9608 private final String[] mMessages;
9609 private final RemoteInput mRemoteInput;
9610 private final PendingIntent mReplyPendingIntent;
9611 private final PendingIntent mReadPendingIntent;
9612 private final String[] mParticipants;
9613 private final long mLatestTimestamp;
9614
9615 UnreadConversation(String[] messages, RemoteInput remoteInput,
9616 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
9617 String[] participants, long latestTimestamp) {
9618 mMessages = messages;
9619 mRemoteInput = remoteInput;
9620 mReadPendingIntent = readPendingIntent;
9621 mReplyPendingIntent = replyPendingIntent;
9622 mParticipants = participants;
9623 mLatestTimestamp = latestTimestamp;
9624 }
9625
9626 /**
9627 * Gets the list of messages conveyed by this notification.
9628 */
9629 public String[] getMessages() {
9630 return mMessages;
9631 }
9632
9633 /**
9634 * Gets the remote input that will be used to convey the response to a message list, or
9635 * null if no such remote input exists.
9636 */
9637 public RemoteInput getRemoteInput() {
9638 return mRemoteInput;
9639 }
9640
9641 /**
9642 * Gets the pending intent that will be triggered when the user replies to this
9643 * notification.
9644 */
9645 public PendingIntent getReplyPendingIntent() {
9646 return mReplyPendingIntent;
9647 }
9648
9649 /**
9650 * Gets the pending intent that Android Auto will send after it reads aloud all messages
9651 * in this object's message list.
9652 */
9653 public PendingIntent getReadPendingIntent() {
9654 return mReadPendingIntent;
9655 }
9656
9657 /**
9658 * Gets the participants in the conversation.
9659 */
9660 public String[] getParticipants() {
9661 return mParticipants;
9662 }
9663
9664 /**
9665 * Gets the firs participant in the conversation.
9666 */
9667 public String getParticipant() {
9668 return mParticipants.length > 0 ? mParticipants[0] : null;
9669 }
9670
9671 /**
9672 * Gets the timestamp of the conversation.
9673 */
9674 public long getLatestTimestamp() {
9675 return mLatestTimestamp;
9676 }
9677
9678 Bundle getBundleForUnreadConversation() {
9679 Bundle b = new Bundle();
9680 String author = null;
9681 if (mParticipants != null && mParticipants.length > 1) {
9682 author = mParticipants[0];
9683 }
9684 Parcelable[] messages = new Parcelable[mMessages.length];
9685 for (int i = 0; i < messages.length; i++) {
9686 Bundle m = new Bundle();
9687 m.putString(KEY_TEXT, mMessages[i]);
9688 m.putString(KEY_AUTHOR, author);
9689 messages[i] = m;
9690 }
9691 b.putParcelableArray(KEY_MESSAGES, messages);
9692 if (mRemoteInput != null) {
9693 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
9694 }
9695 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
9696 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
9697 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
9698 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
9699 return b;
9700 }
9701
9702 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
9703 if (b == null) {
9704 return null;
9705 }
9706 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
9707 String[] messages = null;
9708 if (parcelableMessages != null) {
9709 String[] tmp = new String[parcelableMessages.length];
9710 boolean success = true;
9711 for (int i = 0; i < tmp.length; i++) {
9712 if (!(parcelableMessages[i] instanceof Bundle)) {
9713 success = false;
9714 break;
9715 }
9716 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
9717 if (tmp[i] == null) {
9718 success = false;
9719 break;
9720 }
9721 }
9722 if (success) {
9723 messages = tmp;
9724 } else {
9725 return null;
9726 }
9727 }
9728
9729 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
9730 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
9731
9732 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
9733
9734 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
9735 if (participants == null || participants.length != 1) {
9736 return null;
9737 }
9738
9739 return new UnreadConversation(messages,
9740 remoteInput,
9741 onReply,
9742 onRead,
9743 participants, b.getLong(KEY_TIMESTAMP));
9744 }
9745 };
9746
9747 /**
9748 * Builder class for {@link CarExtender.UnreadConversation} objects.
9749 */
9750 public static class Builder {
9751 private final List<String> mMessages = new ArrayList<String>();
9752 private final String mParticipant;
9753 private RemoteInput mRemoteInput;
9754 private PendingIntent mReadPendingIntent;
9755 private PendingIntent mReplyPendingIntent;
9756 private long mLatestTimestamp;
9757
9758 /**
9759 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
9760 *
9761 * @param name The name of the other participant in the conversation.
9762 */
9763 public Builder(String name) {
9764 mParticipant = name;
9765 }
9766
9767 /**
9768 * Appends a new unread message to the list of messages for this conversation.
9769 *
9770 * The messages should be added from oldest to newest.
9771 *
9772 * @param message The text of the new unread message.
9773 * @return This object for method chaining.
9774 */
9775 public Builder addMessage(String message) {
9776 mMessages.add(message);
9777 return this;
9778 }
9779
9780 /**
9781 * Sets the pending intent and remote input which will convey the reply to this
9782 * notification.
9783 *
9784 * @param pendingIntent The pending intent which will be triggered on a reply.
9785 * @param remoteInput The remote input parcelable which will carry the reply.
9786 * @return This object for method chaining.
9787 *
9788 * @see CarExtender.UnreadConversation#getRemoteInput
9789 * @see CarExtender.UnreadConversation#getReplyPendingIntent
9790 */
9791 public Builder setReplyAction(
9792 PendingIntent pendingIntent, RemoteInput remoteInput) {
9793 mRemoteInput = remoteInput;
9794 mReplyPendingIntent = pendingIntent;
9795
9796 return this;
9797 }
9798
9799 /**
9800 * Sets the pending intent that will be sent once the messages in this notification
9801 * are read.
9802 *
9803 * @param pendingIntent The pending intent to use.
9804 * @return This object for method chaining.
9805 */
9806 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
9807 mReadPendingIntent = pendingIntent;
9808 return this;
9809 }
9810
9811 /**
9812 * Sets the timestamp of the most recent message in an unread conversation.
9813 *
9814 * If a messaging notification has been posted by your application and has not
9815 * yet been cancelled, posting a later notification with the same id and tag
9816 * but without a newer timestamp may result in Android Auto not displaying a
9817 * heads up notification for the later notification.
9818 *
9819 * @param timestamp The timestamp of the most recent message in the conversation.
9820 * @return This object for method chaining.
9821 */
9822 public Builder setLatestTimestamp(long timestamp) {
9823 mLatestTimestamp = timestamp;
9824 return this;
9825 }
9826
9827 /**
9828 * Builds a new unread conversation object.
9829 *
9830 * @return The new unread conversation object.
9831 */
9832 public UnreadConversation build() {
9833 String[] messages = mMessages.toArray(new String[mMessages.size()]);
9834 String[] participants = { mParticipant };
9835 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
9836 mReadPendingIntent, participants, mLatestTimestamp);
9837 }
9838 }
9839 }
9840
9841 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009842 * <p>Helper class to add Android TV extensions to notifications. To create a notification
9843 * with a TV extension:
9844 *
9845 * <ol>
9846 * <li>Create an {@link Notification.Builder}, setting any desired properties.
9847 * <li>Create a {@link TvExtender}.
9848 * <li>Set TV-specific properties using the {@code set} methods of
9849 * {@link TvExtender}.
9850 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9851 * to apply the extension to a notification.
9852 * </ol>
9853 *
9854 * <pre class="prettyprint">
9855 * Notification notification = new Notification.Builder(context)
9856 * ...
9857 * .extend(new TvExtender()
9858 * .set*(...))
9859 * .build();
9860 * </pre>
9861 *
9862 * <p>TV extensions can be accessed on an existing notification by using the
9863 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
9864 * to access values.
9865 *
9866 * @hide
9867 */
9868 @SystemApi
9869 public static final class TvExtender implements Extender {
9870 private static final String TAG = "TvExtender";
9871
9872 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
9873 private static final String EXTRA_FLAGS = "flags";
9874 private static final String EXTRA_CONTENT_INTENT = "content_intent";
9875 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009876 private static final String EXTRA_CHANNEL_ID = "channel_id";
Rhiannon Malia1a083932018-01-24 15:02:30 -08009877 private static final String EXTRA_SUPPRESS_SHOW_OVER_APPS = "suppressShowOverApps";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009878
9879 // Flags bitwise-ored to mFlags
9880 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
9881
9882 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009883 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009884 private PendingIntent mContentIntent;
9885 private PendingIntent mDeleteIntent;
Rhiannon Malia1a083932018-01-24 15:02:30 -08009886 private boolean mSuppressShowOverApps;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009887
9888 /**
9889 * Create a {@link TvExtender} with default options.
9890 */
9891 public TvExtender() {
9892 mFlags = FLAG_AVAILABLE_ON_TV;
9893 }
9894
9895 /**
9896 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
9897 *
9898 * @param notif The notification from which to copy options.
9899 */
9900 public TvExtender(Notification notif) {
9901 Bundle bundle = notif.extras == null ?
9902 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
9903 if (bundle != null) {
9904 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009905 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Rhiannon Malia1a083932018-01-24 15:02:30 -08009906 mSuppressShowOverApps = bundle.getBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009907 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
9908 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
9909 }
9910 }
9911
9912 /**
9913 * Apply a TV extension to a notification that is being built. This is typically called by
9914 * the {@link Notification.Builder#extend(Notification.Extender)}
9915 * method of {@link Notification.Builder}.
9916 */
9917 @Override
9918 public Notification.Builder extend(Notification.Builder builder) {
9919 Bundle bundle = new Bundle();
9920
9921 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009922 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Rhiannon Malia1a083932018-01-24 15:02:30 -08009923 bundle.putBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS, mSuppressShowOverApps);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009924 if (mContentIntent != null) {
9925 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
9926 }
9927
9928 if (mDeleteIntent != null) {
9929 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
9930 }
9931
9932 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
9933 return builder;
9934 }
9935
9936 /**
9937 * Returns true if this notification should be shown on TV. This method return true
9938 * if the notification was extended with a TvExtender.
9939 */
9940 public boolean isAvailableOnTv() {
9941 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
9942 }
9943
9944 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009945 * Specifies the channel the notification should be delivered on when shown on TV.
9946 * It can be different from the channel that the notification is delivered to when
9947 * posting on a non-TV device.
9948 */
9949 public TvExtender setChannel(String channelId) {
9950 mChannelId = channelId;
9951 return this;
9952 }
9953
9954 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04009955 * Specifies the channel the notification should be delivered on when shown on TV.
9956 * It can be different from the channel that the notification is delivered to when
9957 * posting on a non-TV device.
9958 */
9959 public TvExtender setChannelId(String channelId) {
9960 mChannelId = channelId;
9961 return this;
9962 }
9963
Jeff Sharkey000ce802017-04-29 13:13:27 -06009964 /** @removed */
9965 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009966 public String getChannel() {
9967 return mChannelId;
9968 }
9969
9970 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04009971 * Returns the id of the channel this notification posts to on TV.
9972 */
9973 public String getChannelId() {
9974 return mChannelId;
9975 }
9976
9977 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009978 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
9979 * If provided, it is used instead of the content intent specified
9980 * at the level of Notification.
9981 */
9982 public TvExtender setContentIntent(PendingIntent intent) {
9983 mContentIntent = intent;
9984 return this;
9985 }
9986
9987 /**
9988 * Returns the TV-specific content intent. If this method returns null, the
9989 * main content intent on the notification should be used.
9990 *
9991 * @see {@link Notification#contentIntent}
9992 */
9993 public PendingIntent getContentIntent() {
9994 return mContentIntent;
9995 }
9996
9997 /**
9998 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
9999 * by the user on TV. If provided, it is used instead of the delete intent specified
10000 * at the level of Notification.
10001 */
10002 public TvExtender setDeleteIntent(PendingIntent intent) {
10003 mDeleteIntent = intent;
10004 return this;
10005 }
10006
10007 /**
10008 * Returns the TV-specific delete intent. If this method returns null, the
10009 * main delete intent on the notification should be used.
10010 *
10011 * @see {@link Notification#deleteIntent}
10012 */
10013 public PendingIntent getDeleteIntent() {
10014 return mDeleteIntent;
10015 }
Rhiannon Malia1a083932018-01-24 15:02:30 -080010016
10017 /**
10018 * Specifies whether this notification should suppress showing a message over top of apps
10019 * outside of the launcher.
10020 */
10021 public TvExtender setSuppressShowOverApps(boolean suppress) {
10022 mSuppressShowOverApps = suppress;
10023 return this;
10024 }
10025
10026 /**
10027 * Returns true if this notification should not show messages over top of apps
10028 * outside of the launcher.
10029 */
10030 public boolean getSuppressShowOverApps() {
10031 return mSuppressShowOverApps;
10032 }
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010033 }
10034
10035 /**
Griff Hazen61a9e862014-05-22 16:05:19 -070010036 * Get an array of Notification objects from a parcelable array bundle field.
10037 * Update the bundle to have a typed array so fetches in the future don't need
10038 * to do an array copy.
10039 */
10040 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
10041 Parcelable[] array = bundle.getParcelableArray(key);
10042 if (array instanceof Notification[] || array == null) {
10043 return (Notification[]) array;
10044 }
10045 Notification[] typedArray = Arrays.copyOf(array, array.length,
10046 Notification[].class);
10047 bundle.putParcelableArray(key, typedArray);
10048 return typedArray;
10049 }
Christoph Studer4600f9b2014-07-22 22:44:43 +020010050
10051 private static class BuilderRemoteViews extends RemoteViews {
10052 public BuilderRemoteViews(Parcel parcel) {
10053 super(parcel);
10054 }
10055
Kenny Guy77320062014-08-27 21:37:15 +010010056 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
10057 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +020010058 }
10059
10060 @Override
10061 public BuilderRemoteViews clone() {
10062 Parcel p = Parcel.obtain();
10063 writeToParcel(p, 0);
10064 p.setDataPosition(0);
10065 BuilderRemoteViews brv = new BuilderRemoteViews(p);
10066 p.recycle();
10067 return brv;
10068 }
10069 }
Adrian Roos70d7aa32017-01-11 15:39:06 -080010070
Selim Cinek384804b2018-04-18 14:31:07 +080010071 /**
10072 * A result object where information about the template that was created is saved.
10073 */
10074 private static class TemplateBindResult {
Selim Cinek1c72fa02018-04-23 18:00:54 +080010075 int mIconMarginEnd;
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010076 boolean mRightIconContainerVisible;
Selim Cinek384804b2018-04-18 14:31:07 +080010077
10078 /**
Selim Cinek1c72fa02018-04-23 18:00:54 +080010079 * Get the margin end that needs to be added to any fields that may overlap
Selim Cinek384804b2018-04-18 14:31:07 +080010080 * with the right actions.
10081 */
Selim Cinek1c72fa02018-04-23 18:00:54 +080010082 public int getIconMarginEnd() {
10083 return mIconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +080010084 }
10085
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010086 /**
10087 * Is the icon container visible on the right size because of the reply button or the
10088 * right icon.
10089 */
10090 public boolean isRightIconContainerVisible() {
10091 return mRightIconContainerVisible;
10092 }
10093
Selim Cinek1c72fa02018-04-23 18:00:54 +080010094 public void setIconMarginEnd(int iconMarginEnd) {
10095 this.mIconMarginEnd = iconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +080010096 }
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010097
10098 public void setRightIconContainerVisible(boolean iconContainerVisible) {
10099 mRightIconContainerVisible = iconContainerVisible;
10100 }
Selim Cinek384804b2018-04-18 14:31:07 +080010101 }
10102
Adrian Roos70d7aa32017-01-11 15:39:06 -080010103 private static class StandardTemplateParams {
10104 boolean hasProgress = true;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010105 CharSequence title;
10106 CharSequence text;
Selim Cinekafeed292017-12-12 17:32:44 -080010107 CharSequence headerTextSecondary;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010108 CharSequence summaryText;
Selim Cinekbee4e072018-05-21 22:06:43 -070010109 int maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek88188f22017-09-19 16:46:56 -070010110 boolean hideLargeIcon;
Selim Cinek384804b2018-04-18 14:31:07 +080010111 boolean hideReplyIcon;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010112 boolean allowColorization = true;
10113 boolean forceDefaultColor = false;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010114
10115 final StandardTemplateParams reset() {
10116 hasProgress = true;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010117 title = null;
10118 text = null;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010119 summaryText = null;
Selim Cinekafeed292017-12-12 17:32:44 -080010120 headerTextSecondary = null;
Selim Cinekbee4e072018-05-21 22:06:43 -070010121 maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010122 allowColorization = true;
10123 forceDefaultColor = false;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010124 return this;
10125 }
10126
10127 final StandardTemplateParams hasProgress(boolean hasProgress) {
10128 this.hasProgress = hasProgress;
10129 return this;
10130 }
10131
10132 final StandardTemplateParams title(CharSequence title) {
10133 this.title = title;
10134 return this;
10135 }
10136
10137 final StandardTemplateParams text(CharSequence text) {
10138 this.text = text;
10139 return this;
10140 }
10141
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010142 final StandardTemplateParams summaryText(CharSequence text) {
10143 this.summaryText = text;
10144 return this;
10145 }
10146
Selim Cinekafeed292017-12-12 17:32:44 -080010147 final StandardTemplateParams headerTextSecondary(CharSequence text) {
10148 this.headerTextSecondary = text;
10149 return this;
10150 }
10151
Selim Cinek384804b2018-04-18 14:31:07 +080010152 final StandardTemplateParams hideLargeIcon(boolean hideLargeIcon) {
10153 this.hideLargeIcon = hideLargeIcon;
Selim Cinek88188f22017-09-19 16:46:56 -070010154 return this;
10155 }
10156
Selim Cinek384804b2018-04-18 14:31:07 +080010157 final StandardTemplateParams hideReplyIcon(boolean hideReplyIcon) {
10158 this.hideReplyIcon = hideReplyIcon;
Selim Cinek88188f22017-09-19 16:46:56 -070010159 return this;
10160 }
10161
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010162 final StandardTemplateParams disallowColorization() {
10163 this.allowColorization = false;
10164 return this;
10165 }
10166
10167 final StandardTemplateParams forceDefaultColor() {
10168 this.forceDefaultColor = true;
10169 return this;
10170 }
10171
Adrian Roos70d7aa32017-01-11 15:39:06 -080010172 final StandardTemplateParams fillTextsFrom(Builder b) {
10173 Bundle extras = b.mN.extras;
Lucas Dupin00be88f2019-01-03 17:50:52 -080010174 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE));
Lucas Dupin06c5e642017-09-13 16:34:58 -070010175
Lucas Dupin06c5e642017-09-13 16:34:58 -070010176 CharSequence text = extras.getCharSequence(EXTRA_BIG_TEXT);
Lucas Dupin00be88f2019-01-03 17:50:52 -080010177 if (TextUtils.isEmpty(text)) {
Lucas Dupin06c5e642017-09-13 16:34:58 -070010178 text = extras.getCharSequence(EXTRA_TEXT);
10179 }
Lucas Dupin00be88f2019-01-03 17:50:52 -080010180 this.text = b.processLegacyText(text);
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010181 this.summaryText = extras.getCharSequence(EXTRA_SUB_TEXT);
Adrian Roos70d7aa32017-01-11 15:39:06 -080010182 return this;
10183 }
Selim Cinekbee4e072018-05-21 22:06:43 -070010184
10185 /**
10186 * Set the maximum lines of remote input history lines allowed.
10187 * @param maxRemoteInputHistory The number of lines.
10188 * @return The builder for method chaining.
10189 */
10190 public StandardTemplateParams setMaxRemoteInputHistory(int maxRemoteInputHistory) {
10191 this.maxRemoteInputHistory = maxRemoteInputHistory;
10192 return this;
10193 }
Adrian Roos70d7aa32017-01-11 15:39:06 -080010194 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010195}