blob: dba62f222f674fdfdcbb1ca2aeba30d4ac8c00ae [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 Sennton761884c2018-11-19 17:40:19 +00003133 public boolean getAllowSystemGeneratedContextualActions() {
3134 return mAllowSystemGeneratedContextualActions;
3135 }
3136
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003137 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003138 * The small icon representing this notification in the status bar and content view.
3139 *
3140 * @return the small icon representing this notification.
3141 *
3142 * @see Builder#getSmallIcon()
3143 * @see Builder#setSmallIcon(Icon)
3144 */
3145 public Icon getSmallIcon() {
3146 return mSmallIcon;
3147 }
3148
3149 /**
3150 * Used when notifying to clean up legacy small icons.
3151 * @hide
3152 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003153 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04003154 public void setSmallIcon(Icon icon) {
3155 mSmallIcon = icon;
3156 }
3157
3158 /**
3159 * The large icon shown in this notification's content view.
3160 * @see Builder#getLargeIcon()
3161 * @see Builder#setLargeIcon(Icon)
3162 */
3163 public Icon getLargeIcon() {
3164 return mLargeIcon;
3165 }
3166
3167 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02003168 * @hide
3169 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003170 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003171 public boolean isGroupSummary() {
3172 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
3173 }
3174
3175 /**
3176 * @hide
3177 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003178 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003179 public boolean isGroupChild() {
3180 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
3181 }
3182
3183 /**
Julia Reynolds30203152017-05-26 13:36:31 -04003184 * @hide
3185 */
3186 public boolean suppressAlertingDueToGrouping() {
3187 if (isGroupSummary()
3188 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
3189 return true;
3190 } else if (isGroupChild()
3191 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
3192 return true;
3193 }
3194 return false;
3195 }
3196
Tony Mak638430e2018-10-08 19:19:10 +01003197
3198 /**
3199 * Finds and returns a remote input and its corresponding action.
3200 *
3201 * @param requiresFreeform requires the remoteinput to allow freeform or not.
3202 * @return the result pair, {@code null} if no result is found.
3203 *
3204 * @hide
3205 */
3206 @Nullable
3207 public Pair<RemoteInput, Action> findRemoteInputActionPair(boolean requiresFreeform) {
3208 if (actions == null) {
3209 return null;
3210 }
3211 for (Notification.Action action : actions) {
3212 if (action.getRemoteInputs() == null) {
3213 continue;
3214 }
3215 RemoteInput resultRemoteInput = null;
3216 for (RemoteInput remoteInput : action.getRemoteInputs()) {
3217 if (remoteInput.getAllowFreeFormInput() || !requiresFreeform) {
3218 resultRemoteInput = remoteInput;
3219 }
3220 }
3221 if (resultRemoteInput != null) {
3222 return Pair.create(resultRemoteInput, action);
3223 }
3224 }
3225 return null;
3226 }
3227
Julia Reynolds30203152017-05-26 13:36:31 -04003228 /**
Gustav Senntoneab53682018-11-01 16:30:23 +00003229 * Returns the actions that are contextual (marked as SEMANTIC_ACTION_CONTEXTUAL_SUGGESTION) out
3230 * of the actions in this notification.
3231 *
3232 * @hide
3233 */
3234 public List<Notification.Action> getContextualActions() {
3235 if (actions == null) return Collections.emptyList();
3236
3237 List<Notification.Action> contextualActions = new ArrayList<>();
3238 for (Notification.Action action : actions) {
3239 if (action.getSemanticAction()
3240 == Notification.Action.SEMANTIC_ACTION_CONTEXTUAL_SUGGESTION) {
3241 contextualActions.add(action);
3242 }
3243 }
3244 return contextualActions;
3245 }
3246
3247 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003248 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08003249 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003250 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07003251 * content views using the platform's notification layout template. If your app supports
3252 * versions of Android as old as API level 4, you can instead use
3253 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
3254 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
3255 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08003256 *
Scott Main183bf112012-08-13 19:12:13 -07003257 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08003258 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003259 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07003260 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003261 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3262 * .setContentText(subject)
3263 * .setSmallIcon(R.drawable.new_mail)
3264 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003265 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003266 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08003267 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003268 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003269 /**
3270 * @hide
3271 */
3272 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
3273 "android.rebuild.contentViewActionCount";
3274 /**
3275 * @hide
3276 */
3277 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
3278 = "android.rebuild.bigViewActionCount";
3279 /**
3280 * @hide
3281 */
3282 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
3283 = "android.rebuild.hudViewActionCount";
3284
Selim Cinek6743c0b2017-01-18 18:24:01 -08003285 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
3286 SystemProperties.getBoolean("notifications.only_title", true);
3287
Selim Cinek389edcd2017-05-11 19:16:44 -07003288 /**
3289 * The lightness difference that has to be added to the primary text color to obtain the
3290 * secondary text color when the background is light.
3291 */
3292 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
3293
3294 /**
3295 * The lightness difference that has to be added to the primary text color to obtain the
3296 * secondary text color when the background is dark.
3297 * A bit less then the above value, since it looks better on dark backgrounds.
3298 */
3299 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
3300
Joe Onorato46439ce2010-11-19 13:56:21 -08003301 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003302 private Notification mN;
3303 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003304 private Style mStyle;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003305 @UnsupportedAppUsage
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003306 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Selim Cineke7238dd2017-12-14 17:48:32 -08003307 private ArrayList<Person> mPersonList = new ArrayList<>();
Lucas Dupina291d192018-06-07 13:59:42 -07003308 private ContrastColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08003309 private boolean mIsLegacy;
3310 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02003311
3312 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08003313 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
3314 */
3315 private int mCachedContrastColor = COLOR_INVALID;
3316 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08003317 /**
Selim Cinek4717d862018-04-19 09:19:15 +08003318 * Caches a ambient version of {@link #mCachedAmbientColorIsFor}.
Adrian Roos487374f2017-01-11 15:48:14 -08003319 */
3320 private int mCachedAmbientColor = COLOR_INVALID;
3321 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Selim Cinek4717d862018-04-19 09:19:15 +08003322 /**
3323 * A neutral color color that can be used for icons.
3324 */
3325 private int mNeutralColor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08003326
3327 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08003328 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
3329 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
3330 */
3331 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08003332 private int mTextColorsAreForBackground = COLOR_INVALID;
3333 private int mPrimaryTextColor = COLOR_INVALID;
3334 private int mSecondaryTextColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07003335 private int mBackgroundColor = COLOR_INVALID;
3336 private int mForegroundColor = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07003337 /**
3338 * A temporary location where actions are stored. If != null the view originally has action
3339 * but doesn't have any for this inflation.
3340 */
3341 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07003342 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003343
Anthony Chenad4d1582017-04-10 16:07:58 -07003344 private boolean mTintActionButtons;
3345 private boolean mInNightMode;
3346
Adrian Roos70d7aa32017-01-11 15:39:06 -08003347 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003348 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08003349 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003350 * @param context
3351 * A {@link Context} that will be used by the Builder to construct the
3352 * RemoteViews. The Context will not be held past the lifetime of this Builder
3353 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003354 * @param channelId
3355 * The constructed Notification will be posted on this
3356 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
3357 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003358 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003359 public Builder(Context context, String channelId) {
3360 this(context, (Notification) null);
3361 mN.mChannelId = channelId;
3362 }
3363
3364 /**
3365 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
3366 * instead. All posted Notifications must specify a NotificationChannel Id.
3367 */
3368 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003369 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003370 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003371 }
3372
Joe Onoratocb109a02011-01-18 17:57:41 -08003373 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003374 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02003375 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003376 public Builder(Context context, Notification toAdopt) {
3377 mContext = context;
Anthony Chenad4d1582017-04-10 16:07:58 -07003378 Resources res = mContext.getResources();
3379 mTintActionButtons = res.getBoolean(R.bool.config_tintNotificationActionButtons);
3380
3381 if (res.getBoolean(R.bool.config_enableNightMode)) {
3382 Configuration currentConfig = res.getConfiguration();
3383 mInNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
3384 == Configuration.UI_MODE_NIGHT_YES;
3385 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003386
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003387 if (toAdopt == null) {
3388 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003389 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
3390 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
3391 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003392 mN.priority = PRIORITY_DEFAULT;
3393 mN.visibility = VISIBILITY_PRIVATE;
3394 } else {
3395 mN = toAdopt;
3396 if (mN.actions != null) {
3397 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003398 }
3399
Selim Cineke7238dd2017-12-14 17:48:32 -08003400 if (mN.extras.containsKey(EXTRA_PEOPLE_LIST)) {
3401 ArrayList<Person> people = mN.extras.getParcelableArrayList(EXTRA_PEOPLE_LIST);
3402 mPersonList.addAll(people);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003403 }
3404
Selim Cinek4ac6f602016-06-13 15:47:03 -07003405 if (mN.getSmallIcon() == null && mN.icon != 0) {
3406 setSmallIcon(mN.icon);
3407 }
3408
3409 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
3410 setLargeIcon(mN.largeIcon);
3411 }
3412
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003413 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
3414 if (!TextUtils.isEmpty(templateClass)) {
3415 final Class<? extends Style> styleClass
3416 = getNotificationStyleClass(templateClass);
3417 if (styleClass == null) {
3418 Log.d(TAG, "Unknown style class: " + templateClass);
3419 } else {
3420 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003421 final Constructor<? extends Style> ctor =
3422 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003423 ctor.setAccessible(true);
3424 final Style style = ctor.newInstance();
3425 style.restoreFromExtras(mN.extras);
3426
3427 if (style != null) {
3428 setStyle(style);
3429 }
3430 } catch (Throwable t) {
3431 Log.e(TAG, "Could not create Style", t);
3432 }
3433 }
3434 }
3435
3436 }
3437 }
3438
Lucas Dupina291d192018-06-07 13:59:42 -07003439 private ContrastColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08003440 if (mColorUtil == null) {
Lucas Dupina291d192018-06-07 13:59:42 -07003441 mColorUtil = ContrastColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003442 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003443 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003444 }
3445
3446 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003447 * If this notification is duplicative of a Launcher shortcut, sets the
3448 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
3449 * the shortcut.
3450 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04003451 * This field will be ignored by Launchers that don't support badging, don't show
3452 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003453 *
3454 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
3455 * supersedes
3456 */
3457 public Builder setShortcutId(String shortcutId) {
3458 mN.mShortcutId = shortcutId;
3459 return this;
3460 }
3461
3462 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003463 * Sets which icon to display as a badge for this notification.
3464 *
3465 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
3466 * {@link #BADGE_ICON_LARGE}.
3467 *
3468 * Note: This value might be ignored, for launchers that don't support badge icons.
3469 */
Julia Reynolds612beb22017-03-30 10:48:30 -04003470 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04003471 mN.mBadgeIcon = icon;
3472 return this;
3473 }
3474
3475 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003476 * Sets the group alert behavior for this notification. Use this method to mute this
3477 * notification if alerts for this notification's group should be handled by a different
3478 * notification. This is only applicable for notifications that belong to a
Julia Reynolds399d9bf2017-08-11 12:52:14 -04003479 * {@link #setGroup(String) group}. This must be called on all notifications you want to
3480 * mute. For example, if you want only the summary of your group to make noise, all
3481 * children in the group should have the group alert behavior {@link #GROUP_ALERT_SUMMARY}.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003482 *
3483 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
3484 */
3485 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
3486 mN.mGroupAlertBehavior = groupAlertBehavior;
3487 return this;
3488 }
3489
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003490 /**
3491 * Sets the intent that will be used to display app content in a floating window
3492 * over the existing foreground activity.
3493 *
3494 * <p>This intent will be ignored unless this notification is posted to a channel that
3495 * allows {@link NotificationChannel#canOverlayApps() app overlays}.</p>
3496 *
3497 * <p>Notifications with a valid and allowed app overlay intent will be displayed as
3498 * floating windows outside of the notification shade on unlocked devices. When a user
3499 * interacts with one of these windows, this app overlay intent will be invoked and
3500 * displayed.</p>
3501 */
3502 public Builder setAppOverlayIntent(PendingIntent intent) {
3503 mN.mAppOverlayIntent = intent;
3504 return this;
3505 }
3506
Jeff Sharkey000ce802017-04-29 13:13:27 -06003507 /** @removed */
3508 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003509 public Builder setChannel(String channelId) {
3510 mN.mChannelId = channelId;
3511 return this;
3512 }
3513
3514 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003515 * Specifies the channel the notification should be delivered on.
3516 */
3517 public Builder setChannelId(String channelId) {
3518 mN.mChannelId = channelId;
3519 return this;
3520 }
3521
Jeff Sharkey000ce802017-04-29 13:13:27 -06003522 /** @removed */
3523 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05003524 public Builder setTimeout(long durationMs) {
3525 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05003526 return this;
3527 }
3528
3529 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003530 * Specifies a duration in milliseconds after which this notification should be canceled,
3531 * if it is not already canceled.
3532 */
3533 public Builder setTimeoutAfter(long durationMs) {
3534 mN.mTimeout = durationMs;
3535 return this;
3536 }
3537
3538 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003539 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003540 *
3541 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
3542 * shown anymore by default and must be opted into by using
3543 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003544 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003545 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08003546 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003547 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003548 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08003549 return this;
3550 }
3551
Joe Onoratocb109a02011-01-18 17:57:41 -08003552 /**
Griff Hazen50c11652014-05-16 09:46:31 -07003553 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07003554 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003555 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
3556 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07003557 */
3558 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003559 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07003560 return this;
3561 }
3562
3563 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003564 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08003565 *
3566 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003567 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003568 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003569 * Useful when showing an elapsed time (like an ongoing phone call).
3570 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08003571 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07003572 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08003573 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003574 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003575 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07003576 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003577 */
3578 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003579 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003580 return this;
3581 }
3582
3583 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08003584 * Sets the Chronometer to count down instead of counting up.
3585 *
3586 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
3587 * If it isn't set the chronometer will count up.
3588 *
3589 * @see #setUsesChronometer(boolean)
3590 */
Adrian Roos96b7e202016-05-17 13:50:38 -07003591 public Builder setChronometerCountDown(boolean countDown) {
3592 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003593 return this;
3594 }
3595
3596 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003597 * Set the small icon resource, which will be used to represent the notification in the
3598 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003599 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003600
3601 * The platform template for the expanded view will draw this icon in the left, unless a
3602 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3603 * icon will be moved to the right-hand side.
3604 *
3605
3606 * @param icon
3607 * A resource ID in the application's package of the drawable to use.
3608 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003609 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003610 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003611 return setSmallIcon(icon != 0
3612 ? Icon.createWithResource(mContext, icon)
3613 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003614 }
3615
Joe Onoratocb109a02011-01-18 17:57:41 -08003616 /**
3617 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3618 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3619 * LevelListDrawable}.
3620 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003621 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003622 * @param level The level to use for the icon.
3623 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003624 * @see Notification#icon
3625 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003626 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003627 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003628 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003629 return setSmallIcon(icon);
3630 }
3631
3632 /**
3633 * Set the small icon, which will be used to represent the notification in the
koprivadebd4ee2018-09-13 10:59:46 -07003634 * status bar and content view (unless overridden there by a
Dan Sandlerd63f9322015-05-06 15:18:49 -04003635 * {@link #setLargeIcon(Bitmap) large icon}).
3636 *
3637 * @param icon An Icon object to use.
3638 * @see Notification#icon
3639 */
3640 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003641 mN.setSmallIcon(icon);
3642 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3643 mN.icon = icon.getResId();
3644 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003645 return this;
3646 }
3647
Joe Onoratocb109a02011-01-18 17:57:41 -08003648 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003649 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003650 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003651 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003652 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003653 return this;
3654 }
3655
Joe Onoratocb109a02011-01-18 17:57:41 -08003656 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003657 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003658 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003659 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003660 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003661 return this;
3662 }
3663
Joe Onoratocb109a02011-01-18 17:57:41 -08003664 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003665 * This provides some additional information that is displayed in the notification. No
3666 * guarantees are given where exactly it is displayed.
3667 *
3668 * <p>This information should only be provided if it provides an essential
3669 * benefit to the understanding of the notification. The more text you provide the
3670 * less readable it becomes. For example, an email client should only provide the account
3671 * name here if more than one email account has been added.</p>
3672 *
3673 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3674 * notification header area.
3675 *
3676 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3677 * this will be shown in the third line of text in the platform notification template.
3678 * You should not be using {@link #setProgress(int, int, boolean)} at the
3679 * same time on those versions; they occupy the same place.
3680 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003681 */
3682 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003683 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003684 return this;
3685 }
3686
3687 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003688 * Provides text that will appear as a link to your application's settings.
3689 *
3690 * <p>This text does not appear within notification {@link Style templates} but may
3691 * appear when the user uses an affordance to learn more about the notification.
3692 * Additionally, this text will not appear unless you provide a valid link target by
3693 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3694 *
3695 * <p>This text is meant to be concise description about what the user can customize
3696 * when they click on this link. The recommended maximum length is 40 characters.
3697 * @param text
3698 * @return
3699 */
3700 public Builder setSettingsText(CharSequence text) {
3701 mN.mSettingsText = safeCharSequence(text);
3702 return this;
3703 }
3704
3705 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003706 * Set the remote input history.
3707 *
3708 * This should be set to the most recent inputs that have been sent
3709 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3710 * longer relevant (e.g. for chat notifications once the other party has responded).
3711 *
3712 * The most recent input must be stored at the 0 index, the second most recent at the
3713 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3714 * and how much of each individual input is shown.
3715 *
3716 * <p>Note: The reply text will only be shown on notifications that have least one action
3717 * with a {@code RemoteInput}.</p>
3718 */
3719 public Builder setRemoteInputHistory(CharSequence[] text) {
3720 if (text == null) {
3721 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3722 } else {
3723 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3724 CharSequence[] safe = new CharSequence[N];
3725 for (int i = 0; i < N; i++) {
3726 safe[i] = safeCharSequence(text[i]);
3727 }
3728 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3729 }
3730 return this;
3731 }
3732
3733 /**
Kenny Guya0f6de82018-04-06 16:20:16 +01003734 * Sets whether remote history entries view should have a spinner.
3735 * @hide
3736 */
3737 public Builder setShowRemoteInputSpinner(boolean showSpinner) {
3738 mN.extras.putBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER, showSpinner);
3739 return this;
3740 }
3741
3742 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +01003743 * Sets whether smart reply buttons should be hidden.
3744 * @hide
3745 */
3746 public Builder setHideSmartReplies(boolean hideSmartReplies) {
3747 mN.extras.putBoolean(EXTRA_HIDE_SMART_REPLIES, hideSmartReplies);
3748 return this;
3749 }
3750
3751 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003752 * Sets the number of items this notification represents. May be displayed as a badge count
3753 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003754 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08003755 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003756 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003757 return this;
3758 }
3759
Joe Onoratocb109a02011-01-18 17:57:41 -08003760 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003761 * A small piece of additional information pertaining to this notification.
3762 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003763 * The platform template will draw this on the last line of the notification, at the far
3764 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003765 *
3766 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3767 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3768 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003769 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003770 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003771 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003772 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003773 return this;
3774 }
3775
Joe Onoratocb109a02011-01-18 17:57:41 -08003776 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003777 * Set the progress this notification represents.
3778 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003779 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003780 */
3781 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003782 mN.extras.putInt(EXTRA_PROGRESS, progress);
3783 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3784 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003785 return this;
3786 }
3787
3788 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003789 * Supply a custom RemoteViews to use instead of the platform template.
3790 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003791 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003792 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003793 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003794 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003795 return setCustomContentView(views);
3796 }
3797
3798 /**
3799 * Supply custom RemoteViews to use instead of the platform template.
3800 *
3801 * This will override the layout that would otherwise be constructed by this Builder
3802 * object.
3803 */
3804 public Builder setCustomContentView(RemoteViews contentView) {
3805 mN.contentView = contentView;
3806 return this;
3807 }
3808
3809 /**
3810 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3811 *
3812 * This will override the expanded layout that would otherwise be constructed by this
3813 * Builder object.
3814 */
3815 public Builder setCustomBigContentView(RemoteViews contentView) {
3816 mN.bigContentView = contentView;
3817 return this;
3818 }
3819
3820 /**
3821 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3822 *
3823 * This will override the heads-up layout that would otherwise be constructed by this
3824 * Builder object.
3825 */
3826 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3827 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003828 return this;
3829 }
3830
Joe Onoratocb109a02011-01-18 17:57:41 -08003831 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003832 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3833 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003834 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3835 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3836 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003837 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003838 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003839 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003840 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003841 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003842 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003843 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003844 return this;
3845 }
3846
Joe Onoratocb109a02011-01-18 17:57:41 -08003847 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003848 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3849 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003850 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003851 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003852 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003853 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003854 return this;
3855 }
3856
Joe Onoratocb109a02011-01-18 17:57:41 -08003857 /**
3858 * An intent to launch instead of posting the notification to the status bar.
3859 * Only for use with extremely high-priority notifications demanding the user's
3860 * <strong>immediate</strong> attention, such as an incoming phone call or
3861 * alarm clock that the user has explicitly set to a particular time.
3862 * If this facility is used for something else, please give the user an option
3863 * to turn it off and use a normal notification, as this can be extremely
3864 * disruptive.
3865 *
Chris Wren47c20a12014-06-18 17:27:29 -04003866 * <p>
3867 * The system UI may choose to display a heads-up notification, instead of
3868 * launching this intent, while the user is using the device.
3869 * </p>
Julia Reynolds47fd15f2018-11-28 10:16:00 -05003870 * <p>Apps targeting {@link Build.VERSION_CODES#Q} and above will have to request
3871 * a permission ({@link android.Manifest.permission#USE_FULL_SCREEN_INTENT}) in order to
3872 * use full screen intents.</p>
Chris Wren47c20a12014-06-18 17:27:29 -04003873 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003874 * @param intent The pending intent to launch.
3875 * @param highPriority Passing true will cause this notification to be sent
3876 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003877 *
3878 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003879 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003880 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003881 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003882 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3883 return this;
3884 }
3885
Joe Onoratocb109a02011-01-18 17:57:41 -08003886 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003887 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003888 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003889 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003890 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003891 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003892 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003893 return this;
3894 }
3895
Joe Onoratocb109a02011-01-18 17:57:41 -08003896 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003897 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003898 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003899 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003900 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003901 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003902 setTicker(tickerText);
3903 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003904 return this;
3905 }
3906
Joe Onoratocb109a02011-01-18 17:57:41 -08003907 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003908 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003909 *
3910 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04003911 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3912 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04003913 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003914 public Builder setLargeIcon(Bitmap b) {
3915 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3916 }
3917
3918 /**
3919 * Add a large icon to the notification content view.
3920 *
3921 * In the platform template, this image will be shown on the left of the notification view
3922 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3923 * badge atop the large icon).
3924 */
3925 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003926 mN.mLargeIcon = icon;
3927 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08003928 return this;
3929 }
3930
Joe Onoratocb109a02011-01-18 17:57:41 -08003931 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003932 * Set the sound to play.
3933 *
John Spurlockc0650f022014-07-19 13:22:39 -04003934 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
3935 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003936 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003937 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003938 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003939 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08003940 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003941 mN.sound = sound;
3942 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08003943 return this;
3944 }
3945
Joe Onoratocb109a02011-01-18 17:57:41 -08003946 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003947 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08003948 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003949 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
3950 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003951 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003952 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07003953 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003954 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08003955 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003956 mN.sound = sound;
3957 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08003958 return this;
3959 }
3960
Joe Onoratocb109a02011-01-18 17:57:41 -08003961 /**
John Spurlockc0650f022014-07-19 13:22:39 -04003962 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
3963 * use during playback.
3964 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003965 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04003966 * @see Notification#sound
3967 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003968 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04003969 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003970 mN.sound = sound;
3971 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04003972 return this;
3973 }
3974
3975 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003976 * Set the vibration pattern to use.
3977 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003978 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
3979 * <code>pattern</code> parameter.
3980 *
Chris Wren47c20a12014-06-18 17:27:29 -04003981 * <p>
3982 * A notification that vibrates is more likely to be presented as a heads-up notification.
3983 * </p>
3984 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003985 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003986 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08003987 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003988 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003989 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003990 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08003991 return this;
3992 }
3993
Joe Onoratocb109a02011-01-18 17:57:41 -08003994 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003995 * Set the desired color for the indicator LED on the device, as well as the
3996 * blink duty cycle (specified in milliseconds).
3997 *
3998
3999 * Not all devices will honor all (or even any) of these values.
4000 *
Julia Reynolds529e3322017-02-06 08:33:01 -05004001 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004002 * @see Notification#ledARGB
4003 * @see Notification#ledOnMS
4004 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08004005 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004006 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08004007 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004008 mN.ledARGB = argb;
4009 mN.ledOnMS = onMs;
4010 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004011 if (onMs != 0 || offMs != 0) {
4012 mN.flags |= FLAG_SHOW_LIGHTS;
4013 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004014 return this;
4015 }
4016
Joe Onoratocb109a02011-01-18 17:57:41 -08004017 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004018 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08004019 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004020
4021 * Ongoing notifications cannot be dismissed by the user, so your application or service
4022 * must take care of canceling them.
4023 *
4024
4025 * They are typically used to indicate a background task that the user is actively engaged
4026 * with (e.g., playing music) or is pending in some way and therefore occupying the device
4027 * (e.g., a file download, sync operation, active network connection).
4028 *
4029
4030 * @see Notification#FLAG_ONGOING_EVENT
Joe Onoratocb109a02011-01-18 17:57:41 -08004031 */
Joe Onorato46439ce2010-11-19 13:56:21 -08004032 public Builder setOngoing(boolean ongoing) {
4033 setFlag(FLAG_ONGOING_EVENT, ongoing);
4034 return this;
4035 }
4036
Joe Onoratocb109a02011-01-18 17:57:41 -08004037 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08004038 * Set whether this notification should be colorized. When set, the color set with
4039 * {@link #setColor(int)} will be used as the background color of this notification.
4040 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08004041 * This should only be used for high priority ongoing tasks like navigation, an ongoing
4042 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08004043 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07004044 * For most styles, the coloring will only be applied if the notification is for a
4045 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08004046 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07004047 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08004048 *
Aurimas Liutikas7f695332018-05-31 21:07:32 -07004049 * @see #setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08004050 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08004051 */
4052 public Builder setColorized(boolean colorize) {
4053 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
4054 return this;
4055 }
4056
4057 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08004058 * Set this flag if you would only like the sound, vibrate
4059 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004060 *
4061 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08004062 */
Joe Onorato46439ce2010-11-19 13:56:21 -08004063 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
4064 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
4065 return this;
4066 }
4067
Joe Onoratocb109a02011-01-18 17:57:41 -08004068 /**
Julia Reynolds04499532016-09-13 14:04:53 -04004069 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004070 *
4071 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08004072 */
Joe Onorato46439ce2010-11-19 13:56:21 -08004073 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08004074 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08004075 return this;
4076 }
4077
Joe Onoratocb109a02011-01-18 17:57:41 -08004078 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08004079 * Set whether or not this notification should not bridge to other devices.
4080 *
4081 * <p>Some notifications can be bridged to other devices for remote display.
4082 * This hint can be set to recommend this notification not be bridged.
4083 */
4084 public Builder setLocalOnly(boolean localOnly) {
4085 setFlag(FLAG_LOCAL_ONLY, localOnly);
4086 return this;
4087 }
4088
4089 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004090 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08004091 * <p>
4092 * The value should be one or more of the following fields combined with
4093 * bitwise-or:
4094 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
4095 * <p>
4096 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004097 *
4098 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05004099 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004100 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08004101 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004102 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004103 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004104 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08004105 return this;
4106 }
4107
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004108 /**
4109 * Set the priority of this notification.
4110 *
4111 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004112 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004113 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004114 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07004115 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004116 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004117 return this;
4118 }
Joe Malin8d40d042012-11-05 11:36:40 -08004119
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004120 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04004121 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08004122 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04004123 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004124 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04004125 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004126 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004127 return this;
4128 }
4129
4130 /**
Chris Wrendde75302014-03-26 17:24:15 -04004131 * Add a person that is relevant to this notification.
4132 *
Chris Wrene6c48932014-09-29 17:19:27 -04004133 * <P>
4134 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04004135 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4136 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4137 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04004138 * </P>
4139 *
4140 * <P>
4141 * The person should be specified by the {@code String} representation of a
4142 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
4143 * </P>
4144 *
4145 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
4146 * URIs. The path part of these URIs must exist in the contacts database, in the
4147 * appropriate column, or the reference will be discarded as invalid. Telephone schema
4148 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
Selim Cineke7238dd2017-12-14 17:48:32 -08004149 * It is also possible to provide a URI with the schema {@code name:} in order to uniquely
4150 * identify a person without an entry in the contacts database.
Chris Wrene6c48932014-09-29 17:19:27 -04004151 * </P>
4152 *
4153 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04004154 * @see Notification#EXTRA_PEOPLE
Selim Cineke7238dd2017-12-14 17:48:32 -08004155 * @deprecated use {@link #addPerson(Person)}
Chris Wrendde75302014-03-26 17:24:15 -04004156 */
Chris Wrene6c48932014-09-29 17:19:27 -04004157 public Builder addPerson(String uri) {
Selim Cinek9acd6732018-03-23 16:39:02 -07004158 addPerson(new Person.Builder().setUri(uri).build());
Selim Cineke7238dd2017-12-14 17:48:32 -08004159 return this;
4160 }
4161
4162 /**
4163 * Add a person that is relevant to this notification.
4164 *
4165 * <P>
4166 * Depending on user preferences, this annotation may allow the notification to pass
4167 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4168 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4169 * appear more prominently in the user interface.
4170 * </P>
4171 *
4172 * <P>
4173 * A person should usually contain a uri in order to benefit from the ranking boost.
4174 * However, even if no uri is provided, it's beneficial to provide other people in the
4175 * notification, such that listeners and voice only devices can announce and handle them
4176 * properly.
4177 * </P>
4178 *
4179 * @param person the person to add.
4180 * @see Notification#EXTRA_PEOPLE_LIST
4181 */
4182 public Builder addPerson(Person person) {
4183 mPersonList.add(person);
Chris Wrendde75302014-03-26 17:24:15 -04004184 return this;
4185 }
4186
4187 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004188 * Set this notification to be part of a group of notifications sharing the same key.
4189 * Grouped notifications may display in a cluster or stack on devices which
4190 * support such rendering.
4191 *
4192 * <p>To make this notification the summary for its group, also call
4193 * {@link #setGroupSummary}. A sort order can be specified for group members by using
4194 * {@link #setSortKey}.
4195 * @param groupKey The group key of the group.
4196 * @return this object for method chaining
4197 */
4198 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004199 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004200 return this;
4201 }
4202
4203 /**
4204 * Set this notification to be the group summary for a group of notifications.
4205 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04004206 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
4207 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004208 * @param isGroupSummary Whether this notification should be a group summary.
4209 * @return this object for method chaining
4210 */
4211 public Builder setGroupSummary(boolean isGroupSummary) {
4212 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
4213 return this;
4214 }
4215
4216 /**
4217 * Set a sort key that orders this notification among other notifications from the
4218 * same package. This can be useful if an external sort was already applied and an app
4219 * would like to preserve this. Notifications will be sorted lexicographically using this
4220 * value, although providing different priorities in addition to providing sort key may
4221 * cause this value to be ignored.
4222 *
4223 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07004224 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004225 *
4226 * @see String#compareTo(String)
4227 */
4228 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004229 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004230 return this;
4231 }
4232
4233 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004234 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004235 *
Griff Hazen720042b2014-02-24 15:46:56 -08004236 * <p>Values within the Bundle will replace existing extras values in this Builder.
4237 *
4238 * @see Notification#extras
4239 */
Griff Hazen959591e2014-05-15 22:26:18 -07004240 public Builder addExtras(Bundle extras) {
4241 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004242 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08004243 }
4244 return this;
4245 }
4246
4247 /**
4248 * Set metadata for this notification.
4249 *
4250 * <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 -04004251 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004252 * called.
4253 *
Griff Hazen720042b2014-02-24 15:46:56 -08004254 * <p>Replaces any existing extras values with those from the provided Bundle.
4255 * Use {@link #addExtras} to merge in metadata instead.
4256 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004257 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004258 */
Griff Hazen959591e2014-05-15 22:26:18 -07004259 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004260 if (extras != null) {
4261 mUserExtras = extras;
4262 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004263 return this;
4264 }
4265
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004266 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004267 * Get the current metadata Bundle used by this notification Builder.
4268 *
4269 * <p>The returned Bundle is shared with this Builder.
4270 *
4271 * <p>The current contents of this Bundle are copied into the Notification each time
4272 * {@link #build()} is called.
4273 *
4274 * @see Notification#extras
4275 */
4276 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004277 return mUserExtras;
4278 }
4279
4280 private Bundle getAllExtras() {
4281 final Bundle saveExtras = (Bundle) mUserExtras.clone();
4282 saveExtras.putAll(mN.extras);
4283 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08004284 }
4285
4286 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004287 * Add an action to this notification. Actions are typically displayed by
4288 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004289 * <p>
4290 * Every action must have an icon (32dp square and matching the
4291 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4292 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4293 * <p>
4294 * A notification in its expanded form can display up to 3 actions, from left to right in
4295 * the order they were added. Actions will not be displayed when the notification is
4296 * collapsed, however, so be sure that any essential functions may be accessed by the user
4297 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004298 *
4299 * @param icon Resource ID of a drawable that represents the action.
4300 * @param title Text describing the action.
4301 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04004302 *
4303 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004304 */
Dan Sandler86647982015-05-13 23:41:13 -04004305 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004306 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004307 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004308 return this;
4309 }
4310
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004311 /**
Griff Hazen959591e2014-05-15 22:26:18 -07004312 * Add an action to this notification. Actions are typically displayed by
4313 * the system as a button adjacent to the notification content.
4314 * <p>
4315 * Every action must have an icon (32dp square and matching the
4316 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4317 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4318 * <p>
4319 * A notification in its expanded form can display up to 3 actions, from left to right in
4320 * the order they were added. Actions will not be displayed when the notification is
4321 * collapsed, however, so be sure that any essential functions may be accessed by the user
4322 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
4323 *
4324 * @param action The action to add.
4325 */
4326 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08004327 if (action != null) {
4328 mActions.add(action);
4329 }
Griff Hazen959591e2014-05-15 22:26:18 -07004330 return this;
4331 }
4332
4333 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004334 * Alter the complete list of actions attached to this notification.
4335 * @see #addAction(Action).
4336 *
4337 * @param actions
4338 * @return
4339 */
4340 public Builder setActions(Action... actions) {
4341 mActions.clear();
4342 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08004343 if (actions[i] != null) {
4344 mActions.add(actions[i]);
4345 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004346 }
4347 return this;
4348 }
4349
4350 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004351 * Add a rich notification style to be applied at build time.
4352 *
4353 * @param style Object responsible for modifying the notification style.
4354 */
4355 public Builder setStyle(Style style) {
4356 if (mStyle != style) {
4357 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004358 if (mStyle != null) {
4359 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004360 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
4361 } else {
4362 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004363 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004364 }
4365 return this;
4366 }
4367
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004368 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05004369 * Returns the style set by {@link #setStyle(Style)}.
4370 */
4371 public Style getStyle() {
4372 return mStyle;
4373 }
4374
4375 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004376 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07004377 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004378 * @return The same Builder.
4379 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06004380 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004381 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004382 return this;
4383 }
4384
4385 /**
4386 * Supply a replacement Notification whose contents should be shown in insecure contexts
4387 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
4388 * @param n A replacement notification, presumably with some or all info redacted.
4389 * @return The same Builder.
4390 */
4391 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004392 if (n != null) {
4393 mN.publicVersion = new Notification();
4394 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
4395 } else {
4396 mN.publicVersion = null;
4397 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004398 return this;
4399 }
4400
Griff Hazenb720abe2014-05-20 13:15:30 -07004401 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004402 * Apply an extender to this notification builder. Extenders may be used to add
4403 * metadata or change options on this builder.
4404 */
Griff Hazen61a9e862014-05-22 16:05:19 -07004405 public Builder extend(Extender extender) {
4406 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004407 return this;
4408 }
4409
Dan Sandler4e787062015-06-17 15:09:48 -04004410 /**
4411 * @hide
4412 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04004413 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08004414 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004415 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004416 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004417 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004418 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04004419 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08004420 }
4421
Dan Sandler26e81cf2014-05-06 10:01:27 -04004422 /**
4423 * Sets {@link Notification#color}.
4424 *
4425 * @param argb The accent color to use
4426 *
4427 * @return The same Builder.
4428 */
Tor Norbye80756e32015-03-02 09:39:27 -08004429 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004430 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004431 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04004432 return this;
4433 }
4434
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004435 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04004436 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
4437 // This user can never be a badged profile,
4438 // and also includes USER_ALL system notifications.
4439 return null;
4440 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02004441 // Note: This assumes that the current user can read the profile badge of the
4442 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08004443 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05004444 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004445 }
4446
4447 private Bitmap getProfileBadge() {
4448 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01004449 if (badge == null) {
4450 return null;
4451 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004452 final int size = mContext.getResources().getDimensionPixelSize(
4453 R.dimen.notification_badge_size);
4454 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004455 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004456 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004457 badge.draw(canvas);
4458 return bitmap;
4459 }
4460
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004461 private void bindProfileBadge(RemoteViews contentView, StandardTemplateParams p) {
Kenny Guy98193ea2014-07-24 19:54:37 +01004462 Bitmap profileBadge = getProfileBadge();
4463
Kenny Guy98193ea2014-07-24 19:54:37 +01004464 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004465 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
4466 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004467 if (isColorized(p)) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004468 contentView.setDrawableTint(R.id.profile_badge, false,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004469 getPrimaryTextColor(p), PorterDuff.Mode.SRC_ATOP);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004470 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004471 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004472 }
4473
Gus Prevasbae5ba32018-11-28 15:45:13 -05004474 private void bindAlertedIcon(RemoteViews contentView, StandardTemplateParams p) {
4475 contentView.setDrawableTint(
4476 R.id.alerted_icon,
4477 false /* targetBackground */,
4478 getNeutralColor(p),
4479 PorterDuff.Mode.SRC_ATOP);
4480 }
4481
Julia Reynoldsfc640012018-02-21 12:25:27 -05004482 /**
4483 * @hide
4484 */
4485 public boolean usesStandardHeader() {
4486 if (mN.mUsesStandardHeader) {
4487 return true;
4488 }
4489 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.N) {
4490 if (mN.contentView == null && mN.bigContentView == null) {
4491 return true;
4492 }
4493 }
4494 boolean contentViewUsesHeader = mN.contentView == null
4495 || STANDARD_LAYOUTS.contains(mN.contentView.getLayoutId());
4496 boolean bigContentViewUsesHeader = mN.bigContentView == null
4497 || STANDARD_LAYOUTS.contains(mN.bigContentView.getLayoutId());
4498 return contentViewUsesHeader && bigContentViewUsesHeader;
4499 }
4500
Christoph Studerfe718432014-09-01 18:21:18 +02004501 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004502 resetNotificationHeader(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02004503 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08004504 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004505 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08004506 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004507 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08004508 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08004509 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004510 }
4511
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004512 /**
4513 * Resets the notification header to its original state
4514 */
4515 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07004516 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
4517 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08004518 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004519 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02004520 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004521 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07004522 contentView.setTextViewText(R.id.header_text, null);
Selim Cinekafeed292017-12-12 17:32:44 -08004523 contentView.setViewVisibility(R.id.header_text_secondary, View.GONE);
4524 contentView.setTextViewText(R.id.header_text_secondary, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004525 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinekafeed292017-12-12 17:32:44 -08004526 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004527 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004528 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004529 contentView.setImageViewIcon(R.id.profile_badge, null);
4530 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Gus Prevasa3226492018-10-23 11:10:09 -04004531 contentView.setViewVisibility(R.id.alerted_icon, View.GONE);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004532 mN.mUsesStandardHeader = false;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004533 }
4534
Selim Cinek384804b2018-04-18 14:31:07 +08004535 private RemoteViews applyStandardTemplate(int resId, TemplateBindResult result) {
4536 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this),
4537 result);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004538 }
4539
Selim Cinek384804b2018-04-18 14:31:07 +08004540 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p,
4541 TemplateBindResult result) {
Kenny Guy77320062014-08-27 21:37:15 +01004542 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04004543
Christoph Studerfe718432014-09-01 18:21:18 +02004544 resetStandardTemplate(contentView);
4545
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004546 final Bundle ex = mN.extras;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004547 updateBackgroundColor(contentView, p);
4548 bindNotificationHeader(contentView, p);
Selim Cinek384804b2018-04-18 14:31:07 +08004549 bindLargeIconAndReply(contentView, p, result);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004550 boolean showProgress = handleProgressBar(contentView, ex, p);
Adrian Roos70d7aa32017-01-11 15:39:06 -08004551 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08004552 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004553 contentView.setTextViewText(R.id.title, processTextSpans(p.title));
Adrian Roos72171622017-01-27 10:32:06 -08004554 if (!p.ambient) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004555 setTextViewColorPrimary(contentView, R.id.title, p);
Adrian Roos72171622017-01-27 10:32:06 -08004556 }
Selim Cinek954cc232016-05-20 13:29:23 -07004557 contentView.setViewLayoutWidth(R.id.title, showProgress
4558 ? ViewGroup.LayoutParams.WRAP_CONTENT
4559 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08004560 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08004561 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08004562 int textId = showProgress ? com.android.internal.R.id.text_line_1
4563 : com.android.internal.R.id.text;
Selim Cinek48f66b72017-08-18 16:17:51 -07004564 contentView.setTextViewText(textId, processTextSpans(p.text));
Adrian Roos72171622017-01-27 10:32:06 -08004565 if (!p.ambient) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004566 setTextViewColorSecondary(contentView, textId, p);
Adrian Roos72171622017-01-27 10:32:06 -08004567 }
Selim Cinek41598732016-01-11 16:58:37 -08004568 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08004569 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08004570
Selim Cinek279fa862016-06-14 10:57:25 -07004571 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004572
Selim Cinek29603462015-11-17 19:04:39 -08004573 return contentView;
4574 }
4575
Selim Cinek48f66b72017-08-18 16:17:51 -07004576 private CharSequence processTextSpans(CharSequence text) {
Lucas Dupind3c99322018-09-27 10:22:29 -07004577 if (hasForegroundColor() || mInNightMode) {
Lucas Dupina291d192018-06-07 13:59:42 -07004578 return ContrastColorUtil.clearColorSpans(text);
Selim Cinek48f66b72017-08-18 16:17:51 -07004579 }
4580 return text;
4581 }
4582
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004583 private void setTextViewColorPrimary(RemoteViews contentView, int id,
4584 StandardTemplateParams p) {
4585 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004586 contentView.setTextColor(id, mPrimaryTextColor);
4587 }
4588
Selim Cinek48f66b72017-08-18 16:17:51 -07004589 private boolean hasForegroundColor() {
4590 return mForegroundColor != COLOR_INVALID;
4591 }
4592
Selim Cinek389edcd2017-05-11 19:16:44 -07004593 /**
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004594 * Return the primary text color using the existing template params
Selim Cinek389edcd2017-05-11 19:16:44 -07004595 * @hide
4596 */
4597 @VisibleForTesting
4598 public int getPrimaryTextColor() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004599 return getPrimaryTextColor(mParams);
4600 }
4601
4602 /**
4603 * @param p the template params to inflate this with
4604 * @return the primary text color
4605 * @hide
4606 */
4607 @VisibleForTesting
4608 public int getPrimaryTextColor(StandardTemplateParams p) {
4609 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004610 return mPrimaryTextColor;
4611 }
4612
Selim Cinek389edcd2017-05-11 19:16:44 -07004613 /**
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004614 * Return the secondary text color using the existing template params
Selim Cinek389edcd2017-05-11 19:16:44 -07004615 * @hide
4616 */
4617 @VisibleForTesting
4618 public int getSecondaryTextColor() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004619 return getSecondaryTextColor(mParams);
4620 }
4621
4622 /**
4623 * @param p the template params to inflate this with
4624 * @return the secondary text color
4625 * @hide
4626 */
4627 @VisibleForTesting
4628 public int getSecondaryTextColor(StandardTemplateParams p) {
4629 ensureColors(p);
Selim Cinek389edcd2017-05-11 19:16:44 -07004630 return mSecondaryTextColor;
4631 }
4632
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004633 private void setTextViewColorSecondary(RemoteViews contentView, int id,
4634 StandardTemplateParams p) {
4635 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004636 contentView.setTextColor(id, mSecondaryTextColor);
4637 }
4638
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004639 private void ensureColors(StandardTemplateParams p) {
4640 int backgroundColor = getBackgroundColor(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004641 if (mPrimaryTextColor == COLOR_INVALID
4642 || mSecondaryTextColor == COLOR_INVALID
Selim Cinek7b9605b2017-01-19 17:36:00 -08004643 || mTextColorsAreForBackground != backgroundColor) {
4644 mTextColorsAreForBackground = backgroundColor;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004645 if (!hasForegroundColor() || !isColorized(p)) {
Lucas Dupina291d192018-06-07 13:59:42 -07004646 mPrimaryTextColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004647 backgroundColor, mInNightMode);
Lucas Dupina291d192018-06-07 13:59:42 -07004648 mSecondaryTextColor = ContrastColorUtil.resolveSecondaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004649 backgroundColor, mInNightMode);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004650 if (backgroundColor != COLOR_DEFAULT && isColorized(p)) {
Lucas Dupina291d192018-06-07 13:59:42 -07004651 mPrimaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004652 mPrimaryTextColor, backgroundColor, 4.5);
Lucas Dupina291d192018-06-07 13:59:42 -07004653 mSecondaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004654 mSecondaryTextColor, backgroundColor, 4.5);
4655 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07004656 } else {
Lucas Dupina291d192018-06-07 13:59:42 -07004657 double backLum = ContrastColorUtil.calculateLuminance(backgroundColor);
4658 double textLum = ContrastColorUtil.calculateLuminance(mForegroundColor);
4659 double contrast = ContrastColorUtil.calculateContrast(mForegroundColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004660 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07004661 // We only respect the given colors if worst case Black or White still has
4662 // contrast
4663 boolean backgroundLight = backLum > textLum
4664 && satisfiesTextContrast(backgroundColor, Color.BLACK)
4665 || backLum <= textLum
4666 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004667 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07004668 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004669 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004670 mForegroundColor,
4671 backgroundColor,
4672 true /* findFG */,
4673 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004674 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004675 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004676 } else {
4677 mSecondaryTextColor =
Lucas Dupina291d192018-06-07 13:59:42 -07004678 ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004679 mForegroundColor,
4680 backgroundColor,
4681 true /* findFG */,
4682 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004683 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004684 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004685 }
4686 } else {
4687 mPrimaryTextColor = mForegroundColor;
Lucas Dupina291d192018-06-07 13:59:42 -07004688 mSecondaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004689 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4690 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Lucas Dupina291d192018-06-07 13:59:42 -07004691 if (ContrastColorUtil.calculateContrast(mSecondaryTextColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004692 backgroundColor) < 4.5f) {
4693 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07004694 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004695 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004696 mSecondaryTextColor,
4697 backgroundColor,
4698 true /* findFG */,
4699 4.5f);
4700 } else {
4701 mSecondaryTextColor
Lucas Dupina291d192018-06-07 13:59:42 -07004702 = ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004703 mSecondaryTextColor,
4704 backgroundColor,
4705 true /* findFG */,
4706 4.5f);
4707 }
Lucas Dupina291d192018-06-07 13:59:42 -07004708 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004709 mSecondaryTextColor, backgroundLight
4710 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4711 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004712 }
4713 }
4714 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004715 }
4716 }
4717
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004718 private void updateBackgroundColor(RemoteViews contentView,
4719 StandardTemplateParams p) {
4720 if (isColorized(p)) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004721 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004722 getBackgroundColor(p));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004723 } else {
4724 // Clear it!
4725 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4726 0);
4727 }
4728 }
4729
Selim Cinek860b6da2015-12-16 19:02:19 -08004730 /**
4731 * @param remoteView the remote view to update the minheight in
4732 * @param hasMinHeight does it have a mimHeight
4733 * @hide
4734 */
4735 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4736 int minHeight = 0;
4737 if (hasMinHeight) {
4738 // we need to set the minHeight of the notification
4739 minHeight = mContext.getResources().getDimensionPixelSize(
4740 com.android.internal.R.dimen.notification_min_content_height);
4741 }
4742 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4743 }
4744
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004745 private boolean handleProgressBar(RemoteViews contentView, Bundle ex,
4746 StandardTemplateParams p) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004747 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4748 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4749 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004750 if (p.hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004751 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004752 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004753 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004754 contentView.setProgressBackgroundTintList(
4755 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4756 R.color.notification_progress_background_color)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004757 if (getRawColor(p) != COLOR_DEFAULT) {
4758 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor(p));
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004759 contentView.setProgressTintList(R.id.progress, colorStateList);
4760 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004761 }
Selim Cinek29603462015-11-17 19:04:39 -08004762 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004763 } else {
4764 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004765 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004766 }
Joe Onorato561d3852010-11-20 18:09:34 -08004767 }
4768
Selim Cinek384804b2018-04-18 14:31:07 +08004769 private void bindLargeIconAndReply(RemoteViews contentView, StandardTemplateParams p,
4770 TemplateBindResult result) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004771 boolean largeIconShown = bindLargeIcon(contentView, p);
4772 boolean replyIconShown = bindReplyIcon(contentView, p);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004773 boolean iconContainerVisible = largeIconShown || replyIconShown;
Selim Cinek384804b2018-04-18 14:31:07 +08004774 contentView.setViewVisibility(R.id.right_icon_container,
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004775 iconContainerVisible ? View.VISIBLE : View.GONE);
Selim Cinek1c72fa02018-04-23 18:00:54 +08004776 int marginEnd = calculateMarginEnd(largeIconShown, replyIconShown);
4777 contentView.setViewLayoutMarginEnd(R.id.line1, marginEnd);
4778 contentView.setViewLayoutMarginEnd(R.id.text, marginEnd);
4779 contentView.setViewLayoutMarginEnd(R.id.progress, marginEnd);
Selim Cinek384804b2018-04-18 14:31:07 +08004780 if (result != null) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08004781 result.setIconMarginEnd(marginEnd);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004782 result.setRightIconContainerVisible(iconContainerVisible);
Selim Cinek384804b2018-04-18 14:31:07 +08004783 }
4784 }
4785
Selim Cinek1c72fa02018-04-23 18:00:54 +08004786 private int calculateMarginEnd(boolean largeIconShown, boolean replyIconShown) {
4787 int marginEnd = 0;
4788 int contentMargin = mContext.getResources().getDimensionPixelSize(
4789 R.dimen.notification_content_margin_end);
4790 int iconSize = mContext.getResources().getDimensionPixelSize(
4791 R.dimen.notification_right_icon_size);
4792 if (replyIconShown) {
4793 // The size of the reply icon
4794 marginEnd += iconSize;
4795
4796 int replyInset = mContext.getResources().getDimensionPixelSize(
4797 R.dimen.notification_reply_inset);
4798 // We're subtracting the inset of the reply icon to make sure it's
4799 // aligned nicely on the right, and remove it from the following padding
4800 marginEnd -= replyInset * 2;
4801 }
4802 if (largeIconShown) {
4803 // adding size of the right icon
4804 marginEnd += iconSize;
4805
4806 if (replyIconShown) {
4807 // We also add some padding to the reply icon if it's around
4808 marginEnd += contentMargin;
4809 }
4810 }
4811 if (replyIconShown || largeIconShown) {
4812 // The padding to the content
4813 marginEnd += contentMargin;
4814 }
4815 return marginEnd;
4816 }
4817
Selim Cinek384804b2018-04-18 14:31:07 +08004818 /**
4819 * Bind the large icon.
4820 * @return if the largeIcon is visible
4821 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004822 private boolean bindLargeIcon(RemoteViews contentView, StandardTemplateParams p) {
Selim Cinek279fa862016-06-14 10:57:25 -07004823 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4824 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4825 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004826 boolean showLargeIcon = mN.mLargeIcon != null && !p.hideLargeIcon && !p.ambient;
Selim Cinek88188f22017-09-19 16:46:56 -07004827 if (showLargeIcon) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004828 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4829 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004830 processLargeLegacyIcon(mN.mLargeIcon, contentView, p);
Selim Cinek88188f22017-09-19 16:46:56 -07004831 }
Selim Cinek384804b2018-04-18 14:31:07 +08004832 return showLargeIcon;
4833 }
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004834
Selim Cinek384804b2018-04-18 14:31:07 +08004835 /**
4836 * Bind the reply icon.
4837 * @return if the reply icon is visible
4838 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004839 private boolean bindReplyIcon(RemoteViews contentView, StandardTemplateParams p) {
4840 boolean actionVisible = !p.hideReplyIcon && !p.ambient;
Selim Cinek384804b2018-04-18 14:31:07 +08004841 Action action = null;
Selim Cinek88188f22017-09-19 16:46:56 -07004842 if (actionVisible) {
Selim Cinek384804b2018-04-18 14:31:07 +08004843 action = findReplyAction();
4844 actionVisible = action != null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004845 }
Selim Cinek384804b2018-04-18 14:31:07 +08004846 if (actionVisible) {
4847 contentView.setViewVisibility(R.id.reply_icon_action, View.VISIBLE);
4848 contentView.setDrawableTint(R.id.reply_icon_action,
4849 false /* targetBackground */,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004850 getNeutralColor(p),
Selim Cinek384804b2018-04-18 14:31:07 +08004851 PorterDuff.Mode.SRC_ATOP);
4852 contentView.setOnClickPendingIntent(R.id.reply_icon_action, action.actionIntent);
4853 contentView.setRemoteInputs(R.id.reply_icon_action, action.mRemoteInputs);
4854 } else {
4855 contentView.setRemoteInputs(R.id.reply_icon_action, null);
4856 }
4857 contentView.setViewVisibility(R.id.reply_icon_action,
4858 actionVisible ? View.VISIBLE : View.GONE);
4859 return actionVisible;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004860 }
4861
4862 private Action findReplyAction() {
4863 ArrayList<Action> actions = mActions;
4864 if (mOriginalActions != null) {
4865 actions = mOriginalActions;
4866 }
4867 int numActions = actions.size();
4868 for (int i = 0; i < numActions; i++) {
4869 Action action = actions.get(i);
4870 if (hasValidRemoteInput(action)) {
4871 return action;
4872 }
4873 }
4874 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004875 }
4876
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004877 private void bindNotificationHeader(RemoteViews contentView, StandardTemplateParams p) {
4878 bindSmallIcon(contentView, p);
4879 bindHeaderAppName(contentView, p);
4880 if (!p.ambient) {
Adrian Roos487374f2017-01-11 15:48:14 -08004881 // Ambient view does not have these
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004882 bindHeaderText(contentView, p);
4883 bindHeaderTextSecondary(contentView, p);
4884 bindHeaderChronometerAndTime(contentView, p);
4885 bindProfileBadge(contentView, p);
Gus Prevasbae5ba32018-11-28 15:45:13 -05004886 bindAlertedIcon(contentView, p);
Adrian Roos487374f2017-01-11 15:48:14 -08004887 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004888 bindActivePermissions(contentView, p);
4889 bindExpandButton(contentView, p);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004890 mN.mUsesStandardHeader = true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004891 }
4892
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004893 private void bindActivePermissions(RemoteViews contentView, StandardTemplateParams p) {
4894 int color = p.ambient ? resolveAmbientColor(p) : getNeutralColor(p);
Julia Reynoldsb887b232018-04-10 16:38:08 -04004895 contentView.setDrawableTint(R.id.camera, false, color, PorterDuff.Mode.SRC_ATOP);
4896 contentView.setDrawableTint(R.id.mic, false, color, PorterDuff.Mode.SRC_ATOP);
4897 contentView.setDrawableTint(R.id.overlay, false, color, PorterDuff.Mode.SRC_ATOP);
4898 }
4899
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004900 private void bindExpandButton(RemoteViews contentView, StandardTemplateParams p) {
4901 int color = isColorized(p) ? getPrimaryTextColor(p) : getSecondaryTextColor(p);
Sunny Goyal5b153922017-09-21 21:00:36 -07004902 contentView.setDrawableTint(R.id.expand_button, false, color,
4903 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08004904 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08004905 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004906 }
4907
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004908 private void bindHeaderChronometerAndTime(RemoteViews contentView,
4909 StandardTemplateParams p) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004910 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08004911 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004912 setTextViewColorSecondary(contentView, R.id.time_divider, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004913 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
4914 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
4915 contentView.setLong(R.id.chronometer, "setBase",
4916 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
4917 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07004918 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07004919 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004920 setTextViewColorSecondary(contentView, R.id.chronometer, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004921 } else {
4922 contentView.setViewVisibility(R.id.time, View.VISIBLE);
4923 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004924 setTextViewColorSecondary(contentView, R.id.time, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004925 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004926 } else {
4927 // We still want a time to be set but gone, such that we can show and hide it
4928 // on demand in case it's a child notification without anything in the header
4929 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004930 }
4931 }
4932
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004933 private void bindHeaderText(RemoteViews contentView, StandardTemplateParams p) {
4934 CharSequence summaryText = p.summaryText;
4935 if (summaryText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05004936 && mStyle.hasSummaryInHeader()) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004937 summaryText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05004938 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004939 if (summaryText == null
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004940 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4941 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004942 summaryText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004943 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004944 if (summaryText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004945 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek48f66b72017-08-18 16:17:51 -07004946 contentView.setTextViewText(R.id.header_text, processTextSpans(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004947 processLegacyText(summaryText)));
4948 setTextViewColorSecondary(contentView, R.id.header_text, p);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004949 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
4950 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004951 setTextViewColorSecondary(contentView, R.id.header_text_divider, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004952 }
4953 }
4954
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004955 private void bindHeaderTextSecondary(RemoteViews contentView, StandardTemplateParams p) {
4956 if (!TextUtils.isEmpty(p.headerTextSecondary)) {
Selim Cinekafeed292017-12-12 17:32:44 -08004957 contentView.setTextViewText(R.id.header_text_secondary, processTextSpans(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004958 processLegacyText(p.headerTextSecondary)));
4959 setTextViewColorSecondary(contentView, R.id.header_text_secondary, p);
Selim Cinekafeed292017-12-12 17:32:44 -08004960 contentView.setViewVisibility(R.id.header_text_secondary, View.VISIBLE);
4961 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004962 setTextViewColorSecondary(contentView, R.id.header_text_secondary_divider, p);
Selim Cinekafeed292017-12-12 17:32:44 -08004963 }
4964 }
4965
Adrian Rooseba05822016-04-22 17:09:27 -07004966 /**
4967 * @hide
4968 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01004969 @UnsupportedAppUsage
Adrian Rooseba05822016-04-22 17:09:27 -07004970 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004971 CharSequence name = null;
4972 final PackageManager pm = mContext.getPackageManager();
4973 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
4974 // only system packages which lump together a bunch of unrelated stuff
4975 // may substitute a different name to make the purpose of the
4976 // notification more clear. the correct package label should always
4977 // be accessible via SystemUI.
4978 final String pkg = mContext.getPackageName();
4979 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
4980 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
4981 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
4982 name = subName;
4983 } else {
4984 Log.w(TAG, "warning: pkg "
4985 + pkg + " attempting to substitute app name '" + subName
4986 + "' without holding perm "
4987 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
4988 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004989 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04004990 if (TextUtils.isEmpty(name)) {
4991 name = pm.getApplicationLabel(mContext.getApplicationInfo());
4992 }
4993 if (TextUtils.isEmpty(name)) {
4994 // still nothing?
4995 return null;
4996 }
4997
4998 return String.valueOf(name);
4999 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005000 private void bindHeaderAppName(RemoteViews contentView, StandardTemplateParams p) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04005001 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005002 if (isColorized(p)) {
5003 setTextViewColorPrimary(contentView, R.id.app_name_text, p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005004 } else {
5005 contentView.setTextColor(R.id.app_name_text,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005006 p.ambient ? resolveAmbientColor(p) : getSecondaryTextColor(p));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005007 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005008 }
5009
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005010 private boolean isColorized(StandardTemplateParams p) {
5011 return p.allowColorization && !p.ambient && mN.isColorized();
5012 }
5013
5014 private void bindSmallIcon(RemoteViews contentView, StandardTemplateParams p) {
Selim Cinek279fa862016-06-14 10:57:25 -07005015 if (mN.mSmallIcon == null && mN.icon != 0) {
5016 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
5017 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005018 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Sunny Goyal5b153922017-09-21 21:00:36 -07005019 contentView.setInt(R.id.icon, "setImageLevel", mN.iconLevel);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005020 processSmallIconColor(mN.mSmallIcon, contentView, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005021 }
5022
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005023 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005024 * @return true if the built notification will show the time or the chronometer; false
5025 * otherwise
5026 */
5027 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07005028 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005029 }
5030
Christoph Studerfe718432014-09-01 18:21:18 +02005031 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07005032 // actions_container is only reset when there are no actions to avoid focus issues with
5033 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02005034 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02005035 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08005036
5037 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
5038 big.setTextViewText(R.id.notification_material_reply_text_1, null);
Kenny Guya0f6de82018-04-06 16:20:16 +01005039 big.setViewVisibility(R.id.notification_material_reply_text_1_container, View.GONE);
5040 big.setViewVisibility(R.id.notification_material_reply_progress, View.GONE);
Adrian Roose458aa82015-12-08 16:17:19 -08005041
5042 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
5043 big.setTextViewText(R.id.notification_material_reply_text_2, null);
5044 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
5045 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07005046
Selim Cineked64a142018-02-06 18:06:01 -08005047 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
5048 R.dimen.notification_content_margin);
Christoph Studerfe718432014-09-01 18:21:18 +02005049 }
5050
Selim Cinek384804b2018-04-18 14:31:07 +08005051 private RemoteViews applyStandardTemplateWithActions(int layoutId,
5052 TemplateBindResult result) {
5053 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this),
5054 result);
Adrian Roos48d746a2016-04-12 14:57:28 -07005055 }
5056
Gustav Sennton1463d832018-11-06 16:12:48 +00005057 private static List<Notification.Action> filterOutContextualActions(
5058 List<Notification.Action> actions) {
5059 List<Notification.Action> nonContextualActions = new ArrayList<>();
5060 for (Notification.Action action : actions) {
5061 if (action.getSemanticAction()
5062 != Action.SEMANTIC_ACTION_CONTEXTUAL_SUGGESTION) {
5063 nonContextualActions.add(action);
5064 }
5065 }
5066 return nonContextualActions;
5067 }
5068
Adrian Roos70d7aa32017-01-11 15:39:06 -08005069 private RemoteViews applyStandardTemplateWithActions(int layoutId,
Selim Cinek384804b2018-04-18 14:31:07 +08005070 StandardTemplateParams p, TemplateBindResult result) {
5071 RemoteViews big = applyStandardTemplate(layoutId, p, result);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005072
Christoph Studerfe718432014-09-01 18:21:18 +02005073 resetStandardTemplateWithActions(big);
5074
Adrian Roose458aa82015-12-08 16:17:19 -08005075 boolean validRemoteInput = false;
5076
Gustav Sennton1463d832018-11-06 16:12:48 +00005077 // In the UI contextual actions appear separately from the standard actions, so we
5078 // filter them out here.
5079 List<Notification.Action> nonContextualActions = filterOutContextualActions(mActions);
5080
5081 int N = nonContextualActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08005082 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005083 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005084 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08005085 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005086 big.setViewVisibility(R.id.actions, View.VISIBLE);
Selim Cineked64a142018-02-06 18:06:01 -08005087 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Daniel Sandler8680bf82012-05-15 16:52:52 -04005088 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005089 for (int i=0; i<N; i++) {
Gustav Sennton1463d832018-11-06 16:12:48 +00005090 Action action = nonContextualActions.get(i);
5091
Selim Cinekffcc7cf2018-02-06 17:43:51 -08005092 boolean actionHasValidInput = hasValidRemoteInput(action);
5093 validRemoteInput |= actionHasValidInput;
Adrian Roose458aa82015-12-08 16:17:19 -08005094
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005095 final RemoteViews button = generateActionButton(action, emphazisedMode, p);
Selim Cinek396caca2018-04-10 17:46:46 -07005096 if (actionHasValidInput && !emphazisedMode) {
Selim Cinekffcc7cf2018-02-06 17:43:51 -08005097 // Clear the drawable
5098 button.setInt(R.id.action0, "setBackgroundResource", 0);
5099 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005100 big.addView(R.id.actions, button);
5101 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07005102 } else {
5103 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005104 }
Adrian Roose458aa82015-12-08 16:17:19 -08005105
5106 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08005107 if (!p.ambient && validRemoteInput && replyText != null
Selim Cinekbee4e072018-05-21 22:06:43 -07005108 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])
5109 && p.maxRemoteInputHistory > 0) {
Kenny Guya0f6de82018-04-06 16:20:16 +01005110 boolean showSpinner = mN.extras.getBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER);
Adrian Roose458aa82015-12-08 16:17:19 -08005111 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Kenny Guya0f6de82018-04-06 16:20:16 +01005112 big.setViewVisibility(R.id.notification_material_reply_text_1_container,
5113 View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005114 big.setTextViewText(R.id.notification_material_reply_text_1,
5115 processTextSpans(replyText[0]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005116 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1, p);
Kenny Guya0f6de82018-04-06 16:20:16 +01005117 big.setViewVisibility(R.id.notification_material_reply_progress,
5118 showSpinner ? View.VISIBLE : View.GONE);
5119 big.setProgressIndeterminateTintList(
5120 R.id.notification_material_reply_progress,
5121 ColorStateList.valueOf(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005122 isColorized(p) ? getPrimaryTextColor(p) : resolveContrastColor(p)));
Adrian Roose458aa82015-12-08 16:17:19 -08005123
Selim Cinekbee4e072018-05-21 22:06:43 -07005124 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])
5125 && p.maxRemoteInputHistory > 1) {
Adrian Roose458aa82015-12-08 16:17:19 -08005126 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005127 big.setTextViewText(R.id.notification_material_reply_text_2,
5128 processTextSpans(replyText[1]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005129 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2, p);
Adrian Roose458aa82015-12-08 16:17:19 -08005130
Selim Cinekbee4e072018-05-21 22:06:43 -07005131 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])
5132 && p.maxRemoteInputHistory > 2) {
Adrian Roose458aa82015-12-08 16:17:19 -08005133 big.setViewVisibility(
5134 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005135 big.setTextViewText(R.id.notification_material_reply_text_3,
5136 processTextSpans(replyText[2]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005137 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3, p);
Adrian Roose458aa82015-12-08 16:17:19 -08005138 }
5139 }
5140 }
5141
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005142 return big;
5143 }
5144
Adrian Roose458aa82015-12-08 16:17:19 -08005145 private boolean hasValidRemoteInput(Action action) {
5146 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
5147 // Weird actions
5148 return false;
5149 }
5150
5151 RemoteInput[] remoteInputs = action.getRemoteInputs();
5152 if (remoteInputs == null) {
5153 return false;
5154 }
5155
5156 for (RemoteInput r : remoteInputs) {
5157 CharSequence[] choices = r.getChoices();
5158 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
5159 return true;
5160 }
5161 }
5162 return false;
5163 }
5164
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005165 /**
5166 * Construct a RemoteViews for the final 1U notification layout. In order:
5167 * 1. Custom contentView from the caller
5168 * 2. Style's proposed content view
5169 * 3. Standard template view
5170 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005171 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005172 return createContentView(false /* increasedheight */ );
5173 }
5174
5175 /**
5176 * Construct a RemoteViews for the smaller content view.
5177 *
5178 * @param increasedHeight true if this layout be created with an increased height. Some
5179 * styles may support showing more then just that basic 1U size
5180 * and the system may decide to render important notifications
5181 * slightly bigger even when collapsed.
5182 *
5183 * @hide
5184 */
5185 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005186 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005187 return mN.contentView;
5188 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005189 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005190 if (styleView != null) {
5191 return styleView;
5192 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005193 }
Selim Cinek384804b2018-04-18 14:31:07 +08005194 return applyStandardTemplate(getBaseLayoutResource(), null /* result */);
Joe Onorato46439ce2010-11-19 13:56:21 -08005195 }
5196
Selim Cineka7679b62017-05-10 16:33:25 -07005197 private boolean useExistingRemoteView() {
5198 return mStyle == null || (!mStyle.displayCustomViewInline()
5199 && !mRebuildStyledRemoteViews);
5200 }
5201
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005202 /**
5203 * Construct a RemoteViews for the final big notification layout.
5204 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005205 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05005206 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07005207 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005208 return mN.bigContentView;
5209 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05005210 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08005211 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005212 } else if (mActions.size() != 0) {
Selim Cinek384804b2018-04-18 14:31:07 +08005213 result = applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5214 null /* result */);
Selim Cinek850a8542015-11-11 11:48:36 -05005215 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08005216 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05005217 return result;
5218 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005219
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005220 /**
Selim Cinek414ad332017-02-24 19:06:12 -08005221 * Construct a RemoteViews for the final notification header only. This will not be
5222 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005223 *
Adrian Roos6f6e1592017-05-02 16:22:53 -07005224 * @param ambient if true, generate the header for the ambient display layout.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005225 * @hide
5226 */
Adrian Roos6f6e1592017-05-02 16:22:53 -07005227 public RemoteViews makeNotificationHeader(boolean ambient) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005228 return makeNotificationHeader(mParams.reset().ambient(ambient).fillTextsFrom(this));
5229 }
5230
5231 /**
5232 * Construct a RemoteViews for the final notification header only. This will not be
5233 * colorized.
5234 *
5235 * @param p the template params to inflate this with
5236 */
5237 private RemoteViews makeNotificationHeader(StandardTemplateParams p) {
5238 // Headers on their own are never colorized
5239 p.disallowColorization();
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005240 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005241 p.ambient ? R.layout.notification_template_ambient_header
Adrian Roos6f6e1592017-05-02 16:22:53 -07005242 : R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005243 resetNotificationHeader(header);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005244 bindNotificationHeader(header, p);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005245 return header;
5246 }
5247
Adrian Roos487374f2017-01-11 15:48:14 -08005248 /**
5249 * Construct a RemoteViews for the ambient version of the notification.
5250 *
5251 * @hide
5252 */
5253 public RemoteViews makeAmbientNotification() {
5254 RemoteViews ambient = applyStandardTemplateWithActions(
5255 R.layout.notification_template_material_ambient,
Selim Cinek384804b2018-04-18 14:31:07 +08005256 mParams.reset().ambient(true).fillTextsFrom(this).hasProgress(false),
5257 null /* result */);
Adrian Roos487374f2017-01-11 15:48:14 -08005258 return ambient;
5259 }
5260
Selim Cinek29603462015-11-17 19:04:39 -08005261 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005262 if (result != null) {
5263 result.setViewVisibility(R.id.text_line_1, View.GONE);
5264 }
Selim Cinek29603462015-11-17 19:04:39 -08005265 }
5266
Selim Cinek6743c0b2017-01-18 18:24:01 -08005267 /**
5268 * Adapt the Notification header if this view is used as an expanded view.
5269 *
5270 * @hide
5271 */
5272 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005273 if (result != null) {
5274 result.setBoolean(R.id.notification_header, "setExpanded", true);
5275 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005276 }
5277
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005278 /**
5279 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005280 *
5281 * @param increasedHeight true if this layout be created with an increased height. Some
5282 * styles may support showing more then just that basic 1U size
5283 * and the system may decide to render important notifications
5284 * slightly bigger even when collapsed.
5285 *
5286 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005287 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005288 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005289 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005290 return mN.headsUpContentView;
5291 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08005292 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
5293 if (styleView != null) {
5294 return styleView;
5295 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05005296 } else if (mActions.size() == 0) {
5297 return null;
5298 }
5299
Selim Cinekbee4e072018-05-21 22:06:43 -07005300 // We only want at most a single remote input history to be shown here, otherwise
5301 // the content would become squished.
5302 StandardTemplateParams p = mParams.reset().fillTextsFrom(this)
5303 .setMaxRemoteInputHistory(1);
5304 return applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5305 p,
5306 null /* result */);
Chris Wren8fd39ec2014-02-27 17:43:26 -05005307 }
5308
Selim Cinek624c02db2015-12-14 21:00:02 -08005309 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08005310 * Construct a RemoteViews for the final heads-up notification layout.
5311 */
5312 public RemoteViews createHeadsUpContentView() {
5313 return createHeadsUpContentView(false /* useIncreasedHeight */);
5314 }
5315
5316 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08005317 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5318 *
5319 * @hide
5320 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005321 @UnsupportedAppUsage
Selim Cinek624c02db2015-12-14 21:00:02 -08005322 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005323 return makePublicView(false /* ambient */);
5324 }
5325
5326 /**
5327 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5328 *
5329 * @hide
5330 */
5331 public RemoteViews makePublicAmbientNotification() {
5332 return makePublicView(true /* ambient */);
5333 }
5334
5335 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08005336 if (mN.publicVersion != null) {
5337 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005338 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08005339 }
5340 Bundle savedBundle = mN.extras;
5341 Style style = mStyle;
5342 mStyle = null;
5343 Icon largeIcon = mN.mLargeIcon;
5344 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07005345 Bitmap largeIconLegacy = mN.largeIcon;
5346 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005347 ArrayList<Action> actions = mActions;
5348 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08005349 Bundle publicExtras = new Bundle();
5350 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
5351 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
5352 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
5353 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07005354 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
5355 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cineked07b962018-04-30 14:39:35 -07005356 String appName = savedBundle.getString(EXTRA_SUBSTITUTE_APP_NAME);
5357 if (appName != null) {
5358 publicExtras.putString(EXTRA_SUBSTITUTE_APP_NAME, appName);
5359 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005360 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07005361 RemoteViews view;
5362 if (ambient) {
5363 publicExtras.putCharSequence(EXTRA_TITLE,
5364 mContext.getString(com.android.internal.R.string.notification_hidden_text));
5365 view = makeAmbientNotification();
5366 } else{
5367 view = makeNotificationHeader(false /* ambient */);
5368 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
5369 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005370 mN.extras = savedBundle;
5371 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07005372 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005373 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08005374 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005375 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08005376 }
5377
Selim Cinek6743c0b2017-01-18 18:24:01 -08005378 /**
5379 * Construct a content view for the display when low - priority
5380 *
5381 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
5382 * a new subtext is created consisting of the content of the
5383 * notification.
5384 * @hide
5385 */
5386 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005387 StandardTemplateParams p = mParams.reset()
5388 .forceDefaultColor()
5389 .ambient(false)
5390 .fillTextsFrom(this);
5391 if (!useRegularSubtext || TextUtils.isEmpty(mParams.summaryText)) {
5392 p.summaryText(createSummaryText());
Selim Cinek6743c0b2017-01-18 18:24:01 -08005393 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005394 RemoteViews header = makeNotificationHeader(p);
Selim Cinek1b554392017-02-28 17:22:49 -08005395 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08005396 return header;
5397 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005398
Selim Cinek6743c0b2017-01-18 18:24:01 -08005399 private CharSequence createSummaryText() {
5400 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
5401 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
5402 return titleText;
5403 }
5404 SpannableStringBuilder summary = new SpannableStringBuilder();
5405 if (titleText == null) {
5406 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
5407 }
5408 BidiFormatter bidi = BidiFormatter.getInstance();
5409 if (titleText != null) {
5410 summary.append(bidi.unicodeWrap(titleText));
5411 }
5412 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
5413 if (titleText != null && contentText != null) {
5414 summary.append(bidi.unicodeWrap(mContext.getText(
5415 R.string.notification_header_divider_symbol_with_spaces)));
5416 }
5417 if (contentText != null) {
5418 summary.append(bidi.unicodeWrap(contentText));
5419 }
5420 return summary;
5421 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05005422
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005423 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005424 StandardTemplateParams p) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04005425 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07005426 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005427 emphazisedMode ? getEmphasizedActionLayoutResource()
5428 : tombstone ? getActionTombstoneLayoutResource()
5429 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04005430 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04005431 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04005432 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005433 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005434 if (action.mRemoteInputs != null) {
5435 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
5436 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005437 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07005438 // change the background bgColor
Selim Cinek981962e2016-07-20 20:41:58 -07005439 CharSequence title = action.title;
5440 ColorStateList[] outResultColor = null;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005441 int background = resolveBackgroundColor(p);
Selim Cinek981962e2016-07-20 20:41:58 -07005442 if (isLegacy()) {
Lucas Dupina291d192018-06-07 13:59:42 -07005443 title = ContrastColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07005444 } else {
5445 outResultColor = new ColorStateList[1];
Selim Cinek396caca2018-04-10 17:46:46 -07005446 title = ensureColorSpanContrast(title, background, outResultColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005447 }
Selim Cinek48f66b72017-08-18 16:17:51 -07005448 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005449 setTextViewColorPrimary(button, R.id.action0, p);
Selim Cinek396caca2018-04-10 17:46:46 -07005450 int rippleColor;
5451 boolean hasColorOverride = outResultColor != null && outResultColor[0] != null;
5452 if (hasColorOverride) {
5453 // There's a span spanning the full text, let's take it and use it as the
5454 // background color
5455 background = outResultColor[0].getDefaultColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005456 int textColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07005457 background, mInNightMode);
Selim Cinek396caca2018-04-10 17:46:46 -07005458 button.setTextColor(R.id.action0, textColor);
5459 rippleColor = textColor;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005460 } else if (getRawColor(p) != COLOR_DEFAULT && !isColorized(p)
5461 && mTintActionButtons) {
5462 rippleColor = resolveContrastColor(p);
Selim Cinek396caca2018-04-10 17:46:46 -07005463 button.setTextColor(R.id.action0, rippleColor);
5464 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005465 rippleColor = getPrimaryTextColor(p);
Selim Cinek981962e2016-07-20 20:41:58 -07005466 }
Selim Cinek396caca2018-04-10 17:46:46 -07005467 // We only want about 20% alpha for the ripple
5468 rippleColor = (rippleColor & 0x00ffffff) | 0x33000000;
5469 button.setColorStateList(R.id.action0, "setRippleColor",
5470 ColorStateList.valueOf(rippleColor));
5471 button.setColorStateList(R.id.action0, "setButtonBackground",
5472 ColorStateList.valueOf(background));
5473 button.setBoolean(R.id.action0, "setHasStroke", !hasColorOverride);
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005474 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07005475 button.setTextViewText(R.id.action0, processTextSpans(
5476 processLegacyText(action.title)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005477 if (isColorized(p)) {
5478 setTextViewColorPrimary(button, R.id.action0, p);
5479 } else if (getRawColor(p) != COLOR_DEFAULT && mTintActionButtons) {
Adrian Roos487374f2017-01-11 15:48:14 -08005480 button.setTextColor(R.id.action0,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005481 p.ambient ? resolveAmbientColor(p) : resolveContrastColor(p));
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005482 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005483 }
Tony Mak7d4b3a52018-11-27 17:29:36 +00005484 button.setIntTag(R.id.action0, R.id.notification_action_index_tag,
5485 mActions.indexOf(action));
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005486 return button;
5487 }
5488
Joe Onoratocb109a02011-01-18 17:57:41 -08005489 /**
Selim Cinek981962e2016-07-20 20:41:58 -07005490 * Ensures contrast on color spans against a background color. also returns the color of the
5491 * text if a span was found that spans over the whole text.
5492 *
5493 * @param charSequence the charSequence on which the spans are
5494 * @param background the background color to ensure the contrast against
5495 * @param outResultColor an array in which a color will be returned as the first element if
5496 * there exists a full length color span.
5497 * @return the contrasted charSequence
5498 */
5499 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
5500 ColorStateList[] outResultColor) {
5501 if (charSequence instanceof Spanned) {
5502 Spanned ss = (Spanned) charSequence;
5503 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
5504 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
5505 for (Object span : spans) {
5506 Object resultSpan = span;
5507 int spanStart = ss.getSpanStart(span);
5508 int spanEnd = ss.getSpanEnd(span);
5509 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
5510 if (resultSpan instanceof CharacterStyle) {
5511 resultSpan = ((CharacterStyle) span).getUnderlying();
5512 }
5513 if (resultSpan instanceof TextAppearanceSpan) {
5514 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
5515 ColorStateList textColor = originalSpan.getTextColor();
5516 if (textColor != null) {
5517 int[] colors = textColor.getColors();
5518 int[] newColors = new int[colors.length];
5519 for (int i = 0; i < newColors.length; i++) {
Lucas Dupina291d192018-06-07 13:59:42 -07005520 newColors[i] = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005521 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005522 }
5523 textColor = new ColorStateList(textColor.getStates().clone(),
5524 newColors);
Selim Cinek396caca2018-04-10 17:46:46 -07005525 if (fullLength) {
5526 outResultColor[0] = textColor;
5527 // Let's drop the color from the span
5528 textColor = null;
5529 }
Selim Cinek981962e2016-07-20 20:41:58 -07005530 resultSpan = new TextAppearanceSpan(
5531 originalSpan.getFamily(),
5532 originalSpan.getTextStyle(),
5533 originalSpan.getTextSize(),
5534 textColor,
5535 originalSpan.getLinkTextColor());
Selim Cinek981962e2016-07-20 20:41:58 -07005536 }
5537 } else if (resultSpan instanceof ForegroundColorSpan) {
5538 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
5539 int foregroundColor = originalSpan.getForegroundColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005540 foregroundColor = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005541 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005542 if (fullLength) {
5543 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
Selim Cinek396caca2018-04-10 17:46:46 -07005544 resultSpan = null;
5545 } else {
5546 resultSpan = new ForegroundColorSpan(foregroundColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005547 }
5548 } else {
5549 resultSpan = span;
5550 }
Selim Cinek396caca2018-04-10 17:46:46 -07005551 if (resultSpan != null) {
5552 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
5553 }
Selim Cinek981962e2016-07-20 20:41:58 -07005554 }
5555 return builder;
5556 }
5557 return charSequence;
5558 }
5559
5560 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005561 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07005562 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005563 */
5564 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005565 if (!mIsLegacyInitialized) {
5566 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
5567 < Build.VERSION_CODES.LOLLIPOP;
5568 mIsLegacyInitialized = true;
5569 }
5570 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005571 }
5572
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005573 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08005574 return processLegacyText(charSequence, false /* ambient */);
5575 }
5576
5577 private CharSequence processLegacyText(CharSequence charSequence, boolean ambient) {
5578 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
5579 boolean wantLightText = ambient;
5580 if (isAlreadyLightText != wantLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005581 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005582 } else {
5583 return charSequence;
5584 }
5585 }
5586
Dan Sandler26e81cf2014-05-06 10:01:27 -04005587 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005588 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04005589 */
Adrian Roos487374f2017-01-11 15:48:14 -08005590 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005591 StandardTemplateParams p) {
Selim Cinekea4bef72015-12-02 15:51:10 -08005592 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005593 int color;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005594 if (p.ambient) {
5595 color = resolveAmbientColor(p);
5596 } else if (isColorized(p)) {
5597 color = getPrimaryTextColor(p);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005598 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005599 color = resolveContrastColor(p);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005600 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005601 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07005602 contentView.setDrawableTint(R.id.icon, false, color,
5603 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005604
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005605 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005606 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08005607 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005608 }
5609
Dan Sandler26e81cf2014-05-06 10:01:27 -04005610 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005611 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04005612 * if it's grayscale).
5613 */
5614 // TODO: also check bounds, transparency, that sort of thing.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005615 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView,
5616 StandardTemplateParams p) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04005617 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005618 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005619 // resolve color will fall back to the default when legacy
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005620 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(p),
Sunny Goyal5b153922017-09-21 21:00:36 -07005621 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01005622 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005623 }
5624
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05005625 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005626 if (mN.color != COLOR_DEFAULT) {
5627 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02005628 }
Jorim Jaggi74419312014-06-10 20:57:21 +02005629 }
5630
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005631 int resolveContrastColor(StandardTemplateParams p) {
5632 int rawColor = getRawColor(p);
5633 if (mCachedContrastColorIsFor == rawColor && mCachedContrastColor != COLOR_INVALID) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08005634 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005635 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005636
Selim Cinekac5f0272017-05-02 16:05:41 -07005637 int color;
Selim Cinekc7f5a822018-03-20 19:32:06 -07005638 int background = mContext.getColor(
5639 com.android.internal.R.color.notification_material_background_color);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005640 if (rawColor == COLOR_DEFAULT) {
5641 ensureColors(p);
Lucas Dupinf03e7522018-06-25 16:21:13 -07005642 color = ContrastColorUtil.resolveDefaultColor(mContext, background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005643 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005644 color = ContrastColorUtil.resolveContrastColor(mContext, rawColor,
Anthony Chenad4d1582017-04-10 16:07:58 -07005645 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005646 }
5647 if (Color.alpha(color) < 255) {
5648 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005649 color = ContrastColorUtil.compositeColors(color, background);
Selim Cinekac5f0272017-05-02 16:05:41 -07005650 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005651 mCachedContrastColorIsFor = rawColor;
Selim Cinekac5f0272017-05-02 16:05:41 -07005652 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005653 }
5654
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005655 /**
5656 * Return the raw color of this Notification, which doesn't necessarily satisfy contrast.
5657 *
5658 * @see #resolveContrastColor(StandardTemplateParams) for the contrasted color
5659 * @param p the template params to inflate this with
5660 */
5661 private int getRawColor(StandardTemplateParams p) {
5662 if (p.forceDefaultColor) {
5663 return COLOR_DEFAULT;
5664 }
5665 return mN.color;
5666 }
5667
Selim Cinek4717d862018-04-19 09:19:15 +08005668 int resolveNeutralColor() {
5669 if (mNeutralColor != COLOR_INVALID) {
5670 return mNeutralColor;
5671 }
5672 int background = mContext.getColor(
5673 com.android.internal.R.color.notification_material_background_color);
Lucas Dupinf03e7522018-06-25 16:21:13 -07005674 mNeutralColor = ContrastColorUtil.resolveDefaultColor(mContext, background,
5675 mInNightMode);
Selim Cinek4717d862018-04-19 09:19:15 +08005676 if (Color.alpha(mNeutralColor) < 255) {
5677 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005678 mNeutralColor = ContrastColorUtil.compositeColors(mNeutralColor, background);
Selim Cinek4717d862018-04-19 09:19:15 +08005679 }
5680 return mNeutralColor;
5681 }
5682
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005683 int resolveAmbientColor(StandardTemplateParams p) {
5684 int rawColor = getRawColor(p);
5685 if (mCachedAmbientColorIsFor == rawColor && mCachedAmbientColorIsFor != COLOR_INVALID) {
Adrian Roos487374f2017-01-11 15:48:14 -08005686 return mCachedAmbientColor;
5687 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005688 final int contrasted = ContrastColorUtil.resolveAmbientColor(mContext, rawColor);
Adrian Roos487374f2017-01-11 15:48:14 -08005689
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005690 mCachedAmbientColorIsFor = rawColor;
Adrian Roos487374f2017-01-11 15:48:14 -08005691 return mCachedAmbientColor = contrasted;
5692 }
5693
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005694 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005695 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005696 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08005697 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005698 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005699 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005700 mN.actions = new Action[mActions.size()];
5701 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005702 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005703 if (!mPersonList.isEmpty()) {
Selim Cineke7238dd2017-12-14 17:48:32 -08005704 mN.extras.putParcelableArrayList(EXTRA_PEOPLE_LIST, mPersonList);
Dan Sandler0bf2ed82013-12-21 23:33:41 -06005705 }
Selim Cinek247fa012016-02-18 09:50:48 -08005706 if (mN.bigContentView != null || mN.contentView != null
5707 || mN.headsUpContentView != null) {
5708 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
5709 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005710 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08005711 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005712
Julia Reynolds3b848122016-02-26 10:45:32 -05005713 /**
5714 * Creates a Builder from an existing notification so further changes can be made.
5715 * @param context The context for your application / activity.
5716 * @param n The notification to create a Builder from.
5717 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005718 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005719 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005720 ApplicationInfo applicationInfo = n.extras.getParcelable(
5721 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005722 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05005723 if (applicationInfo != null) {
5724 try {
5725 builderContext = context.createApplicationContext(applicationInfo,
5726 Context.CONTEXT_RESTRICTED);
5727 } catch (NameNotFoundException e) {
5728 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
5729 builderContext = context; // try with our context
5730 }
5731 } else {
5732 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02005733 }
5734
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005735 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005736 }
5737
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005738 /**
Gustav Sennton761884c2018-11-19 17:40:19 +00005739 * Determines whether the platform can generate contextual actions for a notification.
5740 * By default this is true.
5741 */
5742 public Builder setAllowSystemGeneratedContextualActions(boolean allowed) {
5743 mN.mAllowSystemGeneratedContextualActions = allowed;
5744 return this;
5745 }
5746
5747 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005748 * @deprecated Use {@link #build()} instead.
5749 */
5750 @Deprecated
5751 public Notification getNotification() {
5752 return build();
5753 }
5754
5755 /**
5756 * Combine all of the options that have been set and return a new {@link Notification}
5757 * object.
5758 */
5759 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005760 // first, add any extras from the calling code
5761 if (mUserExtras != null) {
5762 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005763 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005764
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005765 mN.creationTime = System.currentTimeMillis();
5766
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005767 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05005768 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02005769
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005770 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005771
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005772 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02005773 mStyle.reduceImageSizes(mContext);
5774 mStyle.purgeResources();
Selim Cinek90343862018-02-01 11:07:11 -08005775 mStyle.validate(mContext);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005776 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005777 }
Selim Cinekd0426622017-07-11 13:19:59 +02005778 mN.reduceImageSizes(mContext);
5779
Adrian Roos5081c0d2016-02-26 16:04:19 -08005780 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07005781 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005782 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005783 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005784 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
5785 mN.contentView.getSequenceNumber());
5786 }
5787 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005788 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005789 if (mN.bigContentView != null) {
5790 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
5791 mN.bigContentView.getSequenceNumber());
5792 }
5793 }
5794 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005795 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005796 if (mN.headsUpContentView != null) {
5797 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
5798 mN.headsUpContentView.getSequenceNumber());
5799 }
5800 }
5801 }
5802
Julia Reynolds4c0c2022016-02-02 15:11:59 -05005803 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
5804 mN.flags |= FLAG_SHOW_LIGHTS;
5805 }
5806
Adrian Roosfb921842017-10-26 14:49:56 +02005807 mN.allPendingIntents = null;
5808
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005809 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005810 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005811
5812 /**
5813 * Apply this Builder to an existing {@link Notification} object.
5814 *
5815 * @hide
5816 */
5817 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005818 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005819 return n;
5820 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005821
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005822 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005823 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005824 * change. Also removes extenders on low ram devices, as
5825 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005826 *
5827 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5828 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005829 * @hide
5830 */
Kristian Monsen05f34792018-04-09 10:27:16 +02005831 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam,
5832 Context context) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005833 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005834
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005835 // Only strip views for known Styles because we won't know how to
5836 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005837 if (!isLowRam
5838 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005839 && getNotificationStyleClass(templateClass) == null) {
5840 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005841 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005842
5843 // Only strip unmodified BuilderRemoteViews.
5844 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005845 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005846 n.contentView.getSequenceNumber();
5847 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005848 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005849 n.bigContentView.getSequenceNumber();
5850 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005851 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005852 n.headsUpContentView.getSequenceNumber();
5853
5854 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005855 if (!isLowRam
5856 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005857 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005858 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005859
5860 Notification clone = n.clone();
5861 if (stripContentView) {
5862 clone.contentView = null;
5863 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5864 }
5865 if (stripBigContentView) {
5866 clone.bigContentView = null;
5867 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5868 }
5869 if (stripHeadsUpContentView) {
5870 clone.headsUpContentView = null;
5871 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5872 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005873 if (isLowRam) {
Kristian Monsen05f34792018-04-09 10:27:16 +02005874 String[] allowedServices = context.getResources().getStringArray(
5875 R.array.config_allowedManagedServicesOnLowRamDevices);
5876 if (allowedServices.length == 0) {
5877 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5878 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5879 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5880 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005881 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005882 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005883 }
5884
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005885 @UnsupportedAppUsage
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005886 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005887 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005888 }
5889
5890 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005891 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005892 }
5893
5894 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005895 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005896 }
5897
5898 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005899 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005900 }
5901
5902 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005903 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005904 }
5905
Adrian Roosc1a80b02016-04-05 14:54:55 -07005906 private int getMessagingLayoutResource() {
5907 return R.layout.notification_template_material_messaging;
5908 }
5909
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005910 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005911 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005912 }
5913
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005914 private int getEmphasizedActionLayoutResource() {
5915 return R.layout.notification_material_action_emphasized;
5916 }
5917
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005918 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005919 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005920 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005921
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005922 private int getBackgroundColor(StandardTemplateParams p) {
5923 if (isColorized(p)) {
5924 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : getRawColor(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005925 } else {
Selim Cinekc7f5a822018-03-20 19:32:06 -07005926 return COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005927 }
5928 }
5929
Selim Cinek396caca2018-04-10 17:46:46 -07005930 /**
Selim Cinek4717d862018-04-19 09:19:15 +08005931 * Gets a neutral color that can be used for icons or similar that should not stand out.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005932 * @param p the template params to inflate this with
Selim Cinek4717d862018-04-19 09:19:15 +08005933 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005934 private int getNeutralColor(StandardTemplateParams p) {
5935 if (isColorized(p)) {
5936 return getSecondaryTextColor(p);
Selim Cinek4717d862018-04-19 09:19:15 +08005937 } else {
5938 return resolveNeutralColor();
5939 }
5940 }
5941
5942 /**
Selim Cinek396caca2018-04-10 17:46:46 -07005943 * Same as getBackgroundColor but also resolved the default color to the background.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005944 * @param p the template params to inflate this with
Selim Cinek396caca2018-04-10 17:46:46 -07005945 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005946 private int resolveBackgroundColor(StandardTemplateParams p) {
5947 int backgroundColor = getBackgroundColor(p);
Selim Cinek396caca2018-04-10 17:46:46 -07005948 if (backgroundColor == COLOR_DEFAULT) {
5949 backgroundColor = mContext.getColor(
5950 com.android.internal.R.color.notification_material_background_color);
5951 }
5952 return backgroundColor;
5953 }
5954
Anthony Chenad4d1582017-04-10 16:07:58 -07005955 private boolean shouldTintActionButtons() {
5956 return mTintActionButtons;
5957 }
5958
Selim Cinek99104832017-01-25 14:47:33 -08005959 private boolean textColorsNeedInversion() {
5960 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
5961 return false;
5962 }
5963 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
5964 return targetSdkVersion > Build.VERSION_CODES.M
5965 && targetSdkVersion < Build.VERSION_CODES.O;
5966 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005967
5968 /**
5969 * Set a color palette to be used as the background and textColors
5970 *
5971 * @param backgroundColor the color to be used as the background
5972 * @param foregroundColor the color to be used as the foreground
5973 *
5974 * @hide
5975 */
5976 public void setColorPalette(int backgroundColor, int foregroundColor) {
5977 mBackgroundColor = backgroundColor;
5978 mForegroundColor = foregroundColor;
5979 mTextColorsAreForBackground = COLOR_INVALID;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005980 ensureColors(mParams.reset().fillTextsFrom(this));
Selim Cinek5fb73f82017-04-20 16:55:38 -07005981 }
Selim Cinekac5f0272017-05-02 16:05:41 -07005982
5983 /**
Selim Cineka7679b62017-05-10 16:33:25 -07005984 * Forces all styled remoteViews to be built from scratch and not use any cached
5985 * RemoteViews.
5986 * This is needed for legacy apps that are baking in their remoteviews into the
5987 * notification.
5988 *
5989 * @hide
5990 */
5991 public void setRebuildStyledRemoteViews(boolean rebuild) {
5992 mRebuildStyledRemoteViews = rebuild;
5993 }
Selim Cinekaa9db1f2018-02-27 17:35:47 -08005994
5995 /**
5996 * Get the text that should be displayed in the statusBar when heads upped. This is
5997 * usually just the app name, but may be different depending on the style.
5998 *
5999 * @param publicMode If true, return a text that is safe to display in public.
6000 *
6001 * @hide
6002 */
6003 public CharSequence getHeadsUpStatusBarText(boolean publicMode) {
6004 if (mStyle != null && !publicMode) {
6005 CharSequence text = mStyle.getHeadsUpStatusBarText();
6006 if (!TextUtils.isEmpty(text)) {
6007 return text;
6008 }
6009 }
6010 return loadHeaderAppName();
6011 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08006012 }
6013
6014 /**
Selim Cinekd0426622017-07-11 13:19:59 +02006015 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
6016 * remote views.
6017 *
6018 * @hide
6019 */
6020 void reduceImageSizes(Context context) {
6021 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
6022 return;
6023 }
Selim Cineka8cb1262017-08-15 16:53:44 -07006024 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006025 if (mLargeIcon != null || largeIcon != null) {
6026 Resources resources = context.getResources();
6027 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07006028 int maxWidth = resources.getDimensionPixelSize(isLowRam
6029 ? R.dimen.notification_right_icon_size_low_ram
6030 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006031 int maxHeight = maxWidth;
6032 if (MediaStyle.class.equals(style)
6033 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006034 maxHeight = resources.getDimensionPixelSize(isLowRam
6035 ? R.dimen.notification_media_image_max_height_low_ram
6036 : R.dimen.notification_media_image_max_height);
6037 maxWidth = resources.getDimensionPixelSize(isLowRam
6038 ? R.dimen.notification_media_image_max_width_low_ram
6039 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006040 }
6041 if (mLargeIcon != null) {
6042 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
6043 }
6044 if (largeIcon != null) {
6045 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
6046 }
6047 }
Selim Cineka8cb1262017-08-15 16:53:44 -07006048 reduceImageSizesForRemoteView(contentView, context, isLowRam);
6049 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
6050 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02006051 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
6052 }
6053
Selim Cineka8cb1262017-08-15 16:53:44 -07006054 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
6055 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02006056 if (remoteView != null) {
6057 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006058 int maxWidth = resources.getDimensionPixelSize(isLowRam
6059 ? R.dimen.notification_custom_view_max_image_width_low_ram
6060 : R.dimen.notification_custom_view_max_image_width);
6061 int maxHeight = resources.getDimensionPixelSize(isLowRam
6062 ? R.dimen.notification_custom_view_max_image_height_low_ram
6063 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02006064 remoteView.reduceImageSizes(maxWidth, maxHeight);
6065 }
6066 }
6067
6068 /**
Selim Cinek22714f12017-04-13 16:23:53 -07006069 * @return whether this notification is a foreground service notification
Gus Prevasb0c1a462018-11-05 11:06:15 -05006070 * @hide
Selim Cinek7b9605b2017-01-19 17:36:00 -08006071 */
Gus Prevasb0c1a462018-11-05 11:06:15 -05006072 public boolean isForegroundService() {
Selim Cinek22714f12017-04-13 16:23:53 -07006073 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08006074 }
6075
6076 /**
Selim Cinek99104832017-01-25 14:47:33 -08006077 * @return whether this notification has a media session attached
6078 * @hide
6079 */
6080 public boolean hasMediaSession() {
6081 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
6082 }
6083
6084 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006085 * @return the style class of this notification
6086 * @hide
6087 */
6088 public Class<? extends Notification.Style> getNotificationStyle() {
6089 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
6090
6091 if (!TextUtils.isEmpty(templateClass)) {
6092 return Notification.getNotificationStyleClass(templateClass);
6093 }
6094 return null;
6095 }
6096
6097 /**
Selim Cinek22714f12017-04-13 16:23:53 -07006098 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08006099 *
6100 * @hide
6101 */
6102 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07006103 if (isColorizedMedia()) {
6104 return true;
6105 }
Julia Reynolds4db59552017-06-30 13:34:01 -04006106 return extras.getBoolean(EXTRA_COLORIZED)
6107 && (hasColorizedPermission() || isForegroundService());
6108 }
6109
6110 /**
6111 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
6112 * permission. The permission is checked when a notification is enqueued.
6113 */
6114 private boolean hasColorizedPermission() {
6115 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07006116 }
6117
6118 /**
6119 * @return true if this notification is colorized and it is a media notification
6120 *
6121 * @hide
6122 */
6123 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08006124 Class<? extends Style> style = getNotificationStyle();
6125 if (MediaStyle.class.equals(style)) {
6126 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
6127 if ((colorized == null || colorized) && hasMediaSession()) {
6128 return true;
6129 }
6130 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6131 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
6132 return true;
6133 }
6134 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07006135 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006136 }
6137
Selim Cinek0847acd2017-04-24 19:48:29 -07006138
6139 /**
6140 * @return true if this is a media notification
6141 *
6142 * @hide
6143 */
6144 public boolean isMediaNotification() {
6145 Class<? extends Style> style = getNotificationStyle();
6146 if (MediaStyle.class.equals(style)) {
6147 return true;
6148 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6149 return true;
6150 }
6151 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006152 }
6153
Selim Cinek279fa862016-06-14 10:57:25 -07006154 private boolean hasLargeIcon() {
6155 return mLargeIcon != null || largeIcon != null;
6156 }
6157
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006158 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006159 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006160 * @hide
6161 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07006162 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006163 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
6164 }
6165
6166 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006167 * @return true if the notification will show a chronometer; false otherwise
6168 * @hide
6169 */
6170 public boolean showsChronometer() {
6171 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
6172 }
6173
6174 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04006175 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05006176 */
6177 @SystemApi
6178 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
6179 Class<? extends Style>[] classes = new Class[] {
6180 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
6181 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
6182 MessagingStyle.class };
6183 for (Class<? extends Style> innerClass : classes) {
6184 if (templateClass.equals(innerClass.getName())) {
6185 return innerClass;
6186 }
6187 }
6188 return null;
6189 }
6190
6191 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006192 * An object that can apply a rich notification style to a {@link Notification.Builder}
6193 * object.
6194 */
Griff Hazendfcb0802014-02-11 12:00:00 -08006195 public static abstract class Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07006196
6197 /**
6198 * The number of items allowed simulatanously in the remote input history.
6199 * @hide
6200 */
6201 static final int MAX_REMOTE_INPUT_HISTORY_LINES = 3;
Chris Wrend6297db2012-05-03 16:20:13 -04006202 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006203
6204 /**
6205 * @hide
6206 */
6207 protected CharSequence mSummaryText = null;
6208
6209 /**
6210 * @hide
6211 */
6212 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04006213
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006214 protected Builder mBuilder;
6215
Chris Wrend6297db2012-05-03 16:20:13 -04006216 /**
6217 * Overrides ContentTitle in the big form of the template.
6218 * This defaults to the value passed to setContentTitle().
6219 */
6220 protected void internalSetBigContentTitle(CharSequence title) {
6221 mBigContentTitle = title;
6222 }
6223
6224 /**
6225 * Set the first line of text after the detail section in the big form of the template.
6226 */
6227 protected void internalSetSummaryText(CharSequence cs) {
6228 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04006229 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04006230 }
6231
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006232 public void setBuilder(Builder builder) {
6233 if (mBuilder != builder) {
6234 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07006235 if (mBuilder != null) {
6236 mBuilder.setStyle(this);
6237 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006238 }
6239 }
6240
Chris Wrend6297db2012-05-03 16:20:13 -04006241 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006242 if (mBuilder == null) {
6243 throw new IllegalArgumentException("Style requires a valid Builder object");
6244 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006245 }
Chris Wrend6297db2012-05-03 16:20:13 -04006246
6247 protected RemoteViews getStandardView(int layoutId) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006248 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder);
6249 return getStandardView(layoutId, p, null);
Selim Cinek384804b2018-04-18 14:31:07 +08006250 }
6251
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006252
Selim Cinek384804b2018-04-18 14:31:07 +08006253 /**
6254 * Get the standard view for this style.
6255 *
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006256 * @param layoutId The layout id to use.
6257 * @param p the params for this inflation.
Selim Cinek384804b2018-04-18 14:31:07 +08006258 * @param result The result where template bind information is saved.
6259 * @return A remoteView for this style.
6260 * @hide
6261 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006262 protected RemoteViews getStandardView(int layoutId, StandardTemplateParams p,
6263 TemplateBindResult result) {
Chris Wrend6297db2012-05-03 16:20:13 -04006264 checkBuilder();
6265
6266 if (mBigContentTitle != null) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006267 p.title = mBigContentTitle;
Chris Wrend6297db2012-05-03 16:20:13 -04006268 }
6269
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006270 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId, p,
6271 result);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006272
Chris Wrend6297db2012-05-03 16:20:13 -04006273 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
6274 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04006275 } else {
6276 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04006277 }
6278
Chris Wrend6297db2012-05-03 16:20:13 -04006279 return contentView;
6280 }
6281
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006282 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006283 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006284 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08006285 *
6286 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006287 * @hide
6288 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08006289 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006290 return null;
6291 }
6292
6293 /**
6294 * Construct a Style-specific RemoteViews for the final big notification layout.
6295 * @hide
6296 */
6297 public RemoteViews makeBigContentView() {
6298 return null;
6299 }
6300
6301 /**
6302 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08006303 *
6304 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006305 * @hide
6306 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006307 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006308 return null;
6309 }
6310
6311 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006312 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006313 * @hide
6314 */
6315 public void addExtras(Bundle extras) {
6316 if (mSummaryTextSet) {
6317 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
6318 }
6319 if (mBigContentTitle != null) {
6320 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
6321 }
Chris Wren91ad5632013-06-05 15:05:57 -04006322 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006323 }
6324
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006325 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006326 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006327 * @hide
6328 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02006329 protected void restoreFromExtras(Bundle extras) {
6330 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
6331 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
6332 mSummaryTextSet = true;
6333 }
6334 if (extras.containsKey(EXTRA_TITLE_BIG)) {
6335 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
6336 }
6337 }
6338
6339
6340 /**
6341 * @hide
6342 */
6343 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006344 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006345 return wip;
6346 }
6347
Daniel Sandler0ec46202015-06-24 01:27:05 -04006348 /**
6349 * @hide
6350 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006351 public void purgeResources() {}
6352
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006353 /**
6354 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
6355 * attached to.
6356 *
6357 * @return the fully constructed Notification.
6358 */
6359 public Notification build() {
6360 checkBuilder();
6361 return mBuilder.build();
6362 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006363
6364 /**
6365 * @hide
6366 * @return true if the style positions the progress bar on the second line; false if the
6367 * style hides the progress bar
6368 */
6369 protected boolean hasProgress() {
6370 return true;
6371 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006372
6373 /**
6374 * @hide
6375 * @return Whether we should put the summary be put into the notification header
6376 */
6377 public boolean hasSummaryInHeader() {
6378 return true;
6379 }
Selim Cinek593610c2016-02-16 18:42:57 -08006380
6381 /**
6382 * @hide
6383 * @return Whether custom content views are displayed inline in the style
6384 */
6385 public boolean displayCustomViewInline() {
6386 return false;
6387 }
Selim Cinekd0426622017-07-11 13:19:59 +02006388
6389 /**
6390 * Reduces the image sizes contained in this style.
6391 *
6392 * @hide
6393 */
6394 public void reduceImageSizes(Context context) {
6395 }
Selim Cinek90343862018-02-01 11:07:11 -08006396
6397 /**
6398 * Validate that this style was properly composed. This is called at build time.
6399 * @hide
6400 */
6401 public void validate(Context context) {
6402 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006403
6404 /**
6405 * @hide
6406 */
6407 public abstract boolean areNotificationsVisiblyDifferent(Style other);
Selim Cinekc7f5a822018-03-20 19:32:06 -07006408
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006409 /**
koprivaa1a78482018-10-09 10:09:23 -07006410 * @return the text that should be displayed in the statusBar when heads-upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006411 * If {@code null} is returned, the default implementation will be used.
6412 *
6413 * @hide
6414 */
6415 public CharSequence getHeadsUpStatusBarText() {
6416 return null;
6417 }
Joe Onorato46439ce2010-11-19 13:56:21 -08006418 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006419
6420 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006421 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08006422 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006423 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006424 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006425 * Notification notif = new Notification.Builder(mContext)
6426 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
6427 * .setContentText(subject)
6428 * .setSmallIcon(R.drawable.new_post)
6429 * .setLargeIcon(aBitmap)
6430 * .setStyle(new Notification.BigPictureStyle()
6431 * .bigPicture(aBigBitmap))
6432 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006433 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006434 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006435 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006436 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006437 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006438 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006439 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006440 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006441
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006442 public BigPictureStyle() {
6443 }
6444
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006445 /**
6446 * @deprecated use {@code BigPictureStyle()}.
6447 */
6448 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006449 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006450 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006451 }
6452
Chris Wrend6297db2012-05-03 16:20:13 -04006453 /**
6454 * Overrides ContentTitle in the big form of the template.
6455 * This defaults to the value passed to setContentTitle().
6456 */
6457 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006458 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006459 return this;
6460 }
6461
6462 /**
6463 * Set the first line of text after the detail section in the big form of the template.
6464 */
6465 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006466 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006467 return this;
6468 }
6469
Chris Wren0bd664d2012-08-01 13:56:56 -04006470 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05006471 * @hide
6472 */
6473 public Bitmap getBigPicture() {
6474 return mPicture;
6475 }
6476
6477 /**
Chris Wren0bd664d2012-08-01 13:56:56 -04006478 * Provide the bitmap to be used as the payload for the BigPicture notification.
6479 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006480 public BigPictureStyle bigPicture(Bitmap b) {
6481 mPicture = b;
6482 return this;
6483 }
6484
Chris Wren3745a3d2012-05-22 15:11:52 -04006485 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04006486 * Override the large icon when the big notification is shown.
6487 */
6488 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04006489 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
6490 }
6491
6492 /**
6493 * Override the large icon when the big notification is shown.
6494 */
6495 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04006496 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006497 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006498 return this;
6499 }
6500
Riley Andrews0394a0c2015-11-03 23:36:52 -08006501 /** @hide */
6502 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
6503
Daniel Sandler0ec46202015-06-24 01:27:05 -04006504 /**
6505 * @hide
6506 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006507 @Override
6508 public void purgeResources() {
6509 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08006510 if (mPicture != null &&
6511 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08006512 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006513 mPicture = mPicture.createAshmemBitmap();
6514 }
6515 if (mBigLargeIcon != null) {
6516 mBigLargeIcon.convertToAshmem();
6517 }
6518 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01006519
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006520 /**
6521 * @hide
6522 */
Selim Cinekd0426622017-07-11 13:19:59 +02006523 @Override
6524 public void reduceImageSizes(Context context) {
6525 super.reduceImageSizes(context);
6526 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006527 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006528 if (mPicture != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006529 int maxPictureWidth = resources.getDimensionPixelSize(isLowRam
6530 ? R.dimen.notification_big_picture_max_height_low_ram
6531 : R.dimen.notification_big_picture_max_height);
6532 int maxPictureHeight = resources.getDimensionPixelSize(isLowRam
6533 ? R.dimen.notification_big_picture_max_width_low_ram
6534 : R.dimen.notification_big_picture_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006535 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
6536 }
6537 if (mBigLargeIcon != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006538 int rightIconSize = resources.getDimensionPixelSize(isLowRam
6539 ? R.dimen.notification_right_icon_size_low_ram
6540 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006541 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
6542 }
6543 }
6544
6545 /**
6546 * @hide
6547 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006548 public RemoteViews makeBigContentView() {
6549 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01006550 // This covers the following cases:
6551 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006552 // mN.mLargeIcon
6553 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04006554 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07006555 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006556 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006557 oldLargeIcon = mBuilder.mN.mLargeIcon;
6558 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006559 // The legacy largeIcon might not allow us to clear the image, as it's taken in
6560 // replacement if the other one is null. Because we're restoring these legacy icons
6561 // for old listeners, this is in general non-null.
6562 largeIconLegacy = mBuilder.mN.largeIcon;
6563 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006564 }
6565
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006566 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder);
Selim Cinek384804b2018-04-18 14:31:07 +08006567 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource(),
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006568 p, null /* result */);
Selim Cinek03d0d652015-11-13 13:18:09 -05006569 if (mSummaryTextSet) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006570 contentView.setTextViewText(R.id.text, mBuilder.processTextSpans(
6571 mBuilder.processLegacyText(mSummaryText)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006572 mBuilder.setTextViewColorSecondary(contentView, R.id.text, p);
Selim Cinekc848c3a2016-01-13 15:27:30 -08006573 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05006574 }
Selim Cinek279fa862016-06-14 10:57:25 -07006575 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08006576
Christoph Studer5c510ee2014-12-15 16:32:27 +01006577 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006578 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006579 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006580 }
6581
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006582 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006583 return contentView;
6584 }
6585
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006586 /**
6587 * @hide
6588 */
6589 public void addExtras(Bundle extras) {
6590 super.addExtras(extras);
6591
6592 if (mBigLargeIconSet) {
6593 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
6594 }
6595 extras.putParcelable(EXTRA_PICTURE, mPicture);
6596 }
6597
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006598 /**
6599 * @hide
6600 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006601 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02006602 protected void restoreFromExtras(Bundle extras) {
6603 super.restoreFromExtras(extras);
6604
6605 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01006606 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02006607 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04006608 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006609 mPicture = extras.getParcelable(EXTRA_PICTURE);
6610 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006611
6612 /**
6613 * @hide
6614 */
6615 @Override
6616 public boolean hasSummaryInHeader() {
6617 return false;
6618 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006619
6620 /**
6621 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006622 * Note that we aren't actually comparing the contents of the bitmaps here, so this
6623 * is only doing a cursory inspection. Bitmaps of equal size will appear the same.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006624 */
6625 @Override
6626 public boolean areNotificationsVisiblyDifferent(Style other) {
6627 if (other == null || getClass() != other.getClass()) {
6628 return true;
6629 }
6630 BigPictureStyle otherS = (BigPictureStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006631 return areBitmapsObviouslyDifferent(getBigPicture(), otherS.getBigPicture());
6632 }
6633
6634 private static boolean areBitmapsObviouslyDifferent(Bitmap a, Bitmap b) {
6635 if (a == b) {
6636 return false;
6637 }
6638 if (a == null || b == null) {
6639 return true;
6640 }
6641 return a.getWidth() != b.getWidth()
6642 || a.getHeight() != b.getHeight()
6643 || a.getConfig() != b.getConfig()
6644 || a.getGenerationId() != b.getGenerationId();
Julia Reynolds7217dc92018-03-07 12:12:09 -05006645 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006646 }
6647
6648 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006649 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08006650 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006651 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006652 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006653 * Notification notif = new Notification.Builder(mContext)
6654 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
6655 * .setContentText(subject)
6656 * .setSmallIcon(R.drawable.new_mail)
6657 * .setLargeIcon(aBitmap)
6658 * .setStyle(new Notification.BigTextStyle()
6659 * .bigText(aVeryLongString))
6660 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006661 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006662 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006663 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006664 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006665 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006666
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006667 private CharSequence mBigText;
6668
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006669 public BigTextStyle() {
6670 }
6671
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006672 /**
6673 * @deprecated use {@code BigTextStyle()}.
6674 */
6675 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006676 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006677 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006678 }
6679
Chris Wrend6297db2012-05-03 16:20:13 -04006680 /**
6681 * Overrides ContentTitle in the big form of the template.
6682 * This defaults to the value passed to setContentTitle().
6683 */
6684 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006685 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006686 return this;
6687 }
6688
6689 /**
6690 * Set the first line of text after the detail section in the big form of the template.
6691 */
6692 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006693 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006694 return this;
6695 }
6696
Chris Wren0bd664d2012-08-01 13:56:56 -04006697 /**
6698 * Provide the longer text to be displayed in the big form of the
6699 * template in place of the content text.
6700 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006701 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006702 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006703 return this;
6704 }
6705
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006706 /**
6707 * @hide
6708 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05006709 public CharSequence getBigText() {
6710 return mBigText;
6711 }
6712
6713 /**
6714 * @hide
6715 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006716 public void addExtras(Bundle extras) {
6717 super.addExtras(extras);
6718
Christoph Studer4600f9b2014-07-22 22:44:43 +02006719 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
6720 }
6721
6722 /**
6723 * @hide
6724 */
6725 @Override
6726 protected void restoreFromExtras(Bundle extras) {
6727 super.restoreFromExtras(extras);
6728
6729 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006730 }
6731
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006732 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006733 * @param increasedHeight true if this layout be created with an increased height.
6734 *
6735 * @hide
6736 */
6737 @Override
6738 public RemoteViews makeContentView(boolean increasedHeight) {
6739 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006740 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006741 mBuilder.mActions = new ArrayList<>();
6742 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006743 mBuilder.mActions = mBuilder.mOriginalActions;
6744 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006745 return remoteViews;
6746 }
6747 return super.makeContentView(increasedHeight);
6748 }
6749
6750 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006751 * @hide
6752 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006753 @Override
6754 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
6755 if (increasedHeight && mBuilder.mActions.size() > 0) {
6756 return makeBigContentView();
6757 }
6758 return super.makeHeadsUpContentView(increasedHeight);
6759 }
6760
6761 /**
6762 * @hide
6763 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006764 public RemoteViews makeBigContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006765 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder).text(null);
Selim Cinek384804b2018-04-18 14:31:07 +08006766 TemplateBindResult result = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006767 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource(), p,
6768 result);
Selim Cinek1c72fa02018-04-23 18:00:54 +08006769 contentView.setInt(R.id.big_text, "setImageEndMargin", result.getIconMarginEnd());
Joe Malin8d40d042012-11-05 11:36:40 -08006770
Selim Cinek3a2c4b92015-12-17 17:01:17 -08006771 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07006772 if (TextUtils.isEmpty(bigTextText)) {
6773 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
6774 // experience
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006775 bigTextText = mBuilder.processLegacyText(
6776 mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT));
Selim Cinek75998782016-04-26 10:39:17 -07006777 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006778 contentView.setTextViewText(R.id.big_text, mBuilder.processTextSpans(bigTextText));
6779 mBuilder.setTextViewColorSecondary(contentView, R.id.big_text, p);
6780 contentView.setViewVisibility(R.id.big_text,
6781 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08006782 contentView.setBoolean(R.id.big_text, "setHasImage",
6783 result.isRightIconContainerVisible());
Selim Cinek4fb12d32015-11-19 18:10:48 -08006784
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006785 return contentView;
6786 }
6787
Julia Reynolds7217dc92018-03-07 12:12:09 -05006788 /**
6789 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006790 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006791 */
6792 @Override
6793 public boolean areNotificationsVisiblyDifferent(Style other) {
6794 if (other == null || getClass() != other.getClass()) {
6795 return true;
6796 }
6797 BigTextStyle newS = (BigTextStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006798 return !Objects.equals(String.valueOf(getBigText()), String.valueOf(newS.getBigText()));
Julia Reynolds7217dc92018-03-07 12:12:09 -05006799 }
6800
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006801 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006802
6803 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006804 * Helper class for generating large-format notifications that include multiple back-and-forth
6805 * messages of varying types between any number of people.
6806 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006807 * <p>
Alex Hillsfc737de2016-03-23 17:33:02 -04006808 * If the platform does not provide large-format notifications, this method has no effect. The
6809 * user will always see the normal notification view.
Selim Cinekce8794f2018-05-23 16:46:05 -07006810 *
6811 * <p>
6812 * If the app is targeting Android P and above, it is required to use the {@link Person}
6813 * class in order to get an optimal rendering of the notification and its avatars. For
6814 * conversations involving multiple people, the app should also make sure that it marks the
6815 * conversation as a group with {@link #setGroupConversation(boolean)}.
6816 *
6817 * <p>
6818 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior.
6819 * Here's an example of how this may be used:
Alex Hillsfc737de2016-03-23 17:33:02 -04006820 * <pre class="prettyprint">
6821 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006822 * Person user = new Person.Builder().setIcon(userIcon).setName(userName).build();
6823 * MessagingStyle style = new MessagingStyle(user)
6824 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getPerson())
6825 * .addMessage(messages[2].getText(), messages[2].getTime(), messages[2].getPerson())
6826 * .setGroupConversation(hasMultiplePeople());
6827 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006828 * Notification noti = new Notification.Builder()
Selim Cinekce8794f2018-05-23 16:46:05 -07006829 * .setContentTitle(&quot;2 new messages with &quot; + sender.toString())
Alex Hillsfc737de2016-03-23 17:33:02 -04006830 * .setContentText(subject)
6831 * .setSmallIcon(R.drawable.new_message)
6832 * .setLargeIcon(aBitmap)
Selim Cinekce8794f2018-05-23 16:46:05 -07006833 * .setStyle(style)
Alex Hillsfc737de2016-03-23 17:33:02 -04006834 * .build();
6835 * </pre>
6836 */
6837 public static class MessagingStyle extends Style {
6838
6839 /**
6840 * The maximum number of messages that will be retained in the Notification itself (the
6841 * number displayed is up to the platform).
6842 */
6843 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
6844
Selim Cinekcb8b9852017-12-15 18:01:52 -08006845 @NonNull Person mUser;
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006846 @Nullable CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04006847 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08006848 List<Message> mHistoricMessages = new ArrayList<>();
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006849 boolean mIsGroupConversation;
Alex Hillsfc737de2016-03-23 17:33:02 -04006850
6851 MessagingStyle() {
6852 }
6853
6854 /**
Alex Hillsfd590442016-10-07 09:52:44 -04006855 * @param userDisplayName Required - the name to be displayed for any replies sent by the
6856 * user before the posting app reposts the notification with those messages after they've
6857 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04006858 * {@link #addMessage(Notification.MessagingStyle.Message)}
Selim Cinekcb8b9852017-12-15 18:01:52 -08006859 *
6860 * @deprecated use {@code MessagingStyle(Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006861 */
Alex Hillsfd590442016-10-07 09:52:44 -04006862 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Selim Cinek9acd6732018-03-23 16:39:02 -07006863 this(new Person.Builder().setName(userDisplayName).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006864 }
6865
6866 /**
6867 * @param user Required - The person displayed for any messages that are sent by the
6868 * user. Any messages added with {@link #addMessage(Notification.MessagingStyle.Message)}
6869 * who don't have a Person associated with it will be displayed as if they were sent
Selim Cinek90343862018-02-01 11:07:11 -08006870 * by this user. The user also needs to have a valid name associated with it, which will
6871 * be enforced starting in Android P.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006872 */
6873 public MessagingStyle(@NonNull Person user) {
6874 mUser = user;
Selim Cinek90343862018-02-01 11:07:11 -08006875 }
6876
6877 /**
6878 * Validate that this style was properly composed. This is called at build time.
6879 * @hide
6880 */
6881 @Override
6882 public void validate(Context context) {
6883 super.validate(context);
6884 if (context.getApplicationInfo().targetSdkVersion
6885 >= Build.VERSION_CODES.P && (mUser == null || mUser.getName() == null)) {
6886 throw new RuntimeException("User must be valid and have a name.");
Selim Cinekcb8b9852017-12-15 18:01:52 -08006887 }
6888 }
6889
6890 /**
koprivaa1a78482018-10-09 10:09:23 -07006891 * @return the text that should be displayed in the statusBar when heads upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006892 * If {@code null} is returned, the default implementation will be used.
6893 *
6894 * @hide
6895 */
6896 @Override
6897 public CharSequence getHeadsUpStatusBarText() {
6898 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
6899 ? super.mBigContentTitle
6900 : mConversationTitle;
6901 if (!TextUtils.isEmpty(conversationTitle) && !hasOnlyWhiteSpaceSenders()) {
6902 return conversationTitle;
6903 }
6904 return null;
6905 }
6906
6907 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08006908 * @return the user to be displayed for any replies sent by the user
6909 */
Selim Cinekeb53c222018-04-06 12:56:09 -07006910 @NonNull
Selim Cinekcb8b9852017-12-15 18:01:52 -08006911 public Person getUser() {
6912 return mUser;
Alex Hillsfc737de2016-03-23 17:33:02 -04006913 }
6914
6915 /**
6916 * Returns the name to be displayed for any replies sent by the user
Selim Cinekcb8b9852017-12-15 18:01:52 -08006917 *
6918 * @deprecated use {@link #getUser()} instead
Alex Hillsfc737de2016-03-23 17:33:02 -04006919 */
6920 public CharSequence getUserDisplayName() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006921 return mUser.getName();
Alex Hillsfc737de2016-03-23 17:33:02 -04006922 }
6923
6924 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006925 * Sets the title to be displayed on this conversation. May be set to {@code null}.
6926 *
Kodlee Yin14656422017-12-22 17:00:46 -08006927 * <p>This API's behavior was changed in SDK version {@link Build.VERSION_CODES#P}. If your
6928 * application's target version is less than {@link Build.VERSION_CODES#P}, setting a
6929 * conversation title to a non-null value will make {@link #isGroupConversation()} return
6930 * {@code true} and passing {@code null} will make it return {@code false}. In
6931 * {@link Build.VERSION_CODES#P} and beyond, use {@link #setGroupConversation(boolean)}
6932 * to set group conversation status.
6933 *
6934 * @param conversationTitle Title displayed for this conversation
6935 * @return this object for method chaining
Alex Hillsfc737de2016-03-23 17:33:02 -04006936 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006937 public MessagingStyle setConversationTitle(@Nullable CharSequence conversationTitle) {
Alex Hillsfc737de2016-03-23 17:33:02 -04006938 mConversationTitle = conversationTitle;
6939 return this;
6940 }
6941
6942 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006943 * Return the title to be displayed on this conversation. May return {@code null}.
Alex Hillsfc737de2016-03-23 17:33:02 -04006944 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006945 @Nullable
Alex Hillsfc737de2016-03-23 17:33:02 -04006946 public CharSequence getConversationTitle() {
6947 return mConversationTitle;
6948 }
6949
6950 /**
6951 * Adds a message for display by this notification. Convenience call for a simple
6952 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6953 * @param text A {@link CharSequence} to be displayed as the message content
6954 * @param timestamp Time at which the message arrived
6955 * @param sender A {@link CharSequence} to be used for displaying the name of the
6956 * sender. Should be <code>null</code> for messages by the current user, in which case
6957 * the platform will insert {@link #getUserDisplayName()}.
6958 * Should be unique amongst all individuals in the conversation, and should be
6959 * consistent during re-posts of the notification.
6960 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07006961 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Alex Hillsfc737de2016-03-23 17:33:02 -04006962 *
6963 * @return this object for method chaining
Selim Cinekcb8b9852017-12-15 18:01:52 -08006964 *
6965 * @deprecated use {@link #addMessage(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006966 */
6967 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006968 return addMessage(text, timestamp,
Selim Cinek9acd6732018-03-23 16:39:02 -07006969 sender == null ? null : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006970 }
6971
6972 /**
6973 * Adds a message for display by this notification. Convenience call for a simple
6974 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6975 * @param text A {@link CharSequence} to be displayed as the message content
6976 * @param timestamp Time at which the message arrived
6977 * @param sender The {@link Person} who sent the message.
6978 * Should be <code>null</code> for messages by the current user, in which case
6979 * the platform will insert the user set in {@code MessagingStyle(Person)}.
6980 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07006981 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Selim Cinekcb8b9852017-12-15 18:01:52 -08006982 *
6983 * @return this object for method chaining
6984 */
Selim Cinekeb53c222018-04-06 12:56:09 -07006985 public MessagingStyle addMessage(@NonNull CharSequence text, long timestamp,
6986 @Nullable Person sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08006987 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04006988 }
6989
6990 /**
6991 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08006992 *
6993 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6994 * the newest last.
6995 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006996 * @param message The {@link Message} to be displayed
6997 * @return this object for method chaining
6998 */
6999 public MessagingStyle addMessage(Message message) {
7000 mMessages.add(message);
7001 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
7002 mMessages.remove(0);
7003 }
7004 return this;
7005 }
7006
7007 /**
Adrian Roos437cd562017-01-18 15:47:03 -08007008 * Adds a {@link Message} for historic context in this notification.
7009 *
7010 * <p>Messages should be added as historic if they are not the main subject of the
7011 * notification but may give context to a conversation. The system may choose to present
7012 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
7013 *
7014 * <p>The messages should be added in chronologic order, i.e. the oldest first,
7015 * the newest last.
7016 *
7017 * @param message The historic {@link Message} to be added
7018 * @return this object for method chaining
7019 */
7020 public MessagingStyle addHistoricMessage(Message message) {
7021 mHistoricMessages.add(message);
7022 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
7023 mHistoricMessages.remove(0);
7024 }
7025 return this;
7026 }
7027
7028 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007029 * Gets the list of {@code Message} objects that represent the notification
7030 */
7031 public List<Message> getMessages() {
7032 return mMessages;
7033 }
7034
7035 /**
Adrian Roos437cd562017-01-18 15:47:03 -08007036 * Gets the list of historic {@code Message}s in the notification.
7037 */
7038 public List<Message> getHistoricMessages() {
7039 return mHistoricMessages;
7040 }
7041
7042 /**
Selim Cinekce8794f2018-05-23 16:46:05 -07007043 * Sets whether this conversation notification represents a group. If the app is targeting
7044 * Android P, this is required if the app wants to display the largeIcon set with
7045 * {@link Notification.Builder#setLargeIcon(Bitmap)}, otherwise it will be hidden.
Kodlee Yin14656422017-12-22 17:00:46 -08007046 *
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007047 * @param isGroupConversation {@code true} if the conversation represents a group,
7048 * {@code false} otherwise.
7049 * @return this object for method chaining
7050 */
7051 public MessagingStyle setGroupConversation(boolean isGroupConversation) {
7052 mIsGroupConversation = isGroupConversation;
7053 return this;
7054 }
7055
7056 /**
Kodlee Yin14656422017-12-22 17:00:46 -08007057 * Returns {@code true} if this notification represents a group conversation, otherwise
7058 * {@code false}.
7059 *
7060 * <p> If the application that generated this {@link MessagingStyle} targets an SDK version
7061 * less than {@link Build.VERSION_CODES#P}, this method becomes dependent on whether or
7062 * not the conversation title is set; returning {@code true} if the conversation title is
7063 * a non-null value, or {@code false} otherwise. From {@link Build.VERSION_CODES#P} forward,
7064 * this method returns what's set by {@link #setGroupConversation(boolean)} allowing for
7065 * named, non-group conversations.
7066 *
7067 * @see #setConversationTitle(CharSequence)
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007068 */
7069 public boolean isGroupConversation() {
Kodlee Yin14656422017-12-22 17:00:46 -08007070 // When target SDK version is < P, a non-null conversation title dictates if this is
7071 // as group conversation.
7072 if (mBuilder != null
7073 && mBuilder.mContext.getApplicationInfo().targetSdkVersion
7074 < Build.VERSION_CODES.P) {
7075 return mConversationTitle != null;
7076 }
7077
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007078 return mIsGroupConversation;
7079 }
7080
7081 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007082 * @hide
7083 */
7084 @Override
7085 public void addExtras(Bundle extras) {
7086 super.addExtras(extras);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007087 if (mUser != null) {
7088 // For legacy usages
7089 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUser.getName());
7090 extras.putParcelable(EXTRA_MESSAGING_PERSON, mUser);
Alex Hillsfc737de2016-03-23 17:33:02 -04007091 }
7092 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007093 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04007094 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007095 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
7096 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04007097 }
Adrian Roos437cd562017-01-18 15:47:03 -08007098 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
7099 Message.getBundleArrayForMessages(mHistoricMessages));
7100 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007101
7102 fixTitleAndTextExtras(extras);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007103 extras.putBoolean(EXTRA_IS_GROUP_CONVERSATION, mIsGroupConversation);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007104 }
7105
7106 private void fixTitleAndTextExtras(Bundle extras) {
7107 Message m = findLatestIncomingMessage();
7108 CharSequence text = (m == null) ? null : m.mText;
7109 CharSequence sender = m == null ? null
Selim Cinekcb8b9852017-12-15 18:01:52 -08007110 : m.mSender == null || TextUtils.isEmpty(m.mSender.getName())
7111 ? mUser.getName() : m.mSender.getName();
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007112 CharSequence title;
7113 if (!TextUtils.isEmpty(mConversationTitle)) {
7114 if (!TextUtils.isEmpty(sender)) {
7115 BidiFormatter bidi = BidiFormatter.getInstance();
7116 title = mBuilder.mContext.getString(
7117 com.android.internal.R.string.notification_messaging_title_template,
Selim Cinekcb8b9852017-12-15 18:01:52 -08007118 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(sender));
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007119 } else {
7120 title = mConversationTitle;
7121 }
7122 } else {
7123 title = sender;
7124 }
7125
7126 if (title != null) {
7127 extras.putCharSequence(EXTRA_TITLE, title);
7128 }
7129 if (text != null) {
7130 extras.putCharSequence(EXTRA_TEXT, text);
7131 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007132 }
7133
7134 /**
7135 * @hide
7136 */
7137 @Override
7138 protected void restoreFromExtras(Bundle extras) {
7139 super.restoreFromExtras(extras);
7140
Selim Cinekcb8b9852017-12-15 18:01:52 -08007141 mUser = extras.getParcelable(EXTRA_MESSAGING_PERSON);
7142 if (mUser == null) {
7143 CharSequence displayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
Selim Cinek9acd6732018-03-23 16:39:02 -07007144 mUser = new Person.Builder().setName(displayName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007145 }
Adrian Roos96b7e202016-05-17 13:50:38 -07007146 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08007147 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007148 mMessages = Message.getMessagesFromBundleArray(messages);
Adrian Roos437cd562017-01-18 15:47:03 -08007149 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007150 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007151 mIsGroupConversation = extras.getBoolean(EXTRA_IS_GROUP_CONVERSATION);
Alex Hillsfc737de2016-03-23 17:33:02 -04007152 }
7153
7154 /**
7155 * @hide
7156 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07007157 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007158 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07007159 mBuilder.mOriginalActions = mBuilder.mActions;
7160 mBuilder.mActions = new ArrayList<>();
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007161 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007162 false /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007163 mBuilder.mActions = mBuilder.mOriginalActions;
7164 mBuilder.mOriginalActions = null;
7165 return remoteViews;
Adrian Roosc1a80b02016-04-05 14:54:55 -07007166 }
7167
Julia Reynolds7217dc92018-03-07 12:12:09 -05007168 /**
7169 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04007170 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05007171 */
7172 @Override
7173 public boolean areNotificationsVisiblyDifferent(Style other) {
7174 if (other == null || getClass() != other.getClass()) {
7175 return true;
7176 }
7177 MessagingStyle newS = (MessagingStyle) other;
7178 List<MessagingStyle.Message> oldMs = getMessages();
7179 List<MessagingStyle.Message> newMs = newS.getMessages();
7180
Dan Sandler7d67bd42018-05-15 14:06:38 -04007181 if (oldMs == null || newMs == null) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007182 newMs = new ArrayList<>();
7183 }
7184
7185 int n = oldMs.size();
7186 if (n != newMs.size()) {
7187 return true;
7188 }
7189 for (int i = 0; i < n; i++) {
7190 MessagingStyle.Message oldM = oldMs.get(i);
7191 MessagingStyle.Message newM = newMs.get(i);
Dan Sandler7d67bd42018-05-15 14:06:38 -04007192 if (!Objects.equals(
7193 String.valueOf(oldM.getText()),
7194 String.valueOf(newM.getText()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007195 return true;
7196 }
7197 if (!Objects.equals(oldM.getDataUri(), newM.getDataUri())) {
7198 return true;
7199 }
Dan Sandler7d67bd42018-05-15 14:06:38 -04007200 String oldSender = String.valueOf(oldM.getSenderPerson() == null
7201 ? oldM.getSender()
7202 : oldM.getSenderPerson().getName());
7203 String newSender = String.valueOf(newM.getSenderPerson() == null
7204 ? newM.getSender()
7205 : newM.getSenderPerson().getName());
Julia Reynolds7217dc92018-03-07 12:12:09 -05007206 if (!Objects.equals(oldSender, newSender)) {
7207 return true;
7208 }
7209
7210 String oldKey = oldM.getSenderPerson() == null
7211 ? null : oldM.getSenderPerson().getKey();
7212 String newKey = newM.getSenderPerson() == null
7213 ? null : newM.getSenderPerson().getKey();
7214 if (!Objects.equals(oldKey, newKey)) {
7215 return true;
7216 }
7217 // Other fields (like timestamp) intentionally excluded
7218 }
7219 return false;
7220 }
7221
Adrian Roosc1a80b02016-04-05 14:54:55 -07007222 private Message findLatestIncomingMessage() {
Selim Cinek88188f22017-09-19 16:46:56 -07007223 return findLatestIncomingMessage(mMessages);
7224 }
7225
7226 /**
7227 * @hide
7228 */
7229 @Nullable
7230 public static Message findLatestIncomingMessage(
7231 List<Message> messages) {
7232 for (int i = messages.size() - 1; i >= 0; i--) {
7233 Message m = messages.get(i);
Adrian Roosc1a80b02016-04-05 14:54:55 -07007234 // Incoming messages have a non-empty sender.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007235 if (m.mSender != null && !TextUtils.isEmpty(m.mSender.getName())) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07007236 return m;
7237 }
7238 }
Selim Cinek88188f22017-09-19 16:46:56 -07007239 if (!messages.isEmpty()) {
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007240 // No incoming messages, fall back to outgoing message
Selim Cinek88188f22017-09-19 16:46:56 -07007241 return messages.get(messages.size() - 1);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007242 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07007243 return null;
7244 }
7245
7246 /**
7247 * @hide
7248 */
7249 @Override
7250 public RemoteViews makeBigContentView() {
Selim Cinek384804b2018-04-18 14:31:07 +08007251 return makeMessagingView(false /* displayImagesAtEnd */, true /* hideLargeIcon */);
Selim Cinekafeed292017-12-12 17:32:44 -08007252 }
7253
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007254 /**
7255 * Create a messaging layout.
7256 *
7257 * @param displayImagesAtEnd should images be displayed at the end of the content instead
7258 * of inline.
Selim Cinek384804b2018-04-18 14:31:07 +08007259 * @param hideRightIcons Should the reply affordance be shown at the end of the notification
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007260 * @return the created remoteView.
7261 */
Selim Cinekafeed292017-12-12 17:32:44 -08007262 @NonNull
Selim Cinek384804b2018-04-18 14:31:07 +08007263 private RemoteViews makeMessagingView(boolean displayImagesAtEnd, boolean hideRightIcons) {
Selim Cinek88188f22017-09-19 16:46:56 -07007264 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
Adrian Roosc1a80b02016-04-05 14:54:55 -07007265 ? super.mBigContentTitle
7266 : mConversationTitle;
Selim Cinekce8794f2018-05-23 16:46:05 -07007267 boolean atLeastP = mBuilder.mContext.getApplicationInfo().targetSdkVersion
7268 >= Build.VERSION_CODES.P;
7269 boolean isOneToOne;
Selim Cinek2dd3e722018-01-19 11:06:06 -08007270 CharSequence nameReplacement = null;
Selim Cinekce8794f2018-05-23 16:46:05 -07007271 Icon avatarReplacement = null;
7272 if (!atLeastP) {
7273 isOneToOne = TextUtils.isEmpty(conversationTitle);
7274 avatarReplacement = mBuilder.mN.mLargeIcon;
7275 if (hasOnlyWhiteSpaceSenders()) {
7276 isOneToOne = true;
7277 nameReplacement = conversationTitle;
7278 conversationTitle = null;
7279 }
7280 } else {
7281 isOneToOne = !isGroupConversation();
Adrian Roosb1f427c2016-05-26 12:27:15 -07007282 }
Selim Cinek384804b2018-04-18 14:31:07 +08007283 TemplateBindResult bindResult = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007284 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).title(
7285 conversationTitle).text(null)
7286 .hideLargeIcon(hideRightIcons || isOneToOne)
7287 .hideReplyIcon(hideRightIcons)
7288 .headerTextSecondary(conversationTitle);
Adrian Roos48d746a2016-04-12 14:57:28 -07007289 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07007290 mBuilder.getMessagingLayoutResource(),
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007291 p,
Selim Cinek384804b2018-04-18 14:31:07 +08007292 bindResult);
Selim Cinek88188f22017-09-19 16:46:56 -07007293 addExtras(mBuilder.mN.extras);
Selim Cinekafeed292017-12-12 17:32:44 -08007294 // also update the end margin if there is an image
Selim Cinek1c72fa02018-04-23 18:00:54 +08007295 contentView.setViewLayoutMarginEnd(R.id.notification_messaging,
7296 bindResult.getIconMarginEnd());
Selim Cinek88188f22017-09-19 16:46:56 -07007297 contentView.setInt(R.id.status_bar_latest_event_content, "setLayoutColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007298 mBuilder.isColorized(p) ? mBuilder.getPrimaryTextColor(p)
7299 : mBuilder.resolveContrastColor(p));
Kenny Guy14d035c2018-05-02 19:10:36 +01007300 contentView.setInt(R.id.status_bar_latest_event_content, "setSenderTextColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007301 mBuilder.getPrimaryTextColor(p));
Kenny Guy14d035c2018-05-02 19:10:36 +01007302 contentView.setInt(R.id.status_bar_latest_event_content, "setMessageTextColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007303 mBuilder.getSecondaryTextColor(p));
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007304 contentView.setBoolean(R.id.status_bar_latest_event_content, "setDisplayImagesAtEnd",
7305 displayImagesAtEnd);
Selim Cinekce8794f2018-05-23 16:46:05 -07007306 contentView.setIcon(R.id.status_bar_latest_event_content, "setAvatarReplacement",
7307 avatarReplacement);
Selim Cinek2dd3e722018-01-19 11:06:06 -08007308 contentView.setCharSequence(R.id.status_bar_latest_event_content, "setNameReplacement",
7309 nameReplacement);
Selim Cinek88188f22017-09-19 16:46:56 -07007310 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsOneToOne",
7311 isOneToOne);
7312 contentView.setBundle(R.id.status_bar_latest_event_content, "setData",
7313 mBuilder.mN.extras);
Alex Hillsfc737de2016-03-23 17:33:02 -04007314 return contentView;
7315 }
7316
Selim Cinekf7409db2017-10-24 16:17:14 -07007317 private boolean hasOnlyWhiteSpaceSenders() {
7318 for (int i = 0; i < mMessages.size(); i++) {
7319 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007320 Person sender = m.getSenderPerson();
7321 if (sender != null && !isWhiteSpace(sender.getName())) {
Selim Cinekf7409db2017-10-24 16:17:14 -07007322 return false;
7323 }
7324 }
7325 return true;
7326 }
7327
7328 private boolean isWhiteSpace(CharSequence sender) {
7329 if (TextUtils.isEmpty(sender)) {
7330 return true;
7331 }
7332 if (sender.toString().matches("^\\s*$")) {
7333 return true;
7334 }
7335 // Let's check if we only have 0 whitespace chars. Some apps did this as a workaround
7336 // For the presentation that we had.
7337 for (int i = 0; i < sender.length(); i++) {
7338 char c = sender.charAt(i);
7339 if (c != '\u200B') {
7340 return false;
7341 }
7342 }
7343 return true;
7344 }
7345
Selim Cinek88188f22017-09-19 16:46:56 -07007346 private CharSequence createConversationTitleFromMessages() {
7347 ArraySet<CharSequence> names = new ArraySet<>();
7348 for (int i = 0; i < mMessages.size(); i++) {
7349 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007350 Person sender = m.getSenderPerson();
Selim Cinek88188f22017-09-19 16:46:56 -07007351 if (sender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007352 names.add(sender.getName());
Selim Cinek88188f22017-09-19 16:46:56 -07007353 }
7354 }
7355 SpannableStringBuilder title = new SpannableStringBuilder();
7356 int size = names.size();
7357 for (int i = 0; i < size; i++) {
7358 CharSequence name = names.valueAt(i);
7359 if (!TextUtils.isEmpty(title)) {
7360 title.append(", ");
7361 }
7362 title.append(BidiFormatter.getInstance().unicodeWrap(name));
7363 }
7364 return title;
7365 }
7366
Adrian Roosdedd1df2016-04-26 16:38:47 -07007367 /**
7368 * @hide
7369 */
7370 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007371 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007372 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007373 true /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007374 remoteViews.setInt(R.id.notification_messaging, "setMaxDisplayedLines", 1);
7375 return remoteViews;
Adrian Roosdedd1df2016-04-26 16:38:47 -07007376 }
7377
Adrian Roosc1a80b02016-04-05 14:54:55 -07007378 private static TextAppearanceSpan makeFontColorSpan(int color) {
7379 return new TextAppearanceSpan(null, 0, 0,
7380 ColorStateList.valueOf(color), null);
7381 }
7382
Alex Hillsd9b04d92016-04-11 16:38:16 -04007383 public static final class Message {
Tony Mak09db2ea2018-06-27 18:12:48 +01007384 /** @hide */
7385 public static final String KEY_TEXT = "text";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007386 static final String KEY_TIMESTAMP = "time";
7387 static final String KEY_SENDER = "sender";
Selim Cinekcb8b9852017-12-15 18:01:52 -08007388 static final String KEY_SENDER_PERSON = "sender_person";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007389 static final String KEY_DATA_MIME_TYPE = "type";
7390 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08007391 static final String KEY_EXTRAS_BUNDLE = "extras";
Kenny Guya0f6de82018-04-06 16:20:16 +01007392 static final String KEY_REMOTE_INPUT_HISTORY = "remote_input_history";
Alex Hillsfc737de2016-03-23 17:33:02 -04007393
7394 private final CharSequence mText;
7395 private final long mTimestamp;
Selim Cinekcb8b9852017-12-15 18:01:52 -08007396 @Nullable
7397 private final Person mSender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007398 /** True if this message was generated from the extra
7399 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}
7400 */
7401 private final boolean mRemoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007402
Shane Brennan5a871862017-03-11 13:14:17 -08007403 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007404 private String mDataMimeType;
7405 private Uri mDataUri;
7406
7407 /**
7408 * Constructor
7409 * @param text A {@link CharSequence} to be displayed as the message content
7410 * @param timestamp Time at which the message arrived
7411 * @param sender A {@link CharSequence} to be used for displaying the name of the
7412 * sender. Should be <code>null</code> for messages by the current user, in which case
7413 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
7414 * Should be unique amongst all individuals in the conversation, and should be
7415 * consistent during re-posts of the notification.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007416 *
7417 * @deprecated use {@code Message(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04007418 */
7419 public Message(CharSequence text, long timestamp, CharSequence sender){
Selim Cinek9acd6732018-03-23 16:39:02 -07007420 this(text, timestamp, sender == null ? null
7421 : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08007422 }
7423
7424 /**
7425 * Constructor
7426 * @param text A {@link CharSequence} to be displayed as the message content
7427 * @param timestamp Time at which the message arrived
7428 * @param sender The {@link Person} who sent the message.
7429 * Should be <code>null</code> for messages by the current user, in which case
7430 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7431 * <p>
Selim Cinek9acd6732018-03-23 16:39:02 -07007432 * The person provided should contain an Icon, set with
7433 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7434 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7435 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7436 * to differentiate between the different users.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007437 * </p>
7438 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007439 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender) {
Kenny Guya0f6de82018-04-06 16:20:16 +01007440 this(text, timestamp, sender, false /* remoteHistory */);
7441 }
7442
7443 /**
7444 * Constructor
7445 * @param text A {@link CharSequence} to be displayed as the message content
7446 * @param timestamp Time at which the message arrived
7447 * @param sender The {@link Person} who sent the message.
7448 * Should be <code>null</code> for messages by the current user, in which case
7449 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7450 * @param remoteInputHistory True if the messages was generated from the extra
7451 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7452 * <p>
7453 * The person provided should contain an Icon, set with
7454 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7455 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7456 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7457 * to differentiate between the different users.
7458 * </p>
7459 * @hide
7460 */
7461 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender,
7462 boolean remoteInputHistory) {
Alex Hillsfc737de2016-03-23 17:33:02 -04007463 mText = text;
7464 mTimestamp = timestamp;
7465 mSender = sender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007466 mRemoteInputHistory = remoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007467 }
7468
7469 /**
7470 * Sets a binary blob of data and an associated MIME type for a message. In the case
7471 * where the platform doesn't support the MIME type, the original text provided in the
7472 * constructor will be used.
7473 * @param dataMimeType The MIME type of the content. See
7474 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
7475 * types on Android and Android Wear.
7476 * @param dataUri The uri containing the content whose type is given by the MIME type.
7477 * <p class="note">
7478 * <ol>
7479 * <li>Notification Listeners including the System UI need permission to access the
7480 * data the Uri points to. The recommended ways to do this are:</li>
7481 * <li>Store the data in your own ContentProvider, making sure that other apps have
7482 * the correct permission to access your provider. The preferred mechanism for
7483 * providing access is to use per-URI permissions which are temporary and only
7484 * grant access to the receiving application. An easy way to create a
7485 * ContentProvider like this is to use the FileProvider helper class.</li>
7486 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
7487 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
7488 * also store non-media types (see MediaStore.Files for more info). Files can be
7489 * inserted into the MediaStore using scanFile() after which a content:// style
7490 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
7491 * Note that once added to the system MediaStore the content is accessible to any
7492 * app on the device.</li>
7493 * </ol>
7494 * @return this object for method chaining
7495 */
7496 public Message setData(String dataMimeType, Uri dataUri) {
7497 mDataMimeType = dataMimeType;
7498 mDataUri = dataUri;
7499 return this;
7500 }
7501
Alex Hillsfc737de2016-03-23 17:33:02 -04007502 /**
7503 * Get the text to be used for this message, or the fallback text if a type and content
7504 * Uri have been set
7505 */
7506 public CharSequence getText() {
7507 return mText;
7508 }
7509
7510 /**
7511 * Get the time at which this message arrived
7512 */
7513 public long getTimestamp() {
7514 return mTimestamp;
7515 }
7516
7517 /**
Shane Brennan5a871862017-03-11 13:14:17 -08007518 * Get the extras Bundle for this message.
7519 */
7520 public Bundle getExtras() {
7521 return mExtras;
7522 }
7523
7524 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007525 * Get the text used to display the contact's name in the messaging experience
Selim Cinekcb8b9852017-12-15 18:01:52 -08007526 *
7527 * @deprecated use {@link #getSenderPerson()}
Alex Hillsfc737de2016-03-23 17:33:02 -04007528 */
7529 public CharSequence getSender() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007530 return mSender == null ? null : mSender.getName();
7531 }
7532
7533 /**
7534 * Get the sender associated with this message.
7535 */
7536 @Nullable
7537 public Person getSenderPerson() {
Alex Hillsfc737de2016-03-23 17:33:02 -04007538 return mSender;
7539 }
7540
7541 /**
7542 * Get the MIME type of the data pointed to by the Uri
7543 */
7544 public String getDataMimeType() {
7545 return mDataMimeType;
7546 }
7547
7548 /**
koprivaa1a78482018-10-09 10:09:23 -07007549 * Get the Uri pointing to the content of the message. Can be null, in which case
Alex Hillsfc737de2016-03-23 17:33:02 -04007550 * {@see #getText()} is used.
7551 */
7552 public Uri getDataUri() {
7553 return mDataUri;
7554 }
7555
Kenny Guya0f6de82018-04-06 16:20:16 +01007556 /**
7557 * @return True if the message was generated from
7558 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7559 * @hide
7560 */
7561 public boolean isRemoteInputHistory() {
7562 return mRemoteInputHistory;
7563 }
7564
Beverlye98937a2018-11-15 10:18:57 -05007565 /**
7566 * @hide
7567 */
7568 @VisibleForTesting
7569 public Bundle toBundle() {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007570 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007571 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007572 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04007573 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007574 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04007575 if (mSender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007576 // Legacy listeners need this
7577 bundle.putCharSequence(KEY_SENDER, mSender.getName());
7578 bundle.putParcelable(KEY_SENDER_PERSON, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04007579 }
7580 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007581 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04007582 }
7583 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007584 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04007585 }
Shane Brennan5a871862017-03-11 13:14:17 -08007586 if (mExtras != null) {
7587 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
7588 }
Kenny Guya0f6de82018-04-06 16:20:16 +01007589 if (mRemoteInputHistory) {
7590 bundle.putBoolean(KEY_REMOTE_INPUT_HISTORY, mRemoteInputHistory);
7591 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007592 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04007593 }
7594
Alex Hillsd9b04d92016-04-11 16:38:16 -04007595 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
7596 Bundle[] bundles = new Bundle[messages.size()];
7597 final int N = messages.size();
7598 for (int i = 0; i < N; i++) {
7599 bundles[i] = messages.get(i).toBundle();
7600 }
7601 return bundles;
7602 }
7603
Selim Cinek88188f22017-09-19 16:46:56 -07007604 /**
7605 * @return A list of messages read from the bundles.
7606 *
7607 * @hide
7608 */
7609 public static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
7610 if (bundles == null) {
7611 return new ArrayList<>();
7612 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007613 List<Message> messages = new ArrayList<>(bundles.length);
7614 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07007615 if (bundles[i] instanceof Bundle) {
7616 Message message = getMessageFromBundle((Bundle)bundles[i]);
7617 if (message != null) {
7618 messages.add(message);
7619 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007620 }
7621 }
7622 return messages;
7623 }
7624
Selim Cinekb0dc61b2018-05-22 18:49:36 -07007625 /**
7626 * @return The message that is stored in the bundle or null if the message couldn't be
7627 * resolved.
7628 *
7629 * @hide
7630 */
7631 @Nullable
7632 public static Message getMessageFromBundle(Bundle bundle) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007633 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07007634 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007635 return null;
7636 } else {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007637
7638 Person senderPerson = bundle.getParcelable(KEY_SENDER_PERSON);
7639 if (senderPerson == null) {
7640 // Legacy apps that use compat don't actually provide the sender objects
7641 // We need to fix the compat version to provide people / use
7642 // the native api instead
7643 CharSequence senderName = bundle.getCharSequence(KEY_SENDER);
7644 if (senderName != null) {
Selim Cinek9acd6732018-03-23 16:39:02 -07007645 senderPerson = new Person.Builder().setName(senderName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007646 }
7647 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007648 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
Selim Cinekcb8b9852017-12-15 18:01:52 -08007649 bundle.getLong(KEY_TIMESTAMP),
Kenny Guya0f6de82018-04-06 16:20:16 +01007650 senderPerson,
7651 bundle.getBoolean(KEY_REMOTE_INPUT_HISTORY, false));
Alex Hillsd9b04d92016-04-11 16:38:16 -04007652 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
7653 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007654 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
7655 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04007656 }
Shane Brennan5a871862017-03-11 13:14:17 -08007657 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
7658 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
7659 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007660 return message;
7661 }
7662 } catch (ClassCastException e) {
7663 return null;
7664 }
7665 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007666 }
7667 }
7668
7669 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04007670 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08007671 *
Robert Ly91c5ce32014-06-08 15:37:00 -07007672 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04007673 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07007674 * Notification notif = new Notification.Builder(mContext)
7675 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
7676 * .setContentText(subject)
7677 * .setSmallIcon(R.drawable.new_mail)
7678 * .setLargeIcon(aBitmap)
7679 * .setStyle(new Notification.InboxStyle()
7680 * .addLine(str1)
7681 * .addLine(str2)
7682 * .setContentTitle(&quot;&quot;)
7683 * .setSummaryText(&quot;+3 more&quot;))
7684 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04007685 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08007686 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04007687 * @see Notification#bigContentView
7688 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007689 public static class InboxStyle extends Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07007690
7691 /**
7692 * The number of lines of remote input history allowed until we start reducing lines.
7693 */
7694 private static final int NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION = 1;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007695 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
7696
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007697 public InboxStyle() {
7698 }
7699
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007700 /**
7701 * @deprecated use {@code InboxStyle()}.
7702 */
7703 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04007704 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007705 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04007706 }
7707
Chris Wrend6297db2012-05-03 16:20:13 -04007708 /**
7709 * Overrides ContentTitle in the big form of the template.
7710 * This defaults to the value passed to setContentTitle().
7711 */
7712 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007713 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04007714 return this;
7715 }
7716
7717 /**
7718 * Set the first line of text after the detail section in the big form of the template.
7719 */
7720 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007721 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04007722 return this;
7723 }
7724
Chris Wren0bd664d2012-08-01 13:56:56 -04007725 /**
7726 * Append a line to the digest section of the Inbox notification.
7727 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04007728 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007729 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04007730 return this;
7731 }
7732
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007733 /**
7734 * @hide
7735 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05007736 public ArrayList<CharSequence> getLines() {
7737 return mTexts;
7738 }
7739
7740 /**
7741 * @hide
7742 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007743 public void addExtras(Bundle extras) {
7744 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007745
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007746 CharSequence[] a = new CharSequence[mTexts.size()];
7747 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
7748 }
7749
Christoph Studer4600f9b2014-07-22 22:44:43 +02007750 /**
7751 * @hide
7752 */
7753 @Override
7754 protected void restoreFromExtras(Bundle extras) {
7755 super.restoreFromExtras(extras);
7756
7757 mTexts.clear();
7758 if (extras.containsKey(EXTRA_TEXT_LINES)) {
7759 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
7760 }
7761 }
7762
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007763 /**
7764 * @hide
7765 */
7766 public RemoteViews makeBigContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007767 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder).text(null);
Selim Cinek384804b2018-04-18 14:31:07 +08007768 TemplateBindResult result = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007769 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource(), p, result);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007770
Chris Wrend6297db2012-05-03 16:20:13 -04007771 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 -04007772 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04007773
Chris Wren4ed80d52012-05-17 09:30:03 -04007774 // Make sure all rows are gone in case we reuse a view.
7775 for (int rowId : rowIds) {
7776 contentView.setViewVisibility(rowId, View.GONE);
7777 }
7778
Daniel Sandler879c5e02012-04-17 16:46:51 -04007779 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07007780 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7781 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08007782 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07007783 int onlyViewId = 0;
7784 int maxRows = rowIds.length;
7785 if (mBuilder.mActions.size() > 0) {
7786 maxRows--;
7787 }
Selim Cinekbee4e072018-05-21 22:06:43 -07007788 CharSequence[] remoteInputHistory = mBuilder.mN.extras.getCharSequenceArray(
7789 EXTRA_REMOTE_INPUT_HISTORY);
7790 if (remoteInputHistory != null
7791 && remoteInputHistory.length > NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION) {
7792 // Let's remove some messages to make room for the remote input history.
7793 // 1 is always able to fit, but let's remove them if they are 2 or 3
7794 int numRemoteInputs = Math.min(remoteInputHistory.length,
7795 MAX_REMOTE_INPUT_HISTORY_LINES);
7796 int totalNumRows = mTexts.size() + numRemoteInputs
7797 - NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION;
7798 if (totalNumRows > maxRows) {
7799 int overflow = totalNumRows - maxRows;
7800 if (mTexts.size() > maxRows) {
7801 // Heuristic: if the Texts don't fit anyway, we'll rather drop the last
7802 // few messages, even with the remote input
7803 maxRows -= overflow;
7804 } else {
7805 // otherwise we drop the first messages
7806 i = overflow;
7807 }
7808 }
7809 }
Selim Cinek07c80172016-04-21 16:40:47 -07007810 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007811 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07007812 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007813 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07007814 contentView.setTextViewText(rowIds[i],
7815 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007816 mBuilder.setTextViewColorSecondary(contentView, rowIds[i], p);
Selim Cinek07c80172016-04-21 16:40:47 -07007817 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek384804b2018-04-18 14:31:07 +08007818 handleInboxImageMargin(contentView, rowIds[i], first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007819 result.getIconMarginEnd());
Selim Cinek07c80172016-04-21 16:40:47 -07007820 if (first) {
7821 onlyViewId = rowIds[i];
7822 } else {
7823 onlyViewId = 0;
7824 }
Selim Cinek247fa012016-02-18 09:50:48 -08007825 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007826 }
7827 i++;
7828 }
Selim Cinek07c80172016-04-21 16:40:47 -07007829 if (onlyViewId != 0) {
7830 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
7831 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7832 R.dimen.notification_text_margin_top);
7833 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
7834 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007835
Daniel Sandler879c5e02012-04-17 16:46:51 -04007836 return contentView;
7837 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007838
Julia Reynolds7217dc92018-03-07 12:12:09 -05007839 /**
7840 * @hide
7841 */
7842 @Override
7843 public boolean areNotificationsVisiblyDifferent(Style other) {
7844 if (other == null || getClass() != other.getClass()) {
7845 return true;
7846 }
7847 InboxStyle newS = (InboxStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04007848
7849 final ArrayList<CharSequence> myLines = getLines();
7850 final ArrayList<CharSequence> newLines = newS.getLines();
7851 final int n = myLines.size();
7852 if (n != newLines.size()) {
7853 return true;
7854 }
7855
7856 for (int i = 0; i < n; i++) {
7857 if (!Objects.equals(
7858 String.valueOf(myLines.get(i)),
7859 String.valueOf(newLines.get(i)))) {
7860 return true;
7861 }
7862 }
7863 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05007864 }
7865
Selim Cinek384804b2018-04-18 14:31:07 +08007866 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007867 int marginEndValue) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08007868 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08007869 if (first) {
7870 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
7871 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
7872 boolean hasProgress = max != 0 || ind;
Selim Cinek384804b2018-04-18 14:31:07 +08007873 if (!hasProgress) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08007874 endMargin = marginEndValue;
Selim Cinek247fa012016-02-18 09:50:48 -08007875 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007876 }
Selim Cinek1c72fa02018-04-23 18:00:54 +08007877 contentView.setViewLayoutMarginEnd(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08007878 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04007879 }
Dan Sandler842dd772014-05-15 09:36:47 -04007880
7881 /**
7882 * Notification style for media playback notifications.
7883 *
7884 * In the expanded form, {@link Notification#bigContentView}, up to 5
7885 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04007886 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04007887 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
7888 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
7889 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08007890 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04007891 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
7892 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01007893 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04007894 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08007895 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007896 * Notifications created with MediaStyle will have their category set to
7897 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
7898 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08007899 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07007900 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
7901 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04007902 * the System UI can identify this as a notification representing an active media session
7903 * and respond accordingly (by showing album artwork in the lockscreen, for example).
7904 *
Selim Cinek99104832017-01-25 14:47:33 -08007905 * <p>
7906 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
7907 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
7908 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
7909 * <p>
7910 *
Dan Sandler842dd772014-05-15 09:36:47 -04007911 * To use this style with your Notification, feed it to
7912 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7913 * <pre class="prettyprint">
7914 * Notification noti = new Notification.Builder()
7915 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01007916 * .setContentTitle(&quot;Track title&quot;)
7917 * .setContentText(&quot;Artist - Album&quot;)
7918 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007919 * .setStyle(<b>new Notification.MediaStyle()</b>
7920 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04007921 * .build();
7922 * </pre>
7923 *
7924 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08007925 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04007926 */
7927 public static class MediaStyle extends Style {
Gus Prevas9cc96602018-10-11 11:24:23 -04007928 // Changing max media buttons requires also changing templates
7929 // (notification_template_material_media and notification_template_material_big_media).
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007930 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04007931 static final int MAX_MEDIA_BUTTONS = 5;
Gus Prevas9cc96602018-10-11 11:24:23 -04007932 @IdRes private static final int[] MEDIA_BUTTON_IDS = {
7933 R.id.action0,
7934 R.id.action1,
7935 R.id.action2,
7936 R.id.action3,
7937 R.id.action4,
7938 };
Dan Sandler842dd772014-05-15 09:36:47 -04007939
7940 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07007941 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04007942
7943 public MediaStyle() {
7944 }
7945
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007946 /**
7947 * @deprecated use {@code MediaStyle()}.
7948 */
7949 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04007950 public MediaStyle(Builder builder) {
7951 setBuilder(builder);
7952 }
7953
7954 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007955 * 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 -04007956 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007957 *
7958 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04007959 */
7960 public MediaStyle setShowActionsInCompactView(int...actions) {
7961 mActionsToShowInCompact = actions;
7962 return this;
7963 }
7964
7965 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07007966 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
7967 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04007968 */
Jeff Browndba34ba2014-06-24 20:46:03 -07007969 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04007970 mToken = token;
7971 return this;
7972 }
7973
Christoph Studer4600f9b2014-07-22 22:44:43 +02007974 /**
7975 * @hide
7976 */
Dan Sandler842dd772014-05-15 09:36:47 -04007977 @Override
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007978 @UnsupportedAppUsage
Dan Sandler842dd772014-05-15 09:36:47 -04007979 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02007980 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007981 if (wip.category == null) {
7982 wip.category = Notification.CATEGORY_TRANSPORT;
7983 }
Dan Sandler842dd772014-05-15 09:36:47 -04007984 return wip;
7985 }
7986
Christoph Studer4600f9b2014-07-22 22:44:43 +02007987 /**
7988 * @hide
7989 */
7990 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007991 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007992 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007993 }
7994
7995 /**
7996 * @hide
7997 */
7998 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007999 public RemoteViews makeBigContentView() {
8000 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02008001 }
8002
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008003 /**
8004 * @hide
8005 */
8006 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008007 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008008 RemoteViews expanded = makeMediaBigContentView();
8009 return expanded != null ? expanded : makeMediaContentView();
8010 }
8011
Dan Sandler842dd772014-05-15 09:36:47 -04008012 /** @hide */
8013 @Override
8014 public void addExtras(Bundle extras) {
8015 super.addExtras(extras);
8016
8017 if (mToken != null) {
8018 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
8019 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01008020 if (mActionsToShowInCompact != null) {
8021 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
8022 }
Dan Sandler842dd772014-05-15 09:36:47 -04008023 }
8024
Christoph Studer4600f9b2014-07-22 22:44:43 +02008025 /**
8026 * @hide
8027 */
8028 @Override
8029 protected void restoreFromExtras(Bundle extras) {
8030 super.restoreFromExtras(extras);
8031
8032 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
8033 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
8034 }
8035 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
8036 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
8037 }
8038 }
8039
Julia Reynolds7217dc92018-03-07 12:12:09 -05008040 /**
8041 * @hide
8042 */
8043 @Override
8044 public boolean areNotificationsVisiblyDifferent(Style other) {
8045 if (other == null || getClass() != other.getClass()) {
8046 return true;
8047 }
8048 // All fields to compare are on the Notification object
8049 return false;
8050 }
8051
Gus Prevas9cc96602018-10-11 11:24:23 -04008052 private void bindMediaActionButton(RemoteViews container, @IdRes int buttonId,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008053 Action action, StandardTemplateParams p) {
Dan Sandler842dd772014-05-15 09:36:47 -04008054 final boolean tombstone = (action.actionIntent == null);
Gus Prevas9cc96602018-10-11 11:24:23 -04008055 container.setViewVisibility(buttonId, View.VISIBLE);
8056 container.setImageViewIcon(buttonId, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07008057
8058 // If the action buttons should not be tinted, then just use the default
8059 // notification color. Otherwise, just use the passed-in color.
Gus Prevas9cc96602018-10-11 11:24:23 -04008060 Resources resources = mBuilder.mContext.getResources();
8061 Configuration currentConfig = resources.getConfiguration();
Lucas Dupinf03e7522018-06-25 16:21:13 -07008062 boolean inNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
8063 == Configuration.UI_MODE_NIGHT_YES;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008064 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized(p)
8065 ? getActionColor(p)
Lucas Dupina291d192018-06-07 13:59:42 -07008066 : ContrastColorUtil.resolveColor(mBuilder.mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07008067 Notification.COLOR_DEFAULT, inNightMode);
Anthony Chenad4d1582017-04-10 16:07:58 -07008068
Gus Prevas9cc96602018-10-11 11:24:23 -04008069 container.setDrawableTint(buttonId, false, tintColor,
Sunny Goyal5b153922017-09-21 21:00:36 -07008070 PorterDuff.Mode.SRC_ATOP);
Gus Prevas9cc96602018-10-11 11:24:23 -04008071
8072 final TypedArray typedArray = mBuilder.mContext.obtainStyledAttributes(
8073 new int[]{ android.R.attr.colorControlHighlight });
8074 int rippleAlpha = Color.alpha(typedArray.getColor(0, 0));
8075 typedArray.recycle();
8076 int rippleColor = Color.argb(rippleAlpha, Color.red(tintColor), Color.green(tintColor),
8077 Color.blue(tintColor));
8078 container.setRippleDrawableColor(buttonId, ColorStateList.valueOf(rippleColor));
8079
Dan Sandler842dd772014-05-15 09:36:47 -04008080 if (!tombstone) {
Gus Prevas9cc96602018-10-11 11:24:23 -04008081 container.setOnClickPendingIntent(buttonId, action.actionIntent);
Dan Sandler842dd772014-05-15 09:36:47 -04008082 }
Gus Prevas9cc96602018-10-11 11:24:23 -04008083 container.setContentDescription(buttonId, action.title);
Dan Sandler842dd772014-05-15 09:36:47 -04008084 }
8085
8086 private RemoteViews makeMediaContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008087 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).fillTextsFrom(
8088 mBuilder);
Dan Sandler842dd772014-05-15 09:36:47 -04008089 RemoteViews view = mBuilder.applyStandardTemplate(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008090 R.layout.notification_template_material_media, p,
Selim Cinek384804b2018-04-18 14:31:07 +08008091 null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04008092
8093 final int numActions = mBuilder.mActions.size();
Gus Prevas9cc96602018-10-11 11:24:23 -04008094 final int numActionsToShow = mActionsToShowInCompact == null
Dan Sandler842dd772014-05-15 09:36:47 -04008095 ? 0
8096 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Gus Prevas9cc96602018-10-11 11:24:23 -04008097 if (numActionsToShow > numActions) {
8098 throw new IllegalArgumentException(String.format(
8099 "setShowActionsInCompactView: action %d out of bounds (max %d)",
8100 numActions, numActions - 1));
8101 }
8102 for (int i = 0; i < MAX_MEDIA_BUTTONS_IN_COMPACT; i++) {
8103 if (i < numActionsToShow) {
Dan Sandler842dd772014-05-15 09:36:47 -04008104 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008105 bindMediaActionButton(view, MEDIA_BUTTON_IDS[i], action, p);
Gus Prevas9cc96602018-10-11 11:24:23 -04008106 } else {
8107 view.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04008108 }
8109 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08008110 handleImage(view);
8111 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008112 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07008113 if (mBuilder.mN.hasLargeIcon()) {
Selim Cinek384804b2018-04-18 14:31:07 +08008114 endMargin = R.dimen.notification_media_image_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08008115 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008116 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04008117 return view;
8118 }
8119
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008120 private int getActionColor(StandardTemplateParams p) {
8121 return mBuilder.isColorized(p) ? mBuilder.getPrimaryTextColor(p)
8122 : mBuilder.resolveContrastColor(p);
Selim Cinek99104832017-01-25 14:47:33 -08008123 }
8124
Dan Sandler842dd772014-05-15 09:36:47 -04008125 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008126 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008127 // Dont add an expanded view if there is no more content to be revealed
8128 int actionsInCompact = mActionsToShowInCompact == null
8129 ? 0
8130 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07008131 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008132 return null;
8133 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008134 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).fillTextsFrom(
8135 mBuilder);
Selim Cinek5bf069a2015-11-10 19:14:27 -05008136 RemoteViews big = mBuilder.applyStandardTemplate(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008137 R.layout.notification_template_material_big_media, p , null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04008138
Gus Prevas9cc96602018-10-11 11:24:23 -04008139 for (int i = 0; i < MAX_MEDIA_BUTTONS; i++) {
8140 if (i < actionCount) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008141 bindMediaActionButton(big, MEDIA_BUTTON_IDS[i], mBuilder.mActions.get(i), p);
Gus Prevas9cc96602018-10-11 11:24:23 -04008142 } else {
8143 big.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04008144 }
8145 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05008146 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04008147 return big;
8148 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008149
Selim Cinek5bf069a2015-11-10 19:14:27 -05008150 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07008151 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008152 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
8153 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008154 }
8155 }
8156
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008157 /**
8158 * @hide
8159 */
8160 @Override
8161 protected boolean hasProgress() {
8162 return false;
8163 }
Dan Sandler842dd772014-05-15 09:36:47 -04008164 }
Griff Hazen61a9e862014-05-22 16:05:19 -07008165
Selim Cinek593610c2016-02-16 18:42:57 -08008166 /**
8167 * Notification style for custom views that are decorated by the system
8168 *
8169 * <p>Instead of providing a notification that is completely custom, a developer can set this
8170 * style and still obtain system decorations like the notification header with the expand
8171 * affordance and actions.
8172 *
8173 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8174 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8175 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8176 * corresponding custom views to display.
8177 *
8178 * To use this style with your Notification, feed it to
8179 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8180 * <pre class="prettyprint">
8181 * Notification noti = new Notification.Builder()
8182 * .setSmallIcon(R.drawable.ic_stat_player)
8183 * .setLargeIcon(albumArtBitmap))
8184 * .setCustomContentView(contentView);
8185 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
8186 * .build();
8187 * </pre>
8188 */
8189 public static class DecoratedCustomViewStyle extends Style {
8190
8191 public DecoratedCustomViewStyle() {
8192 }
8193
Selim Cinek593610c2016-02-16 18:42:57 -08008194 /**
8195 * @hide
8196 */
8197 public boolean displayCustomViewInline() {
8198 return true;
8199 }
8200
8201 /**
8202 * @hide
8203 */
8204 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008205 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008206 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
8207 }
8208
8209 /**
8210 * @hide
8211 */
8212 @Override
8213 public RemoteViews makeBigContentView() {
8214 return makeDecoratedBigContentView();
8215 }
8216
8217 /**
8218 * @hide
8219 */
8220 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008221 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008222 return makeDecoratedHeadsUpContentView();
8223 }
8224
Selim Cinek593610c2016-02-16 18:42:57 -08008225 private RemoteViews makeDecoratedHeadsUpContentView() {
8226 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
8227 ? mBuilder.mN.contentView
8228 : mBuilder.mN.headsUpContentView;
8229 if (mBuilder.mActions.size() == 0) {
8230 return makeStandardTemplateWithCustomContent(headsUpContentView);
8231 }
Selim Cinek384804b2018-04-18 14:31:07 +08008232 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008233 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008234 mBuilder.getBigBaseLayoutResource(), result);
8235 buildIntoRemoteViewContent(remoteViews, headsUpContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008236 return remoteViews;
8237 }
8238
Selim Cinek593610c2016-02-16 18:42:57 -08008239 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
Selim Cinek384804b2018-04-18 14:31:07 +08008240 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008241 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08008242 mBuilder.getBaseLayoutResource(), result);
8243 buildIntoRemoteViewContent(remoteViews, customContent, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008244 return remoteViews;
8245 }
8246
Selim Cinek593610c2016-02-16 18:42:57 -08008247 private RemoteViews makeDecoratedBigContentView() {
8248 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
8249 ? mBuilder.mN.contentView
8250 : mBuilder.mN.bigContentView;
8251 if (mBuilder.mActions.size() == 0) {
8252 return makeStandardTemplateWithCustomContent(bigContentView);
8253 }
Selim Cinek384804b2018-04-18 14:31:07 +08008254 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008255 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008256 mBuilder.getBigBaseLayoutResource(), result);
8257 buildIntoRemoteViewContent(remoteViews, bigContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008258 return remoteViews;
8259 }
Selim Cinek247fa012016-02-18 09:50:48 -08008260
8261 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
Selim Cinek384804b2018-04-18 14:31:07 +08008262 RemoteViews customContent, TemplateBindResult result) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08008263 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008264 // Need to clone customContent before adding, because otherwise it can no longer be
8265 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008266 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07008267 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
8268 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Selim Cinekfc8073c2017-08-16 17:50:20 -07008269 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08008270 }
Selim Cinek247fa012016-02-18 09:50:48 -08008271 // also update the end margin if there is an image
Selim Cinek384804b2018-04-18 14:31:07 +08008272 Resources resources = mBuilder.mContext.getResources();
8273 int endMargin = resources.getDimensionPixelSize(
Selim Cinek1c72fa02018-04-23 18:00:54 +08008274 R.dimen.notification_content_margin_end) + result.getIconMarginEnd();
Selim Cinek384804b2018-04-18 14:31:07 +08008275 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08008276 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05008277
8278 /**
8279 * @hide
8280 */
8281 @Override
8282 public boolean areNotificationsVisiblyDifferent(Style other) {
8283 if (other == null || getClass() != other.getClass()) {
8284 return true;
8285 }
8286 // Comparison done for all custom RemoteViews, independent of style
8287 return false;
8288 }
Selim Cinek593610c2016-02-16 18:42:57 -08008289 }
8290
Selim Cinek03eb3b72016-02-18 10:39:45 -08008291 /**
8292 * Notification style for media custom views that are decorated by the system
8293 *
8294 * <p>Instead of providing a media notification that is completely custom, a developer can set
8295 * this style and still obtain system decorations like the notification header with the expand
8296 * affordance and actions.
8297 *
8298 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8299 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8300 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8301 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08008302 * <p>
8303 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
8304 * notification by using {@link Notification.Builder#setColorized(boolean)}.
8305 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08008306 * To use this style with your Notification, feed it to
8307 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8308 * <pre class="prettyprint">
8309 * Notification noti = new Notification.Builder()
8310 * .setSmallIcon(R.drawable.ic_stat_player)
8311 * .setLargeIcon(albumArtBitmap))
8312 * .setCustomContentView(contentView);
8313 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
8314 * .setMediaSession(mySession))
8315 * .build();
8316 * </pre>
8317 *
8318 * @see android.app.Notification.DecoratedCustomViewStyle
8319 * @see android.app.Notification.MediaStyle
8320 */
8321 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
8322
8323 public DecoratedMediaCustomViewStyle() {
8324 }
8325
Selim Cinek03eb3b72016-02-18 10:39:45 -08008326 /**
8327 * @hide
8328 */
8329 public boolean displayCustomViewInline() {
8330 return true;
8331 }
8332
8333 /**
8334 * @hide
8335 */
8336 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008337 public RemoteViews makeContentView(boolean increasedHeight) {
8338 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008339 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8340 mBuilder.mN.contentView);
8341 }
8342
8343 /**
8344 * @hide
8345 */
8346 @Override
8347 public RemoteViews makeBigContentView() {
8348 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
8349 ? mBuilder.mN.bigContentView
8350 : mBuilder.mN.contentView;
8351 return makeBigContentViewWithCustomContent(customRemoteView);
8352 }
8353
8354 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
8355 RemoteViews remoteViews = super.makeBigContentView();
8356 if (remoteViews != null) {
8357 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
8358 customRemoteView);
8359 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08008360 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008361 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8362 customRemoteView);
8363 } else {
8364 return null;
8365 }
8366 }
8367
8368 /**
8369 * @hide
8370 */
8371 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008372 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08008373 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
8374 ? mBuilder.mN.headsUpContentView
8375 : mBuilder.mN.contentView;
8376 return makeBigContentViewWithCustomContent(customRemoteView);
8377 }
8378
Julia Reynolds7217dc92018-03-07 12:12:09 -05008379 /**
8380 * @hide
8381 */
8382 @Override
8383 public boolean areNotificationsVisiblyDifferent(Style other) {
8384 if (other == null || getClass() != other.getClass()) {
8385 return true;
8386 }
8387 // Comparison done for all custom RemoteViews, independent of style
8388 return false;
8389 }
8390
Selim Cinek03eb3b72016-02-18 10:39:45 -08008391 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
8392 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08008393 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008394 // Need to clone customContent before adding, because otherwise it can no longer be
8395 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008396 customContent = customContent.clone();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008397 customContent.overrideTextColors(mBuilder.getPrimaryTextColor(mBuilder.mParams));
Selim Cinekf91017e2016-03-14 12:25:09 -07008398 remoteViews.removeAllViews(id);
8399 remoteViews.addView(id, customContent);
Selim Cinekfc8073c2017-08-16 17:50:20 -07008400 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08008401 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08008402 return remoteViews;
8403 }
8404 }
8405
Christoph Studer4600f9b2014-07-22 22:44:43 +02008406 // When adding a new Style subclass here, don't forget to update
8407 // Builder.getNotificationStyleClass.
8408
Griff Hazen61a9e862014-05-22 16:05:19 -07008409 /**
8410 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
8411 * metadata or change options on a notification builder.
8412 */
8413 public interface Extender {
8414 /**
8415 * Apply this extender to a notification builder.
8416 * @param builder the builder to be modified.
8417 * @return the build object for chaining.
8418 */
8419 public Builder extend(Builder builder);
8420 }
8421
8422 /**
8423 * Helper class to add wearable extensions to notifications.
8424 * <p class="note"> See
8425 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
8426 * for Android Wear</a> for more information on how to use this class.
8427 * <p>
8428 * To create a notification with wearable extensions:
8429 * <ol>
8430 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
8431 * properties.
8432 * <li>Create a {@link android.app.Notification.WearableExtender}.
8433 * <li>Set wearable-specific properties using the
8434 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
8435 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
8436 * notification.
8437 * <li>Post the notification to the notification system with the
8438 * {@code NotificationManager.notify(...)} methods.
8439 * </ol>
8440 *
8441 * <pre class="prettyprint">
8442 * Notification notif = new Notification.Builder(mContext)
8443 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
8444 * .setContentText(subject)
8445 * .setSmallIcon(R.drawable.new_mail)
8446 * .extend(new Notification.WearableExtender()
8447 * .setContentIcon(R.drawable.new_mail))
8448 * .build();
8449 * NotificationManager notificationManger =
8450 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
8451 * notificationManger.notify(0, notif);</pre>
8452 *
8453 * <p>Wearable extensions can be accessed on an existing notification by using the
8454 * {@code WearableExtender(Notification)} constructor,
8455 * and then using the {@code get} methods to access values.
8456 *
8457 * <pre class="prettyprint">
8458 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
8459 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07008460 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008461 */
8462 public static final class WearableExtender implements Extender {
8463 /**
8464 * Sentinel value for an action index that is unset.
8465 */
8466 public static final int UNSET_ACTION_INDEX = -1;
8467
8468 /**
8469 * Size value for use with {@link #setCustomSizePreset} to show this notification with
8470 * default sizing.
8471 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07008472 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07008473 * on their content.
Gus Prevasd7363752018-09-18 14:35:15 -04008474 *
8475 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008476 */
Gus Prevasd7363752018-09-18 14:35:15 -04008477 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008478 public static final int SIZE_DEFAULT = 0;
8479
8480 /**
8481 * Size value for use with {@link #setCustomSizePreset} to show this notification
8482 * with an extra small size.
8483 * <p>This value is only applicable for custom display notifications created using
8484 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008485 *
8486 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008487 */
Gus Prevasd7363752018-09-18 14:35:15 -04008488 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008489 public static final int SIZE_XSMALL = 1;
8490
8491 /**
8492 * Size value for use with {@link #setCustomSizePreset} to show this notification
8493 * with a small size.
8494 * <p>This value is only applicable for custom display notifications created using
8495 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008496 *
8497 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008498 */
Gus Prevasd7363752018-09-18 14:35:15 -04008499 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008500 public static final int SIZE_SMALL = 2;
8501
8502 /**
8503 * Size value for use with {@link #setCustomSizePreset} to show this notification
8504 * with a medium size.
8505 * <p>This value is only applicable for custom display notifications created using
8506 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008507 *
8508 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008509 */
Gus Prevasd7363752018-09-18 14:35:15 -04008510 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008511 public static final int SIZE_MEDIUM = 3;
8512
8513 /**
8514 * Size value for use with {@link #setCustomSizePreset} to show this notification
8515 * with a large size.
8516 * <p>This value is only applicable for custom display notifications created using
8517 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008518 *
8519 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008520 */
Gus Prevasd7363752018-09-18 14:35:15 -04008521 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008522 public static final int SIZE_LARGE = 4;
8523
Griff Hazend5f11f92014-05-27 15:40:09 -07008524 /**
8525 * Size value for use with {@link #setCustomSizePreset} to show this notification
8526 * full screen.
8527 * <p>This value is only applicable for custom display notifications created using
8528 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008529 *
8530 * @deprecated Display intents are no longer supported.
Griff Hazend5f11f92014-05-27 15:40:09 -07008531 */
Gus Prevasd7363752018-09-18 14:35:15 -04008532 @Deprecated
Griff Hazend5f11f92014-05-27 15:40:09 -07008533 public static final int SIZE_FULL_SCREEN = 5;
8534
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008535 /**
8536 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
8537 * short amount of time when this notification is displayed on the screen. This
8538 * is the default value.
Gus Prevasd7363752018-09-18 14:35:15 -04008539 *
8540 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008541 */
Gus Prevasd7363752018-09-18 14:35:15 -04008542 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008543 public static final int SCREEN_TIMEOUT_SHORT = 0;
8544
8545 /**
8546 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
8547 * for a longer amount of time when this notification is displayed on the screen.
Gus Prevasd7363752018-09-18 14:35:15 -04008548 *
8549 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008550 */
Gus Prevasd7363752018-09-18 14:35:15 -04008551 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008552 public static final int SCREEN_TIMEOUT_LONG = -1;
8553
Griff Hazen61a9e862014-05-22 16:05:19 -07008554 /** Notification extra which contains wearable extensions */
8555 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
8556
Pete Gastaf6781d2014-10-07 15:17:05 -04008557 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07008558 private static final String KEY_ACTIONS = "actions";
8559 private static final String KEY_FLAGS = "flags";
8560 private static final String KEY_DISPLAY_INTENT = "displayIntent";
8561 private static final String KEY_PAGES = "pages";
8562 private static final String KEY_BACKGROUND = "background";
8563 private static final String KEY_CONTENT_ICON = "contentIcon";
8564 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
8565 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
8566 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
8567 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
8568 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008569 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04008570 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008571 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07008572
8573 // Flags bitwise-ored to mFlags
8574 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
8575 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
8576 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
8577 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008578 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04008579 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04008580 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07008581
8582 // Default value for flags integer
8583 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
8584
8585 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
8586 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
8587
8588 private ArrayList<Action> mActions = new ArrayList<Action>();
8589 private int mFlags = DEFAULT_FLAGS;
8590 private PendingIntent mDisplayIntent;
8591 private ArrayList<Notification> mPages = new ArrayList<Notification>();
8592 private Bitmap mBackground;
8593 private int mContentIcon;
8594 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
8595 private int mContentActionIndex = UNSET_ACTION_INDEX;
8596 private int mCustomSizePreset = SIZE_DEFAULT;
8597 private int mCustomContentHeight;
8598 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008599 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04008600 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008601 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07008602
8603 /**
8604 * Create a {@link android.app.Notification.WearableExtender} with default
8605 * options.
8606 */
8607 public WearableExtender() {
8608 }
8609
8610 public WearableExtender(Notification notif) {
8611 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
8612 if (wearableBundle != null) {
8613 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
8614 if (actions != null) {
8615 mActions.addAll(actions);
8616 }
8617
8618 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
8619 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
8620
8621 Notification[] pages = getNotificationArrayFromBundle(
8622 wearableBundle, KEY_PAGES);
8623 if (pages != null) {
8624 Collections.addAll(mPages, pages);
8625 }
8626
8627 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
8628 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
8629 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
8630 DEFAULT_CONTENT_ICON_GRAVITY);
8631 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
8632 UNSET_ACTION_INDEX);
8633 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
8634 SIZE_DEFAULT);
8635 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
8636 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008637 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04008638 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008639 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07008640 }
8641 }
8642
8643 /**
8644 * Apply wearable extensions to a notification that is being built. This is typically
8645 * called by the {@link android.app.Notification.Builder#extend} method of
8646 * {@link android.app.Notification.Builder}.
8647 */
8648 @Override
8649 public Notification.Builder extend(Notification.Builder builder) {
8650 Bundle wearableBundle = new Bundle();
8651
8652 if (!mActions.isEmpty()) {
8653 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
8654 }
8655 if (mFlags != DEFAULT_FLAGS) {
8656 wearableBundle.putInt(KEY_FLAGS, mFlags);
8657 }
8658 if (mDisplayIntent != null) {
8659 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
8660 }
8661 if (!mPages.isEmpty()) {
8662 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
8663 new Notification[mPages.size()]));
8664 }
8665 if (mBackground != null) {
8666 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
8667 }
8668 if (mContentIcon != 0) {
8669 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
8670 }
8671 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
8672 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
8673 }
8674 if (mContentActionIndex != UNSET_ACTION_INDEX) {
8675 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
8676 mContentActionIndex);
8677 }
8678 if (mCustomSizePreset != SIZE_DEFAULT) {
8679 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
8680 }
8681 if (mCustomContentHeight != 0) {
8682 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
8683 }
8684 if (mGravity != DEFAULT_GRAVITY) {
8685 wearableBundle.putInt(KEY_GRAVITY, mGravity);
8686 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008687 if (mHintScreenTimeout != 0) {
8688 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
8689 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04008690 if (mDismissalId != null) {
8691 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
8692 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008693 if (mBridgeTag != null) {
8694 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
8695 }
Griff Hazen61a9e862014-05-22 16:05:19 -07008696
8697 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
8698 return builder;
8699 }
8700
8701 @Override
8702 public WearableExtender clone() {
8703 WearableExtender that = new WearableExtender();
8704 that.mActions = new ArrayList<Action>(this.mActions);
8705 that.mFlags = this.mFlags;
8706 that.mDisplayIntent = this.mDisplayIntent;
8707 that.mPages = new ArrayList<Notification>(this.mPages);
8708 that.mBackground = this.mBackground;
8709 that.mContentIcon = this.mContentIcon;
8710 that.mContentIconGravity = this.mContentIconGravity;
8711 that.mContentActionIndex = this.mContentActionIndex;
8712 that.mCustomSizePreset = this.mCustomSizePreset;
8713 that.mCustomContentHeight = this.mCustomContentHeight;
8714 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008715 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04008716 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008717 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07008718 return that;
8719 }
8720
8721 /**
8722 * Add a wearable action to this notification.
8723 *
8724 * <p>When wearable actions are added using this method, the set of actions that
8725 * show on a wearable device splits from devices that only show actions added
8726 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
8727 * of which actions display on different devices.
8728 *
8729 * @param action the action to add to this notification
8730 * @return this object for method chaining
8731 * @see android.app.Notification.Action
8732 */
8733 public WearableExtender addAction(Action action) {
8734 mActions.add(action);
8735 return this;
8736 }
8737
8738 /**
8739 * Adds wearable actions to this notification.
8740 *
8741 * <p>When wearable actions are added using this method, the set of actions that
8742 * show on a wearable device splits from devices that only show actions added
8743 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
8744 * of which actions display on different devices.
8745 *
8746 * @param actions the actions to add to this notification
8747 * @return this object for method chaining
8748 * @see android.app.Notification.Action
8749 */
8750 public WearableExtender addActions(List<Action> actions) {
8751 mActions.addAll(actions);
8752 return this;
8753 }
8754
8755 /**
8756 * Clear all wearable actions present on this builder.
8757 * @return this object for method chaining.
8758 * @see #addAction
8759 */
8760 public WearableExtender clearActions() {
8761 mActions.clear();
8762 return this;
8763 }
8764
8765 /**
8766 * Get the wearable actions present on this notification.
8767 */
8768 public List<Action> getActions() {
8769 return mActions;
8770 }
8771
8772 /**
8773 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07008774 * this notification. The {@link PendingIntent} provided should be for an activity.
8775 *
8776 * <pre class="prettyprint">
8777 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
8778 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
8779 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
8780 * Notification notif = new Notification.Builder(context)
8781 * .extend(new Notification.WearableExtender()
8782 * .setDisplayIntent(displayPendingIntent)
8783 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
8784 * .build();</pre>
8785 *
8786 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07008787 * should have an empty task affinity. It is also recommended to use the device
8788 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07008789 *
8790 * <p>Example AndroidManifest.xml entry:
8791 * <pre class="prettyprint">
8792 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
8793 * android:exported=&quot;true&quot;
8794 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07008795 * android:taskAffinity=&quot;&quot;
8796 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008797 *
8798 * @param intent the {@link PendingIntent} for an activity
8799 * @return this object for method chaining
8800 * @see android.app.Notification.WearableExtender#getDisplayIntent
Gus Prevasd7363752018-09-18 14:35:15 -04008801 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008802 */
Gus Prevasd7363752018-09-18 14:35:15 -04008803 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008804 public WearableExtender setDisplayIntent(PendingIntent intent) {
8805 mDisplayIntent = intent;
8806 return this;
8807 }
8808
8809 /**
8810 * Get the intent to launch inside of an activity view when displaying this
8811 * notification. This {@code PendingIntent} should be for an activity.
Gus Prevasd7363752018-09-18 14:35:15 -04008812 *
8813 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008814 */
Gus Prevasd7363752018-09-18 14:35:15 -04008815 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008816 public PendingIntent getDisplayIntent() {
8817 return mDisplayIntent;
8818 }
8819
8820 /**
8821 * Add an additional page of content to display with this notification. The current
8822 * notification forms the first page, and pages added using this function form
8823 * subsequent pages. This field can be used to separate a notification into multiple
8824 * sections.
8825 *
8826 * @param page the notification to add as another page
8827 * @return this object for method chaining
8828 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04008829 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008830 */
Gus Prevasd7363752018-09-18 14:35:15 -04008831 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008832 public WearableExtender addPage(Notification page) {
8833 mPages.add(page);
8834 return this;
8835 }
8836
8837 /**
8838 * Add additional pages of content to display with this notification. The current
8839 * notification forms the first page, and pages added using this function form
8840 * subsequent pages. This field can be used to separate a notification into multiple
8841 * sections.
8842 *
8843 * @param pages a list of notifications
8844 * @return this object for method chaining
8845 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04008846 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008847 */
Gus Prevasd7363752018-09-18 14:35:15 -04008848 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008849 public WearableExtender addPages(List<Notification> pages) {
8850 mPages.addAll(pages);
8851 return this;
8852 }
8853
8854 /**
8855 * Clear all additional pages present on this builder.
8856 * @return this object for method chaining.
8857 * @see #addPage
Gus Prevasd7363752018-09-18 14:35:15 -04008858 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008859 */
Gus Prevasd7363752018-09-18 14:35:15 -04008860 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008861 public WearableExtender clearPages() {
8862 mPages.clear();
8863 return this;
8864 }
8865
8866 /**
8867 * Get the array of additional pages of content for displaying this notification. The
8868 * current notification forms the first page, and elements within this array form
8869 * subsequent pages. This field can be used to separate a notification into multiple
8870 * sections.
8871 * @return the pages for this notification
Gus Prevasd7363752018-09-18 14:35:15 -04008872 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008873 */
Gus Prevasd7363752018-09-18 14:35:15 -04008874 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008875 public List<Notification> getPages() {
8876 return mPages;
8877 }
8878
8879 /**
8880 * Set a background image to be displayed behind the notification content.
8881 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8882 * will work with any notification style.
8883 *
8884 * @param background the background bitmap
8885 * @return this object for method chaining
8886 * @see android.app.Notification.WearableExtender#getBackground
Gus Prevasd7363752018-09-18 14:35:15 -04008887 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008888 */
Gus Prevasd7363752018-09-18 14:35:15 -04008889 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008890 public WearableExtender setBackground(Bitmap background) {
8891 mBackground = background;
8892 return this;
8893 }
8894
8895 /**
8896 * Get a background image to be displayed behind the notification content.
8897 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8898 * will work with any notification style.
8899 *
8900 * @return the background image
8901 * @see android.app.Notification.WearableExtender#setBackground
Gus Prevasd7363752018-09-18 14:35:15 -04008902 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008903 */
Gus Prevasd7363752018-09-18 14:35:15 -04008904 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008905 public Bitmap getBackground() {
8906 return mBackground;
8907 }
8908
8909 /**
8910 * Set an icon that goes with the content of this notification.
8911 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008912 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008913 public WearableExtender setContentIcon(int icon) {
8914 mContentIcon = icon;
8915 return this;
8916 }
8917
8918 /**
8919 * Get an icon that goes with the content of this notification.
8920 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008921 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008922 public int getContentIcon() {
8923 return mContentIcon;
8924 }
8925
8926 /**
8927 * Set the gravity that the content icon should have within the notification display.
8928 * Supported values include {@link android.view.Gravity#START} and
8929 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8930 * @see #setContentIcon
8931 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008932 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008933 public WearableExtender setContentIconGravity(int contentIconGravity) {
8934 mContentIconGravity = contentIconGravity;
8935 return this;
8936 }
8937
8938 /**
8939 * Get the gravity that the content icon should have within the notification display.
8940 * Supported values include {@link android.view.Gravity#START} and
8941 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8942 * @see #getContentIcon
8943 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008944 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008945 public int getContentIconGravity() {
8946 return mContentIconGravity;
8947 }
8948
8949 /**
Gus Prevasd7363752018-09-18 14:35:15 -04008950 * Set an action from this notification's actions as the primary action. If the action has a
8951 * {@link RemoteInput} associated with it, shortcuts to the options for that input are shown
8952 * directly on the notification.
Griff Hazen14f57992014-05-26 09:07:14 -07008953 *
Gus Prevasd7363752018-09-18 14:35:15 -04008954 * @param actionIndex The index of the primary action.
Griff Hazen14f57992014-05-26 09:07:14 -07008955 * If wearable actions were added to the main notification, this index
8956 * will apply to that list, otherwise it will apply to the regular
8957 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07008958 */
8959 public WearableExtender setContentAction(int actionIndex) {
8960 mContentActionIndex = actionIndex;
8961 return this;
8962 }
8963
8964 /**
Gus Prevasd7363752018-09-18 14:35:15 -04008965 * Get the index of the notification action, if any, that was specified as the primary
8966 * action.
Griff Hazen14f57992014-05-26 09:07:14 -07008967 *
8968 * <p>If wearable specific actions were added to the main notification, this index will
8969 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07008970 *
8971 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07008972 */
8973 public int getContentAction() {
8974 return mContentActionIndex;
8975 }
8976
8977 /**
8978 * Set the gravity that this notification should have within the available viewport space.
8979 * Supported values include {@link android.view.Gravity#TOP},
8980 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8981 * The default value is {@link android.view.Gravity#BOTTOM}.
8982 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008983 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008984 public WearableExtender setGravity(int gravity) {
8985 mGravity = gravity;
8986 return this;
8987 }
8988
8989 /**
8990 * Get the gravity that this notification should have within the available viewport space.
8991 * Supported values include {@link android.view.Gravity#TOP},
8992 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8993 * The default value is {@link android.view.Gravity#BOTTOM}.
8994 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008995 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008996 public int getGravity() {
8997 return mGravity;
8998 }
8999
9000 /**
9001 * Set the custom size preset for the display of this notification out of the available
9002 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
9003 * {@link #SIZE_LARGE}.
9004 * <p>Some custom size presets are only applicable for custom display notifications created
9005 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
9006 * documentation for the preset in question. See also
9007 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
9008 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009009 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009010 public WearableExtender setCustomSizePreset(int sizePreset) {
9011 mCustomSizePreset = sizePreset;
9012 return this;
9013 }
9014
9015 /**
9016 * Get the custom size preset for the display of this notification out of the available
9017 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
9018 * {@link #SIZE_LARGE}.
9019 * <p>Some custom size presets are only applicable for custom display notifications created
9020 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
9021 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
9022 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009023 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009024 public int getCustomSizePreset() {
9025 return mCustomSizePreset;
9026 }
9027
9028 /**
9029 * Set the custom height in pixels for the display of this notification's content.
9030 * <p>This option is only available for custom display notifications created
9031 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
9032 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
9033 * {@link #getCustomContentHeight}.
9034 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009035 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009036 public WearableExtender setCustomContentHeight(int height) {
9037 mCustomContentHeight = height;
9038 return this;
9039 }
9040
9041 /**
9042 * Get the custom height in pixels for the display of this notification's content.
9043 * <p>This option is only available for custom display notifications created
9044 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
9045 * {@link #setCustomContentHeight}.
9046 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009047 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009048 public int getCustomContentHeight() {
9049 return mCustomContentHeight;
9050 }
9051
9052 /**
9053 * Set whether the scrolling position for the contents of this notification should start
9054 * at the bottom of the contents instead of the top when the contents are too long to
9055 * display within the screen. Default is false (start scroll at the top).
9056 */
9057 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
9058 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
9059 return this;
9060 }
9061
9062 /**
9063 * Get whether the scrolling position for the contents of this notification should start
9064 * at the bottom of the contents instead of the top when the contents are too long to
9065 * display within the screen. Default is false (start scroll at the top).
9066 */
9067 public boolean getStartScrollBottom() {
9068 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
9069 }
9070
9071 /**
9072 * Set whether the content intent is available when the wearable device is not connected
9073 * to a companion device. The user can still trigger this intent when the wearable device
9074 * is offline, but a visual hint will indicate that the content intent may not be available.
9075 * Defaults to true.
9076 */
9077 public WearableExtender setContentIntentAvailableOffline(
9078 boolean contentIntentAvailableOffline) {
9079 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
9080 return this;
9081 }
9082
9083 /**
9084 * Get whether the content intent is available when the wearable device is not connected
9085 * to a companion device. The user can still trigger this intent when the wearable device
9086 * is offline, but a visual hint will indicate that the content intent may not be available.
9087 * Defaults to true.
9088 */
9089 public boolean getContentIntentAvailableOffline() {
9090 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
9091 }
9092
9093 /**
9094 * Set a hint that this notification's icon should not be displayed.
9095 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
9096 * @return this object for method chaining
9097 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009098 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009099 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
9100 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
9101 return this;
9102 }
9103
9104 /**
9105 * Get a hint that this notification's icon should not be displayed.
9106 * @return {@code true} if this icon should not be displayed, false otherwise.
9107 * The default value is {@code false} if this was never set.
9108 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009109 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009110 public boolean getHintHideIcon() {
9111 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
9112 }
9113
9114 /**
9115 * Set a visual hint that only the background image of this notification should be
9116 * displayed, and other semantic content should be hidden. This hint is only applicable
9117 * to sub-pages added using {@link #addPage}.
9118 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009119 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009120 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
9121 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
9122 return this;
9123 }
9124
9125 /**
9126 * Get a visual hint that only the background image of this notification should be
9127 * displayed, and other semantic content should be hidden. This hint is only applicable
9128 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
9129 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009130 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009131 public boolean getHintShowBackgroundOnly() {
9132 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
9133 }
9134
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009135 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009136 * Set a hint that this notification's background should not be clipped if possible,
9137 * and should instead be resized to fully display on the screen, retaining the aspect
9138 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009139 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
9140 * @return this object for method chaining
9141 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009142 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009143 public WearableExtender setHintAvoidBackgroundClipping(
9144 boolean hintAvoidBackgroundClipping) {
9145 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
9146 return this;
9147 }
9148
9149 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009150 * Get a hint that this notification's background should not be clipped if possible,
9151 * and should instead be resized to fully display on the screen, retaining the aspect
9152 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009153 * @return {@code true} if it's ok if the background is clipped on the screen, false
9154 * otherwise. The default value is {@code false} if this was never set.
9155 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009156 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009157 public boolean getHintAvoidBackgroundClipping() {
9158 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
9159 }
9160
9161 /**
9162 * Set a hint that the screen should remain on for at least this duration when
9163 * this notification is displayed on the screen.
9164 * @param timeout The requested screen timeout in milliseconds. Can also be either
9165 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9166 * @return this object for method chaining
9167 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009168 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009169 public WearableExtender setHintScreenTimeout(int timeout) {
9170 mHintScreenTimeout = timeout;
9171 return this;
9172 }
9173
9174 /**
9175 * Get the duration, in milliseconds, that the screen should remain on for
9176 * when this notification is displayed.
9177 * @return the duration in milliseconds if > 0, or either one of the sentinel values
9178 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9179 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009180 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009181 public int getHintScreenTimeout() {
9182 return mHintScreenTimeout;
9183 }
9184
Alex Hills9ab3a232016-04-05 14:54:56 -04009185 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04009186 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
9187 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9188 * qr codes, as well as other simple black-and-white tickets.
9189 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
9190 * @return this object for method chaining
Gus Prevasd7363752018-09-18 14:35:15 -04009191 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009192 */
Gus Prevasd7363752018-09-18 14:35:15 -04009193 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009194 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
9195 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
9196 return this;
9197 }
9198
9199 /**
9200 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
9201 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9202 * qr codes, as well as other simple black-and-white tickets.
9203 * @return {@code true} if it should be displayed in ambient, false otherwise
9204 * otherwise. The default value is {@code false} if this was never set.
Gus Prevasd7363752018-09-18 14:35:15 -04009205 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009206 */
Gus Prevasd7363752018-09-18 14:35:15 -04009207 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009208 public boolean getHintAmbientBigPicture() {
9209 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
9210 }
9211
9212 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04009213 * Set a hint that this notification's content intent will launch an {@link Activity}
9214 * directly, telling the platform that it can generate the appropriate transitions.
9215 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
9216 * an activity and transitions should be generated, false otherwise.
9217 * @return this object for method chaining
9218 */
9219 public WearableExtender setHintContentIntentLaunchesActivity(
9220 boolean hintContentIntentLaunchesActivity) {
9221 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
9222 return this;
9223 }
9224
9225 /**
9226 * Get a hint that this notification's content intent will launch an {@link Activity}
9227 * directly, telling the platform that it can generate the appropriate transitions
9228 * @return {@code true} if the content intent will launch an activity and transitions should
9229 * be generated, false otherwise. The default value is {@code false} if this was never set.
9230 */
9231 public boolean getHintContentIntentLaunchesActivity() {
9232 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
9233 }
9234
Nadia Benbernou948627e2016-04-14 14:41:08 -04009235 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009236 * Sets the dismissal id for this notification. If a notification is posted with a
9237 * dismissal id, then when that notification is canceled, notifications on other wearables
9238 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04009239 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009240 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04009241 * @param dismissalId the dismissal id of the notification.
9242 * @return this object for method chaining
9243 */
9244 public WearableExtender setDismissalId(String dismissalId) {
9245 mDismissalId = dismissalId;
9246 return this;
9247 }
9248
9249 /**
9250 * Returns the dismissal id of the notification.
9251 * @return the dismissal id of the notification or null if it has not been set.
9252 */
9253 public String getDismissalId() {
9254 return mDismissalId;
9255 }
9256
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009257 /**
9258 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
9259 * posted from a phone to provide finer-grained control on what notifications are bridged
9260 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
9261 * Features to Notifications</a> for more information.
9262 * @param bridgeTag the bridge tag of the notification.
9263 * @return this object for method chaining
9264 */
9265 public WearableExtender setBridgeTag(String bridgeTag) {
9266 mBridgeTag = bridgeTag;
9267 return this;
9268 }
9269
9270 /**
9271 * Returns the bridge tag of the notification.
9272 * @return the bridge tag or null if not present.
9273 */
9274 public String getBridgeTag() {
9275 return mBridgeTag;
9276 }
9277
Griff Hazen61a9e862014-05-22 16:05:19 -07009278 private void setFlag(int mask, boolean value) {
9279 if (value) {
9280 mFlags |= mask;
9281 } else {
9282 mFlags &= ~mask;
9283 }
9284 }
9285 }
9286
9287 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009288 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
9289 * with car extensions:
9290 *
9291 * <ol>
9292 * <li>Create an {@link Notification.Builder}, setting any desired
9293 * properties.
9294 * <li>Create a {@link CarExtender}.
9295 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
9296 * {@link CarExtender}.
9297 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9298 * to apply the extensions to a notification.
9299 * </ol>
9300 *
9301 * <pre class="prettyprint">
9302 * Notification notification = new Notification.Builder(context)
9303 * ...
9304 * .extend(new CarExtender()
9305 * .set*(...))
9306 * .build();
9307 * </pre>
9308 *
9309 * <p>Car extensions can be accessed on an existing notification by using the
9310 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
9311 * to access values.
9312 */
9313 public static final class CarExtender implements Extender {
9314 private static final String TAG = "CarExtender";
9315
9316 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
9317 private static final String EXTRA_LARGE_ICON = "large_icon";
9318 private static final String EXTRA_CONVERSATION = "car_conversation";
9319 private static final String EXTRA_COLOR = "app_color";
9320
9321 private Bitmap mLargeIcon;
9322 private UnreadConversation mUnreadConversation;
9323 private int mColor = Notification.COLOR_DEFAULT;
9324
9325 /**
9326 * Create a {@link CarExtender} with default options.
9327 */
9328 public CarExtender() {
9329 }
9330
9331 /**
9332 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
9333 *
9334 * @param notif The notification from which to copy options.
9335 */
9336 public CarExtender(Notification notif) {
9337 Bundle carBundle = notif.extras == null ?
9338 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
9339 if (carBundle != null) {
9340 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
9341 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
9342
9343 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
9344 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
9345 }
9346 }
9347
9348 /**
9349 * Apply car extensions to a notification that is being built. This is typically called by
9350 * the {@link Notification.Builder#extend(Notification.Extender)}
9351 * method of {@link Notification.Builder}.
9352 */
9353 @Override
9354 public Notification.Builder extend(Notification.Builder builder) {
9355 Bundle carExtensions = new Bundle();
9356
9357 if (mLargeIcon != null) {
9358 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
9359 }
9360 if (mColor != Notification.COLOR_DEFAULT) {
9361 carExtensions.putInt(EXTRA_COLOR, mColor);
9362 }
9363
9364 if (mUnreadConversation != null) {
9365 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
9366 carExtensions.putBundle(EXTRA_CONVERSATION, b);
9367 }
9368
9369 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
9370 return builder;
9371 }
9372
9373 /**
9374 * Sets the accent color to use when Android Auto presents the notification.
9375 *
9376 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
9377 * to accent the displayed notification. However, not all colors are acceptable in an
9378 * automotive setting. This method can be used to override the color provided in the
9379 * notification in such a situation.
9380 */
Tor Norbye80756e32015-03-02 09:39:27 -08009381 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009382 mColor = color;
9383 return this;
9384 }
9385
9386 /**
9387 * Gets the accent color.
9388 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04009389 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009390 */
Tor Norbye80756e32015-03-02 09:39:27 -08009391 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009392 public int getColor() {
9393 return mColor;
9394 }
9395
9396 /**
9397 * Sets the large icon of the car notification.
9398 *
9399 * If no large icon is set in the extender, Android Auto will display the icon
9400 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
9401 *
9402 * @param largeIcon The large icon to use in the car notification.
9403 * @return This object for method chaining.
9404 */
9405 public CarExtender setLargeIcon(Bitmap largeIcon) {
9406 mLargeIcon = largeIcon;
9407 return this;
9408 }
9409
9410 /**
9411 * Gets the large icon used in this car notification, or null if no icon has been set.
9412 *
9413 * @return The large icon for the car notification.
9414 * @see CarExtender#setLargeIcon
9415 */
9416 public Bitmap getLargeIcon() {
9417 return mLargeIcon;
9418 }
9419
9420 /**
9421 * Sets the unread conversation in a message notification.
9422 *
9423 * @param unreadConversation The unread part of the conversation this notification conveys.
9424 * @return This object for method chaining.
9425 */
9426 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
9427 mUnreadConversation = unreadConversation;
9428 return this;
9429 }
9430
9431 /**
9432 * Returns the unread conversation conveyed by this notification.
9433 * @see #setUnreadConversation(UnreadConversation)
9434 */
9435 public UnreadConversation getUnreadConversation() {
9436 return mUnreadConversation;
9437 }
9438
9439 /**
9440 * A class which holds the unread messages from a conversation.
9441 */
9442 public static class UnreadConversation {
9443 private static final String KEY_AUTHOR = "author";
9444 private static final String KEY_TEXT = "text";
9445 private static final String KEY_MESSAGES = "messages";
9446 private static final String KEY_REMOTE_INPUT = "remote_input";
9447 private static final String KEY_ON_REPLY = "on_reply";
9448 private static final String KEY_ON_READ = "on_read";
9449 private static final String KEY_PARTICIPANTS = "participants";
9450 private static final String KEY_TIMESTAMP = "timestamp";
9451
9452 private final String[] mMessages;
9453 private final RemoteInput mRemoteInput;
9454 private final PendingIntent mReplyPendingIntent;
9455 private final PendingIntent mReadPendingIntent;
9456 private final String[] mParticipants;
9457 private final long mLatestTimestamp;
9458
9459 UnreadConversation(String[] messages, RemoteInput remoteInput,
9460 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
9461 String[] participants, long latestTimestamp) {
9462 mMessages = messages;
9463 mRemoteInput = remoteInput;
9464 mReadPendingIntent = readPendingIntent;
9465 mReplyPendingIntent = replyPendingIntent;
9466 mParticipants = participants;
9467 mLatestTimestamp = latestTimestamp;
9468 }
9469
9470 /**
9471 * Gets the list of messages conveyed by this notification.
9472 */
9473 public String[] getMessages() {
9474 return mMessages;
9475 }
9476
9477 /**
9478 * Gets the remote input that will be used to convey the response to a message list, or
9479 * null if no such remote input exists.
9480 */
9481 public RemoteInput getRemoteInput() {
9482 return mRemoteInput;
9483 }
9484
9485 /**
9486 * Gets the pending intent that will be triggered when the user replies to this
9487 * notification.
9488 */
9489 public PendingIntent getReplyPendingIntent() {
9490 return mReplyPendingIntent;
9491 }
9492
9493 /**
9494 * Gets the pending intent that Android Auto will send after it reads aloud all messages
9495 * in this object's message list.
9496 */
9497 public PendingIntent getReadPendingIntent() {
9498 return mReadPendingIntent;
9499 }
9500
9501 /**
9502 * Gets the participants in the conversation.
9503 */
9504 public String[] getParticipants() {
9505 return mParticipants;
9506 }
9507
9508 /**
9509 * Gets the firs participant in the conversation.
9510 */
9511 public String getParticipant() {
9512 return mParticipants.length > 0 ? mParticipants[0] : null;
9513 }
9514
9515 /**
9516 * Gets the timestamp of the conversation.
9517 */
9518 public long getLatestTimestamp() {
9519 return mLatestTimestamp;
9520 }
9521
9522 Bundle getBundleForUnreadConversation() {
9523 Bundle b = new Bundle();
9524 String author = null;
9525 if (mParticipants != null && mParticipants.length > 1) {
9526 author = mParticipants[0];
9527 }
9528 Parcelable[] messages = new Parcelable[mMessages.length];
9529 for (int i = 0; i < messages.length; i++) {
9530 Bundle m = new Bundle();
9531 m.putString(KEY_TEXT, mMessages[i]);
9532 m.putString(KEY_AUTHOR, author);
9533 messages[i] = m;
9534 }
9535 b.putParcelableArray(KEY_MESSAGES, messages);
9536 if (mRemoteInput != null) {
9537 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
9538 }
9539 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
9540 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
9541 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
9542 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
9543 return b;
9544 }
9545
9546 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
9547 if (b == null) {
9548 return null;
9549 }
9550 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
9551 String[] messages = null;
9552 if (parcelableMessages != null) {
9553 String[] tmp = new String[parcelableMessages.length];
9554 boolean success = true;
9555 for (int i = 0; i < tmp.length; i++) {
9556 if (!(parcelableMessages[i] instanceof Bundle)) {
9557 success = false;
9558 break;
9559 }
9560 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
9561 if (tmp[i] == null) {
9562 success = false;
9563 break;
9564 }
9565 }
9566 if (success) {
9567 messages = tmp;
9568 } else {
9569 return null;
9570 }
9571 }
9572
9573 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
9574 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
9575
9576 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
9577
9578 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
9579 if (participants == null || participants.length != 1) {
9580 return null;
9581 }
9582
9583 return new UnreadConversation(messages,
9584 remoteInput,
9585 onReply,
9586 onRead,
9587 participants, b.getLong(KEY_TIMESTAMP));
9588 }
9589 };
9590
9591 /**
9592 * Builder class for {@link CarExtender.UnreadConversation} objects.
9593 */
9594 public static class Builder {
9595 private final List<String> mMessages = new ArrayList<String>();
9596 private final String mParticipant;
9597 private RemoteInput mRemoteInput;
9598 private PendingIntent mReadPendingIntent;
9599 private PendingIntent mReplyPendingIntent;
9600 private long mLatestTimestamp;
9601
9602 /**
9603 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
9604 *
9605 * @param name The name of the other participant in the conversation.
9606 */
9607 public Builder(String name) {
9608 mParticipant = name;
9609 }
9610
9611 /**
9612 * Appends a new unread message to the list of messages for this conversation.
9613 *
9614 * The messages should be added from oldest to newest.
9615 *
9616 * @param message The text of the new unread message.
9617 * @return This object for method chaining.
9618 */
9619 public Builder addMessage(String message) {
9620 mMessages.add(message);
9621 return this;
9622 }
9623
9624 /**
9625 * Sets the pending intent and remote input which will convey the reply to this
9626 * notification.
9627 *
9628 * @param pendingIntent The pending intent which will be triggered on a reply.
9629 * @param remoteInput The remote input parcelable which will carry the reply.
9630 * @return This object for method chaining.
9631 *
9632 * @see CarExtender.UnreadConversation#getRemoteInput
9633 * @see CarExtender.UnreadConversation#getReplyPendingIntent
9634 */
9635 public Builder setReplyAction(
9636 PendingIntent pendingIntent, RemoteInput remoteInput) {
9637 mRemoteInput = remoteInput;
9638 mReplyPendingIntent = pendingIntent;
9639
9640 return this;
9641 }
9642
9643 /**
9644 * Sets the pending intent that will be sent once the messages in this notification
9645 * are read.
9646 *
9647 * @param pendingIntent The pending intent to use.
9648 * @return This object for method chaining.
9649 */
9650 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
9651 mReadPendingIntent = pendingIntent;
9652 return this;
9653 }
9654
9655 /**
9656 * Sets the timestamp of the most recent message in an unread conversation.
9657 *
9658 * If a messaging notification has been posted by your application and has not
9659 * yet been cancelled, posting a later notification with the same id and tag
9660 * but without a newer timestamp may result in Android Auto not displaying a
9661 * heads up notification for the later notification.
9662 *
9663 * @param timestamp The timestamp of the most recent message in the conversation.
9664 * @return This object for method chaining.
9665 */
9666 public Builder setLatestTimestamp(long timestamp) {
9667 mLatestTimestamp = timestamp;
9668 return this;
9669 }
9670
9671 /**
9672 * Builds a new unread conversation object.
9673 *
9674 * @return The new unread conversation object.
9675 */
9676 public UnreadConversation build() {
9677 String[] messages = mMessages.toArray(new String[mMessages.size()]);
9678 String[] participants = { mParticipant };
9679 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
9680 mReadPendingIntent, participants, mLatestTimestamp);
9681 }
9682 }
9683 }
9684
9685 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009686 * <p>Helper class to add Android TV extensions to notifications. To create a notification
9687 * with a TV extension:
9688 *
9689 * <ol>
9690 * <li>Create an {@link Notification.Builder}, setting any desired properties.
9691 * <li>Create a {@link TvExtender}.
9692 * <li>Set TV-specific properties using the {@code set} methods of
9693 * {@link TvExtender}.
9694 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9695 * to apply the extension to a notification.
9696 * </ol>
9697 *
9698 * <pre class="prettyprint">
9699 * Notification notification = new Notification.Builder(context)
9700 * ...
9701 * .extend(new TvExtender()
9702 * .set*(...))
9703 * .build();
9704 * </pre>
9705 *
9706 * <p>TV extensions can be accessed on an existing notification by using the
9707 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
9708 * to access values.
9709 *
9710 * @hide
9711 */
9712 @SystemApi
9713 public static final class TvExtender implements Extender {
9714 private static final String TAG = "TvExtender";
9715
9716 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
9717 private static final String EXTRA_FLAGS = "flags";
9718 private static final String EXTRA_CONTENT_INTENT = "content_intent";
9719 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009720 private static final String EXTRA_CHANNEL_ID = "channel_id";
Rhiannon Malia1a083932018-01-24 15:02:30 -08009721 private static final String EXTRA_SUPPRESS_SHOW_OVER_APPS = "suppressShowOverApps";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009722
9723 // Flags bitwise-ored to mFlags
9724 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
9725
9726 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009727 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009728 private PendingIntent mContentIntent;
9729 private PendingIntent mDeleteIntent;
Rhiannon Malia1a083932018-01-24 15:02:30 -08009730 private boolean mSuppressShowOverApps;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009731
9732 /**
9733 * Create a {@link TvExtender} with default options.
9734 */
9735 public TvExtender() {
9736 mFlags = FLAG_AVAILABLE_ON_TV;
9737 }
9738
9739 /**
9740 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
9741 *
9742 * @param notif The notification from which to copy options.
9743 */
9744 public TvExtender(Notification notif) {
9745 Bundle bundle = notif.extras == null ?
9746 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
9747 if (bundle != null) {
9748 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009749 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Rhiannon Malia1a083932018-01-24 15:02:30 -08009750 mSuppressShowOverApps = bundle.getBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009751 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
9752 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
9753 }
9754 }
9755
9756 /**
9757 * Apply a TV extension to a notification that is being built. This is typically called by
9758 * the {@link Notification.Builder#extend(Notification.Extender)}
9759 * method of {@link Notification.Builder}.
9760 */
9761 @Override
9762 public Notification.Builder extend(Notification.Builder builder) {
9763 Bundle bundle = new Bundle();
9764
9765 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009766 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Rhiannon Malia1a083932018-01-24 15:02:30 -08009767 bundle.putBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS, mSuppressShowOverApps);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009768 if (mContentIntent != null) {
9769 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
9770 }
9771
9772 if (mDeleteIntent != null) {
9773 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
9774 }
9775
9776 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
9777 return builder;
9778 }
9779
9780 /**
9781 * Returns true if this notification should be shown on TV. This method return true
9782 * if the notification was extended with a TvExtender.
9783 */
9784 public boolean isAvailableOnTv() {
9785 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
9786 }
9787
9788 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009789 * Specifies the channel the notification should be delivered on when shown on TV.
9790 * It can be different from the channel that the notification is delivered to when
9791 * posting on a non-TV device.
9792 */
9793 public TvExtender setChannel(String channelId) {
9794 mChannelId = channelId;
9795 return this;
9796 }
9797
9798 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04009799 * Specifies the channel the notification should be delivered on when shown on TV.
9800 * It can be different from the channel that the notification is delivered to when
9801 * posting on a non-TV device.
9802 */
9803 public TvExtender setChannelId(String channelId) {
9804 mChannelId = channelId;
9805 return this;
9806 }
9807
Jeff Sharkey000ce802017-04-29 13:13:27 -06009808 /** @removed */
9809 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009810 public String getChannel() {
9811 return mChannelId;
9812 }
9813
9814 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04009815 * Returns the id of the channel this notification posts to on TV.
9816 */
9817 public String getChannelId() {
9818 return mChannelId;
9819 }
9820
9821 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009822 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
9823 * If provided, it is used instead of the content intent specified
9824 * at the level of Notification.
9825 */
9826 public TvExtender setContentIntent(PendingIntent intent) {
9827 mContentIntent = intent;
9828 return this;
9829 }
9830
9831 /**
9832 * Returns the TV-specific content intent. If this method returns null, the
9833 * main content intent on the notification should be used.
9834 *
9835 * @see {@link Notification#contentIntent}
9836 */
9837 public PendingIntent getContentIntent() {
9838 return mContentIntent;
9839 }
9840
9841 /**
9842 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
9843 * by the user on TV. If provided, it is used instead of the delete intent specified
9844 * at the level of Notification.
9845 */
9846 public TvExtender setDeleteIntent(PendingIntent intent) {
9847 mDeleteIntent = intent;
9848 return this;
9849 }
9850
9851 /**
9852 * Returns the TV-specific delete intent. If this method returns null, the
9853 * main delete intent on the notification should be used.
9854 *
9855 * @see {@link Notification#deleteIntent}
9856 */
9857 public PendingIntent getDeleteIntent() {
9858 return mDeleteIntent;
9859 }
Rhiannon Malia1a083932018-01-24 15:02:30 -08009860
9861 /**
9862 * Specifies whether this notification should suppress showing a message over top of apps
9863 * outside of the launcher.
9864 */
9865 public TvExtender setSuppressShowOverApps(boolean suppress) {
9866 mSuppressShowOverApps = suppress;
9867 return this;
9868 }
9869
9870 /**
9871 * Returns true if this notification should not show messages over top of apps
9872 * outside of the launcher.
9873 */
9874 public boolean getSuppressShowOverApps() {
9875 return mSuppressShowOverApps;
9876 }
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009877 }
9878
9879 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07009880 * Get an array of Notification objects from a parcelable array bundle field.
9881 * Update the bundle to have a typed array so fetches in the future don't need
9882 * to do an array copy.
9883 */
9884 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
9885 Parcelable[] array = bundle.getParcelableArray(key);
9886 if (array instanceof Notification[] || array == null) {
9887 return (Notification[]) array;
9888 }
9889 Notification[] typedArray = Arrays.copyOf(array, array.length,
9890 Notification[].class);
9891 bundle.putParcelableArray(key, typedArray);
9892 return typedArray;
9893 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02009894
9895 private static class BuilderRemoteViews extends RemoteViews {
9896 public BuilderRemoteViews(Parcel parcel) {
9897 super(parcel);
9898 }
9899
Kenny Guy77320062014-08-27 21:37:15 +01009900 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
9901 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02009902 }
9903
9904 @Override
9905 public BuilderRemoteViews clone() {
9906 Parcel p = Parcel.obtain();
9907 writeToParcel(p, 0);
9908 p.setDataPosition(0);
9909 BuilderRemoteViews brv = new BuilderRemoteViews(p);
9910 p.recycle();
9911 return brv;
9912 }
9913 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08009914
Selim Cinek384804b2018-04-18 14:31:07 +08009915 /**
9916 * A result object where information about the template that was created is saved.
9917 */
9918 private static class TemplateBindResult {
Selim Cinek1c72fa02018-04-23 18:00:54 +08009919 int mIconMarginEnd;
Selim Cinek6e41b0e2018-12-06 18:10:20 -08009920 boolean mRightIconContainerVisible;
Selim Cinek384804b2018-04-18 14:31:07 +08009921
9922 /**
Selim Cinek1c72fa02018-04-23 18:00:54 +08009923 * Get the margin end that needs to be added to any fields that may overlap
Selim Cinek384804b2018-04-18 14:31:07 +08009924 * with the right actions.
9925 */
Selim Cinek1c72fa02018-04-23 18:00:54 +08009926 public int getIconMarginEnd() {
9927 return mIconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +08009928 }
9929
Selim Cinek6e41b0e2018-12-06 18:10:20 -08009930 /**
9931 * Is the icon container visible on the right size because of the reply button or the
9932 * right icon.
9933 */
9934 public boolean isRightIconContainerVisible() {
9935 return mRightIconContainerVisible;
9936 }
9937
Selim Cinek1c72fa02018-04-23 18:00:54 +08009938 public void setIconMarginEnd(int iconMarginEnd) {
9939 this.mIconMarginEnd = iconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +08009940 }
Selim Cinek6e41b0e2018-12-06 18:10:20 -08009941
9942 public void setRightIconContainerVisible(boolean iconContainerVisible) {
9943 mRightIconContainerVisible = iconContainerVisible;
9944 }
Selim Cinek384804b2018-04-18 14:31:07 +08009945 }
9946
Adrian Roos70d7aa32017-01-11 15:39:06 -08009947 private static class StandardTemplateParams {
9948 boolean hasProgress = true;
9949 boolean ambient = false;
9950 CharSequence title;
9951 CharSequence text;
Selim Cinekafeed292017-12-12 17:32:44 -08009952 CharSequence headerTextSecondary;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08009953 CharSequence summaryText;
Selim Cinekbee4e072018-05-21 22:06:43 -07009954 int maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek88188f22017-09-19 16:46:56 -07009955 boolean hideLargeIcon;
Selim Cinek384804b2018-04-18 14:31:07 +08009956 boolean hideReplyIcon;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08009957 boolean allowColorization = true;
9958 boolean forceDefaultColor = false;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009959
9960 final StandardTemplateParams reset() {
9961 hasProgress = true;
9962 ambient = false;
9963 title = null;
9964 text = null;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08009965 summaryText = null;
Selim Cinekafeed292017-12-12 17:32:44 -08009966 headerTextSecondary = null;
Selim Cinekbee4e072018-05-21 22:06:43 -07009967 maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08009968 allowColorization = true;
9969 forceDefaultColor = false;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009970 return this;
9971 }
9972
9973 final StandardTemplateParams hasProgress(boolean hasProgress) {
9974 this.hasProgress = hasProgress;
9975 return this;
9976 }
9977
9978 final StandardTemplateParams title(CharSequence title) {
9979 this.title = title;
9980 return this;
9981 }
9982
9983 final StandardTemplateParams text(CharSequence text) {
9984 this.text = text;
9985 return this;
9986 }
9987
Selim Cinek10bbc0e2018-11-21 15:14:10 -08009988 final StandardTemplateParams summaryText(CharSequence text) {
9989 this.summaryText = text;
9990 return this;
9991 }
9992
Selim Cinekafeed292017-12-12 17:32:44 -08009993 final StandardTemplateParams headerTextSecondary(CharSequence text) {
9994 this.headerTextSecondary = text;
9995 return this;
9996 }
9997
Selim Cinek384804b2018-04-18 14:31:07 +08009998 final StandardTemplateParams hideLargeIcon(boolean hideLargeIcon) {
9999 this.hideLargeIcon = hideLargeIcon;
Selim Cinek88188f22017-09-19 16:46:56 -070010000 return this;
10001 }
10002
Selim Cinek384804b2018-04-18 14:31:07 +080010003 final StandardTemplateParams hideReplyIcon(boolean hideReplyIcon) {
10004 this.hideReplyIcon = hideReplyIcon;
Selim Cinek88188f22017-09-19 16:46:56 -070010005 return this;
10006 }
10007
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010008 final StandardTemplateParams disallowColorization() {
10009 this.allowColorization = false;
10010 return this;
10011 }
10012
10013 final StandardTemplateParams forceDefaultColor() {
10014 this.forceDefaultColor = true;
10015 return this;
10016 }
10017
Adrian Roos70d7aa32017-01-11 15:39:06 -080010018 final StandardTemplateParams ambient(boolean ambient) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -080010019 Preconditions.checkState(title == null && text == null, "must set ambient before text");
Adrian Roos70d7aa32017-01-11 15:39:06 -080010020 this.ambient = ambient;
10021 return this;
10022 }
10023
10024 final StandardTemplateParams fillTextsFrom(Builder b) {
10025 Bundle extras = b.mN.extras;
Lucas Dupin06c5e642017-09-13 16:34:58 -070010026 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE), ambient);
10027
10028 // Big text notifications should contain their content when viewed in ambient mode.
10029 CharSequence text = extras.getCharSequence(EXTRA_BIG_TEXT);
10030 if (!ambient || TextUtils.isEmpty(text)) {
10031 text = extras.getCharSequence(EXTRA_TEXT);
10032 }
10033 this.text = b.processLegacyText(text, ambient);
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010034 this.summaryText = extras.getCharSequence(EXTRA_SUB_TEXT);
Adrian Roos70d7aa32017-01-11 15:39:06 -080010035 return this;
10036 }
Selim Cinekbee4e072018-05-21 22:06:43 -070010037
10038 /**
10039 * Set the maximum lines of remote input history lines allowed.
10040 * @param maxRemoteInputHistory The number of lines.
10041 * @return The builder for method chaining.
10042 */
10043 public StandardTemplateParams setMaxRemoteInputHistory(int maxRemoteInputHistory) {
10044 this.maxRemoteInputHistory = maxRemoteInputHistory;
10045 return this;
10046 }
Adrian Roos70d7aa32017-01-11 15:39:06 -080010047 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010048}