blob: e066f06542c3dd7f75f2a79f77c0c63259c24369 [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);
387 STANDARD_LAYOUTS.add(R.layout.notification_template_ambient_header);
388 STANDARD_LAYOUTS.add(R.layout.notification_template_header);
389 STANDARD_LAYOUTS.add(R.layout.notification_template_material_ambient);
390 }
391
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400392 /**
Dan Sandler86647982015-05-13 23:41:13 -0400393 * A large bitmap to be shown in the notification content area.
394 *
395 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 */
Dan Sandler86647982015-05-13 23:41:13 -0400397 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800398 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399
400 /**
401 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500402 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400404 * A notification that is noisy is more likely to be presented as a heads-up notification.
405 * </p>
406 *
407 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500408 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500410 * @deprecated use {@link NotificationChannel#getSound()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500412 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 public Uri sound;
414
415 /**
416 * Use this constant as the value for audioStreamType to request that
417 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700418 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400419 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500420 * @deprecated Use {@link NotificationChannel#getAudioAttributes()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700422 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 public static final int STREAM_DEFAULT = -1;
424
425 /**
426 * The audio stream type to use when playing the sound.
427 * Should be one of the STREAM_ constants from
428 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400429 *
430 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700432 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 public int audioStreamType = STREAM_DEFAULT;
434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400436 * The default value of {@link #audioAttributes}.
437 */
438 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
439 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
440 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
441 .build();
442
443 /**
444 * The {@link AudioAttributes audio attributes} to use when playing the sound.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500445 *
446 * @deprecated use {@link NotificationChannel#getAudioAttributes()} instead.
John Spurlockc0650f022014-07-19 13:22:39 -0400447 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500448 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -0400449 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
450
451 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500452 * The pattern with which to vibrate.
453 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 * <p>
455 * To vibrate the default pattern, see {@link #defaults}.
456 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500457 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 * @see android.os.Vibrator#vibrate(long[],int)
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500459 * @deprecated use {@link NotificationChannel#getVibrationPattern()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500461 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 public long[] vibrate;
463
464 /**
465 * The color of the led. The hardware will do its best approximation.
466 *
467 * @see #FLAG_SHOW_LIGHTS
468 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500469 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 */
Tor Norbye80756e32015-03-02 09:39:27 -0800471 @ColorInt
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500472 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 public int ledARGB;
474
475 /**
476 * The number of milliseconds for the LED to be on while it's flashing.
477 * The hardware will do its best approximation.
478 *
479 * @see #FLAG_SHOW_LIGHTS
480 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500481 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500483 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 public int ledOnMS;
485
486 /**
487 * The number of milliseconds for the LED to be off while it's flashing.
488 * The hardware will do its best approximation.
489 *
490 * @see #FLAG_SHOW_LIGHTS
491 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500492 *
493 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500495 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 public int ledOffMS;
497
498 /**
499 * Specifies which values should be taken from the defaults.
500 * <p>
501 * To set, OR the desired from {@link #DEFAULT_SOUND},
502 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
503 * values, use {@link #DEFAULT_ALL}.
504 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500505 *
506 * @deprecated use {@link NotificationChannel#getSound()} and
507 * {@link NotificationChannel#shouldShowLights()} and
508 * {@link NotificationChannel#shouldVibrate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500510 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 public int defaults;
512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 /**
514 * Bit to be bitwise-ored into the {@link #flags} field that should be
515 * set if you want the LED on for this notification.
516 * <ul>
517 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
518 * or 0 for both ledOnMS and ledOffMS.</li>
519 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
520 * <li>To flash the LED, pass the number of milliseconds that it should
521 * be on and off to ledOnMS and ledOffMS.</li>
522 * </ul>
523 * <p>
524 * Since hardware varies, you are not guaranteed that any of the values
Ricardo Loo Forondaf8f6d0a2018-01-25 08:42:43 -0800525 * you pass are honored exactly. Use the system defaults if possible
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 * because they will be set to values that work on any given hardware.
527 * <p>
528 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500529 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500530 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500532 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
534
535 /**
536 * Bit to be bitwise-ored into the {@link #flags} field that should be
537 * set if this notification is in reference to something that is ongoing,
538 * like a phone call. It should not be set if this notification is in
539 * reference to something that happened at a particular point in time,
540 * like a missed phone call.
541 */
542 public static final int FLAG_ONGOING_EVENT = 0x00000002;
543
544 /**
545 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700546 * the audio will be repeated until the notification is
547 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 */
549 public static final int FLAG_INSISTENT = 0x00000004;
550
551 /**
552 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700553 * set if you would only like the sound, vibrate and ticker to be played
554 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 */
556 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
557
558 /**
559 * Bit to be bitwise-ored into the {@link #flags} field that should be
560 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500561 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 */
563 public static final int FLAG_AUTO_CANCEL = 0x00000010;
564
565 /**
566 * Bit to be bitwise-ored into the {@link #flags} field that should be
567 * set if the notification should not be canceled when the user clicks
568 * the Clear all button.
569 */
570 public static final int FLAG_NO_CLEAR = 0x00000020;
571
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700572 /**
573 * Bit to be bitwise-ored into the {@link #flags} field that should be
574 * set if this notification represents a currently running service. This
575 * will normally be set for you by {@link Service#startForeground}.
576 */
577 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
578
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400579 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500580 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800581 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500582 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400583 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700584 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500585 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400586
Griff Hazendfcb0802014-02-11 12:00:00 -0800587 /**
588 * Bit to be bitswise-ored into the {@link #flags} field that should be
589 * set if this notification is relevant to the current device only
590 * and it is not recommended that it bridge to other devices.
591 */
592 public static final int FLAG_LOCAL_ONLY = 0x00000100;
593
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700594 /**
595 * Bit to be bitswise-ored into the {@link #flags} field that should be
596 * set if this notification is the group summary for a group of notifications.
597 * Grouped notifications may display in a cluster or stack on devices which
598 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
599 */
600 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
601
Julia Reynoldse46bb372016-03-17 11:05:58 -0400602 /**
603 * Bit to be bitswise-ored into the {@link #flags} field that should be
604 * set if this notification is the group summary for an auto-group of notifications.
605 *
606 * @hide
607 */
608 @SystemApi
609 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
610
Julia Reynolds4db59552017-06-30 13:34:01 -0400611 /**
612 * @hide
613 */
614 public static final int FLAG_CAN_COLORIZE = 0x00000800;
615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 public int flags;
617
Tor Norbyed9273d62013-05-30 15:59:53 -0700618 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700619 @IntDef(prefix = { "PRIORITY_" }, value = {
620 PRIORITY_DEFAULT,
621 PRIORITY_LOW,
622 PRIORITY_MIN,
623 PRIORITY_HIGH,
624 PRIORITY_MAX
625 })
Tor Norbyed9273d62013-05-30 15:59:53 -0700626 @Retention(RetentionPolicy.SOURCE)
627 public @interface Priority {}
628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500630 * Default notification {@link #priority}. If your application does not prioritize its own
631 * notifications, use this value for all notifications.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500632 *
633 * @deprecated use {@link NotificationManager#IMPORTANCE_DEFAULT} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500634 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500635 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500636 public static final int PRIORITY_DEFAULT = 0;
637
638 /**
639 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
640 * items smaller, or at a different position in the list, compared with your app's
641 * {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500642 *
643 * @deprecated use {@link NotificationManager#IMPORTANCE_LOW} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500644 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500645 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500646 public static final int PRIORITY_LOW = -1;
647
648 /**
649 * Lowest {@link #priority}; these items might not be shown to the user except under special
650 * circumstances, such as detailed notification logs.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500651 *
652 * @deprecated use {@link NotificationManager#IMPORTANCE_MIN} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500653 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500654 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500655 public static final int PRIORITY_MIN = -2;
656
657 /**
658 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
659 * show these items larger, or at a different position in notification lists, compared with
660 * your app's {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500661 *
662 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500663 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500664 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500665 public static final int PRIORITY_HIGH = 1;
666
667 /**
668 * Highest {@link #priority}, for your application's most important items that require the
669 * user's prompt attention or input.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500670 *
671 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500672 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500673 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500674 public static final int PRIORITY_MAX = 2;
675
676 /**
677 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800678 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500679 * Priority is an indication of how much of the user's valuable attention should be consumed by
680 * this notification. Low-priority notifications may be hidden from the user in certain
681 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500682 * system will make a determination about how to interpret this priority when presenting
683 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400684 *
685 * <p>
686 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
687 * as a heads-up notification.
688 * </p>
689 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500690 * @deprecated use {@link NotificationChannel#getImportance()} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500691 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700692 @Priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500693 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500694 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800695
Dan Sandler26e81cf2014-05-06 10:01:27 -0400696 /**
697 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
698 * to be applied by the standard Style templates when presenting this notification.
699 *
700 * The current template design constructs a colorful header image by overlaying the
701 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
702 * ignored.
703 */
Tor Norbye80756e32015-03-02 09:39:27 -0800704 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400705 public int color = COLOR_DEFAULT;
706
707 /**
708 * Special value of {@link #color} telling the system not to decorate this notification with
709 * any special color but instead use default colors when presenting this notification.
710 */
Tor Norbye80756e32015-03-02 09:39:27 -0800711 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400712 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600713
714 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800715 * Special value of {@link #color} used as a place holder for an invalid color.
Selim Cinek99104832017-01-25 14:47:33 -0800716 * @hide
Adrian Roos4ff3b122016-02-01 12:26:13 -0800717 */
718 @ColorInt
Selim Cinek99104832017-01-25 14:47:33 -0800719 public static final int COLOR_INVALID = 1;
Adrian Roos4ff3b122016-02-01 12:26:13 -0800720
721 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700722 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
723 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600724 * lockscreen).
725 *
726 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
727 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
728 * shown in all situations, but the contents are only available if the device is unlocked for
729 * the appropriate user.
730 *
731 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
732 * can be read even in an "insecure" context (that is, above a secure lockscreen).
733 * To modify the public version of this notification—for example, to redact some portions—see
734 * {@link Builder#setPublicVersion(Notification)}.
735 *
736 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
737 * and ticker until the user has bypassed the lockscreen.
738 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600739 public @Visibility int visibility;
740
741 /** @hide */
742 @IntDef(prefix = { "VISIBILITY_" }, value = {
743 VISIBILITY_PUBLIC,
744 VISIBILITY_PRIVATE,
745 VISIBILITY_SECRET,
746 })
747 @Retention(RetentionPolicy.SOURCE)
748 public @interface Visibility {}
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600749
Griff Hazenfc3922d2014-08-20 11:56:44 -0700750 /**
751 * Notification visibility: Show this notification in its entirety on all lockscreens.
752 *
753 * {@see #visibility}
754 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600755 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700756
757 /**
758 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
759 * private information on secure lockscreens.
760 *
761 * {@see #visibility}
762 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600763 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700764
765 /**
766 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
767 *
768 * {@see #visibility}
769 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600770 public static final int VISIBILITY_SECRET = -1;
771
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500772 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400773 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500774 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400775 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500776
777 /**
Adora Zhangaa90e872018-03-12 14:07:50 -0700778 * Notification category: map turn-by-turn navigation.
779 */
780 public static final String CATEGORY_NAVIGATION = "navigation";
781
782 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400783 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500784 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400785 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500786
787 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400788 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500789 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400790 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500791
792 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400793 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500794 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400795 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500796
797 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400798 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500799 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400800 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500801
802 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400803 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500804 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400805 public static final String CATEGORY_ALARM = "alarm";
806
807 /**
808 * Notification category: progress of a long-running background operation.
809 */
810 public static final String CATEGORY_PROGRESS = "progress";
811
812 /**
813 * Notification category: social network or sharing update.
814 */
815 public static final String CATEGORY_SOCIAL = "social";
816
817 /**
818 * Notification category: error in background operation or authentication status.
819 */
820 public static final String CATEGORY_ERROR = "err";
821
822 /**
823 * Notification category: media transport control for playback.
824 */
825 public static final String CATEGORY_TRANSPORT = "transport";
826
827 /**
828 * Notification category: system or device status update. Reserved for system use.
829 */
830 public static final String CATEGORY_SYSTEM = "sys";
831
832 /**
833 * Notification category: indication of running background service.
834 */
835 public static final String CATEGORY_SERVICE = "service";
836
837 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400838 * Notification category: a specific, timely recommendation for a single thing.
839 * For example, a news app might want to recommend a news story it believes the user will
840 * want to read next.
841 */
842 public static final String CATEGORY_RECOMMENDATION = "recommendation";
843
844 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400845 * Notification category: ongoing information about device or contextual status.
846 */
847 public static final String CATEGORY_STATUS = "status";
848
849 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400850 * Notification category: user-scheduled reminder.
851 */
852 public static final String CATEGORY_REMINDER = "reminder";
853
854 /**
Adora Zhangaa90e872018-03-12 14:07:50 -0700855 * Notification category: extreme car emergencies.
856 * @hide
857 */
858 @SystemApi
859 public static final String CATEGORY_CAR_EMERGENCY = "car_emergency";
860
861 /**
862 * Notification category: car warnings.
863 * @hide
864 */
865 @SystemApi
866 public static final String CATEGORY_CAR_WARNING = "car_warning";
867
868 /**
869 * Notification category: general car system information.
870 * @hide
871 */
872 @SystemApi
873 public static final String CATEGORY_CAR_INFORMATION = "car_information";
874
875 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400876 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
877 * that best describes this Notification. May be used by the system for ranking and filtering.
878 */
879 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500880
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100881 @UnsupportedAppUsage
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700882 private String mGroupKey;
883
884 /**
885 * Get the key used to group this notification into a cluster or stack
886 * with other notifications on devices which support such rendering.
887 */
888 public String getGroup() {
889 return mGroupKey;
890 }
891
892 private String mSortKey;
893
894 /**
895 * Get a sort key that orders this notification among other notifications from the
896 * same package. This can be useful if an external sort was already applied and an app
897 * would like to preserve this. Notifications will be sorted lexicographically using this
898 * value, although providing different priorities in addition to providing sort key may
899 * cause this value to be ignored.
900 *
901 * <p>This sort key can also be used to order members of a notification group. See
902 * {@link Builder#setGroup}.
903 *
904 * @see String#compareTo(String)
905 */
906 public String getSortKey() {
907 return mSortKey;
908 }
909
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500910 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400911 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400912 * <p>
913 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
914 * APIs, and are intended to be used by
915 * {@link android.service.notification.NotificationListenerService} implementations to extract
916 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500917 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400918 public Bundle extras = new Bundle();
919
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400920 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700921 * All pending intents in the notification as the system needs to be able to access them but
922 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700923 * custom parcelable objects.
924 *
925 * @hide
926 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100927 @UnsupportedAppUsage
Felipe Lemedd85da62016-06-28 11:29:54 -0700928 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700929
930 /**
Dianne Hackborn98305522017-05-05 17:53:53 -0700931 * Token identifying the notification that is applying doze/bgcheck whitelisting to the
932 * pending intents inside of it, so only those will get the behavior.
933 *
934 * @hide
935 */
Adrian Roosfb921842017-10-26 14:49:56 +0200936 private IBinder mWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -0700937
938 /**
939 * Must be set by a process to start associating tokens with Notification objects
940 * coming in to it. This is set by NotificationManagerService.
941 *
942 * @hide
943 */
944 static public IBinder processWhitelistToken;
945
946 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400947 * {@link #extras} key: this is the title of the notification,
948 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
949 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500950 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400951
952 /**
953 * {@link #extras} key: this is the title of the notification when shown in expanded form,
954 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
955 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400956 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400957
958 /**
959 * {@link #extras} key: this is the main text payload, as supplied to
960 * {@link Builder#setContentText(CharSequence)}.
961 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500962 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400963
964 /**
965 * {@link #extras} key: this is a third line of text, as supplied to
966 * {@link Builder#setSubText(CharSequence)}.
967 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400968 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400969
970 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800971 * {@link #extras} key: this is the remote input history, as supplied to
972 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700973 *
974 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
975 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
976 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
977 * notifications once the other party has responded).
978 *
979 * The extra with this key is of type CharSequence[] and contains the most recent entry at
980 * the 0 index, the second most recent at the 1 index, etc.
981 *
982 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800983 */
984 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
985
986 /**
Kenny Guya0f6de82018-04-06 16:20:16 +0100987 * {@link #extras} key: boolean as supplied to
988 * {@link Builder#setShowRemoteInputSpinner(boolean)}.
989 *
990 * If set to true, then the view displaying the remote input history from
991 * {@link Builder#setRemoteInputHistory(CharSequence[])} will have a progress spinner.
992 *
993 * @see Builder#setShowRemoteInputSpinner(boolean)
994 * @hide
995 */
996 public static final String EXTRA_SHOW_REMOTE_INPUT_SPINNER = "android.remoteInputSpinner";
997
998 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +0100999 * {@link #extras} key: boolean as supplied to
1000 * {@link Builder#setHideSmartReplies(boolean)}.
1001 *
1002 * If set to true, then any smart reply buttons will be hidden.
1003 *
1004 * @see Builder#setHideSmartReplies(boolean)
1005 * @hide
1006 */
1007 public static final String EXTRA_HIDE_SMART_REPLIES = "android.hideSmartReplies";
1008
1009 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001010 * {@link #extras} key: this is a small piece of additional text as supplied to
1011 * {@link Builder#setContentInfo(CharSequence)}.
1012 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001013 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001014
1015 /**
1016 * {@link #extras} key: this is a line of summary information intended to be shown
1017 * alongside expanded notifications, as supplied to (e.g.)
1018 * {@link BigTextStyle#setSummaryText(CharSequence)}.
1019 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001020 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001021
1022 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02001023 * {@link #extras} key: this is the longer text shown in the big form of a
1024 * {@link BigTextStyle} notification, as supplied to
1025 * {@link BigTextStyle#bigText(CharSequence)}.
1026 */
1027 public static final String EXTRA_BIG_TEXT = "android.bigText";
1028
1029 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001030 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
1031 * supplied to {@link Builder#setSmallIcon(int)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -04001032 *
1033 * @deprecated Use {@link #getSmallIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001034 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04001035 @Deprecated
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001036 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001037
1038 /**
1039 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
1040 * notification payload, as
1041 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -04001042 *
1043 * @deprecated Use {@link #getLargeIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001044 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04001045 @Deprecated
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001046 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001047
1048 /**
1049 * {@link #extras} key: this is a bitmap to be used instead of the one from
1050 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
1051 * shown in its expanded form, as supplied to
1052 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
1053 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001054 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001055
1056 /**
1057 * {@link #extras} key: this is the progress value supplied to
1058 * {@link Builder#setProgress(int, int, boolean)}.
1059 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001060 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001061
1062 /**
1063 * {@link #extras} key: this is the maximum value supplied to
1064 * {@link Builder#setProgress(int, int, boolean)}.
1065 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001066 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001067
1068 /**
1069 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
1070 * {@link Builder#setProgress(int, int, boolean)}.
1071 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001072 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001073
1074 /**
1075 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
1076 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
1077 * {@link Builder#setUsesChronometer(boolean)}.
1078 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001079 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001080
1081 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08001082 * {@link #extras} key: whether the chronometer set on the notification should count down
1083 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -07001084 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -08001085 */
Adrian Roos96b7e202016-05-17 13:50:38 -07001086 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -08001087
1088 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001089 * {@link #extras} key: whether {@link #when} should be shown,
1090 * as supplied to {@link Builder#setShowWhen(boolean)}.
1091 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001092 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001093
1094 /**
1095 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
1096 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
1097 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001098 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001099
1100 /**
1101 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
1102 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
1103 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001104 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -04001105
1106 /**
1107 * {@link #extras} key: A string representing the name of the specific
1108 * {@link android.app.Notification.Style} used to create this notification.
1109 */
Chris Wren91ad5632013-06-05 15:05:57 -04001110 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001111
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001112 /**
Chris Wrene6c48932014-09-29 17:19:27 -04001113 * {@link #extras} key: A String array containing the people that this notification relates to,
1114 * each of which was supplied to {@link Builder#addPerson(String)}.
Selim Cineke7238dd2017-12-14 17:48:32 -08001115 *
1116 * @deprecated the actual objects are now in {@link #EXTRA_PEOPLE_LIST}
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001117 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001118 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001119
1120 /**
Selim Cineke7238dd2017-12-14 17:48:32 -08001121 * {@link #extras} key: An arrayList of {@link Person} objects containing the people that
1122 * this notification relates to.
1123 */
1124 public static final String EXTRA_PEOPLE_LIST = "android.people.list";
1125
1126 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04001127 * Allow certain system-generated notifications to appear before the device is provisioned.
1128 * Only available to notifications coming from the android package.
1129 * @hide
1130 */
Maurice Lam96c10032017-03-29 15:42:38 -07001131 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001132 @RequiresPermission(android.Manifest.permission.NOTIFICATION_DURING_SETUP)
John Spurlockfd7f1e02014-03-18 16:41:57 -04001133 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
1134
1135 /**
Robin Leed107af62018-04-27 13:55:56 +02001136 * {@link #extras} key:
1137 * flat {@link String} representation of a {@link android.content.ContentUris content URI}
1138 * pointing to an image that can be displayed in the background when the notification is
1139 * selected. Used on television platforms. The URI must point to an image stream suitable for
1140 * passing into {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001141 * BitmapFactory.decodeStream}; all other content types will be ignored.
Jose Limae9e3b3b2014-05-18 23:44:50 -07001142 */
1143 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
1144
1145 /**
Dan Sandler842dd772014-05-15 09:36:47 -04001146 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -07001147 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -04001148 * {@link android.app.Notification.MediaStyle} notification.
1149 */
1150 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
1151
1152 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +01001153 * {@link #extras} key: the indices of actions to be shown in the compact view,
1154 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
1155 */
1156 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
1157
Christoph Studer943aa672014-08-03 20:31:16 +02001158 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04001159 * {@link #extras} key: the username to be displayed for all messages sent by the user including
1160 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -07001161 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1162 * {@link CharSequence}
Selim Cinekcb8b9852017-12-15 18:01:52 -08001163 *
1164 * @deprecated use {@link #EXTRA_MESSAGING_PERSON}
Alex Hillsfc737de2016-03-23 17:33:02 -04001165 */
1166 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
1167
1168 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08001169 * {@link #extras} key: the person to be displayed for all messages sent by the user including
1170 * direct replies
1171 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1172 * {@link Person}
1173 */
1174 public static final String EXTRA_MESSAGING_PERSON = "android.messagingUser";
1175
1176 /**
Adrian Roos96b7e202016-05-17 13:50:38 -07001177 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -04001178 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -04001179 */
Alex Hillsd9b04d92016-04-11 16:38:16 -04001180 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -04001181
1182 /**
1183 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
1184 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -07001185 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1186 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -04001187 */
1188 public static final String EXTRA_MESSAGES = "android.messages";
1189
1190 /**
Adrian Roos437cd562017-01-18 15:47:03 -08001191 * {@link #extras} key: an array of
1192 * {@link android.app.Notification.MessagingStyle#addHistoricMessage historic}
1193 * {@link android.app.Notification.MessagingStyle.Message} bundles provided by a
1194 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1195 * array of bundles.
1196 */
1197 public static final String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
1198
1199 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08001200 * {@link #extras} key: whether the {@link android.app.Notification.MessagingStyle} notification
1201 * represents a group conversation.
1202 */
1203 public static final String EXTRA_IS_GROUP_CONVERSATION = "android.isGroupConversation";
1204
1205 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08001206 * {@link #extras} key: whether the notification should be colorized as
1207 * supplied to {@link Builder#setColorized(boolean)}}.
1208 */
1209 public static final String EXTRA_COLORIZED = "android.colorized";
1210
1211 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001212 * @hide
1213 */
1214 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
1215
Selim Cinek247fa012016-02-18 09:50:48 -08001216 /**
1217 * @hide
1218 */
1219 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
1220
Shane Brennan472a3b32016-12-12 15:28:10 -08001221 /**
Selim Cinekd0426622017-07-11 13:19:59 +02001222 * @hide
1223 */
1224 public static final String EXTRA_REDUCED_IMAGES = "android.reduced.images";
1225
1226 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001227 * {@link #extras} key: the audio contents of this notification.
1228 *
1229 * This is for use when rendering the notification on an audio-focused interface;
1230 * the audio contents are a complete sound sample that contains the contents/body of the
1231 * notification. This may be used in substitute of a Text-to-Speech reading of the
1232 * notification. For example if the notification represents a voice message this should point
1233 * to the audio of that message.
1234 *
1235 * The data stored under this key should be a String representation of a Uri that contains the
1236 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1237 *
1238 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1239 * has a field for holding data URI. That field can be used for audio.
1240 * See {@code Message#setData}.
1241 *
1242 * Example usage:
1243 * <pre>
1244 * {@code
1245 * Notification.Builder myBuilder = (build your Notification as normal);
1246 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1247 * }
1248 * </pre>
1249 */
1250 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1251
Dan Sandler80eaa592016-04-14 23:34:54 -04001252 /** @hide */
1253 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001254 @RequiresPermission(android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME)
Dan Sandler732bd6c2016-04-12 14:20:32 -04001255 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1256
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001257 /**
Julia Reynolds3c7de112018-03-28 09:33:13 -04001258 * This is set on the notifications shown by system_server about apps running foreground
1259 * services. It indicates that the notification should be shown
1260 * only if any of the given apps do not already have a properly tagged
1261 * {@link #FLAG_FOREGROUND_SERVICE} notification currently visible to the user.
1262 * This is a string array of all package names of the apps.
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001263 * @hide
1264 */
1265 public static final String EXTRA_FOREGROUND_APPS = "android.foregroundApps";
1266
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001267 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04001268 private Icon mSmallIcon;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001269 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04001270 private Icon mLargeIcon;
1271
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001272 @UnsupportedAppUsage
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001273 private String mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001274 private long mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001275
Julia Reynolds13d898c2017-02-02 12:22:05 -05001276 private String mShortcutId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04001277 private CharSequence mSettingsText;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001278
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04001279 private PendingIntent mAppOverlayIntent;
1280
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001281 /** @hide */
1282 @IntDef(prefix = { "GROUP_ALERT_" }, value = {
1283 GROUP_ALERT_ALL, GROUP_ALERT_CHILDREN, GROUP_ALERT_SUMMARY
1284 })
1285 @Retention(RetentionPolicy.SOURCE)
1286 public @interface GroupAlertBehavior {}
1287
1288 /**
1289 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all notifications in a
1290 * group with sound or vibration ought to make sound or vibrate (respectively), so this
1291 * notification will not be muted when it is in a group.
1292 */
1293 public static final int GROUP_ALERT_ALL = 0;
1294
1295 /**
1296 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all children
1297 * notification in a group should be silenced (no sound or vibration) even if they are posted
1298 * to a {@link NotificationChannel} that has sound and/or vibration. Use this constant to
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001299 * mute this notification if this notification is a group child. This must be applied to all
1300 * children notifications you want to mute.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001301 *
1302 * <p> For example, you might want to use this constant if you post a number of children
1303 * notifications at once (say, after a periodic sync), and only need to notify the user
1304 * audibly once.
1305 */
1306 public static final int GROUP_ALERT_SUMMARY = 1;
1307
1308 /**
1309 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that the summary
1310 * notification in a group should be silenced (no sound or vibration) even if they are
1311 * posted to a {@link NotificationChannel} that has sound and/or vibration. Use this constant
1312 * to mute this notification if this notification is a group summary.
1313 *
1314 * <p>For example, you might want to use this constant if only the children notifications
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001315 * in your group have content and the summary is only used to visually group notifications
1316 * rather than to alert the user that new information is available.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001317 */
1318 public static final int GROUP_ALERT_CHILDREN = 2;
1319
Julia Reynolds2f431e22017-06-07 14:12:09 +00001320 private int mGroupAlertBehavior = GROUP_ALERT_ALL;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001321
Julia Reynolds13d898c2017-02-02 12:22:05 -05001322 /**
1323 * If this notification is being shown as a badge, always show as a number.
1324 */
1325 public static final int BADGE_ICON_NONE = 0;
1326
1327 /**
1328 * If this notification is being shown as a badge, use the {@link #getSmallIcon()} to
1329 * represent this notification.
1330 */
1331 public static final int BADGE_ICON_SMALL = 1;
1332
1333 /**
1334 * If this notification is being shown as a badge, use the {@link #getLargeIcon()} to
1335 * represent this notification.
1336 */
1337 public static final int BADGE_ICON_LARGE = 2;
Julia Reynolds7d5b4da2017-03-20 10:18:49 -04001338 private int mBadgeIcon = BADGE_ICON_NONE;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001339
Chris Wren51c75102013-07-16 20:49:17 -04001340 /**
Gustav Sennton761884c2018-11-19 17:40:19 +00001341 * Determines whether the platform can generate contextual actions for a notification.
1342 */
1343 private boolean mAllowSystemGeneratedContextualActions = true;
1344
1345 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001346 * Structure to encapsulate a named action that can be shown as part of this notification.
1347 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1348 * selected by the user.
1349 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001350 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1351 * or {@link Notification.Builder#addAction(Notification.Action)}
1352 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001353 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001354 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001355 /**
1356 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1357 * {@link RemoteInput}s.
1358 *
1359 * This is intended for {@link RemoteInput}s that only accept data, meaning
1360 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
1361 * is null or empty, and {@link RemoteInput#getAllowedDataTypes is non-null and not
1362 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1363 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1364 *
1365 * You can test if a RemoteInput matches these constraints using
1366 * {@link RemoteInput#isDataOnly}.
1367 */
1368 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1369
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001370 /**
1371 * {@link }: No semantic action defined.
1372 */
1373 public static final int SEMANTIC_ACTION_NONE = 0;
1374
1375 /**
1376 * {@code SemanticAction}: Reply to a conversation, chat, group, or wherever replies
1377 * may be appropriate.
1378 */
1379 public static final int SEMANTIC_ACTION_REPLY = 1;
1380
1381 /**
1382 * {@code SemanticAction}: Mark content as read.
1383 */
1384 public static final int SEMANTIC_ACTION_MARK_AS_READ = 2;
1385
1386 /**
1387 * {@code SemanticAction}: Mark content as unread.
1388 */
1389 public static final int SEMANTIC_ACTION_MARK_AS_UNREAD = 3;
1390
1391 /**
1392 * {@code SemanticAction}: Delete the content associated with the notification. This
1393 * could mean deleting an email, message, etc.
1394 */
1395 public static final int SEMANTIC_ACTION_DELETE = 4;
1396
1397 /**
1398 * {@code SemanticAction}: Archive the content associated with the notification. This
1399 * could mean archiving an email, message, etc.
1400 */
1401 public static final int SEMANTIC_ACTION_ARCHIVE = 5;
1402
1403 /**
1404 * {@code SemanticAction}: Mute the content associated with the notification. This could
1405 * mean silencing a conversation or currently playing media.
1406 */
1407 public static final int SEMANTIC_ACTION_MUTE = 6;
1408
1409 /**
1410 * {@code SemanticAction}: Unmute the content associated with the notification. This could
1411 * mean un-silencing a conversation or currently playing media.
1412 */
1413 public static final int SEMANTIC_ACTION_UNMUTE = 7;
1414
1415 /**
1416 * {@code SemanticAction}: Mark content with a thumbs up.
1417 */
1418 public static final int SEMANTIC_ACTION_THUMBS_UP = 8;
1419
1420 /**
1421 * {@code SemanticAction}: Mark content with a thumbs down.
1422 */
1423 public static final int SEMANTIC_ACTION_THUMBS_DOWN = 9;
1424
Kodlee Yincda5b092018-02-15 15:34:53 -08001425 /**
1426 * {@code SemanticAction}: Call a contact, group, etc.
1427 */
1428 public static final int SEMANTIC_ACTION_CALL = 10;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001429
Gustav Sennton1463d832018-11-06 16:12:48 +00001430 /**
1431 * {@code SemanticAction}: Contextual action - dependent on the current notification. E.g.
1432 * open a Map application with an address shown in the notification.
1433 */
1434 public static final int SEMANTIC_ACTION_CONTEXTUAL_SUGGESTION = 11;
1435
Griff Hazen959591e2014-05-15 22:26:18 -07001436 private final Bundle mExtras;
Mathew Inwood8c854f82018-09-14 12:35:36 +01001437 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Dan Sandler86647982015-05-13 23:41:13 -04001438 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001439 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001440 private boolean mAllowGeneratedReplies = true;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001441 private final @SemanticAction int mSemanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001442
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001443 /**
1444 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001445 *
1446 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001447 */
Dan Sandler86647982015-05-13 23:41:13 -04001448 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001449 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001450
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001451 /**
1452 * Title of the action.
1453 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001454 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001455
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001456 /**
1457 * Intent to send when the user invokes this action. May be null, in which case the action
1458 * may be rendered in a disabled presentation by the system UI.
1459 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001460 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001461
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001462 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001463 if (in.readInt() != 0) {
1464 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001465 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1466 icon = mIcon.getResId();
1467 }
Dan Sandler86647982015-05-13 23:41:13 -04001468 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001469 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1470 if (in.readInt() == 1) {
1471 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1472 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001473 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001474 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001475 mAllowGeneratedReplies = in.readInt() == 1;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001476 mSemanticAction = in.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001477 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001478
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001479 /**
Dan Sandler86647982015-05-13 23:41:13 -04001480 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001481 */
Dan Sandler86647982015-05-13 23:41:13 -04001482 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001483 public Action(int icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001484 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true,
1485 SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001486 }
1487
Adrian Roos7af53622016-10-12 13:44:05 -07001488 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001489 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001490 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1491 @SemanticAction int semanticAction) {
Dan Sandler86647982015-05-13 23:41:13 -04001492 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001493 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1494 this.icon = icon.getResId();
1495 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001496 this.title = title;
1497 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001498 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001499 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001500 this.mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001501 this.mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001502 }
1503
1504 /**
Dan Sandler86647982015-05-13 23:41:13 -04001505 * Return an icon representing the action.
1506 */
1507 public Icon getIcon() {
1508 if (mIcon == null && icon != 0) {
1509 // you snuck an icon in here without using the builder; let's try to keep it
1510 mIcon = Icon.createWithResource("", icon);
1511 }
1512 return mIcon;
1513 }
1514
1515 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001516 * Get additional metadata carried around with this Action.
1517 */
1518 public Bundle getExtras() {
1519 return mExtras;
1520 }
1521
1522 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001523 * Return whether the platform should automatically generate possible replies for this
1524 * {@link Action}
1525 */
1526 public boolean getAllowGeneratedReplies() {
1527 return mAllowGeneratedReplies;
1528 }
1529
1530 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001531 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001532 * May return null if no remote inputs were added. Only returns inputs which accept
1533 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001534 */
1535 public RemoteInput[] getRemoteInputs() {
1536 return mRemoteInputs;
1537 }
1538
1539 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001540 * Returns the {@code SemanticAction} associated with this {@link Action}. A
1541 * {@code SemanticAction} denotes what an {@link Action}'s {@link PendingIntent} will do
1542 * (eg. reply, mark as read, delete, etc).
1543 */
1544 public @SemanticAction int getSemanticAction() {
1545 return mSemanticAction;
1546 }
1547
1548 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001549 * Get the list of inputs to be collected from the user that ONLY accept data when this
1550 * action is sent. These remote inputs are guaranteed to return true on a call to
1551 * {@link RemoteInput#isDataOnly}.
1552 *
Shane Brennanf670d6c2017-04-25 13:05:45 -07001553 * Returns null if there are no data-only remote inputs.
Shane Brennan472a3b32016-12-12 15:28:10 -08001554 *
1555 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1556 * of non-textual RemoteInputs do not access these remote inputs.
1557 */
1558 public RemoteInput[] getDataOnlyRemoteInputs() {
1559 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1560 }
1561
1562 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001563 * Builder class for {@link Action} objects.
1564 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001565 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001566 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001567 private final CharSequence mTitle;
1568 private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001569 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001570 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001571 private ArrayList<RemoteInput> mRemoteInputs;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001572 private @SemanticAction int mSemanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001573
1574 /**
1575 * Construct a new builder for {@link Action} object.
1576 * @param icon icon to show for this action
1577 * @param title the title of the action
1578 * @param intent the {@link PendingIntent} to fire when users trigger this action
1579 */
Dan Sandler86647982015-05-13 23:41:13 -04001580 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001581 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001582 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001583 }
1584
1585 /**
1586 * Construct a new builder for {@link Action} object.
1587 * @param icon icon to show for this action
1588 * @param title the title of the action
1589 * @param intent the {@link PendingIntent} to fire when users trigger this action
1590 */
1591 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001592 this(icon, title, intent, new Bundle(), null, true, SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001593 }
1594
1595 /**
1596 * Construct a new builder for {@link Action} object using the fields from an
1597 * {@link Action}.
1598 * @param action the action to read fields from.
1599 */
1600 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001601 this(action.getIcon(), action.title, action.actionIntent,
1602 new Bundle(action.mExtras), action.getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001603 action.getAllowGeneratedReplies(), action.getSemanticAction());
Griff Hazen959591e2014-05-15 22:26:18 -07001604 }
1605
Dan Sandler86647982015-05-13 23:41:13 -04001606 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001607 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1608 @SemanticAction int semanticAction) {
Griff Hazen959591e2014-05-15 22:26:18 -07001609 mIcon = icon;
1610 mTitle = title;
1611 mIntent = intent;
1612 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001613 if (remoteInputs != null) {
1614 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1615 Collections.addAll(mRemoteInputs, remoteInputs);
1616 }
Adrian Roos7af53622016-10-12 13:44:05 -07001617 mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001618 mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001619 }
1620
1621 /**
1622 * Merge additional metadata into this builder.
1623 *
1624 * <p>Values within the Bundle will replace existing extras values in this Builder.
1625 *
1626 * @see Notification.Action#extras
1627 */
1628 public Builder addExtras(Bundle extras) {
1629 if (extras != null) {
1630 mExtras.putAll(extras);
1631 }
1632 return this;
1633 }
1634
1635 /**
1636 * Get the metadata Bundle used by this Builder.
1637 *
1638 * <p>The returned Bundle is shared with this Builder.
1639 */
1640 public Bundle getExtras() {
1641 return mExtras;
1642 }
1643
1644 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001645 * Add an input to be collected from the user when this action is sent.
1646 * Response values can be retrieved from the fired intent by using the
1647 * {@link RemoteInput#getResultsFromIntent} function.
1648 * @param remoteInput a {@link RemoteInput} to add to the action
1649 * @return this object for method chaining
1650 */
1651 public Builder addRemoteInput(RemoteInput remoteInput) {
1652 if (mRemoteInputs == null) {
1653 mRemoteInputs = new ArrayList<RemoteInput>();
1654 }
1655 mRemoteInputs.add(remoteInput);
1656 return this;
1657 }
1658
1659 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001660 * Set whether the platform should automatically generate possible replies to add to
1661 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1662 * {@link RemoteInput}, this has no effect.
1663 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1664 * otherwise
1665 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001666 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001667 */
1668 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1669 mAllowGeneratedReplies = allowGeneratedReplies;
1670 return this;
1671 }
1672
1673 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001674 * Sets the {@code SemanticAction} for this {@link Action}. A
1675 * {@code SemanticAction} denotes what an {@link Action}'s
1676 * {@link PendingIntent} will do (eg. reply, mark as read, delete, etc).
1677 * @param semanticAction a SemanticAction defined within {@link Action} with
1678 * {@code SEMANTIC_ACTION_} prefixes
1679 * @return this object for method chaining
1680 */
1681 public Builder setSemanticAction(@SemanticAction int semanticAction) {
1682 mSemanticAction = semanticAction;
1683 return this;
1684 }
1685
1686 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001687 * Apply an extender to this action builder. Extenders may be used to add
1688 * metadata or change options on this builder.
1689 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001690 public Builder extend(Extender extender) {
1691 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001692 return this;
1693 }
1694
1695 /**
Gustav Senntonc98b1632018-11-23 12:26:15 +00001696 * Throws an NPE if we are building a contextual action missing one of the fields
1697 * necessary to display the action.
1698 */
1699 private void checkContextualActionNullFields() {
1700 if (mSemanticAction != SEMANTIC_ACTION_CONTEXTUAL_SUGGESTION) return;
1701
1702 if (mIcon == null) {
1703 throw new NullPointerException("Contextual Actions must contain a valid icon");
1704 }
1705
1706 if (mIntent == null) {
1707 throw new NullPointerException(
1708 "Contextual Actions must contain a valid PendingIntent");
1709 }
1710 }
1711
1712 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001713 * Combine all of the options that have been set and return a new {@link Action}
1714 * object.
1715 * @return the built action
1716 */
1717 public Action build() {
Gustav Senntonc98b1632018-11-23 12:26:15 +00001718 checkContextualActionNullFields();
1719
Shane Brennan472a3b32016-12-12 15:28:10 -08001720 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1721 RemoteInput[] previousDataInputs =
1722 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001723 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001724 for (RemoteInput input : previousDataInputs) {
1725 dataOnlyInputs.add(input);
1726 }
1727 }
1728 List<RemoteInput> textInputs = new ArrayList<>();
1729 if (mRemoteInputs != null) {
1730 for (RemoteInput input : mRemoteInputs) {
1731 if (input.isDataOnly()) {
1732 dataOnlyInputs.add(input);
1733 } else {
1734 textInputs.add(input);
1735 }
1736 }
1737 }
1738 if (!dataOnlyInputs.isEmpty()) {
1739 RemoteInput[] dataInputsArr =
1740 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1741 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1742 }
1743 RemoteInput[] textInputsArr = textInputs.isEmpty()
1744 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1745 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001746 mAllowGeneratedReplies, mSemanticAction);
Griff Hazen959591e2014-05-15 22:26:18 -07001747 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001748 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001749
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001750 @Override
1751 public Action clone() {
1752 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001753 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001754 title,
1755 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001756 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001757 getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001758 getAllowGeneratedReplies(),
1759 getSemanticAction());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001760 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001761
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001762 @Override
1763 public int describeContents() {
1764 return 0;
1765 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001766
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001767 @Override
1768 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001769 final Icon ic = getIcon();
1770 if (ic != null) {
1771 out.writeInt(1);
1772 ic.writeToParcel(out, 0);
1773 } else {
1774 out.writeInt(0);
1775 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001776 TextUtils.writeToParcel(title, out, flags);
1777 if (actionIntent != null) {
1778 out.writeInt(1);
1779 actionIntent.writeToParcel(out, flags);
1780 } else {
1781 out.writeInt(0);
1782 }
Griff Hazen959591e2014-05-15 22:26:18 -07001783 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001784 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001785 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001786 out.writeInt(mSemanticAction);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001787 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001788
Griff Hazen959591e2014-05-15 22:26:18 -07001789 public static final Parcelable.Creator<Action> CREATOR =
1790 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001791 public Action createFromParcel(Parcel in) {
1792 return new Action(in);
1793 }
1794 public Action[] newArray(int size) {
1795 return new Action[size];
1796 }
1797 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001798
1799 /**
1800 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1801 * metadata or change options on an action builder.
1802 */
1803 public interface Extender {
1804 /**
1805 * Apply this extender to a notification action builder.
1806 * @param builder the builder to be modified.
1807 * @return the build object for chaining.
1808 */
1809 public Builder extend(Builder builder);
1810 }
1811
1812 /**
1813 * Wearable extender for notification actions. To add extensions to an action,
1814 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1815 * the {@code WearableExtender()} constructor and apply it to a
1816 * {@link android.app.Notification.Action.Builder} using
1817 * {@link android.app.Notification.Action.Builder#extend}.
1818 *
1819 * <pre class="prettyprint">
1820 * Notification.Action action = new Notification.Action.Builder(
1821 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001822 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001823 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001824 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001825 */
1826 public static final class WearableExtender implements Extender {
1827 /** Notification action extra which contains wearable extensions */
1828 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1829
Pete Gastaf6781d2014-10-07 15:17:05 -04001830 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001831 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001832 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1833 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1834 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001835
1836 // Flags bitwise-ored to mFlags
1837 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001838 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001839 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001840
1841 // Default value for flags integer
1842 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1843
1844 private int mFlags = DEFAULT_FLAGS;
1845
Pete Gastaf6781d2014-10-07 15:17:05 -04001846 private CharSequence mInProgressLabel;
1847 private CharSequence mConfirmLabel;
1848 private CharSequence mCancelLabel;
1849
Griff Hazen61a9e862014-05-22 16:05:19 -07001850 /**
1851 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1852 * options.
1853 */
1854 public WearableExtender() {
1855 }
1856
1857 /**
1858 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1859 * wearable options present in an existing notification action.
1860 * @param action the notification action to inspect.
1861 */
1862 public WearableExtender(Action action) {
1863 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1864 if (wearableBundle != null) {
1865 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001866 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1867 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1868 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001869 }
1870 }
1871
1872 /**
1873 * Apply wearable extensions to a notification action that is being built. This is
1874 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1875 * method of {@link android.app.Notification.Action.Builder}.
1876 */
1877 @Override
1878 public Action.Builder extend(Action.Builder builder) {
1879 Bundle wearableBundle = new Bundle();
1880
1881 if (mFlags != DEFAULT_FLAGS) {
1882 wearableBundle.putInt(KEY_FLAGS, mFlags);
1883 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001884 if (mInProgressLabel != null) {
1885 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1886 }
1887 if (mConfirmLabel != null) {
1888 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1889 }
1890 if (mCancelLabel != null) {
1891 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1892 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001893
1894 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1895 return builder;
1896 }
1897
1898 @Override
1899 public WearableExtender clone() {
1900 WearableExtender that = new WearableExtender();
1901 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001902 that.mInProgressLabel = this.mInProgressLabel;
1903 that.mConfirmLabel = this.mConfirmLabel;
1904 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001905 return that;
1906 }
1907
1908 /**
1909 * Set whether this action is available when the wearable device is not connected to
1910 * a companion device. The user can still trigger this action when the wearable device is
1911 * offline, but a visual hint will indicate that the action may not be available.
1912 * Defaults to true.
1913 */
1914 public WearableExtender setAvailableOffline(boolean availableOffline) {
1915 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1916 return this;
1917 }
1918
1919 /**
1920 * Get whether this action is available when the wearable device is not connected to
1921 * a companion device. The user can still trigger this action when the wearable device is
1922 * offline, but a visual hint will indicate that the action may not be available.
1923 * Defaults to true.
1924 */
1925 public boolean isAvailableOffline() {
1926 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1927 }
1928
1929 private void setFlag(int mask, boolean value) {
1930 if (value) {
1931 mFlags |= mask;
1932 } else {
1933 mFlags &= ~mask;
1934 }
1935 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001936
1937 /**
1938 * Set a label to display while the wearable is preparing to automatically execute the
1939 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1940 *
1941 * @param label the label to display while the action is being prepared to execute
1942 * @return this object for method chaining
1943 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001944 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001945 public WearableExtender setInProgressLabel(CharSequence label) {
1946 mInProgressLabel = label;
1947 return this;
1948 }
1949
1950 /**
1951 * Get the label to display while the wearable is preparing to automatically execute
1952 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1953 *
1954 * @return the label to display while the action is being prepared to execute
1955 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001956 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001957 public CharSequence getInProgressLabel() {
1958 return mInProgressLabel;
1959 }
1960
1961 /**
1962 * Set a label to display to confirm that the action should be executed.
1963 * This is usually an imperative verb like "Send".
1964 *
1965 * @param label the label to confirm the action should be executed
1966 * @return this object for method chaining
1967 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001968 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001969 public WearableExtender setConfirmLabel(CharSequence label) {
1970 mConfirmLabel = label;
1971 return this;
1972 }
1973
1974 /**
1975 * Get the label to display to confirm that the action should be executed.
1976 * This is usually an imperative verb like "Send".
1977 *
1978 * @return the label to confirm the action should be executed
1979 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001980 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001981 public CharSequence getConfirmLabel() {
1982 return mConfirmLabel;
1983 }
1984
1985 /**
1986 * Set a label to display to cancel the action.
1987 * This is usually an imperative verb, like "Cancel".
1988 *
1989 * @param label the label to display to cancel the action
1990 * @return this object for method chaining
1991 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001992 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001993 public WearableExtender setCancelLabel(CharSequence label) {
1994 mCancelLabel = label;
1995 return this;
1996 }
1997
1998 /**
1999 * Get the label to display to cancel the action.
2000 * This is usually an imperative verb like "Cancel".
2001 *
2002 * @return the label to display to cancel the action
2003 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002004 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002005 public CharSequence getCancelLabel() {
2006 return mCancelLabel;
2007 }
Alex Hills9ab3a232016-04-05 14:54:56 -04002008
2009 /**
2010 * Set a hint that this Action will launch an {@link Activity} directly, telling the
2011 * platform that it can generate the appropriate transitions.
2012 * @param hintLaunchesActivity {@code true} if the content intent will launch
2013 * an activity and transitions should be generated, false otherwise.
2014 * @return this object for method chaining
2015 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04002016 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04002017 boolean hintLaunchesActivity) {
2018 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
2019 return this;
2020 }
2021
2022 /**
2023 * Get a hint that this Action will launch an {@link Activity} directly, telling the
2024 * platform that it can generate the appropriate transitions
2025 * @return {@code true} if the content intent will launch an activity and transitions
2026 * should be generated, false otherwise. The default value is {@code false} if this was
2027 * never set.
2028 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04002029 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04002030 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
2031 }
Alex Hills9f087612016-06-07 09:08:59 -04002032
2033 /**
2034 * Set a hint that this Action should be displayed inline.
2035 *
2036 * @param hintDisplayInline {@code true} if action should be displayed inline, false
2037 * otherwise
2038 * @return this object for method chaining
2039 */
2040 public WearableExtender setHintDisplayActionInline(
2041 boolean hintDisplayInline) {
2042 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
2043 return this;
2044 }
2045
2046 /**
2047 * Get a hint that this Action should be displayed inline.
2048 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08002049 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04002050 * otherwise. The default value is {@code false} if this was never set.
2051 */
2052 public boolean getHintDisplayActionInline() {
2053 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
2054 }
Griff Hazen61a9e862014-05-22 16:05:19 -07002055 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002056
2057 /**
2058 * Provides meaning to an {@link Action} that hints at what the associated
2059 * {@link PendingIntent} will do. For example, an {@link Action} with a
2060 * {@link PendingIntent} that replies to a text message notification may have the
2061 * {@link #SEMANTIC_ACTION_REPLY} {@code SemanticAction} set within it.
2062 *
2063 * @hide
2064 */
2065 @IntDef(prefix = { "SEMANTIC_ACTION_" }, value = {
2066 SEMANTIC_ACTION_NONE,
2067 SEMANTIC_ACTION_REPLY,
2068 SEMANTIC_ACTION_MARK_AS_READ,
2069 SEMANTIC_ACTION_MARK_AS_UNREAD,
2070 SEMANTIC_ACTION_DELETE,
2071 SEMANTIC_ACTION_ARCHIVE,
2072 SEMANTIC_ACTION_MUTE,
2073 SEMANTIC_ACTION_UNMUTE,
2074 SEMANTIC_ACTION_THUMBS_UP,
Kodlee Yincda5b092018-02-15 15:34:53 -08002075 SEMANTIC_ACTION_THUMBS_DOWN,
Gustav Sennton1463d832018-11-06 16:12:48 +00002076 SEMANTIC_ACTION_CALL,
2077 SEMANTIC_ACTION_CONTEXTUAL_SUGGESTION
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002078 })
2079 @Retention(RetentionPolicy.SOURCE)
2080 public @interface SemanticAction {}
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002081 }
2082
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002083 /**
2084 * Array of all {@link Action} structures attached to this notification by
2085 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
2086 * {@link android.service.notification.NotificationListenerService} that provide an alternative
2087 * interface for invoking actions.
2088 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05002089 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002090
2091 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002092 * Replacement version of this notification whose content will be shown
2093 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
2094 * and {@link #VISIBILITY_PUBLIC}.
2095 */
2096 public Notification publicVersion;
2097
2098 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002099 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08002100 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002101 */
2102 public Notification()
2103 {
2104 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002105 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002106 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 }
2108
2109 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110 * @hide
2111 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002112 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002113 public Notification(Context context, int icon, CharSequence tickerText, long when,
2114 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
2115 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04002116 new Builder(context)
2117 .setWhen(when)
2118 .setSmallIcon(icon)
2119 .setTicker(tickerText)
2120 .setContentTitle(contentTitle)
2121 .setContentText(contentText)
2122 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
2123 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002124 }
2125
2126 /**
2127 * Constructs a Notification object with the information needed to
2128 * have a status bar icon without the standard expanded view.
2129 *
2130 * @param icon The resource id of the icon to put in the status bar.
2131 * @param tickerText The text that flows by in the status bar when the notification first
2132 * activates.
2133 * @param when The time to show in the time field. In the System.currentTimeMillis
2134 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08002135 *
2136 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002137 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002138 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002139 public Notification(int icon, CharSequence tickerText, long when)
2140 {
2141 this.icon = icon;
2142 this.tickerText = tickerText;
2143 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002144 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002145 }
2146
2147 /**
2148 * Unflatten the notification from a parcel.
2149 */
Svet Ganovddb94882016-06-23 19:55:24 -07002150 @SuppressWarnings("unchecked")
2151 public Notification(Parcel parcel) {
2152 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
2153 // intents in extras are always written as the last entry.
2154 readFromParcelImpl(parcel);
2155 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002156 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07002157 }
2158
2159 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002160 {
2161 int version = parcel.readInt();
2162
Adrian Roosfb921842017-10-26 14:49:56 +02002163 mWhitelistToken = parcel.readStrongBinder();
2164 if (mWhitelistToken == null) {
2165 mWhitelistToken = processWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -07002166 }
2167 // Propagate this token to all pending intents that are unmarshalled from the parcel.
Adrian Roosfb921842017-10-26 14:49:56 +02002168 parcel.setClassCookie(PendingIntent.class, mWhitelistToken);
Dianne Hackborn98305522017-05-05 17:53:53 -07002169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002170 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002171 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04002172 if (parcel.readInt() != 0) {
2173 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04002174 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
2175 icon = mSmallIcon.getResId();
2176 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002177 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178 number = parcel.readInt();
2179 if (parcel.readInt() != 0) {
2180 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2181 }
2182 if (parcel.readInt() != 0) {
2183 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2184 }
2185 if (parcel.readInt() != 0) {
2186 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2187 }
2188 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002189 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002190 }
2191 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002192 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
2193 }
Joe Onorato561d3852010-11-20 18:09:34 -08002194 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002195 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08002196 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002197 defaults = parcel.readInt();
2198 flags = parcel.readInt();
2199 if (parcel.readInt() != 0) {
2200 sound = Uri.CREATOR.createFromParcel(parcel);
2201 }
2202
2203 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04002204 if (parcel.readInt() != 0) {
2205 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
2206 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002207 vibrate = parcel.createLongArray();
2208 ledARGB = parcel.readInt();
2209 ledOnMS = parcel.readInt();
2210 ledOffMS = parcel.readInt();
2211 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002212
2213 if (parcel.readInt() != 0) {
2214 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2215 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002216
2217 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002218
John Spurlockfd7f1e02014-03-18 16:41:57 -04002219 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002220
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002221 mGroupKey = parcel.readString();
2222
2223 mSortKey = parcel.readString();
2224
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06002225 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Robin Leead7e72a2017-12-04 15:45:46 +01002226 fixDuplicateExtras();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002227
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002228 actions = parcel.createTypedArray(Action.CREATOR); // may be null
2229
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002230 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002231 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2232 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002233
Chris Wren8fd39ec2014-02-27 17:43:26 -05002234 if (parcel.readInt() != 0) {
2235 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2236 }
2237
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002238 visibility = parcel.readInt();
2239
2240 if (parcel.readInt() != 0) {
2241 publicVersion = Notification.CREATOR.createFromParcel(parcel);
2242 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002243
2244 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002245
2246 if (parcel.readInt() != 0) {
2247 mChannelId = parcel.readString();
2248 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002249 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05002250
2251 if (parcel.readInt() != 0) {
2252 mShortcutId = parcel.readString();
2253 }
2254
2255 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04002256
2257 if (parcel.readInt() != 0) {
2258 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2259 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002260
2261 mGroupAlertBehavior = parcel.readInt();
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002262 if (parcel.readInt() != 0) {
2263 mAppOverlayIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2264 }
Gustav Sennton761884c2018-11-19 17:40:19 +00002265
2266 mAllowSystemGeneratedContextualActions = parcel.readBoolean();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002267 }
2268
Andy Stadler110988c2010-12-03 14:29:16 -08002269 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07002270 public Notification clone() {
2271 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04002272 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002273 return that;
2274 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002275
Daniel Sandler1a497d32013-04-18 14:52:45 -04002276 /**
2277 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
2278 * of this into that.
2279 * @hide
2280 */
2281 public void cloneInto(Notification that, boolean heavy) {
Adrian Roosfb921842017-10-26 14:49:56 +02002282 that.mWhitelistToken = this.mWhitelistToken;
Joe Onorato18e69df2010-05-17 22:26:12 -07002283 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002284 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002285 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07002286 that.number = this.number;
2287
2288 // PendingIntents are global, so there's no reason (or way) to clone them.
2289 that.contentIntent = this.contentIntent;
2290 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002291 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07002292
2293 if (this.tickerText != null) {
2294 that.tickerText = this.tickerText.toString();
2295 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002296 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002297 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04002298 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002299 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07002300 that.contentView = this.contentView.clone();
2301 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002302 if (heavy && this.mLargeIcon != null) {
2303 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08002304 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01002305 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07002306 that.sound = this.sound; // android.net.Uri is immutable
2307 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002308 if (this.audioAttributes != null) {
2309 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
2310 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002311
2312 final long[] vibrate = this.vibrate;
2313 if (vibrate != null) {
2314 final int N = vibrate.length;
2315 final long[] vib = that.vibrate = new long[N];
2316 System.arraycopy(vibrate, 0, vib, 0, N);
2317 }
2318
2319 that.ledARGB = this.ledARGB;
2320 that.ledOnMS = this.ledOnMS;
2321 that.ledOffMS = this.ledOffMS;
2322 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002323
Joe Onorato18e69df2010-05-17 22:26:12 -07002324 that.flags = this.flags;
2325
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002326 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08002327
John Spurlockfd7f1e02014-03-18 16:41:57 -04002328 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002329
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002330 that.mGroupKey = this.mGroupKey;
2331
2332 that.mSortKey = this.mSortKey;
2333
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002334 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002335 try {
2336 that.extras = new Bundle(this.extras);
2337 // will unparcel
2338 that.extras.size();
2339 } catch (BadParcelableException e) {
2340 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
2341 that.extras = null;
2342 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002343 }
2344
Felipe Lemedd85da62016-06-28 11:29:54 -07002345 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2346 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002347 }
2348
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002349 if (this.actions != null) {
2350 that.actions = new Action[this.actions.length];
2351 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002352 if ( this.actions[i] != null) {
2353 that.actions[i] = this.actions[i].clone();
2354 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002355 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002356 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002357
Daniel Sandler1a497d32013-04-18 14:52:45 -04002358 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002359 that.bigContentView = this.bigContentView.clone();
2360 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002361
Chris Wren8fd39ec2014-02-27 17:43:26 -05002362 if (heavy && this.headsUpContentView != null) {
2363 that.headsUpContentView = this.headsUpContentView.clone();
2364 }
2365
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002366 that.visibility = this.visibility;
2367
2368 if (this.publicVersion != null) {
2369 that.publicVersion = new Notification();
2370 this.publicVersion.cloneInto(that.publicVersion, heavy);
2371 }
2372
Dan Sandler26e81cf2014-05-06 10:01:27 -04002373 that.color = this.color;
2374
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002375 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002376 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002377 that.mShortcutId = this.mShortcutId;
2378 that.mBadgeIcon = this.mBadgeIcon;
2379 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002380 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002381 that.mAppOverlayIntent = this.mAppOverlayIntent;
Gustav Sennton761884c2018-11-19 17:40:19 +00002382 that.mAllowSystemGeneratedContextualActions = this.mAllowSystemGeneratedContextualActions;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002383
Daniel Sandler1a497d32013-04-18 14:52:45 -04002384 if (!heavy) {
2385 that.lightenPayload(); // will clean out extras
2386 }
2387 }
2388
2389 /**
Jeff Sharkey23b31182018-04-18 21:32:12 -06002390 * Note all {@link Uri} that are referenced internally, with the expectation
2391 * that Uri permission grants will need to be issued to ensure the recipient
2392 * of this object is able to render its contents.
2393 *
2394 * @hide
2395 */
2396 public void visitUris(@NonNull Consumer<Uri> visitor) {
2397 visitor.accept(sound);
2398
2399 if (tickerView != null) tickerView.visitUris(visitor);
2400 if (contentView != null) contentView.visitUris(visitor);
2401 if (bigContentView != null) bigContentView.visitUris(visitor);
2402 if (headsUpContentView != null) headsUpContentView.visitUris(visitor);
2403
2404 if (extras != null) {
2405 visitor.accept(extras.getParcelable(EXTRA_AUDIO_CONTENTS_URI));
Robin Leed107af62018-04-27 13:55:56 +02002406 if (extras.containsKey(EXTRA_BACKGROUND_IMAGE_URI)) {
2407 visitor.accept(Uri.parse(extras.getString(EXTRA_BACKGROUND_IMAGE_URI)));
2408 }
Jeff Sharkey23b31182018-04-18 21:32:12 -06002409 }
2410
2411 if (MessagingStyle.class.equals(getNotificationStyle()) && extras != null) {
2412 final Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
2413 if (!ArrayUtils.isEmpty(messages)) {
2414 for (MessagingStyle.Message message : MessagingStyle.Message
2415 .getMessagesFromBundleArray(messages)) {
2416 visitor.accept(message.getDataUri());
2417 }
2418 }
2419
2420 final Parcelable[] historic = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
2421 if (!ArrayUtils.isEmpty(historic)) {
2422 for (MessagingStyle.Message message : MessagingStyle.Message
2423 .getMessagesFromBundleArray(historic)) {
2424 visitor.accept(message.getDataUri());
2425 }
2426 }
2427 }
2428 }
2429
2430 /**
Daniel Sandler1a497d32013-04-18 14:52:45 -04002431 * Removes heavyweight parts of the Notification object for archival or for sending to
2432 * listeners when the full contents are not necessary.
2433 * @hide
2434 */
2435 public final void lightenPayload() {
2436 tickerView = null;
2437 contentView = null;
2438 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002439 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002440 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002441 if (extras != null && !extras.isEmpty()) {
2442 final Set<String> keyset = extras.keySet();
2443 final int N = keyset.size();
2444 final String[] keys = keyset.toArray(new String[N]);
2445 for (int i=0; i<N; i++) {
2446 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002447 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2448 continue;
2449 }
Dan Sandler50128532015-12-08 15:42:41 -05002450 final Object obj = extras.get(key);
2451 if (obj != null &&
2452 ( obj instanceof Parcelable
2453 || obj instanceof Parcelable[]
2454 || obj instanceof SparseArray
2455 || obj instanceof ArrayList)) {
2456 extras.remove(key);
2457 }
2458 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002459 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002460 }
2461
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002462 /**
2463 * Make sure this CharSequence is safe to put into a bundle, which basically
2464 * means it had better not be some custom Parcelable implementation.
2465 * @hide
2466 */
2467 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002468 if (cs == null) return cs;
2469 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2470 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2471 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002472 if (cs instanceof Parcelable) {
2473 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2474 + " instance is a custom Parcelable and not allowed in Notification");
2475 return cs.toString();
2476 }
Selim Cinek60a54252016-02-26 17:03:25 -08002477 return removeTextSizeSpans(cs);
2478 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002479
Selim Cinek60a54252016-02-26 17:03:25 -08002480 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2481 if (charSequence instanceof Spanned) {
2482 Spanned ss = (Spanned) charSequence;
2483 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2484 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2485 for (Object span : spans) {
2486 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002487 if (resultSpan instanceof CharacterStyle) {
2488 resultSpan = ((CharacterStyle) span).getUnderlying();
2489 }
2490 if (resultSpan instanceof TextAppearanceSpan) {
2491 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002492 resultSpan = new TextAppearanceSpan(
2493 originalSpan.getFamily(),
2494 originalSpan.getTextStyle(),
2495 -1,
2496 originalSpan.getTextColor(),
2497 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002498 } else if (resultSpan instanceof RelativeSizeSpan
2499 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002500 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002501 } else {
2502 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002503 }
2504 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2505 ss.getSpanFlags(span));
2506 }
2507 return builder;
2508 }
2509 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002510 }
2511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002512 public int describeContents() {
2513 return 0;
2514 }
2515
2516 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002517 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002518 */
Svet Ganovddb94882016-06-23 19:55:24 -07002519 public void writeToParcel(Parcel parcel, int flags) {
2520 // We need to mark all pending intents getting into the notification
2521 // system as being put there to later allow the notification ranker
2522 // to launch them and by doing so add the app to the battery saver white
2523 // list for a short period of time. The problem is that the system
2524 // cannot look into the extras as there may be parcelables there that
2525 // the platform does not know how to handle. To go around that we have
2526 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002527 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002528 if (collectPendingIntents) {
2529 PendingIntent.setOnMarshaledListener(
2530 (PendingIntent intent, Parcel out, int outFlags) -> {
2531 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002532 if (allPendingIntents == null) {
2533 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002534 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002535 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002536 }
2537 });
2538 }
2539 try {
2540 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002541 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002542 writeToParcelImpl(parcel, flags);
2543 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002544 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002545 } finally {
2546 if (collectPendingIntents) {
2547 PendingIntent.setOnMarshaledListener(null);
2548 }
2549 }
2550 }
2551
2552 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002553 parcel.writeInt(1);
2554
Adrian Roosfb921842017-10-26 14:49:56 +02002555 parcel.writeStrongBinder(mWhitelistToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002556 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002557 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002558 if (mSmallIcon == null && icon != 0) {
2559 // you snuck an icon in here without using the builder; let's try to keep it
2560 mSmallIcon = Icon.createWithResource("", icon);
2561 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002562 if (mSmallIcon != null) {
2563 parcel.writeInt(1);
2564 mSmallIcon.writeToParcel(parcel, 0);
2565 } else {
2566 parcel.writeInt(0);
2567 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002568 parcel.writeInt(number);
2569 if (contentIntent != null) {
2570 parcel.writeInt(1);
2571 contentIntent.writeToParcel(parcel, 0);
2572 } else {
2573 parcel.writeInt(0);
2574 }
2575 if (deleteIntent != null) {
2576 parcel.writeInt(1);
2577 deleteIntent.writeToParcel(parcel, 0);
2578 } else {
2579 parcel.writeInt(0);
2580 }
2581 if (tickerText != null) {
2582 parcel.writeInt(1);
2583 TextUtils.writeToParcel(tickerText, parcel, flags);
2584 } else {
2585 parcel.writeInt(0);
2586 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002587 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002588 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002589 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002590 } else {
2591 parcel.writeInt(0);
2592 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002593 if (contentView != null) {
2594 parcel.writeInt(1);
2595 contentView.writeToParcel(parcel, 0);
2596 } else {
2597 parcel.writeInt(0);
2598 }
Selim Cinek279fa862016-06-14 10:57:25 -07002599 if (mLargeIcon == null && largeIcon != null) {
2600 // you snuck an icon in here without using the builder; let's try to keep it
2601 mLargeIcon = Icon.createWithBitmap(largeIcon);
2602 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002603 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002604 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002605 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002606 } else {
2607 parcel.writeInt(0);
2608 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002609
2610 parcel.writeInt(defaults);
2611 parcel.writeInt(this.flags);
2612
2613 if (sound != null) {
2614 parcel.writeInt(1);
2615 sound.writeToParcel(parcel, 0);
2616 } else {
2617 parcel.writeInt(0);
2618 }
2619 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002620
2621 if (audioAttributes != null) {
2622 parcel.writeInt(1);
2623 audioAttributes.writeToParcel(parcel, 0);
2624 } else {
2625 parcel.writeInt(0);
2626 }
2627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002628 parcel.writeLongArray(vibrate);
2629 parcel.writeInt(ledARGB);
2630 parcel.writeInt(ledOnMS);
2631 parcel.writeInt(ledOffMS);
2632 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002633
2634 if (fullScreenIntent != null) {
2635 parcel.writeInt(1);
2636 fullScreenIntent.writeToParcel(parcel, 0);
2637 } else {
2638 parcel.writeInt(0);
2639 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002640
2641 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002642
John Spurlockfd7f1e02014-03-18 16:41:57 -04002643 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002644
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002645 parcel.writeString(mGroupKey);
2646
2647 parcel.writeString(mSortKey);
2648
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002649 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002650
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002651 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002652
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002653 if (bigContentView != null) {
2654 parcel.writeInt(1);
2655 bigContentView.writeToParcel(parcel, 0);
2656 } else {
2657 parcel.writeInt(0);
2658 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002659
Chris Wren8fd39ec2014-02-27 17:43:26 -05002660 if (headsUpContentView != null) {
2661 parcel.writeInt(1);
2662 headsUpContentView.writeToParcel(parcel, 0);
2663 } else {
2664 parcel.writeInt(0);
2665 }
2666
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002667 parcel.writeInt(visibility);
2668
2669 if (publicVersion != null) {
2670 parcel.writeInt(1);
2671 publicVersion.writeToParcel(parcel, 0);
2672 } else {
2673 parcel.writeInt(0);
2674 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002675
2676 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002677
2678 if (mChannelId != null) {
2679 parcel.writeInt(1);
2680 parcel.writeString(mChannelId);
2681 } else {
2682 parcel.writeInt(0);
2683 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002684 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002685
2686 if (mShortcutId != null) {
2687 parcel.writeInt(1);
2688 parcel.writeString(mShortcutId);
2689 } else {
2690 parcel.writeInt(0);
2691 }
2692
2693 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002694
2695 if (mSettingsText != null) {
2696 parcel.writeInt(1);
2697 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2698 } else {
2699 parcel.writeInt(0);
2700 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002701
2702 parcel.writeInt(mGroupAlertBehavior);
Julia Reynoldsfc640012018-02-21 12:25:27 -05002703
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002704 if (mAppOverlayIntent != null) {
2705 parcel.writeInt(1);
2706 mAppOverlayIntent.writeToParcel(parcel, 0);
2707 } else {
2708 parcel.writeInt(0);
2709 }
2710
Gustav Sennton761884c2018-11-19 17:40:19 +00002711 parcel.writeBoolean(mAllowSystemGeneratedContextualActions);
2712
Julia Reynoldsfc640012018-02-21 12:25:27 -05002713 // mUsesStandardHeader is not written because it should be recomputed in listeners
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002714 }
2715
2716 /**
2717 * Parcelable.Creator that instantiates Notification objects
2718 */
2719 public static final Parcelable.Creator<Notification> CREATOR
2720 = new Parcelable.Creator<Notification>()
2721 {
2722 public Notification createFromParcel(Parcel parcel)
2723 {
2724 return new Notification(parcel);
2725 }
2726
2727 public Notification[] newArray(int size)
2728 {
2729 return new Notification[size];
2730 }
2731 };
2732
2733 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05002734 * @hide
2735 */
2736 public static boolean areActionsVisiblyDifferent(Notification first, Notification second) {
2737 Notification.Action[] firstAs = first.actions;
2738 Notification.Action[] secondAs = second.actions;
2739 if (firstAs == null && secondAs != null || firstAs != null && secondAs == null) {
2740 return true;
2741 }
2742 if (firstAs != null && secondAs != null) {
2743 if (firstAs.length != secondAs.length) {
2744 return true;
2745 }
2746 for (int i = 0; i < firstAs.length; i++) {
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -04002747 if (!Objects.equals(String.valueOf(firstAs[i].title),
2748 String.valueOf(secondAs[i].title))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05002749 return true;
2750 }
2751 RemoteInput[] firstRs = firstAs[i].getRemoteInputs();
2752 RemoteInput[] secondRs = secondAs[i].getRemoteInputs();
2753 if (firstRs == null) {
2754 firstRs = new RemoteInput[0];
2755 }
2756 if (secondRs == null) {
2757 secondRs = new RemoteInput[0];
2758 }
2759 if (firstRs.length != secondRs.length) {
2760 return true;
2761 }
2762 for (int j = 0; j < firstRs.length; j++) {
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -04002763 if (!Objects.equals(String.valueOf(firstRs[j].getLabel()),
2764 String.valueOf(secondRs[j].getLabel()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05002765 return true;
2766 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05002767 }
2768 }
2769 }
2770 return false;
2771 }
2772
2773 /**
2774 * @hide
2775 */
2776 public static boolean areStyledNotificationsVisiblyDifferent(Builder first, Builder second) {
2777 if (first.getStyle() == null) {
2778 return second.getStyle() != null;
2779 }
2780 if (second.getStyle() == null) {
2781 return true;
2782 }
2783 return first.getStyle().areNotificationsVisiblyDifferent(second.getStyle());
2784 }
2785
2786 /**
2787 * @hide
2788 */
2789 public static boolean areRemoteViewsChanged(Builder first, Builder second) {
Julia Reynoldse5c60452018-04-30 14:41:36 -04002790 if (!Objects.equals(first.usesStandardHeader(), second.usesStandardHeader())) {
2791 return true;
2792 }
2793
2794 if (areRemoteViewsChanged(first.mN.contentView, second.mN.contentView)) {
2795 return true;
2796 }
2797 if (areRemoteViewsChanged(first.mN.bigContentView, second.mN.bigContentView)) {
2798 return true;
2799 }
2800 if (areRemoteViewsChanged(first.mN.headsUpContentView, second.mN.headsUpContentView)) {
2801 return true;
2802 }
2803
2804 return false;
2805 }
2806
2807 private static boolean areRemoteViewsChanged(RemoteViews first, RemoteViews second) {
2808 if (first == null && second == null) {
2809 return false;
2810 }
2811 if (first == null && second != null || first != null && second == null) {
2812 return true;
2813 }
2814
2815 if (!Objects.equals(first.getLayoutId(), second.getLayoutId())) {
2816 return true;
2817 }
2818
2819 if (!Objects.equals(first.getSequenceNumber(), second.getSequenceNumber())) {
2820 return true;
2821 }
2822
2823 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05002824 }
2825
2826 /**
Robin Leead7e72a2017-12-04 15:45:46 +01002827 * Parcelling creates multiple copies of objects in {@code extras}. Fix them.
2828 * <p>
2829 * For backwards compatibility {@code extras} holds some references to "real" member data such
2830 * as {@link getLargeIcon()} which is mirrored by {@link #EXTRA_LARGE_ICON}. This is mostly
2831 * fine as long as the object stays in one process.
2832 * <p>
2833 * However, once the notification goes into a parcel each reference gets marshalled separately,
2834 * wasting memory. Especially with large images on Auto and TV, this is worth fixing.
2835 */
2836 private void fixDuplicateExtras() {
2837 if (extras != null) {
Robin Leead7e72a2017-12-04 15:45:46 +01002838 fixDuplicateExtra(mLargeIcon, EXTRA_LARGE_ICON);
2839 }
2840 }
2841
2842 /**
2843 * If we find an extra that's exactly the same as one of the "real" fields but refers to a
2844 * separate object, replace it with the field's version to avoid holding duplicate copies.
2845 */
2846 private void fixDuplicateExtra(@Nullable Parcelable original, @NonNull String extraName) {
2847 if (original != null && extras.getParcelable(extraName) != null) {
2848 extras.putParcelable(extraName, original);
2849 }
2850 }
2851
2852 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002853 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2854 * layout.
2855 *
2856 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2857 * in the view.</p>
2858 * @param context The context for your application / activity.
2859 * @param contentTitle The title that goes in the expanded entry.
2860 * @param contentText The text that goes in the expanded entry.
2861 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2862 * If this is an activity, it must include the
2863 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002864 * that you take care of task management as described in the
2865 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2866 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002867 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002868 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002869 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002870 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002871 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872 public void setLatestEventInfo(Context context,
2873 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002874 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2875 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2876 new Throwable());
2877 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002878
Selim Cinek4ac6f602016-06-13 15:47:03 -07002879 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2880 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2881 }
2882
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002883 // ensure that any information already set directly is preserved
2884 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002885
2886 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002887 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002888 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002889 }
2890 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002891 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002892 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002893 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002894
2895 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 }
2897
Julia Reynoldsda303542015-11-23 14:00:20 -05002898 /**
2899 * @hide
2900 */
2901 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002902 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002903 }
2904
2905 /**
2906 * @hide
2907 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002908 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002909 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002910 }
2911
Yi Jin6b514142017-10-30 14:54:12 -07002912 /**
2913 * @hide
2914 */
2915 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2916 long token = proto.start(fieldId);
2917 proto.write(NotificationProto.CHANNEL_ID, getChannelId());
2918 proto.write(NotificationProto.HAS_TICKER_TEXT, this.tickerText != null);
2919 proto.write(NotificationProto.FLAGS, this.flags);
2920 proto.write(NotificationProto.COLOR, this.color);
2921 proto.write(NotificationProto.CATEGORY, this.category);
2922 proto.write(NotificationProto.GROUP_KEY, this.mGroupKey);
2923 proto.write(NotificationProto.SORT_KEY, this.mSortKey);
2924 if (this.actions != null) {
2925 proto.write(NotificationProto.ACTION_LENGTH, this.actions.length);
2926 }
2927 if (this.visibility >= VISIBILITY_SECRET && this.visibility <= VISIBILITY_PUBLIC) {
2928 proto.write(NotificationProto.VISIBILITY, this.visibility);
2929 }
2930 if (publicVersion != null) {
2931 publicVersion.writeToProto(proto, NotificationProto.PUBLIC_VERSION);
2932 }
2933 proto.end(token);
2934 }
2935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002936 @Override
2937 public String toString() {
2938 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002939 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04002940 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002941 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002942 sb.append(priority);
2943 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002944 if (contentView != null) {
2945 sb.append(contentView.getPackage());
2946 sb.append("/0x");
2947 sb.append(Integer.toHexString(contentView.getLayoutId()));
2948 } else {
2949 sb.append("null");
2950 }
2951 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002952 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2953 sb.append("default");
2954 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002955 int N = this.vibrate.length-1;
2956 sb.append("[");
2957 for (int i=0; i<N; i++) {
2958 sb.append(this.vibrate[i]);
2959 sb.append(',');
2960 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002961 if (N != -1) {
2962 sb.append(this.vibrate[N]);
2963 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002964 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002965 } else {
2966 sb.append("null");
2967 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002968 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002969 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002970 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002971 } else if (this.sound != null) {
2972 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002973 } else {
2974 sb.append("null");
2975 }
Chris Wren365b6d32015-07-16 10:39:26 -04002976 if (this.tickerText != null) {
2977 sb.append(" tick");
2978 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002979 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002980 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002981 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002982 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002983 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002984 if (this.category != null) {
2985 sb.append(" category=");
2986 sb.append(this.category);
2987 }
2988 if (this.mGroupKey != null) {
2989 sb.append(" groupKey=");
2990 sb.append(this.mGroupKey);
2991 }
2992 if (this.mSortKey != null) {
2993 sb.append(" sortKey=");
2994 sb.append(this.mSortKey);
2995 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002996 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002997 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002998 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002999 }
3000 sb.append(" vis=");
3001 sb.append(visibilityToString(this.visibility));
3002 if (this.publicVersion != null) {
3003 sb.append(" publicVersion=");
3004 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003005 }
3006 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003007 return sb.toString();
3008 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003009
Dan Sandler1b718782014-07-18 12:43:45 -04003010 /**
3011 * {@hide}
3012 */
3013 public static String visibilityToString(int vis) {
3014 switch (vis) {
3015 case VISIBILITY_PRIVATE:
3016 return "PRIVATE";
3017 case VISIBILITY_PUBLIC:
3018 return "PUBLIC";
3019 case VISIBILITY_SECRET:
3020 return "SECRET";
3021 default:
3022 return "UNKNOWN(" + String.valueOf(vis) + ")";
3023 }
3024 }
3025
Joe Onoratocb109a02011-01-18 17:57:41 -08003026 /**
John Spurlock1d881a12015-03-18 19:21:54 -04003027 * {@hide}
3028 */
3029 public static String priorityToString(@Priority int pri) {
3030 switch (pri) {
3031 case PRIORITY_MIN:
3032 return "MIN";
3033 case PRIORITY_LOW:
3034 return "LOW";
3035 case PRIORITY_DEFAULT:
3036 return "DEFAULT";
3037 case PRIORITY_HIGH:
3038 return "HIGH";
3039 case PRIORITY_MAX:
3040 return "MAX";
3041 default:
3042 return "UNKNOWN(" + String.valueOf(pri) + ")";
3043 }
3044 }
3045
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04003046 /**
3047 * @hide
3048 */
3049 public boolean hasCompletedProgress() {
3050 // not a progress notification; can't be complete
3051 if (!extras.containsKey(EXTRA_PROGRESS)
3052 || !extras.containsKey(EXTRA_PROGRESS_MAX)) {
3053 return false;
3054 }
3055 // many apps use max 0 for 'indeterminate'; not complete
3056 if (extras.getInt(EXTRA_PROGRESS_MAX) == 0) {
3057 return false;
3058 }
3059 return extras.getInt(EXTRA_PROGRESS) == extras.getInt(EXTRA_PROGRESS_MAX);
3060 }
3061
Jeff Sharkey000ce802017-04-29 13:13:27 -06003062 /** @removed */
3063 @Deprecated
Julia Reynolds37856052016-11-11 09:20:07 -05003064 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003065 return mChannelId;
3066 }
3067
3068 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003069 * Returns the id of the channel this notification posts to.
3070 */
3071 public String getChannelId() {
3072 return mChannelId;
3073 }
3074
Jeff Sharkey000ce802017-04-29 13:13:27 -06003075 /** @removed */
3076 @Deprecated
Julia Reynolds2a128742016-11-28 14:29:25 -05003077 public long getTimeout() {
3078 return mTimeout;
3079 }
3080
3081 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003082 * Returns the duration from posting after which this notification should be canceled by the
3083 * system, if it's not canceled already.
3084 */
3085 public long getTimeoutAfter() {
3086 return mTimeout;
3087 }
3088
3089 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003090 * Returns what icon should be shown for this notification if it is being displayed in a
3091 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
3092 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
3093 */
3094 public int getBadgeIconType() {
3095 return mBadgeIcon;
3096 }
3097
3098 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003099 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04003100 *
3101 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
3102 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003103 */
3104 public String getShortcutId() {
3105 return mShortcutId;
3106 }
3107
Julia Reynolds3aedded2017-03-31 14:42:09 -04003108
3109 /**
3110 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
3111 */
3112 public CharSequence getSettingsText() {
3113 return mSettingsText;
3114 }
3115
Julia Reynolds13d898c2017-02-02 12:22:05 -05003116 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003117 * Returns which type of notifications in a group are responsible for audibly alerting the
3118 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
3119 * {@link #GROUP_ALERT_SUMMARY}.
3120 */
3121 public @GroupAlertBehavior int getGroupAlertBehavior() {
3122 return mGroupAlertBehavior;
3123 }
3124
3125 /**
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003126 * Returns the intent that will be used to display app content in a floating window over the
3127 * existing foreground activity.
3128 */
3129 public PendingIntent getAppOverlayIntent() {
3130 return mAppOverlayIntent;
3131 }
3132
Gustav Sennton79ebf4b2018-12-19 10:31:51 +00003133 /**
3134 * Returns whether the platform is allowed (by the app developer) to generate contextual actions
3135 * for this notification.
3136 */
Gustav Sennton761884c2018-11-19 17:40:19 +00003137 public boolean getAllowSystemGeneratedContextualActions() {
3138 return mAllowSystemGeneratedContextualActions;
3139 }
3140
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003141 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003142 * The small icon representing this notification in the status bar and content view.
3143 *
3144 * @return the small icon representing this notification.
3145 *
3146 * @see Builder#getSmallIcon()
3147 * @see Builder#setSmallIcon(Icon)
3148 */
3149 public Icon getSmallIcon() {
3150 return mSmallIcon;
3151 }
3152
3153 /**
3154 * Used when notifying to clean up legacy small icons.
3155 * @hide
3156 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003157 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04003158 public void setSmallIcon(Icon icon) {
3159 mSmallIcon = icon;
3160 }
3161
3162 /**
3163 * The large icon shown in this notification's content view.
3164 * @see Builder#getLargeIcon()
3165 * @see Builder#setLargeIcon(Icon)
3166 */
3167 public Icon getLargeIcon() {
3168 return mLargeIcon;
3169 }
3170
3171 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02003172 * @hide
3173 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003174 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003175 public boolean isGroupSummary() {
3176 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
3177 }
3178
3179 /**
3180 * @hide
3181 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003182 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003183 public boolean isGroupChild() {
3184 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
3185 }
3186
3187 /**
Julia Reynolds30203152017-05-26 13:36:31 -04003188 * @hide
3189 */
3190 public boolean suppressAlertingDueToGrouping() {
3191 if (isGroupSummary()
3192 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
3193 return true;
3194 } else if (isGroupChild()
3195 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
3196 return true;
3197 }
3198 return false;
3199 }
3200
Tony Mak638430e2018-10-08 19:19:10 +01003201
3202 /**
3203 * Finds and returns a remote input and its corresponding action.
3204 *
3205 * @param requiresFreeform requires the remoteinput to allow freeform or not.
3206 * @return the result pair, {@code null} if no result is found.
3207 *
3208 * @hide
3209 */
3210 @Nullable
3211 public Pair<RemoteInput, Action> findRemoteInputActionPair(boolean requiresFreeform) {
3212 if (actions == null) {
3213 return null;
3214 }
3215 for (Notification.Action action : actions) {
3216 if (action.getRemoteInputs() == null) {
3217 continue;
3218 }
3219 RemoteInput resultRemoteInput = null;
3220 for (RemoteInput remoteInput : action.getRemoteInputs()) {
3221 if (remoteInput.getAllowFreeFormInput() || !requiresFreeform) {
3222 resultRemoteInput = remoteInput;
3223 }
3224 }
3225 if (resultRemoteInput != null) {
3226 return Pair.create(resultRemoteInput, action);
3227 }
3228 }
3229 return null;
3230 }
3231
Julia Reynolds30203152017-05-26 13:36:31 -04003232 /**
Gustav Senntoneab53682018-11-01 16:30:23 +00003233 * Returns the actions that are contextual (marked as SEMANTIC_ACTION_CONTEXTUAL_SUGGESTION) out
3234 * of the actions in this notification.
3235 *
3236 * @hide
3237 */
3238 public List<Notification.Action> getContextualActions() {
3239 if (actions == null) return Collections.emptyList();
3240
3241 List<Notification.Action> contextualActions = new ArrayList<>();
3242 for (Notification.Action action : actions) {
3243 if (action.getSemanticAction()
3244 == Notification.Action.SEMANTIC_ACTION_CONTEXTUAL_SUGGESTION) {
3245 contextualActions.add(action);
3246 }
3247 }
3248 return contextualActions;
3249 }
3250
3251 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003252 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08003253 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003254 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07003255 * content views using the platform's notification layout template. If your app supports
3256 * versions of Android as old as API level 4, you can instead use
3257 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
3258 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
3259 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08003260 *
Scott Main183bf112012-08-13 19:12:13 -07003261 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08003262 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003263 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07003264 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003265 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3266 * .setContentText(subject)
3267 * .setSmallIcon(R.drawable.new_mail)
3268 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003269 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003270 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08003271 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003272 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003273 /**
3274 * @hide
3275 */
3276 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
3277 "android.rebuild.contentViewActionCount";
3278 /**
3279 * @hide
3280 */
3281 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
3282 = "android.rebuild.bigViewActionCount";
3283 /**
3284 * @hide
3285 */
3286 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
3287 = "android.rebuild.hudViewActionCount";
3288
Selim Cinek6743c0b2017-01-18 18:24:01 -08003289 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
3290 SystemProperties.getBoolean("notifications.only_title", true);
3291
Selim Cinek389edcd2017-05-11 19:16:44 -07003292 /**
3293 * The lightness difference that has to be added to the primary text color to obtain the
3294 * secondary text color when the background is light.
3295 */
3296 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
3297
3298 /**
3299 * The lightness difference that has to be added to the primary text color to obtain the
3300 * secondary text color when the background is dark.
3301 * A bit less then the above value, since it looks better on dark backgrounds.
3302 */
3303 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
3304
Joe Onorato46439ce2010-11-19 13:56:21 -08003305 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003306 private Notification mN;
3307 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003308 private Style mStyle;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003309 @UnsupportedAppUsage
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003310 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Selim Cineke7238dd2017-12-14 17:48:32 -08003311 private ArrayList<Person> mPersonList = new ArrayList<>();
Lucas Dupina291d192018-06-07 13:59:42 -07003312 private ContrastColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08003313 private boolean mIsLegacy;
3314 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02003315
3316 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08003317 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
3318 */
3319 private int mCachedContrastColor = COLOR_INVALID;
3320 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08003321 /**
Selim Cinek4717d862018-04-19 09:19:15 +08003322 * Caches a ambient version of {@link #mCachedAmbientColorIsFor}.
Adrian Roos487374f2017-01-11 15:48:14 -08003323 */
3324 private int mCachedAmbientColor = COLOR_INVALID;
3325 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Selim Cinek4717d862018-04-19 09:19:15 +08003326 /**
3327 * A neutral color color that can be used for icons.
3328 */
3329 private int mNeutralColor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08003330
3331 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08003332 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
3333 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
3334 */
3335 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08003336 private int mTextColorsAreForBackground = COLOR_INVALID;
3337 private int mPrimaryTextColor = COLOR_INVALID;
3338 private int mSecondaryTextColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07003339 private int mBackgroundColor = COLOR_INVALID;
3340 private int mForegroundColor = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07003341 /**
3342 * A temporary location where actions are stored. If != null the view originally has action
3343 * but doesn't have any for this inflation.
3344 */
3345 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07003346 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003347
Anthony Chenad4d1582017-04-10 16:07:58 -07003348 private boolean mTintActionButtons;
3349 private boolean mInNightMode;
3350
Adrian Roos70d7aa32017-01-11 15:39:06 -08003351 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003352 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08003353 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003354 * @param context
3355 * A {@link Context} that will be used by the Builder to construct the
3356 * RemoteViews. The Context will not be held past the lifetime of this Builder
3357 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003358 * @param channelId
3359 * The constructed Notification will be posted on this
3360 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
3361 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003362 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003363 public Builder(Context context, String channelId) {
3364 this(context, (Notification) null);
3365 mN.mChannelId = channelId;
3366 }
3367
3368 /**
3369 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
3370 * instead. All posted Notifications must specify a NotificationChannel Id.
3371 */
3372 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003373 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003374 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003375 }
3376
Joe Onoratocb109a02011-01-18 17:57:41 -08003377 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003378 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02003379 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003380 public Builder(Context context, Notification toAdopt) {
3381 mContext = context;
Anthony Chenad4d1582017-04-10 16:07:58 -07003382 Resources res = mContext.getResources();
3383 mTintActionButtons = res.getBoolean(R.bool.config_tintNotificationActionButtons);
3384
3385 if (res.getBoolean(R.bool.config_enableNightMode)) {
3386 Configuration currentConfig = res.getConfiguration();
3387 mInNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
3388 == Configuration.UI_MODE_NIGHT_YES;
3389 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003390
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003391 if (toAdopt == null) {
3392 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003393 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
3394 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
3395 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003396 mN.priority = PRIORITY_DEFAULT;
3397 mN.visibility = VISIBILITY_PRIVATE;
3398 } else {
3399 mN = toAdopt;
3400 if (mN.actions != null) {
3401 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003402 }
3403
Selim Cineke7238dd2017-12-14 17:48:32 -08003404 if (mN.extras.containsKey(EXTRA_PEOPLE_LIST)) {
3405 ArrayList<Person> people = mN.extras.getParcelableArrayList(EXTRA_PEOPLE_LIST);
3406 mPersonList.addAll(people);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003407 }
3408
Selim Cinek4ac6f602016-06-13 15:47:03 -07003409 if (mN.getSmallIcon() == null && mN.icon != 0) {
3410 setSmallIcon(mN.icon);
3411 }
3412
3413 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
3414 setLargeIcon(mN.largeIcon);
3415 }
3416
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003417 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
3418 if (!TextUtils.isEmpty(templateClass)) {
3419 final Class<? extends Style> styleClass
3420 = getNotificationStyleClass(templateClass);
3421 if (styleClass == null) {
3422 Log.d(TAG, "Unknown style class: " + templateClass);
3423 } else {
3424 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003425 final Constructor<? extends Style> ctor =
3426 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003427 ctor.setAccessible(true);
3428 final Style style = ctor.newInstance();
3429 style.restoreFromExtras(mN.extras);
3430
3431 if (style != null) {
3432 setStyle(style);
3433 }
3434 } catch (Throwable t) {
3435 Log.e(TAG, "Could not create Style", t);
3436 }
3437 }
3438 }
3439
3440 }
3441 }
3442
Lucas Dupina291d192018-06-07 13:59:42 -07003443 private ContrastColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08003444 if (mColorUtil == null) {
Lucas Dupina291d192018-06-07 13:59:42 -07003445 mColorUtil = ContrastColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003446 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003447 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003448 }
3449
3450 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003451 * If this notification is duplicative of a Launcher shortcut, sets the
3452 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
3453 * the shortcut.
3454 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04003455 * This field will be ignored by Launchers that don't support badging, don't show
3456 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003457 *
3458 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
3459 * supersedes
3460 */
3461 public Builder setShortcutId(String shortcutId) {
3462 mN.mShortcutId = shortcutId;
3463 return this;
3464 }
3465
3466 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003467 * Sets which icon to display as a badge for this notification.
3468 *
3469 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
3470 * {@link #BADGE_ICON_LARGE}.
3471 *
3472 * Note: This value might be ignored, for launchers that don't support badge icons.
3473 */
Julia Reynolds612beb22017-03-30 10:48:30 -04003474 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04003475 mN.mBadgeIcon = icon;
3476 return this;
3477 }
3478
3479 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003480 * Sets the group alert behavior for this notification. Use this method to mute this
3481 * notification if alerts for this notification's group should be handled by a different
3482 * notification. This is only applicable for notifications that belong to a
Julia Reynolds399d9bf2017-08-11 12:52:14 -04003483 * {@link #setGroup(String) group}. This must be called on all notifications you want to
3484 * mute. For example, if you want only the summary of your group to make noise, all
3485 * children in the group should have the group alert behavior {@link #GROUP_ALERT_SUMMARY}.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003486 *
3487 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
3488 */
3489 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
3490 mN.mGroupAlertBehavior = groupAlertBehavior;
3491 return this;
3492 }
3493
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003494 /**
3495 * Sets the intent that will be used to display app content in a floating window
3496 * over the existing foreground activity.
3497 *
3498 * <p>This intent will be ignored unless this notification is posted to a channel that
3499 * allows {@link NotificationChannel#canOverlayApps() app overlays}.</p>
3500 *
3501 * <p>Notifications with a valid and allowed app overlay intent will be displayed as
3502 * floating windows outside of the notification shade on unlocked devices. When a user
3503 * interacts with one of these windows, this app overlay intent will be invoked and
3504 * displayed.</p>
3505 */
3506 public Builder setAppOverlayIntent(PendingIntent intent) {
3507 mN.mAppOverlayIntent = intent;
3508 return this;
3509 }
3510
Jeff Sharkey000ce802017-04-29 13:13:27 -06003511 /** @removed */
3512 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003513 public Builder setChannel(String channelId) {
3514 mN.mChannelId = channelId;
3515 return this;
3516 }
3517
3518 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003519 * Specifies the channel the notification should be delivered on.
3520 */
3521 public Builder setChannelId(String channelId) {
3522 mN.mChannelId = channelId;
3523 return this;
3524 }
3525
Jeff Sharkey000ce802017-04-29 13:13:27 -06003526 /** @removed */
3527 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05003528 public Builder setTimeout(long durationMs) {
3529 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05003530 return this;
3531 }
3532
3533 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003534 * Specifies a duration in milliseconds after which this notification should be canceled,
3535 * if it is not already canceled.
3536 */
3537 public Builder setTimeoutAfter(long durationMs) {
3538 mN.mTimeout = durationMs;
3539 return this;
3540 }
3541
3542 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003543 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003544 *
3545 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
3546 * shown anymore by default and must be opted into by using
3547 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003548 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003549 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08003550 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003551 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003552 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08003553 return this;
3554 }
3555
Joe Onoratocb109a02011-01-18 17:57:41 -08003556 /**
Griff Hazen50c11652014-05-16 09:46:31 -07003557 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07003558 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003559 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
3560 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07003561 */
3562 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003563 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07003564 return this;
3565 }
3566
3567 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003568 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08003569 *
3570 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003571 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003572 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003573 * Useful when showing an elapsed time (like an ongoing phone call).
3574 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08003575 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07003576 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08003577 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003578 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003579 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07003580 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003581 */
3582 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003583 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003584 return this;
3585 }
3586
3587 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08003588 * Sets the Chronometer to count down instead of counting up.
3589 *
3590 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
3591 * If it isn't set the chronometer will count up.
3592 *
3593 * @see #setUsesChronometer(boolean)
3594 */
Adrian Roos96b7e202016-05-17 13:50:38 -07003595 public Builder setChronometerCountDown(boolean countDown) {
3596 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003597 return this;
3598 }
3599
3600 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003601 * Set the small icon resource, which will be used to represent the notification in the
3602 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003603 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003604
3605 * The platform template for the expanded view will draw this icon in the left, unless a
3606 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3607 * icon will be moved to the right-hand side.
3608 *
3609
3610 * @param icon
3611 * A resource ID in the application's package of the drawable to use.
3612 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003613 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003614 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003615 return setSmallIcon(icon != 0
3616 ? Icon.createWithResource(mContext, icon)
3617 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003618 }
3619
Joe Onoratocb109a02011-01-18 17:57:41 -08003620 /**
3621 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3622 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3623 * LevelListDrawable}.
3624 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003625 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003626 * @param level The level to use for the icon.
3627 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003628 * @see Notification#icon
3629 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003630 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003631 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003632 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003633 return setSmallIcon(icon);
3634 }
3635
3636 /**
3637 * Set the small icon, which will be used to represent the notification in the
koprivadebd4ee2018-09-13 10:59:46 -07003638 * status bar and content view (unless overridden there by a
Dan Sandlerd63f9322015-05-06 15:18:49 -04003639 * {@link #setLargeIcon(Bitmap) large icon}).
3640 *
3641 * @param icon An Icon object to use.
3642 * @see Notification#icon
3643 */
3644 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003645 mN.setSmallIcon(icon);
3646 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3647 mN.icon = icon.getResId();
3648 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003649 return this;
3650 }
3651
Joe Onoratocb109a02011-01-18 17:57:41 -08003652 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003653 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003654 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003655 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003656 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003657 return this;
3658 }
3659
Joe Onoratocb109a02011-01-18 17:57:41 -08003660 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003661 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003662 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003663 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003664 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003665 return this;
3666 }
3667
Joe Onoratocb109a02011-01-18 17:57:41 -08003668 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003669 * This provides some additional information that is displayed in the notification. No
3670 * guarantees are given where exactly it is displayed.
3671 *
3672 * <p>This information should only be provided if it provides an essential
3673 * benefit to the understanding of the notification. The more text you provide the
3674 * less readable it becomes. For example, an email client should only provide the account
3675 * name here if more than one email account has been added.</p>
3676 *
3677 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3678 * notification header area.
3679 *
3680 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3681 * this will be shown in the third line of text in the platform notification template.
3682 * You should not be using {@link #setProgress(int, int, boolean)} at the
3683 * same time on those versions; they occupy the same place.
3684 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003685 */
3686 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003687 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003688 return this;
3689 }
3690
3691 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003692 * Provides text that will appear as a link to your application's settings.
3693 *
3694 * <p>This text does not appear within notification {@link Style templates} but may
3695 * appear when the user uses an affordance to learn more about the notification.
3696 * Additionally, this text will not appear unless you provide a valid link target by
3697 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3698 *
3699 * <p>This text is meant to be concise description about what the user can customize
3700 * when they click on this link. The recommended maximum length is 40 characters.
3701 * @param text
3702 * @return
3703 */
3704 public Builder setSettingsText(CharSequence text) {
3705 mN.mSettingsText = safeCharSequence(text);
3706 return this;
3707 }
3708
3709 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003710 * Set the remote input history.
3711 *
3712 * This should be set to the most recent inputs that have been sent
3713 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3714 * longer relevant (e.g. for chat notifications once the other party has responded).
3715 *
3716 * The most recent input must be stored at the 0 index, the second most recent at the
3717 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3718 * and how much of each individual input is shown.
3719 *
3720 * <p>Note: The reply text will only be shown on notifications that have least one action
3721 * with a {@code RemoteInput}.</p>
3722 */
3723 public Builder setRemoteInputHistory(CharSequence[] text) {
3724 if (text == null) {
3725 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3726 } else {
3727 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3728 CharSequence[] safe = new CharSequence[N];
3729 for (int i = 0; i < N; i++) {
3730 safe[i] = safeCharSequence(text[i]);
3731 }
3732 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3733 }
3734 return this;
3735 }
3736
3737 /**
Kenny Guya0f6de82018-04-06 16:20:16 +01003738 * Sets whether remote history entries view should have a spinner.
3739 * @hide
3740 */
3741 public Builder setShowRemoteInputSpinner(boolean showSpinner) {
3742 mN.extras.putBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER, showSpinner);
3743 return this;
3744 }
3745
3746 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +01003747 * Sets whether smart reply buttons should be hidden.
3748 * @hide
3749 */
3750 public Builder setHideSmartReplies(boolean hideSmartReplies) {
3751 mN.extras.putBoolean(EXTRA_HIDE_SMART_REPLIES, hideSmartReplies);
3752 return this;
3753 }
3754
3755 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003756 * Sets the number of items this notification represents. May be displayed as a badge count
3757 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003758 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08003759 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003760 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003761 return this;
3762 }
3763
Joe Onoratocb109a02011-01-18 17:57:41 -08003764 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003765 * A small piece of additional information pertaining to this notification.
3766 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003767 * The platform template will draw this on the last line of the notification, at the far
3768 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003769 *
3770 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3771 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3772 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003773 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003774 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003775 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003776 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003777 return this;
3778 }
3779
Joe Onoratocb109a02011-01-18 17:57:41 -08003780 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003781 * Set the progress this notification represents.
3782 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003783 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003784 */
3785 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003786 mN.extras.putInt(EXTRA_PROGRESS, progress);
3787 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3788 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003789 return this;
3790 }
3791
3792 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003793 * Supply a custom RemoteViews to use instead of the platform template.
3794 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003795 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003796 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003797 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003798 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003799 return setCustomContentView(views);
3800 }
3801
3802 /**
3803 * Supply custom RemoteViews to use instead of the platform template.
3804 *
3805 * This will override the layout that would otherwise be constructed by this Builder
3806 * object.
3807 */
3808 public Builder setCustomContentView(RemoteViews contentView) {
3809 mN.contentView = contentView;
3810 return this;
3811 }
3812
3813 /**
3814 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3815 *
3816 * This will override the expanded layout that would otherwise be constructed by this
3817 * Builder object.
3818 */
3819 public Builder setCustomBigContentView(RemoteViews contentView) {
3820 mN.bigContentView = contentView;
3821 return this;
3822 }
3823
3824 /**
3825 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3826 *
3827 * This will override the heads-up layout that would otherwise be constructed by this
3828 * Builder object.
3829 */
3830 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3831 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003832 return this;
3833 }
3834
Joe Onoratocb109a02011-01-18 17:57:41 -08003835 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003836 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3837 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003838 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3839 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3840 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003841 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003842 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003843 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003844 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003845 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003846 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003847 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003848 return this;
3849 }
3850
Joe Onoratocb109a02011-01-18 17:57:41 -08003851 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003852 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3853 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003854 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003855 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003856 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003857 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003858 return this;
3859 }
3860
Joe Onoratocb109a02011-01-18 17:57:41 -08003861 /**
3862 * An intent to launch instead of posting the notification to the status bar.
3863 * Only for use with extremely high-priority notifications demanding the user's
3864 * <strong>immediate</strong> attention, such as an incoming phone call or
3865 * alarm clock that the user has explicitly set to a particular time.
3866 * If this facility is used for something else, please give the user an option
3867 * to turn it off and use a normal notification, as this can be extremely
3868 * disruptive.
3869 *
Chris Wren47c20a12014-06-18 17:27:29 -04003870 * <p>
3871 * The system UI may choose to display a heads-up notification, instead of
3872 * launching this intent, while the user is using the device.
3873 * </p>
Julia Reynolds47fd15f2018-11-28 10:16:00 -05003874 * <p>Apps targeting {@link Build.VERSION_CODES#Q} and above will have to request
3875 * a permission ({@link android.Manifest.permission#USE_FULL_SCREEN_INTENT}) in order to
3876 * use full screen intents.</p>
Chris Wren47c20a12014-06-18 17:27:29 -04003877 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003878 * @param intent The pending intent to launch.
3879 * @param highPriority Passing true will cause this notification to be sent
3880 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003881 *
3882 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003883 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003884 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003885 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003886 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3887 return this;
3888 }
3889
Joe Onoratocb109a02011-01-18 17:57:41 -08003890 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003891 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003892 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003893 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003894 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003895 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003896 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003897 return this;
3898 }
3899
Joe Onoratocb109a02011-01-18 17:57:41 -08003900 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003901 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003902 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003903 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003904 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003905 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003906 setTicker(tickerText);
3907 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003908 return this;
3909 }
3910
Joe Onoratocb109a02011-01-18 17:57:41 -08003911 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003912 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003913 *
3914 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04003915 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3916 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04003917 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003918 public Builder setLargeIcon(Bitmap b) {
3919 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3920 }
3921
3922 /**
3923 * Add a large icon to the notification content view.
3924 *
3925 * In the platform template, this image will be shown on the left of the notification view
3926 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3927 * badge atop the large icon).
3928 */
3929 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003930 mN.mLargeIcon = icon;
3931 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08003932 return this;
3933 }
3934
Joe Onoratocb109a02011-01-18 17:57:41 -08003935 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003936 * Set the sound to play.
3937 *
John Spurlockc0650f022014-07-19 13:22:39 -04003938 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
3939 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003940 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003941 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003942 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003943 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08003944 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003945 mN.sound = sound;
3946 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08003947 return this;
3948 }
3949
Joe Onoratocb109a02011-01-18 17:57:41 -08003950 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003951 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08003952 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003953 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
3954 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003955 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003956 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07003957 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003958 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08003959 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003960 mN.sound = sound;
3961 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08003962 return this;
3963 }
3964
Joe Onoratocb109a02011-01-18 17:57:41 -08003965 /**
John Spurlockc0650f022014-07-19 13:22:39 -04003966 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
3967 * use during playback.
3968 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003969 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04003970 * @see Notification#sound
3971 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003972 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04003973 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003974 mN.sound = sound;
3975 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04003976 return this;
3977 }
3978
3979 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003980 * Set the vibration pattern to use.
3981 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003982 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
3983 * <code>pattern</code> parameter.
3984 *
Chris Wren47c20a12014-06-18 17:27:29 -04003985 * <p>
3986 * A notification that vibrates is more likely to be presented as a heads-up notification.
3987 * </p>
3988 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003989 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003990 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08003991 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003992 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003993 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003994 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08003995 return this;
3996 }
3997
Joe Onoratocb109a02011-01-18 17:57:41 -08003998 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003999 * Set the desired color for the indicator LED on the device, as well as the
4000 * blink duty cycle (specified in milliseconds).
4001 *
4002
4003 * Not all devices will honor all (or even any) of these values.
4004 *
Julia Reynolds529e3322017-02-06 08:33:01 -05004005 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004006 * @see Notification#ledARGB
4007 * @see Notification#ledOnMS
4008 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08004009 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004010 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08004011 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004012 mN.ledARGB = argb;
4013 mN.ledOnMS = onMs;
4014 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004015 if (onMs != 0 || offMs != 0) {
4016 mN.flags |= FLAG_SHOW_LIGHTS;
4017 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004018 return this;
4019 }
4020
Joe Onoratocb109a02011-01-18 17:57:41 -08004021 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004022 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08004023 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004024
4025 * Ongoing notifications cannot be dismissed by the user, so your application or service
4026 * must take care of canceling them.
4027 *
4028
4029 * They are typically used to indicate a background task that the user is actively engaged
4030 * with (e.g., playing music) or is pending in some way and therefore occupying the device
4031 * (e.g., a file download, sync operation, active network connection).
4032 *
4033
4034 * @see Notification#FLAG_ONGOING_EVENT
Joe Onoratocb109a02011-01-18 17:57:41 -08004035 */
Joe Onorato46439ce2010-11-19 13:56:21 -08004036 public Builder setOngoing(boolean ongoing) {
4037 setFlag(FLAG_ONGOING_EVENT, ongoing);
4038 return this;
4039 }
4040
Joe Onoratocb109a02011-01-18 17:57:41 -08004041 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08004042 * Set whether this notification should be colorized. When set, the color set with
4043 * {@link #setColor(int)} will be used as the background color of this notification.
4044 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08004045 * This should only be used for high priority ongoing tasks like navigation, an ongoing
4046 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08004047 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07004048 * For most styles, the coloring will only be applied if the notification is for a
4049 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08004050 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07004051 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08004052 *
Aurimas Liutikas7f695332018-05-31 21:07:32 -07004053 * @see #setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08004054 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08004055 */
4056 public Builder setColorized(boolean colorize) {
4057 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
4058 return this;
4059 }
4060
4061 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08004062 * Set this flag if you would only like the sound, vibrate
4063 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004064 *
4065 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08004066 */
Joe Onorato46439ce2010-11-19 13:56:21 -08004067 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
4068 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
4069 return this;
4070 }
4071
Joe Onoratocb109a02011-01-18 17:57:41 -08004072 /**
Julia Reynolds04499532016-09-13 14:04:53 -04004073 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004074 *
4075 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08004076 */
Joe Onorato46439ce2010-11-19 13:56:21 -08004077 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08004078 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08004079 return this;
4080 }
4081
Joe Onoratocb109a02011-01-18 17:57:41 -08004082 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08004083 * Set whether or not this notification should not bridge to other devices.
4084 *
4085 * <p>Some notifications can be bridged to other devices for remote display.
4086 * This hint can be set to recommend this notification not be bridged.
4087 */
4088 public Builder setLocalOnly(boolean localOnly) {
4089 setFlag(FLAG_LOCAL_ONLY, localOnly);
4090 return this;
4091 }
4092
4093 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004094 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08004095 * <p>
4096 * The value should be one or more of the following fields combined with
4097 * bitwise-or:
4098 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
4099 * <p>
4100 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004101 *
4102 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05004103 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004104 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08004105 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004106 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004107 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004108 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08004109 return this;
4110 }
4111
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004112 /**
4113 * Set the priority of this notification.
4114 *
4115 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004116 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004117 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004118 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07004119 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004120 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004121 return this;
4122 }
Joe Malin8d40d042012-11-05 11:36:40 -08004123
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004124 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04004125 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08004126 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04004127 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004128 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04004129 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004130 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004131 return this;
4132 }
4133
4134 /**
Chris Wrendde75302014-03-26 17:24:15 -04004135 * Add a person that is relevant to this notification.
4136 *
Chris Wrene6c48932014-09-29 17:19:27 -04004137 * <P>
4138 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04004139 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4140 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4141 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04004142 * </P>
4143 *
4144 * <P>
4145 * The person should be specified by the {@code String} representation of a
4146 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
4147 * </P>
4148 *
4149 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
4150 * URIs. The path part of these URIs must exist in the contacts database, in the
4151 * appropriate column, or the reference will be discarded as invalid. Telephone schema
4152 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
Selim Cineke7238dd2017-12-14 17:48:32 -08004153 * It is also possible to provide a URI with the schema {@code name:} in order to uniquely
4154 * identify a person without an entry in the contacts database.
Chris Wrene6c48932014-09-29 17:19:27 -04004155 * </P>
4156 *
4157 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04004158 * @see Notification#EXTRA_PEOPLE
Selim Cineke7238dd2017-12-14 17:48:32 -08004159 * @deprecated use {@link #addPerson(Person)}
Chris Wrendde75302014-03-26 17:24:15 -04004160 */
Chris Wrene6c48932014-09-29 17:19:27 -04004161 public Builder addPerson(String uri) {
Selim Cinek9acd6732018-03-23 16:39:02 -07004162 addPerson(new Person.Builder().setUri(uri).build());
Selim Cineke7238dd2017-12-14 17:48:32 -08004163 return this;
4164 }
4165
4166 /**
4167 * Add a person that is relevant to this notification.
4168 *
4169 * <P>
4170 * Depending on user preferences, this annotation may allow the notification to pass
4171 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4172 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4173 * appear more prominently in the user interface.
4174 * </P>
4175 *
4176 * <P>
4177 * A person should usually contain a uri in order to benefit from the ranking boost.
4178 * However, even if no uri is provided, it's beneficial to provide other people in the
4179 * notification, such that listeners and voice only devices can announce and handle them
4180 * properly.
4181 * </P>
4182 *
4183 * @param person the person to add.
4184 * @see Notification#EXTRA_PEOPLE_LIST
4185 */
4186 public Builder addPerson(Person person) {
4187 mPersonList.add(person);
Chris Wrendde75302014-03-26 17:24:15 -04004188 return this;
4189 }
4190
4191 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004192 * Set this notification to be part of a group of notifications sharing the same key.
4193 * Grouped notifications may display in a cluster or stack on devices which
4194 * support such rendering.
4195 *
4196 * <p>To make this notification the summary for its group, also call
4197 * {@link #setGroupSummary}. A sort order can be specified for group members by using
4198 * {@link #setSortKey}.
4199 * @param groupKey The group key of the group.
4200 * @return this object for method chaining
4201 */
4202 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004203 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004204 return this;
4205 }
4206
4207 /**
4208 * Set this notification to be the group summary for a group of notifications.
4209 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04004210 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
4211 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004212 * @param isGroupSummary Whether this notification should be a group summary.
4213 * @return this object for method chaining
4214 */
4215 public Builder setGroupSummary(boolean isGroupSummary) {
4216 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
4217 return this;
4218 }
4219
4220 /**
4221 * Set a sort key that orders this notification among other notifications from the
4222 * same package. This can be useful if an external sort was already applied and an app
4223 * would like to preserve this. Notifications will be sorted lexicographically using this
4224 * value, although providing different priorities in addition to providing sort key may
4225 * cause this value to be ignored.
4226 *
4227 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07004228 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004229 *
4230 * @see String#compareTo(String)
4231 */
4232 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004233 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004234 return this;
4235 }
4236
4237 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004238 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004239 *
Griff Hazen720042b2014-02-24 15:46:56 -08004240 * <p>Values within the Bundle will replace existing extras values in this Builder.
4241 *
4242 * @see Notification#extras
4243 */
Griff Hazen959591e2014-05-15 22:26:18 -07004244 public Builder addExtras(Bundle extras) {
4245 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004246 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08004247 }
4248 return this;
4249 }
4250
4251 /**
4252 * Set metadata for this notification.
4253 *
4254 * <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 -04004255 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004256 * called.
4257 *
Griff Hazen720042b2014-02-24 15:46:56 -08004258 * <p>Replaces any existing extras values with those from the provided Bundle.
4259 * Use {@link #addExtras} to merge in metadata instead.
4260 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004261 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004262 */
Griff Hazen959591e2014-05-15 22:26:18 -07004263 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004264 if (extras != null) {
4265 mUserExtras = extras;
4266 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004267 return this;
4268 }
4269
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004270 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004271 * Get the current metadata Bundle used by this notification Builder.
4272 *
4273 * <p>The returned Bundle is shared with this Builder.
4274 *
4275 * <p>The current contents of this Bundle are copied into the Notification each time
4276 * {@link #build()} is called.
4277 *
4278 * @see Notification#extras
4279 */
4280 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004281 return mUserExtras;
4282 }
4283
4284 private Bundle getAllExtras() {
4285 final Bundle saveExtras = (Bundle) mUserExtras.clone();
4286 saveExtras.putAll(mN.extras);
4287 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08004288 }
4289
4290 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004291 * Add an action to this notification. Actions are typically displayed by
4292 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004293 * <p>
4294 * Every action must have an icon (32dp square and matching the
4295 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4296 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4297 * <p>
4298 * A notification in its expanded form can display up to 3 actions, from left to right in
4299 * the order they were added. Actions will not be displayed when the notification is
4300 * collapsed, however, so be sure that any essential functions may be accessed by the user
4301 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004302 *
4303 * @param icon Resource ID of a drawable that represents the action.
4304 * @param title Text describing the action.
4305 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04004306 *
4307 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004308 */
Dan Sandler86647982015-05-13 23:41:13 -04004309 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004310 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004311 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004312 return this;
4313 }
4314
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004315 /**
Griff Hazen959591e2014-05-15 22:26:18 -07004316 * Add an action to this notification. Actions are typically displayed by
4317 * the system as a button adjacent to the notification content.
4318 * <p>
4319 * Every action must have an icon (32dp square and matching the
4320 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4321 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4322 * <p>
4323 * A notification in its expanded form can display up to 3 actions, from left to right in
4324 * the order they were added. Actions will not be displayed when the notification is
4325 * collapsed, however, so be sure that any essential functions may be accessed by the user
4326 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
4327 *
4328 * @param action The action to add.
4329 */
4330 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08004331 if (action != null) {
4332 mActions.add(action);
4333 }
Griff Hazen959591e2014-05-15 22:26:18 -07004334 return this;
4335 }
4336
4337 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004338 * Alter the complete list of actions attached to this notification.
4339 * @see #addAction(Action).
4340 *
4341 * @param actions
4342 * @return
4343 */
4344 public Builder setActions(Action... actions) {
4345 mActions.clear();
4346 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08004347 if (actions[i] != null) {
4348 mActions.add(actions[i]);
4349 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004350 }
4351 return this;
4352 }
4353
4354 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004355 * Add a rich notification style to be applied at build time.
4356 *
4357 * @param style Object responsible for modifying the notification style.
4358 */
4359 public Builder setStyle(Style style) {
4360 if (mStyle != style) {
4361 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004362 if (mStyle != null) {
4363 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004364 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
4365 } else {
4366 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004367 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004368 }
4369 return this;
4370 }
4371
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004372 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05004373 * Returns the style set by {@link #setStyle(Style)}.
4374 */
4375 public Style getStyle() {
4376 return mStyle;
4377 }
4378
4379 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004380 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07004381 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004382 * @return The same Builder.
4383 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06004384 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004385 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004386 return this;
4387 }
4388
4389 /**
4390 * Supply a replacement Notification whose contents should be shown in insecure contexts
4391 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
4392 * @param n A replacement notification, presumably with some or all info redacted.
4393 * @return The same Builder.
4394 */
4395 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004396 if (n != null) {
4397 mN.publicVersion = new Notification();
4398 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
4399 } else {
4400 mN.publicVersion = null;
4401 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004402 return this;
4403 }
4404
Griff Hazenb720abe2014-05-20 13:15:30 -07004405 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004406 * Apply an extender to this notification builder. Extenders may be used to add
4407 * metadata or change options on this builder.
4408 */
Griff Hazen61a9e862014-05-22 16:05:19 -07004409 public Builder extend(Extender extender) {
4410 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004411 return this;
4412 }
4413
Dan Sandler4e787062015-06-17 15:09:48 -04004414 /**
4415 * @hide
4416 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04004417 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08004418 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004419 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004420 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004421 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004422 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04004423 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08004424 }
4425
Dan Sandler26e81cf2014-05-06 10:01:27 -04004426 /**
4427 * Sets {@link Notification#color}.
4428 *
4429 * @param argb The accent color to use
4430 *
4431 * @return The same Builder.
4432 */
Tor Norbye80756e32015-03-02 09:39:27 -08004433 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004434 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004435 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04004436 return this;
4437 }
4438
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004439 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04004440 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
4441 // This user can never be a badged profile,
4442 // and also includes USER_ALL system notifications.
4443 return null;
4444 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02004445 // Note: This assumes that the current user can read the profile badge of the
4446 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08004447 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05004448 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004449 }
4450
4451 private Bitmap getProfileBadge() {
4452 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01004453 if (badge == null) {
4454 return null;
4455 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004456 final int size = mContext.getResources().getDimensionPixelSize(
4457 R.dimen.notification_badge_size);
4458 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004459 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004460 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004461 badge.draw(canvas);
4462 return bitmap;
4463 }
4464
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004465 private void bindProfileBadge(RemoteViews contentView, StandardTemplateParams p) {
Kenny Guy98193ea2014-07-24 19:54:37 +01004466 Bitmap profileBadge = getProfileBadge();
4467
Kenny Guy98193ea2014-07-24 19:54:37 +01004468 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004469 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
4470 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004471 if (isColorized(p)) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004472 contentView.setDrawableTint(R.id.profile_badge, false,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004473 getPrimaryTextColor(p), PorterDuff.Mode.SRC_ATOP);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004474 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004475 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004476 }
4477
Gus Prevasbae5ba32018-11-28 15:45:13 -05004478 private void bindAlertedIcon(RemoteViews contentView, StandardTemplateParams p) {
4479 contentView.setDrawableTint(
4480 R.id.alerted_icon,
4481 false /* targetBackground */,
4482 getNeutralColor(p),
4483 PorterDuff.Mode.SRC_ATOP);
4484 }
4485
Julia Reynoldsfc640012018-02-21 12:25:27 -05004486 /**
4487 * @hide
4488 */
4489 public boolean usesStandardHeader() {
4490 if (mN.mUsesStandardHeader) {
4491 return true;
4492 }
4493 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.N) {
4494 if (mN.contentView == null && mN.bigContentView == null) {
4495 return true;
4496 }
4497 }
4498 boolean contentViewUsesHeader = mN.contentView == null
4499 || STANDARD_LAYOUTS.contains(mN.contentView.getLayoutId());
4500 boolean bigContentViewUsesHeader = mN.bigContentView == null
4501 || STANDARD_LAYOUTS.contains(mN.bigContentView.getLayoutId());
4502 return contentViewUsesHeader && bigContentViewUsesHeader;
4503 }
4504
Christoph Studerfe718432014-09-01 18:21:18 +02004505 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004506 resetNotificationHeader(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02004507 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08004508 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004509 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08004510 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004511 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08004512 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08004513 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004514 }
4515
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004516 /**
4517 * Resets the notification header to its original state
4518 */
4519 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07004520 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
4521 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08004522 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004523 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02004524 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004525 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07004526 contentView.setTextViewText(R.id.header_text, null);
Selim Cinekafeed292017-12-12 17:32:44 -08004527 contentView.setViewVisibility(R.id.header_text_secondary, View.GONE);
4528 contentView.setTextViewText(R.id.header_text_secondary, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004529 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinekafeed292017-12-12 17:32:44 -08004530 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004531 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004532 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004533 contentView.setImageViewIcon(R.id.profile_badge, null);
4534 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Gus Prevasa3226492018-10-23 11:10:09 -04004535 contentView.setViewVisibility(R.id.alerted_icon, View.GONE);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004536 mN.mUsesStandardHeader = false;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004537 }
4538
Selim Cinek384804b2018-04-18 14:31:07 +08004539 private RemoteViews applyStandardTemplate(int resId, TemplateBindResult result) {
4540 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this),
4541 result);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004542 }
4543
Selim Cinek384804b2018-04-18 14:31:07 +08004544 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p,
4545 TemplateBindResult result) {
Kenny Guy77320062014-08-27 21:37:15 +01004546 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04004547
Christoph Studerfe718432014-09-01 18:21:18 +02004548 resetStandardTemplate(contentView);
4549
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004550 final Bundle ex = mN.extras;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004551 updateBackgroundColor(contentView, p);
4552 bindNotificationHeader(contentView, p);
Selim Cinek384804b2018-04-18 14:31:07 +08004553 bindLargeIconAndReply(contentView, p, result);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004554 boolean showProgress = handleProgressBar(contentView, ex, p);
Adrian Roos70d7aa32017-01-11 15:39:06 -08004555 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08004556 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004557 contentView.setTextViewText(R.id.title, processTextSpans(p.title));
Adrian Roos72171622017-01-27 10:32:06 -08004558 if (!p.ambient) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004559 setTextViewColorPrimary(contentView, R.id.title, p);
Adrian Roos72171622017-01-27 10:32:06 -08004560 }
Selim Cinek954cc232016-05-20 13:29:23 -07004561 contentView.setViewLayoutWidth(R.id.title, showProgress
4562 ? ViewGroup.LayoutParams.WRAP_CONTENT
4563 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08004564 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08004565 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08004566 int textId = showProgress ? com.android.internal.R.id.text_line_1
4567 : com.android.internal.R.id.text;
Selim Cinek48f66b72017-08-18 16:17:51 -07004568 contentView.setTextViewText(textId, processTextSpans(p.text));
Adrian Roos72171622017-01-27 10:32:06 -08004569 if (!p.ambient) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004570 setTextViewColorSecondary(contentView, textId, p);
Adrian Roos72171622017-01-27 10:32:06 -08004571 }
Selim Cinek41598732016-01-11 16:58:37 -08004572 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08004573 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08004574
Selim Cinek279fa862016-06-14 10:57:25 -07004575 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004576
Selim Cinek29603462015-11-17 19:04:39 -08004577 return contentView;
4578 }
4579
Selim Cinek48f66b72017-08-18 16:17:51 -07004580 private CharSequence processTextSpans(CharSequence text) {
Lucas Dupind3c99322018-09-27 10:22:29 -07004581 if (hasForegroundColor() || mInNightMode) {
Lucas Dupina291d192018-06-07 13:59:42 -07004582 return ContrastColorUtil.clearColorSpans(text);
Selim Cinek48f66b72017-08-18 16:17:51 -07004583 }
4584 return text;
4585 }
4586
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004587 private void setTextViewColorPrimary(RemoteViews contentView, int id,
4588 StandardTemplateParams p) {
4589 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004590 contentView.setTextColor(id, mPrimaryTextColor);
4591 }
4592
Selim Cinek48f66b72017-08-18 16:17:51 -07004593 private boolean hasForegroundColor() {
4594 return mForegroundColor != COLOR_INVALID;
4595 }
4596
Selim Cinek389edcd2017-05-11 19:16:44 -07004597 /**
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004598 * Return the primary text color using the existing template params
Selim Cinek389edcd2017-05-11 19:16:44 -07004599 * @hide
4600 */
4601 @VisibleForTesting
4602 public int getPrimaryTextColor() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004603 return getPrimaryTextColor(mParams);
4604 }
4605
4606 /**
4607 * @param p the template params to inflate this with
4608 * @return the primary text color
4609 * @hide
4610 */
4611 @VisibleForTesting
4612 public int getPrimaryTextColor(StandardTemplateParams p) {
4613 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004614 return mPrimaryTextColor;
4615 }
4616
Selim Cinek389edcd2017-05-11 19:16:44 -07004617 /**
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004618 * Return the secondary text color using the existing template params
Selim Cinek389edcd2017-05-11 19:16:44 -07004619 * @hide
4620 */
4621 @VisibleForTesting
4622 public int getSecondaryTextColor() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004623 return getSecondaryTextColor(mParams);
4624 }
4625
4626 /**
4627 * @param p the template params to inflate this with
4628 * @return the secondary text color
4629 * @hide
4630 */
4631 @VisibleForTesting
4632 public int getSecondaryTextColor(StandardTemplateParams p) {
4633 ensureColors(p);
Selim Cinek389edcd2017-05-11 19:16:44 -07004634 return mSecondaryTextColor;
4635 }
4636
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004637 private void setTextViewColorSecondary(RemoteViews contentView, int id,
4638 StandardTemplateParams p) {
4639 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004640 contentView.setTextColor(id, mSecondaryTextColor);
4641 }
4642
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004643 private void ensureColors(StandardTemplateParams p) {
4644 int backgroundColor = getBackgroundColor(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004645 if (mPrimaryTextColor == COLOR_INVALID
4646 || mSecondaryTextColor == COLOR_INVALID
Selim Cinek7b9605b2017-01-19 17:36:00 -08004647 || mTextColorsAreForBackground != backgroundColor) {
4648 mTextColorsAreForBackground = backgroundColor;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004649 if (!hasForegroundColor() || !isColorized(p)) {
Lucas Dupina291d192018-06-07 13:59:42 -07004650 mPrimaryTextColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004651 backgroundColor, mInNightMode);
Lucas Dupina291d192018-06-07 13:59:42 -07004652 mSecondaryTextColor = ContrastColorUtil.resolveSecondaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004653 backgroundColor, mInNightMode);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004654 if (backgroundColor != COLOR_DEFAULT && isColorized(p)) {
Lucas Dupina291d192018-06-07 13:59:42 -07004655 mPrimaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004656 mPrimaryTextColor, backgroundColor, 4.5);
Lucas Dupina291d192018-06-07 13:59:42 -07004657 mSecondaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004658 mSecondaryTextColor, backgroundColor, 4.5);
4659 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07004660 } else {
Lucas Dupina291d192018-06-07 13:59:42 -07004661 double backLum = ContrastColorUtil.calculateLuminance(backgroundColor);
4662 double textLum = ContrastColorUtil.calculateLuminance(mForegroundColor);
4663 double contrast = ContrastColorUtil.calculateContrast(mForegroundColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004664 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07004665 // We only respect the given colors if worst case Black or White still has
4666 // contrast
4667 boolean backgroundLight = backLum > textLum
4668 && satisfiesTextContrast(backgroundColor, Color.BLACK)
4669 || backLum <= textLum
4670 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004671 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07004672 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004673 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004674 mForegroundColor,
4675 backgroundColor,
4676 true /* findFG */,
4677 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004678 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004679 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004680 } else {
4681 mSecondaryTextColor =
Lucas Dupina291d192018-06-07 13:59:42 -07004682 ContrastColorUtil.findContrastColorAgainstDark(
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_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004689 }
4690 } else {
4691 mPrimaryTextColor = mForegroundColor;
Lucas Dupina291d192018-06-07 13:59:42 -07004692 mSecondaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004693 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4694 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Lucas Dupina291d192018-06-07 13:59:42 -07004695 if (ContrastColorUtil.calculateContrast(mSecondaryTextColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004696 backgroundColor) < 4.5f) {
4697 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07004698 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004699 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004700 mSecondaryTextColor,
4701 backgroundColor,
4702 true /* findFG */,
4703 4.5f);
4704 } else {
4705 mSecondaryTextColor
Lucas Dupina291d192018-06-07 13:59:42 -07004706 = ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004707 mSecondaryTextColor,
4708 backgroundColor,
4709 true /* findFG */,
4710 4.5f);
4711 }
Lucas Dupina291d192018-06-07 13:59:42 -07004712 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004713 mSecondaryTextColor, backgroundLight
4714 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4715 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004716 }
4717 }
4718 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004719 }
4720 }
4721
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004722 private void updateBackgroundColor(RemoteViews contentView,
4723 StandardTemplateParams p) {
4724 if (isColorized(p)) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004725 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004726 getBackgroundColor(p));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004727 } else {
4728 // Clear it!
4729 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4730 0);
4731 }
4732 }
4733
Selim Cinek860b6da2015-12-16 19:02:19 -08004734 /**
4735 * @param remoteView the remote view to update the minheight in
4736 * @param hasMinHeight does it have a mimHeight
4737 * @hide
4738 */
4739 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4740 int minHeight = 0;
4741 if (hasMinHeight) {
4742 // we need to set the minHeight of the notification
4743 minHeight = mContext.getResources().getDimensionPixelSize(
4744 com.android.internal.R.dimen.notification_min_content_height);
4745 }
4746 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4747 }
4748
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004749 private boolean handleProgressBar(RemoteViews contentView, Bundle ex,
4750 StandardTemplateParams p) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004751 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4752 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4753 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004754 if (p.hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004755 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004756 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004757 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004758 contentView.setProgressBackgroundTintList(
4759 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4760 R.color.notification_progress_background_color)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004761 if (getRawColor(p) != COLOR_DEFAULT) {
4762 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor(p));
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004763 contentView.setProgressTintList(R.id.progress, colorStateList);
4764 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004765 }
Selim Cinek29603462015-11-17 19:04:39 -08004766 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004767 } else {
4768 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004769 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004770 }
Joe Onorato561d3852010-11-20 18:09:34 -08004771 }
4772
Selim Cinek384804b2018-04-18 14:31:07 +08004773 private void bindLargeIconAndReply(RemoteViews contentView, StandardTemplateParams p,
4774 TemplateBindResult result) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004775 boolean largeIconShown = bindLargeIcon(contentView, p);
4776 boolean replyIconShown = bindReplyIcon(contentView, p);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004777 boolean iconContainerVisible = largeIconShown || replyIconShown;
Selim Cinek384804b2018-04-18 14:31:07 +08004778 contentView.setViewVisibility(R.id.right_icon_container,
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004779 iconContainerVisible ? View.VISIBLE : View.GONE);
Selim Cinek1c72fa02018-04-23 18:00:54 +08004780 int marginEnd = calculateMarginEnd(largeIconShown, replyIconShown);
4781 contentView.setViewLayoutMarginEnd(R.id.line1, marginEnd);
4782 contentView.setViewLayoutMarginEnd(R.id.text, marginEnd);
4783 contentView.setViewLayoutMarginEnd(R.id.progress, marginEnd);
Selim Cinek384804b2018-04-18 14:31:07 +08004784 if (result != null) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08004785 result.setIconMarginEnd(marginEnd);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004786 result.setRightIconContainerVisible(iconContainerVisible);
Selim Cinek384804b2018-04-18 14:31:07 +08004787 }
4788 }
4789
Selim Cinek1c72fa02018-04-23 18:00:54 +08004790 private int calculateMarginEnd(boolean largeIconShown, boolean replyIconShown) {
4791 int marginEnd = 0;
4792 int contentMargin = mContext.getResources().getDimensionPixelSize(
4793 R.dimen.notification_content_margin_end);
4794 int iconSize = mContext.getResources().getDimensionPixelSize(
4795 R.dimen.notification_right_icon_size);
4796 if (replyIconShown) {
4797 // The size of the reply icon
4798 marginEnd += iconSize;
4799
4800 int replyInset = mContext.getResources().getDimensionPixelSize(
4801 R.dimen.notification_reply_inset);
4802 // We're subtracting the inset of the reply icon to make sure it's
4803 // aligned nicely on the right, and remove it from the following padding
4804 marginEnd -= replyInset * 2;
4805 }
4806 if (largeIconShown) {
4807 // adding size of the right icon
4808 marginEnd += iconSize;
4809
4810 if (replyIconShown) {
4811 // We also add some padding to the reply icon if it's around
4812 marginEnd += contentMargin;
4813 }
4814 }
4815 if (replyIconShown || largeIconShown) {
4816 // The padding to the content
4817 marginEnd += contentMargin;
4818 }
4819 return marginEnd;
4820 }
4821
Selim Cinek384804b2018-04-18 14:31:07 +08004822 /**
4823 * Bind the large icon.
4824 * @return if the largeIcon is visible
4825 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004826 private boolean bindLargeIcon(RemoteViews contentView, StandardTemplateParams p) {
Selim Cinek279fa862016-06-14 10:57:25 -07004827 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4828 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4829 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004830 boolean showLargeIcon = mN.mLargeIcon != null && !p.hideLargeIcon && !p.ambient;
Selim Cinek88188f22017-09-19 16:46:56 -07004831 if (showLargeIcon) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004832 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4833 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004834 processLargeLegacyIcon(mN.mLargeIcon, contentView, p);
Selim Cinek88188f22017-09-19 16:46:56 -07004835 }
Selim Cinek384804b2018-04-18 14:31:07 +08004836 return showLargeIcon;
4837 }
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004838
Selim Cinek384804b2018-04-18 14:31:07 +08004839 /**
4840 * Bind the reply icon.
4841 * @return if the reply icon is visible
4842 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004843 private boolean bindReplyIcon(RemoteViews contentView, StandardTemplateParams p) {
4844 boolean actionVisible = !p.hideReplyIcon && !p.ambient;
Selim Cinek384804b2018-04-18 14:31:07 +08004845 Action action = null;
Selim Cinek88188f22017-09-19 16:46:56 -07004846 if (actionVisible) {
Selim Cinek384804b2018-04-18 14:31:07 +08004847 action = findReplyAction();
4848 actionVisible = action != null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004849 }
Selim Cinek384804b2018-04-18 14:31:07 +08004850 if (actionVisible) {
4851 contentView.setViewVisibility(R.id.reply_icon_action, View.VISIBLE);
4852 contentView.setDrawableTint(R.id.reply_icon_action,
4853 false /* targetBackground */,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004854 getNeutralColor(p),
Selim Cinek384804b2018-04-18 14:31:07 +08004855 PorterDuff.Mode.SRC_ATOP);
4856 contentView.setOnClickPendingIntent(R.id.reply_icon_action, action.actionIntent);
4857 contentView.setRemoteInputs(R.id.reply_icon_action, action.mRemoteInputs);
4858 } else {
4859 contentView.setRemoteInputs(R.id.reply_icon_action, null);
4860 }
4861 contentView.setViewVisibility(R.id.reply_icon_action,
4862 actionVisible ? View.VISIBLE : View.GONE);
4863 return actionVisible;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004864 }
4865
4866 private Action findReplyAction() {
4867 ArrayList<Action> actions = mActions;
4868 if (mOriginalActions != null) {
4869 actions = mOriginalActions;
4870 }
4871 int numActions = actions.size();
4872 for (int i = 0; i < numActions; i++) {
4873 Action action = actions.get(i);
4874 if (hasValidRemoteInput(action)) {
4875 return action;
4876 }
4877 }
4878 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004879 }
4880
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004881 private void bindNotificationHeader(RemoteViews contentView, StandardTemplateParams p) {
4882 bindSmallIcon(contentView, p);
4883 bindHeaderAppName(contentView, p);
4884 if (!p.ambient) {
Adrian Roos487374f2017-01-11 15:48:14 -08004885 // Ambient view does not have these
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004886 bindHeaderText(contentView, p);
4887 bindHeaderTextSecondary(contentView, p);
4888 bindHeaderChronometerAndTime(contentView, p);
4889 bindProfileBadge(contentView, p);
Gus Prevasbae5ba32018-11-28 15:45:13 -05004890 bindAlertedIcon(contentView, p);
Adrian Roos487374f2017-01-11 15:48:14 -08004891 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004892 bindActivePermissions(contentView, p);
4893 bindExpandButton(contentView, p);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004894 mN.mUsesStandardHeader = true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004895 }
4896
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004897 private void bindActivePermissions(RemoteViews contentView, StandardTemplateParams p) {
4898 int color = p.ambient ? resolveAmbientColor(p) : getNeutralColor(p);
Julia Reynoldsb887b232018-04-10 16:38:08 -04004899 contentView.setDrawableTint(R.id.camera, false, color, PorterDuff.Mode.SRC_ATOP);
4900 contentView.setDrawableTint(R.id.mic, false, color, PorterDuff.Mode.SRC_ATOP);
4901 contentView.setDrawableTint(R.id.overlay, false, color, PorterDuff.Mode.SRC_ATOP);
4902 }
4903
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004904 private void bindExpandButton(RemoteViews contentView, StandardTemplateParams p) {
4905 int color = isColorized(p) ? getPrimaryTextColor(p) : getSecondaryTextColor(p);
Sunny Goyal5b153922017-09-21 21:00:36 -07004906 contentView.setDrawableTint(R.id.expand_button, false, color,
4907 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08004908 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08004909 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004910 }
4911
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004912 private void bindHeaderChronometerAndTime(RemoteViews contentView,
4913 StandardTemplateParams p) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004914 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08004915 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004916 setTextViewColorSecondary(contentView, R.id.time_divider, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004917 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
4918 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
4919 contentView.setLong(R.id.chronometer, "setBase",
4920 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
4921 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07004922 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07004923 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004924 setTextViewColorSecondary(contentView, R.id.chronometer, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004925 } else {
4926 contentView.setViewVisibility(R.id.time, View.VISIBLE);
4927 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004928 setTextViewColorSecondary(contentView, R.id.time, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004929 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004930 } else {
4931 // We still want a time to be set but gone, such that we can show and hide it
4932 // on demand in case it's a child notification without anything in the header
4933 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004934 }
4935 }
4936
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004937 private void bindHeaderText(RemoteViews contentView, StandardTemplateParams p) {
4938 CharSequence summaryText = p.summaryText;
4939 if (summaryText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05004940 && mStyle.hasSummaryInHeader()) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004941 summaryText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05004942 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004943 if (summaryText == null
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004944 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4945 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004946 summaryText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004947 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004948 if (summaryText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004949 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek48f66b72017-08-18 16:17:51 -07004950 contentView.setTextViewText(R.id.header_text, processTextSpans(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004951 processLegacyText(summaryText)));
4952 setTextViewColorSecondary(contentView, R.id.header_text, p);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004953 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
4954 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004955 setTextViewColorSecondary(contentView, R.id.header_text_divider, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004956 }
4957 }
4958
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004959 private void bindHeaderTextSecondary(RemoteViews contentView, StandardTemplateParams p) {
4960 if (!TextUtils.isEmpty(p.headerTextSecondary)) {
Selim Cinekafeed292017-12-12 17:32:44 -08004961 contentView.setTextViewText(R.id.header_text_secondary, processTextSpans(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004962 processLegacyText(p.headerTextSecondary)));
4963 setTextViewColorSecondary(contentView, R.id.header_text_secondary, p);
Selim Cinekafeed292017-12-12 17:32:44 -08004964 contentView.setViewVisibility(R.id.header_text_secondary, View.VISIBLE);
4965 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004966 setTextViewColorSecondary(contentView, R.id.header_text_secondary_divider, p);
Selim Cinekafeed292017-12-12 17:32:44 -08004967 }
4968 }
4969
Adrian Rooseba05822016-04-22 17:09:27 -07004970 /**
4971 * @hide
4972 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01004973 @UnsupportedAppUsage
Adrian Rooseba05822016-04-22 17:09:27 -07004974 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004975 CharSequence name = null;
4976 final PackageManager pm = mContext.getPackageManager();
4977 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
4978 // only system packages which lump together a bunch of unrelated stuff
4979 // may substitute a different name to make the purpose of the
4980 // notification more clear. the correct package label should always
4981 // be accessible via SystemUI.
4982 final String pkg = mContext.getPackageName();
4983 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
4984 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
4985 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
4986 name = subName;
4987 } else {
4988 Log.w(TAG, "warning: pkg "
4989 + pkg + " attempting to substitute app name '" + subName
4990 + "' without holding perm "
4991 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
4992 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004993 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04004994 if (TextUtils.isEmpty(name)) {
4995 name = pm.getApplicationLabel(mContext.getApplicationInfo());
4996 }
4997 if (TextUtils.isEmpty(name)) {
4998 // still nothing?
4999 return null;
5000 }
5001
5002 return String.valueOf(name);
5003 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005004 private void bindHeaderAppName(RemoteViews contentView, StandardTemplateParams p) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04005005 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005006 if (isColorized(p)) {
5007 setTextViewColorPrimary(contentView, R.id.app_name_text, p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005008 } else {
5009 contentView.setTextColor(R.id.app_name_text,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005010 p.ambient ? resolveAmbientColor(p) : getSecondaryTextColor(p));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005011 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005012 }
5013
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005014 private boolean isColorized(StandardTemplateParams p) {
5015 return p.allowColorization && !p.ambient && mN.isColorized();
5016 }
5017
5018 private void bindSmallIcon(RemoteViews contentView, StandardTemplateParams p) {
Selim Cinek279fa862016-06-14 10:57:25 -07005019 if (mN.mSmallIcon == null && mN.icon != 0) {
5020 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
5021 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005022 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Sunny Goyal5b153922017-09-21 21:00:36 -07005023 contentView.setInt(R.id.icon, "setImageLevel", mN.iconLevel);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005024 processSmallIconColor(mN.mSmallIcon, contentView, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005025 }
5026
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005027 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005028 * @return true if the built notification will show the time or the chronometer; false
5029 * otherwise
5030 */
5031 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07005032 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005033 }
5034
Christoph Studerfe718432014-09-01 18:21:18 +02005035 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07005036 // actions_container is only reset when there are no actions to avoid focus issues with
5037 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02005038 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02005039 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08005040
5041 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
5042 big.setTextViewText(R.id.notification_material_reply_text_1, null);
Kenny Guya0f6de82018-04-06 16:20:16 +01005043 big.setViewVisibility(R.id.notification_material_reply_text_1_container, View.GONE);
5044 big.setViewVisibility(R.id.notification_material_reply_progress, View.GONE);
Adrian Roose458aa82015-12-08 16:17:19 -08005045
5046 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
5047 big.setTextViewText(R.id.notification_material_reply_text_2, null);
5048 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
5049 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07005050
Selim Cineked64a142018-02-06 18:06:01 -08005051 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
5052 R.dimen.notification_content_margin);
Christoph Studerfe718432014-09-01 18:21:18 +02005053 }
5054
Selim Cinek384804b2018-04-18 14:31:07 +08005055 private RemoteViews applyStandardTemplateWithActions(int layoutId,
5056 TemplateBindResult result) {
5057 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this),
5058 result);
Adrian Roos48d746a2016-04-12 14:57:28 -07005059 }
5060
Gustav Sennton1463d832018-11-06 16:12:48 +00005061 private static List<Notification.Action> filterOutContextualActions(
5062 List<Notification.Action> actions) {
5063 List<Notification.Action> nonContextualActions = new ArrayList<>();
5064 for (Notification.Action action : actions) {
5065 if (action.getSemanticAction()
5066 != Action.SEMANTIC_ACTION_CONTEXTUAL_SUGGESTION) {
5067 nonContextualActions.add(action);
5068 }
5069 }
5070 return nonContextualActions;
5071 }
5072
Adrian Roos70d7aa32017-01-11 15:39:06 -08005073 private RemoteViews applyStandardTemplateWithActions(int layoutId,
Selim Cinek384804b2018-04-18 14:31:07 +08005074 StandardTemplateParams p, TemplateBindResult result) {
5075 RemoteViews big = applyStandardTemplate(layoutId, p, result);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005076
Christoph Studerfe718432014-09-01 18:21:18 +02005077 resetStandardTemplateWithActions(big);
5078
Adrian Roose458aa82015-12-08 16:17:19 -08005079 boolean validRemoteInput = false;
5080
Gustav Sennton1463d832018-11-06 16:12:48 +00005081 // In the UI contextual actions appear separately from the standard actions, so we
5082 // filter them out here.
5083 List<Notification.Action> nonContextualActions = filterOutContextualActions(mActions);
5084
5085 int N = nonContextualActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08005086 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005087 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005088 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08005089 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005090 big.setViewVisibility(R.id.actions, View.VISIBLE);
Selim Cineked64a142018-02-06 18:06:01 -08005091 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Daniel Sandler8680bf82012-05-15 16:52:52 -04005092 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005093 for (int i=0; i<N; i++) {
Gustav Sennton1463d832018-11-06 16:12:48 +00005094 Action action = nonContextualActions.get(i);
5095
Selim Cinekffcc7cf2018-02-06 17:43:51 -08005096 boolean actionHasValidInput = hasValidRemoteInput(action);
5097 validRemoteInput |= actionHasValidInput;
Adrian Roose458aa82015-12-08 16:17:19 -08005098
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005099 final RemoteViews button = generateActionButton(action, emphazisedMode, p);
Selim Cinek396caca2018-04-10 17:46:46 -07005100 if (actionHasValidInput && !emphazisedMode) {
Selim Cinekffcc7cf2018-02-06 17:43:51 -08005101 // Clear the drawable
5102 button.setInt(R.id.action0, "setBackgroundResource", 0);
5103 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005104 big.addView(R.id.actions, button);
5105 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07005106 } else {
5107 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005108 }
Adrian Roose458aa82015-12-08 16:17:19 -08005109
5110 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08005111 if (!p.ambient && validRemoteInput && replyText != null
Selim Cinekbee4e072018-05-21 22:06:43 -07005112 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])
5113 && p.maxRemoteInputHistory > 0) {
Kenny Guya0f6de82018-04-06 16:20:16 +01005114 boolean showSpinner = mN.extras.getBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER);
Adrian Roose458aa82015-12-08 16:17:19 -08005115 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Kenny Guya0f6de82018-04-06 16:20:16 +01005116 big.setViewVisibility(R.id.notification_material_reply_text_1_container,
5117 View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005118 big.setTextViewText(R.id.notification_material_reply_text_1,
5119 processTextSpans(replyText[0]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005120 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1, p);
Kenny Guya0f6de82018-04-06 16:20:16 +01005121 big.setViewVisibility(R.id.notification_material_reply_progress,
5122 showSpinner ? View.VISIBLE : View.GONE);
5123 big.setProgressIndeterminateTintList(
5124 R.id.notification_material_reply_progress,
5125 ColorStateList.valueOf(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005126 isColorized(p) ? getPrimaryTextColor(p) : resolveContrastColor(p)));
Adrian Roose458aa82015-12-08 16:17:19 -08005127
Selim Cinekbee4e072018-05-21 22:06:43 -07005128 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])
5129 && p.maxRemoteInputHistory > 1) {
Adrian Roose458aa82015-12-08 16:17:19 -08005130 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005131 big.setTextViewText(R.id.notification_material_reply_text_2,
5132 processTextSpans(replyText[1]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005133 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2, p);
Adrian Roose458aa82015-12-08 16:17:19 -08005134
Selim Cinekbee4e072018-05-21 22:06:43 -07005135 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])
5136 && p.maxRemoteInputHistory > 2) {
Adrian Roose458aa82015-12-08 16:17:19 -08005137 big.setViewVisibility(
5138 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005139 big.setTextViewText(R.id.notification_material_reply_text_3,
5140 processTextSpans(replyText[2]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005141 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3, p);
Adrian Roose458aa82015-12-08 16:17:19 -08005142 }
5143 }
5144 }
5145
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005146 return big;
5147 }
5148
Adrian Roose458aa82015-12-08 16:17:19 -08005149 private boolean hasValidRemoteInput(Action action) {
5150 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
5151 // Weird actions
5152 return false;
5153 }
5154
5155 RemoteInput[] remoteInputs = action.getRemoteInputs();
5156 if (remoteInputs == null) {
5157 return false;
5158 }
5159
5160 for (RemoteInput r : remoteInputs) {
5161 CharSequence[] choices = r.getChoices();
5162 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
5163 return true;
5164 }
5165 }
5166 return false;
5167 }
5168
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005169 /**
5170 * Construct a RemoteViews for the final 1U notification layout. In order:
5171 * 1. Custom contentView from the caller
5172 * 2. Style's proposed content view
5173 * 3. Standard template view
5174 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005175 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005176 return createContentView(false /* increasedheight */ );
5177 }
5178
5179 /**
5180 * Construct a RemoteViews for the smaller content view.
5181 *
5182 * @param increasedHeight true if this layout be created with an increased height. Some
5183 * styles may support showing more then just that basic 1U size
5184 * and the system may decide to render important notifications
5185 * slightly bigger even when collapsed.
5186 *
5187 * @hide
5188 */
5189 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005190 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005191 return mN.contentView;
5192 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005193 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005194 if (styleView != null) {
5195 return styleView;
5196 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005197 }
Selim Cinek384804b2018-04-18 14:31:07 +08005198 return applyStandardTemplate(getBaseLayoutResource(), null /* result */);
Joe Onorato46439ce2010-11-19 13:56:21 -08005199 }
5200
Selim Cineka7679b62017-05-10 16:33:25 -07005201 private boolean useExistingRemoteView() {
5202 return mStyle == null || (!mStyle.displayCustomViewInline()
5203 && !mRebuildStyledRemoteViews);
5204 }
5205
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005206 /**
5207 * Construct a RemoteViews for the final big notification layout.
5208 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005209 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05005210 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07005211 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005212 return mN.bigContentView;
5213 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05005214 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08005215 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005216 } else if (mActions.size() != 0) {
Selim Cinek384804b2018-04-18 14:31:07 +08005217 result = applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5218 null /* result */);
Selim Cinek850a8542015-11-11 11:48:36 -05005219 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08005220 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05005221 return result;
5222 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005223
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005224 /**
Selim Cinek414ad332017-02-24 19:06:12 -08005225 * Construct a RemoteViews for the final notification header only. This will not be
5226 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005227 *
Adrian Roos6f6e1592017-05-02 16:22:53 -07005228 * @param ambient if true, generate the header for the ambient display layout.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005229 * @hide
5230 */
Adrian Roos6f6e1592017-05-02 16:22:53 -07005231 public RemoteViews makeNotificationHeader(boolean ambient) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005232 return makeNotificationHeader(mParams.reset().ambient(ambient).fillTextsFrom(this));
5233 }
5234
5235 /**
5236 * Construct a RemoteViews for the final notification header only. This will not be
5237 * colorized.
5238 *
5239 * @param p the template params to inflate this with
5240 */
5241 private RemoteViews makeNotificationHeader(StandardTemplateParams p) {
5242 // Headers on their own are never colorized
5243 p.disallowColorization();
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005244 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005245 p.ambient ? R.layout.notification_template_ambient_header
Adrian Roos6f6e1592017-05-02 16:22:53 -07005246 : R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005247 resetNotificationHeader(header);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005248 bindNotificationHeader(header, p);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005249 return header;
5250 }
5251
Adrian Roos487374f2017-01-11 15:48:14 -08005252 /**
5253 * Construct a RemoteViews for the ambient version of the notification.
5254 *
5255 * @hide
5256 */
5257 public RemoteViews makeAmbientNotification() {
5258 RemoteViews ambient = applyStandardTemplateWithActions(
5259 R.layout.notification_template_material_ambient,
Selim Cinek384804b2018-04-18 14:31:07 +08005260 mParams.reset().ambient(true).fillTextsFrom(this).hasProgress(false),
5261 null /* result */);
Adrian Roos487374f2017-01-11 15:48:14 -08005262 return ambient;
5263 }
5264
Selim Cinek29603462015-11-17 19:04:39 -08005265 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005266 if (result != null) {
5267 result.setViewVisibility(R.id.text_line_1, View.GONE);
5268 }
Selim Cinek29603462015-11-17 19:04:39 -08005269 }
5270
Selim Cinek6743c0b2017-01-18 18:24:01 -08005271 /**
5272 * Adapt the Notification header if this view is used as an expanded view.
5273 *
5274 * @hide
5275 */
5276 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005277 if (result != null) {
5278 result.setBoolean(R.id.notification_header, "setExpanded", true);
5279 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005280 }
5281
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005282 /**
5283 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005284 *
5285 * @param increasedHeight true if this layout be created with an increased height. Some
5286 * styles may support showing more then just that basic 1U size
5287 * and the system may decide to render important notifications
5288 * slightly bigger even when collapsed.
5289 *
5290 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005291 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005292 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005293 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005294 return mN.headsUpContentView;
5295 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08005296 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
5297 if (styleView != null) {
5298 return styleView;
5299 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05005300 } else if (mActions.size() == 0) {
5301 return null;
5302 }
5303
Selim Cinekbee4e072018-05-21 22:06:43 -07005304 // We only want at most a single remote input history to be shown here, otherwise
5305 // the content would become squished.
5306 StandardTemplateParams p = mParams.reset().fillTextsFrom(this)
5307 .setMaxRemoteInputHistory(1);
5308 return applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5309 p,
5310 null /* result */);
Chris Wren8fd39ec2014-02-27 17:43:26 -05005311 }
5312
Selim Cinek624c02db2015-12-14 21:00:02 -08005313 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08005314 * Construct a RemoteViews for the final heads-up notification layout.
5315 */
5316 public RemoteViews createHeadsUpContentView() {
5317 return createHeadsUpContentView(false /* useIncreasedHeight */);
5318 }
5319
5320 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08005321 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5322 *
5323 * @hide
5324 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005325 @UnsupportedAppUsage
Selim Cinek624c02db2015-12-14 21:00:02 -08005326 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005327 return makePublicView(false /* ambient */);
5328 }
5329
5330 /**
5331 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5332 *
5333 * @hide
5334 */
5335 public RemoteViews makePublicAmbientNotification() {
5336 return makePublicView(true /* ambient */);
5337 }
5338
5339 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08005340 if (mN.publicVersion != null) {
5341 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005342 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08005343 }
5344 Bundle savedBundle = mN.extras;
5345 Style style = mStyle;
5346 mStyle = null;
5347 Icon largeIcon = mN.mLargeIcon;
5348 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07005349 Bitmap largeIconLegacy = mN.largeIcon;
5350 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005351 ArrayList<Action> actions = mActions;
5352 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08005353 Bundle publicExtras = new Bundle();
5354 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
5355 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
5356 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
5357 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07005358 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
5359 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cineked07b962018-04-30 14:39:35 -07005360 String appName = savedBundle.getString(EXTRA_SUBSTITUTE_APP_NAME);
5361 if (appName != null) {
5362 publicExtras.putString(EXTRA_SUBSTITUTE_APP_NAME, appName);
5363 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005364 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07005365 RemoteViews view;
5366 if (ambient) {
5367 publicExtras.putCharSequence(EXTRA_TITLE,
5368 mContext.getString(com.android.internal.R.string.notification_hidden_text));
5369 view = makeAmbientNotification();
5370 } else{
5371 view = makeNotificationHeader(false /* ambient */);
5372 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
5373 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005374 mN.extras = savedBundle;
5375 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07005376 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005377 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08005378 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005379 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08005380 }
5381
Selim Cinek6743c0b2017-01-18 18:24:01 -08005382 /**
5383 * Construct a content view for the display when low - priority
5384 *
5385 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
5386 * a new subtext is created consisting of the content of the
5387 * notification.
5388 * @hide
5389 */
5390 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005391 StandardTemplateParams p = mParams.reset()
5392 .forceDefaultColor()
5393 .ambient(false)
5394 .fillTextsFrom(this);
5395 if (!useRegularSubtext || TextUtils.isEmpty(mParams.summaryText)) {
5396 p.summaryText(createSummaryText());
Selim Cinek6743c0b2017-01-18 18:24:01 -08005397 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005398 RemoteViews header = makeNotificationHeader(p);
Selim Cinek1b554392017-02-28 17:22:49 -08005399 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08005400 return header;
5401 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005402
Selim Cinek6743c0b2017-01-18 18:24:01 -08005403 private CharSequence createSummaryText() {
5404 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
5405 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
5406 return titleText;
5407 }
5408 SpannableStringBuilder summary = new SpannableStringBuilder();
5409 if (titleText == null) {
5410 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
5411 }
5412 BidiFormatter bidi = BidiFormatter.getInstance();
5413 if (titleText != null) {
5414 summary.append(bidi.unicodeWrap(titleText));
5415 }
5416 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
5417 if (titleText != null && contentText != null) {
5418 summary.append(bidi.unicodeWrap(mContext.getText(
5419 R.string.notification_header_divider_symbol_with_spaces)));
5420 }
5421 if (contentText != null) {
5422 summary.append(bidi.unicodeWrap(contentText));
5423 }
5424 return summary;
5425 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05005426
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005427 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005428 StandardTemplateParams p) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04005429 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07005430 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005431 emphazisedMode ? getEmphasizedActionLayoutResource()
5432 : tombstone ? getActionTombstoneLayoutResource()
5433 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04005434 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04005435 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04005436 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005437 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005438 if (action.mRemoteInputs != null) {
5439 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
5440 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005441 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07005442 // change the background bgColor
Selim Cinek981962e2016-07-20 20:41:58 -07005443 CharSequence title = action.title;
5444 ColorStateList[] outResultColor = null;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005445 int background = resolveBackgroundColor(p);
Selim Cinek981962e2016-07-20 20:41:58 -07005446 if (isLegacy()) {
Lucas Dupina291d192018-06-07 13:59:42 -07005447 title = ContrastColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07005448 } else {
5449 outResultColor = new ColorStateList[1];
Selim Cinek396caca2018-04-10 17:46:46 -07005450 title = ensureColorSpanContrast(title, background, outResultColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005451 }
Selim Cinek48f66b72017-08-18 16:17:51 -07005452 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005453 setTextViewColorPrimary(button, R.id.action0, p);
Selim Cinek396caca2018-04-10 17:46:46 -07005454 int rippleColor;
5455 boolean hasColorOverride = outResultColor != null && outResultColor[0] != null;
5456 if (hasColorOverride) {
5457 // There's a span spanning the full text, let's take it and use it as the
5458 // background color
5459 background = outResultColor[0].getDefaultColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005460 int textColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07005461 background, mInNightMode);
Selim Cinek396caca2018-04-10 17:46:46 -07005462 button.setTextColor(R.id.action0, textColor);
5463 rippleColor = textColor;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005464 } else if (getRawColor(p) != COLOR_DEFAULT && !isColorized(p)
5465 && mTintActionButtons) {
5466 rippleColor = resolveContrastColor(p);
Selim Cinek396caca2018-04-10 17:46:46 -07005467 button.setTextColor(R.id.action0, rippleColor);
5468 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005469 rippleColor = getPrimaryTextColor(p);
Selim Cinek981962e2016-07-20 20:41:58 -07005470 }
Selim Cinek396caca2018-04-10 17:46:46 -07005471 // We only want about 20% alpha for the ripple
5472 rippleColor = (rippleColor & 0x00ffffff) | 0x33000000;
5473 button.setColorStateList(R.id.action0, "setRippleColor",
5474 ColorStateList.valueOf(rippleColor));
5475 button.setColorStateList(R.id.action0, "setButtonBackground",
5476 ColorStateList.valueOf(background));
5477 button.setBoolean(R.id.action0, "setHasStroke", !hasColorOverride);
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005478 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07005479 button.setTextViewText(R.id.action0, processTextSpans(
5480 processLegacyText(action.title)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005481 if (isColorized(p)) {
5482 setTextViewColorPrimary(button, R.id.action0, p);
5483 } else if (getRawColor(p) != COLOR_DEFAULT && mTintActionButtons) {
Adrian Roos487374f2017-01-11 15:48:14 -08005484 button.setTextColor(R.id.action0,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005485 p.ambient ? resolveAmbientColor(p) : resolveContrastColor(p));
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005486 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005487 }
Tony Mak7d4b3a52018-11-27 17:29:36 +00005488 button.setIntTag(R.id.action0, R.id.notification_action_index_tag,
5489 mActions.indexOf(action));
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005490 return button;
5491 }
5492
Joe Onoratocb109a02011-01-18 17:57:41 -08005493 /**
Selim Cinek981962e2016-07-20 20:41:58 -07005494 * Ensures contrast on color spans against a background color. also returns the color of the
5495 * text if a span was found that spans over the whole text.
5496 *
5497 * @param charSequence the charSequence on which the spans are
5498 * @param background the background color to ensure the contrast against
5499 * @param outResultColor an array in which a color will be returned as the first element if
5500 * there exists a full length color span.
5501 * @return the contrasted charSequence
5502 */
5503 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
5504 ColorStateList[] outResultColor) {
5505 if (charSequence instanceof Spanned) {
5506 Spanned ss = (Spanned) charSequence;
5507 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
5508 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
5509 for (Object span : spans) {
5510 Object resultSpan = span;
5511 int spanStart = ss.getSpanStart(span);
5512 int spanEnd = ss.getSpanEnd(span);
5513 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
5514 if (resultSpan instanceof CharacterStyle) {
5515 resultSpan = ((CharacterStyle) span).getUnderlying();
5516 }
5517 if (resultSpan instanceof TextAppearanceSpan) {
5518 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
5519 ColorStateList textColor = originalSpan.getTextColor();
5520 if (textColor != null) {
5521 int[] colors = textColor.getColors();
5522 int[] newColors = new int[colors.length];
5523 for (int i = 0; i < newColors.length; i++) {
Lucas Dupina291d192018-06-07 13:59:42 -07005524 newColors[i] = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005525 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005526 }
5527 textColor = new ColorStateList(textColor.getStates().clone(),
5528 newColors);
Selim Cinek396caca2018-04-10 17:46:46 -07005529 if (fullLength) {
5530 outResultColor[0] = textColor;
5531 // Let's drop the color from the span
5532 textColor = null;
5533 }
Selim Cinek981962e2016-07-20 20:41:58 -07005534 resultSpan = new TextAppearanceSpan(
5535 originalSpan.getFamily(),
5536 originalSpan.getTextStyle(),
5537 originalSpan.getTextSize(),
5538 textColor,
5539 originalSpan.getLinkTextColor());
Selim Cinek981962e2016-07-20 20:41:58 -07005540 }
5541 } else if (resultSpan instanceof ForegroundColorSpan) {
5542 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
5543 int foregroundColor = originalSpan.getForegroundColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005544 foregroundColor = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005545 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005546 if (fullLength) {
5547 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
Selim Cinek396caca2018-04-10 17:46:46 -07005548 resultSpan = null;
5549 } else {
5550 resultSpan = new ForegroundColorSpan(foregroundColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005551 }
5552 } else {
5553 resultSpan = span;
5554 }
Selim Cinek396caca2018-04-10 17:46:46 -07005555 if (resultSpan != null) {
5556 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
5557 }
Selim Cinek981962e2016-07-20 20:41:58 -07005558 }
5559 return builder;
5560 }
5561 return charSequence;
5562 }
5563
5564 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005565 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07005566 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005567 */
5568 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005569 if (!mIsLegacyInitialized) {
5570 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
5571 < Build.VERSION_CODES.LOLLIPOP;
5572 mIsLegacyInitialized = true;
5573 }
5574 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005575 }
5576
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005577 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08005578 return processLegacyText(charSequence, false /* ambient */);
5579 }
5580
5581 private CharSequence processLegacyText(CharSequence charSequence, boolean ambient) {
5582 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
5583 boolean wantLightText = ambient;
5584 if (isAlreadyLightText != wantLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005585 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005586 } else {
5587 return charSequence;
5588 }
5589 }
5590
Dan Sandler26e81cf2014-05-06 10:01:27 -04005591 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005592 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04005593 */
Adrian Roos487374f2017-01-11 15:48:14 -08005594 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005595 StandardTemplateParams p) {
Selim Cinekea4bef72015-12-02 15:51:10 -08005596 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005597 int color;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005598 if (p.ambient) {
5599 color = resolveAmbientColor(p);
5600 } else if (isColorized(p)) {
5601 color = getPrimaryTextColor(p);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005602 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005603 color = resolveContrastColor(p);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005604 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005605 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07005606 contentView.setDrawableTint(R.id.icon, false, color,
5607 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005608
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005609 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005610 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08005611 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005612 }
5613
Dan Sandler26e81cf2014-05-06 10:01:27 -04005614 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005615 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04005616 * if it's grayscale).
5617 */
5618 // TODO: also check bounds, transparency, that sort of thing.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005619 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView,
5620 StandardTemplateParams p) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04005621 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005622 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005623 // resolve color will fall back to the default when legacy
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005624 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(p),
Sunny Goyal5b153922017-09-21 21:00:36 -07005625 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01005626 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005627 }
5628
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05005629 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005630 if (mN.color != COLOR_DEFAULT) {
5631 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02005632 }
Jorim Jaggi74419312014-06-10 20:57:21 +02005633 }
5634
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005635 int resolveContrastColor(StandardTemplateParams p) {
5636 int rawColor = getRawColor(p);
5637 if (mCachedContrastColorIsFor == rawColor && mCachedContrastColor != COLOR_INVALID) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08005638 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005639 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005640
Selim Cinekac5f0272017-05-02 16:05:41 -07005641 int color;
Selim Cinekc7f5a822018-03-20 19:32:06 -07005642 int background = mContext.getColor(
5643 com.android.internal.R.color.notification_material_background_color);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005644 if (rawColor == COLOR_DEFAULT) {
5645 ensureColors(p);
Lucas Dupinf03e7522018-06-25 16:21:13 -07005646 color = ContrastColorUtil.resolveDefaultColor(mContext, background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005647 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005648 color = ContrastColorUtil.resolveContrastColor(mContext, rawColor,
Anthony Chenad4d1582017-04-10 16:07:58 -07005649 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005650 }
5651 if (Color.alpha(color) < 255) {
5652 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005653 color = ContrastColorUtil.compositeColors(color, background);
Selim Cinekac5f0272017-05-02 16:05:41 -07005654 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005655 mCachedContrastColorIsFor = rawColor;
Selim Cinekac5f0272017-05-02 16:05:41 -07005656 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005657 }
5658
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005659 /**
5660 * Return the raw color of this Notification, which doesn't necessarily satisfy contrast.
5661 *
5662 * @see #resolveContrastColor(StandardTemplateParams) for the contrasted color
5663 * @param p the template params to inflate this with
5664 */
5665 private int getRawColor(StandardTemplateParams p) {
5666 if (p.forceDefaultColor) {
5667 return COLOR_DEFAULT;
5668 }
5669 return mN.color;
5670 }
5671
Selim Cinek4717d862018-04-19 09:19:15 +08005672 int resolveNeutralColor() {
5673 if (mNeutralColor != COLOR_INVALID) {
5674 return mNeutralColor;
5675 }
5676 int background = mContext.getColor(
5677 com.android.internal.R.color.notification_material_background_color);
Lucas Dupinf03e7522018-06-25 16:21:13 -07005678 mNeutralColor = ContrastColorUtil.resolveDefaultColor(mContext, background,
5679 mInNightMode);
Selim Cinek4717d862018-04-19 09:19:15 +08005680 if (Color.alpha(mNeutralColor) < 255) {
5681 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005682 mNeutralColor = ContrastColorUtil.compositeColors(mNeutralColor, background);
Selim Cinek4717d862018-04-19 09:19:15 +08005683 }
5684 return mNeutralColor;
5685 }
5686
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005687 int resolveAmbientColor(StandardTemplateParams p) {
5688 int rawColor = getRawColor(p);
5689 if (mCachedAmbientColorIsFor == rawColor && mCachedAmbientColorIsFor != COLOR_INVALID) {
Adrian Roos487374f2017-01-11 15:48:14 -08005690 return mCachedAmbientColor;
5691 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005692 final int contrasted = ContrastColorUtil.resolveAmbientColor(mContext, rawColor);
Adrian Roos487374f2017-01-11 15:48:14 -08005693
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005694 mCachedAmbientColorIsFor = rawColor;
Adrian Roos487374f2017-01-11 15:48:14 -08005695 return mCachedAmbientColor = contrasted;
5696 }
5697
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005698 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005699 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005700 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08005701 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005702 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005703 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005704 mN.actions = new Action[mActions.size()];
5705 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005706 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005707 if (!mPersonList.isEmpty()) {
Selim Cineke7238dd2017-12-14 17:48:32 -08005708 mN.extras.putParcelableArrayList(EXTRA_PEOPLE_LIST, mPersonList);
Dan Sandler0bf2ed82013-12-21 23:33:41 -06005709 }
Selim Cinek247fa012016-02-18 09:50:48 -08005710 if (mN.bigContentView != null || mN.contentView != null
5711 || mN.headsUpContentView != null) {
5712 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
5713 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005714 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08005715 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005716
Julia Reynolds3b848122016-02-26 10:45:32 -05005717 /**
5718 * Creates a Builder from an existing notification so further changes can be made.
5719 * @param context The context for your application / activity.
5720 * @param n The notification to create a Builder from.
5721 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005722 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005723 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005724 ApplicationInfo applicationInfo = n.extras.getParcelable(
5725 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005726 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05005727 if (applicationInfo != null) {
5728 try {
5729 builderContext = context.createApplicationContext(applicationInfo,
5730 Context.CONTEXT_RESTRICTED);
5731 } catch (NameNotFoundException e) {
5732 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
5733 builderContext = context; // try with our context
5734 }
5735 } else {
5736 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02005737 }
5738
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005739 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005740 }
5741
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005742 /**
Gustav Sennton761884c2018-11-19 17:40:19 +00005743 * Determines whether the platform can generate contextual actions for a notification.
5744 * By default this is true.
5745 */
5746 public Builder setAllowSystemGeneratedContextualActions(boolean allowed) {
5747 mN.mAllowSystemGeneratedContextualActions = allowed;
5748 return this;
5749 }
5750
5751 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005752 * @deprecated Use {@link #build()} instead.
5753 */
5754 @Deprecated
5755 public Notification getNotification() {
5756 return build();
5757 }
5758
5759 /**
5760 * Combine all of the options that have been set and return a new {@link Notification}
5761 * object.
5762 */
5763 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005764 // first, add any extras from the calling code
5765 if (mUserExtras != null) {
5766 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005767 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005768
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005769 mN.creationTime = System.currentTimeMillis();
5770
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005771 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05005772 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02005773
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005774 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005775
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005776 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02005777 mStyle.reduceImageSizes(mContext);
5778 mStyle.purgeResources();
Selim Cinek90343862018-02-01 11:07:11 -08005779 mStyle.validate(mContext);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005780 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005781 }
Selim Cinekd0426622017-07-11 13:19:59 +02005782 mN.reduceImageSizes(mContext);
5783
Adrian Roos5081c0d2016-02-26 16:04:19 -08005784 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07005785 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005786 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005787 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005788 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
5789 mN.contentView.getSequenceNumber());
5790 }
5791 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005792 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005793 if (mN.bigContentView != null) {
5794 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
5795 mN.bigContentView.getSequenceNumber());
5796 }
5797 }
5798 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005799 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005800 if (mN.headsUpContentView != null) {
5801 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
5802 mN.headsUpContentView.getSequenceNumber());
5803 }
5804 }
5805 }
5806
Julia Reynolds4c0c2022016-02-02 15:11:59 -05005807 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
5808 mN.flags |= FLAG_SHOW_LIGHTS;
5809 }
5810
Adrian Roosfb921842017-10-26 14:49:56 +02005811 mN.allPendingIntents = null;
5812
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005813 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005814 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005815
5816 /**
5817 * Apply this Builder to an existing {@link Notification} object.
5818 *
5819 * @hide
5820 */
5821 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005822 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005823 return n;
5824 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005825
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005826 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005827 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005828 * change. Also removes extenders on low ram devices, as
5829 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005830 *
5831 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5832 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005833 * @hide
5834 */
Kristian Monsen05f34792018-04-09 10:27:16 +02005835 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam,
5836 Context context) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005837 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005838
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005839 // Only strip views for known Styles because we won't know how to
5840 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005841 if (!isLowRam
5842 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005843 && getNotificationStyleClass(templateClass) == null) {
5844 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005845 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005846
5847 // Only strip unmodified BuilderRemoteViews.
5848 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005849 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005850 n.contentView.getSequenceNumber();
5851 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005852 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005853 n.bigContentView.getSequenceNumber();
5854 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005855 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005856 n.headsUpContentView.getSequenceNumber();
5857
5858 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005859 if (!isLowRam
5860 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005861 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005862 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005863
5864 Notification clone = n.clone();
5865 if (stripContentView) {
5866 clone.contentView = null;
5867 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5868 }
5869 if (stripBigContentView) {
5870 clone.bigContentView = null;
5871 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5872 }
5873 if (stripHeadsUpContentView) {
5874 clone.headsUpContentView = null;
5875 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5876 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005877 if (isLowRam) {
Kristian Monsen05f34792018-04-09 10:27:16 +02005878 String[] allowedServices = context.getResources().getStringArray(
5879 R.array.config_allowedManagedServicesOnLowRamDevices);
5880 if (allowedServices.length == 0) {
5881 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5882 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5883 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5884 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005885 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005886 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005887 }
5888
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005889 @UnsupportedAppUsage
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005890 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005891 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005892 }
5893
5894 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005895 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005896 }
5897
5898 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005899 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005900 }
5901
5902 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005903 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005904 }
5905
5906 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005907 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005908 }
5909
Adrian Roosc1a80b02016-04-05 14:54:55 -07005910 private int getMessagingLayoutResource() {
5911 return R.layout.notification_template_material_messaging;
5912 }
5913
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005914 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005915 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005916 }
5917
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005918 private int getEmphasizedActionLayoutResource() {
5919 return R.layout.notification_material_action_emphasized;
5920 }
5921
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005922 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005923 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005924 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005925
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005926 private int getBackgroundColor(StandardTemplateParams p) {
5927 if (isColorized(p)) {
5928 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : getRawColor(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005929 } else {
Selim Cinekc7f5a822018-03-20 19:32:06 -07005930 return COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005931 }
5932 }
5933
Selim Cinek396caca2018-04-10 17:46:46 -07005934 /**
Selim Cinek4717d862018-04-19 09:19:15 +08005935 * Gets a neutral color that can be used for icons or similar that should not stand out.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005936 * @param p the template params to inflate this with
Selim Cinek4717d862018-04-19 09:19:15 +08005937 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005938 private int getNeutralColor(StandardTemplateParams p) {
5939 if (isColorized(p)) {
5940 return getSecondaryTextColor(p);
Selim Cinek4717d862018-04-19 09:19:15 +08005941 } else {
5942 return resolveNeutralColor();
5943 }
5944 }
5945
5946 /**
Selim Cinek396caca2018-04-10 17:46:46 -07005947 * Same as getBackgroundColor but also resolved the default color to the background.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005948 * @param p the template params to inflate this with
Selim Cinek396caca2018-04-10 17:46:46 -07005949 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005950 private int resolveBackgroundColor(StandardTemplateParams p) {
5951 int backgroundColor = getBackgroundColor(p);
Selim Cinek396caca2018-04-10 17:46:46 -07005952 if (backgroundColor == COLOR_DEFAULT) {
5953 backgroundColor = mContext.getColor(
5954 com.android.internal.R.color.notification_material_background_color);
5955 }
5956 return backgroundColor;
5957 }
5958
Anthony Chenad4d1582017-04-10 16:07:58 -07005959 private boolean shouldTintActionButtons() {
5960 return mTintActionButtons;
5961 }
5962
Selim Cinek99104832017-01-25 14:47:33 -08005963 private boolean textColorsNeedInversion() {
5964 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
5965 return false;
5966 }
5967 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
5968 return targetSdkVersion > Build.VERSION_CODES.M
5969 && targetSdkVersion < Build.VERSION_CODES.O;
5970 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005971
5972 /**
5973 * Set a color palette to be used as the background and textColors
5974 *
5975 * @param backgroundColor the color to be used as the background
5976 * @param foregroundColor the color to be used as the foreground
5977 *
5978 * @hide
5979 */
5980 public void setColorPalette(int backgroundColor, int foregroundColor) {
5981 mBackgroundColor = backgroundColor;
5982 mForegroundColor = foregroundColor;
5983 mTextColorsAreForBackground = COLOR_INVALID;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005984 ensureColors(mParams.reset().fillTextsFrom(this));
Selim Cinek5fb73f82017-04-20 16:55:38 -07005985 }
Selim Cinekac5f0272017-05-02 16:05:41 -07005986
5987 /**
Selim Cineka7679b62017-05-10 16:33:25 -07005988 * Forces all styled remoteViews to be built from scratch and not use any cached
5989 * RemoteViews.
5990 * This is needed for legacy apps that are baking in their remoteviews into the
5991 * notification.
5992 *
5993 * @hide
5994 */
5995 public void setRebuildStyledRemoteViews(boolean rebuild) {
5996 mRebuildStyledRemoteViews = rebuild;
5997 }
Selim Cinekaa9db1f2018-02-27 17:35:47 -08005998
5999 /**
6000 * Get the text that should be displayed in the statusBar when heads upped. This is
6001 * usually just the app name, but may be different depending on the style.
6002 *
6003 * @param publicMode If true, return a text that is safe to display in public.
6004 *
6005 * @hide
6006 */
6007 public CharSequence getHeadsUpStatusBarText(boolean publicMode) {
6008 if (mStyle != null && !publicMode) {
6009 CharSequence text = mStyle.getHeadsUpStatusBarText();
6010 if (!TextUtils.isEmpty(text)) {
6011 return text;
6012 }
6013 }
6014 return loadHeaderAppName();
6015 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08006016 }
6017
6018 /**
Selim Cinekd0426622017-07-11 13:19:59 +02006019 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
6020 * remote views.
6021 *
6022 * @hide
6023 */
6024 void reduceImageSizes(Context context) {
6025 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
6026 return;
6027 }
Selim Cineka8cb1262017-08-15 16:53:44 -07006028 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006029 if (mLargeIcon != null || largeIcon != null) {
6030 Resources resources = context.getResources();
6031 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07006032 int maxWidth = resources.getDimensionPixelSize(isLowRam
6033 ? R.dimen.notification_right_icon_size_low_ram
6034 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006035 int maxHeight = maxWidth;
6036 if (MediaStyle.class.equals(style)
6037 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006038 maxHeight = resources.getDimensionPixelSize(isLowRam
6039 ? R.dimen.notification_media_image_max_height_low_ram
6040 : R.dimen.notification_media_image_max_height);
6041 maxWidth = resources.getDimensionPixelSize(isLowRam
6042 ? R.dimen.notification_media_image_max_width_low_ram
6043 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006044 }
6045 if (mLargeIcon != null) {
6046 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
6047 }
6048 if (largeIcon != null) {
6049 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
6050 }
6051 }
Selim Cineka8cb1262017-08-15 16:53:44 -07006052 reduceImageSizesForRemoteView(contentView, context, isLowRam);
6053 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
6054 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02006055 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
6056 }
6057
Selim Cineka8cb1262017-08-15 16:53:44 -07006058 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
6059 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02006060 if (remoteView != null) {
6061 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006062 int maxWidth = resources.getDimensionPixelSize(isLowRam
6063 ? R.dimen.notification_custom_view_max_image_width_low_ram
6064 : R.dimen.notification_custom_view_max_image_width);
6065 int maxHeight = resources.getDimensionPixelSize(isLowRam
6066 ? R.dimen.notification_custom_view_max_image_height_low_ram
6067 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02006068 remoteView.reduceImageSizes(maxWidth, maxHeight);
6069 }
6070 }
6071
6072 /**
Selim Cinek22714f12017-04-13 16:23:53 -07006073 * @return whether this notification is a foreground service notification
Gus Prevasb0c1a462018-11-05 11:06:15 -05006074 * @hide
Selim Cinek7b9605b2017-01-19 17:36:00 -08006075 */
Gus Prevasb0c1a462018-11-05 11:06:15 -05006076 public boolean isForegroundService() {
Selim Cinek22714f12017-04-13 16:23:53 -07006077 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08006078 }
6079
6080 /**
Selim Cinek99104832017-01-25 14:47:33 -08006081 * @return whether this notification has a media session attached
6082 * @hide
6083 */
6084 public boolean hasMediaSession() {
6085 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
6086 }
6087
6088 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006089 * @return the style class of this notification
6090 * @hide
6091 */
6092 public Class<? extends Notification.Style> getNotificationStyle() {
6093 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
6094
6095 if (!TextUtils.isEmpty(templateClass)) {
6096 return Notification.getNotificationStyleClass(templateClass);
6097 }
6098 return null;
6099 }
6100
6101 /**
Selim Cinek22714f12017-04-13 16:23:53 -07006102 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08006103 *
6104 * @hide
6105 */
6106 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07006107 if (isColorizedMedia()) {
6108 return true;
6109 }
Julia Reynolds4db59552017-06-30 13:34:01 -04006110 return extras.getBoolean(EXTRA_COLORIZED)
6111 && (hasColorizedPermission() || isForegroundService());
6112 }
6113
6114 /**
6115 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
6116 * permission. The permission is checked when a notification is enqueued.
6117 */
6118 private boolean hasColorizedPermission() {
6119 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07006120 }
6121
6122 /**
6123 * @return true if this notification is colorized and it is a media notification
6124 *
6125 * @hide
6126 */
6127 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08006128 Class<? extends Style> style = getNotificationStyle();
6129 if (MediaStyle.class.equals(style)) {
6130 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
6131 if ((colorized == null || colorized) && hasMediaSession()) {
6132 return true;
6133 }
6134 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6135 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
6136 return true;
6137 }
6138 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07006139 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006140 }
6141
Selim Cinek0847acd2017-04-24 19:48:29 -07006142
6143 /**
6144 * @return true if this is a media notification
6145 *
6146 * @hide
6147 */
6148 public boolean isMediaNotification() {
6149 Class<? extends Style> style = getNotificationStyle();
6150 if (MediaStyle.class.equals(style)) {
6151 return true;
6152 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6153 return true;
6154 }
6155 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006156 }
6157
Selim Cinek279fa862016-06-14 10:57:25 -07006158 private boolean hasLargeIcon() {
6159 return mLargeIcon != null || largeIcon != null;
6160 }
6161
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006162 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006163 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006164 * @hide
6165 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07006166 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006167 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
6168 }
6169
6170 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006171 * @return true if the notification will show a chronometer; false otherwise
6172 * @hide
6173 */
6174 public boolean showsChronometer() {
6175 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
6176 }
6177
6178 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04006179 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05006180 */
6181 @SystemApi
6182 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
6183 Class<? extends Style>[] classes = new Class[] {
6184 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
6185 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
6186 MessagingStyle.class };
6187 for (Class<? extends Style> innerClass : classes) {
6188 if (templateClass.equals(innerClass.getName())) {
6189 return innerClass;
6190 }
6191 }
6192 return null;
6193 }
6194
6195 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006196 * An object that can apply a rich notification style to a {@link Notification.Builder}
6197 * object.
6198 */
Griff Hazendfcb0802014-02-11 12:00:00 -08006199 public static abstract class Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07006200
6201 /**
6202 * The number of items allowed simulatanously in the remote input history.
6203 * @hide
6204 */
6205 static final int MAX_REMOTE_INPUT_HISTORY_LINES = 3;
Chris Wrend6297db2012-05-03 16:20:13 -04006206 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006207
6208 /**
6209 * @hide
6210 */
6211 protected CharSequence mSummaryText = null;
6212
6213 /**
6214 * @hide
6215 */
6216 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04006217
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006218 protected Builder mBuilder;
6219
Chris Wrend6297db2012-05-03 16:20:13 -04006220 /**
6221 * Overrides ContentTitle in the big form of the template.
6222 * This defaults to the value passed to setContentTitle().
6223 */
6224 protected void internalSetBigContentTitle(CharSequence title) {
6225 mBigContentTitle = title;
6226 }
6227
6228 /**
6229 * Set the first line of text after the detail section in the big form of the template.
6230 */
6231 protected void internalSetSummaryText(CharSequence cs) {
6232 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04006233 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04006234 }
6235
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006236 public void setBuilder(Builder builder) {
6237 if (mBuilder != builder) {
6238 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07006239 if (mBuilder != null) {
6240 mBuilder.setStyle(this);
6241 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006242 }
6243 }
6244
Chris Wrend6297db2012-05-03 16:20:13 -04006245 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006246 if (mBuilder == null) {
6247 throw new IllegalArgumentException("Style requires a valid Builder object");
6248 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006249 }
Chris Wrend6297db2012-05-03 16:20:13 -04006250
6251 protected RemoteViews getStandardView(int layoutId) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006252 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder);
6253 return getStandardView(layoutId, p, null);
Selim Cinek384804b2018-04-18 14:31:07 +08006254 }
6255
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006256
Selim Cinek384804b2018-04-18 14:31:07 +08006257 /**
6258 * Get the standard view for this style.
6259 *
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006260 * @param layoutId The layout id to use.
6261 * @param p the params for this inflation.
Selim Cinek384804b2018-04-18 14:31:07 +08006262 * @param result The result where template bind information is saved.
6263 * @return A remoteView for this style.
6264 * @hide
6265 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006266 protected RemoteViews getStandardView(int layoutId, StandardTemplateParams p,
6267 TemplateBindResult result) {
Chris Wrend6297db2012-05-03 16:20:13 -04006268 checkBuilder();
6269
6270 if (mBigContentTitle != null) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006271 p.title = mBigContentTitle;
Chris Wrend6297db2012-05-03 16:20:13 -04006272 }
6273
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006274 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId, p,
6275 result);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006276
Chris Wrend6297db2012-05-03 16:20:13 -04006277 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
6278 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04006279 } else {
6280 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04006281 }
6282
Chris Wrend6297db2012-05-03 16:20:13 -04006283 return contentView;
6284 }
6285
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006286 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006287 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006288 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08006289 *
6290 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006291 * @hide
6292 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08006293 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006294 return null;
6295 }
6296
6297 /**
6298 * Construct a Style-specific RemoteViews for the final big notification layout.
6299 * @hide
6300 */
6301 public RemoteViews makeBigContentView() {
6302 return null;
6303 }
6304
6305 /**
6306 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08006307 *
6308 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006309 * @hide
6310 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006311 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006312 return null;
6313 }
6314
6315 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006316 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006317 * @hide
6318 */
6319 public void addExtras(Bundle extras) {
6320 if (mSummaryTextSet) {
6321 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
6322 }
6323 if (mBigContentTitle != null) {
6324 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
6325 }
Chris Wren91ad5632013-06-05 15:05:57 -04006326 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006327 }
6328
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006329 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006330 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006331 * @hide
6332 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02006333 protected void restoreFromExtras(Bundle extras) {
6334 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
6335 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
6336 mSummaryTextSet = true;
6337 }
6338 if (extras.containsKey(EXTRA_TITLE_BIG)) {
6339 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
6340 }
6341 }
6342
6343
6344 /**
6345 * @hide
6346 */
6347 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006348 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006349 return wip;
6350 }
6351
Daniel Sandler0ec46202015-06-24 01:27:05 -04006352 /**
6353 * @hide
6354 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006355 public void purgeResources() {}
6356
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006357 /**
6358 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
6359 * attached to.
6360 *
6361 * @return the fully constructed Notification.
6362 */
6363 public Notification build() {
6364 checkBuilder();
6365 return mBuilder.build();
6366 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006367
6368 /**
6369 * @hide
6370 * @return true if the style positions the progress bar on the second line; false if the
6371 * style hides the progress bar
6372 */
6373 protected boolean hasProgress() {
6374 return true;
6375 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006376
6377 /**
6378 * @hide
6379 * @return Whether we should put the summary be put into the notification header
6380 */
6381 public boolean hasSummaryInHeader() {
6382 return true;
6383 }
Selim Cinek593610c2016-02-16 18:42:57 -08006384
6385 /**
6386 * @hide
6387 * @return Whether custom content views are displayed inline in the style
6388 */
6389 public boolean displayCustomViewInline() {
6390 return false;
6391 }
Selim Cinekd0426622017-07-11 13:19:59 +02006392
6393 /**
6394 * Reduces the image sizes contained in this style.
6395 *
6396 * @hide
6397 */
6398 public void reduceImageSizes(Context context) {
6399 }
Selim Cinek90343862018-02-01 11:07:11 -08006400
6401 /**
6402 * Validate that this style was properly composed. This is called at build time.
6403 * @hide
6404 */
6405 public void validate(Context context) {
6406 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006407
6408 /**
6409 * @hide
6410 */
6411 public abstract boolean areNotificationsVisiblyDifferent(Style other);
Selim Cinekc7f5a822018-03-20 19:32:06 -07006412
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006413 /**
koprivaa1a78482018-10-09 10:09:23 -07006414 * @return the text that should be displayed in the statusBar when heads-upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006415 * If {@code null} is returned, the default implementation will be used.
6416 *
6417 * @hide
6418 */
6419 public CharSequence getHeadsUpStatusBarText() {
6420 return null;
6421 }
Joe Onorato46439ce2010-11-19 13:56:21 -08006422 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006423
6424 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006425 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08006426 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006427 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006428 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006429 * Notification notif = new Notification.Builder(mContext)
6430 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
6431 * .setContentText(subject)
6432 * .setSmallIcon(R.drawable.new_post)
6433 * .setLargeIcon(aBitmap)
6434 * .setStyle(new Notification.BigPictureStyle()
6435 * .bigPicture(aBigBitmap))
6436 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006437 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006438 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006439 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006440 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006441 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006442 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006443 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006444 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006445
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006446 public BigPictureStyle() {
6447 }
6448
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006449 /**
6450 * @deprecated use {@code BigPictureStyle()}.
6451 */
6452 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006453 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006454 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006455 }
6456
Chris Wrend6297db2012-05-03 16:20:13 -04006457 /**
6458 * Overrides ContentTitle in the big form of the template.
6459 * This defaults to the value passed to setContentTitle().
6460 */
6461 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006462 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006463 return this;
6464 }
6465
6466 /**
6467 * Set the first line of text after the detail section in the big form of the template.
6468 */
6469 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006470 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006471 return this;
6472 }
6473
Chris Wren0bd664d2012-08-01 13:56:56 -04006474 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05006475 * @hide
6476 */
6477 public Bitmap getBigPicture() {
6478 return mPicture;
6479 }
6480
6481 /**
Chris Wren0bd664d2012-08-01 13:56:56 -04006482 * Provide the bitmap to be used as the payload for the BigPicture notification.
6483 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006484 public BigPictureStyle bigPicture(Bitmap b) {
6485 mPicture = b;
6486 return this;
6487 }
6488
Chris Wren3745a3d2012-05-22 15:11:52 -04006489 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04006490 * Override the large icon when the big notification is shown.
6491 */
6492 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04006493 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
6494 }
6495
6496 /**
6497 * Override the large icon when the big notification is shown.
6498 */
6499 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04006500 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006501 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006502 return this;
6503 }
6504
Riley Andrews0394a0c2015-11-03 23:36:52 -08006505 /** @hide */
6506 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
6507
Daniel Sandler0ec46202015-06-24 01:27:05 -04006508 /**
6509 * @hide
6510 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006511 @Override
6512 public void purgeResources() {
6513 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08006514 if (mPicture != null &&
6515 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08006516 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006517 mPicture = mPicture.createAshmemBitmap();
6518 }
6519 if (mBigLargeIcon != null) {
6520 mBigLargeIcon.convertToAshmem();
6521 }
6522 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01006523
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006524 /**
6525 * @hide
6526 */
Selim Cinekd0426622017-07-11 13:19:59 +02006527 @Override
6528 public void reduceImageSizes(Context context) {
6529 super.reduceImageSizes(context);
6530 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006531 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006532 if (mPicture != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006533 int maxPictureWidth = resources.getDimensionPixelSize(isLowRam
6534 ? R.dimen.notification_big_picture_max_height_low_ram
6535 : R.dimen.notification_big_picture_max_height);
6536 int maxPictureHeight = resources.getDimensionPixelSize(isLowRam
6537 ? R.dimen.notification_big_picture_max_width_low_ram
6538 : R.dimen.notification_big_picture_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006539 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
6540 }
6541 if (mBigLargeIcon != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006542 int rightIconSize = resources.getDimensionPixelSize(isLowRam
6543 ? R.dimen.notification_right_icon_size_low_ram
6544 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006545 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
6546 }
6547 }
6548
6549 /**
6550 * @hide
6551 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006552 public RemoteViews makeBigContentView() {
6553 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01006554 // This covers the following cases:
6555 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006556 // mN.mLargeIcon
6557 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04006558 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07006559 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006560 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006561 oldLargeIcon = mBuilder.mN.mLargeIcon;
6562 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006563 // The legacy largeIcon might not allow us to clear the image, as it's taken in
6564 // replacement if the other one is null. Because we're restoring these legacy icons
6565 // for old listeners, this is in general non-null.
6566 largeIconLegacy = mBuilder.mN.largeIcon;
6567 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006568 }
6569
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006570 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder);
Selim Cinek384804b2018-04-18 14:31:07 +08006571 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource(),
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006572 p, null /* result */);
Selim Cinek03d0d652015-11-13 13:18:09 -05006573 if (mSummaryTextSet) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006574 contentView.setTextViewText(R.id.text, mBuilder.processTextSpans(
6575 mBuilder.processLegacyText(mSummaryText)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006576 mBuilder.setTextViewColorSecondary(contentView, R.id.text, p);
Selim Cinekc848c3a2016-01-13 15:27:30 -08006577 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05006578 }
Selim Cinek279fa862016-06-14 10:57:25 -07006579 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08006580
Christoph Studer5c510ee2014-12-15 16:32:27 +01006581 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006582 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006583 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006584 }
6585
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006586 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006587 return contentView;
6588 }
6589
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006590 /**
6591 * @hide
6592 */
6593 public void addExtras(Bundle extras) {
6594 super.addExtras(extras);
6595
6596 if (mBigLargeIconSet) {
6597 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
6598 }
6599 extras.putParcelable(EXTRA_PICTURE, mPicture);
6600 }
6601
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006602 /**
6603 * @hide
6604 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006605 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02006606 protected void restoreFromExtras(Bundle extras) {
6607 super.restoreFromExtras(extras);
6608
6609 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01006610 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02006611 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04006612 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006613 mPicture = extras.getParcelable(EXTRA_PICTURE);
6614 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006615
6616 /**
6617 * @hide
6618 */
6619 @Override
6620 public boolean hasSummaryInHeader() {
6621 return false;
6622 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006623
6624 /**
6625 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006626 * Note that we aren't actually comparing the contents of the bitmaps here, so this
6627 * is only doing a cursory inspection. Bitmaps of equal size will appear the same.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006628 */
6629 @Override
6630 public boolean areNotificationsVisiblyDifferent(Style other) {
6631 if (other == null || getClass() != other.getClass()) {
6632 return true;
6633 }
6634 BigPictureStyle otherS = (BigPictureStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006635 return areBitmapsObviouslyDifferent(getBigPicture(), otherS.getBigPicture());
6636 }
6637
6638 private static boolean areBitmapsObviouslyDifferent(Bitmap a, Bitmap b) {
6639 if (a == b) {
6640 return false;
6641 }
6642 if (a == null || b == null) {
6643 return true;
6644 }
6645 return a.getWidth() != b.getWidth()
6646 || a.getHeight() != b.getHeight()
6647 || a.getConfig() != b.getConfig()
6648 || a.getGenerationId() != b.getGenerationId();
Julia Reynolds7217dc92018-03-07 12:12:09 -05006649 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006650 }
6651
6652 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006653 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08006654 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006655 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006656 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006657 * Notification notif = new Notification.Builder(mContext)
6658 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
6659 * .setContentText(subject)
6660 * .setSmallIcon(R.drawable.new_mail)
6661 * .setLargeIcon(aBitmap)
6662 * .setStyle(new Notification.BigTextStyle()
6663 * .bigText(aVeryLongString))
6664 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006665 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006666 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006667 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006668 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006669 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006670
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006671 private CharSequence mBigText;
6672
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006673 public BigTextStyle() {
6674 }
6675
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006676 /**
6677 * @deprecated use {@code BigTextStyle()}.
6678 */
6679 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006680 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006681 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006682 }
6683
Chris Wrend6297db2012-05-03 16:20:13 -04006684 /**
6685 * Overrides ContentTitle in the big form of the template.
6686 * This defaults to the value passed to setContentTitle().
6687 */
6688 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006689 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006690 return this;
6691 }
6692
6693 /**
6694 * Set the first line of text after the detail section in the big form of the template.
6695 */
6696 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006697 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006698 return this;
6699 }
6700
Chris Wren0bd664d2012-08-01 13:56:56 -04006701 /**
6702 * Provide the longer text to be displayed in the big form of the
6703 * template in place of the content text.
6704 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006705 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006706 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006707 return this;
6708 }
6709
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006710 /**
6711 * @hide
6712 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05006713 public CharSequence getBigText() {
6714 return mBigText;
6715 }
6716
6717 /**
6718 * @hide
6719 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006720 public void addExtras(Bundle extras) {
6721 super.addExtras(extras);
6722
Christoph Studer4600f9b2014-07-22 22:44:43 +02006723 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
6724 }
6725
6726 /**
6727 * @hide
6728 */
6729 @Override
6730 protected void restoreFromExtras(Bundle extras) {
6731 super.restoreFromExtras(extras);
6732
6733 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006734 }
6735
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006736 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006737 * @param increasedHeight true if this layout be created with an increased height.
6738 *
6739 * @hide
6740 */
6741 @Override
6742 public RemoteViews makeContentView(boolean increasedHeight) {
6743 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006744 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006745 mBuilder.mActions = new ArrayList<>();
6746 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006747 mBuilder.mActions = mBuilder.mOriginalActions;
6748 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006749 return remoteViews;
6750 }
6751 return super.makeContentView(increasedHeight);
6752 }
6753
6754 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006755 * @hide
6756 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006757 @Override
6758 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
6759 if (increasedHeight && mBuilder.mActions.size() > 0) {
6760 return makeBigContentView();
6761 }
6762 return super.makeHeadsUpContentView(increasedHeight);
6763 }
6764
6765 /**
6766 * @hide
6767 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006768 public RemoteViews makeBigContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006769 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder).text(null);
Selim Cinek384804b2018-04-18 14:31:07 +08006770 TemplateBindResult result = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006771 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource(), p,
6772 result);
Selim Cinek1c72fa02018-04-23 18:00:54 +08006773 contentView.setInt(R.id.big_text, "setImageEndMargin", result.getIconMarginEnd());
Joe Malin8d40d042012-11-05 11:36:40 -08006774
Selim Cinek3a2c4b92015-12-17 17:01:17 -08006775 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07006776 if (TextUtils.isEmpty(bigTextText)) {
6777 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
6778 // experience
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006779 bigTextText = mBuilder.processLegacyText(
6780 mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT));
Selim Cinek75998782016-04-26 10:39:17 -07006781 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006782 contentView.setTextViewText(R.id.big_text, mBuilder.processTextSpans(bigTextText));
6783 mBuilder.setTextViewColorSecondary(contentView, R.id.big_text, p);
6784 contentView.setViewVisibility(R.id.big_text,
6785 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08006786 contentView.setBoolean(R.id.big_text, "setHasImage",
6787 result.isRightIconContainerVisible());
Selim Cinek4fb12d32015-11-19 18:10:48 -08006788
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006789 return contentView;
6790 }
6791
Julia Reynolds7217dc92018-03-07 12:12:09 -05006792 /**
6793 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006794 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006795 */
6796 @Override
6797 public boolean areNotificationsVisiblyDifferent(Style other) {
6798 if (other == null || getClass() != other.getClass()) {
6799 return true;
6800 }
6801 BigTextStyle newS = (BigTextStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006802 return !Objects.equals(String.valueOf(getBigText()), String.valueOf(newS.getBigText()));
Julia Reynolds7217dc92018-03-07 12:12:09 -05006803 }
6804
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006805 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006806
6807 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006808 * Helper class for generating large-format notifications that include multiple back-and-forth
6809 * messages of varying types between any number of people.
6810 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006811 * <p>
Alex Hillsfc737de2016-03-23 17:33:02 -04006812 * If the platform does not provide large-format notifications, this method has no effect. The
6813 * user will always see the normal notification view.
Selim Cinekce8794f2018-05-23 16:46:05 -07006814 *
6815 * <p>
6816 * If the app is targeting Android P and above, it is required to use the {@link Person}
6817 * class in order to get an optimal rendering of the notification and its avatars. For
6818 * conversations involving multiple people, the app should also make sure that it marks the
6819 * conversation as a group with {@link #setGroupConversation(boolean)}.
6820 *
6821 * <p>
6822 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior.
6823 * Here's an example of how this may be used:
Alex Hillsfc737de2016-03-23 17:33:02 -04006824 * <pre class="prettyprint">
6825 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006826 * Person user = new Person.Builder().setIcon(userIcon).setName(userName).build();
6827 * MessagingStyle style = new MessagingStyle(user)
6828 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getPerson())
6829 * .addMessage(messages[2].getText(), messages[2].getTime(), messages[2].getPerson())
6830 * .setGroupConversation(hasMultiplePeople());
6831 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006832 * Notification noti = new Notification.Builder()
Selim Cinekce8794f2018-05-23 16:46:05 -07006833 * .setContentTitle(&quot;2 new messages with &quot; + sender.toString())
Alex Hillsfc737de2016-03-23 17:33:02 -04006834 * .setContentText(subject)
6835 * .setSmallIcon(R.drawable.new_message)
6836 * .setLargeIcon(aBitmap)
Selim Cinekce8794f2018-05-23 16:46:05 -07006837 * .setStyle(style)
Alex Hillsfc737de2016-03-23 17:33:02 -04006838 * .build();
6839 * </pre>
6840 */
6841 public static class MessagingStyle extends Style {
6842
6843 /**
6844 * The maximum number of messages that will be retained in the Notification itself (the
6845 * number displayed is up to the platform).
6846 */
6847 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
6848
Selim Cinekcb8b9852017-12-15 18:01:52 -08006849 @NonNull Person mUser;
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006850 @Nullable CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04006851 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08006852 List<Message> mHistoricMessages = new ArrayList<>();
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006853 boolean mIsGroupConversation;
Alex Hillsfc737de2016-03-23 17:33:02 -04006854
6855 MessagingStyle() {
6856 }
6857
6858 /**
Alex Hillsfd590442016-10-07 09:52:44 -04006859 * @param userDisplayName Required - the name to be displayed for any replies sent by the
6860 * user before the posting app reposts the notification with those messages after they've
6861 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04006862 * {@link #addMessage(Notification.MessagingStyle.Message)}
Selim Cinekcb8b9852017-12-15 18:01:52 -08006863 *
6864 * @deprecated use {@code MessagingStyle(Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006865 */
Alex Hillsfd590442016-10-07 09:52:44 -04006866 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Selim Cinek9acd6732018-03-23 16:39:02 -07006867 this(new Person.Builder().setName(userDisplayName).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006868 }
6869
6870 /**
6871 * @param user Required - The person displayed for any messages that are sent by the
6872 * user. Any messages added with {@link #addMessage(Notification.MessagingStyle.Message)}
6873 * who don't have a Person associated with it will be displayed as if they were sent
Selim Cinek90343862018-02-01 11:07:11 -08006874 * by this user. The user also needs to have a valid name associated with it, which will
6875 * be enforced starting in Android P.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006876 */
6877 public MessagingStyle(@NonNull Person user) {
6878 mUser = user;
Selim Cinek90343862018-02-01 11:07:11 -08006879 }
6880
6881 /**
6882 * Validate that this style was properly composed. This is called at build time.
6883 * @hide
6884 */
6885 @Override
6886 public void validate(Context context) {
6887 super.validate(context);
6888 if (context.getApplicationInfo().targetSdkVersion
6889 >= Build.VERSION_CODES.P && (mUser == null || mUser.getName() == null)) {
6890 throw new RuntimeException("User must be valid and have a name.");
Selim Cinekcb8b9852017-12-15 18:01:52 -08006891 }
6892 }
6893
6894 /**
koprivaa1a78482018-10-09 10:09:23 -07006895 * @return the text that should be displayed in the statusBar when heads upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006896 * If {@code null} is returned, the default implementation will be used.
6897 *
6898 * @hide
6899 */
6900 @Override
6901 public CharSequence getHeadsUpStatusBarText() {
6902 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
6903 ? super.mBigContentTitle
6904 : mConversationTitle;
6905 if (!TextUtils.isEmpty(conversationTitle) && !hasOnlyWhiteSpaceSenders()) {
6906 return conversationTitle;
6907 }
6908 return null;
6909 }
6910
6911 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08006912 * @return the user to be displayed for any replies sent by the user
6913 */
Selim Cinekeb53c222018-04-06 12:56:09 -07006914 @NonNull
Selim Cinekcb8b9852017-12-15 18:01:52 -08006915 public Person getUser() {
6916 return mUser;
Alex Hillsfc737de2016-03-23 17:33:02 -04006917 }
6918
6919 /**
6920 * Returns the name to be displayed for any replies sent by the user
Selim Cinekcb8b9852017-12-15 18:01:52 -08006921 *
6922 * @deprecated use {@link #getUser()} instead
Alex Hillsfc737de2016-03-23 17:33:02 -04006923 */
6924 public CharSequence getUserDisplayName() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006925 return mUser.getName();
Alex Hillsfc737de2016-03-23 17:33:02 -04006926 }
6927
6928 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006929 * Sets the title to be displayed on this conversation. May be set to {@code null}.
6930 *
Kodlee Yin14656422017-12-22 17:00:46 -08006931 * <p>This API's behavior was changed in SDK version {@link Build.VERSION_CODES#P}. If your
6932 * application's target version is less than {@link Build.VERSION_CODES#P}, setting a
6933 * conversation title to a non-null value will make {@link #isGroupConversation()} return
6934 * {@code true} and passing {@code null} will make it return {@code false}. In
6935 * {@link Build.VERSION_CODES#P} and beyond, use {@link #setGroupConversation(boolean)}
6936 * to set group conversation status.
6937 *
6938 * @param conversationTitle Title displayed for this conversation
6939 * @return this object for method chaining
Alex Hillsfc737de2016-03-23 17:33:02 -04006940 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006941 public MessagingStyle setConversationTitle(@Nullable CharSequence conversationTitle) {
Alex Hillsfc737de2016-03-23 17:33:02 -04006942 mConversationTitle = conversationTitle;
6943 return this;
6944 }
6945
6946 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006947 * Return the title to be displayed on this conversation. May return {@code null}.
Alex Hillsfc737de2016-03-23 17:33:02 -04006948 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006949 @Nullable
Alex Hillsfc737de2016-03-23 17:33:02 -04006950 public CharSequence getConversationTitle() {
6951 return mConversationTitle;
6952 }
6953
6954 /**
6955 * Adds a message for display by this notification. Convenience call for a simple
6956 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6957 * @param text A {@link CharSequence} to be displayed as the message content
6958 * @param timestamp Time at which the message arrived
6959 * @param sender A {@link CharSequence} to be used for displaying the name of the
6960 * sender. Should be <code>null</code> for messages by the current user, in which case
6961 * the platform will insert {@link #getUserDisplayName()}.
6962 * Should be unique amongst all individuals in the conversation, and should be
6963 * consistent during re-posts of the notification.
6964 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07006965 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Alex Hillsfc737de2016-03-23 17:33:02 -04006966 *
6967 * @return this object for method chaining
Selim Cinekcb8b9852017-12-15 18:01:52 -08006968 *
6969 * @deprecated use {@link #addMessage(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006970 */
6971 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006972 return addMessage(text, timestamp,
Selim Cinek9acd6732018-03-23 16:39:02 -07006973 sender == null ? null : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006974 }
6975
6976 /**
6977 * Adds a message for display by this notification. Convenience call for a simple
6978 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6979 * @param text A {@link CharSequence} to be displayed as the message content
6980 * @param timestamp Time at which the message arrived
6981 * @param sender The {@link Person} who sent the message.
6982 * Should be <code>null</code> for messages by the current user, in which case
6983 * the platform will insert the user set in {@code MessagingStyle(Person)}.
6984 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07006985 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Selim Cinekcb8b9852017-12-15 18:01:52 -08006986 *
6987 * @return this object for method chaining
6988 */
Selim Cinekeb53c222018-04-06 12:56:09 -07006989 public MessagingStyle addMessage(@NonNull CharSequence text, long timestamp,
6990 @Nullable Person sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08006991 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04006992 }
6993
6994 /**
6995 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08006996 *
6997 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6998 * the newest last.
6999 *
Alex Hillsfc737de2016-03-23 17:33:02 -04007000 * @param message The {@link Message} to be displayed
7001 * @return this object for method chaining
7002 */
7003 public MessagingStyle addMessage(Message message) {
7004 mMessages.add(message);
7005 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
7006 mMessages.remove(0);
7007 }
7008 return this;
7009 }
7010
7011 /**
Adrian Roos437cd562017-01-18 15:47:03 -08007012 * Adds a {@link Message} for historic context in this notification.
7013 *
7014 * <p>Messages should be added as historic if they are not the main subject of the
7015 * notification but may give context to a conversation. The system may choose to present
7016 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
7017 *
7018 * <p>The messages should be added in chronologic order, i.e. the oldest first,
7019 * the newest last.
7020 *
7021 * @param message The historic {@link Message} to be added
7022 * @return this object for method chaining
7023 */
7024 public MessagingStyle addHistoricMessage(Message message) {
7025 mHistoricMessages.add(message);
7026 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
7027 mHistoricMessages.remove(0);
7028 }
7029 return this;
7030 }
7031
7032 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007033 * Gets the list of {@code Message} objects that represent the notification
7034 */
7035 public List<Message> getMessages() {
7036 return mMessages;
7037 }
7038
7039 /**
Adrian Roos437cd562017-01-18 15:47:03 -08007040 * Gets the list of historic {@code Message}s in the notification.
7041 */
7042 public List<Message> getHistoricMessages() {
7043 return mHistoricMessages;
7044 }
7045
7046 /**
Selim Cinekce8794f2018-05-23 16:46:05 -07007047 * Sets whether this conversation notification represents a group. If the app is targeting
7048 * Android P, this is required if the app wants to display the largeIcon set with
7049 * {@link Notification.Builder#setLargeIcon(Bitmap)}, otherwise it will be hidden.
Kodlee Yin14656422017-12-22 17:00:46 -08007050 *
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007051 * @param isGroupConversation {@code true} if the conversation represents a group,
7052 * {@code false} otherwise.
7053 * @return this object for method chaining
7054 */
7055 public MessagingStyle setGroupConversation(boolean isGroupConversation) {
7056 mIsGroupConversation = isGroupConversation;
7057 return this;
7058 }
7059
7060 /**
Kodlee Yin14656422017-12-22 17:00:46 -08007061 * Returns {@code true} if this notification represents a group conversation, otherwise
7062 * {@code false}.
7063 *
7064 * <p> If the application that generated this {@link MessagingStyle} targets an SDK version
7065 * less than {@link Build.VERSION_CODES#P}, this method becomes dependent on whether or
7066 * not the conversation title is set; returning {@code true} if the conversation title is
7067 * a non-null value, or {@code false} otherwise. From {@link Build.VERSION_CODES#P} forward,
7068 * this method returns what's set by {@link #setGroupConversation(boolean)} allowing for
7069 * named, non-group conversations.
7070 *
7071 * @see #setConversationTitle(CharSequence)
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007072 */
7073 public boolean isGroupConversation() {
Kodlee Yin14656422017-12-22 17:00:46 -08007074 // When target SDK version is < P, a non-null conversation title dictates if this is
7075 // as group conversation.
7076 if (mBuilder != null
7077 && mBuilder.mContext.getApplicationInfo().targetSdkVersion
7078 < Build.VERSION_CODES.P) {
7079 return mConversationTitle != null;
7080 }
7081
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007082 return mIsGroupConversation;
7083 }
7084
7085 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007086 * @hide
7087 */
7088 @Override
7089 public void addExtras(Bundle extras) {
7090 super.addExtras(extras);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007091 if (mUser != null) {
7092 // For legacy usages
7093 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUser.getName());
7094 extras.putParcelable(EXTRA_MESSAGING_PERSON, mUser);
Alex Hillsfc737de2016-03-23 17:33:02 -04007095 }
7096 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007097 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04007098 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007099 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
7100 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04007101 }
Adrian Roos437cd562017-01-18 15:47:03 -08007102 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
7103 Message.getBundleArrayForMessages(mHistoricMessages));
7104 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007105
7106 fixTitleAndTextExtras(extras);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007107 extras.putBoolean(EXTRA_IS_GROUP_CONVERSATION, mIsGroupConversation);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007108 }
7109
7110 private void fixTitleAndTextExtras(Bundle extras) {
7111 Message m = findLatestIncomingMessage();
7112 CharSequence text = (m == null) ? null : m.mText;
7113 CharSequence sender = m == null ? null
Selim Cinekcb8b9852017-12-15 18:01:52 -08007114 : m.mSender == null || TextUtils.isEmpty(m.mSender.getName())
7115 ? mUser.getName() : m.mSender.getName();
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007116 CharSequence title;
7117 if (!TextUtils.isEmpty(mConversationTitle)) {
7118 if (!TextUtils.isEmpty(sender)) {
7119 BidiFormatter bidi = BidiFormatter.getInstance();
7120 title = mBuilder.mContext.getString(
7121 com.android.internal.R.string.notification_messaging_title_template,
Selim Cinekcb8b9852017-12-15 18:01:52 -08007122 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(sender));
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007123 } else {
7124 title = mConversationTitle;
7125 }
7126 } else {
7127 title = sender;
7128 }
7129
7130 if (title != null) {
7131 extras.putCharSequence(EXTRA_TITLE, title);
7132 }
7133 if (text != null) {
7134 extras.putCharSequence(EXTRA_TEXT, text);
7135 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007136 }
7137
7138 /**
7139 * @hide
7140 */
7141 @Override
7142 protected void restoreFromExtras(Bundle extras) {
7143 super.restoreFromExtras(extras);
7144
Selim Cinekcb8b9852017-12-15 18:01:52 -08007145 mUser = extras.getParcelable(EXTRA_MESSAGING_PERSON);
7146 if (mUser == null) {
7147 CharSequence displayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
Selim Cinek9acd6732018-03-23 16:39:02 -07007148 mUser = new Person.Builder().setName(displayName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007149 }
Adrian Roos96b7e202016-05-17 13:50:38 -07007150 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08007151 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007152 mMessages = Message.getMessagesFromBundleArray(messages);
Adrian Roos437cd562017-01-18 15:47:03 -08007153 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007154 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007155 mIsGroupConversation = extras.getBoolean(EXTRA_IS_GROUP_CONVERSATION);
Alex Hillsfc737de2016-03-23 17:33:02 -04007156 }
7157
7158 /**
7159 * @hide
7160 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07007161 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007162 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07007163 mBuilder.mOriginalActions = mBuilder.mActions;
7164 mBuilder.mActions = new ArrayList<>();
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007165 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007166 false /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007167 mBuilder.mActions = mBuilder.mOriginalActions;
7168 mBuilder.mOriginalActions = null;
7169 return remoteViews;
Adrian Roosc1a80b02016-04-05 14:54:55 -07007170 }
7171
Julia Reynolds7217dc92018-03-07 12:12:09 -05007172 /**
7173 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04007174 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05007175 */
7176 @Override
7177 public boolean areNotificationsVisiblyDifferent(Style other) {
7178 if (other == null || getClass() != other.getClass()) {
7179 return true;
7180 }
7181 MessagingStyle newS = (MessagingStyle) other;
7182 List<MessagingStyle.Message> oldMs = getMessages();
7183 List<MessagingStyle.Message> newMs = newS.getMessages();
7184
Dan Sandler7d67bd42018-05-15 14:06:38 -04007185 if (oldMs == null || newMs == null) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007186 newMs = new ArrayList<>();
7187 }
7188
7189 int n = oldMs.size();
7190 if (n != newMs.size()) {
7191 return true;
7192 }
7193 for (int i = 0; i < n; i++) {
7194 MessagingStyle.Message oldM = oldMs.get(i);
7195 MessagingStyle.Message newM = newMs.get(i);
Dan Sandler7d67bd42018-05-15 14:06:38 -04007196 if (!Objects.equals(
7197 String.valueOf(oldM.getText()),
7198 String.valueOf(newM.getText()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007199 return true;
7200 }
7201 if (!Objects.equals(oldM.getDataUri(), newM.getDataUri())) {
7202 return true;
7203 }
Dan Sandler7d67bd42018-05-15 14:06:38 -04007204 String oldSender = String.valueOf(oldM.getSenderPerson() == null
7205 ? oldM.getSender()
7206 : oldM.getSenderPerson().getName());
7207 String newSender = String.valueOf(newM.getSenderPerson() == null
7208 ? newM.getSender()
7209 : newM.getSenderPerson().getName());
Julia Reynolds7217dc92018-03-07 12:12:09 -05007210 if (!Objects.equals(oldSender, newSender)) {
7211 return true;
7212 }
7213
7214 String oldKey = oldM.getSenderPerson() == null
7215 ? null : oldM.getSenderPerson().getKey();
7216 String newKey = newM.getSenderPerson() == null
7217 ? null : newM.getSenderPerson().getKey();
7218 if (!Objects.equals(oldKey, newKey)) {
7219 return true;
7220 }
7221 // Other fields (like timestamp) intentionally excluded
7222 }
7223 return false;
7224 }
7225
Adrian Roosc1a80b02016-04-05 14:54:55 -07007226 private Message findLatestIncomingMessage() {
Selim Cinek88188f22017-09-19 16:46:56 -07007227 return findLatestIncomingMessage(mMessages);
7228 }
7229
7230 /**
7231 * @hide
7232 */
7233 @Nullable
7234 public static Message findLatestIncomingMessage(
7235 List<Message> messages) {
7236 for (int i = messages.size() - 1; i >= 0; i--) {
7237 Message m = messages.get(i);
Adrian Roosc1a80b02016-04-05 14:54:55 -07007238 // Incoming messages have a non-empty sender.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007239 if (m.mSender != null && !TextUtils.isEmpty(m.mSender.getName())) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07007240 return m;
7241 }
7242 }
Selim Cinek88188f22017-09-19 16:46:56 -07007243 if (!messages.isEmpty()) {
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007244 // No incoming messages, fall back to outgoing message
Selim Cinek88188f22017-09-19 16:46:56 -07007245 return messages.get(messages.size() - 1);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007246 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07007247 return null;
7248 }
7249
7250 /**
7251 * @hide
7252 */
7253 @Override
7254 public RemoteViews makeBigContentView() {
Selim Cinek384804b2018-04-18 14:31:07 +08007255 return makeMessagingView(false /* displayImagesAtEnd */, true /* hideLargeIcon */);
Selim Cinekafeed292017-12-12 17:32:44 -08007256 }
7257
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007258 /**
7259 * Create a messaging layout.
7260 *
7261 * @param displayImagesAtEnd should images be displayed at the end of the content instead
7262 * of inline.
Selim Cinek384804b2018-04-18 14:31:07 +08007263 * @param hideRightIcons Should the reply affordance be shown at the end of the notification
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007264 * @return the created remoteView.
7265 */
Selim Cinekafeed292017-12-12 17:32:44 -08007266 @NonNull
Selim Cinek384804b2018-04-18 14:31:07 +08007267 private RemoteViews makeMessagingView(boolean displayImagesAtEnd, boolean hideRightIcons) {
Selim Cinek88188f22017-09-19 16:46:56 -07007268 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
Adrian Roosc1a80b02016-04-05 14:54:55 -07007269 ? super.mBigContentTitle
7270 : mConversationTitle;
Selim Cinekce8794f2018-05-23 16:46:05 -07007271 boolean atLeastP = mBuilder.mContext.getApplicationInfo().targetSdkVersion
7272 >= Build.VERSION_CODES.P;
7273 boolean isOneToOne;
Selim Cinek2dd3e722018-01-19 11:06:06 -08007274 CharSequence nameReplacement = null;
Selim Cinekce8794f2018-05-23 16:46:05 -07007275 Icon avatarReplacement = null;
7276 if (!atLeastP) {
7277 isOneToOne = TextUtils.isEmpty(conversationTitle);
7278 avatarReplacement = mBuilder.mN.mLargeIcon;
7279 if (hasOnlyWhiteSpaceSenders()) {
7280 isOneToOne = true;
7281 nameReplacement = conversationTitle;
7282 conversationTitle = null;
7283 }
7284 } else {
7285 isOneToOne = !isGroupConversation();
Adrian Roosb1f427c2016-05-26 12:27:15 -07007286 }
Selim Cinek384804b2018-04-18 14:31:07 +08007287 TemplateBindResult bindResult = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007288 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).title(
7289 conversationTitle).text(null)
7290 .hideLargeIcon(hideRightIcons || isOneToOne)
7291 .hideReplyIcon(hideRightIcons)
7292 .headerTextSecondary(conversationTitle);
Adrian Roos48d746a2016-04-12 14:57:28 -07007293 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07007294 mBuilder.getMessagingLayoutResource(),
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007295 p,
Selim Cinek384804b2018-04-18 14:31:07 +08007296 bindResult);
Selim Cinek88188f22017-09-19 16:46:56 -07007297 addExtras(mBuilder.mN.extras);
Selim Cinekafeed292017-12-12 17:32:44 -08007298 // also update the end margin if there is an image
Selim Cinek1c72fa02018-04-23 18:00:54 +08007299 contentView.setViewLayoutMarginEnd(R.id.notification_messaging,
7300 bindResult.getIconMarginEnd());
Selim Cinek88188f22017-09-19 16:46:56 -07007301 contentView.setInt(R.id.status_bar_latest_event_content, "setLayoutColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007302 mBuilder.isColorized(p) ? mBuilder.getPrimaryTextColor(p)
7303 : mBuilder.resolveContrastColor(p));
Kenny Guy14d035c2018-05-02 19:10:36 +01007304 contentView.setInt(R.id.status_bar_latest_event_content, "setSenderTextColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007305 mBuilder.getPrimaryTextColor(p));
Kenny Guy14d035c2018-05-02 19:10:36 +01007306 contentView.setInt(R.id.status_bar_latest_event_content, "setMessageTextColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007307 mBuilder.getSecondaryTextColor(p));
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007308 contentView.setBoolean(R.id.status_bar_latest_event_content, "setDisplayImagesAtEnd",
7309 displayImagesAtEnd);
Selim Cinekce8794f2018-05-23 16:46:05 -07007310 contentView.setIcon(R.id.status_bar_latest_event_content, "setAvatarReplacement",
7311 avatarReplacement);
Selim Cinek2dd3e722018-01-19 11:06:06 -08007312 contentView.setCharSequence(R.id.status_bar_latest_event_content, "setNameReplacement",
7313 nameReplacement);
Selim Cinek88188f22017-09-19 16:46:56 -07007314 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsOneToOne",
7315 isOneToOne);
7316 contentView.setBundle(R.id.status_bar_latest_event_content, "setData",
7317 mBuilder.mN.extras);
Alex Hillsfc737de2016-03-23 17:33:02 -04007318 return contentView;
7319 }
7320
Selim Cinekf7409db2017-10-24 16:17:14 -07007321 private boolean hasOnlyWhiteSpaceSenders() {
7322 for (int i = 0; i < mMessages.size(); i++) {
7323 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007324 Person sender = m.getSenderPerson();
7325 if (sender != null && !isWhiteSpace(sender.getName())) {
Selim Cinekf7409db2017-10-24 16:17:14 -07007326 return false;
7327 }
7328 }
7329 return true;
7330 }
7331
7332 private boolean isWhiteSpace(CharSequence sender) {
7333 if (TextUtils.isEmpty(sender)) {
7334 return true;
7335 }
7336 if (sender.toString().matches("^\\s*$")) {
7337 return true;
7338 }
7339 // Let's check if we only have 0 whitespace chars. Some apps did this as a workaround
7340 // For the presentation that we had.
7341 for (int i = 0; i < sender.length(); i++) {
7342 char c = sender.charAt(i);
7343 if (c != '\u200B') {
7344 return false;
7345 }
7346 }
7347 return true;
7348 }
7349
Selim Cinek88188f22017-09-19 16:46:56 -07007350 private CharSequence createConversationTitleFromMessages() {
7351 ArraySet<CharSequence> names = new ArraySet<>();
7352 for (int i = 0; i < mMessages.size(); i++) {
7353 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007354 Person sender = m.getSenderPerson();
Selim Cinek88188f22017-09-19 16:46:56 -07007355 if (sender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007356 names.add(sender.getName());
Selim Cinek88188f22017-09-19 16:46:56 -07007357 }
7358 }
7359 SpannableStringBuilder title = new SpannableStringBuilder();
7360 int size = names.size();
7361 for (int i = 0; i < size; i++) {
7362 CharSequence name = names.valueAt(i);
7363 if (!TextUtils.isEmpty(title)) {
7364 title.append(", ");
7365 }
7366 title.append(BidiFormatter.getInstance().unicodeWrap(name));
7367 }
7368 return title;
7369 }
7370
Adrian Roosdedd1df2016-04-26 16:38:47 -07007371 /**
7372 * @hide
7373 */
7374 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007375 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007376 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007377 true /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007378 remoteViews.setInt(R.id.notification_messaging, "setMaxDisplayedLines", 1);
7379 return remoteViews;
Adrian Roosdedd1df2016-04-26 16:38:47 -07007380 }
7381
Adrian Roosc1a80b02016-04-05 14:54:55 -07007382 private static TextAppearanceSpan makeFontColorSpan(int color) {
7383 return new TextAppearanceSpan(null, 0, 0,
7384 ColorStateList.valueOf(color), null);
7385 }
7386
Alex Hillsd9b04d92016-04-11 16:38:16 -04007387 public static final class Message {
Tony Mak09db2ea2018-06-27 18:12:48 +01007388 /** @hide */
7389 public static final String KEY_TEXT = "text";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007390 static final String KEY_TIMESTAMP = "time";
7391 static final String KEY_SENDER = "sender";
Selim Cinekcb8b9852017-12-15 18:01:52 -08007392 static final String KEY_SENDER_PERSON = "sender_person";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007393 static final String KEY_DATA_MIME_TYPE = "type";
7394 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08007395 static final String KEY_EXTRAS_BUNDLE = "extras";
Kenny Guya0f6de82018-04-06 16:20:16 +01007396 static final String KEY_REMOTE_INPUT_HISTORY = "remote_input_history";
Alex Hillsfc737de2016-03-23 17:33:02 -04007397
7398 private final CharSequence mText;
7399 private final long mTimestamp;
Selim Cinekcb8b9852017-12-15 18:01:52 -08007400 @Nullable
7401 private final Person mSender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007402 /** True if this message was generated from the extra
7403 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}
7404 */
7405 private final boolean mRemoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007406
Shane Brennan5a871862017-03-11 13:14:17 -08007407 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007408 private String mDataMimeType;
7409 private Uri mDataUri;
7410
7411 /**
7412 * Constructor
7413 * @param text A {@link CharSequence} to be displayed as the message content
7414 * @param timestamp Time at which the message arrived
7415 * @param sender A {@link CharSequence} to be used for displaying the name of the
7416 * sender. Should be <code>null</code> for messages by the current user, in which case
7417 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
7418 * Should be unique amongst all individuals in the conversation, and should be
7419 * consistent during re-posts of the notification.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007420 *
7421 * @deprecated use {@code Message(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04007422 */
7423 public Message(CharSequence text, long timestamp, CharSequence sender){
Selim Cinek9acd6732018-03-23 16:39:02 -07007424 this(text, timestamp, sender == null ? null
7425 : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08007426 }
7427
7428 /**
7429 * Constructor
7430 * @param text A {@link CharSequence} to be displayed as the message content
7431 * @param timestamp Time at which the message arrived
7432 * @param sender The {@link Person} who sent the message.
7433 * Should be <code>null</code> for messages by the current user, in which case
7434 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7435 * <p>
Selim Cinek9acd6732018-03-23 16:39:02 -07007436 * The person provided should contain an Icon, set with
7437 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7438 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7439 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7440 * to differentiate between the different users.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007441 * </p>
7442 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007443 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender) {
Kenny Guya0f6de82018-04-06 16:20:16 +01007444 this(text, timestamp, sender, false /* remoteHistory */);
7445 }
7446
7447 /**
7448 * Constructor
7449 * @param text A {@link CharSequence} to be displayed as the message content
7450 * @param timestamp Time at which the message arrived
7451 * @param sender The {@link Person} who sent the message.
7452 * Should be <code>null</code> for messages by the current user, in which case
7453 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7454 * @param remoteInputHistory True if the messages was generated from the extra
7455 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7456 * <p>
7457 * The person provided should contain an Icon, set with
7458 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7459 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7460 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7461 * to differentiate between the different users.
7462 * </p>
7463 * @hide
7464 */
7465 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender,
7466 boolean remoteInputHistory) {
Alex Hillsfc737de2016-03-23 17:33:02 -04007467 mText = text;
7468 mTimestamp = timestamp;
7469 mSender = sender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007470 mRemoteInputHistory = remoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007471 }
7472
7473 /**
7474 * Sets a binary blob of data and an associated MIME type for a message. In the case
7475 * where the platform doesn't support the MIME type, the original text provided in the
7476 * constructor will be used.
7477 * @param dataMimeType The MIME type of the content. See
7478 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
7479 * types on Android and Android Wear.
7480 * @param dataUri The uri containing the content whose type is given by the MIME type.
7481 * <p class="note">
7482 * <ol>
7483 * <li>Notification Listeners including the System UI need permission to access the
7484 * data the Uri points to. The recommended ways to do this are:</li>
7485 * <li>Store the data in your own ContentProvider, making sure that other apps have
7486 * the correct permission to access your provider. The preferred mechanism for
7487 * providing access is to use per-URI permissions which are temporary and only
7488 * grant access to the receiving application. An easy way to create a
7489 * ContentProvider like this is to use the FileProvider helper class.</li>
7490 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
7491 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
7492 * also store non-media types (see MediaStore.Files for more info). Files can be
7493 * inserted into the MediaStore using scanFile() after which a content:// style
7494 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
7495 * Note that once added to the system MediaStore the content is accessible to any
7496 * app on the device.</li>
7497 * </ol>
7498 * @return this object for method chaining
7499 */
7500 public Message setData(String dataMimeType, Uri dataUri) {
7501 mDataMimeType = dataMimeType;
7502 mDataUri = dataUri;
7503 return this;
7504 }
7505
Alex Hillsfc737de2016-03-23 17:33:02 -04007506 /**
7507 * Get the text to be used for this message, or the fallback text if a type and content
7508 * Uri have been set
7509 */
7510 public CharSequence getText() {
7511 return mText;
7512 }
7513
7514 /**
7515 * Get the time at which this message arrived
7516 */
7517 public long getTimestamp() {
7518 return mTimestamp;
7519 }
7520
7521 /**
Shane Brennan5a871862017-03-11 13:14:17 -08007522 * Get the extras Bundle for this message.
7523 */
7524 public Bundle getExtras() {
7525 return mExtras;
7526 }
7527
7528 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007529 * Get the text used to display the contact's name in the messaging experience
Selim Cinekcb8b9852017-12-15 18:01:52 -08007530 *
7531 * @deprecated use {@link #getSenderPerson()}
Alex Hillsfc737de2016-03-23 17:33:02 -04007532 */
7533 public CharSequence getSender() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007534 return mSender == null ? null : mSender.getName();
7535 }
7536
7537 /**
7538 * Get the sender associated with this message.
7539 */
7540 @Nullable
7541 public Person getSenderPerson() {
Alex Hillsfc737de2016-03-23 17:33:02 -04007542 return mSender;
7543 }
7544
7545 /**
7546 * Get the MIME type of the data pointed to by the Uri
7547 */
7548 public String getDataMimeType() {
7549 return mDataMimeType;
7550 }
7551
7552 /**
koprivaa1a78482018-10-09 10:09:23 -07007553 * Get the Uri pointing to the content of the message. Can be null, in which case
Alex Hillsfc737de2016-03-23 17:33:02 -04007554 * {@see #getText()} is used.
7555 */
7556 public Uri getDataUri() {
7557 return mDataUri;
7558 }
7559
Kenny Guya0f6de82018-04-06 16:20:16 +01007560 /**
7561 * @return True if the message was generated from
7562 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7563 * @hide
7564 */
7565 public boolean isRemoteInputHistory() {
7566 return mRemoteInputHistory;
7567 }
7568
Beverlye98937a2018-11-15 10:18:57 -05007569 /**
7570 * @hide
7571 */
7572 @VisibleForTesting
7573 public Bundle toBundle() {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007574 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007575 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007576 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04007577 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007578 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04007579 if (mSender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007580 // Legacy listeners need this
7581 bundle.putCharSequence(KEY_SENDER, mSender.getName());
7582 bundle.putParcelable(KEY_SENDER_PERSON, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04007583 }
7584 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007585 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04007586 }
7587 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007588 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04007589 }
Shane Brennan5a871862017-03-11 13:14:17 -08007590 if (mExtras != null) {
7591 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
7592 }
Kenny Guya0f6de82018-04-06 16:20:16 +01007593 if (mRemoteInputHistory) {
7594 bundle.putBoolean(KEY_REMOTE_INPUT_HISTORY, mRemoteInputHistory);
7595 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007596 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04007597 }
7598
Alex Hillsd9b04d92016-04-11 16:38:16 -04007599 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
7600 Bundle[] bundles = new Bundle[messages.size()];
7601 final int N = messages.size();
7602 for (int i = 0; i < N; i++) {
7603 bundles[i] = messages.get(i).toBundle();
7604 }
7605 return bundles;
7606 }
7607
Selim Cinek88188f22017-09-19 16:46:56 -07007608 /**
7609 * @return A list of messages read from the bundles.
7610 *
7611 * @hide
7612 */
7613 public static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
7614 if (bundles == null) {
7615 return new ArrayList<>();
7616 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007617 List<Message> messages = new ArrayList<>(bundles.length);
7618 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07007619 if (bundles[i] instanceof Bundle) {
7620 Message message = getMessageFromBundle((Bundle)bundles[i]);
7621 if (message != null) {
7622 messages.add(message);
7623 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007624 }
7625 }
7626 return messages;
7627 }
7628
Selim Cinekb0dc61b2018-05-22 18:49:36 -07007629 /**
7630 * @return The message that is stored in the bundle or null if the message couldn't be
7631 * resolved.
7632 *
7633 * @hide
7634 */
7635 @Nullable
7636 public static Message getMessageFromBundle(Bundle bundle) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007637 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07007638 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007639 return null;
7640 } else {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007641
7642 Person senderPerson = bundle.getParcelable(KEY_SENDER_PERSON);
7643 if (senderPerson == null) {
7644 // Legacy apps that use compat don't actually provide the sender objects
7645 // We need to fix the compat version to provide people / use
7646 // the native api instead
7647 CharSequence senderName = bundle.getCharSequence(KEY_SENDER);
7648 if (senderName != null) {
Selim Cinek9acd6732018-03-23 16:39:02 -07007649 senderPerson = new Person.Builder().setName(senderName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007650 }
7651 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007652 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
Selim Cinekcb8b9852017-12-15 18:01:52 -08007653 bundle.getLong(KEY_TIMESTAMP),
Kenny Guya0f6de82018-04-06 16:20:16 +01007654 senderPerson,
7655 bundle.getBoolean(KEY_REMOTE_INPUT_HISTORY, false));
Alex Hillsd9b04d92016-04-11 16:38:16 -04007656 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
7657 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007658 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
7659 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04007660 }
Shane Brennan5a871862017-03-11 13:14:17 -08007661 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
7662 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
7663 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007664 return message;
7665 }
7666 } catch (ClassCastException e) {
7667 return null;
7668 }
7669 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007670 }
7671 }
7672
7673 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04007674 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08007675 *
Robert Ly91c5ce32014-06-08 15:37:00 -07007676 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04007677 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07007678 * Notification notif = new Notification.Builder(mContext)
7679 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
7680 * .setContentText(subject)
7681 * .setSmallIcon(R.drawable.new_mail)
7682 * .setLargeIcon(aBitmap)
7683 * .setStyle(new Notification.InboxStyle()
7684 * .addLine(str1)
7685 * .addLine(str2)
7686 * .setContentTitle(&quot;&quot;)
7687 * .setSummaryText(&quot;+3 more&quot;))
7688 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04007689 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08007690 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04007691 * @see Notification#bigContentView
7692 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007693 public static class InboxStyle extends Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07007694
7695 /**
7696 * The number of lines of remote input history allowed until we start reducing lines.
7697 */
7698 private static final int NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION = 1;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007699 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
7700
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007701 public InboxStyle() {
7702 }
7703
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007704 /**
7705 * @deprecated use {@code InboxStyle()}.
7706 */
7707 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04007708 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007709 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04007710 }
7711
Chris Wrend6297db2012-05-03 16:20:13 -04007712 /**
7713 * Overrides ContentTitle in the big form of the template.
7714 * This defaults to the value passed to setContentTitle().
7715 */
7716 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007717 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04007718 return this;
7719 }
7720
7721 /**
7722 * Set the first line of text after the detail section in the big form of the template.
7723 */
7724 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007725 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04007726 return this;
7727 }
7728
Chris Wren0bd664d2012-08-01 13:56:56 -04007729 /**
7730 * Append a line to the digest section of the Inbox notification.
7731 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04007732 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007733 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04007734 return this;
7735 }
7736
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007737 /**
7738 * @hide
7739 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05007740 public ArrayList<CharSequence> getLines() {
7741 return mTexts;
7742 }
7743
7744 /**
7745 * @hide
7746 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007747 public void addExtras(Bundle extras) {
7748 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007749
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007750 CharSequence[] a = new CharSequence[mTexts.size()];
7751 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
7752 }
7753
Christoph Studer4600f9b2014-07-22 22:44:43 +02007754 /**
7755 * @hide
7756 */
7757 @Override
7758 protected void restoreFromExtras(Bundle extras) {
7759 super.restoreFromExtras(extras);
7760
7761 mTexts.clear();
7762 if (extras.containsKey(EXTRA_TEXT_LINES)) {
7763 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
7764 }
7765 }
7766
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007767 /**
7768 * @hide
7769 */
7770 public RemoteViews makeBigContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007771 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder).text(null);
Selim Cinek384804b2018-04-18 14:31:07 +08007772 TemplateBindResult result = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007773 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource(), p, result);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007774
Chris Wrend6297db2012-05-03 16:20:13 -04007775 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 -04007776 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04007777
Chris Wren4ed80d52012-05-17 09:30:03 -04007778 // Make sure all rows are gone in case we reuse a view.
7779 for (int rowId : rowIds) {
7780 contentView.setViewVisibility(rowId, View.GONE);
7781 }
7782
Daniel Sandler879c5e02012-04-17 16:46:51 -04007783 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07007784 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7785 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08007786 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07007787 int onlyViewId = 0;
7788 int maxRows = rowIds.length;
7789 if (mBuilder.mActions.size() > 0) {
7790 maxRows--;
7791 }
Selim Cinekbee4e072018-05-21 22:06:43 -07007792 CharSequence[] remoteInputHistory = mBuilder.mN.extras.getCharSequenceArray(
7793 EXTRA_REMOTE_INPUT_HISTORY);
7794 if (remoteInputHistory != null
7795 && remoteInputHistory.length > NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION) {
7796 // Let's remove some messages to make room for the remote input history.
7797 // 1 is always able to fit, but let's remove them if they are 2 or 3
7798 int numRemoteInputs = Math.min(remoteInputHistory.length,
7799 MAX_REMOTE_INPUT_HISTORY_LINES);
7800 int totalNumRows = mTexts.size() + numRemoteInputs
7801 - NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION;
7802 if (totalNumRows > maxRows) {
7803 int overflow = totalNumRows - maxRows;
7804 if (mTexts.size() > maxRows) {
7805 // Heuristic: if the Texts don't fit anyway, we'll rather drop the last
7806 // few messages, even with the remote input
7807 maxRows -= overflow;
7808 } else {
7809 // otherwise we drop the first messages
7810 i = overflow;
7811 }
7812 }
7813 }
Selim Cinek07c80172016-04-21 16:40:47 -07007814 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007815 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07007816 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007817 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07007818 contentView.setTextViewText(rowIds[i],
7819 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007820 mBuilder.setTextViewColorSecondary(contentView, rowIds[i], p);
Selim Cinek07c80172016-04-21 16:40:47 -07007821 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek384804b2018-04-18 14:31:07 +08007822 handleInboxImageMargin(contentView, rowIds[i], first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007823 result.getIconMarginEnd());
Selim Cinek07c80172016-04-21 16:40:47 -07007824 if (first) {
7825 onlyViewId = rowIds[i];
7826 } else {
7827 onlyViewId = 0;
7828 }
Selim Cinek247fa012016-02-18 09:50:48 -08007829 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007830 }
7831 i++;
7832 }
Selim Cinek07c80172016-04-21 16:40:47 -07007833 if (onlyViewId != 0) {
7834 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
7835 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7836 R.dimen.notification_text_margin_top);
7837 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
7838 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007839
Daniel Sandler879c5e02012-04-17 16:46:51 -04007840 return contentView;
7841 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007842
Julia Reynolds7217dc92018-03-07 12:12:09 -05007843 /**
7844 * @hide
7845 */
7846 @Override
7847 public boolean areNotificationsVisiblyDifferent(Style other) {
7848 if (other == null || getClass() != other.getClass()) {
7849 return true;
7850 }
7851 InboxStyle newS = (InboxStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04007852
7853 final ArrayList<CharSequence> myLines = getLines();
7854 final ArrayList<CharSequence> newLines = newS.getLines();
7855 final int n = myLines.size();
7856 if (n != newLines.size()) {
7857 return true;
7858 }
7859
7860 for (int i = 0; i < n; i++) {
7861 if (!Objects.equals(
7862 String.valueOf(myLines.get(i)),
7863 String.valueOf(newLines.get(i)))) {
7864 return true;
7865 }
7866 }
7867 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05007868 }
7869
Selim Cinek384804b2018-04-18 14:31:07 +08007870 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007871 int marginEndValue) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08007872 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08007873 if (first) {
7874 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
7875 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
7876 boolean hasProgress = max != 0 || ind;
Selim Cinek384804b2018-04-18 14:31:07 +08007877 if (!hasProgress) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08007878 endMargin = marginEndValue;
Selim Cinek247fa012016-02-18 09:50:48 -08007879 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007880 }
Selim Cinek1c72fa02018-04-23 18:00:54 +08007881 contentView.setViewLayoutMarginEnd(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08007882 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04007883 }
Dan Sandler842dd772014-05-15 09:36:47 -04007884
7885 /**
7886 * Notification style for media playback notifications.
7887 *
7888 * In the expanded form, {@link Notification#bigContentView}, up to 5
7889 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04007890 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04007891 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
7892 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
7893 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08007894 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04007895 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
7896 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01007897 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04007898 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08007899 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007900 * Notifications created with MediaStyle will have their category set to
7901 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
7902 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08007903 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07007904 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
7905 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04007906 * the System UI can identify this as a notification representing an active media session
7907 * and respond accordingly (by showing album artwork in the lockscreen, for example).
7908 *
Selim Cinek99104832017-01-25 14:47:33 -08007909 * <p>
7910 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
7911 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
7912 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
7913 * <p>
7914 *
Dan Sandler842dd772014-05-15 09:36:47 -04007915 * To use this style with your Notification, feed it to
7916 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7917 * <pre class="prettyprint">
7918 * Notification noti = new Notification.Builder()
7919 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01007920 * .setContentTitle(&quot;Track title&quot;)
7921 * .setContentText(&quot;Artist - Album&quot;)
7922 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007923 * .setStyle(<b>new Notification.MediaStyle()</b>
7924 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04007925 * .build();
7926 * </pre>
7927 *
7928 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08007929 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04007930 */
7931 public static class MediaStyle extends Style {
Gus Prevas9cc96602018-10-11 11:24:23 -04007932 // Changing max media buttons requires also changing templates
7933 // (notification_template_material_media and notification_template_material_big_media).
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007934 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04007935 static final int MAX_MEDIA_BUTTONS = 5;
Gus Prevas9cc96602018-10-11 11:24:23 -04007936 @IdRes private static final int[] MEDIA_BUTTON_IDS = {
7937 R.id.action0,
7938 R.id.action1,
7939 R.id.action2,
7940 R.id.action3,
7941 R.id.action4,
7942 };
Dan Sandler842dd772014-05-15 09:36:47 -04007943
7944 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07007945 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04007946
7947 public MediaStyle() {
7948 }
7949
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007950 /**
7951 * @deprecated use {@code MediaStyle()}.
7952 */
7953 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04007954 public MediaStyle(Builder builder) {
7955 setBuilder(builder);
7956 }
7957
7958 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007959 * 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 -04007960 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007961 *
7962 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04007963 */
7964 public MediaStyle setShowActionsInCompactView(int...actions) {
7965 mActionsToShowInCompact = actions;
7966 return this;
7967 }
7968
7969 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07007970 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
7971 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04007972 */
Jeff Browndba34ba2014-06-24 20:46:03 -07007973 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04007974 mToken = token;
7975 return this;
7976 }
7977
Christoph Studer4600f9b2014-07-22 22:44:43 +02007978 /**
7979 * @hide
7980 */
Dan Sandler842dd772014-05-15 09:36:47 -04007981 @Override
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007982 @UnsupportedAppUsage
Dan Sandler842dd772014-05-15 09:36:47 -04007983 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02007984 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007985 if (wip.category == null) {
7986 wip.category = Notification.CATEGORY_TRANSPORT;
7987 }
Dan Sandler842dd772014-05-15 09:36:47 -04007988 return wip;
7989 }
7990
Christoph Studer4600f9b2014-07-22 22:44:43 +02007991 /**
7992 * @hide
7993 */
7994 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007995 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007996 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007997 }
7998
7999 /**
8000 * @hide
8001 */
8002 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04008003 public RemoteViews makeBigContentView() {
8004 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02008005 }
8006
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008007 /**
8008 * @hide
8009 */
8010 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008011 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008012 RemoteViews expanded = makeMediaBigContentView();
8013 return expanded != null ? expanded : makeMediaContentView();
8014 }
8015
Dan Sandler842dd772014-05-15 09:36:47 -04008016 /** @hide */
8017 @Override
8018 public void addExtras(Bundle extras) {
8019 super.addExtras(extras);
8020
8021 if (mToken != null) {
8022 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
8023 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01008024 if (mActionsToShowInCompact != null) {
8025 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
8026 }
Dan Sandler842dd772014-05-15 09:36:47 -04008027 }
8028
Christoph Studer4600f9b2014-07-22 22:44:43 +02008029 /**
8030 * @hide
8031 */
8032 @Override
8033 protected void restoreFromExtras(Bundle extras) {
8034 super.restoreFromExtras(extras);
8035
8036 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
8037 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
8038 }
8039 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
8040 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
8041 }
8042 }
8043
Julia Reynolds7217dc92018-03-07 12:12:09 -05008044 /**
8045 * @hide
8046 */
8047 @Override
8048 public boolean areNotificationsVisiblyDifferent(Style other) {
8049 if (other == null || getClass() != other.getClass()) {
8050 return true;
8051 }
8052 // All fields to compare are on the Notification object
8053 return false;
8054 }
8055
Gus Prevas9cc96602018-10-11 11:24:23 -04008056 private void bindMediaActionButton(RemoteViews container, @IdRes int buttonId,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008057 Action action, StandardTemplateParams p) {
Dan Sandler842dd772014-05-15 09:36:47 -04008058 final boolean tombstone = (action.actionIntent == null);
Gus Prevas9cc96602018-10-11 11:24:23 -04008059 container.setViewVisibility(buttonId, View.VISIBLE);
8060 container.setImageViewIcon(buttonId, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07008061
8062 // If the action buttons should not be tinted, then just use the default
8063 // notification color. Otherwise, just use the passed-in color.
Gus Prevas9cc96602018-10-11 11:24:23 -04008064 Resources resources = mBuilder.mContext.getResources();
8065 Configuration currentConfig = resources.getConfiguration();
Lucas Dupinf03e7522018-06-25 16:21:13 -07008066 boolean inNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
8067 == Configuration.UI_MODE_NIGHT_YES;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008068 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized(p)
8069 ? getActionColor(p)
Lucas Dupina291d192018-06-07 13:59:42 -07008070 : ContrastColorUtil.resolveColor(mBuilder.mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07008071 Notification.COLOR_DEFAULT, inNightMode);
Anthony Chenad4d1582017-04-10 16:07:58 -07008072
Gus Prevas9cc96602018-10-11 11:24:23 -04008073 container.setDrawableTint(buttonId, false, tintColor,
Sunny Goyal5b153922017-09-21 21:00:36 -07008074 PorterDuff.Mode.SRC_ATOP);
Gus Prevas9cc96602018-10-11 11:24:23 -04008075
8076 final TypedArray typedArray = mBuilder.mContext.obtainStyledAttributes(
8077 new int[]{ android.R.attr.colorControlHighlight });
8078 int rippleAlpha = Color.alpha(typedArray.getColor(0, 0));
8079 typedArray.recycle();
8080 int rippleColor = Color.argb(rippleAlpha, Color.red(tintColor), Color.green(tintColor),
8081 Color.blue(tintColor));
8082 container.setRippleDrawableColor(buttonId, ColorStateList.valueOf(rippleColor));
8083
Dan Sandler842dd772014-05-15 09:36:47 -04008084 if (!tombstone) {
Gus Prevas9cc96602018-10-11 11:24:23 -04008085 container.setOnClickPendingIntent(buttonId, action.actionIntent);
Dan Sandler842dd772014-05-15 09:36:47 -04008086 }
Gus Prevas9cc96602018-10-11 11:24:23 -04008087 container.setContentDescription(buttonId, action.title);
Dan Sandler842dd772014-05-15 09:36:47 -04008088 }
8089
8090 private RemoteViews makeMediaContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008091 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).fillTextsFrom(
8092 mBuilder);
Dan Sandler842dd772014-05-15 09:36:47 -04008093 RemoteViews view = mBuilder.applyStandardTemplate(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008094 R.layout.notification_template_material_media, p,
Selim Cinek384804b2018-04-18 14:31:07 +08008095 null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04008096
8097 final int numActions = mBuilder.mActions.size();
Gus Prevas9cc96602018-10-11 11:24:23 -04008098 final int numActionsToShow = mActionsToShowInCompact == null
Dan Sandler842dd772014-05-15 09:36:47 -04008099 ? 0
8100 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Gus Prevas9cc96602018-10-11 11:24:23 -04008101 if (numActionsToShow > numActions) {
8102 throw new IllegalArgumentException(String.format(
8103 "setShowActionsInCompactView: action %d out of bounds (max %d)",
8104 numActions, numActions - 1));
8105 }
8106 for (int i = 0; i < MAX_MEDIA_BUTTONS_IN_COMPACT; i++) {
8107 if (i < numActionsToShow) {
Dan Sandler842dd772014-05-15 09:36:47 -04008108 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008109 bindMediaActionButton(view, MEDIA_BUTTON_IDS[i], action, p);
Gus Prevas9cc96602018-10-11 11:24:23 -04008110 } else {
8111 view.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04008112 }
8113 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08008114 handleImage(view);
8115 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008116 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07008117 if (mBuilder.mN.hasLargeIcon()) {
Selim Cinek384804b2018-04-18 14:31:07 +08008118 endMargin = R.dimen.notification_media_image_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08008119 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008120 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04008121 return view;
8122 }
8123
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008124 private int getActionColor(StandardTemplateParams p) {
8125 return mBuilder.isColorized(p) ? mBuilder.getPrimaryTextColor(p)
8126 : mBuilder.resolveContrastColor(p);
Selim Cinek99104832017-01-25 14:47:33 -08008127 }
8128
Dan Sandler842dd772014-05-15 09:36:47 -04008129 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008130 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008131 // Dont add an expanded view if there is no more content to be revealed
8132 int actionsInCompact = mActionsToShowInCompact == null
8133 ? 0
8134 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07008135 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008136 return null;
8137 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008138 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).fillTextsFrom(
8139 mBuilder);
Selim Cinek5bf069a2015-11-10 19:14:27 -05008140 RemoteViews big = mBuilder.applyStandardTemplate(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008141 R.layout.notification_template_material_big_media, p , null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04008142
Gus Prevas9cc96602018-10-11 11:24:23 -04008143 for (int i = 0; i < MAX_MEDIA_BUTTONS; i++) {
8144 if (i < actionCount) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008145 bindMediaActionButton(big, MEDIA_BUTTON_IDS[i], mBuilder.mActions.get(i), p);
Gus Prevas9cc96602018-10-11 11:24:23 -04008146 } else {
8147 big.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04008148 }
8149 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05008150 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04008151 return big;
8152 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008153
Selim Cinek5bf069a2015-11-10 19:14:27 -05008154 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07008155 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008156 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
8157 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008158 }
8159 }
8160
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008161 /**
8162 * @hide
8163 */
8164 @Override
8165 protected boolean hasProgress() {
8166 return false;
8167 }
Dan Sandler842dd772014-05-15 09:36:47 -04008168 }
Griff Hazen61a9e862014-05-22 16:05:19 -07008169
Selim Cinek593610c2016-02-16 18:42:57 -08008170 /**
8171 * Notification style for custom views that are decorated by the system
8172 *
8173 * <p>Instead of providing a notification that is completely custom, a developer can set this
8174 * style and still obtain system decorations like the notification header with the expand
8175 * affordance and actions.
8176 *
8177 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8178 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8179 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8180 * corresponding custom views to display.
8181 *
8182 * To use this style with your Notification, feed it to
8183 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8184 * <pre class="prettyprint">
8185 * Notification noti = new Notification.Builder()
8186 * .setSmallIcon(R.drawable.ic_stat_player)
8187 * .setLargeIcon(albumArtBitmap))
8188 * .setCustomContentView(contentView);
8189 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
8190 * .build();
8191 * </pre>
8192 */
8193 public static class DecoratedCustomViewStyle extends Style {
8194
8195 public DecoratedCustomViewStyle() {
8196 }
8197
Selim Cinek593610c2016-02-16 18:42:57 -08008198 /**
8199 * @hide
8200 */
8201 public boolean displayCustomViewInline() {
8202 return true;
8203 }
8204
8205 /**
8206 * @hide
8207 */
8208 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008209 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008210 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
8211 }
8212
8213 /**
8214 * @hide
8215 */
8216 @Override
8217 public RemoteViews makeBigContentView() {
8218 return makeDecoratedBigContentView();
8219 }
8220
8221 /**
8222 * @hide
8223 */
8224 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008225 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008226 return makeDecoratedHeadsUpContentView();
8227 }
8228
Selim Cinek593610c2016-02-16 18:42:57 -08008229 private RemoteViews makeDecoratedHeadsUpContentView() {
8230 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
8231 ? mBuilder.mN.contentView
8232 : mBuilder.mN.headsUpContentView;
8233 if (mBuilder.mActions.size() == 0) {
8234 return makeStandardTemplateWithCustomContent(headsUpContentView);
8235 }
Selim Cinek384804b2018-04-18 14:31:07 +08008236 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008237 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008238 mBuilder.getBigBaseLayoutResource(), result);
8239 buildIntoRemoteViewContent(remoteViews, headsUpContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008240 return remoteViews;
8241 }
8242
Selim Cinek593610c2016-02-16 18:42:57 -08008243 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
Selim Cinek384804b2018-04-18 14:31:07 +08008244 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008245 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08008246 mBuilder.getBaseLayoutResource(), result);
8247 buildIntoRemoteViewContent(remoteViews, customContent, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008248 return remoteViews;
8249 }
8250
Selim Cinek593610c2016-02-16 18:42:57 -08008251 private RemoteViews makeDecoratedBigContentView() {
8252 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
8253 ? mBuilder.mN.contentView
8254 : mBuilder.mN.bigContentView;
8255 if (mBuilder.mActions.size() == 0) {
8256 return makeStandardTemplateWithCustomContent(bigContentView);
8257 }
Selim Cinek384804b2018-04-18 14:31:07 +08008258 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008259 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008260 mBuilder.getBigBaseLayoutResource(), result);
8261 buildIntoRemoteViewContent(remoteViews, bigContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008262 return remoteViews;
8263 }
Selim Cinek247fa012016-02-18 09:50:48 -08008264
8265 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
Selim Cinek384804b2018-04-18 14:31:07 +08008266 RemoteViews customContent, TemplateBindResult result) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08008267 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008268 // Need to clone customContent before adding, because otherwise it can no longer be
8269 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008270 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07008271 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
8272 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Sunny Goyalc12d31c2018-11-12 16:29:18 -08008273 remoteViews.addFlags(RemoteViews.FLAG_REAPPLY_DISALLOWED);
Adrian Roos5081c0d2016-02-26 16:04:19 -08008274 }
Selim Cinek247fa012016-02-18 09:50:48 -08008275 // also update the end margin if there is an image
Selim Cinek384804b2018-04-18 14:31:07 +08008276 Resources resources = mBuilder.mContext.getResources();
8277 int endMargin = resources.getDimensionPixelSize(
Selim Cinek1c72fa02018-04-23 18:00:54 +08008278 R.dimen.notification_content_margin_end) + result.getIconMarginEnd();
Selim Cinek384804b2018-04-18 14:31:07 +08008279 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08008280 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05008281
8282 /**
8283 * @hide
8284 */
8285 @Override
8286 public boolean areNotificationsVisiblyDifferent(Style other) {
8287 if (other == null || getClass() != other.getClass()) {
8288 return true;
8289 }
8290 // Comparison done for all custom RemoteViews, independent of style
8291 return false;
8292 }
Selim Cinek593610c2016-02-16 18:42:57 -08008293 }
8294
Selim Cinek03eb3b72016-02-18 10:39:45 -08008295 /**
8296 * Notification style for media custom views that are decorated by the system
8297 *
8298 * <p>Instead of providing a media notification that is completely custom, a developer can set
8299 * this style and still obtain system decorations like the notification header with the expand
8300 * affordance and actions.
8301 *
8302 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8303 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8304 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8305 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08008306 * <p>
8307 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
8308 * notification by using {@link Notification.Builder#setColorized(boolean)}.
8309 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08008310 * To use this style with your Notification, feed it to
8311 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8312 * <pre class="prettyprint">
8313 * Notification noti = new Notification.Builder()
8314 * .setSmallIcon(R.drawable.ic_stat_player)
8315 * .setLargeIcon(albumArtBitmap))
8316 * .setCustomContentView(contentView);
8317 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
8318 * .setMediaSession(mySession))
8319 * .build();
8320 * </pre>
8321 *
8322 * @see android.app.Notification.DecoratedCustomViewStyle
8323 * @see android.app.Notification.MediaStyle
8324 */
8325 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
8326
8327 public DecoratedMediaCustomViewStyle() {
8328 }
8329
Selim Cinek03eb3b72016-02-18 10:39:45 -08008330 /**
8331 * @hide
8332 */
8333 public boolean displayCustomViewInline() {
8334 return true;
8335 }
8336
8337 /**
8338 * @hide
8339 */
8340 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008341 public RemoteViews makeContentView(boolean increasedHeight) {
8342 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008343 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8344 mBuilder.mN.contentView);
8345 }
8346
8347 /**
8348 * @hide
8349 */
8350 @Override
8351 public RemoteViews makeBigContentView() {
8352 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
8353 ? mBuilder.mN.bigContentView
8354 : mBuilder.mN.contentView;
8355 return makeBigContentViewWithCustomContent(customRemoteView);
8356 }
8357
8358 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
8359 RemoteViews remoteViews = super.makeBigContentView();
8360 if (remoteViews != null) {
8361 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
8362 customRemoteView);
8363 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08008364 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008365 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8366 customRemoteView);
8367 } else {
8368 return null;
8369 }
8370 }
8371
8372 /**
8373 * @hide
8374 */
8375 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008376 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08008377 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
8378 ? mBuilder.mN.headsUpContentView
8379 : mBuilder.mN.contentView;
8380 return makeBigContentViewWithCustomContent(customRemoteView);
8381 }
8382
Julia Reynolds7217dc92018-03-07 12:12:09 -05008383 /**
8384 * @hide
8385 */
8386 @Override
8387 public boolean areNotificationsVisiblyDifferent(Style other) {
8388 if (other == null || getClass() != other.getClass()) {
8389 return true;
8390 }
8391 // Comparison done for all custom RemoteViews, independent of style
8392 return false;
8393 }
8394
Selim Cinek03eb3b72016-02-18 10:39:45 -08008395 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
8396 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08008397 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008398 // Need to clone customContent before adding, because otherwise it can no longer be
8399 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008400 customContent = customContent.clone();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008401 customContent.overrideTextColors(mBuilder.getPrimaryTextColor(mBuilder.mParams));
Selim Cinekf91017e2016-03-14 12:25:09 -07008402 remoteViews.removeAllViews(id);
8403 remoteViews.addView(id, customContent);
Sunny Goyalc12d31c2018-11-12 16:29:18 -08008404 remoteViews.addFlags(RemoteViews.FLAG_REAPPLY_DISALLOWED);
Adrian Roos5081c0d2016-02-26 16:04:19 -08008405 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08008406 return remoteViews;
8407 }
8408 }
8409
Christoph Studer4600f9b2014-07-22 22:44:43 +02008410 // When adding a new Style subclass here, don't forget to update
8411 // Builder.getNotificationStyleClass.
8412
Griff Hazen61a9e862014-05-22 16:05:19 -07008413 /**
8414 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
8415 * metadata or change options on a notification builder.
8416 */
8417 public interface Extender {
8418 /**
8419 * Apply this extender to a notification builder.
8420 * @param builder the builder to be modified.
8421 * @return the build object for chaining.
8422 */
8423 public Builder extend(Builder builder);
8424 }
8425
8426 /**
8427 * Helper class to add wearable extensions to notifications.
8428 * <p class="note"> See
8429 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
8430 * for Android Wear</a> for more information on how to use this class.
8431 * <p>
8432 * To create a notification with wearable extensions:
8433 * <ol>
8434 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
8435 * properties.
8436 * <li>Create a {@link android.app.Notification.WearableExtender}.
8437 * <li>Set wearable-specific properties using the
8438 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
8439 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
8440 * notification.
8441 * <li>Post the notification to the notification system with the
8442 * {@code NotificationManager.notify(...)} methods.
8443 * </ol>
8444 *
8445 * <pre class="prettyprint">
8446 * Notification notif = new Notification.Builder(mContext)
8447 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
8448 * .setContentText(subject)
8449 * .setSmallIcon(R.drawable.new_mail)
8450 * .extend(new Notification.WearableExtender()
8451 * .setContentIcon(R.drawable.new_mail))
8452 * .build();
8453 * NotificationManager notificationManger =
8454 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
8455 * notificationManger.notify(0, notif);</pre>
8456 *
8457 * <p>Wearable extensions can be accessed on an existing notification by using the
8458 * {@code WearableExtender(Notification)} constructor,
8459 * and then using the {@code get} methods to access values.
8460 *
8461 * <pre class="prettyprint">
8462 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
8463 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07008464 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008465 */
8466 public static final class WearableExtender implements Extender {
8467 /**
8468 * Sentinel value for an action index that is unset.
8469 */
8470 public static final int UNSET_ACTION_INDEX = -1;
8471
8472 /**
8473 * Size value for use with {@link #setCustomSizePreset} to show this notification with
8474 * default sizing.
8475 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07008476 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07008477 * on their content.
Gus Prevasd7363752018-09-18 14:35:15 -04008478 *
8479 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008480 */
Gus Prevasd7363752018-09-18 14:35:15 -04008481 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008482 public static final int SIZE_DEFAULT = 0;
8483
8484 /**
8485 * Size value for use with {@link #setCustomSizePreset} to show this notification
8486 * with an extra small size.
8487 * <p>This value is only applicable for custom display notifications created using
8488 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008489 *
8490 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008491 */
Gus Prevasd7363752018-09-18 14:35:15 -04008492 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008493 public static final int SIZE_XSMALL = 1;
8494
8495 /**
8496 * Size value for use with {@link #setCustomSizePreset} to show this notification
8497 * with a small size.
8498 * <p>This value is only applicable for custom display notifications created using
8499 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008500 *
8501 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008502 */
Gus Prevasd7363752018-09-18 14:35:15 -04008503 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008504 public static final int SIZE_SMALL = 2;
8505
8506 /**
8507 * Size value for use with {@link #setCustomSizePreset} to show this notification
8508 * with a medium size.
8509 * <p>This value is only applicable for custom display notifications created using
8510 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008511 *
8512 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008513 */
Gus Prevasd7363752018-09-18 14:35:15 -04008514 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008515 public static final int SIZE_MEDIUM = 3;
8516
8517 /**
8518 * Size value for use with {@link #setCustomSizePreset} to show this notification
8519 * with a large size.
8520 * <p>This value is only applicable for custom display notifications created using
8521 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008522 *
8523 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008524 */
Gus Prevasd7363752018-09-18 14:35:15 -04008525 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008526 public static final int SIZE_LARGE = 4;
8527
Griff Hazend5f11f92014-05-27 15:40:09 -07008528 /**
8529 * Size value for use with {@link #setCustomSizePreset} to show this notification
8530 * full screen.
8531 * <p>This value is only applicable for custom display notifications created using
8532 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008533 *
8534 * @deprecated Display intents are no longer supported.
Griff Hazend5f11f92014-05-27 15:40:09 -07008535 */
Gus Prevasd7363752018-09-18 14:35:15 -04008536 @Deprecated
Griff Hazend5f11f92014-05-27 15:40:09 -07008537 public static final int SIZE_FULL_SCREEN = 5;
8538
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008539 /**
8540 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
8541 * short amount of time when this notification is displayed on the screen. This
8542 * is the default value.
Gus Prevasd7363752018-09-18 14:35:15 -04008543 *
8544 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008545 */
Gus Prevasd7363752018-09-18 14:35:15 -04008546 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008547 public static final int SCREEN_TIMEOUT_SHORT = 0;
8548
8549 /**
8550 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
8551 * for a longer amount of time when this notification is displayed on the screen.
Gus Prevasd7363752018-09-18 14:35:15 -04008552 *
8553 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008554 */
Gus Prevasd7363752018-09-18 14:35:15 -04008555 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008556 public static final int SCREEN_TIMEOUT_LONG = -1;
8557
Griff Hazen61a9e862014-05-22 16:05:19 -07008558 /** Notification extra which contains wearable extensions */
8559 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
8560
Pete Gastaf6781d2014-10-07 15:17:05 -04008561 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07008562 private static final String KEY_ACTIONS = "actions";
8563 private static final String KEY_FLAGS = "flags";
8564 private static final String KEY_DISPLAY_INTENT = "displayIntent";
8565 private static final String KEY_PAGES = "pages";
8566 private static final String KEY_BACKGROUND = "background";
8567 private static final String KEY_CONTENT_ICON = "contentIcon";
8568 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
8569 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
8570 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
8571 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
8572 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008573 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04008574 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008575 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07008576
8577 // Flags bitwise-ored to mFlags
8578 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
8579 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
8580 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
8581 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008582 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04008583 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04008584 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07008585
8586 // Default value for flags integer
8587 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
8588
8589 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
8590 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
8591
8592 private ArrayList<Action> mActions = new ArrayList<Action>();
8593 private int mFlags = DEFAULT_FLAGS;
8594 private PendingIntent mDisplayIntent;
8595 private ArrayList<Notification> mPages = new ArrayList<Notification>();
8596 private Bitmap mBackground;
8597 private int mContentIcon;
8598 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
8599 private int mContentActionIndex = UNSET_ACTION_INDEX;
8600 private int mCustomSizePreset = SIZE_DEFAULT;
8601 private int mCustomContentHeight;
8602 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008603 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04008604 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008605 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07008606
8607 /**
8608 * Create a {@link android.app.Notification.WearableExtender} with default
8609 * options.
8610 */
8611 public WearableExtender() {
8612 }
8613
8614 public WearableExtender(Notification notif) {
8615 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
8616 if (wearableBundle != null) {
8617 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
8618 if (actions != null) {
8619 mActions.addAll(actions);
8620 }
8621
8622 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
8623 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
8624
8625 Notification[] pages = getNotificationArrayFromBundle(
8626 wearableBundle, KEY_PAGES);
8627 if (pages != null) {
8628 Collections.addAll(mPages, pages);
8629 }
8630
8631 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
8632 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
8633 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
8634 DEFAULT_CONTENT_ICON_GRAVITY);
8635 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
8636 UNSET_ACTION_INDEX);
8637 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
8638 SIZE_DEFAULT);
8639 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
8640 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008641 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04008642 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008643 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07008644 }
8645 }
8646
8647 /**
8648 * Apply wearable extensions to a notification that is being built. This is typically
8649 * called by the {@link android.app.Notification.Builder#extend} method of
8650 * {@link android.app.Notification.Builder}.
8651 */
8652 @Override
8653 public Notification.Builder extend(Notification.Builder builder) {
8654 Bundle wearableBundle = new Bundle();
8655
8656 if (!mActions.isEmpty()) {
8657 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
8658 }
8659 if (mFlags != DEFAULT_FLAGS) {
8660 wearableBundle.putInt(KEY_FLAGS, mFlags);
8661 }
8662 if (mDisplayIntent != null) {
8663 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
8664 }
8665 if (!mPages.isEmpty()) {
8666 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
8667 new Notification[mPages.size()]));
8668 }
8669 if (mBackground != null) {
8670 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
8671 }
8672 if (mContentIcon != 0) {
8673 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
8674 }
8675 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
8676 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
8677 }
8678 if (mContentActionIndex != UNSET_ACTION_INDEX) {
8679 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
8680 mContentActionIndex);
8681 }
8682 if (mCustomSizePreset != SIZE_DEFAULT) {
8683 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
8684 }
8685 if (mCustomContentHeight != 0) {
8686 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
8687 }
8688 if (mGravity != DEFAULT_GRAVITY) {
8689 wearableBundle.putInt(KEY_GRAVITY, mGravity);
8690 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008691 if (mHintScreenTimeout != 0) {
8692 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
8693 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04008694 if (mDismissalId != null) {
8695 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
8696 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008697 if (mBridgeTag != null) {
8698 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
8699 }
Griff Hazen61a9e862014-05-22 16:05:19 -07008700
8701 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
8702 return builder;
8703 }
8704
8705 @Override
8706 public WearableExtender clone() {
8707 WearableExtender that = new WearableExtender();
8708 that.mActions = new ArrayList<Action>(this.mActions);
8709 that.mFlags = this.mFlags;
8710 that.mDisplayIntent = this.mDisplayIntent;
8711 that.mPages = new ArrayList<Notification>(this.mPages);
8712 that.mBackground = this.mBackground;
8713 that.mContentIcon = this.mContentIcon;
8714 that.mContentIconGravity = this.mContentIconGravity;
8715 that.mContentActionIndex = this.mContentActionIndex;
8716 that.mCustomSizePreset = this.mCustomSizePreset;
8717 that.mCustomContentHeight = this.mCustomContentHeight;
8718 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008719 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04008720 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008721 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07008722 return that;
8723 }
8724
8725 /**
8726 * Add a wearable action to this notification.
8727 *
8728 * <p>When wearable actions are added using this method, the set of actions that
8729 * show on a wearable device splits from devices that only show actions added
8730 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
8731 * of which actions display on different devices.
8732 *
8733 * @param action the action to add to this notification
8734 * @return this object for method chaining
8735 * @see android.app.Notification.Action
8736 */
8737 public WearableExtender addAction(Action action) {
8738 mActions.add(action);
8739 return this;
8740 }
8741
8742 /**
8743 * Adds wearable actions to this notification.
8744 *
8745 * <p>When wearable actions are added using this method, the set of actions that
8746 * show on a wearable device splits from devices that only show actions added
8747 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
8748 * of which actions display on different devices.
8749 *
8750 * @param actions the actions to add to this notification
8751 * @return this object for method chaining
8752 * @see android.app.Notification.Action
8753 */
8754 public WearableExtender addActions(List<Action> actions) {
8755 mActions.addAll(actions);
8756 return this;
8757 }
8758
8759 /**
8760 * Clear all wearable actions present on this builder.
8761 * @return this object for method chaining.
8762 * @see #addAction
8763 */
8764 public WearableExtender clearActions() {
8765 mActions.clear();
8766 return this;
8767 }
8768
8769 /**
8770 * Get the wearable actions present on this notification.
8771 */
8772 public List<Action> getActions() {
8773 return mActions;
8774 }
8775
8776 /**
8777 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07008778 * this notification. The {@link PendingIntent} provided should be for an activity.
8779 *
8780 * <pre class="prettyprint">
8781 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
8782 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
8783 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
8784 * Notification notif = new Notification.Builder(context)
8785 * .extend(new Notification.WearableExtender()
8786 * .setDisplayIntent(displayPendingIntent)
8787 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
8788 * .build();</pre>
8789 *
8790 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07008791 * should have an empty task affinity. It is also recommended to use the device
8792 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07008793 *
8794 * <p>Example AndroidManifest.xml entry:
8795 * <pre class="prettyprint">
8796 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
8797 * android:exported=&quot;true&quot;
8798 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07008799 * android:taskAffinity=&quot;&quot;
8800 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008801 *
8802 * @param intent the {@link PendingIntent} for an activity
8803 * @return this object for method chaining
8804 * @see android.app.Notification.WearableExtender#getDisplayIntent
Gus Prevasd7363752018-09-18 14:35:15 -04008805 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008806 */
Gus Prevasd7363752018-09-18 14:35:15 -04008807 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008808 public WearableExtender setDisplayIntent(PendingIntent intent) {
8809 mDisplayIntent = intent;
8810 return this;
8811 }
8812
8813 /**
8814 * Get the intent to launch inside of an activity view when displaying this
8815 * notification. This {@code PendingIntent} should be for an activity.
Gus Prevasd7363752018-09-18 14:35:15 -04008816 *
8817 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008818 */
Gus Prevasd7363752018-09-18 14:35:15 -04008819 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008820 public PendingIntent getDisplayIntent() {
8821 return mDisplayIntent;
8822 }
8823
8824 /**
8825 * Add an additional page of content to display with this notification. The current
8826 * notification forms the first page, and pages added using this function form
8827 * subsequent pages. This field can be used to separate a notification into multiple
8828 * sections.
8829 *
8830 * @param page the notification to add as another page
8831 * @return this object for method chaining
8832 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04008833 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008834 */
Gus Prevasd7363752018-09-18 14:35:15 -04008835 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008836 public WearableExtender addPage(Notification page) {
8837 mPages.add(page);
8838 return this;
8839 }
8840
8841 /**
8842 * Add additional pages of content to display with this notification. The current
8843 * notification forms the first page, and pages added using this function form
8844 * subsequent pages. This field can be used to separate a notification into multiple
8845 * sections.
8846 *
8847 * @param pages a list of notifications
8848 * @return this object for method chaining
8849 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04008850 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008851 */
Gus Prevasd7363752018-09-18 14:35:15 -04008852 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008853 public WearableExtender addPages(List<Notification> pages) {
8854 mPages.addAll(pages);
8855 return this;
8856 }
8857
8858 /**
8859 * Clear all additional pages present on this builder.
8860 * @return this object for method chaining.
8861 * @see #addPage
Gus Prevasd7363752018-09-18 14:35:15 -04008862 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008863 */
Gus Prevasd7363752018-09-18 14:35:15 -04008864 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008865 public WearableExtender clearPages() {
8866 mPages.clear();
8867 return this;
8868 }
8869
8870 /**
8871 * Get the array of additional pages of content for displaying this notification. The
8872 * current notification forms the first page, and elements within this array form
8873 * subsequent pages. This field can be used to separate a notification into multiple
8874 * sections.
8875 * @return the pages for this notification
Gus Prevasd7363752018-09-18 14:35:15 -04008876 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008877 */
Gus Prevasd7363752018-09-18 14:35:15 -04008878 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008879 public List<Notification> getPages() {
8880 return mPages;
8881 }
8882
8883 /**
8884 * Set a background image to be displayed behind the notification content.
8885 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8886 * will work with any notification style.
8887 *
8888 * @param background the background bitmap
8889 * @return this object for method chaining
8890 * @see android.app.Notification.WearableExtender#getBackground
Gus Prevasd7363752018-09-18 14:35:15 -04008891 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008892 */
Gus Prevasd7363752018-09-18 14:35:15 -04008893 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008894 public WearableExtender setBackground(Bitmap background) {
8895 mBackground = background;
8896 return this;
8897 }
8898
8899 /**
8900 * Get a background image to be displayed behind the notification content.
8901 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8902 * will work with any notification style.
8903 *
8904 * @return the background image
8905 * @see android.app.Notification.WearableExtender#setBackground
Gus Prevasd7363752018-09-18 14:35:15 -04008906 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008907 */
Gus Prevasd7363752018-09-18 14:35:15 -04008908 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008909 public Bitmap getBackground() {
8910 return mBackground;
8911 }
8912
8913 /**
8914 * Set an icon that goes with the content of this notification.
8915 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008916 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008917 public WearableExtender setContentIcon(int icon) {
8918 mContentIcon = icon;
8919 return this;
8920 }
8921
8922 /**
8923 * Get an icon that goes with the content of this notification.
8924 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008925 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008926 public int getContentIcon() {
8927 return mContentIcon;
8928 }
8929
8930 /**
8931 * Set the gravity that the content icon should have within the notification display.
8932 * Supported values include {@link android.view.Gravity#START} and
8933 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8934 * @see #setContentIcon
8935 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008936 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008937 public WearableExtender setContentIconGravity(int contentIconGravity) {
8938 mContentIconGravity = contentIconGravity;
8939 return this;
8940 }
8941
8942 /**
8943 * Get the gravity that the content icon should have within the notification display.
8944 * Supported values include {@link android.view.Gravity#START} and
8945 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8946 * @see #getContentIcon
8947 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008948 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008949 public int getContentIconGravity() {
8950 return mContentIconGravity;
8951 }
8952
8953 /**
Gus Prevasd7363752018-09-18 14:35:15 -04008954 * Set an action from this notification's actions as the primary action. If the action has a
8955 * {@link RemoteInput} associated with it, shortcuts to the options for that input are shown
8956 * directly on the notification.
Griff Hazen14f57992014-05-26 09:07:14 -07008957 *
Gus Prevasd7363752018-09-18 14:35:15 -04008958 * @param actionIndex The index of the primary action.
Griff Hazen14f57992014-05-26 09:07:14 -07008959 * If wearable actions were added to the main notification, this index
8960 * will apply to that list, otherwise it will apply to the regular
8961 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07008962 */
8963 public WearableExtender setContentAction(int actionIndex) {
8964 mContentActionIndex = actionIndex;
8965 return this;
8966 }
8967
8968 /**
Gus Prevasd7363752018-09-18 14:35:15 -04008969 * Get the index of the notification action, if any, that was specified as the primary
8970 * action.
Griff Hazen14f57992014-05-26 09:07:14 -07008971 *
8972 * <p>If wearable specific actions were added to the main notification, this index will
8973 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07008974 *
8975 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07008976 */
8977 public int getContentAction() {
8978 return mContentActionIndex;
8979 }
8980
8981 /**
8982 * Set the gravity that this notification should have within the available viewport space.
8983 * Supported values include {@link android.view.Gravity#TOP},
8984 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8985 * The default value is {@link android.view.Gravity#BOTTOM}.
8986 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008987 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008988 public WearableExtender setGravity(int gravity) {
8989 mGravity = gravity;
8990 return this;
8991 }
8992
8993 /**
8994 * Get the gravity that this notification should have within the available viewport space.
8995 * Supported values include {@link android.view.Gravity#TOP},
8996 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8997 * The default value is {@link android.view.Gravity#BOTTOM}.
8998 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008999 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009000 public int getGravity() {
9001 return mGravity;
9002 }
9003
9004 /**
9005 * Set the custom size preset for the display of this notification out of the available
9006 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
9007 * {@link #SIZE_LARGE}.
9008 * <p>Some custom size presets are only applicable for custom display notifications created
9009 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
9010 * documentation for the preset in question. See also
9011 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
9012 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009013 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009014 public WearableExtender setCustomSizePreset(int sizePreset) {
9015 mCustomSizePreset = sizePreset;
9016 return this;
9017 }
9018
9019 /**
9020 * Get the custom size preset for the display of this notification out of the available
9021 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
9022 * {@link #SIZE_LARGE}.
9023 * <p>Some custom size presets are only applicable for custom display notifications created
9024 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
9025 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
9026 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009027 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009028 public int getCustomSizePreset() {
9029 return mCustomSizePreset;
9030 }
9031
9032 /**
9033 * Set the custom height in pixels for the display of this notification's content.
9034 * <p>This option is only available for custom display notifications created
9035 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
9036 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
9037 * {@link #getCustomContentHeight}.
9038 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009039 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009040 public WearableExtender setCustomContentHeight(int height) {
9041 mCustomContentHeight = height;
9042 return this;
9043 }
9044
9045 /**
9046 * Get the custom height in pixels for the display of this notification's content.
9047 * <p>This option is only available for custom display notifications created
9048 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
9049 * {@link #setCustomContentHeight}.
9050 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009051 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009052 public int getCustomContentHeight() {
9053 return mCustomContentHeight;
9054 }
9055
9056 /**
9057 * Set whether the scrolling position for the contents of this notification should start
9058 * at the bottom of the contents instead of the top when the contents are too long to
9059 * display within the screen. Default is false (start scroll at the top).
9060 */
9061 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
9062 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
9063 return this;
9064 }
9065
9066 /**
9067 * Get whether the scrolling position for the contents of this notification should start
9068 * at the bottom of the contents instead of the top when the contents are too long to
9069 * display within the screen. Default is false (start scroll at the top).
9070 */
9071 public boolean getStartScrollBottom() {
9072 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
9073 }
9074
9075 /**
9076 * Set whether the content intent is available when the wearable device is not connected
9077 * to a companion device. The user can still trigger this intent when the wearable device
9078 * is offline, but a visual hint will indicate that the content intent may not be available.
9079 * Defaults to true.
9080 */
9081 public WearableExtender setContentIntentAvailableOffline(
9082 boolean contentIntentAvailableOffline) {
9083 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
9084 return this;
9085 }
9086
9087 /**
9088 * Get whether the content intent is available when the wearable device is not connected
9089 * to a companion device. The user can still trigger this intent when the wearable device
9090 * is offline, but a visual hint will indicate that the content intent may not be available.
9091 * Defaults to true.
9092 */
9093 public boolean getContentIntentAvailableOffline() {
9094 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
9095 }
9096
9097 /**
9098 * Set a hint that this notification's icon should not be displayed.
9099 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
9100 * @return this object for method chaining
9101 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009102 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009103 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
9104 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
9105 return this;
9106 }
9107
9108 /**
9109 * Get a hint that this notification's icon should not be displayed.
9110 * @return {@code true} if this icon should not be displayed, false otherwise.
9111 * The default value is {@code false} if this was never set.
9112 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009113 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009114 public boolean getHintHideIcon() {
9115 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
9116 }
9117
9118 /**
9119 * Set a visual hint that only the background image of this notification should be
9120 * displayed, and other semantic content should be hidden. This hint is only applicable
9121 * to sub-pages added using {@link #addPage}.
9122 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009123 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009124 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
9125 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
9126 return this;
9127 }
9128
9129 /**
9130 * Get a visual hint that only the background image of this notification should be
9131 * displayed, and other semantic content should be hidden. This hint is only applicable
9132 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
9133 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009134 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009135 public boolean getHintShowBackgroundOnly() {
9136 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
9137 }
9138
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009139 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009140 * Set a hint that this notification's background should not be clipped if possible,
9141 * and should instead be resized to fully display on the screen, retaining the aspect
9142 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009143 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
9144 * @return this object for method chaining
9145 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009146 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009147 public WearableExtender setHintAvoidBackgroundClipping(
9148 boolean hintAvoidBackgroundClipping) {
9149 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
9150 return this;
9151 }
9152
9153 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009154 * Get a hint that this notification's background should not be clipped if possible,
9155 * and should instead be resized to fully display on the screen, retaining the aspect
9156 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009157 * @return {@code true} if it's ok if the background is clipped on the screen, false
9158 * otherwise. The default value is {@code false} if this was never set.
9159 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009160 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009161 public boolean getHintAvoidBackgroundClipping() {
9162 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
9163 }
9164
9165 /**
9166 * Set a hint that the screen should remain on for at least this duration when
9167 * this notification is displayed on the screen.
9168 * @param timeout The requested screen timeout in milliseconds. Can also be either
9169 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9170 * @return this object for method chaining
9171 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009172 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009173 public WearableExtender setHintScreenTimeout(int timeout) {
9174 mHintScreenTimeout = timeout;
9175 return this;
9176 }
9177
9178 /**
9179 * Get the duration, in milliseconds, that the screen should remain on for
9180 * when this notification is displayed.
9181 * @return the duration in milliseconds if > 0, or either one of the sentinel values
9182 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9183 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009184 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009185 public int getHintScreenTimeout() {
9186 return mHintScreenTimeout;
9187 }
9188
Alex Hills9ab3a232016-04-05 14:54:56 -04009189 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04009190 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
9191 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9192 * qr codes, as well as other simple black-and-white tickets.
9193 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
9194 * @return this object for method chaining
Gus Prevasd7363752018-09-18 14:35:15 -04009195 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009196 */
Gus Prevasd7363752018-09-18 14:35:15 -04009197 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009198 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
9199 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
9200 return this;
9201 }
9202
9203 /**
9204 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
9205 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9206 * qr codes, as well as other simple black-and-white tickets.
9207 * @return {@code true} if it should be displayed in ambient, false otherwise
9208 * otherwise. The default value is {@code false} if this was never set.
Gus Prevasd7363752018-09-18 14:35:15 -04009209 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009210 */
Gus Prevasd7363752018-09-18 14:35:15 -04009211 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009212 public boolean getHintAmbientBigPicture() {
9213 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
9214 }
9215
9216 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04009217 * Set a hint that this notification's content intent will launch an {@link Activity}
9218 * directly, telling the platform that it can generate the appropriate transitions.
9219 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
9220 * an activity and transitions should be generated, false otherwise.
9221 * @return this object for method chaining
9222 */
9223 public WearableExtender setHintContentIntentLaunchesActivity(
9224 boolean hintContentIntentLaunchesActivity) {
9225 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
9226 return this;
9227 }
9228
9229 /**
9230 * Get a hint that this notification's content intent will launch an {@link Activity}
9231 * directly, telling the platform that it can generate the appropriate transitions
9232 * @return {@code true} if the content intent will launch an activity and transitions should
9233 * be generated, false otherwise. The default value is {@code false} if this was never set.
9234 */
9235 public boolean getHintContentIntentLaunchesActivity() {
9236 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
9237 }
9238
Nadia Benbernou948627e2016-04-14 14:41:08 -04009239 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009240 * Sets the dismissal id for this notification. If a notification is posted with a
9241 * dismissal id, then when that notification is canceled, notifications on other wearables
9242 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04009243 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009244 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04009245 * @param dismissalId the dismissal id of the notification.
9246 * @return this object for method chaining
9247 */
9248 public WearableExtender setDismissalId(String dismissalId) {
9249 mDismissalId = dismissalId;
9250 return this;
9251 }
9252
9253 /**
9254 * Returns the dismissal id of the notification.
9255 * @return the dismissal id of the notification or null if it has not been set.
9256 */
9257 public String getDismissalId() {
9258 return mDismissalId;
9259 }
9260
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009261 /**
9262 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
9263 * posted from a phone to provide finer-grained control on what notifications are bridged
9264 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
9265 * Features to Notifications</a> for more information.
9266 * @param bridgeTag the bridge tag of the notification.
9267 * @return this object for method chaining
9268 */
9269 public WearableExtender setBridgeTag(String bridgeTag) {
9270 mBridgeTag = bridgeTag;
9271 return this;
9272 }
9273
9274 /**
9275 * Returns the bridge tag of the notification.
9276 * @return the bridge tag or null if not present.
9277 */
9278 public String getBridgeTag() {
9279 return mBridgeTag;
9280 }
9281
Griff Hazen61a9e862014-05-22 16:05:19 -07009282 private void setFlag(int mask, boolean value) {
9283 if (value) {
9284 mFlags |= mask;
9285 } else {
9286 mFlags &= ~mask;
9287 }
9288 }
9289 }
9290
9291 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009292 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
9293 * with car extensions:
9294 *
9295 * <ol>
9296 * <li>Create an {@link Notification.Builder}, setting any desired
9297 * properties.
9298 * <li>Create a {@link CarExtender}.
9299 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
9300 * {@link CarExtender}.
9301 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9302 * to apply the extensions to a notification.
9303 * </ol>
9304 *
9305 * <pre class="prettyprint">
9306 * Notification notification = new Notification.Builder(context)
9307 * ...
9308 * .extend(new CarExtender()
9309 * .set*(...))
9310 * .build();
9311 * </pre>
9312 *
9313 * <p>Car extensions can be accessed on an existing notification by using the
9314 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
9315 * to access values.
9316 */
9317 public static final class CarExtender implements Extender {
9318 private static final String TAG = "CarExtender";
9319
9320 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
9321 private static final String EXTRA_LARGE_ICON = "large_icon";
9322 private static final String EXTRA_CONVERSATION = "car_conversation";
9323 private static final String EXTRA_COLOR = "app_color";
9324
9325 private Bitmap mLargeIcon;
9326 private UnreadConversation mUnreadConversation;
9327 private int mColor = Notification.COLOR_DEFAULT;
9328
9329 /**
9330 * Create a {@link CarExtender} with default options.
9331 */
9332 public CarExtender() {
9333 }
9334
9335 /**
9336 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
9337 *
9338 * @param notif The notification from which to copy options.
9339 */
9340 public CarExtender(Notification notif) {
9341 Bundle carBundle = notif.extras == null ?
9342 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
9343 if (carBundle != null) {
9344 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
9345 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
9346
9347 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
9348 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
9349 }
9350 }
9351
9352 /**
9353 * Apply car extensions to a notification that is being built. This is typically called by
9354 * the {@link Notification.Builder#extend(Notification.Extender)}
9355 * method of {@link Notification.Builder}.
9356 */
9357 @Override
9358 public Notification.Builder extend(Notification.Builder builder) {
9359 Bundle carExtensions = new Bundle();
9360
9361 if (mLargeIcon != null) {
9362 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
9363 }
9364 if (mColor != Notification.COLOR_DEFAULT) {
9365 carExtensions.putInt(EXTRA_COLOR, mColor);
9366 }
9367
9368 if (mUnreadConversation != null) {
9369 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
9370 carExtensions.putBundle(EXTRA_CONVERSATION, b);
9371 }
9372
9373 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
9374 return builder;
9375 }
9376
9377 /**
9378 * Sets the accent color to use when Android Auto presents the notification.
9379 *
9380 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
9381 * to accent the displayed notification. However, not all colors are acceptable in an
9382 * automotive setting. This method can be used to override the color provided in the
9383 * notification in such a situation.
9384 */
Tor Norbye80756e32015-03-02 09:39:27 -08009385 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009386 mColor = color;
9387 return this;
9388 }
9389
9390 /**
9391 * Gets the accent color.
9392 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04009393 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009394 */
Tor Norbye80756e32015-03-02 09:39:27 -08009395 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009396 public int getColor() {
9397 return mColor;
9398 }
9399
9400 /**
9401 * Sets the large icon of the car notification.
9402 *
9403 * If no large icon is set in the extender, Android Auto will display the icon
9404 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
9405 *
9406 * @param largeIcon The large icon to use in the car notification.
9407 * @return This object for method chaining.
9408 */
9409 public CarExtender setLargeIcon(Bitmap largeIcon) {
9410 mLargeIcon = largeIcon;
9411 return this;
9412 }
9413
9414 /**
9415 * Gets the large icon used in this car notification, or null if no icon has been set.
9416 *
9417 * @return The large icon for the car notification.
9418 * @see CarExtender#setLargeIcon
9419 */
9420 public Bitmap getLargeIcon() {
9421 return mLargeIcon;
9422 }
9423
9424 /**
9425 * Sets the unread conversation in a message notification.
9426 *
9427 * @param unreadConversation The unread part of the conversation this notification conveys.
9428 * @return This object for method chaining.
9429 */
9430 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
9431 mUnreadConversation = unreadConversation;
9432 return this;
9433 }
9434
9435 /**
9436 * Returns the unread conversation conveyed by this notification.
9437 * @see #setUnreadConversation(UnreadConversation)
9438 */
9439 public UnreadConversation getUnreadConversation() {
9440 return mUnreadConversation;
9441 }
9442
9443 /**
9444 * A class which holds the unread messages from a conversation.
9445 */
9446 public static class UnreadConversation {
9447 private static final String KEY_AUTHOR = "author";
9448 private static final String KEY_TEXT = "text";
9449 private static final String KEY_MESSAGES = "messages";
9450 private static final String KEY_REMOTE_INPUT = "remote_input";
9451 private static final String KEY_ON_REPLY = "on_reply";
9452 private static final String KEY_ON_READ = "on_read";
9453 private static final String KEY_PARTICIPANTS = "participants";
9454 private static final String KEY_TIMESTAMP = "timestamp";
9455
9456 private final String[] mMessages;
9457 private final RemoteInput mRemoteInput;
9458 private final PendingIntent mReplyPendingIntent;
9459 private final PendingIntent mReadPendingIntent;
9460 private final String[] mParticipants;
9461 private final long mLatestTimestamp;
9462
9463 UnreadConversation(String[] messages, RemoteInput remoteInput,
9464 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
9465 String[] participants, long latestTimestamp) {
9466 mMessages = messages;
9467 mRemoteInput = remoteInput;
9468 mReadPendingIntent = readPendingIntent;
9469 mReplyPendingIntent = replyPendingIntent;
9470 mParticipants = participants;
9471 mLatestTimestamp = latestTimestamp;
9472 }
9473
9474 /**
9475 * Gets the list of messages conveyed by this notification.
9476 */
9477 public String[] getMessages() {
9478 return mMessages;
9479 }
9480
9481 /**
9482 * Gets the remote input that will be used to convey the response to a message list, or
9483 * null if no such remote input exists.
9484 */
9485 public RemoteInput getRemoteInput() {
9486 return mRemoteInput;
9487 }
9488
9489 /**
9490 * Gets the pending intent that will be triggered when the user replies to this
9491 * notification.
9492 */
9493 public PendingIntent getReplyPendingIntent() {
9494 return mReplyPendingIntent;
9495 }
9496
9497 /**
9498 * Gets the pending intent that Android Auto will send after it reads aloud all messages
9499 * in this object's message list.
9500 */
9501 public PendingIntent getReadPendingIntent() {
9502 return mReadPendingIntent;
9503 }
9504
9505 /**
9506 * Gets the participants in the conversation.
9507 */
9508 public String[] getParticipants() {
9509 return mParticipants;
9510 }
9511
9512 /**
9513 * Gets the firs participant in the conversation.
9514 */
9515 public String getParticipant() {
9516 return mParticipants.length > 0 ? mParticipants[0] : null;
9517 }
9518
9519 /**
9520 * Gets the timestamp of the conversation.
9521 */
9522 public long getLatestTimestamp() {
9523 return mLatestTimestamp;
9524 }
9525
9526 Bundle getBundleForUnreadConversation() {
9527 Bundle b = new Bundle();
9528 String author = null;
9529 if (mParticipants != null && mParticipants.length > 1) {
9530 author = mParticipants[0];
9531 }
9532 Parcelable[] messages = new Parcelable[mMessages.length];
9533 for (int i = 0; i < messages.length; i++) {
9534 Bundle m = new Bundle();
9535 m.putString(KEY_TEXT, mMessages[i]);
9536 m.putString(KEY_AUTHOR, author);
9537 messages[i] = m;
9538 }
9539 b.putParcelableArray(KEY_MESSAGES, messages);
9540 if (mRemoteInput != null) {
9541 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
9542 }
9543 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
9544 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
9545 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
9546 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
9547 return b;
9548 }
9549
9550 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
9551 if (b == null) {
9552 return null;
9553 }
9554 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
9555 String[] messages = null;
9556 if (parcelableMessages != null) {
9557 String[] tmp = new String[parcelableMessages.length];
9558 boolean success = true;
9559 for (int i = 0; i < tmp.length; i++) {
9560 if (!(parcelableMessages[i] instanceof Bundle)) {
9561 success = false;
9562 break;
9563 }
9564 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
9565 if (tmp[i] == null) {
9566 success = false;
9567 break;
9568 }
9569 }
9570 if (success) {
9571 messages = tmp;
9572 } else {
9573 return null;
9574 }
9575 }
9576
9577 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
9578 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
9579
9580 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
9581
9582 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
9583 if (participants == null || participants.length != 1) {
9584 return null;
9585 }
9586
9587 return new UnreadConversation(messages,
9588 remoteInput,
9589 onReply,
9590 onRead,
9591 participants, b.getLong(KEY_TIMESTAMP));
9592 }
9593 };
9594
9595 /**
9596 * Builder class for {@link CarExtender.UnreadConversation} objects.
9597 */
9598 public static class Builder {
9599 private final List<String> mMessages = new ArrayList<String>();
9600 private final String mParticipant;
9601 private RemoteInput mRemoteInput;
9602 private PendingIntent mReadPendingIntent;
9603 private PendingIntent mReplyPendingIntent;
9604 private long mLatestTimestamp;
9605
9606 /**
9607 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
9608 *
9609 * @param name The name of the other participant in the conversation.
9610 */
9611 public Builder(String name) {
9612 mParticipant = name;
9613 }
9614
9615 /**
9616 * Appends a new unread message to the list of messages for this conversation.
9617 *
9618 * The messages should be added from oldest to newest.
9619 *
9620 * @param message The text of the new unread message.
9621 * @return This object for method chaining.
9622 */
9623 public Builder addMessage(String message) {
9624 mMessages.add(message);
9625 return this;
9626 }
9627
9628 /**
9629 * Sets the pending intent and remote input which will convey the reply to this
9630 * notification.
9631 *
9632 * @param pendingIntent The pending intent which will be triggered on a reply.
9633 * @param remoteInput The remote input parcelable which will carry the reply.
9634 * @return This object for method chaining.
9635 *
9636 * @see CarExtender.UnreadConversation#getRemoteInput
9637 * @see CarExtender.UnreadConversation#getReplyPendingIntent
9638 */
9639 public Builder setReplyAction(
9640 PendingIntent pendingIntent, RemoteInput remoteInput) {
9641 mRemoteInput = remoteInput;
9642 mReplyPendingIntent = pendingIntent;
9643
9644 return this;
9645 }
9646
9647 /**
9648 * Sets the pending intent that will be sent once the messages in this notification
9649 * are read.
9650 *
9651 * @param pendingIntent The pending intent to use.
9652 * @return This object for method chaining.
9653 */
9654 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
9655 mReadPendingIntent = pendingIntent;
9656 return this;
9657 }
9658
9659 /**
9660 * Sets the timestamp of the most recent message in an unread conversation.
9661 *
9662 * If a messaging notification has been posted by your application and has not
9663 * yet been cancelled, posting a later notification with the same id and tag
9664 * but without a newer timestamp may result in Android Auto not displaying a
9665 * heads up notification for the later notification.
9666 *
9667 * @param timestamp The timestamp of the most recent message in the conversation.
9668 * @return This object for method chaining.
9669 */
9670 public Builder setLatestTimestamp(long timestamp) {
9671 mLatestTimestamp = timestamp;
9672 return this;
9673 }
9674
9675 /**
9676 * Builds a new unread conversation object.
9677 *
9678 * @return The new unread conversation object.
9679 */
9680 public UnreadConversation build() {
9681 String[] messages = mMessages.toArray(new String[mMessages.size()]);
9682 String[] participants = { mParticipant };
9683 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
9684 mReadPendingIntent, participants, mLatestTimestamp);
9685 }
9686 }
9687 }
9688
9689 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009690 * <p>Helper class to add Android TV extensions to notifications. To create a notification
9691 * with a TV extension:
9692 *
9693 * <ol>
9694 * <li>Create an {@link Notification.Builder}, setting any desired properties.
9695 * <li>Create a {@link TvExtender}.
9696 * <li>Set TV-specific properties using the {@code set} methods of
9697 * {@link TvExtender}.
9698 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9699 * to apply the extension to a notification.
9700 * </ol>
9701 *
9702 * <pre class="prettyprint">
9703 * Notification notification = new Notification.Builder(context)
9704 * ...
9705 * .extend(new TvExtender()
9706 * .set*(...))
9707 * .build();
9708 * </pre>
9709 *
9710 * <p>TV extensions can be accessed on an existing notification by using the
9711 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
9712 * to access values.
9713 *
9714 * @hide
9715 */
9716 @SystemApi
9717 public static final class TvExtender implements Extender {
9718 private static final String TAG = "TvExtender";
9719
9720 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
9721 private static final String EXTRA_FLAGS = "flags";
9722 private static final String EXTRA_CONTENT_INTENT = "content_intent";
9723 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009724 private static final String EXTRA_CHANNEL_ID = "channel_id";
Rhiannon Malia1a083932018-01-24 15:02:30 -08009725 private static final String EXTRA_SUPPRESS_SHOW_OVER_APPS = "suppressShowOverApps";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009726
9727 // Flags bitwise-ored to mFlags
9728 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
9729
9730 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009731 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009732 private PendingIntent mContentIntent;
9733 private PendingIntent mDeleteIntent;
Rhiannon Malia1a083932018-01-24 15:02:30 -08009734 private boolean mSuppressShowOverApps;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009735
9736 /**
9737 * Create a {@link TvExtender} with default options.
9738 */
9739 public TvExtender() {
9740 mFlags = FLAG_AVAILABLE_ON_TV;
9741 }
9742
9743 /**
9744 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
9745 *
9746 * @param notif The notification from which to copy options.
9747 */
9748 public TvExtender(Notification notif) {
9749 Bundle bundle = notif.extras == null ?
9750 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
9751 if (bundle != null) {
9752 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009753 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Rhiannon Malia1a083932018-01-24 15:02:30 -08009754 mSuppressShowOverApps = bundle.getBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009755 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
9756 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
9757 }
9758 }
9759
9760 /**
9761 * Apply a TV extension to a notification that is being built. This is typically called by
9762 * the {@link Notification.Builder#extend(Notification.Extender)}
9763 * method of {@link Notification.Builder}.
9764 */
9765 @Override
9766 public Notification.Builder extend(Notification.Builder builder) {
9767 Bundle bundle = new Bundle();
9768
9769 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009770 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Rhiannon Malia1a083932018-01-24 15:02:30 -08009771 bundle.putBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS, mSuppressShowOverApps);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009772 if (mContentIntent != null) {
9773 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
9774 }
9775
9776 if (mDeleteIntent != null) {
9777 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
9778 }
9779
9780 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
9781 return builder;
9782 }
9783
9784 /**
9785 * Returns true if this notification should be shown on TV. This method return true
9786 * if the notification was extended with a TvExtender.
9787 */
9788 public boolean isAvailableOnTv() {
9789 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
9790 }
9791
9792 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009793 * Specifies the channel the notification should be delivered on when shown on TV.
9794 * It can be different from the channel that the notification is delivered to when
9795 * posting on a non-TV device.
9796 */
9797 public TvExtender setChannel(String channelId) {
9798 mChannelId = channelId;
9799 return this;
9800 }
9801
9802 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04009803 * Specifies the channel the notification should be delivered on when shown on TV.
9804 * It can be different from the channel that the notification is delivered to when
9805 * posting on a non-TV device.
9806 */
9807 public TvExtender setChannelId(String channelId) {
9808 mChannelId = channelId;
9809 return this;
9810 }
9811
Jeff Sharkey000ce802017-04-29 13:13:27 -06009812 /** @removed */
9813 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009814 public String getChannel() {
9815 return mChannelId;
9816 }
9817
9818 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04009819 * Returns the id of the channel this notification posts to on TV.
9820 */
9821 public String getChannelId() {
9822 return mChannelId;
9823 }
9824
9825 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009826 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
9827 * If provided, it is used instead of the content intent specified
9828 * at the level of Notification.
9829 */
9830 public TvExtender setContentIntent(PendingIntent intent) {
9831 mContentIntent = intent;
9832 return this;
9833 }
9834
9835 /**
9836 * Returns the TV-specific content intent. If this method returns null, the
9837 * main content intent on the notification should be used.
9838 *
9839 * @see {@link Notification#contentIntent}
9840 */
9841 public PendingIntent getContentIntent() {
9842 return mContentIntent;
9843 }
9844
9845 /**
9846 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
9847 * by the user on TV. If provided, it is used instead of the delete intent specified
9848 * at the level of Notification.
9849 */
9850 public TvExtender setDeleteIntent(PendingIntent intent) {
9851 mDeleteIntent = intent;
9852 return this;
9853 }
9854
9855 /**
9856 * Returns the TV-specific delete intent. If this method returns null, the
9857 * main delete intent on the notification should be used.
9858 *
9859 * @see {@link Notification#deleteIntent}
9860 */
9861 public PendingIntent getDeleteIntent() {
9862 return mDeleteIntent;
9863 }
Rhiannon Malia1a083932018-01-24 15:02:30 -08009864
9865 /**
9866 * Specifies whether this notification should suppress showing a message over top of apps
9867 * outside of the launcher.
9868 */
9869 public TvExtender setSuppressShowOverApps(boolean suppress) {
9870 mSuppressShowOverApps = suppress;
9871 return this;
9872 }
9873
9874 /**
9875 * Returns true if this notification should not show messages over top of apps
9876 * outside of the launcher.
9877 */
9878 public boolean getSuppressShowOverApps() {
9879 return mSuppressShowOverApps;
9880 }
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009881 }
9882
9883 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07009884 * Get an array of Notification objects from a parcelable array bundle field.
9885 * Update the bundle to have a typed array so fetches in the future don't need
9886 * to do an array copy.
9887 */
9888 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
9889 Parcelable[] array = bundle.getParcelableArray(key);
9890 if (array instanceof Notification[] || array == null) {
9891 return (Notification[]) array;
9892 }
9893 Notification[] typedArray = Arrays.copyOf(array, array.length,
9894 Notification[].class);
9895 bundle.putParcelableArray(key, typedArray);
9896 return typedArray;
9897 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02009898
9899 private static class BuilderRemoteViews extends RemoteViews {
9900 public BuilderRemoteViews(Parcel parcel) {
9901 super(parcel);
9902 }
9903
Kenny Guy77320062014-08-27 21:37:15 +01009904 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
9905 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02009906 }
9907
9908 @Override
9909 public BuilderRemoteViews clone() {
9910 Parcel p = Parcel.obtain();
9911 writeToParcel(p, 0);
9912 p.setDataPosition(0);
9913 BuilderRemoteViews brv = new BuilderRemoteViews(p);
9914 p.recycle();
9915 return brv;
9916 }
9917 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08009918
Selim Cinek384804b2018-04-18 14:31:07 +08009919 /**
9920 * A result object where information about the template that was created is saved.
9921 */
9922 private static class TemplateBindResult {
Selim Cinek1c72fa02018-04-23 18:00:54 +08009923 int mIconMarginEnd;
Selim Cinek6e41b0e2018-12-06 18:10:20 -08009924 boolean mRightIconContainerVisible;
Selim Cinek384804b2018-04-18 14:31:07 +08009925
9926 /**
Selim Cinek1c72fa02018-04-23 18:00:54 +08009927 * Get the margin end that needs to be added to any fields that may overlap
Selim Cinek384804b2018-04-18 14:31:07 +08009928 * with the right actions.
9929 */
Selim Cinek1c72fa02018-04-23 18:00:54 +08009930 public int getIconMarginEnd() {
9931 return mIconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +08009932 }
9933
Selim Cinek6e41b0e2018-12-06 18:10:20 -08009934 /**
9935 * Is the icon container visible on the right size because of the reply button or the
9936 * right icon.
9937 */
9938 public boolean isRightIconContainerVisible() {
9939 return mRightIconContainerVisible;
9940 }
9941
Selim Cinek1c72fa02018-04-23 18:00:54 +08009942 public void setIconMarginEnd(int iconMarginEnd) {
9943 this.mIconMarginEnd = iconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +08009944 }
Selim Cinek6e41b0e2018-12-06 18:10:20 -08009945
9946 public void setRightIconContainerVisible(boolean iconContainerVisible) {
9947 mRightIconContainerVisible = iconContainerVisible;
9948 }
Selim Cinek384804b2018-04-18 14:31:07 +08009949 }
9950
Adrian Roos70d7aa32017-01-11 15:39:06 -08009951 private static class StandardTemplateParams {
9952 boolean hasProgress = true;
9953 boolean ambient = false;
9954 CharSequence title;
9955 CharSequence text;
Selim Cinekafeed292017-12-12 17:32:44 -08009956 CharSequence headerTextSecondary;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08009957 CharSequence summaryText;
Selim Cinekbee4e072018-05-21 22:06:43 -07009958 int maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek88188f22017-09-19 16:46:56 -07009959 boolean hideLargeIcon;
Selim Cinek384804b2018-04-18 14:31:07 +08009960 boolean hideReplyIcon;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08009961 boolean allowColorization = true;
9962 boolean forceDefaultColor = false;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009963
9964 final StandardTemplateParams reset() {
9965 hasProgress = true;
9966 ambient = false;
9967 title = null;
9968 text = null;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08009969 summaryText = null;
Selim Cinekafeed292017-12-12 17:32:44 -08009970 headerTextSecondary = null;
Selim Cinekbee4e072018-05-21 22:06:43 -07009971 maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08009972 allowColorization = true;
9973 forceDefaultColor = false;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009974 return this;
9975 }
9976
9977 final StandardTemplateParams hasProgress(boolean hasProgress) {
9978 this.hasProgress = hasProgress;
9979 return this;
9980 }
9981
9982 final StandardTemplateParams title(CharSequence title) {
9983 this.title = title;
9984 return this;
9985 }
9986
9987 final StandardTemplateParams text(CharSequence text) {
9988 this.text = text;
9989 return this;
9990 }
9991
Selim Cinek10bbc0e2018-11-21 15:14:10 -08009992 final StandardTemplateParams summaryText(CharSequence text) {
9993 this.summaryText = text;
9994 return this;
9995 }
9996
Selim Cinekafeed292017-12-12 17:32:44 -08009997 final StandardTemplateParams headerTextSecondary(CharSequence text) {
9998 this.headerTextSecondary = text;
9999 return this;
10000 }
10001
Selim Cinek384804b2018-04-18 14:31:07 +080010002 final StandardTemplateParams hideLargeIcon(boolean hideLargeIcon) {
10003 this.hideLargeIcon = hideLargeIcon;
Selim Cinek88188f22017-09-19 16:46:56 -070010004 return this;
10005 }
10006
Selim Cinek384804b2018-04-18 14:31:07 +080010007 final StandardTemplateParams hideReplyIcon(boolean hideReplyIcon) {
10008 this.hideReplyIcon = hideReplyIcon;
Selim Cinek88188f22017-09-19 16:46:56 -070010009 return this;
10010 }
10011
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010012 final StandardTemplateParams disallowColorization() {
10013 this.allowColorization = false;
10014 return this;
10015 }
10016
10017 final StandardTemplateParams forceDefaultColor() {
10018 this.forceDefaultColor = true;
10019 return this;
10020 }
10021
Adrian Roos70d7aa32017-01-11 15:39:06 -080010022 final StandardTemplateParams ambient(boolean ambient) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -080010023 Preconditions.checkState(title == null && text == null, "must set ambient before text");
Adrian Roos70d7aa32017-01-11 15:39:06 -080010024 this.ambient = ambient;
10025 return this;
10026 }
10027
10028 final StandardTemplateParams fillTextsFrom(Builder b) {
10029 Bundle extras = b.mN.extras;
Lucas Dupin06c5e642017-09-13 16:34:58 -070010030 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE), ambient);
10031
10032 // Big text notifications should contain their content when viewed in ambient mode.
10033 CharSequence text = extras.getCharSequence(EXTRA_BIG_TEXT);
10034 if (!ambient || TextUtils.isEmpty(text)) {
10035 text = extras.getCharSequence(EXTRA_TEXT);
10036 }
10037 this.text = b.processLegacyText(text, ambient);
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010038 this.summaryText = extras.getCharSequence(EXTRA_SUB_TEXT);
Adrian Roos70d7aa32017-01-11 15:39:06 -080010039 return this;
10040 }
Selim Cinekbee4e072018-05-21 22:06:43 -070010041
10042 /**
10043 * Set the maximum lines of remote input history lines allowed.
10044 * @param maxRemoteInputHistory The number of lines.
10045 * @return The builder for method chaining.
10046 */
10047 public StandardTemplateParams setMaxRemoteInputHistory(int maxRemoteInputHistory) {
10048 this.maxRemoteInputHistory = maxRemoteInputHistory;
10049 return this;
10050 }
Adrian Roos70d7aa32017-01-11 15:39:06 -080010051 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010052}