blob: cf43f59e523e9e2375926bc2d3969ea8a3c49569 [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 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001696 * Combine all of the options that have been set and return a new {@link Action}
1697 * object.
1698 * @return the built action
1699 */
1700 public Action build() {
Shane Brennan472a3b32016-12-12 15:28:10 -08001701 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1702 RemoteInput[] previousDataInputs =
1703 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001704 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001705 for (RemoteInput input : previousDataInputs) {
1706 dataOnlyInputs.add(input);
1707 }
1708 }
1709 List<RemoteInput> textInputs = new ArrayList<>();
1710 if (mRemoteInputs != null) {
1711 for (RemoteInput input : mRemoteInputs) {
1712 if (input.isDataOnly()) {
1713 dataOnlyInputs.add(input);
1714 } else {
1715 textInputs.add(input);
1716 }
1717 }
1718 }
1719 if (!dataOnlyInputs.isEmpty()) {
1720 RemoteInput[] dataInputsArr =
1721 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1722 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1723 }
1724 RemoteInput[] textInputsArr = textInputs.isEmpty()
1725 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1726 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001727 mAllowGeneratedReplies, mSemanticAction);
Griff Hazen959591e2014-05-15 22:26:18 -07001728 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001729 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001730
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001731 @Override
1732 public Action clone() {
1733 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001734 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001735 title,
1736 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001737 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001738 getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001739 getAllowGeneratedReplies(),
1740 getSemanticAction());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001741 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001742
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001743 @Override
1744 public int describeContents() {
1745 return 0;
1746 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001747
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001748 @Override
1749 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001750 final Icon ic = getIcon();
1751 if (ic != null) {
1752 out.writeInt(1);
1753 ic.writeToParcel(out, 0);
1754 } else {
1755 out.writeInt(0);
1756 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001757 TextUtils.writeToParcel(title, out, flags);
1758 if (actionIntent != null) {
1759 out.writeInt(1);
1760 actionIntent.writeToParcel(out, flags);
1761 } else {
1762 out.writeInt(0);
1763 }
Griff Hazen959591e2014-05-15 22:26:18 -07001764 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001765 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001766 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001767 out.writeInt(mSemanticAction);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001768 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001769
Griff Hazen959591e2014-05-15 22:26:18 -07001770 public static final Parcelable.Creator<Action> CREATOR =
1771 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001772 public Action createFromParcel(Parcel in) {
1773 return new Action(in);
1774 }
1775 public Action[] newArray(int size) {
1776 return new Action[size];
1777 }
1778 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001779
1780 /**
1781 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1782 * metadata or change options on an action builder.
1783 */
1784 public interface Extender {
1785 /**
1786 * Apply this extender to a notification action builder.
1787 * @param builder the builder to be modified.
1788 * @return the build object for chaining.
1789 */
1790 public Builder extend(Builder builder);
1791 }
1792
1793 /**
1794 * Wearable extender for notification actions. To add extensions to an action,
1795 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1796 * the {@code WearableExtender()} constructor and apply it to a
1797 * {@link android.app.Notification.Action.Builder} using
1798 * {@link android.app.Notification.Action.Builder#extend}.
1799 *
1800 * <pre class="prettyprint">
1801 * Notification.Action action = new Notification.Action.Builder(
1802 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001803 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001804 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001805 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001806 */
1807 public static final class WearableExtender implements Extender {
1808 /** Notification action extra which contains wearable extensions */
1809 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1810
Pete Gastaf6781d2014-10-07 15:17:05 -04001811 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001812 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001813 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1814 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1815 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001816
1817 // Flags bitwise-ored to mFlags
1818 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001819 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001820 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001821
1822 // Default value for flags integer
1823 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1824
1825 private int mFlags = DEFAULT_FLAGS;
1826
Pete Gastaf6781d2014-10-07 15:17:05 -04001827 private CharSequence mInProgressLabel;
1828 private CharSequence mConfirmLabel;
1829 private CharSequence mCancelLabel;
1830
Griff Hazen61a9e862014-05-22 16:05:19 -07001831 /**
1832 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1833 * options.
1834 */
1835 public WearableExtender() {
1836 }
1837
1838 /**
1839 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1840 * wearable options present in an existing notification action.
1841 * @param action the notification action to inspect.
1842 */
1843 public WearableExtender(Action action) {
1844 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1845 if (wearableBundle != null) {
1846 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001847 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1848 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1849 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001850 }
1851 }
1852
1853 /**
1854 * Apply wearable extensions to a notification action that is being built. This is
1855 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1856 * method of {@link android.app.Notification.Action.Builder}.
1857 */
1858 @Override
1859 public Action.Builder extend(Action.Builder builder) {
1860 Bundle wearableBundle = new Bundle();
1861
1862 if (mFlags != DEFAULT_FLAGS) {
1863 wearableBundle.putInt(KEY_FLAGS, mFlags);
1864 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001865 if (mInProgressLabel != null) {
1866 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1867 }
1868 if (mConfirmLabel != null) {
1869 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1870 }
1871 if (mCancelLabel != null) {
1872 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1873 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001874
1875 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1876 return builder;
1877 }
1878
1879 @Override
1880 public WearableExtender clone() {
1881 WearableExtender that = new WearableExtender();
1882 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001883 that.mInProgressLabel = this.mInProgressLabel;
1884 that.mConfirmLabel = this.mConfirmLabel;
1885 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001886 return that;
1887 }
1888
1889 /**
1890 * Set whether this action is available when the wearable device is not connected to
1891 * a companion device. The user can still trigger this action when the wearable device is
1892 * offline, but a visual hint will indicate that the action may not be available.
1893 * Defaults to true.
1894 */
1895 public WearableExtender setAvailableOffline(boolean availableOffline) {
1896 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1897 return this;
1898 }
1899
1900 /**
1901 * Get whether this action is available when the wearable device is not connected to
1902 * a companion device. The user can still trigger this action when the wearable device is
1903 * offline, but a visual hint will indicate that the action may not be available.
1904 * Defaults to true.
1905 */
1906 public boolean isAvailableOffline() {
1907 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1908 }
1909
1910 private void setFlag(int mask, boolean value) {
1911 if (value) {
1912 mFlags |= mask;
1913 } else {
1914 mFlags &= ~mask;
1915 }
1916 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001917
1918 /**
1919 * Set a label to display while the wearable is preparing to automatically execute the
1920 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1921 *
1922 * @param label the label to display while the action is being prepared to execute
1923 * @return this object for method chaining
1924 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001925 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001926 public WearableExtender setInProgressLabel(CharSequence label) {
1927 mInProgressLabel = label;
1928 return this;
1929 }
1930
1931 /**
1932 * Get the label to display while the wearable is preparing to automatically execute
1933 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1934 *
1935 * @return the label to display while the action is being prepared to execute
1936 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001937 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001938 public CharSequence getInProgressLabel() {
1939 return mInProgressLabel;
1940 }
1941
1942 /**
1943 * Set a label to display to confirm that the action should be executed.
1944 * This is usually an imperative verb like "Send".
1945 *
1946 * @param label the label to confirm the action should be executed
1947 * @return this object for method chaining
1948 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001949 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001950 public WearableExtender setConfirmLabel(CharSequence label) {
1951 mConfirmLabel = label;
1952 return this;
1953 }
1954
1955 /**
1956 * Get the label to display to confirm that the action should be executed.
1957 * This is usually an imperative verb like "Send".
1958 *
1959 * @return the label to confirm the action should be executed
1960 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001961 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001962 public CharSequence getConfirmLabel() {
1963 return mConfirmLabel;
1964 }
1965
1966 /**
1967 * Set a label to display to cancel the action.
1968 * This is usually an imperative verb, like "Cancel".
1969 *
1970 * @param label the label to display to cancel the action
1971 * @return this object for method chaining
1972 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001973 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001974 public WearableExtender setCancelLabel(CharSequence label) {
1975 mCancelLabel = label;
1976 return this;
1977 }
1978
1979 /**
1980 * Get the label to display to cancel the action.
1981 * This is usually an imperative verb like "Cancel".
1982 *
1983 * @return the label to display to cancel the action
1984 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001985 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001986 public CharSequence getCancelLabel() {
1987 return mCancelLabel;
1988 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001989
1990 /**
1991 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1992 * platform that it can generate the appropriate transitions.
1993 * @param hintLaunchesActivity {@code true} if the content intent will launch
1994 * an activity and transitions should be generated, false otherwise.
1995 * @return this object for method chaining
1996 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001997 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001998 boolean hintLaunchesActivity) {
1999 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
2000 return this;
2001 }
2002
2003 /**
2004 * Get a hint that this Action will launch an {@link Activity} directly, telling the
2005 * platform that it can generate the appropriate transitions
2006 * @return {@code true} if the content intent will launch an activity and transitions
2007 * should be generated, false otherwise. The default value is {@code false} if this was
2008 * never set.
2009 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04002010 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04002011 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
2012 }
Alex Hills9f087612016-06-07 09:08:59 -04002013
2014 /**
2015 * Set a hint that this Action should be displayed inline.
2016 *
2017 * @param hintDisplayInline {@code true} if action should be displayed inline, false
2018 * otherwise
2019 * @return this object for method chaining
2020 */
2021 public WearableExtender setHintDisplayActionInline(
2022 boolean hintDisplayInline) {
2023 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
2024 return this;
2025 }
2026
2027 /**
2028 * Get a hint that this Action should be displayed inline.
2029 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08002030 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04002031 * otherwise. The default value is {@code false} if this was never set.
2032 */
2033 public boolean getHintDisplayActionInline() {
2034 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
2035 }
Griff Hazen61a9e862014-05-22 16:05:19 -07002036 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002037
2038 /**
2039 * Provides meaning to an {@link Action} that hints at what the associated
2040 * {@link PendingIntent} will do. For example, an {@link Action} with a
2041 * {@link PendingIntent} that replies to a text message notification may have the
2042 * {@link #SEMANTIC_ACTION_REPLY} {@code SemanticAction} set within it.
2043 *
2044 * @hide
2045 */
2046 @IntDef(prefix = { "SEMANTIC_ACTION_" }, value = {
2047 SEMANTIC_ACTION_NONE,
2048 SEMANTIC_ACTION_REPLY,
2049 SEMANTIC_ACTION_MARK_AS_READ,
2050 SEMANTIC_ACTION_MARK_AS_UNREAD,
2051 SEMANTIC_ACTION_DELETE,
2052 SEMANTIC_ACTION_ARCHIVE,
2053 SEMANTIC_ACTION_MUTE,
2054 SEMANTIC_ACTION_UNMUTE,
2055 SEMANTIC_ACTION_THUMBS_UP,
Kodlee Yincda5b092018-02-15 15:34:53 -08002056 SEMANTIC_ACTION_THUMBS_DOWN,
Gustav Sennton1463d832018-11-06 16:12:48 +00002057 SEMANTIC_ACTION_CALL,
2058 SEMANTIC_ACTION_CONTEXTUAL_SUGGESTION
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002059 })
2060 @Retention(RetentionPolicy.SOURCE)
2061 public @interface SemanticAction {}
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002062 }
2063
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002064 /**
2065 * Array of all {@link Action} structures attached to this notification by
2066 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
2067 * {@link android.service.notification.NotificationListenerService} that provide an alternative
2068 * interface for invoking actions.
2069 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05002070 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002071
2072 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002073 * Replacement version of this notification whose content will be shown
2074 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
2075 * and {@link #VISIBILITY_PUBLIC}.
2076 */
2077 public Notification publicVersion;
2078
2079 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002080 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08002081 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 */
2083 public Notification()
2084 {
2085 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002086 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002087 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002088 }
2089
2090 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 * @hide
2092 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002093 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002094 public Notification(Context context, int icon, CharSequence tickerText, long when,
2095 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
2096 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04002097 new Builder(context)
2098 .setWhen(when)
2099 .setSmallIcon(icon)
2100 .setTicker(tickerText)
2101 .setContentTitle(contentTitle)
2102 .setContentText(contentText)
2103 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
2104 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002105 }
2106
2107 /**
2108 * Constructs a Notification object with the information needed to
2109 * have a status bar icon without the standard expanded view.
2110 *
2111 * @param icon The resource id of the icon to put in the status bar.
2112 * @param tickerText The text that flows by in the status bar when the notification first
2113 * activates.
2114 * @param when The time to show in the time field. In the System.currentTimeMillis
2115 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08002116 *
2117 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002118 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002119 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002120 public Notification(int icon, CharSequence tickerText, long when)
2121 {
2122 this.icon = icon;
2123 this.tickerText = tickerText;
2124 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002125 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002126 }
2127
2128 /**
2129 * Unflatten the notification from a parcel.
2130 */
Svet Ganovddb94882016-06-23 19:55:24 -07002131 @SuppressWarnings("unchecked")
2132 public Notification(Parcel parcel) {
2133 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
2134 // intents in extras are always written as the last entry.
2135 readFromParcelImpl(parcel);
2136 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002137 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07002138 }
2139
2140 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002141 {
2142 int version = parcel.readInt();
2143
Adrian Roosfb921842017-10-26 14:49:56 +02002144 mWhitelistToken = parcel.readStrongBinder();
2145 if (mWhitelistToken == null) {
2146 mWhitelistToken = processWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -07002147 }
2148 // Propagate this token to all pending intents that are unmarshalled from the parcel.
Adrian Roosfb921842017-10-26 14:49:56 +02002149 parcel.setClassCookie(PendingIntent.class, mWhitelistToken);
Dianne Hackborn98305522017-05-05 17:53:53 -07002150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002151 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002152 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04002153 if (parcel.readInt() != 0) {
2154 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04002155 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
2156 icon = mSmallIcon.getResId();
2157 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002158 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 number = parcel.readInt();
2160 if (parcel.readInt() != 0) {
2161 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2162 }
2163 if (parcel.readInt() != 0) {
2164 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2165 }
2166 if (parcel.readInt() != 0) {
2167 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2168 }
2169 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002170 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002171 }
2172 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002173 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
2174 }
Joe Onorato561d3852010-11-20 18:09:34 -08002175 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002176 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08002177 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178 defaults = parcel.readInt();
2179 flags = parcel.readInt();
2180 if (parcel.readInt() != 0) {
2181 sound = Uri.CREATOR.createFromParcel(parcel);
2182 }
2183
2184 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04002185 if (parcel.readInt() != 0) {
2186 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
2187 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002188 vibrate = parcel.createLongArray();
2189 ledARGB = parcel.readInt();
2190 ledOnMS = parcel.readInt();
2191 ledOffMS = parcel.readInt();
2192 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002193
2194 if (parcel.readInt() != 0) {
2195 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2196 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002197
2198 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002199
John Spurlockfd7f1e02014-03-18 16:41:57 -04002200 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002201
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002202 mGroupKey = parcel.readString();
2203
2204 mSortKey = parcel.readString();
2205
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06002206 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Robin Leead7e72a2017-12-04 15:45:46 +01002207 fixDuplicateExtras();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002208
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002209 actions = parcel.createTypedArray(Action.CREATOR); // may be null
2210
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002211 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002212 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2213 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002214
Chris Wren8fd39ec2014-02-27 17:43:26 -05002215 if (parcel.readInt() != 0) {
2216 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2217 }
2218
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002219 visibility = parcel.readInt();
2220
2221 if (parcel.readInt() != 0) {
2222 publicVersion = Notification.CREATOR.createFromParcel(parcel);
2223 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002224
2225 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002226
2227 if (parcel.readInt() != 0) {
2228 mChannelId = parcel.readString();
2229 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002230 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05002231
2232 if (parcel.readInt() != 0) {
2233 mShortcutId = parcel.readString();
2234 }
2235
2236 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04002237
2238 if (parcel.readInt() != 0) {
2239 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2240 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002241
2242 mGroupAlertBehavior = parcel.readInt();
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002243 if (parcel.readInt() != 0) {
2244 mAppOverlayIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2245 }
Gustav Sennton761884c2018-11-19 17:40:19 +00002246
2247 mAllowSystemGeneratedContextualActions = parcel.readBoolean();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002248 }
2249
Andy Stadler110988c2010-12-03 14:29:16 -08002250 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07002251 public Notification clone() {
2252 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04002253 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002254 return that;
2255 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002256
Daniel Sandler1a497d32013-04-18 14:52:45 -04002257 /**
2258 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
2259 * of this into that.
2260 * @hide
2261 */
2262 public void cloneInto(Notification that, boolean heavy) {
Adrian Roosfb921842017-10-26 14:49:56 +02002263 that.mWhitelistToken = this.mWhitelistToken;
Joe Onorato18e69df2010-05-17 22:26:12 -07002264 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002265 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002266 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07002267 that.number = this.number;
2268
2269 // PendingIntents are global, so there's no reason (or way) to clone them.
2270 that.contentIntent = this.contentIntent;
2271 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002272 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07002273
2274 if (this.tickerText != null) {
2275 that.tickerText = this.tickerText.toString();
2276 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002277 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002278 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04002279 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002280 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07002281 that.contentView = this.contentView.clone();
2282 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002283 if (heavy && this.mLargeIcon != null) {
2284 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08002285 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01002286 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07002287 that.sound = this.sound; // android.net.Uri is immutable
2288 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002289 if (this.audioAttributes != null) {
2290 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
2291 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002292
2293 final long[] vibrate = this.vibrate;
2294 if (vibrate != null) {
2295 final int N = vibrate.length;
2296 final long[] vib = that.vibrate = new long[N];
2297 System.arraycopy(vibrate, 0, vib, 0, N);
2298 }
2299
2300 that.ledARGB = this.ledARGB;
2301 that.ledOnMS = this.ledOnMS;
2302 that.ledOffMS = this.ledOffMS;
2303 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002304
Joe Onorato18e69df2010-05-17 22:26:12 -07002305 that.flags = this.flags;
2306
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002307 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08002308
John Spurlockfd7f1e02014-03-18 16:41:57 -04002309 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002310
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002311 that.mGroupKey = this.mGroupKey;
2312
2313 that.mSortKey = this.mSortKey;
2314
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002315 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002316 try {
2317 that.extras = new Bundle(this.extras);
2318 // will unparcel
2319 that.extras.size();
2320 } catch (BadParcelableException e) {
2321 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
2322 that.extras = null;
2323 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002324 }
2325
Felipe Lemedd85da62016-06-28 11:29:54 -07002326 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2327 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002328 }
2329
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002330 if (this.actions != null) {
2331 that.actions = new Action[this.actions.length];
2332 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002333 if ( this.actions[i] != null) {
2334 that.actions[i] = this.actions[i].clone();
2335 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002336 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002337 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002338
Daniel Sandler1a497d32013-04-18 14:52:45 -04002339 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002340 that.bigContentView = this.bigContentView.clone();
2341 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002342
Chris Wren8fd39ec2014-02-27 17:43:26 -05002343 if (heavy && this.headsUpContentView != null) {
2344 that.headsUpContentView = this.headsUpContentView.clone();
2345 }
2346
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002347 that.visibility = this.visibility;
2348
2349 if (this.publicVersion != null) {
2350 that.publicVersion = new Notification();
2351 this.publicVersion.cloneInto(that.publicVersion, heavy);
2352 }
2353
Dan Sandler26e81cf2014-05-06 10:01:27 -04002354 that.color = this.color;
2355
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002356 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002357 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002358 that.mShortcutId = this.mShortcutId;
2359 that.mBadgeIcon = this.mBadgeIcon;
2360 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002361 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002362 that.mAppOverlayIntent = this.mAppOverlayIntent;
Gustav Sennton761884c2018-11-19 17:40:19 +00002363 that.mAllowSystemGeneratedContextualActions = this.mAllowSystemGeneratedContextualActions;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002364
Daniel Sandler1a497d32013-04-18 14:52:45 -04002365 if (!heavy) {
2366 that.lightenPayload(); // will clean out extras
2367 }
2368 }
2369
2370 /**
Jeff Sharkey23b31182018-04-18 21:32:12 -06002371 * Note all {@link Uri} that are referenced internally, with the expectation
2372 * that Uri permission grants will need to be issued to ensure the recipient
2373 * of this object is able to render its contents.
2374 *
2375 * @hide
2376 */
2377 public void visitUris(@NonNull Consumer<Uri> visitor) {
2378 visitor.accept(sound);
2379
2380 if (tickerView != null) tickerView.visitUris(visitor);
2381 if (contentView != null) contentView.visitUris(visitor);
2382 if (bigContentView != null) bigContentView.visitUris(visitor);
2383 if (headsUpContentView != null) headsUpContentView.visitUris(visitor);
2384
2385 if (extras != null) {
2386 visitor.accept(extras.getParcelable(EXTRA_AUDIO_CONTENTS_URI));
Robin Leed107af62018-04-27 13:55:56 +02002387 if (extras.containsKey(EXTRA_BACKGROUND_IMAGE_URI)) {
2388 visitor.accept(Uri.parse(extras.getString(EXTRA_BACKGROUND_IMAGE_URI)));
2389 }
Jeff Sharkey23b31182018-04-18 21:32:12 -06002390 }
2391
2392 if (MessagingStyle.class.equals(getNotificationStyle()) && extras != null) {
2393 final Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
2394 if (!ArrayUtils.isEmpty(messages)) {
2395 for (MessagingStyle.Message message : MessagingStyle.Message
2396 .getMessagesFromBundleArray(messages)) {
2397 visitor.accept(message.getDataUri());
2398 }
2399 }
2400
2401 final Parcelable[] historic = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
2402 if (!ArrayUtils.isEmpty(historic)) {
2403 for (MessagingStyle.Message message : MessagingStyle.Message
2404 .getMessagesFromBundleArray(historic)) {
2405 visitor.accept(message.getDataUri());
2406 }
2407 }
2408 }
2409 }
2410
2411 /**
Daniel Sandler1a497d32013-04-18 14:52:45 -04002412 * Removes heavyweight parts of the Notification object for archival or for sending to
2413 * listeners when the full contents are not necessary.
2414 * @hide
2415 */
2416 public final void lightenPayload() {
2417 tickerView = null;
2418 contentView = null;
2419 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002420 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002421 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002422 if (extras != null && !extras.isEmpty()) {
2423 final Set<String> keyset = extras.keySet();
2424 final int N = keyset.size();
2425 final String[] keys = keyset.toArray(new String[N]);
2426 for (int i=0; i<N; i++) {
2427 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002428 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2429 continue;
2430 }
Dan Sandler50128532015-12-08 15:42:41 -05002431 final Object obj = extras.get(key);
2432 if (obj != null &&
2433 ( obj instanceof Parcelable
2434 || obj instanceof Parcelable[]
2435 || obj instanceof SparseArray
2436 || obj instanceof ArrayList)) {
2437 extras.remove(key);
2438 }
2439 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002440 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002441 }
2442
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002443 /**
2444 * Make sure this CharSequence is safe to put into a bundle, which basically
2445 * means it had better not be some custom Parcelable implementation.
2446 * @hide
2447 */
2448 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002449 if (cs == null) return cs;
2450 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2451 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2452 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002453 if (cs instanceof Parcelable) {
2454 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2455 + " instance is a custom Parcelable and not allowed in Notification");
2456 return cs.toString();
2457 }
Selim Cinek60a54252016-02-26 17:03:25 -08002458 return removeTextSizeSpans(cs);
2459 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002460
Selim Cinek60a54252016-02-26 17:03:25 -08002461 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2462 if (charSequence instanceof Spanned) {
2463 Spanned ss = (Spanned) charSequence;
2464 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2465 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2466 for (Object span : spans) {
2467 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002468 if (resultSpan instanceof CharacterStyle) {
2469 resultSpan = ((CharacterStyle) span).getUnderlying();
2470 }
2471 if (resultSpan instanceof TextAppearanceSpan) {
2472 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002473 resultSpan = new TextAppearanceSpan(
2474 originalSpan.getFamily(),
2475 originalSpan.getTextStyle(),
2476 -1,
2477 originalSpan.getTextColor(),
2478 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002479 } else if (resultSpan instanceof RelativeSizeSpan
2480 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002481 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002482 } else {
2483 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002484 }
2485 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2486 ss.getSpanFlags(span));
2487 }
2488 return builder;
2489 }
2490 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002491 }
2492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002493 public int describeContents() {
2494 return 0;
2495 }
2496
2497 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002498 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002499 */
Svet Ganovddb94882016-06-23 19:55:24 -07002500 public void writeToParcel(Parcel parcel, int flags) {
2501 // We need to mark all pending intents getting into the notification
2502 // system as being put there to later allow the notification ranker
2503 // to launch them and by doing so add the app to the battery saver white
2504 // list for a short period of time. The problem is that the system
2505 // cannot look into the extras as there may be parcelables there that
2506 // the platform does not know how to handle. To go around that we have
2507 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002508 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002509 if (collectPendingIntents) {
2510 PendingIntent.setOnMarshaledListener(
2511 (PendingIntent intent, Parcel out, int outFlags) -> {
2512 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002513 if (allPendingIntents == null) {
2514 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002515 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002516 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002517 }
2518 });
2519 }
2520 try {
2521 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002522 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002523 writeToParcelImpl(parcel, flags);
2524 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002525 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002526 } finally {
2527 if (collectPendingIntents) {
2528 PendingIntent.setOnMarshaledListener(null);
2529 }
2530 }
2531 }
2532
2533 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002534 parcel.writeInt(1);
2535
Adrian Roosfb921842017-10-26 14:49:56 +02002536 parcel.writeStrongBinder(mWhitelistToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002537 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002538 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002539 if (mSmallIcon == null && icon != 0) {
2540 // you snuck an icon in here without using the builder; let's try to keep it
2541 mSmallIcon = Icon.createWithResource("", icon);
2542 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002543 if (mSmallIcon != null) {
2544 parcel.writeInt(1);
2545 mSmallIcon.writeToParcel(parcel, 0);
2546 } else {
2547 parcel.writeInt(0);
2548 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002549 parcel.writeInt(number);
2550 if (contentIntent != null) {
2551 parcel.writeInt(1);
2552 contentIntent.writeToParcel(parcel, 0);
2553 } else {
2554 parcel.writeInt(0);
2555 }
2556 if (deleteIntent != null) {
2557 parcel.writeInt(1);
2558 deleteIntent.writeToParcel(parcel, 0);
2559 } else {
2560 parcel.writeInt(0);
2561 }
2562 if (tickerText != null) {
2563 parcel.writeInt(1);
2564 TextUtils.writeToParcel(tickerText, parcel, flags);
2565 } else {
2566 parcel.writeInt(0);
2567 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002568 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002569 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002570 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002571 } else {
2572 parcel.writeInt(0);
2573 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002574 if (contentView != null) {
2575 parcel.writeInt(1);
2576 contentView.writeToParcel(parcel, 0);
2577 } else {
2578 parcel.writeInt(0);
2579 }
Selim Cinek279fa862016-06-14 10:57:25 -07002580 if (mLargeIcon == null && largeIcon != null) {
2581 // you snuck an icon in here without using the builder; let's try to keep it
2582 mLargeIcon = Icon.createWithBitmap(largeIcon);
2583 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002584 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002585 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002586 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002587 } else {
2588 parcel.writeInt(0);
2589 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002590
2591 parcel.writeInt(defaults);
2592 parcel.writeInt(this.flags);
2593
2594 if (sound != null) {
2595 parcel.writeInt(1);
2596 sound.writeToParcel(parcel, 0);
2597 } else {
2598 parcel.writeInt(0);
2599 }
2600 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002601
2602 if (audioAttributes != null) {
2603 parcel.writeInt(1);
2604 audioAttributes.writeToParcel(parcel, 0);
2605 } else {
2606 parcel.writeInt(0);
2607 }
2608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002609 parcel.writeLongArray(vibrate);
2610 parcel.writeInt(ledARGB);
2611 parcel.writeInt(ledOnMS);
2612 parcel.writeInt(ledOffMS);
2613 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002614
2615 if (fullScreenIntent != null) {
2616 parcel.writeInt(1);
2617 fullScreenIntent.writeToParcel(parcel, 0);
2618 } else {
2619 parcel.writeInt(0);
2620 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002621
2622 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002623
John Spurlockfd7f1e02014-03-18 16:41:57 -04002624 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002625
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002626 parcel.writeString(mGroupKey);
2627
2628 parcel.writeString(mSortKey);
2629
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002630 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002631
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002632 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002633
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002634 if (bigContentView != null) {
2635 parcel.writeInt(1);
2636 bigContentView.writeToParcel(parcel, 0);
2637 } else {
2638 parcel.writeInt(0);
2639 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002640
Chris Wren8fd39ec2014-02-27 17:43:26 -05002641 if (headsUpContentView != null) {
2642 parcel.writeInt(1);
2643 headsUpContentView.writeToParcel(parcel, 0);
2644 } else {
2645 parcel.writeInt(0);
2646 }
2647
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002648 parcel.writeInt(visibility);
2649
2650 if (publicVersion != null) {
2651 parcel.writeInt(1);
2652 publicVersion.writeToParcel(parcel, 0);
2653 } else {
2654 parcel.writeInt(0);
2655 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002656
2657 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002658
2659 if (mChannelId != null) {
2660 parcel.writeInt(1);
2661 parcel.writeString(mChannelId);
2662 } else {
2663 parcel.writeInt(0);
2664 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002665 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002666
2667 if (mShortcutId != null) {
2668 parcel.writeInt(1);
2669 parcel.writeString(mShortcutId);
2670 } else {
2671 parcel.writeInt(0);
2672 }
2673
2674 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002675
2676 if (mSettingsText != null) {
2677 parcel.writeInt(1);
2678 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2679 } else {
2680 parcel.writeInt(0);
2681 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002682
2683 parcel.writeInt(mGroupAlertBehavior);
Julia Reynoldsfc640012018-02-21 12:25:27 -05002684
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002685 if (mAppOverlayIntent != null) {
2686 parcel.writeInt(1);
2687 mAppOverlayIntent.writeToParcel(parcel, 0);
2688 } else {
2689 parcel.writeInt(0);
2690 }
2691
Gustav Sennton761884c2018-11-19 17:40:19 +00002692 parcel.writeBoolean(mAllowSystemGeneratedContextualActions);
2693
Julia Reynoldsfc640012018-02-21 12:25:27 -05002694 // mUsesStandardHeader is not written because it should be recomputed in listeners
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002695 }
2696
2697 /**
2698 * Parcelable.Creator that instantiates Notification objects
2699 */
2700 public static final Parcelable.Creator<Notification> CREATOR
2701 = new Parcelable.Creator<Notification>()
2702 {
2703 public Notification createFromParcel(Parcel parcel)
2704 {
2705 return new Notification(parcel);
2706 }
2707
2708 public Notification[] newArray(int size)
2709 {
2710 return new Notification[size];
2711 }
2712 };
2713
2714 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05002715 * @hide
2716 */
2717 public static boolean areActionsVisiblyDifferent(Notification first, Notification second) {
2718 Notification.Action[] firstAs = first.actions;
2719 Notification.Action[] secondAs = second.actions;
2720 if (firstAs == null && secondAs != null || firstAs != null && secondAs == null) {
2721 return true;
2722 }
2723 if (firstAs != null && secondAs != null) {
2724 if (firstAs.length != secondAs.length) {
2725 return true;
2726 }
2727 for (int i = 0; i < firstAs.length; i++) {
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -04002728 if (!Objects.equals(String.valueOf(firstAs[i].title),
2729 String.valueOf(secondAs[i].title))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05002730 return true;
2731 }
2732 RemoteInput[] firstRs = firstAs[i].getRemoteInputs();
2733 RemoteInput[] secondRs = secondAs[i].getRemoteInputs();
2734 if (firstRs == null) {
2735 firstRs = new RemoteInput[0];
2736 }
2737 if (secondRs == null) {
2738 secondRs = new RemoteInput[0];
2739 }
2740 if (firstRs.length != secondRs.length) {
2741 return true;
2742 }
2743 for (int j = 0; j < firstRs.length; j++) {
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -04002744 if (!Objects.equals(String.valueOf(firstRs[j].getLabel()),
2745 String.valueOf(secondRs[j].getLabel()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05002746 return true;
2747 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05002748 }
2749 }
2750 }
2751 return false;
2752 }
2753
2754 /**
2755 * @hide
2756 */
2757 public static boolean areStyledNotificationsVisiblyDifferent(Builder first, Builder second) {
2758 if (first.getStyle() == null) {
2759 return second.getStyle() != null;
2760 }
2761 if (second.getStyle() == null) {
2762 return true;
2763 }
2764 return first.getStyle().areNotificationsVisiblyDifferent(second.getStyle());
2765 }
2766
2767 /**
2768 * @hide
2769 */
2770 public static boolean areRemoteViewsChanged(Builder first, Builder second) {
Julia Reynoldse5c60452018-04-30 14:41:36 -04002771 if (!Objects.equals(first.usesStandardHeader(), second.usesStandardHeader())) {
2772 return true;
2773 }
2774
2775 if (areRemoteViewsChanged(first.mN.contentView, second.mN.contentView)) {
2776 return true;
2777 }
2778 if (areRemoteViewsChanged(first.mN.bigContentView, second.mN.bigContentView)) {
2779 return true;
2780 }
2781 if (areRemoteViewsChanged(first.mN.headsUpContentView, second.mN.headsUpContentView)) {
2782 return true;
2783 }
2784
2785 return false;
2786 }
2787
2788 private static boolean areRemoteViewsChanged(RemoteViews first, RemoteViews second) {
2789 if (first == null && second == null) {
2790 return false;
2791 }
2792 if (first == null && second != null || first != null && second == null) {
2793 return true;
2794 }
2795
2796 if (!Objects.equals(first.getLayoutId(), second.getLayoutId())) {
2797 return true;
2798 }
2799
2800 if (!Objects.equals(first.getSequenceNumber(), second.getSequenceNumber())) {
2801 return true;
2802 }
2803
2804 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05002805 }
2806
2807 /**
Robin Leead7e72a2017-12-04 15:45:46 +01002808 * Parcelling creates multiple copies of objects in {@code extras}. Fix them.
2809 * <p>
2810 * For backwards compatibility {@code extras} holds some references to "real" member data such
2811 * as {@link getLargeIcon()} which is mirrored by {@link #EXTRA_LARGE_ICON}. This is mostly
2812 * fine as long as the object stays in one process.
2813 * <p>
2814 * However, once the notification goes into a parcel each reference gets marshalled separately,
2815 * wasting memory. Especially with large images on Auto and TV, this is worth fixing.
2816 */
2817 private void fixDuplicateExtras() {
2818 if (extras != null) {
Robin Leead7e72a2017-12-04 15:45:46 +01002819 fixDuplicateExtra(mLargeIcon, EXTRA_LARGE_ICON);
2820 }
2821 }
2822
2823 /**
2824 * If we find an extra that's exactly the same as one of the "real" fields but refers to a
2825 * separate object, replace it with the field's version to avoid holding duplicate copies.
2826 */
2827 private void fixDuplicateExtra(@Nullable Parcelable original, @NonNull String extraName) {
2828 if (original != null && extras.getParcelable(extraName) != null) {
2829 extras.putParcelable(extraName, original);
2830 }
2831 }
2832
2833 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002834 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2835 * layout.
2836 *
2837 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2838 * in the view.</p>
2839 * @param context The context for your application / activity.
2840 * @param contentTitle The title that goes in the expanded entry.
2841 * @param contentText The text that goes in the expanded entry.
2842 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2843 * If this is an activity, it must include the
2844 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002845 * that you take care of task management as described in the
2846 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2847 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002848 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002849 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002850 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002851 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002852 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002853 public void setLatestEventInfo(Context context,
2854 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002855 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2856 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2857 new Throwable());
2858 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002859
Selim Cinek4ac6f602016-06-13 15:47:03 -07002860 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2861 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2862 }
2863
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002864 // ensure that any information already set directly is preserved
2865 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002866
2867 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002868 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002869 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002870 }
2871 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002872 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002873 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002874 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002875
2876 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002877 }
2878
Julia Reynoldsda303542015-11-23 14:00:20 -05002879 /**
2880 * @hide
2881 */
2882 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002883 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002884 }
2885
2886 /**
2887 * @hide
2888 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002889 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002890 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002891 }
2892
Yi Jin6b514142017-10-30 14:54:12 -07002893 /**
2894 * @hide
2895 */
2896 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2897 long token = proto.start(fieldId);
2898 proto.write(NotificationProto.CHANNEL_ID, getChannelId());
2899 proto.write(NotificationProto.HAS_TICKER_TEXT, this.tickerText != null);
2900 proto.write(NotificationProto.FLAGS, this.flags);
2901 proto.write(NotificationProto.COLOR, this.color);
2902 proto.write(NotificationProto.CATEGORY, this.category);
2903 proto.write(NotificationProto.GROUP_KEY, this.mGroupKey);
2904 proto.write(NotificationProto.SORT_KEY, this.mSortKey);
2905 if (this.actions != null) {
2906 proto.write(NotificationProto.ACTION_LENGTH, this.actions.length);
2907 }
2908 if (this.visibility >= VISIBILITY_SECRET && this.visibility <= VISIBILITY_PUBLIC) {
2909 proto.write(NotificationProto.VISIBILITY, this.visibility);
2910 }
2911 if (publicVersion != null) {
2912 publicVersion.writeToProto(proto, NotificationProto.PUBLIC_VERSION);
2913 }
2914 proto.end(token);
2915 }
2916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002917 @Override
2918 public String toString() {
2919 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002920 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04002921 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002922 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002923 sb.append(priority);
2924 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002925 if (contentView != null) {
2926 sb.append(contentView.getPackage());
2927 sb.append("/0x");
2928 sb.append(Integer.toHexString(contentView.getLayoutId()));
2929 } else {
2930 sb.append("null");
2931 }
2932 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002933 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2934 sb.append("default");
2935 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002936 int N = this.vibrate.length-1;
2937 sb.append("[");
2938 for (int i=0; i<N; i++) {
2939 sb.append(this.vibrate[i]);
2940 sb.append(',');
2941 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002942 if (N != -1) {
2943 sb.append(this.vibrate[N]);
2944 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002945 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002946 } else {
2947 sb.append("null");
2948 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002949 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002950 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002952 } else if (this.sound != null) {
2953 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002954 } else {
2955 sb.append("null");
2956 }
Chris Wren365b6d32015-07-16 10:39:26 -04002957 if (this.tickerText != null) {
2958 sb.append(" tick");
2959 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002960 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002961 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002962 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002963 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002964 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002965 if (this.category != null) {
2966 sb.append(" category=");
2967 sb.append(this.category);
2968 }
2969 if (this.mGroupKey != null) {
2970 sb.append(" groupKey=");
2971 sb.append(this.mGroupKey);
2972 }
2973 if (this.mSortKey != null) {
2974 sb.append(" sortKey=");
2975 sb.append(this.mSortKey);
2976 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002977 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002978 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002979 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002980 }
2981 sb.append(" vis=");
2982 sb.append(visibilityToString(this.visibility));
2983 if (this.publicVersion != null) {
2984 sb.append(" publicVersion=");
2985 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002986 }
2987 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002988 return sb.toString();
2989 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002990
Dan Sandler1b718782014-07-18 12:43:45 -04002991 /**
2992 * {@hide}
2993 */
2994 public static String visibilityToString(int vis) {
2995 switch (vis) {
2996 case VISIBILITY_PRIVATE:
2997 return "PRIVATE";
2998 case VISIBILITY_PUBLIC:
2999 return "PUBLIC";
3000 case VISIBILITY_SECRET:
3001 return "SECRET";
3002 default:
3003 return "UNKNOWN(" + String.valueOf(vis) + ")";
3004 }
3005 }
3006
Joe Onoratocb109a02011-01-18 17:57:41 -08003007 /**
John Spurlock1d881a12015-03-18 19:21:54 -04003008 * {@hide}
3009 */
3010 public static String priorityToString(@Priority int pri) {
3011 switch (pri) {
3012 case PRIORITY_MIN:
3013 return "MIN";
3014 case PRIORITY_LOW:
3015 return "LOW";
3016 case PRIORITY_DEFAULT:
3017 return "DEFAULT";
3018 case PRIORITY_HIGH:
3019 return "HIGH";
3020 case PRIORITY_MAX:
3021 return "MAX";
3022 default:
3023 return "UNKNOWN(" + String.valueOf(pri) + ")";
3024 }
3025 }
3026
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04003027 /**
3028 * @hide
3029 */
3030 public boolean hasCompletedProgress() {
3031 // not a progress notification; can't be complete
3032 if (!extras.containsKey(EXTRA_PROGRESS)
3033 || !extras.containsKey(EXTRA_PROGRESS_MAX)) {
3034 return false;
3035 }
3036 // many apps use max 0 for 'indeterminate'; not complete
3037 if (extras.getInt(EXTRA_PROGRESS_MAX) == 0) {
3038 return false;
3039 }
3040 return extras.getInt(EXTRA_PROGRESS) == extras.getInt(EXTRA_PROGRESS_MAX);
3041 }
3042
Jeff Sharkey000ce802017-04-29 13:13:27 -06003043 /** @removed */
3044 @Deprecated
Julia Reynolds37856052016-11-11 09:20:07 -05003045 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003046 return mChannelId;
3047 }
3048
3049 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003050 * Returns the id of the channel this notification posts to.
3051 */
3052 public String getChannelId() {
3053 return mChannelId;
3054 }
3055
Jeff Sharkey000ce802017-04-29 13:13:27 -06003056 /** @removed */
3057 @Deprecated
Julia Reynolds2a128742016-11-28 14:29:25 -05003058 public long getTimeout() {
3059 return mTimeout;
3060 }
3061
3062 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003063 * Returns the duration from posting after which this notification should be canceled by the
3064 * system, if it's not canceled already.
3065 */
3066 public long getTimeoutAfter() {
3067 return mTimeout;
3068 }
3069
3070 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003071 * Returns what icon should be shown for this notification if it is being displayed in a
3072 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
3073 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
3074 */
3075 public int getBadgeIconType() {
3076 return mBadgeIcon;
3077 }
3078
3079 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003080 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04003081 *
3082 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
3083 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003084 */
3085 public String getShortcutId() {
3086 return mShortcutId;
3087 }
3088
Julia Reynolds3aedded2017-03-31 14:42:09 -04003089
3090 /**
3091 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
3092 */
3093 public CharSequence getSettingsText() {
3094 return mSettingsText;
3095 }
3096
Julia Reynolds13d898c2017-02-02 12:22:05 -05003097 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003098 * Returns which type of notifications in a group are responsible for audibly alerting the
3099 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
3100 * {@link #GROUP_ALERT_SUMMARY}.
3101 */
3102 public @GroupAlertBehavior int getGroupAlertBehavior() {
3103 return mGroupAlertBehavior;
3104 }
3105
3106 /**
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003107 * Returns the intent that will be used to display app content in a floating window over the
3108 * existing foreground activity.
3109 */
3110 public PendingIntent getAppOverlayIntent() {
3111 return mAppOverlayIntent;
3112 }
3113
Gustav Sennton761884c2018-11-19 17:40:19 +00003114 public boolean getAllowSystemGeneratedContextualActions() {
3115 return mAllowSystemGeneratedContextualActions;
3116 }
3117
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003118 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003119 * The small icon representing this notification in the status bar and content view.
3120 *
3121 * @return the small icon representing this notification.
3122 *
3123 * @see Builder#getSmallIcon()
3124 * @see Builder#setSmallIcon(Icon)
3125 */
3126 public Icon getSmallIcon() {
3127 return mSmallIcon;
3128 }
3129
3130 /**
3131 * Used when notifying to clean up legacy small icons.
3132 * @hide
3133 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003134 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04003135 public void setSmallIcon(Icon icon) {
3136 mSmallIcon = icon;
3137 }
3138
3139 /**
3140 * The large icon shown in this notification's content view.
3141 * @see Builder#getLargeIcon()
3142 * @see Builder#setLargeIcon(Icon)
3143 */
3144 public Icon getLargeIcon() {
3145 return mLargeIcon;
3146 }
3147
3148 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02003149 * @hide
3150 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003151 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003152 public boolean isGroupSummary() {
3153 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
3154 }
3155
3156 /**
3157 * @hide
3158 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003159 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003160 public boolean isGroupChild() {
3161 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
3162 }
3163
3164 /**
Julia Reynolds30203152017-05-26 13:36:31 -04003165 * @hide
3166 */
3167 public boolean suppressAlertingDueToGrouping() {
3168 if (isGroupSummary()
3169 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
3170 return true;
3171 } else if (isGroupChild()
3172 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
3173 return true;
3174 }
3175 return false;
3176 }
3177
Tony Mak638430e2018-10-08 19:19:10 +01003178
3179 /**
3180 * Finds and returns a remote input and its corresponding action.
3181 *
3182 * @param requiresFreeform requires the remoteinput to allow freeform or not.
3183 * @return the result pair, {@code null} if no result is found.
3184 *
3185 * @hide
3186 */
3187 @Nullable
3188 public Pair<RemoteInput, Action> findRemoteInputActionPair(boolean requiresFreeform) {
3189 if (actions == null) {
3190 return null;
3191 }
3192 for (Notification.Action action : actions) {
3193 if (action.getRemoteInputs() == null) {
3194 continue;
3195 }
3196 RemoteInput resultRemoteInput = null;
3197 for (RemoteInput remoteInput : action.getRemoteInputs()) {
3198 if (remoteInput.getAllowFreeFormInput() || !requiresFreeform) {
3199 resultRemoteInput = remoteInput;
3200 }
3201 }
3202 if (resultRemoteInput != null) {
3203 return Pair.create(resultRemoteInput, action);
3204 }
3205 }
3206 return null;
3207 }
3208
Julia Reynolds30203152017-05-26 13:36:31 -04003209 /**
Gustav Senntoneab53682018-11-01 16:30:23 +00003210 * Returns the actions that are contextual (marked as SEMANTIC_ACTION_CONTEXTUAL_SUGGESTION) out
3211 * of the actions in this notification.
3212 *
3213 * @hide
3214 */
3215 public List<Notification.Action> getContextualActions() {
3216 if (actions == null) return Collections.emptyList();
3217
3218 List<Notification.Action> contextualActions = new ArrayList<>();
3219 for (Notification.Action action : actions) {
3220 if (action.getSemanticAction()
3221 == Notification.Action.SEMANTIC_ACTION_CONTEXTUAL_SUGGESTION) {
3222 contextualActions.add(action);
3223 }
3224 }
3225 return contextualActions;
3226 }
3227
3228 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003229 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08003230 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003231 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07003232 * content views using the platform's notification layout template. If your app supports
3233 * versions of Android as old as API level 4, you can instead use
3234 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
3235 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
3236 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08003237 *
Scott Main183bf112012-08-13 19:12:13 -07003238 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08003239 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003240 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07003241 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003242 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3243 * .setContentText(subject)
3244 * .setSmallIcon(R.drawable.new_mail)
3245 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003246 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003247 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08003248 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003249 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003250 /**
3251 * @hide
3252 */
3253 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
3254 "android.rebuild.contentViewActionCount";
3255 /**
3256 * @hide
3257 */
3258 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
3259 = "android.rebuild.bigViewActionCount";
3260 /**
3261 * @hide
3262 */
3263 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
3264 = "android.rebuild.hudViewActionCount";
3265
Selim Cinek6743c0b2017-01-18 18:24:01 -08003266 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
3267 SystemProperties.getBoolean("notifications.only_title", true);
3268
Selim Cinek389edcd2017-05-11 19:16:44 -07003269 /**
3270 * The lightness difference that has to be added to the primary text color to obtain the
3271 * secondary text color when the background is light.
3272 */
3273 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
3274
3275 /**
3276 * The lightness difference that has to be added to the primary text color to obtain the
3277 * secondary text color when the background is dark.
3278 * A bit less then the above value, since it looks better on dark backgrounds.
3279 */
3280 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
3281
Joe Onorato46439ce2010-11-19 13:56:21 -08003282 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003283 private Notification mN;
3284 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003285 private Style mStyle;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003286 @UnsupportedAppUsage
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003287 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Selim Cineke7238dd2017-12-14 17:48:32 -08003288 private ArrayList<Person> mPersonList = new ArrayList<>();
Lucas Dupina291d192018-06-07 13:59:42 -07003289 private ContrastColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08003290 private boolean mIsLegacy;
3291 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02003292
3293 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08003294 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
3295 */
3296 private int mCachedContrastColor = COLOR_INVALID;
3297 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08003298 /**
Selim Cinek4717d862018-04-19 09:19:15 +08003299 * Caches a ambient version of {@link #mCachedAmbientColorIsFor}.
Adrian Roos487374f2017-01-11 15:48:14 -08003300 */
3301 private int mCachedAmbientColor = COLOR_INVALID;
3302 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Selim Cinek4717d862018-04-19 09:19:15 +08003303 /**
3304 * A neutral color color that can be used for icons.
3305 */
3306 private int mNeutralColor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08003307
3308 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08003309 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
3310 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
3311 */
3312 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08003313 private int mTextColorsAreForBackground = COLOR_INVALID;
3314 private int mPrimaryTextColor = COLOR_INVALID;
3315 private int mSecondaryTextColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07003316 private int mBackgroundColor = COLOR_INVALID;
3317 private int mForegroundColor = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07003318 /**
3319 * A temporary location where actions are stored. If != null the view originally has action
3320 * but doesn't have any for this inflation.
3321 */
3322 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07003323 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003324
Anthony Chenad4d1582017-04-10 16:07:58 -07003325 private boolean mTintActionButtons;
3326 private boolean mInNightMode;
3327
Adrian Roos70d7aa32017-01-11 15:39:06 -08003328 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003329 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08003330 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003331 * @param context
3332 * A {@link Context} that will be used by the Builder to construct the
3333 * RemoteViews. The Context will not be held past the lifetime of this Builder
3334 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003335 * @param channelId
3336 * The constructed Notification will be posted on this
3337 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
3338 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003339 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003340 public Builder(Context context, String channelId) {
3341 this(context, (Notification) null);
3342 mN.mChannelId = channelId;
3343 }
3344
3345 /**
3346 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
3347 * instead. All posted Notifications must specify a NotificationChannel Id.
3348 */
3349 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003350 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003351 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003352 }
3353
Joe Onoratocb109a02011-01-18 17:57:41 -08003354 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003355 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02003356 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003357 public Builder(Context context, Notification toAdopt) {
3358 mContext = context;
Anthony Chenad4d1582017-04-10 16:07:58 -07003359 Resources res = mContext.getResources();
3360 mTintActionButtons = res.getBoolean(R.bool.config_tintNotificationActionButtons);
3361
3362 if (res.getBoolean(R.bool.config_enableNightMode)) {
3363 Configuration currentConfig = res.getConfiguration();
3364 mInNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
3365 == Configuration.UI_MODE_NIGHT_YES;
3366 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003367
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003368 if (toAdopt == null) {
3369 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003370 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
3371 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
3372 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003373 mN.priority = PRIORITY_DEFAULT;
3374 mN.visibility = VISIBILITY_PRIVATE;
3375 } else {
3376 mN = toAdopt;
3377 if (mN.actions != null) {
3378 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003379 }
3380
Selim Cineke7238dd2017-12-14 17:48:32 -08003381 if (mN.extras.containsKey(EXTRA_PEOPLE_LIST)) {
3382 ArrayList<Person> people = mN.extras.getParcelableArrayList(EXTRA_PEOPLE_LIST);
3383 mPersonList.addAll(people);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003384 }
3385
Selim Cinek4ac6f602016-06-13 15:47:03 -07003386 if (mN.getSmallIcon() == null && mN.icon != 0) {
3387 setSmallIcon(mN.icon);
3388 }
3389
3390 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
3391 setLargeIcon(mN.largeIcon);
3392 }
3393
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003394 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
3395 if (!TextUtils.isEmpty(templateClass)) {
3396 final Class<? extends Style> styleClass
3397 = getNotificationStyleClass(templateClass);
3398 if (styleClass == null) {
3399 Log.d(TAG, "Unknown style class: " + templateClass);
3400 } else {
3401 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003402 final Constructor<? extends Style> ctor =
3403 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003404 ctor.setAccessible(true);
3405 final Style style = ctor.newInstance();
3406 style.restoreFromExtras(mN.extras);
3407
3408 if (style != null) {
3409 setStyle(style);
3410 }
3411 } catch (Throwable t) {
3412 Log.e(TAG, "Could not create Style", t);
3413 }
3414 }
3415 }
3416
3417 }
3418 }
3419
Lucas Dupina291d192018-06-07 13:59:42 -07003420 private ContrastColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08003421 if (mColorUtil == null) {
Lucas Dupina291d192018-06-07 13:59:42 -07003422 mColorUtil = ContrastColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003423 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003424 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003425 }
3426
3427 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003428 * If this notification is duplicative of a Launcher shortcut, sets the
3429 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
3430 * the shortcut.
3431 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04003432 * This field will be ignored by Launchers that don't support badging, don't show
3433 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003434 *
3435 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
3436 * supersedes
3437 */
3438 public Builder setShortcutId(String shortcutId) {
3439 mN.mShortcutId = shortcutId;
3440 return this;
3441 }
3442
3443 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003444 * Sets which icon to display as a badge for this notification.
3445 *
3446 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
3447 * {@link #BADGE_ICON_LARGE}.
3448 *
3449 * Note: This value might be ignored, for launchers that don't support badge icons.
3450 */
Julia Reynolds612beb22017-03-30 10:48:30 -04003451 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04003452 mN.mBadgeIcon = icon;
3453 return this;
3454 }
3455
3456 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003457 * Sets the group alert behavior for this notification. Use this method to mute this
3458 * notification if alerts for this notification's group should be handled by a different
3459 * notification. This is only applicable for notifications that belong to a
Julia Reynolds399d9bf2017-08-11 12:52:14 -04003460 * {@link #setGroup(String) group}. This must be called on all notifications you want to
3461 * mute. For example, if you want only the summary of your group to make noise, all
3462 * children in the group should have the group alert behavior {@link #GROUP_ALERT_SUMMARY}.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003463 *
3464 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
3465 */
3466 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
3467 mN.mGroupAlertBehavior = groupAlertBehavior;
3468 return this;
3469 }
3470
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003471 /**
3472 * Sets the intent that will be used to display app content in a floating window
3473 * over the existing foreground activity.
3474 *
3475 * <p>This intent will be ignored unless this notification is posted to a channel that
3476 * allows {@link NotificationChannel#canOverlayApps() app overlays}.</p>
3477 *
3478 * <p>Notifications with a valid and allowed app overlay intent will be displayed as
3479 * floating windows outside of the notification shade on unlocked devices. When a user
3480 * interacts with one of these windows, this app overlay intent will be invoked and
3481 * displayed.</p>
3482 */
3483 public Builder setAppOverlayIntent(PendingIntent intent) {
3484 mN.mAppOverlayIntent = intent;
3485 return this;
3486 }
3487
Jeff Sharkey000ce802017-04-29 13:13:27 -06003488 /** @removed */
3489 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003490 public Builder setChannel(String channelId) {
3491 mN.mChannelId = channelId;
3492 return this;
3493 }
3494
3495 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003496 * Specifies the channel the notification should be delivered on.
3497 */
3498 public Builder setChannelId(String channelId) {
3499 mN.mChannelId = channelId;
3500 return this;
3501 }
3502
Jeff Sharkey000ce802017-04-29 13:13:27 -06003503 /** @removed */
3504 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05003505 public Builder setTimeout(long durationMs) {
3506 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05003507 return this;
3508 }
3509
3510 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003511 * Specifies a duration in milliseconds after which this notification should be canceled,
3512 * if it is not already canceled.
3513 */
3514 public Builder setTimeoutAfter(long durationMs) {
3515 mN.mTimeout = durationMs;
3516 return this;
3517 }
3518
3519 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003520 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003521 *
3522 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
3523 * shown anymore by default and must be opted into by using
3524 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003525 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003526 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08003527 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003528 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003529 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08003530 return this;
3531 }
3532
Joe Onoratocb109a02011-01-18 17:57:41 -08003533 /**
Griff Hazen50c11652014-05-16 09:46:31 -07003534 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07003535 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003536 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
3537 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07003538 */
3539 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003540 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07003541 return this;
3542 }
3543
3544 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003545 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08003546 *
3547 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003548 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003549 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003550 * Useful when showing an elapsed time (like an ongoing phone call).
3551 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08003552 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07003553 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08003554 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003555 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003556 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07003557 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003558 */
3559 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003560 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003561 return this;
3562 }
3563
3564 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08003565 * Sets the Chronometer to count down instead of counting up.
3566 *
3567 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
3568 * If it isn't set the chronometer will count up.
3569 *
3570 * @see #setUsesChronometer(boolean)
3571 */
Adrian Roos96b7e202016-05-17 13:50:38 -07003572 public Builder setChronometerCountDown(boolean countDown) {
3573 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003574 return this;
3575 }
3576
3577 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003578 * Set the small icon resource, which will be used to represent the notification in the
3579 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003580 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003581
3582 * The platform template for the expanded view will draw this icon in the left, unless a
3583 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3584 * icon will be moved to the right-hand side.
3585 *
3586
3587 * @param icon
3588 * A resource ID in the application's package of the drawable to use.
3589 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003590 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003591 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003592 return setSmallIcon(icon != 0
3593 ? Icon.createWithResource(mContext, icon)
3594 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003595 }
3596
Joe Onoratocb109a02011-01-18 17:57:41 -08003597 /**
3598 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3599 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3600 * LevelListDrawable}.
3601 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003602 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003603 * @param level The level to use for the icon.
3604 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003605 * @see Notification#icon
3606 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003607 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003608 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003609 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003610 return setSmallIcon(icon);
3611 }
3612
3613 /**
3614 * Set the small icon, which will be used to represent the notification in the
koprivadebd4ee2018-09-13 10:59:46 -07003615 * status bar and content view (unless overridden there by a
Dan Sandlerd63f9322015-05-06 15:18:49 -04003616 * {@link #setLargeIcon(Bitmap) large icon}).
3617 *
3618 * @param icon An Icon object to use.
3619 * @see Notification#icon
3620 */
3621 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003622 mN.setSmallIcon(icon);
3623 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3624 mN.icon = icon.getResId();
3625 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003626 return this;
3627 }
3628
Joe Onoratocb109a02011-01-18 17:57:41 -08003629 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003630 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003631 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003632 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003633 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003634 return this;
3635 }
3636
Joe Onoratocb109a02011-01-18 17:57:41 -08003637 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003638 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003639 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003640 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003641 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003642 return this;
3643 }
3644
Joe Onoratocb109a02011-01-18 17:57:41 -08003645 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003646 * This provides some additional information that is displayed in the notification. No
3647 * guarantees are given where exactly it is displayed.
3648 *
3649 * <p>This information should only be provided if it provides an essential
3650 * benefit to the understanding of the notification. The more text you provide the
3651 * less readable it becomes. For example, an email client should only provide the account
3652 * name here if more than one email account has been added.</p>
3653 *
3654 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3655 * notification header area.
3656 *
3657 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3658 * this will be shown in the third line of text in the platform notification template.
3659 * You should not be using {@link #setProgress(int, int, boolean)} at the
3660 * same time on those versions; they occupy the same place.
3661 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003662 */
3663 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003664 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003665 return this;
3666 }
3667
3668 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003669 * Provides text that will appear as a link to your application's settings.
3670 *
3671 * <p>This text does not appear within notification {@link Style templates} but may
3672 * appear when the user uses an affordance to learn more about the notification.
3673 * Additionally, this text will not appear unless you provide a valid link target by
3674 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3675 *
3676 * <p>This text is meant to be concise description about what the user can customize
3677 * when they click on this link. The recommended maximum length is 40 characters.
3678 * @param text
3679 * @return
3680 */
3681 public Builder setSettingsText(CharSequence text) {
3682 mN.mSettingsText = safeCharSequence(text);
3683 return this;
3684 }
3685
3686 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003687 * Set the remote input history.
3688 *
3689 * This should be set to the most recent inputs that have been sent
3690 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3691 * longer relevant (e.g. for chat notifications once the other party has responded).
3692 *
3693 * The most recent input must be stored at the 0 index, the second most recent at the
3694 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3695 * and how much of each individual input is shown.
3696 *
3697 * <p>Note: The reply text will only be shown on notifications that have least one action
3698 * with a {@code RemoteInput}.</p>
3699 */
3700 public Builder setRemoteInputHistory(CharSequence[] text) {
3701 if (text == null) {
3702 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3703 } else {
3704 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3705 CharSequence[] safe = new CharSequence[N];
3706 for (int i = 0; i < N; i++) {
3707 safe[i] = safeCharSequence(text[i]);
3708 }
3709 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3710 }
3711 return this;
3712 }
3713
3714 /**
Kenny Guya0f6de82018-04-06 16:20:16 +01003715 * Sets whether remote history entries view should have a spinner.
3716 * @hide
3717 */
3718 public Builder setShowRemoteInputSpinner(boolean showSpinner) {
3719 mN.extras.putBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER, showSpinner);
3720 return this;
3721 }
3722
3723 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +01003724 * Sets whether smart reply buttons should be hidden.
3725 * @hide
3726 */
3727 public Builder setHideSmartReplies(boolean hideSmartReplies) {
3728 mN.extras.putBoolean(EXTRA_HIDE_SMART_REPLIES, hideSmartReplies);
3729 return this;
3730 }
3731
3732 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003733 * Sets the number of items this notification represents. May be displayed as a badge count
3734 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003735 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08003736 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003737 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003738 return this;
3739 }
3740
Joe Onoratocb109a02011-01-18 17:57:41 -08003741 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003742 * A small piece of additional information pertaining to this notification.
3743 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003744 * The platform template will draw this on the last line of the notification, at the far
3745 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003746 *
3747 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3748 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3749 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003750 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003751 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003752 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003753 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003754 return this;
3755 }
3756
Joe Onoratocb109a02011-01-18 17:57:41 -08003757 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003758 * Set the progress this notification represents.
3759 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003760 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003761 */
3762 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003763 mN.extras.putInt(EXTRA_PROGRESS, progress);
3764 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3765 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003766 return this;
3767 }
3768
3769 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003770 * Supply a custom RemoteViews to use instead of the platform template.
3771 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003772 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003773 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003774 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003775 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003776 return setCustomContentView(views);
3777 }
3778
3779 /**
3780 * Supply custom RemoteViews to use instead of the platform template.
3781 *
3782 * This will override the layout that would otherwise be constructed by this Builder
3783 * object.
3784 */
3785 public Builder setCustomContentView(RemoteViews contentView) {
3786 mN.contentView = contentView;
3787 return this;
3788 }
3789
3790 /**
3791 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3792 *
3793 * This will override the expanded layout that would otherwise be constructed by this
3794 * Builder object.
3795 */
3796 public Builder setCustomBigContentView(RemoteViews contentView) {
3797 mN.bigContentView = contentView;
3798 return this;
3799 }
3800
3801 /**
3802 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3803 *
3804 * This will override the heads-up layout that would otherwise be constructed by this
3805 * Builder object.
3806 */
3807 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3808 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003809 return this;
3810 }
3811
Joe Onoratocb109a02011-01-18 17:57:41 -08003812 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003813 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3814 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003815 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3816 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3817 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003818 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003819 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003820 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003821 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003822 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003823 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003824 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003825 return this;
3826 }
3827
Joe Onoratocb109a02011-01-18 17:57:41 -08003828 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003829 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3830 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003831 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003832 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003833 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003834 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003835 return this;
3836 }
3837
Joe Onoratocb109a02011-01-18 17:57:41 -08003838 /**
3839 * An intent to launch instead of posting the notification to the status bar.
3840 * Only for use with extremely high-priority notifications demanding the user's
3841 * <strong>immediate</strong> attention, such as an incoming phone call or
3842 * alarm clock that the user has explicitly set to a particular time.
3843 * If this facility is used for something else, please give the user an option
3844 * to turn it off and use a normal notification, as this can be extremely
3845 * disruptive.
3846 *
Chris Wren47c20a12014-06-18 17:27:29 -04003847 * <p>
3848 * The system UI may choose to display a heads-up notification, instead of
3849 * launching this intent, while the user is using the device.
3850 * </p>
3851 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003852 * @param intent The pending intent to launch.
3853 * @param highPriority Passing true will cause this notification to be sent
3854 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003855 *
3856 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003857 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003858 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003859 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003860 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3861 return this;
3862 }
3863
Joe Onoratocb109a02011-01-18 17:57:41 -08003864 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003865 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003866 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003867 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003868 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003869 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003870 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003871 return this;
3872 }
3873
Joe Onoratocb109a02011-01-18 17:57:41 -08003874 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003875 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003876 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003877 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003878 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003879 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003880 setTicker(tickerText);
3881 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003882 return this;
3883 }
3884
Joe Onoratocb109a02011-01-18 17:57:41 -08003885 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003886 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003887 *
3888 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04003889 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3890 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04003891 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003892 public Builder setLargeIcon(Bitmap b) {
3893 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3894 }
3895
3896 /**
3897 * Add a large icon to the notification content view.
3898 *
3899 * In the platform template, this image will be shown on the left of the notification view
3900 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3901 * badge atop the large icon).
3902 */
3903 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003904 mN.mLargeIcon = icon;
3905 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08003906 return this;
3907 }
3908
Joe Onoratocb109a02011-01-18 17:57:41 -08003909 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003910 * Set the sound to play.
3911 *
John Spurlockc0650f022014-07-19 13:22:39 -04003912 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
3913 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003914 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003915 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003916 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003917 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08003918 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003919 mN.sound = sound;
3920 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08003921 return this;
3922 }
3923
Joe Onoratocb109a02011-01-18 17:57:41 -08003924 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003925 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08003926 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003927 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
3928 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003929 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003930 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07003931 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003932 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08003933 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003934 mN.sound = sound;
3935 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08003936 return this;
3937 }
3938
Joe Onoratocb109a02011-01-18 17:57:41 -08003939 /**
John Spurlockc0650f022014-07-19 13:22:39 -04003940 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
3941 * use during playback.
3942 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003943 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04003944 * @see Notification#sound
3945 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003946 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04003947 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003948 mN.sound = sound;
3949 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04003950 return this;
3951 }
3952
3953 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003954 * Set the vibration pattern to use.
3955 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003956 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
3957 * <code>pattern</code> parameter.
3958 *
Chris Wren47c20a12014-06-18 17:27:29 -04003959 * <p>
3960 * A notification that vibrates is more likely to be presented as a heads-up notification.
3961 * </p>
3962 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003963 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003964 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08003965 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003966 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003967 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003968 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08003969 return this;
3970 }
3971
Joe Onoratocb109a02011-01-18 17:57:41 -08003972 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003973 * Set the desired color for the indicator LED on the device, as well as the
3974 * blink duty cycle (specified in milliseconds).
3975 *
3976
3977 * Not all devices will honor all (or even any) of these values.
3978 *
Julia Reynolds529e3322017-02-06 08:33:01 -05003979 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003980 * @see Notification#ledARGB
3981 * @see Notification#ledOnMS
3982 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08003983 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003984 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08003985 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003986 mN.ledARGB = argb;
3987 mN.ledOnMS = onMs;
3988 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003989 if (onMs != 0 || offMs != 0) {
3990 mN.flags |= FLAG_SHOW_LIGHTS;
3991 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003992 return this;
3993 }
3994
Joe Onoratocb109a02011-01-18 17:57:41 -08003995 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003996 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08003997 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003998
3999 * Ongoing notifications cannot be dismissed by the user, so your application or service
4000 * must take care of canceling them.
4001 *
4002
4003 * They are typically used to indicate a background task that the user is actively engaged
4004 * with (e.g., playing music) or is pending in some way and therefore occupying the device
4005 * (e.g., a file download, sync operation, active network connection).
4006 *
4007
4008 * @see Notification#FLAG_ONGOING_EVENT
Joe Onoratocb109a02011-01-18 17:57:41 -08004009 */
Joe Onorato46439ce2010-11-19 13:56:21 -08004010 public Builder setOngoing(boolean ongoing) {
4011 setFlag(FLAG_ONGOING_EVENT, ongoing);
4012 return this;
4013 }
4014
Joe Onoratocb109a02011-01-18 17:57:41 -08004015 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08004016 * Set whether this notification should be colorized. When set, the color set with
4017 * {@link #setColor(int)} will be used as the background color of this notification.
4018 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08004019 * This should only be used for high priority ongoing tasks like navigation, an ongoing
4020 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08004021 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07004022 * For most styles, the coloring will only be applied if the notification is for a
4023 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08004024 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07004025 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08004026 *
Aurimas Liutikas7f695332018-05-31 21:07:32 -07004027 * @see #setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08004028 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08004029 */
4030 public Builder setColorized(boolean colorize) {
4031 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
4032 return this;
4033 }
4034
4035 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08004036 * Set this flag if you would only like the sound, vibrate
4037 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004038 *
4039 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08004040 */
Joe Onorato46439ce2010-11-19 13:56:21 -08004041 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
4042 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
4043 return this;
4044 }
4045
Joe Onoratocb109a02011-01-18 17:57:41 -08004046 /**
Julia Reynolds04499532016-09-13 14:04:53 -04004047 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004048 *
4049 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08004050 */
Joe Onorato46439ce2010-11-19 13:56:21 -08004051 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08004052 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08004053 return this;
4054 }
4055
Joe Onoratocb109a02011-01-18 17:57:41 -08004056 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08004057 * Set whether or not this notification should not bridge to other devices.
4058 *
4059 * <p>Some notifications can be bridged to other devices for remote display.
4060 * This hint can be set to recommend this notification not be bridged.
4061 */
4062 public Builder setLocalOnly(boolean localOnly) {
4063 setFlag(FLAG_LOCAL_ONLY, localOnly);
4064 return this;
4065 }
4066
4067 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004068 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08004069 * <p>
4070 * The value should be one or more of the following fields combined with
4071 * bitwise-or:
4072 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
4073 * <p>
4074 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004075 *
4076 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05004077 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004078 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08004079 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004080 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004081 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004082 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08004083 return this;
4084 }
4085
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004086 /**
4087 * Set the priority of this notification.
4088 *
4089 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004090 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004091 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004092 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07004093 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004094 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004095 return this;
4096 }
Joe Malin8d40d042012-11-05 11:36:40 -08004097
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004098 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04004099 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08004100 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04004101 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004102 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04004103 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004104 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004105 return this;
4106 }
4107
4108 /**
Chris Wrendde75302014-03-26 17:24:15 -04004109 * Add a person that is relevant to this notification.
4110 *
Chris Wrene6c48932014-09-29 17:19:27 -04004111 * <P>
4112 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04004113 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4114 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4115 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04004116 * </P>
4117 *
4118 * <P>
4119 * The person should be specified by the {@code String} representation of a
4120 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
4121 * </P>
4122 *
4123 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
4124 * URIs. The path part of these URIs must exist in the contacts database, in the
4125 * appropriate column, or the reference will be discarded as invalid. Telephone schema
4126 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
Selim Cineke7238dd2017-12-14 17:48:32 -08004127 * It is also possible to provide a URI with the schema {@code name:} in order to uniquely
4128 * identify a person without an entry in the contacts database.
Chris Wrene6c48932014-09-29 17:19:27 -04004129 * </P>
4130 *
4131 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04004132 * @see Notification#EXTRA_PEOPLE
Selim Cineke7238dd2017-12-14 17:48:32 -08004133 * @deprecated use {@link #addPerson(Person)}
Chris Wrendde75302014-03-26 17:24:15 -04004134 */
Chris Wrene6c48932014-09-29 17:19:27 -04004135 public Builder addPerson(String uri) {
Selim Cinek9acd6732018-03-23 16:39:02 -07004136 addPerson(new Person.Builder().setUri(uri).build());
Selim Cineke7238dd2017-12-14 17:48:32 -08004137 return this;
4138 }
4139
4140 /**
4141 * Add a person that is relevant to this notification.
4142 *
4143 * <P>
4144 * Depending on user preferences, this annotation may allow the notification to pass
4145 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4146 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4147 * appear more prominently in the user interface.
4148 * </P>
4149 *
4150 * <P>
4151 * A person should usually contain a uri in order to benefit from the ranking boost.
4152 * However, even if no uri is provided, it's beneficial to provide other people in the
4153 * notification, such that listeners and voice only devices can announce and handle them
4154 * properly.
4155 * </P>
4156 *
4157 * @param person the person to add.
4158 * @see Notification#EXTRA_PEOPLE_LIST
4159 */
4160 public Builder addPerson(Person person) {
4161 mPersonList.add(person);
Chris Wrendde75302014-03-26 17:24:15 -04004162 return this;
4163 }
4164
4165 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004166 * Set this notification to be part of a group of notifications sharing the same key.
4167 * Grouped notifications may display in a cluster or stack on devices which
4168 * support such rendering.
4169 *
4170 * <p>To make this notification the summary for its group, also call
4171 * {@link #setGroupSummary}. A sort order can be specified for group members by using
4172 * {@link #setSortKey}.
4173 * @param groupKey The group key of the group.
4174 * @return this object for method chaining
4175 */
4176 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004177 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004178 return this;
4179 }
4180
4181 /**
4182 * Set this notification to be the group summary for a group of notifications.
4183 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04004184 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
4185 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004186 * @param isGroupSummary Whether this notification should be a group summary.
4187 * @return this object for method chaining
4188 */
4189 public Builder setGroupSummary(boolean isGroupSummary) {
4190 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
4191 return this;
4192 }
4193
4194 /**
4195 * Set a sort key that orders this notification among other notifications from the
4196 * same package. This can be useful if an external sort was already applied and an app
4197 * would like to preserve this. Notifications will be sorted lexicographically using this
4198 * value, although providing different priorities in addition to providing sort key may
4199 * cause this value to be ignored.
4200 *
4201 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07004202 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004203 *
4204 * @see String#compareTo(String)
4205 */
4206 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004207 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004208 return this;
4209 }
4210
4211 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004212 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004213 *
Griff Hazen720042b2014-02-24 15:46:56 -08004214 * <p>Values within the Bundle will replace existing extras values in this Builder.
4215 *
4216 * @see Notification#extras
4217 */
Griff Hazen959591e2014-05-15 22:26:18 -07004218 public Builder addExtras(Bundle extras) {
4219 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004220 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08004221 }
4222 return this;
4223 }
4224
4225 /**
4226 * Set metadata for this notification.
4227 *
4228 * <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 -04004229 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004230 * called.
4231 *
Griff Hazen720042b2014-02-24 15:46:56 -08004232 * <p>Replaces any existing extras values with those from the provided Bundle.
4233 * Use {@link #addExtras} to merge in metadata instead.
4234 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004235 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004236 */
Griff Hazen959591e2014-05-15 22:26:18 -07004237 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004238 if (extras != null) {
4239 mUserExtras = extras;
4240 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004241 return this;
4242 }
4243
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004244 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004245 * Get the current metadata Bundle used by this notification Builder.
4246 *
4247 * <p>The returned Bundle is shared with this Builder.
4248 *
4249 * <p>The current contents of this Bundle are copied into the Notification each time
4250 * {@link #build()} is called.
4251 *
4252 * @see Notification#extras
4253 */
4254 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004255 return mUserExtras;
4256 }
4257
4258 private Bundle getAllExtras() {
4259 final Bundle saveExtras = (Bundle) mUserExtras.clone();
4260 saveExtras.putAll(mN.extras);
4261 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08004262 }
4263
4264 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004265 * Add an action to this notification. Actions are typically displayed by
4266 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004267 * <p>
4268 * Every action must have an icon (32dp square and matching the
4269 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4270 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4271 * <p>
4272 * A notification in its expanded form can display up to 3 actions, from left to right in
4273 * the order they were added. Actions will not be displayed when the notification is
4274 * collapsed, however, so be sure that any essential functions may be accessed by the user
4275 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004276 *
4277 * @param icon Resource ID of a drawable that represents the action.
4278 * @param title Text describing the action.
4279 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04004280 *
4281 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004282 */
Dan Sandler86647982015-05-13 23:41:13 -04004283 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004284 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004285 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004286 return this;
4287 }
4288
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004289 /**
Griff Hazen959591e2014-05-15 22:26:18 -07004290 * Add an action to this notification. Actions are typically displayed by
4291 * the system as a button adjacent to the notification content.
4292 * <p>
4293 * Every action must have an icon (32dp square and matching the
4294 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4295 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4296 * <p>
4297 * A notification in its expanded form can display up to 3 actions, from left to right in
4298 * the order they were added. Actions will not be displayed when the notification is
4299 * collapsed, however, so be sure that any essential functions may be accessed by the user
4300 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
4301 *
4302 * @param action The action to add.
4303 */
4304 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08004305 if (action != null) {
4306 mActions.add(action);
4307 }
Griff Hazen959591e2014-05-15 22:26:18 -07004308 return this;
4309 }
4310
4311 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004312 * Alter the complete list of actions attached to this notification.
4313 * @see #addAction(Action).
4314 *
4315 * @param actions
4316 * @return
4317 */
4318 public Builder setActions(Action... actions) {
4319 mActions.clear();
4320 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08004321 if (actions[i] != null) {
4322 mActions.add(actions[i]);
4323 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004324 }
4325 return this;
4326 }
4327
4328 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004329 * Add a rich notification style to be applied at build time.
4330 *
4331 * @param style Object responsible for modifying the notification style.
4332 */
4333 public Builder setStyle(Style style) {
4334 if (mStyle != style) {
4335 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004336 if (mStyle != null) {
4337 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004338 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
4339 } else {
4340 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004341 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004342 }
4343 return this;
4344 }
4345
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004346 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05004347 * Returns the style set by {@link #setStyle(Style)}.
4348 */
4349 public Style getStyle() {
4350 return mStyle;
4351 }
4352
4353 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004354 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07004355 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004356 * @return The same Builder.
4357 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06004358 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004359 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004360 return this;
4361 }
4362
4363 /**
4364 * Supply a replacement Notification whose contents should be shown in insecure contexts
4365 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
4366 * @param n A replacement notification, presumably with some or all info redacted.
4367 * @return The same Builder.
4368 */
4369 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004370 if (n != null) {
4371 mN.publicVersion = new Notification();
4372 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
4373 } else {
4374 mN.publicVersion = null;
4375 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004376 return this;
4377 }
4378
Griff Hazenb720abe2014-05-20 13:15:30 -07004379 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004380 * Apply an extender to this notification builder. Extenders may be used to add
4381 * metadata or change options on this builder.
4382 */
Griff Hazen61a9e862014-05-22 16:05:19 -07004383 public Builder extend(Extender extender) {
4384 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004385 return this;
4386 }
4387
Dan Sandler4e787062015-06-17 15:09:48 -04004388 /**
4389 * @hide
4390 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04004391 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08004392 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004393 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004394 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004395 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004396 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04004397 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08004398 }
4399
Dan Sandler26e81cf2014-05-06 10:01:27 -04004400 /**
4401 * Sets {@link Notification#color}.
4402 *
4403 * @param argb The accent color to use
4404 *
4405 * @return The same Builder.
4406 */
Tor Norbye80756e32015-03-02 09:39:27 -08004407 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004408 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004409 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04004410 return this;
4411 }
4412
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004413 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04004414 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
4415 // This user can never be a badged profile,
4416 // and also includes USER_ALL system notifications.
4417 return null;
4418 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02004419 // Note: This assumes that the current user can read the profile badge of the
4420 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08004421 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05004422 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004423 }
4424
4425 private Bitmap getProfileBadge() {
4426 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01004427 if (badge == null) {
4428 return null;
4429 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004430 final int size = mContext.getResources().getDimensionPixelSize(
4431 R.dimen.notification_badge_size);
4432 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004433 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004434 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004435 badge.draw(canvas);
4436 return bitmap;
4437 }
4438
Selim Cinekc848c3a2016-01-13 15:27:30 -08004439 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01004440 Bitmap profileBadge = getProfileBadge();
4441
Kenny Guy98193ea2014-07-24 19:54:37 +01004442 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004443 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
4444 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004445 if (isColorized()) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004446 contentView.setDrawableTint(R.id.profile_badge, false,
4447 getPrimaryTextColor(), PorterDuff.Mode.SRC_ATOP);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004448 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004449 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004450 }
4451
Julia Reynoldsfc640012018-02-21 12:25:27 -05004452 /**
4453 * @hide
4454 */
4455 public boolean usesStandardHeader() {
4456 if (mN.mUsesStandardHeader) {
4457 return true;
4458 }
4459 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.N) {
4460 if (mN.contentView == null && mN.bigContentView == null) {
4461 return true;
4462 }
4463 }
4464 boolean contentViewUsesHeader = mN.contentView == null
4465 || STANDARD_LAYOUTS.contains(mN.contentView.getLayoutId());
4466 boolean bigContentViewUsesHeader = mN.bigContentView == null
4467 || STANDARD_LAYOUTS.contains(mN.bigContentView.getLayoutId());
4468 return contentViewUsesHeader && bigContentViewUsesHeader;
4469 }
4470
Christoph Studerfe718432014-09-01 18:21:18 +02004471 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004472 resetNotificationHeader(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02004473 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08004474 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004475 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08004476 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004477 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08004478 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08004479 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004480 }
4481
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004482 /**
4483 * Resets the notification header to its original state
4484 */
4485 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07004486 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
4487 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08004488 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004489 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02004490 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004491 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07004492 contentView.setTextViewText(R.id.header_text, null);
Selim Cinekafeed292017-12-12 17:32:44 -08004493 contentView.setViewVisibility(R.id.header_text_secondary, View.GONE);
4494 contentView.setTextViewText(R.id.header_text_secondary, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004495 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinekafeed292017-12-12 17:32:44 -08004496 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004497 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004498 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004499 contentView.setImageViewIcon(R.id.profile_badge, null);
4500 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Gus Prevasa3226492018-10-23 11:10:09 -04004501 contentView.setViewVisibility(R.id.alerted_icon, View.GONE);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004502 mN.mUsesStandardHeader = false;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004503 }
4504
Selim Cinek384804b2018-04-18 14:31:07 +08004505 private RemoteViews applyStandardTemplate(int resId, TemplateBindResult result) {
4506 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this),
4507 result);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004508 }
4509
4510 /**
4511 * @param hasProgress whether the progress bar should be shown and set
Selim Cinek384804b2018-04-18 14:31:07 +08004512 * @param result
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004513 */
Selim Cinek384804b2018-04-18 14:31:07 +08004514 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress,
4515 TemplateBindResult result) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004516 return applyStandardTemplate(resId, mParams.reset().hasProgress(hasProgress)
Selim Cinek384804b2018-04-18 14:31:07 +08004517 .fillTextsFrom(this), result);
Adrian Roosc1a80b02016-04-05 14:54:55 -07004518 }
4519
Selim Cinek384804b2018-04-18 14:31:07 +08004520 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p,
4521 TemplateBindResult result) {
Kenny Guy77320062014-08-27 21:37:15 +01004522 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04004523
Christoph Studerfe718432014-09-01 18:21:18 +02004524 resetStandardTemplate(contentView);
4525
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004526 final Bundle ex = mN.extras;
Selim Cinek7b9605b2017-01-19 17:36:00 -08004527 updateBackgroundColor(contentView);
Selim Cinekafeed292017-12-12 17:32:44 -08004528 bindNotificationHeader(contentView, p.ambient, p.headerTextSecondary);
Selim Cinek384804b2018-04-18 14:31:07 +08004529 bindLargeIconAndReply(contentView, p, result);
Adrian Roos70d7aa32017-01-11 15:39:06 -08004530 boolean showProgress = handleProgressBar(p.hasProgress, contentView, ex);
4531 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08004532 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004533 contentView.setTextViewText(R.id.title, processTextSpans(p.title));
Adrian Roos72171622017-01-27 10:32:06 -08004534 if (!p.ambient) {
4535 setTextViewColorPrimary(contentView, R.id.title);
4536 }
Selim Cinek954cc232016-05-20 13:29:23 -07004537 contentView.setViewLayoutWidth(R.id.title, showProgress
4538 ? ViewGroup.LayoutParams.WRAP_CONTENT
4539 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08004540 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08004541 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08004542 int textId = showProgress ? com.android.internal.R.id.text_line_1
4543 : com.android.internal.R.id.text;
Selim Cinek48f66b72017-08-18 16:17:51 -07004544 contentView.setTextViewText(textId, processTextSpans(p.text));
Adrian Roos72171622017-01-27 10:32:06 -08004545 if (!p.ambient) {
4546 setTextViewColorSecondary(contentView, textId);
4547 }
Selim Cinek41598732016-01-11 16:58:37 -08004548 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08004549 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08004550
Selim Cinek279fa862016-06-14 10:57:25 -07004551 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004552
Selim Cinek29603462015-11-17 19:04:39 -08004553 return contentView;
4554 }
4555
Selim Cinek48f66b72017-08-18 16:17:51 -07004556 private CharSequence processTextSpans(CharSequence text) {
Lucas Dupind3c99322018-09-27 10:22:29 -07004557 if (hasForegroundColor() || mInNightMode) {
Lucas Dupina291d192018-06-07 13:59:42 -07004558 return ContrastColorUtil.clearColorSpans(text);
Selim Cinek48f66b72017-08-18 16:17:51 -07004559 }
4560 return text;
4561 }
4562
Selim Cinek7b9605b2017-01-19 17:36:00 -08004563 private void setTextViewColorPrimary(RemoteViews contentView, int id) {
4564 ensureColors();
4565 contentView.setTextColor(id, mPrimaryTextColor);
4566 }
4567
Selim Cinek48f66b72017-08-18 16:17:51 -07004568 private boolean hasForegroundColor() {
4569 return mForegroundColor != COLOR_INVALID;
4570 }
4571
Selim Cinek389edcd2017-05-11 19:16:44 -07004572 /**
4573 * @return the primary text color
4574 * @hide
4575 */
4576 @VisibleForTesting
4577 public int getPrimaryTextColor() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004578 ensureColors();
4579 return mPrimaryTextColor;
4580 }
4581
Selim Cinek389edcd2017-05-11 19:16:44 -07004582 /**
4583 * @return the secondary text color
4584 * @hide
4585 */
4586 @VisibleForTesting
4587 public int getSecondaryTextColor() {
4588 ensureColors();
4589 return mSecondaryTextColor;
4590 }
4591
Selim Cinek7b9605b2017-01-19 17:36:00 -08004592 private void setTextViewColorSecondary(RemoteViews contentView, int id) {
4593 ensureColors();
4594 contentView.setTextColor(id, mSecondaryTextColor);
4595 }
4596
4597 private void ensureColors() {
4598 int backgroundColor = getBackgroundColor();
4599 if (mPrimaryTextColor == COLOR_INVALID
4600 || mSecondaryTextColor == COLOR_INVALID
Selim Cinek7b9605b2017-01-19 17:36:00 -08004601 || mTextColorsAreForBackground != backgroundColor) {
4602 mTextColorsAreForBackground = backgroundColor;
Selim Cinek48f66b72017-08-18 16:17:51 -07004603 if (!hasForegroundColor() || !isColorized()) {
Lucas Dupina291d192018-06-07 13:59:42 -07004604 mPrimaryTextColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004605 backgroundColor, mInNightMode);
Lucas Dupina291d192018-06-07 13:59:42 -07004606 mSecondaryTextColor = ContrastColorUtil.resolveSecondaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004607 backgroundColor, mInNightMode);
Selim Cinekc7f5a822018-03-20 19:32:06 -07004608 if (backgroundColor != COLOR_DEFAULT && isColorized()) {
Lucas Dupina291d192018-06-07 13:59:42 -07004609 mPrimaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004610 mPrimaryTextColor, backgroundColor, 4.5);
Lucas Dupina291d192018-06-07 13:59:42 -07004611 mSecondaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004612 mSecondaryTextColor, backgroundColor, 4.5);
4613 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07004614 } else {
Lucas Dupina291d192018-06-07 13:59:42 -07004615 double backLum = ContrastColorUtil.calculateLuminance(backgroundColor);
4616 double textLum = ContrastColorUtil.calculateLuminance(mForegroundColor);
4617 double contrast = ContrastColorUtil.calculateContrast(mForegroundColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004618 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07004619 // We only respect the given colors if worst case Black or White still has
4620 // contrast
4621 boolean backgroundLight = backLum > textLum
4622 && satisfiesTextContrast(backgroundColor, Color.BLACK)
4623 || backLum <= textLum
4624 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004625 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07004626 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004627 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004628 mForegroundColor,
4629 backgroundColor,
4630 true /* findFG */,
4631 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004632 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004633 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004634 } else {
4635 mSecondaryTextColor =
Lucas Dupina291d192018-06-07 13:59:42 -07004636 ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004637 mForegroundColor,
4638 backgroundColor,
4639 true /* findFG */,
4640 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004641 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004642 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004643 }
4644 } else {
4645 mPrimaryTextColor = mForegroundColor;
Lucas Dupina291d192018-06-07 13:59:42 -07004646 mSecondaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004647 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4648 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Lucas Dupina291d192018-06-07 13:59:42 -07004649 if (ContrastColorUtil.calculateContrast(mSecondaryTextColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004650 backgroundColor) < 4.5f) {
4651 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07004652 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004653 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004654 mSecondaryTextColor,
4655 backgroundColor,
4656 true /* findFG */,
4657 4.5f);
4658 } else {
4659 mSecondaryTextColor
Lucas Dupina291d192018-06-07 13:59:42 -07004660 = ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004661 mSecondaryTextColor,
4662 backgroundColor,
4663 true /* findFG */,
4664 4.5f);
4665 }
Lucas Dupina291d192018-06-07 13:59:42 -07004666 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004667 mSecondaryTextColor, backgroundLight
4668 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4669 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004670 }
4671 }
4672 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004673 }
4674 }
4675
4676 private void updateBackgroundColor(RemoteViews contentView) {
4677 if (isColorized()) {
4678 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
4679 getBackgroundColor());
4680 } else {
4681 // Clear it!
4682 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4683 0);
4684 }
4685 }
4686
Selim Cinek860b6da2015-12-16 19:02:19 -08004687 /**
4688 * @param remoteView the remote view to update the minheight in
4689 * @param hasMinHeight does it have a mimHeight
4690 * @hide
4691 */
4692 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4693 int minHeight = 0;
4694 if (hasMinHeight) {
4695 // we need to set the minHeight of the notification
4696 minHeight = mContext.getResources().getDimensionPixelSize(
4697 com.android.internal.R.dimen.notification_min_content_height);
4698 }
4699 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4700 }
4701
Selim Cinek29603462015-11-17 19:04:39 -08004702 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004703 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4704 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4705 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4706 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004707 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004708 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004709 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004710 contentView.setProgressBackgroundTintList(
4711 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4712 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08004713 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08004714 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004715 contentView.setProgressTintList(R.id.progress, colorStateList);
4716 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004717 }
Selim Cinek29603462015-11-17 19:04:39 -08004718 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004719 } else {
4720 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004721 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004722 }
Joe Onorato561d3852010-11-20 18:09:34 -08004723 }
4724
Selim Cinek384804b2018-04-18 14:31:07 +08004725 private void bindLargeIconAndReply(RemoteViews contentView, StandardTemplateParams p,
4726 TemplateBindResult result) {
4727 boolean largeIconShown = bindLargeIcon(contentView, p.hideLargeIcon || p.ambient);
Selim Cinek1c72fa02018-04-23 18:00:54 +08004728 boolean replyIconShown = bindReplyIcon(contentView, p.hideReplyIcon || p.ambient);
Selim Cinek384804b2018-04-18 14:31:07 +08004729 contentView.setViewVisibility(R.id.right_icon_container,
Selim Cinek1c72fa02018-04-23 18:00:54 +08004730 largeIconShown || replyIconShown ? View.VISIBLE : View.GONE);
4731 int marginEnd = calculateMarginEnd(largeIconShown, replyIconShown);
4732 contentView.setViewLayoutMarginEnd(R.id.line1, marginEnd);
4733 contentView.setViewLayoutMarginEnd(R.id.text, marginEnd);
4734 contentView.setViewLayoutMarginEnd(R.id.progress, marginEnd);
Selim Cinek384804b2018-04-18 14:31:07 +08004735 if (result != null) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08004736 result.setIconMarginEnd(marginEnd);
Selim Cinek384804b2018-04-18 14:31:07 +08004737 }
4738 }
4739
Selim Cinek1c72fa02018-04-23 18:00:54 +08004740 private int calculateMarginEnd(boolean largeIconShown, boolean replyIconShown) {
4741 int marginEnd = 0;
4742 int contentMargin = mContext.getResources().getDimensionPixelSize(
4743 R.dimen.notification_content_margin_end);
4744 int iconSize = mContext.getResources().getDimensionPixelSize(
4745 R.dimen.notification_right_icon_size);
4746 if (replyIconShown) {
4747 // The size of the reply icon
4748 marginEnd += iconSize;
4749
4750 int replyInset = mContext.getResources().getDimensionPixelSize(
4751 R.dimen.notification_reply_inset);
4752 // We're subtracting the inset of the reply icon to make sure it's
4753 // aligned nicely on the right, and remove it from the following padding
4754 marginEnd -= replyInset * 2;
4755 }
4756 if (largeIconShown) {
4757 // adding size of the right icon
4758 marginEnd += iconSize;
4759
4760 if (replyIconShown) {
4761 // We also add some padding to the reply icon if it's around
4762 marginEnd += contentMargin;
4763 }
4764 }
4765 if (replyIconShown || largeIconShown) {
4766 // The padding to the content
4767 marginEnd += contentMargin;
4768 }
4769 return marginEnd;
4770 }
4771
Selim Cinek384804b2018-04-18 14:31:07 +08004772 /**
4773 * Bind the large icon.
4774 * @return if the largeIcon is visible
4775 */
4776 private boolean bindLargeIcon(RemoteViews contentView, boolean hideLargeIcon) {
Selim Cinek279fa862016-06-14 10:57:25 -07004777 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4778 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4779 }
Selim Cinek88188f22017-09-19 16:46:56 -07004780 boolean showLargeIcon = mN.mLargeIcon != null && !hideLargeIcon;
4781 if (showLargeIcon) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004782 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4783 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
4784 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Selim Cinek88188f22017-09-19 16:46:56 -07004785 }
Selim Cinek384804b2018-04-18 14:31:07 +08004786 return showLargeIcon;
4787 }
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004788
Selim Cinek384804b2018-04-18 14:31:07 +08004789 /**
4790 * Bind the reply icon.
4791 * @return if the reply icon is visible
4792 */
4793 private boolean bindReplyIcon(RemoteViews contentView, boolean hideReplyIcon) {
4794 boolean actionVisible = !hideReplyIcon;
4795 Action action = null;
Selim Cinek88188f22017-09-19 16:46:56 -07004796 if (actionVisible) {
Selim Cinek384804b2018-04-18 14:31:07 +08004797 action = findReplyAction();
4798 actionVisible = action != null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004799 }
Selim Cinek384804b2018-04-18 14:31:07 +08004800 if (actionVisible) {
4801 contentView.setViewVisibility(R.id.reply_icon_action, View.VISIBLE);
4802 contentView.setDrawableTint(R.id.reply_icon_action,
4803 false /* targetBackground */,
Selim Cinek4717d862018-04-19 09:19:15 +08004804 getNeutralColor(),
Selim Cinek384804b2018-04-18 14:31:07 +08004805 PorterDuff.Mode.SRC_ATOP);
4806 contentView.setOnClickPendingIntent(R.id.reply_icon_action, action.actionIntent);
4807 contentView.setRemoteInputs(R.id.reply_icon_action, action.mRemoteInputs);
4808 } else {
4809 contentView.setRemoteInputs(R.id.reply_icon_action, null);
4810 }
4811 contentView.setViewVisibility(R.id.reply_icon_action,
4812 actionVisible ? View.VISIBLE : View.GONE);
4813 return actionVisible;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004814 }
4815
4816 private Action findReplyAction() {
4817 ArrayList<Action> actions = mActions;
4818 if (mOriginalActions != null) {
4819 actions = mOriginalActions;
4820 }
4821 int numActions = actions.size();
4822 for (int i = 0; i < numActions; i++) {
4823 Action action = actions.get(i);
4824 if (hasValidRemoteInput(action)) {
4825 return action;
4826 }
4827 }
4828 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004829 }
4830
Selim Cinekafeed292017-12-12 17:32:44 -08004831 private void bindNotificationHeader(RemoteViews contentView, boolean ambient,
4832 CharSequence secondaryHeaderText) {
Adrian Roos487374f2017-01-11 15:48:14 -08004833 bindSmallIcon(contentView, ambient);
4834 bindHeaderAppName(contentView, ambient);
4835 if (!ambient) {
4836 // Ambient view does not have these
4837 bindHeaderText(contentView);
Selim Cinekafeed292017-12-12 17:32:44 -08004838 bindHeaderTextSecondary(contentView, secondaryHeaderText);
Adrian Roos487374f2017-01-11 15:48:14 -08004839 bindHeaderChronometerAndTime(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08004840 bindProfileBadge(contentView);
4841 }
Julia Reynolds6013e5c2018-04-18 08:43:34 -04004842 bindActivePermissions(contentView, ambient);
Adrian Roosd83e9992017-03-16 15:17:57 -07004843 bindExpandButton(contentView);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004844 mN.mUsesStandardHeader = true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004845 }
4846
Julia Reynolds6013e5c2018-04-18 08:43:34 -04004847 private void bindActivePermissions(RemoteViews contentView, boolean ambient) {
Selim Cinek4717d862018-04-19 09:19:15 +08004848 int color = ambient ? resolveAmbientColor() : getNeutralColor();
Julia Reynoldsb887b232018-04-10 16:38:08 -04004849 contentView.setDrawableTint(R.id.camera, false, color, PorterDuff.Mode.SRC_ATOP);
4850 contentView.setDrawableTint(R.id.mic, false, color, PorterDuff.Mode.SRC_ATOP);
4851 contentView.setDrawableTint(R.id.overlay, false, color, PorterDuff.Mode.SRC_ATOP);
4852 }
4853
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004854 private void bindExpandButton(RemoteViews contentView) {
Selim Cinekc7f5a822018-03-20 19:32:06 -07004855 int color = isColorized() ? getPrimaryTextColor() : getSecondaryTextColor();
Sunny Goyal5b153922017-09-21 21:00:36 -07004856 contentView.setDrawableTint(R.id.expand_button, false, color,
4857 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08004858 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08004859 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004860 }
4861
4862 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
4863 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08004864 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004865 setTextViewColorSecondary(contentView, R.id.time_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004866 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
4867 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
4868 contentView.setLong(R.id.chronometer, "setBase",
4869 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
4870 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07004871 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07004872 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004873 setTextViewColorSecondary(contentView, R.id.chronometer);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004874 } else {
4875 contentView.setViewVisibility(R.id.time, View.VISIBLE);
4876 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004877 setTextViewColorSecondary(contentView, R.id.time);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004878 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004879 } else {
4880 // We still want a time to be set but gone, such that we can show and hide it
4881 // on demand in case it's a child notification without anything in the header
4882 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004883 }
4884 }
4885
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004886 private void bindHeaderText(RemoteViews contentView) {
4887 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4888 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05004889 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004890 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05004891 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004892 if (headerText == null
4893 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4894 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
4895 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
4896 }
4897 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004898 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek48f66b72017-08-18 16:17:51 -07004899 contentView.setTextViewText(R.id.header_text, processTextSpans(
4900 processLegacyText(headerText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004901 setTextViewColorSecondary(contentView, R.id.header_text);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004902 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
4903 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004904 setTextViewColorSecondary(contentView, R.id.header_text_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004905 }
4906 }
4907
Selim Cinekafeed292017-12-12 17:32:44 -08004908 private void bindHeaderTextSecondary(RemoteViews contentView, CharSequence secondaryText) {
4909 if (!TextUtils.isEmpty(secondaryText)) {
4910 contentView.setTextViewText(R.id.header_text_secondary, processTextSpans(
4911 processLegacyText(secondaryText)));
4912 setTextViewColorSecondary(contentView, R.id.header_text_secondary);
4913 contentView.setViewVisibility(R.id.header_text_secondary, View.VISIBLE);
4914 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.VISIBLE);
4915 setTextViewColorSecondary(contentView, R.id.header_text_secondary_divider);
4916 }
4917 }
4918
Adrian Rooseba05822016-04-22 17:09:27 -07004919 /**
4920 * @hide
4921 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01004922 @UnsupportedAppUsage
Adrian Rooseba05822016-04-22 17:09:27 -07004923 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004924 CharSequence name = null;
4925 final PackageManager pm = mContext.getPackageManager();
4926 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
4927 // only system packages which lump together a bunch of unrelated stuff
4928 // may substitute a different name to make the purpose of the
4929 // notification more clear. the correct package label should always
4930 // be accessible via SystemUI.
4931 final String pkg = mContext.getPackageName();
4932 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
4933 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
4934 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
4935 name = subName;
4936 } else {
4937 Log.w(TAG, "warning: pkg "
4938 + pkg + " attempting to substitute app name '" + subName
4939 + "' without holding perm "
4940 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
4941 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004942 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04004943 if (TextUtils.isEmpty(name)) {
4944 name = pm.getApplicationLabel(mContext.getApplicationInfo());
4945 }
4946 if (TextUtils.isEmpty(name)) {
4947 // still nothing?
4948 return null;
4949 }
4950
4951 return String.valueOf(name);
4952 }
Adrian Roos487374f2017-01-11 15:48:14 -08004953 private void bindHeaderAppName(RemoteViews contentView, boolean ambient) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004954 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos72171622017-01-27 10:32:06 -08004955 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004956 setTextViewColorPrimary(contentView, R.id.app_name_text);
4957 } else {
4958 contentView.setTextColor(R.id.app_name_text,
Selim Cinekc7f5a822018-03-20 19:32:06 -07004959 ambient ? resolveAmbientColor() : getSecondaryTextColor());
Selim Cinek7b9605b2017-01-19 17:36:00 -08004960 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004961 }
4962
Adrian Roos487374f2017-01-11 15:48:14 -08004963 private void bindSmallIcon(RemoteViews contentView, boolean ambient) {
Selim Cinek279fa862016-06-14 10:57:25 -07004964 if (mN.mSmallIcon == null && mN.icon != 0) {
4965 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
4966 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004967 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Sunny Goyal5b153922017-09-21 21:00:36 -07004968 contentView.setInt(R.id.icon, "setImageLevel", mN.iconLevel);
Adrian Roos487374f2017-01-11 15:48:14 -08004969 processSmallIconColor(mN.mSmallIcon, contentView, ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004970 }
4971
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004972 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004973 * @return true if the built notification will show the time or the chronometer; false
4974 * otherwise
4975 */
4976 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07004977 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004978 }
4979
Christoph Studerfe718432014-09-01 18:21:18 +02004980 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004981 // actions_container is only reset when there are no actions to avoid focus issues with
4982 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02004983 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004984 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08004985
4986 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
4987 big.setTextViewText(R.id.notification_material_reply_text_1, null);
Kenny Guya0f6de82018-04-06 16:20:16 +01004988 big.setViewVisibility(R.id.notification_material_reply_text_1_container, View.GONE);
4989 big.setViewVisibility(R.id.notification_material_reply_progress, View.GONE);
Adrian Roose458aa82015-12-08 16:17:19 -08004990
4991 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
4992 big.setTextViewText(R.id.notification_material_reply_text_2, null);
4993 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
4994 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07004995
Selim Cineked64a142018-02-06 18:06:01 -08004996 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
4997 R.dimen.notification_content_margin);
Christoph Studerfe718432014-09-01 18:21:18 +02004998 }
4999
Selim Cinek384804b2018-04-18 14:31:07 +08005000 private RemoteViews applyStandardTemplateWithActions(int layoutId,
5001 TemplateBindResult result) {
5002 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this),
5003 result);
Adrian Roos48d746a2016-04-12 14:57:28 -07005004 }
5005
Gustav Sennton1463d832018-11-06 16:12:48 +00005006 private static List<Notification.Action> filterOutContextualActions(
5007 List<Notification.Action> actions) {
5008 List<Notification.Action> nonContextualActions = new ArrayList<>();
5009 for (Notification.Action action : actions) {
5010 if (action.getSemanticAction()
5011 != Action.SEMANTIC_ACTION_CONTEXTUAL_SUGGESTION) {
5012 nonContextualActions.add(action);
5013 }
5014 }
5015 return nonContextualActions;
5016 }
5017
Adrian Roos70d7aa32017-01-11 15:39:06 -08005018 private RemoteViews applyStandardTemplateWithActions(int layoutId,
Selim Cinek384804b2018-04-18 14:31:07 +08005019 StandardTemplateParams p, TemplateBindResult result) {
5020 RemoteViews big = applyStandardTemplate(layoutId, p, result);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005021
Christoph Studerfe718432014-09-01 18:21:18 +02005022 resetStandardTemplateWithActions(big);
5023
Adrian Roose458aa82015-12-08 16:17:19 -08005024 boolean validRemoteInput = false;
5025
Gustav Sennton1463d832018-11-06 16:12:48 +00005026 // In the UI contextual actions appear separately from the standard actions, so we
5027 // filter them out here.
5028 List<Notification.Action> nonContextualActions = filterOutContextualActions(mActions);
5029
5030 int N = nonContextualActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08005031 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005032 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005033 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08005034 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005035 big.setViewVisibility(R.id.actions, View.VISIBLE);
Selim Cineked64a142018-02-06 18:06:01 -08005036 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Daniel Sandler8680bf82012-05-15 16:52:52 -04005037 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005038 for (int i=0; i<N; i++) {
Gustav Sennton1463d832018-11-06 16:12:48 +00005039 Action action = nonContextualActions.get(i);
5040
Selim Cinekffcc7cf2018-02-06 17:43:51 -08005041 boolean actionHasValidInput = hasValidRemoteInput(action);
5042 validRemoteInput |= actionHasValidInput;
Adrian Roose458aa82015-12-08 16:17:19 -08005043
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005044 final RemoteViews button = generateActionButton(action, emphazisedMode,
Selim Cinek396caca2018-04-10 17:46:46 -07005045 p.ambient);
5046 if (actionHasValidInput && !emphazisedMode) {
Selim Cinekffcc7cf2018-02-06 17:43:51 -08005047 // Clear the drawable
5048 button.setInt(R.id.action0, "setBackgroundResource", 0);
5049 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005050 big.addView(R.id.actions, button);
5051 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07005052 } else {
5053 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005054 }
Adrian Roose458aa82015-12-08 16:17:19 -08005055
5056 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08005057 if (!p.ambient && validRemoteInput && replyText != null
Selim Cinekbee4e072018-05-21 22:06:43 -07005058 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])
5059 && p.maxRemoteInputHistory > 0) {
Kenny Guya0f6de82018-04-06 16:20:16 +01005060 boolean showSpinner = mN.extras.getBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER);
Adrian Roose458aa82015-12-08 16:17:19 -08005061 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Kenny Guya0f6de82018-04-06 16:20:16 +01005062 big.setViewVisibility(R.id.notification_material_reply_text_1_container,
5063 View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005064 big.setTextViewText(R.id.notification_material_reply_text_1,
5065 processTextSpans(replyText[0]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005066 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1);
Kenny Guya0f6de82018-04-06 16:20:16 +01005067 big.setViewVisibility(R.id.notification_material_reply_progress,
5068 showSpinner ? View.VISIBLE : View.GONE);
5069 big.setProgressIndeterminateTintList(
5070 R.id.notification_material_reply_progress,
5071 ColorStateList.valueOf(
5072 isColorized() ? getPrimaryTextColor() : resolveContrastColor()));
Adrian Roose458aa82015-12-08 16:17:19 -08005073
Selim Cinekbee4e072018-05-21 22:06:43 -07005074 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])
5075 && p.maxRemoteInputHistory > 1) {
Adrian Roose458aa82015-12-08 16:17:19 -08005076 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005077 big.setTextViewText(R.id.notification_material_reply_text_2,
5078 processTextSpans(replyText[1]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005079 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2);
Adrian Roose458aa82015-12-08 16:17:19 -08005080
Selim Cinekbee4e072018-05-21 22:06:43 -07005081 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])
5082 && p.maxRemoteInputHistory > 2) {
Adrian Roose458aa82015-12-08 16:17:19 -08005083 big.setViewVisibility(
5084 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005085 big.setTextViewText(R.id.notification_material_reply_text_3,
5086 processTextSpans(replyText[2]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005087 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3);
Adrian Roose458aa82015-12-08 16:17:19 -08005088 }
5089 }
5090 }
5091
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005092 return big;
5093 }
5094
Adrian Roose458aa82015-12-08 16:17:19 -08005095 private boolean hasValidRemoteInput(Action action) {
5096 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
5097 // Weird actions
5098 return false;
5099 }
5100
5101 RemoteInput[] remoteInputs = action.getRemoteInputs();
5102 if (remoteInputs == null) {
5103 return false;
5104 }
5105
5106 for (RemoteInput r : remoteInputs) {
5107 CharSequence[] choices = r.getChoices();
5108 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
5109 return true;
5110 }
5111 }
5112 return false;
5113 }
5114
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005115 /**
5116 * Construct a RemoteViews for the final 1U notification layout. In order:
5117 * 1. Custom contentView from the caller
5118 * 2. Style's proposed content view
5119 * 3. Standard template view
5120 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005121 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005122 return createContentView(false /* increasedheight */ );
5123 }
5124
5125 /**
5126 * Construct a RemoteViews for the smaller content view.
5127 *
5128 * @param increasedHeight true if this layout be created with an increased height. Some
5129 * styles may support showing more then just that basic 1U size
5130 * and the system may decide to render important notifications
5131 * slightly bigger even when collapsed.
5132 *
5133 * @hide
5134 */
5135 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005136 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005137 return mN.contentView;
5138 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005139 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005140 if (styleView != null) {
5141 return styleView;
5142 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005143 }
Selim Cinek384804b2018-04-18 14:31:07 +08005144 return applyStandardTemplate(getBaseLayoutResource(), null /* result */);
Joe Onorato46439ce2010-11-19 13:56:21 -08005145 }
5146
Selim Cineka7679b62017-05-10 16:33:25 -07005147 private boolean useExistingRemoteView() {
5148 return mStyle == null || (!mStyle.displayCustomViewInline()
5149 && !mRebuildStyledRemoteViews);
5150 }
5151
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005152 /**
5153 * Construct a RemoteViews for the final big notification layout.
5154 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005155 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05005156 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07005157 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005158 return mN.bigContentView;
5159 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05005160 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08005161 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005162 } else if (mActions.size() != 0) {
Selim Cinek384804b2018-04-18 14:31:07 +08005163 result = applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5164 null /* result */);
Selim Cinek850a8542015-11-11 11:48:36 -05005165 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08005166 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05005167 return result;
5168 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005169
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005170 /**
Selim Cinek414ad332017-02-24 19:06:12 -08005171 * Construct a RemoteViews for the final notification header only. This will not be
5172 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005173 *
Adrian Roos6f6e1592017-05-02 16:22:53 -07005174 * @param ambient if true, generate the header for the ambient display layout.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005175 * @hide
5176 */
Adrian Roos6f6e1592017-05-02 16:22:53 -07005177 public RemoteViews makeNotificationHeader(boolean ambient) {
Selim Cinek414ad332017-02-24 19:06:12 -08005178 Boolean colorized = (Boolean) mN.extras.get(EXTRA_COLORIZED);
5179 mN.extras.putBoolean(EXTRA_COLORIZED, false);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005180 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Adrian Roos6f6e1592017-05-02 16:22:53 -07005181 ambient ? R.layout.notification_template_ambient_header
5182 : R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005183 resetNotificationHeader(header);
Selim Cinekafeed292017-12-12 17:32:44 -08005184 bindNotificationHeader(header, ambient, null);
Selim Cinek414ad332017-02-24 19:06:12 -08005185 if (colorized != null) {
5186 mN.extras.putBoolean(EXTRA_COLORIZED, colorized);
5187 } else {
5188 mN.extras.remove(EXTRA_COLORIZED);
5189 }
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005190 return header;
5191 }
5192
Adrian Roos487374f2017-01-11 15:48:14 -08005193 /**
5194 * Construct a RemoteViews for the ambient version of the notification.
5195 *
5196 * @hide
5197 */
5198 public RemoteViews makeAmbientNotification() {
5199 RemoteViews ambient = applyStandardTemplateWithActions(
5200 R.layout.notification_template_material_ambient,
Selim Cinek384804b2018-04-18 14:31:07 +08005201 mParams.reset().ambient(true).fillTextsFrom(this).hasProgress(false),
5202 null /* result */);
Adrian Roos487374f2017-01-11 15:48:14 -08005203 return ambient;
5204 }
5205
Selim Cinek29603462015-11-17 19:04:39 -08005206 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005207 if (result != null) {
5208 result.setViewVisibility(R.id.text_line_1, View.GONE);
5209 }
Selim Cinek29603462015-11-17 19:04:39 -08005210 }
5211
Selim Cinek6743c0b2017-01-18 18:24:01 -08005212 /**
5213 * Adapt the Notification header if this view is used as an expanded view.
5214 *
5215 * @hide
5216 */
5217 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005218 if (result != null) {
5219 result.setBoolean(R.id.notification_header, "setExpanded", true);
5220 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005221 }
5222
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005223 /**
5224 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005225 *
5226 * @param increasedHeight true if this layout be created with an increased height. Some
5227 * styles may support showing more then just that basic 1U size
5228 * and the system may decide to render important notifications
5229 * slightly bigger even when collapsed.
5230 *
5231 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005232 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005233 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005234 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005235 return mN.headsUpContentView;
5236 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08005237 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
5238 if (styleView != null) {
5239 return styleView;
5240 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05005241 } else if (mActions.size() == 0) {
5242 return null;
5243 }
5244
Selim Cinekbee4e072018-05-21 22:06:43 -07005245 // We only want at most a single remote input history to be shown here, otherwise
5246 // the content would become squished.
5247 StandardTemplateParams p = mParams.reset().fillTextsFrom(this)
5248 .setMaxRemoteInputHistory(1);
5249 return applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5250 p,
5251 null /* result */);
Chris Wren8fd39ec2014-02-27 17:43:26 -05005252 }
5253
Selim Cinek624c02db2015-12-14 21:00:02 -08005254 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08005255 * Construct a RemoteViews for the final heads-up notification layout.
5256 */
5257 public RemoteViews createHeadsUpContentView() {
5258 return createHeadsUpContentView(false /* useIncreasedHeight */);
5259 }
5260
5261 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08005262 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5263 *
5264 * @hide
5265 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005266 @UnsupportedAppUsage
Selim Cinek624c02db2015-12-14 21:00:02 -08005267 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005268 return makePublicView(false /* ambient */);
5269 }
5270
5271 /**
5272 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5273 *
5274 * @hide
5275 */
5276 public RemoteViews makePublicAmbientNotification() {
5277 return makePublicView(true /* ambient */);
5278 }
5279
5280 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08005281 if (mN.publicVersion != null) {
5282 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005283 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08005284 }
5285 Bundle savedBundle = mN.extras;
5286 Style style = mStyle;
5287 mStyle = null;
5288 Icon largeIcon = mN.mLargeIcon;
5289 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07005290 Bitmap largeIconLegacy = mN.largeIcon;
5291 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005292 ArrayList<Action> actions = mActions;
5293 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08005294 Bundle publicExtras = new Bundle();
5295 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
5296 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
5297 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
5298 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07005299 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
5300 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cineked07b962018-04-30 14:39:35 -07005301 String appName = savedBundle.getString(EXTRA_SUBSTITUTE_APP_NAME);
5302 if (appName != null) {
5303 publicExtras.putString(EXTRA_SUBSTITUTE_APP_NAME, appName);
5304 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005305 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07005306 RemoteViews view;
5307 if (ambient) {
5308 publicExtras.putCharSequence(EXTRA_TITLE,
5309 mContext.getString(com.android.internal.R.string.notification_hidden_text));
5310 view = makeAmbientNotification();
5311 } else{
5312 view = makeNotificationHeader(false /* ambient */);
5313 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
5314 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005315 mN.extras = savedBundle;
5316 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07005317 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005318 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08005319 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005320 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08005321 }
5322
Selim Cinek6743c0b2017-01-18 18:24:01 -08005323 /**
5324 * Construct a content view for the display when low - priority
5325 *
5326 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
5327 * a new subtext is created consisting of the content of the
5328 * notification.
5329 * @hide
5330 */
5331 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
5332 int color = mN.color;
5333 mN.color = COLOR_DEFAULT;
5334 CharSequence summary = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
5335 if (!useRegularSubtext || TextUtils.isEmpty(summary)) {
5336 CharSequence newSummary = createSummaryText();
5337 if (!TextUtils.isEmpty(newSummary)) {
5338 mN.extras.putCharSequence(EXTRA_SUB_TEXT, newSummary);
5339 }
5340 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08005341
Adrian Roos6f6e1592017-05-02 16:22:53 -07005342 RemoteViews header = makeNotificationHeader(false /* ambient */);
Selim Cinek1b554392017-02-28 17:22:49 -08005343 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08005344 if (summary != null) {
5345 mN.extras.putCharSequence(EXTRA_SUB_TEXT, summary);
5346 } else {
5347 mN.extras.remove(EXTRA_SUB_TEXT);
5348 }
5349 mN.color = color;
5350 return header;
5351 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005352
Selim Cinek6743c0b2017-01-18 18:24:01 -08005353 private CharSequence createSummaryText() {
5354 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
5355 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
5356 return titleText;
5357 }
5358 SpannableStringBuilder summary = new SpannableStringBuilder();
5359 if (titleText == null) {
5360 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
5361 }
5362 BidiFormatter bidi = BidiFormatter.getInstance();
5363 if (titleText != null) {
5364 summary.append(bidi.unicodeWrap(titleText));
5365 }
5366 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
5367 if (titleText != null && contentText != null) {
5368 summary.append(bidi.unicodeWrap(mContext.getText(
5369 R.string.notification_header_divider_symbol_with_spaces)));
5370 }
5371 if (contentText != null) {
5372 summary.append(bidi.unicodeWrap(contentText));
5373 }
5374 return summary;
5375 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05005376
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005377 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Selim Cinek396caca2018-04-10 17:46:46 -07005378 boolean ambient) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04005379 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07005380 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005381 emphazisedMode ? getEmphasizedActionLayoutResource()
5382 : tombstone ? getActionTombstoneLayoutResource()
5383 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04005384 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04005385 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04005386 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005387 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005388 if (action.mRemoteInputs != null) {
5389 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
5390 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005391 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07005392 // change the background bgColor
Selim Cinek981962e2016-07-20 20:41:58 -07005393 CharSequence title = action.title;
5394 ColorStateList[] outResultColor = null;
Selim Cinek396caca2018-04-10 17:46:46 -07005395 int background = resolveBackgroundColor();
Selim Cinek981962e2016-07-20 20:41:58 -07005396 if (isLegacy()) {
Lucas Dupina291d192018-06-07 13:59:42 -07005397 title = ContrastColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07005398 } else {
5399 outResultColor = new ColorStateList[1];
Selim Cinek396caca2018-04-10 17:46:46 -07005400 title = ensureColorSpanContrast(title, background, outResultColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005401 }
Selim Cinek48f66b72017-08-18 16:17:51 -07005402 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005403 setTextViewColorPrimary(button, R.id.action0);
Selim Cinek396caca2018-04-10 17:46:46 -07005404 int rippleColor;
5405 boolean hasColorOverride = outResultColor != null && outResultColor[0] != null;
5406 if (hasColorOverride) {
5407 // There's a span spanning the full text, let's take it and use it as the
5408 // background color
5409 background = outResultColor[0].getDefaultColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005410 int textColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07005411 background, mInNightMode);
Selim Cinek396caca2018-04-10 17:46:46 -07005412 button.setTextColor(R.id.action0, textColor);
5413 rippleColor = textColor;
Anthony Chenad4d1582017-04-10 16:07:58 -07005414 } else if (mN.color != COLOR_DEFAULT && !isColorized() && mTintActionButtons) {
Selim Cinek396caca2018-04-10 17:46:46 -07005415 rippleColor = resolveContrastColor();
5416 button.setTextColor(R.id.action0, rippleColor);
5417 } else {
5418 rippleColor = getPrimaryTextColor();
Selim Cinek981962e2016-07-20 20:41:58 -07005419 }
Selim Cinek396caca2018-04-10 17:46:46 -07005420 // We only want about 20% alpha for the ripple
5421 rippleColor = (rippleColor & 0x00ffffff) | 0x33000000;
5422 button.setColorStateList(R.id.action0, "setRippleColor",
5423 ColorStateList.valueOf(rippleColor));
5424 button.setColorStateList(R.id.action0, "setButtonBackground",
5425 ColorStateList.valueOf(background));
5426 button.setBoolean(R.id.action0, "setHasStroke", !hasColorOverride);
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005427 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07005428 button.setTextViewText(R.id.action0, processTextSpans(
5429 processLegacyText(action.title)));
Adrian Roos72171622017-01-27 10:32:06 -08005430 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005431 setTextViewColorPrimary(button, R.id.action0);
Anthony Chenad4d1582017-04-10 16:07:58 -07005432 } else if (mN.color != COLOR_DEFAULT && mTintActionButtons) {
Adrian Roos487374f2017-01-11 15:48:14 -08005433 button.setTextColor(R.id.action0,
5434 ambient ? resolveAmbientColor() : resolveContrastColor());
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005435 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005436 }
Tony Mak7d4b3a52018-11-27 17:29:36 +00005437 button.setIntTag(R.id.action0, R.id.notification_action_index_tag,
5438 mActions.indexOf(action));
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005439 return button;
5440 }
5441
Joe Onoratocb109a02011-01-18 17:57:41 -08005442 /**
Selim Cinek981962e2016-07-20 20:41:58 -07005443 * Ensures contrast on color spans against a background color. also returns the color of the
5444 * text if a span was found that spans over the whole text.
5445 *
5446 * @param charSequence the charSequence on which the spans are
5447 * @param background the background color to ensure the contrast against
5448 * @param outResultColor an array in which a color will be returned as the first element if
5449 * there exists a full length color span.
5450 * @return the contrasted charSequence
5451 */
5452 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
5453 ColorStateList[] outResultColor) {
5454 if (charSequence instanceof Spanned) {
5455 Spanned ss = (Spanned) charSequence;
5456 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
5457 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
5458 for (Object span : spans) {
5459 Object resultSpan = span;
5460 int spanStart = ss.getSpanStart(span);
5461 int spanEnd = ss.getSpanEnd(span);
5462 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
5463 if (resultSpan instanceof CharacterStyle) {
5464 resultSpan = ((CharacterStyle) span).getUnderlying();
5465 }
5466 if (resultSpan instanceof TextAppearanceSpan) {
5467 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
5468 ColorStateList textColor = originalSpan.getTextColor();
5469 if (textColor != null) {
5470 int[] colors = textColor.getColors();
5471 int[] newColors = new int[colors.length];
5472 for (int i = 0; i < newColors.length; i++) {
Lucas Dupina291d192018-06-07 13:59:42 -07005473 newColors[i] = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005474 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005475 }
5476 textColor = new ColorStateList(textColor.getStates().clone(),
5477 newColors);
Selim Cinek396caca2018-04-10 17:46:46 -07005478 if (fullLength) {
5479 outResultColor[0] = textColor;
5480 // Let's drop the color from the span
5481 textColor = null;
5482 }
Selim Cinek981962e2016-07-20 20:41:58 -07005483 resultSpan = new TextAppearanceSpan(
5484 originalSpan.getFamily(),
5485 originalSpan.getTextStyle(),
5486 originalSpan.getTextSize(),
5487 textColor,
5488 originalSpan.getLinkTextColor());
Selim Cinek981962e2016-07-20 20:41:58 -07005489 }
5490 } else if (resultSpan instanceof ForegroundColorSpan) {
5491 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
5492 int foregroundColor = originalSpan.getForegroundColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005493 foregroundColor = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005494 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005495 if (fullLength) {
5496 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
Selim Cinek396caca2018-04-10 17:46:46 -07005497 resultSpan = null;
5498 } else {
5499 resultSpan = new ForegroundColorSpan(foregroundColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005500 }
5501 } else {
5502 resultSpan = span;
5503 }
Selim Cinek396caca2018-04-10 17:46:46 -07005504 if (resultSpan != null) {
5505 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
5506 }
Selim Cinek981962e2016-07-20 20:41:58 -07005507 }
5508 return builder;
5509 }
5510 return charSequence;
5511 }
5512
5513 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005514 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07005515 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005516 */
5517 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005518 if (!mIsLegacyInitialized) {
5519 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
5520 < Build.VERSION_CODES.LOLLIPOP;
5521 mIsLegacyInitialized = true;
5522 }
5523 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005524 }
5525
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005526 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08005527 return processLegacyText(charSequence, false /* ambient */);
5528 }
5529
5530 private CharSequence processLegacyText(CharSequence charSequence, boolean ambient) {
5531 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
5532 boolean wantLightText = ambient;
5533 if (isAlreadyLightText != wantLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005534 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005535 } else {
5536 return charSequence;
5537 }
5538 }
5539
Dan Sandler26e81cf2014-05-06 10:01:27 -04005540 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005541 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04005542 */
Adrian Roos487374f2017-01-11 15:48:14 -08005543 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
5544 boolean ambient) {
Selim Cinekea4bef72015-12-02 15:51:10 -08005545 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005546 int color;
5547 if (ambient) {
5548 color = resolveAmbientColor();
5549 } else if (isColorized()) {
5550 color = getPrimaryTextColor();
5551 } else {
5552 color = resolveContrastColor();
5553 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005554 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07005555 contentView.setDrawableTint(R.id.icon, false, color,
5556 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005557
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005558 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005559 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08005560 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005561 }
5562
Dan Sandler26e81cf2014-05-06 10:01:27 -04005563 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005564 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04005565 * if it's grayscale).
5566 */
5567 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04005568 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
5569 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005570 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005571 // resolve color will fall back to the default when legacy
Sunny Goyal5b153922017-09-21 21:00:36 -07005572 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(),
5573 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01005574 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005575 }
5576
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05005577 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005578 if (mN.color != COLOR_DEFAULT) {
5579 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02005580 }
Jorim Jaggi74419312014-06-10 20:57:21 +02005581 }
5582
Adrian Roos4ff3b122016-02-01 12:26:13 -08005583 int resolveContrastColor() {
5584 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
5585 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005586 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005587
Selim Cinekac5f0272017-05-02 16:05:41 -07005588 int color;
Selim Cinekc7f5a822018-03-20 19:32:06 -07005589 int background = mContext.getColor(
5590 com.android.internal.R.color.notification_material_background_color);
Selim Cinekac5f0272017-05-02 16:05:41 -07005591 if (mN.color == COLOR_DEFAULT) {
5592 ensureColors();
Lucas Dupinf03e7522018-06-25 16:21:13 -07005593 color = ContrastColorUtil.resolveDefaultColor(mContext, background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005594 } else {
Lucas Dupina291d192018-06-07 13:59:42 -07005595 color = ContrastColorUtil.resolveContrastColor(mContext, mN.color,
Anthony Chenad4d1582017-04-10 16:07:58 -07005596 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005597 }
5598 if (Color.alpha(color) < 255) {
5599 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005600 color = ContrastColorUtil.compositeColors(color, background);
Selim Cinekac5f0272017-05-02 16:05:41 -07005601 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005602 mCachedContrastColorIsFor = mN.color;
Selim Cinekac5f0272017-05-02 16:05:41 -07005603 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005604 }
5605
Selim Cinek4717d862018-04-19 09:19:15 +08005606 int resolveNeutralColor() {
5607 if (mNeutralColor != COLOR_INVALID) {
5608 return mNeutralColor;
5609 }
5610 int background = mContext.getColor(
5611 com.android.internal.R.color.notification_material_background_color);
Lucas Dupinf03e7522018-06-25 16:21:13 -07005612 mNeutralColor = ContrastColorUtil.resolveDefaultColor(mContext, background,
5613 mInNightMode);
Selim Cinek4717d862018-04-19 09:19:15 +08005614 if (Color.alpha(mNeutralColor) < 255) {
5615 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005616 mNeutralColor = ContrastColorUtil.compositeColors(mNeutralColor, background);
Selim Cinek4717d862018-04-19 09:19:15 +08005617 }
5618 return mNeutralColor;
5619 }
5620
Adrian Roos487374f2017-01-11 15:48:14 -08005621 int resolveAmbientColor() {
5622 if (mCachedAmbientColorIsFor == mN.color && mCachedAmbientColorIsFor != COLOR_INVALID) {
5623 return mCachedAmbientColor;
5624 }
Lucas Dupina291d192018-06-07 13:59:42 -07005625 final int contrasted = ContrastColorUtil.resolveAmbientColor(mContext, mN.color);
Adrian Roos487374f2017-01-11 15:48:14 -08005626
5627 mCachedAmbientColorIsFor = mN.color;
5628 return mCachedAmbientColor = contrasted;
5629 }
5630
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005631 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005632 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005633 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08005634 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005635 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005636 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005637 mN.actions = new Action[mActions.size()];
5638 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005639 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005640 if (!mPersonList.isEmpty()) {
Selim Cineke7238dd2017-12-14 17:48:32 -08005641 mN.extras.putParcelableArrayList(EXTRA_PEOPLE_LIST, mPersonList);
Dan Sandler0bf2ed82013-12-21 23:33:41 -06005642 }
Selim Cinek247fa012016-02-18 09:50:48 -08005643 if (mN.bigContentView != null || mN.contentView != null
5644 || mN.headsUpContentView != null) {
5645 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
5646 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005647 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08005648 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005649
Julia Reynolds3b848122016-02-26 10:45:32 -05005650 /**
5651 * Creates a Builder from an existing notification so further changes can be made.
5652 * @param context The context for your application / activity.
5653 * @param n The notification to create a Builder from.
5654 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005655 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005656 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005657 ApplicationInfo applicationInfo = n.extras.getParcelable(
5658 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005659 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05005660 if (applicationInfo != null) {
5661 try {
5662 builderContext = context.createApplicationContext(applicationInfo,
5663 Context.CONTEXT_RESTRICTED);
5664 } catch (NameNotFoundException e) {
5665 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
5666 builderContext = context; // try with our context
5667 }
5668 } else {
5669 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02005670 }
5671
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005672 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005673 }
5674
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005675 /**
Gustav Sennton761884c2018-11-19 17:40:19 +00005676 * Determines whether the platform can generate contextual actions for a notification.
5677 * By default this is true.
5678 */
5679 public Builder setAllowSystemGeneratedContextualActions(boolean allowed) {
5680 mN.mAllowSystemGeneratedContextualActions = allowed;
5681 return this;
5682 }
5683
5684 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005685 * @deprecated Use {@link #build()} instead.
5686 */
5687 @Deprecated
5688 public Notification getNotification() {
5689 return build();
5690 }
5691
5692 /**
5693 * Combine all of the options that have been set and return a new {@link Notification}
5694 * object.
5695 */
5696 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005697 // first, add any extras from the calling code
5698 if (mUserExtras != null) {
5699 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005700 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005701
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005702 mN.creationTime = System.currentTimeMillis();
5703
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005704 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05005705 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02005706
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005707 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005708
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005709 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02005710 mStyle.reduceImageSizes(mContext);
5711 mStyle.purgeResources();
Selim Cinek90343862018-02-01 11:07:11 -08005712 mStyle.validate(mContext);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005713 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005714 }
Selim Cinekd0426622017-07-11 13:19:59 +02005715 mN.reduceImageSizes(mContext);
5716
Adrian Roos5081c0d2016-02-26 16:04:19 -08005717 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07005718 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005719 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005720 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005721 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
5722 mN.contentView.getSequenceNumber());
5723 }
5724 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005725 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005726 if (mN.bigContentView != null) {
5727 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
5728 mN.bigContentView.getSequenceNumber());
5729 }
5730 }
5731 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005732 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005733 if (mN.headsUpContentView != null) {
5734 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
5735 mN.headsUpContentView.getSequenceNumber());
5736 }
5737 }
5738 }
5739
Julia Reynolds4c0c2022016-02-02 15:11:59 -05005740 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
5741 mN.flags |= FLAG_SHOW_LIGHTS;
5742 }
5743
Adrian Roosfb921842017-10-26 14:49:56 +02005744 mN.allPendingIntents = null;
5745
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005746 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005747 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005748
5749 /**
5750 * Apply this Builder to an existing {@link Notification} object.
5751 *
5752 * @hide
5753 */
5754 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005755 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005756 return n;
5757 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005758
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005759 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005760 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005761 * change. Also removes extenders on low ram devices, as
5762 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005763 *
5764 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5765 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005766 * @hide
5767 */
Kristian Monsen05f34792018-04-09 10:27:16 +02005768 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam,
5769 Context context) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005770 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005771
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005772 // Only strip views for known Styles because we won't know how to
5773 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005774 if (!isLowRam
5775 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005776 && getNotificationStyleClass(templateClass) == null) {
5777 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005778 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005779
5780 // Only strip unmodified BuilderRemoteViews.
5781 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005782 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005783 n.contentView.getSequenceNumber();
5784 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005785 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005786 n.bigContentView.getSequenceNumber();
5787 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005788 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005789 n.headsUpContentView.getSequenceNumber();
5790
5791 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005792 if (!isLowRam
5793 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005794 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005795 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005796
5797 Notification clone = n.clone();
5798 if (stripContentView) {
5799 clone.contentView = null;
5800 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5801 }
5802 if (stripBigContentView) {
5803 clone.bigContentView = null;
5804 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5805 }
5806 if (stripHeadsUpContentView) {
5807 clone.headsUpContentView = null;
5808 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5809 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005810 if (isLowRam) {
Kristian Monsen05f34792018-04-09 10:27:16 +02005811 String[] allowedServices = context.getResources().getStringArray(
5812 R.array.config_allowedManagedServicesOnLowRamDevices);
5813 if (allowedServices.length == 0) {
5814 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5815 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5816 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5817 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005818 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005819 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005820 }
5821
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005822 @UnsupportedAppUsage
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005823 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005824 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005825 }
5826
5827 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005828 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005829 }
5830
5831 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005832 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005833 }
5834
5835 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005836 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005837 }
5838
5839 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005840 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005841 }
5842
Adrian Roosc1a80b02016-04-05 14:54:55 -07005843 private int getMessagingLayoutResource() {
5844 return R.layout.notification_template_material_messaging;
5845 }
5846
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005847 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005848 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005849 }
5850
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005851 private int getEmphasizedActionLayoutResource() {
5852 return R.layout.notification_material_action_emphasized;
5853 }
5854
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005855 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005856 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005857 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005858
5859 private int getBackgroundColor() {
5860 if (isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005861 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : mN.color;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005862 } else {
Selim Cinekc7f5a822018-03-20 19:32:06 -07005863 return COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005864 }
5865 }
5866
Selim Cinek396caca2018-04-10 17:46:46 -07005867 /**
Selim Cinek4717d862018-04-19 09:19:15 +08005868 * Gets a neutral color that can be used for icons or similar that should not stand out.
5869 */
5870 private int getNeutralColor() {
5871 if (isColorized()) {
5872 return getSecondaryTextColor();
5873 } else {
5874 return resolveNeutralColor();
5875 }
5876 }
5877
5878 /**
Selim Cinek396caca2018-04-10 17:46:46 -07005879 * Same as getBackgroundColor but also resolved the default color to the background.
5880 */
5881 private int resolveBackgroundColor() {
5882 int backgroundColor = getBackgroundColor();
5883 if (backgroundColor == COLOR_DEFAULT) {
5884 backgroundColor = mContext.getColor(
5885 com.android.internal.R.color.notification_material_background_color);
5886 }
5887 return backgroundColor;
5888 }
5889
Selim Cinek7b9605b2017-01-19 17:36:00 -08005890 private boolean isColorized() {
5891 return mN.isColorized();
5892 }
Selim Cinek99104832017-01-25 14:47:33 -08005893
Anthony Chenad4d1582017-04-10 16:07:58 -07005894 private boolean shouldTintActionButtons() {
5895 return mTintActionButtons;
5896 }
5897
Selim Cinek99104832017-01-25 14:47:33 -08005898 private boolean textColorsNeedInversion() {
5899 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
5900 return false;
5901 }
5902 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
5903 return targetSdkVersion > Build.VERSION_CODES.M
5904 && targetSdkVersion < Build.VERSION_CODES.O;
5905 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005906
5907 /**
5908 * Set a color palette to be used as the background and textColors
5909 *
5910 * @param backgroundColor the color to be used as the background
5911 * @param foregroundColor the color to be used as the foreground
5912 *
5913 * @hide
5914 */
5915 public void setColorPalette(int backgroundColor, int foregroundColor) {
5916 mBackgroundColor = backgroundColor;
5917 mForegroundColor = foregroundColor;
5918 mTextColorsAreForBackground = COLOR_INVALID;
5919 ensureColors();
5920 }
Selim Cinekac5f0272017-05-02 16:05:41 -07005921
5922 /**
Selim Cineka7679b62017-05-10 16:33:25 -07005923 * Forces all styled remoteViews to be built from scratch and not use any cached
5924 * RemoteViews.
5925 * This is needed for legacy apps that are baking in their remoteviews into the
5926 * notification.
5927 *
5928 * @hide
5929 */
5930 public void setRebuildStyledRemoteViews(boolean rebuild) {
5931 mRebuildStyledRemoteViews = rebuild;
5932 }
Selim Cinekaa9db1f2018-02-27 17:35:47 -08005933
5934 /**
5935 * Get the text that should be displayed in the statusBar when heads upped. This is
5936 * usually just the app name, but may be different depending on the style.
5937 *
5938 * @param publicMode If true, return a text that is safe to display in public.
5939 *
5940 * @hide
5941 */
5942 public CharSequence getHeadsUpStatusBarText(boolean publicMode) {
5943 if (mStyle != null && !publicMode) {
5944 CharSequence text = mStyle.getHeadsUpStatusBarText();
5945 if (!TextUtils.isEmpty(text)) {
5946 return text;
5947 }
5948 }
5949 return loadHeaderAppName();
5950 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005951 }
5952
5953 /**
Selim Cinekd0426622017-07-11 13:19:59 +02005954 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
5955 * remote views.
5956 *
5957 * @hide
5958 */
5959 void reduceImageSizes(Context context) {
5960 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
5961 return;
5962 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005963 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02005964 if (mLargeIcon != null || largeIcon != null) {
5965 Resources resources = context.getResources();
5966 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07005967 int maxWidth = resources.getDimensionPixelSize(isLowRam
5968 ? R.dimen.notification_right_icon_size_low_ram
5969 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02005970 int maxHeight = maxWidth;
5971 if (MediaStyle.class.equals(style)
5972 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005973 maxHeight = resources.getDimensionPixelSize(isLowRam
5974 ? R.dimen.notification_media_image_max_height_low_ram
5975 : R.dimen.notification_media_image_max_height);
5976 maxWidth = resources.getDimensionPixelSize(isLowRam
5977 ? R.dimen.notification_media_image_max_width_low_ram
5978 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02005979 }
5980 if (mLargeIcon != null) {
5981 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
5982 }
5983 if (largeIcon != null) {
5984 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
5985 }
5986 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005987 reduceImageSizesForRemoteView(contentView, context, isLowRam);
5988 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
5989 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02005990 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
5991 }
5992
Selim Cineka8cb1262017-08-15 16:53:44 -07005993 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
5994 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02005995 if (remoteView != null) {
5996 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07005997 int maxWidth = resources.getDimensionPixelSize(isLowRam
5998 ? R.dimen.notification_custom_view_max_image_width_low_ram
5999 : R.dimen.notification_custom_view_max_image_width);
6000 int maxHeight = resources.getDimensionPixelSize(isLowRam
6001 ? R.dimen.notification_custom_view_max_image_height_low_ram
6002 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02006003 remoteView.reduceImageSizes(maxWidth, maxHeight);
6004 }
6005 }
6006
6007 /**
Selim Cinek22714f12017-04-13 16:23:53 -07006008 * @return whether this notification is a foreground service notification
Gus Prevasb0c1a462018-11-05 11:06:15 -05006009 * @hide
Selim Cinek7b9605b2017-01-19 17:36:00 -08006010 */
Gus Prevasb0c1a462018-11-05 11:06:15 -05006011 public boolean isForegroundService() {
Selim Cinek22714f12017-04-13 16:23:53 -07006012 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08006013 }
6014
6015 /**
Selim Cinek99104832017-01-25 14:47:33 -08006016 * @return whether this notification has a media session attached
6017 * @hide
6018 */
6019 public boolean hasMediaSession() {
6020 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
6021 }
6022
6023 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006024 * @return the style class of this notification
6025 * @hide
6026 */
6027 public Class<? extends Notification.Style> getNotificationStyle() {
6028 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
6029
6030 if (!TextUtils.isEmpty(templateClass)) {
6031 return Notification.getNotificationStyleClass(templateClass);
6032 }
6033 return null;
6034 }
6035
6036 /**
Selim Cinek22714f12017-04-13 16:23:53 -07006037 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08006038 *
6039 * @hide
6040 */
6041 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07006042 if (isColorizedMedia()) {
6043 return true;
6044 }
Julia Reynolds4db59552017-06-30 13:34:01 -04006045 return extras.getBoolean(EXTRA_COLORIZED)
6046 && (hasColorizedPermission() || isForegroundService());
6047 }
6048
6049 /**
6050 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
6051 * permission. The permission is checked when a notification is enqueued.
6052 */
6053 private boolean hasColorizedPermission() {
6054 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07006055 }
6056
6057 /**
6058 * @return true if this notification is colorized and it is a media notification
6059 *
6060 * @hide
6061 */
6062 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08006063 Class<? extends Style> style = getNotificationStyle();
6064 if (MediaStyle.class.equals(style)) {
6065 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
6066 if ((colorized == null || colorized) && hasMediaSession()) {
6067 return true;
6068 }
6069 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6070 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
6071 return true;
6072 }
6073 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07006074 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006075 }
6076
Selim Cinek0847acd2017-04-24 19:48:29 -07006077
6078 /**
6079 * @return true if this is a media notification
6080 *
6081 * @hide
6082 */
6083 public boolean isMediaNotification() {
6084 Class<? extends Style> style = getNotificationStyle();
6085 if (MediaStyle.class.equals(style)) {
6086 return true;
6087 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6088 return true;
6089 }
6090 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006091 }
6092
Selim Cinek279fa862016-06-14 10:57:25 -07006093 private boolean hasLargeIcon() {
6094 return mLargeIcon != null || largeIcon != null;
6095 }
6096
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006097 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006098 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006099 * @hide
6100 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07006101 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006102 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
6103 }
6104
6105 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006106 * @return true if the notification will show a chronometer; false otherwise
6107 * @hide
6108 */
6109 public boolean showsChronometer() {
6110 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
6111 }
6112
6113 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04006114 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05006115 */
6116 @SystemApi
6117 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
6118 Class<? extends Style>[] classes = new Class[] {
6119 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
6120 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
6121 MessagingStyle.class };
6122 for (Class<? extends Style> innerClass : classes) {
6123 if (templateClass.equals(innerClass.getName())) {
6124 return innerClass;
6125 }
6126 }
6127 return null;
6128 }
6129
6130 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006131 * An object that can apply a rich notification style to a {@link Notification.Builder}
6132 * object.
6133 */
Griff Hazendfcb0802014-02-11 12:00:00 -08006134 public static abstract class Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07006135
6136 /**
6137 * The number of items allowed simulatanously in the remote input history.
6138 * @hide
6139 */
6140 static final int MAX_REMOTE_INPUT_HISTORY_LINES = 3;
Chris Wrend6297db2012-05-03 16:20:13 -04006141 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006142
6143 /**
6144 * @hide
6145 */
6146 protected CharSequence mSummaryText = null;
6147
6148 /**
6149 * @hide
6150 */
6151 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04006152
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006153 protected Builder mBuilder;
6154
Chris Wrend6297db2012-05-03 16:20:13 -04006155 /**
6156 * Overrides ContentTitle in the big form of the template.
6157 * This defaults to the value passed to setContentTitle().
6158 */
6159 protected void internalSetBigContentTitle(CharSequence title) {
6160 mBigContentTitle = title;
6161 }
6162
6163 /**
6164 * Set the first line of text after the detail section in the big form of the template.
6165 */
6166 protected void internalSetSummaryText(CharSequence cs) {
6167 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04006168 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04006169 }
6170
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006171 public void setBuilder(Builder builder) {
6172 if (mBuilder != builder) {
6173 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07006174 if (mBuilder != null) {
6175 mBuilder.setStyle(this);
6176 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006177 }
6178 }
6179
Chris Wrend6297db2012-05-03 16:20:13 -04006180 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006181 if (mBuilder == null) {
6182 throw new IllegalArgumentException("Style requires a valid Builder object");
6183 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006184 }
Chris Wrend6297db2012-05-03 16:20:13 -04006185
6186 protected RemoteViews getStandardView(int layoutId) {
Selim Cinek384804b2018-04-18 14:31:07 +08006187 return getStandardView(layoutId, null);
6188 }
6189
6190 /**
6191 * Get the standard view for this style.
6192 *
6193 * @param layoutId The layout id to use
6194 * @param result The result where template bind information is saved.
6195 * @return A remoteView for this style.
6196 * @hide
6197 */
6198 protected RemoteViews getStandardView(int layoutId, TemplateBindResult result) {
Chris Wrend6297db2012-05-03 16:20:13 -04006199 checkBuilder();
6200
Christoph Studer4600f9b2014-07-22 22:44:43 +02006201 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006202 CharSequence oldBuilderContentTitle =
6203 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04006204 if (mBigContentTitle != null) {
6205 mBuilder.setContentTitle(mBigContentTitle);
6206 }
6207
Selim Cinek384804b2018-04-18 14:31:07 +08006208 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId, result);
Chris Wrend6297db2012-05-03 16:20:13 -04006209
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006210 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006211
Chris Wrend6297db2012-05-03 16:20:13 -04006212 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
6213 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04006214 } else {
6215 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04006216 }
6217
Chris Wrend6297db2012-05-03 16:20:13 -04006218 return contentView;
6219 }
6220
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006221 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006222 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006223 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08006224 *
6225 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006226 * @hide
6227 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08006228 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006229 return null;
6230 }
6231
6232 /**
6233 * Construct a Style-specific RemoteViews for the final big notification layout.
6234 * @hide
6235 */
6236 public RemoteViews makeBigContentView() {
6237 return null;
6238 }
6239
6240 /**
6241 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08006242 *
6243 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006244 * @hide
6245 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006246 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006247 return null;
6248 }
6249
6250 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006251 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006252 * @hide
6253 */
6254 public void addExtras(Bundle extras) {
6255 if (mSummaryTextSet) {
6256 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
6257 }
6258 if (mBigContentTitle != null) {
6259 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
6260 }
Chris Wren91ad5632013-06-05 15:05:57 -04006261 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006262 }
6263
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006264 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006265 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006266 * @hide
6267 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02006268 protected void restoreFromExtras(Bundle extras) {
6269 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
6270 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
6271 mSummaryTextSet = true;
6272 }
6273 if (extras.containsKey(EXTRA_TITLE_BIG)) {
6274 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
6275 }
6276 }
6277
6278
6279 /**
6280 * @hide
6281 */
6282 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006283 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006284 return wip;
6285 }
6286
Daniel Sandler0ec46202015-06-24 01:27:05 -04006287 /**
6288 * @hide
6289 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006290 public void purgeResources() {}
6291
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006292 /**
6293 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
6294 * attached to.
6295 *
6296 * @return the fully constructed Notification.
6297 */
6298 public Notification build() {
6299 checkBuilder();
6300 return mBuilder.build();
6301 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006302
6303 /**
6304 * @hide
6305 * @return true if the style positions the progress bar on the second line; false if the
6306 * style hides the progress bar
6307 */
6308 protected boolean hasProgress() {
6309 return true;
6310 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006311
6312 /**
6313 * @hide
6314 * @return Whether we should put the summary be put into the notification header
6315 */
6316 public boolean hasSummaryInHeader() {
6317 return true;
6318 }
Selim Cinek593610c2016-02-16 18:42:57 -08006319
6320 /**
6321 * @hide
6322 * @return Whether custom content views are displayed inline in the style
6323 */
6324 public boolean displayCustomViewInline() {
6325 return false;
6326 }
Selim Cinekd0426622017-07-11 13:19:59 +02006327
6328 /**
6329 * Reduces the image sizes contained in this style.
6330 *
6331 * @hide
6332 */
6333 public void reduceImageSizes(Context context) {
6334 }
Selim Cinek90343862018-02-01 11:07:11 -08006335
6336 /**
6337 * Validate that this style was properly composed. This is called at build time.
6338 * @hide
6339 */
6340 public void validate(Context context) {
6341 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006342
6343 /**
6344 * @hide
6345 */
6346 public abstract boolean areNotificationsVisiblyDifferent(Style other);
Selim Cinekc7f5a822018-03-20 19:32:06 -07006347
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006348 /**
koprivaa1a78482018-10-09 10:09:23 -07006349 * @return the text that should be displayed in the statusBar when heads-upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006350 * If {@code null} is returned, the default implementation will be used.
6351 *
6352 * @hide
6353 */
6354 public CharSequence getHeadsUpStatusBarText() {
6355 return null;
6356 }
Joe Onorato46439ce2010-11-19 13:56:21 -08006357 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006358
6359 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006360 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08006361 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006362 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006363 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006364 * Notification notif = new Notification.Builder(mContext)
6365 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
6366 * .setContentText(subject)
6367 * .setSmallIcon(R.drawable.new_post)
6368 * .setLargeIcon(aBitmap)
6369 * .setStyle(new Notification.BigPictureStyle()
6370 * .bigPicture(aBigBitmap))
6371 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006372 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006373 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006374 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006375 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006376 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006377 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006378 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006379 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006380
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006381 public BigPictureStyle() {
6382 }
6383
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006384 /**
6385 * @deprecated use {@code BigPictureStyle()}.
6386 */
6387 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006388 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006389 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006390 }
6391
Chris Wrend6297db2012-05-03 16:20:13 -04006392 /**
6393 * Overrides ContentTitle in the big form of the template.
6394 * This defaults to the value passed to setContentTitle().
6395 */
6396 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006397 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006398 return this;
6399 }
6400
6401 /**
6402 * Set the first line of text after the detail section in the big form of the template.
6403 */
6404 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006405 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006406 return this;
6407 }
6408
Chris Wren0bd664d2012-08-01 13:56:56 -04006409 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05006410 * @hide
6411 */
6412 public Bitmap getBigPicture() {
6413 return mPicture;
6414 }
6415
6416 /**
Chris Wren0bd664d2012-08-01 13:56:56 -04006417 * Provide the bitmap to be used as the payload for the BigPicture notification.
6418 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006419 public BigPictureStyle bigPicture(Bitmap b) {
6420 mPicture = b;
6421 return this;
6422 }
6423
Chris Wren3745a3d2012-05-22 15:11:52 -04006424 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04006425 * Override the large icon when the big notification is shown.
6426 */
6427 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04006428 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
6429 }
6430
6431 /**
6432 * Override the large icon when the big notification is shown.
6433 */
6434 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04006435 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006436 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006437 return this;
6438 }
6439
Riley Andrews0394a0c2015-11-03 23:36:52 -08006440 /** @hide */
6441 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
6442
Daniel Sandler0ec46202015-06-24 01:27:05 -04006443 /**
6444 * @hide
6445 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006446 @Override
6447 public void purgeResources() {
6448 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08006449 if (mPicture != null &&
6450 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08006451 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006452 mPicture = mPicture.createAshmemBitmap();
6453 }
6454 if (mBigLargeIcon != null) {
6455 mBigLargeIcon.convertToAshmem();
6456 }
6457 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01006458
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006459 /**
6460 * @hide
6461 */
Selim Cinekd0426622017-07-11 13:19:59 +02006462 @Override
6463 public void reduceImageSizes(Context context) {
6464 super.reduceImageSizes(context);
6465 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006466 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006467 if (mPicture != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006468 int maxPictureWidth = resources.getDimensionPixelSize(isLowRam
6469 ? R.dimen.notification_big_picture_max_height_low_ram
6470 : R.dimen.notification_big_picture_max_height);
6471 int maxPictureHeight = resources.getDimensionPixelSize(isLowRam
6472 ? R.dimen.notification_big_picture_max_width_low_ram
6473 : R.dimen.notification_big_picture_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006474 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
6475 }
6476 if (mBigLargeIcon != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006477 int rightIconSize = resources.getDimensionPixelSize(isLowRam
6478 ? R.dimen.notification_right_icon_size_low_ram
6479 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006480 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
6481 }
6482 }
6483
6484 /**
6485 * @hide
6486 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006487 public RemoteViews makeBigContentView() {
6488 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01006489 // This covers the following cases:
6490 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006491 // mN.mLargeIcon
6492 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04006493 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07006494 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006495 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006496 oldLargeIcon = mBuilder.mN.mLargeIcon;
6497 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006498 // The legacy largeIcon might not allow us to clear the image, as it's taken in
6499 // replacement if the other one is null. Because we're restoring these legacy icons
6500 // for old listeners, this is in general non-null.
6501 largeIconLegacy = mBuilder.mN.largeIcon;
6502 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006503 }
6504
Selim Cinek384804b2018-04-18 14:31:07 +08006505 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource(),
6506 null /* result */);
Selim Cinek03d0d652015-11-13 13:18:09 -05006507 if (mSummaryTextSet) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006508 contentView.setTextViewText(R.id.text, mBuilder.processTextSpans(
6509 mBuilder.processLegacyText(mSummaryText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006510 mBuilder.setTextViewColorSecondary(contentView, R.id.text);
Selim Cinekc848c3a2016-01-13 15:27:30 -08006511 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05006512 }
Selim Cinek279fa862016-06-14 10:57:25 -07006513 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08006514
Christoph Studer5c510ee2014-12-15 16:32:27 +01006515 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006516 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006517 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006518 }
6519
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006520 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006521 return contentView;
6522 }
6523
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006524 /**
6525 * @hide
6526 */
6527 public void addExtras(Bundle extras) {
6528 super.addExtras(extras);
6529
6530 if (mBigLargeIconSet) {
6531 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
6532 }
6533 extras.putParcelable(EXTRA_PICTURE, mPicture);
6534 }
6535
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006536 /**
6537 * @hide
6538 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006539 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02006540 protected void restoreFromExtras(Bundle extras) {
6541 super.restoreFromExtras(extras);
6542
6543 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01006544 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02006545 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04006546 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006547 mPicture = extras.getParcelable(EXTRA_PICTURE);
6548 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006549
6550 /**
6551 * @hide
6552 */
6553 @Override
6554 public boolean hasSummaryInHeader() {
6555 return false;
6556 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006557
6558 /**
6559 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006560 * Note that we aren't actually comparing the contents of the bitmaps here, so this
6561 * is only doing a cursory inspection. Bitmaps of equal size will appear the same.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006562 */
6563 @Override
6564 public boolean areNotificationsVisiblyDifferent(Style other) {
6565 if (other == null || getClass() != other.getClass()) {
6566 return true;
6567 }
6568 BigPictureStyle otherS = (BigPictureStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006569 return areBitmapsObviouslyDifferent(getBigPicture(), otherS.getBigPicture());
6570 }
6571
6572 private static boolean areBitmapsObviouslyDifferent(Bitmap a, Bitmap b) {
6573 if (a == b) {
6574 return false;
6575 }
6576 if (a == null || b == null) {
6577 return true;
6578 }
6579 return a.getWidth() != b.getWidth()
6580 || a.getHeight() != b.getHeight()
6581 || a.getConfig() != b.getConfig()
6582 || a.getGenerationId() != b.getGenerationId();
Julia Reynolds7217dc92018-03-07 12:12:09 -05006583 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006584 }
6585
6586 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006587 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08006588 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006589 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006590 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006591 * Notification notif = new Notification.Builder(mContext)
6592 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
6593 * .setContentText(subject)
6594 * .setSmallIcon(R.drawable.new_mail)
6595 * .setLargeIcon(aBitmap)
6596 * .setStyle(new Notification.BigTextStyle()
6597 * .bigText(aVeryLongString))
6598 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006599 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006600 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006601 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006602 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006603 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006604
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006605 private CharSequence mBigText;
6606
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006607 public BigTextStyle() {
6608 }
6609
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006610 /**
6611 * @deprecated use {@code BigTextStyle()}.
6612 */
6613 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006614 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006615 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006616 }
6617
Chris Wrend6297db2012-05-03 16:20:13 -04006618 /**
6619 * Overrides ContentTitle in the big form of the template.
6620 * This defaults to the value passed to setContentTitle().
6621 */
6622 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006623 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006624 return this;
6625 }
6626
6627 /**
6628 * Set the first line of text after the detail section in the big form of the template.
6629 */
6630 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006631 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006632 return this;
6633 }
6634
Chris Wren0bd664d2012-08-01 13:56:56 -04006635 /**
6636 * Provide the longer text to be displayed in the big form of the
6637 * template in place of the content text.
6638 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006639 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006640 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006641 return this;
6642 }
6643
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006644 /**
6645 * @hide
6646 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05006647 public CharSequence getBigText() {
6648 return mBigText;
6649 }
6650
6651 /**
6652 * @hide
6653 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006654 public void addExtras(Bundle extras) {
6655 super.addExtras(extras);
6656
Christoph Studer4600f9b2014-07-22 22:44:43 +02006657 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
6658 }
6659
6660 /**
6661 * @hide
6662 */
6663 @Override
6664 protected void restoreFromExtras(Bundle extras) {
6665 super.restoreFromExtras(extras);
6666
6667 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006668 }
6669
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006670 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006671 * @param increasedHeight true if this layout be created with an increased height.
6672 *
6673 * @hide
6674 */
6675 @Override
6676 public RemoteViews makeContentView(boolean increasedHeight) {
6677 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006678 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006679 mBuilder.mActions = new ArrayList<>();
6680 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006681 mBuilder.mActions = mBuilder.mOriginalActions;
6682 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006683 return remoteViews;
6684 }
6685 return super.makeContentView(increasedHeight);
6686 }
6687
6688 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006689 * @hide
6690 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006691 @Override
6692 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
6693 if (increasedHeight && mBuilder.mActions.size() > 0) {
6694 return makeBigContentView();
6695 }
6696 return super.makeHeadsUpContentView(increasedHeight);
6697 }
6698
6699 /**
6700 * @hide
6701 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006702 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02006703
6704 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07006705 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006706 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04006707
Selim Cinek384804b2018-04-18 14:31:07 +08006708 TemplateBindResult result = new TemplateBindResult();
6709 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource(), result);
Selim Cinek1c72fa02018-04-23 18:00:54 +08006710 contentView.setInt(R.id.big_text, "setImageEndMargin", result.getIconMarginEnd());
Joe Malin8d40d042012-11-05 11:36:40 -08006711
Selim Cinek75998782016-04-26 10:39:17 -07006712 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006713
Selim Cinek3a2c4b92015-12-17 17:01:17 -08006714 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07006715 if (TextUtils.isEmpty(bigTextText)) {
6716 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
6717 // experience
6718 bigTextText = mBuilder.processLegacyText(text);
6719 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07006720 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08006721
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006722 return contentView;
6723 }
6724
Julia Reynolds7217dc92018-03-07 12:12:09 -05006725 /**
6726 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006727 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006728 */
6729 @Override
6730 public boolean areNotificationsVisiblyDifferent(Style other) {
6731 if (other == null || getClass() != other.getClass()) {
6732 return true;
6733 }
6734 BigTextStyle newS = (BigTextStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006735 return !Objects.equals(String.valueOf(getBigText()), String.valueOf(newS.getBigText()));
Julia Reynolds7217dc92018-03-07 12:12:09 -05006736 }
6737
Adrian Roosb1f427c2016-05-26 12:27:15 -07006738 static void applyBigTextContentView(Builder builder,
6739 RemoteViews contentView, CharSequence bigTextText) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006740 contentView.setTextViewText(R.id.big_text, builder.processTextSpans(bigTextText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006741 builder.setTextViewColorSecondary(contentView, R.id.big_text);
Adrian Roosb1f427c2016-05-26 12:27:15 -07006742 contentView.setViewVisibility(R.id.big_text,
6743 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek279fa862016-06-14 10:57:25 -07006744 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07006745 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006746 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006747
6748 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006749 * Helper class for generating large-format notifications that include multiple back-and-forth
6750 * messages of varying types between any number of people.
6751 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006752 * <p>
Alex Hillsfc737de2016-03-23 17:33:02 -04006753 * If the platform does not provide large-format notifications, this method has no effect. The
6754 * user will always see the normal notification view.
Selim Cinekce8794f2018-05-23 16:46:05 -07006755 *
6756 * <p>
6757 * If the app is targeting Android P and above, it is required to use the {@link Person}
6758 * class in order to get an optimal rendering of the notification and its avatars. For
6759 * conversations involving multiple people, the app should also make sure that it marks the
6760 * conversation as a group with {@link #setGroupConversation(boolean)}.
6761 *
6762 * <p>
6763 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior.
6764 * Here's an example of how this may be used:
Alex Hillsfc737de2016-03-23 17:33:02 -04006765 * <pre class="prettyprint">
6766 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006767 * Person user = new Person.Builder().setIcon(userIcon).setName(userName).build();
6768 * MessagingStyle style = new MessagingStyle(user)
6769 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getPerson())
6770 * .addMessage(messages[2].getText(), messages[2].getTime(), messages[2].getPerson())
6771 * .setGroupConversation(hasMultiplePeople());
6772 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006773 * Notification noti = new Notification.Builder()
Selim Cinekce8794f2018-05-23 16:46:05 -07006774 * .setContentTitle(&quot;2 new messages with &quot; + sender.toString())
Alex Hillsfc737de2016-03-23 17:33:02 -04006775 * .setContentText(subject)
6776 * .setSmallIcon(R.drawable.new_message)
6777 * .setLargeIcon(aBitmap)
Selim Cinekce8794f2018-05-23 16:46:05 -07006778 * .setStyle(style)
Alex Hillsfc737de2016-03-23 17:33:02 -04006779 * .build();
6780 * </pre>
6781 */
6782 public static class MessagingStyle extends Style {
6783
6784 /**
6785 * The maximum number of messages that will be retained in the Notification itself (the
6786 * number displayed is up to the platform).
6787 */
6788 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
6789
Selim Cinekcb8b9852017-12-15 18:01:52 -08006790 @NonNull Person mUser;
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006791 @Nullable CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04006792 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08006793 List<Message> mHistoricMessages = new ArrayList<>();
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006794 boolean mIsGroupConversation;
Alex Hillsfc737de2016-03-23 17:33:02 -04006795
6796 MessagingStyle() {
6797 }
6798
6799 /**
Alex Hillsfd590442016-10-07 09:52:44 -04006800 * @param userDisplayName Required - the name to be displayed for any replies sent by the
6801 * user before the posting app reposts the notification with those messages after they've
6802 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04006803 * {@link #addMessage(Notification.MessagingStyle.Message)}
Selim Cinekcb8b9852017-12-15 18:01:52 -08006804 *
6805 * @deprecated use {@code MessagingStyle(Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006806 */
Alex Hillsfd590442016-10-07 09:52:44 -04006807 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Selim Cinek9acd6732018-03-23 16:39:02 -07006808 this(new Person.Builder().setName(userDisplayName).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006809 }
6810
6811 /**
6812 * @param user Required - The person displayed for any messages that are sent by the
6813 * user. Any messages added with {@link #addMessage(Notification.MessagingStyle.Message)}
6814 * who don't have a Person associated with it will be displayed as if they were sent
Selim Cinek90343862018-02-01 11:07:11 -08006815 * by this user. The user also needs to have a valid name associated with it, which will
6816 * be enforced starting in Android P.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006817 */
6818 public MessagingStyle(@NonNull Person user) {
6819 mUser = user;
Selim Cinek90343862018-02-01 11:07:11 -08006820 }
6821
6822 /**
6823 * Validate that this style was properly composed. This is called at build time.
6824 * @hide
6825 */
6826 @Override
6827 public void validate(Context context) {
6828 super.validate(context);
6829 if (context.getApplicationInfo().targetSdkVersion
6830 >= Build.VERSION_CODES.P && (mUser == null || mUser.getName() == null)) {
6831 throw new RuntimeException("User must be valid and have a name.");
Selim Cinekcb8b9852017-12-15 18:01:52 -08006832 }
6833 }
6834
6835 /**
koprivaa1a78482018-10-09 10:09:23 -07006836 * @return the text that should be displayed in the statusBar when heads upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006837 * If {@code null} is returned, the default implementation will be used.
6838 *
6839 * @hide
6840 */
6841 @Override
6842 public CharSequence getHeadsUpStatusBarText() {
6843 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
6844 ? super.mBigContentTitle
6845 : mConversationTitle;
6846 if (!TextUtils.isEmpty(conversationTitle) && !hasOnlyWhiteSpaceSenders()) {
6847 return conversationTitle;
6848 }
6849 return null;
6850 }
6851
6852 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08006853 * @return the user to be displayed for any replies sent by the user
6854 */
Selim Cinekeb53c222018-04-06 12:56:09 -07006855 @NonNull
Selim Cinekcb8b9852017-12-15 18:01:52 -08006856 public Person getUser() {
6857 return mUser;
Alex Hillsfc737de2016-03-23 17:33:02 -04006858 }
6859
6860 /**
6861 * Returns the name to be displayed for any replies sent by the user
Selim Cinekcb8b9852017-12-15 18:01:52 -08006862 *
6863 * @deprecated use {@link #getUser()} instead
Alex Hillsfc737de2016-03-23 17:33:02 -04006864 */
6865 public CharSequence getUserDisplayName() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006866 return mUser.getName();
Alex Hillsfc737de2016-03-23 17:33:02 -04006867 }
6868
6869 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006870 * Sets the title to be displayed on this conversation. May be set to {@code null}.
6871 *
Kodlee Yin14656422017-12-22 17:00:46 -08006872 * <p>This API's behavior was changed in SDK version {@link Build.VERSION_CODES#P}. If your
6873 * application's target version is less than {@link Build.VERSION_CODES#P}, setting a
6874 * conversation title to a non-null value will make {@link #isGroupConversation()} return
6875 * {@code true} and passing {@code null} will make it return {@code false}. In
6876 * {@link Build.VERSION_CODES#P} and beyond, use {@link #setGroupConversation(boolean)}
6877 * to set group conversation status.
6878 *
6879 * @param conversationTitle Title displayed for this conversation
6880 * @return this object for method chaining
Alex Hillsfc737de2016-03-23 17:33:02 -04006881 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006882 public MessagingStyle setConversationTitle(@Nullable CharSequence conversationTitle) {
Alex Hillsfc737de2016-03-23 17:33:02 -04006883 mConversationTitle = conversationTitle;
6884 return this;
6885 }
6886
6887 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006888 * Return the title to be displayed on this conversation. May return {@code null}.
Alex Hillsfc737de2016-03-23 17:33:02 -04006889 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006890 @Nullable
Alex Hillsfc737de2016-03-23 17:33:02 -04006891 public CharSequence getConversationTitle() {
6892 return mConversationTitle;
6893 }
6894
6895 /**
6896 * Adds a message for display by this notification. Convenience call for a simple
6897 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6898 * @param text A {@link CharSequence} to be displayed as the message content
6899 * @param timestamp Time at which the message arrived
6900 * @param sender A {@link CharSequence} to be used for displaying the name of the
6901 * sender. Should be <code>null</code> for messages by the current user, in which case
6902 * the platform will insert {@link #getUserDisplayName()}.
6903 * Should be unique amongst all individuals in the conversation, and should be
6904 * consistent during re-posts of the notification.
6905 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07006906 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Alex Hillsfc737de2016-03-23 17:33:02 -04006907 *
6908 * @return this object for method chaining
Selim Cinekcb8b9852017-12-15 18:01:52 -08006909 *
6910 * @deprecated use {@link #addMessage(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006911 */
6912 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006913 return addMessage(text, timestamp,
Selim Cinek9acd6732018-03-23 16:39:02 -07006914 sender == null ? null : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006915 }
6916
6917 /**
6918 * Adds a message for display by this notification. Convenience call for a simple
6919 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6920 * @param text A {@link CharSequence} to be displayed as the message content
6921 * @param timestamp Time at which the message arrived
6922 * @param sender The {@link Person} who sent the message.
6923 * Should be <code>null</code> for messages by the current user, in which case
6924 * the platform will insert the user set in {@code MessagingStyle(Person)}.
6925 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07006926 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Selim Cinekcb8b9852017-12-15 18:01:52 -08006927 *
6928 * @return this object for method chaining
6929 */
Selim Cinekeb53c222018-04-06 12:56:09 -07006930 public MessagingStyle addMessage(@NonNull CharSequence text, long timestamp,
6931 @Nullable Person sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08006932 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04006933 }
6934
6935 /**
6936 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08006937 *
6938 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6939 * the newest last.
6940 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006941 * @param message The {@link Message} to be displayed
6942 * @return this object for method chaining
6943 */
6944 public MessagingStyle addMessage(Message message) {
6945 mMessages.add(message);
6946 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6947 mMessages.remove(0);
6948 }
6949 return this;
6950 }
6951
6952 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006953 * Adds a {@link Message} for historic context in this notification.
6954 *
6955 * <p>Messages should be added as historic if they are not the main subject of the
6956 * notification but may give context to a conversation. The system may choose to present
6957 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
6958 *
6959 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6960 * the newest last.
6961 *
6962 * @param message The historic {@link Message} to be added
6963 * @return this object for method chaining
6964 */
6965 public MessagingStyle addHistoricMessage(Message message) {
6966 mHistoricMessages.add(message);
6967 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6968 mHistoricMessages.remove(0);
6969 }
6970 return this;
6971 }
6972
6973 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006974 * Gets the list of {@code Message} objects that represent the notification
6975 */
6976 public List<Message> getMessages() {
6977 return mMessages;
6978 }
6979
6980 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006981 * Gets the list of historic {@code Message}s in the notification.
6982 */
6983 public List<Message> getHistoricMessages() {
6984 return mHistoricMessages;
6985 }
6986
6987 /**
Selim Cinekce8794f2018-05-23 16:46:05 -07006988 * Sets whether this conversation notification represents a group. If the app is targeting
6989 * Android P, this is required if the app wants to display the largeIcon set with
6990 * {@link Notification.Builder#setLargeIcon(Bitmap)}, otherwise it will be hidden.
Kodlee Yin14656422017-12-22 17:00:46 -08006991 *
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006992 * @param isGroupConversation {@code true} if the conversation represents a group,
6993 * {@code false} otherwise.
6994 * @return this object for method chaining
6995 */
6996 public MessagingStyle setGroupConversation(boolean isGroupConversation) {
6997 mIsGroupConversation = isGroupConversation;
6998 return this;
6999 }
7000
7001 /**
Kodlee Yin14656422017-12-22 17:00:46 -08007002 * Returns {@code true} if this notification represents a group conversation, otherwise
7003 * {@code false}.
7004 *
7005 * <p> If the application that generated this {@link MessagingStyle} targets an SDK version
7006 * less than {@link Build.VERSION_CODES#P}, this method becomes dependent on whether or
7007 * not the conversation title is set; returning {@code true} if the conversation title is
7008 * a non-null value, or {@code false} otherwise. From {@link Build.VERSION_CODES#P} forward,
7009 * this method returns what's set by {@link #setGroupConversation(boolean)} allowing for
7010 * named, non-group conversations.
7011 *
7012 * @see #setConversationTitle(CharSequence)
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007013 */
7014 public boolean isGroupConversation() {
Kodlee Yin14656422017-12-22 17:00:46 -08007015 // When target SDK version is < P, a non-null conversation title dictates if this is
7016 // as group conversation.
7017 if (mBuilder != null
7018 && mBuilder.mContext.getApplicationInfo().targetSdkVersion
7019 < Build.VERSION_CODES.P) {
7020 return mConversationTitle != null;
7021 }
7022
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007023 return mIsGroupConversation;
7024 }
7025
7026 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007027 * @hide
7028 */
7029 @Override
7030 public void addExtras(Bundle extras) {
7031 super.addExtras(extras);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007032 if (mUser != null) {
7033 // For legacy usages
7034 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUser.getName());
7035 extras.putParcelable(EXTRA_MESSAGING_PERSON, mUser);
Alex Hillsfc737de2016-03-23 17:33:02 -04007036 }
7037 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007038 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04007039 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007040 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
7041 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04007042 }
Adrian Roos437cd562017-01-18 15:47:03 -08007043 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
7044 Message.getBundleArrayForMessages(mHistoricMessages));
7045 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007046
7047 fixTitleAndTextExtras(extras);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007048 extras.putBoolean(EXTRA_IS_GROUP_CONVERSATION, mIsGroupConversation);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007049 }
7050
7051 private void fixTitleAndTextExtras(Bundle extras) {
7052 Message m = findLatestIncomingMessage();
7053 CharSequence text = (m == null) ? null : m.mText;
7054 CharSequence sender = m == null ? null
Selim Cinekcb8b9852017-12-15 18:01:52 -08007055 : m.mSender == null || TextUtils.isEmpty(m.mSender.getName())
7056 ? mUser.getName() : m.mSender.getName();
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007057 CharSequence title;
7058 if (!TextUtils.isEmpty(mConversationTitle)) {
7059 if (!TextUtils.isEmpty(sender)) {
7060 BidiFormatter bidi = BidiFormatter.getInstance();
7061 title = mBuilder.mContext.getString(
7062 com.android.internal.R.string.notification_messaging_title_template,
Selim Cinekcb8b9852017-12-15 18:01:52 -08007063 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(sender));
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007064 } else {
7065 title = mConversationTitle;
7066 }
7067 } else {
7068 title = sender;
7069 }
7070
7071 if (title != null) {
7072 extras.putCharSequence(EXTRA_TITLE, title);
7073 }
7074 if (text != null) {
7075 extras.putCharSequence(EXTRA_TEXT, text);
7076 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007077 }
7078
7079 /**
7080 * @hide
7081 */
7082 @Override
7083 protected void restoreFromExtras(Bundle extras) {
7084 super.restoreFromExtras(extras);
7085
Selim Cinekcb8b9852017-12-15 18:01:52 -08007086 mUser = extras.getParcelable(EXTRA_MESSAGING_PERSON);
7087 if (mUser == null) {
7088 CharSequence displayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
Selim Cinek9acd6732018-03-23 16:39:02 -07007089 mUser = new Person.Builder().setName(displayName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007090 }
Adrian Roos96b7e202016-05-17 13:50:38 -07007091 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08007092 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007093 mMessages = Message.getMessagesFromBundleArray(messages);
Adrian Roos437cd562017-01-18 15:47:03 -08007094 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007095 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007096 mIsGroupConversation = extras.getBoolean(EXTRA_IS_GROUP_CONVERSATION);
Alex Hillsfc737de2016-03-23 17:33:02 -04007097 }
7098
7099 /**
7100 * @hide
7101 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07007102 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007103 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07007104 mBuilder.mOriginalActions = mBuilder.mActions;
7105 mBuilder.mActions = new ArrayList<>();
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007106 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007107 false /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007108 mBuilder.mActions = mBuilder.mOriginalActions;
7109 mBuilder.mOriginalActions = null;
7110 return remoteViews;
Adrian Roosc1a80b02016-04-05 14:54:55 -07007111 }
7112
Julia Reynolds7217dc92018-03-07 12:12:09 -05007113 /**
7114 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04007115 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05007116 */
7117 @Override
7118 public boolean areNotificationsVisiblyDifferent(Style other) {
7119 if (other == null || getClass() != other.getClass()) {
7120 return true;
7121 }
7122 MessagingStyle newS = (MessagingStyle) other;
7123 List<MessagingStyle.Message> oldMs = getMessages();
7124 List<MessagingStyle.Message> newMs = newS.getMessages();
7125
Dan Sandler7d67bd42018-05-15 14:06:38 -04007126 if (oldMs == null || newMs == null) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007127 newMs = new ArrayList<>();
7128 }
7129
7130 int n = oldMs.size();
7131 if (n != newMs.size()) {
7132 return true;
7133 }
7134 for (int i = 0; i < n; i++) {
7135 MessagingStyle.Message oldM = oldMs.get(i);
7136 MessagingStyle.Message newM = newMs.get(i);
Dan Sandler7d67bd42018-05-15 14:06:38 -04007137 if (!Objects.equals(
7138 String.valueOf(oldM.getText()),
7139 String.valueOf(newM.getText()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007140 return true;
7141 }
7142 if (!Objects.equals(oldM.getDataUri(), newM.getDataUri())) {
7143 return true;
7144 }
Dan Sandler7d67bd42018-05-15 14:06:38 -04007145 String oldSender = String.valueOf(oldM.getSenderPerson() == null
7146 ? oldM.getSender()
7147 : oldM.getSenderPerson().getName());
7148 String newSender = String.valueOf(newM.getSenderPerson() == null
7149 ? newM.getSender()
7150 : newM.getSenderPerson().getName());
Julia Reynolds7217dc92018-03-07 12:12:09 -05007151 if (!Objects.equals(oldSender, newSender)) {
7152 return true;
7153 }
7154
7155 String oldKey = oldM.getSenderPerson() == null
7156 ? null : oldM.getSenderPerson().getKey();
7157 String newKey = newM.getSenderPerson() == null
7158 ? null : newM.getSenderPerson().getKey();
7159 if (!Objects.equals(oldKey, newKey)) {
7160 return true;
7161 }
7162 // Other fields (like timestamp) intentionally excluded
7163 }
7164 return false;
7165 }
7166
Adrian Roosc1a80b02016-04-05 14:54:55 -07007167 private Message findLatestIncomingMessage() {
Selim Cinek88188f22017-09-19 16:46:56 -07007168 return findLatestIncomingMessage(mMessages);
7169 }
7170
7171 /**
7172 * @hide
7173 */
7174 @Nullable
7175 public static Message findLatestIncomingMessage(
7176 List<Message> messages) {
7177 for (int i = messages.size() - 1; i >= 0; i--) {
7178 Message m = messages.get(i);
Adrian Roosc1a80b02016-04-05 14:54:55 -07007179 // Incoming messages have a non-empty sender.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007180 if (m.mSender != null && !TextUtils.isEmpty(m.mSender.getName())) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07007181 return m;
7182 }
7183 }
Selim Cinek88188f22017-09-19 16:46:56 -07007184 if (!messages.isEmpty()) {
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007185 // No incoming messages, fall back to outgoing message
Selim Cinek88188f22017-09-19 16:46:56 -07007186 return messages.get(messages.size() - 1);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007187 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07007188 return null;
7189 }
7190
7191 /**
7192 * @hide
7193 */
7194 @Override
7195 public RemoteViews makeBigContentView() {
Selim Cinek384804b2018-04-18 14:31:07 +08007196 return makeMessagingView(false /* displayImagesAtEnd */, true /* hideLargeIcon */);
Selim Cinekafeed292017-12-12 17:32:44 -08007197 }
7198
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007199 /**
7200 * Create a messaging layout.
7201 *
7202 * @param displayImagesAtEnd should images be displayed at the end of the content instead
7203 * of inline.
Selim Cinek384804b2018-04-18 14:31:07 +08007204 * @param hideRightIcons Should the reply affordance be shown at the end of the notification
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007205 * @return the created remoteView.
7206 */
Selim Cinekafeed292017-12-12 17:32:44 -08007207 @NonNull
Selim Cinek384804b2018-04-18 14:31:07 +08007208 private RemoteViews makeMessagingView(boolean displayImagesAtEnd, boolean hideRightIcons) {
Selim Cinek88188f22017-09-19 16:46:56 -07007209 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
Adrian Roosc1a80b02016-04-05 14:54:55 -07007210 ? super.mBigContentTitle
7211 : mConversationTitle;
Selim Cinekce8794f2018-05-23 16:46:05 -07007212 boolean atLeastP = mBuilder.mContext.getApplicationInfo().targetSdkVersion
7213 >= Build.VERSION_CODES.P;
7214 boolean isOneToOne;
Selim Cinek2dd3e722018-01-19 11:06:06 -08007215 CharSequence nameReplacement = null;
Selim Cinekce8794f2018-05-23 16:46:05 -07007216 Icon avatarReplacement = null;
7217 if (!atLeastP) {
7218 isOneToOne = TextUtils.isEmpty(conversationTitle);
7219 avatarReplacement = mBuilder.mN.mLargeIcon;
7220 if (hasOnlyWhiteSpaceSenders()) {
7221 isOneToOne = true;
7222 nameReplacement = conversationTitle;
7223 conversationTitle = null;
7224 }
7225 } else {
7226 isOneToOne = !isGroupConversation();
Adrian Roosb1f427c2016-05-26 12:27:15 -07007227 }
Selim Cinek384804b2018-04-18 14:31:07 +08007228 TemplateBindResult bindResult = new TemplateBindResult();
Adrian Roos48d746a2016-04-12 14:57:28 -07007229 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07007230 mBuilder.getMessagingLayoutResource(),
Selim Cinek88188f22017-09-19 16:46:56 -07007231 mBuilder.mParams.reset().hasProgress(false).title(conversationTitle).text(null)
Selim Cinek384804b2018-04-18 14:31:07 +08007232 .hideLargeIcon(hideRightIcons || isOneToOne)
7233 .hideReplyIcon(hideRightIcons)
7234 .headerTextSecondary(conversationTitle),
7235 bindResult);
Selim Cinek88188f22017-09-19 16:46:56 -07007236 addExtras(mBuilder.mN.extras);
Selim Cinekafeed292017-12-12 17:32:44 -08007237 // also update the end margin if there is an image
Selim Cinek1c72fa02018-04-23 18:00:54 +08007238 contentView.setViewLayoutMarginEnd(R.id.notification_messaging,
7239 bindResult.getIconMarginEnd());
Selim Cinek88188f22017-09-19 16:46:56 -07007240 contentView.setInt(R.id.status_bar_latest_event_content, "setLayoutColor",
Kenny Guy14d035c2018-05-02 19:10:36 +01007241 mBuilder.isColorized() ? mBuilder.getPrimaryTextColor()
7242 : mBuilder.resolveContrastColor());
7243 contentView.setInt(R.id.status_bar_latest_event_content, "setSenderTextColor",
7244 mBuilder.getPrimaryTextColor());
7245 contentView.setInt(R.id.status_bar_latest_event_content, "setMessageTextColor",
7246 mBuilder.getSecondaryTextColor());
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007247 contentView.setBoolean(R.id.status_bar_latest_event_content, "setDisplayImagesAtEnd",
7248 displayImagesAtEnd);
Selim Cinekce8794f2018-05-23 16:46:05 -07007249 contentView.setIcon(R.id.status_bar_latest_event_content, "setAvatarReplacement",
7250 avatarReplacement);
Selim Cinek2dd3e722018-01-19 11:06:06 -08007251 contentView.setCharSequence(R.id.status_bar_latest_event_content, "setNameReplacement",
7252 nameReplacement);
Selim Cinek88188f22017-09-19 16:46:56 -07007253 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsOneToOne",
7254 isOneToOne);
7255 contentView.setBundle(R.id.status_bar_latest_event_content, "setData",
7256 mBuilder.mN.extras);
Alex Hillsfc737de2016-03-23 17:33:02 -04007257 return contentView;
7258 }
7259
Selim Cinekf7409db2017-10-24 16:17:14 -07007260 private boolean hasOnlyWhiteSpaceSenders() {
7261 for (int i = 0; i < mMessages.size(); i++) {
7262 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007263 Person sender = m.getSenderPerson();
7264 if (sender != null && !isWhiteSpace(sender.getName())) {
Selim Cinekf7409db2017-10-24 16:17:14 -07007265 return false;
7266 }
7267 }
7268 return true;
7269 }
7270
7271 private boolean isWhiteSpace(CharSequence sender) {
7272 if (TextUtils.isEmpty(sender)) {
7273 return true;
7274 }
7275 if (sender.toString().matches("^\\s*$")) {
7276 return true;
7277 }
7278 // Let's check if we only have 0 whitespace chars. Some apps did this as a workaround
7279 // For the presentation that we had.
7280 for (int i = 0; i < sender.length(); i++) {
7281 char c = sender.charAt(i);
7282 if (c != '\u200B') {
7283 return false;
7284 }
7285 }
7286 return true;
7287 }
7288
Selim Cinek88188f22017-09-19 16:46:56 -07007289 private CharSequence createConversationTitleFromMessages() {
7290 ArraySet<CharSequence> names = new ArraySet<>();
7291 for (int i = 0; i < mMessages.size(); i++) {
7292 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007293 Person sender = m.getSenderPerson();
Selim Cinek88188f22017-09-19 16:46:56 -07007294 if (sender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007295 names.add(sender.getName());
Selim Cinek88188f22017-09-19 16:46:56 -07007296 }
7297 }
7298 SpannableStringBuilder title = new SpannableStringBuilder();
7299 int size = names.size();
7300 for (int i = 0; i < size; i++) {
7301 CharSequence name = names.valueAt(i);
7302 if (!TextUtils.isEmpty(title)) {
7303 title.append(", ");
7304 }
7305 title.append(BidiFormatter.getInstance().unicodeWrap(name));
7306 }
7307 return title;
7308 }
7309
Adrian Roosdedd1df2016-04-26 16:38:47 -07007310 /**
7311 * @hide
7312 */
7313 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007314 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007315 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007316 true /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007317 remoteViews.setInt(R.id.notification_messaging, "setMaxDisplayedLines", 1);
7318 return remoteViews;
Adrian Roosdedd1df2016-04-26 16:38:47 -07007319 }
7320
Adrian Roosc1a80b02016-04-05 14:54:55 -07007321 private static TextAppearanceSpan makeFontColorSpan(int color) {
7322 return new TextAppearanceSpan(null, 0, 0,
7323 ColorStateList.valueOf(color), null);
7324 }
7325
Alex Hillsd9b04d92016-04-11 16:38:16 -04007326 public static final class Message {
Tony Mak09db2ea2018-06-27 18:12:48 +01007327 /** @hide */
7328 public static final String KEY_TEXT = "text";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007329 static final String KEY_TIMESTAMP = "time";
7330 static final String KEY_SENDER = "sender";
Selim Cinekcb8b9852017-12-15 18:01:52 -08007331 static final String KEY_SENDER_PERSON = "sender_person";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007332 static final String KEY_DATA_MIME_TYPE = "type";
7333 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08007334 static final String KEY_EXTRAS_BUNDLE = "extras";
Kenny Guya0f6de82018-04-06 16:20:16 +01007335 static final String KEY_REMOTE_INPUT_HISTORY = "remote_input_history";
Alex Hillsfc737de2016-03-23 17:33:02 -04007336
7337 private final CharSequence mText;
7338 private final long mTimestamp;
Selim Cinekcb8b9852017-12-15 18:01:52 -08007339 @Nullable
7340 private final Person mSender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007341 /** True if this message was generated from the extra
7342 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}
7343 */
7344 private final boolean mRemoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007345
Shane Brennan5a871862017-03-11 13:14:17 -08007346 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007347 private String mDataMimeType;
7348 private Uri mDataUri;
7349
7350 /**
7351 * Constructor
7352 * @param text A {@link CharSequence} to be displayed as the message content
7353 * @param timestamp Time at which the message arrived
7354 * @param sender A {@link CharSequence} to be used for displaying the name of the
7355 * sender. Should be <code>null</code> for messages by the current user, in which case
7356 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
7357 * Should be unique amongst all individuals in the conversation, and should be
7358 * consistent during re-posts of the notification.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007359 *
7360 * @deprecated use {@code Message(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04007361 */
7362 public Message(CharSequence text, long timestamp, CharSequence sender){
Selim Cinek9acd6732018-03-23 16:39:02 -07007363 this(text, timestamp, sender == null ? null
7364 : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08007365 }
7366
7367 /**
7368 * Constructor
7369 * @param text A {@link CharSequence} to be displayed as the message content
7370 * @param timestamp Time at which the message arrived
7371 * @param sender The {@link Person} who sent the message.
7372 * Should be <code>null</code> for messages by the current user, in which case
7373 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7374 * <p>
Selim Cinek9acd6732018-03-23 16:39:02 -07007375 * The person provided should contain an Icon, set with
7376 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7377 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7378 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7379 * to differentiate between the different users.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007380 * </p>
7381 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007382 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender) {
Kenny Guya0f6de82018-04-06 16:20:16 +01007383 this(text, timestamp, sender, false /* remoteHistory */);
7384 }
7385
7386 /**
7387 * Constructor
7388 * @param text A {@link CharSequence} to be displayed as the message content
7389 * @param timestamp Time at which the message arrived
7390 * @param sender The {@link Person} who sent the message.
7391 * Should be <code>null</code> for messages by the current user, in which case
7392 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7393 * @param remoteInputHistory True if the messages was generated from the extra
7394 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7395 * <p>
7396 * The person provided should contain an Icon, set with
7397 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7398 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7399 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7400 * to differentiate between the different users.
7401 * </p>
7402 * @hide
7403 */
7404 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender,
7405 boolean remoteInputHistory) {
Alex Hillsfc737de2016-03-23 17:33:02 -04007406 mText = text;
7407 mTimestamp = timestamp;
7408 mSender = sender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007409 mRemoteInputHistory = remoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007410 }
7411
7412 /**
7413 * Sets a binary blob of data and an associated MIME type for a message. In the case
7414 * where the platform doesn't support the MIME type, the original text provided in the
7415 * constructor will be used.
7416 * @param dataMimeType The MIME type of the content. See
7417 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
7418 * types on Android and Android Wear.
7419 * @param dataUri The uri containing the content whose type is given by the MIME type.
7420 * <p class="note">
7421 * <ol>
7422 * <li>Notification Listeners including the System UI need permission to access the
7423 * data the Uri points to. The recommended ways to do this are:</li>
7424 * <li>Store the data in your own ContentProvider, making sure that other apps have
7425 * the correct permission to access your provider. The preferred mechanism for
7426 * providing access is to use per-URI permissions which are temporary and only
7427 * grant access to the receiving application. An easy way to create a
7428 * ContentProvider like this is to use the FileProvider helper class.</li>
7429 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
7430 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
7431 * also store non-media types (see MediaStore.Files for more info). Files can be
7432 * inserted into the MediaStore using scanFile() after which a content:// style
7433 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
7434 * Note that once added to the system MediaStore the content is accessible to any
7435 * app on the device.</li>
7436 * </ol>
7437 * @return this object for method chaining
7438 */
7439 public Message setData(String dataMimeType, Uri dataUri) {
7440 mDataMimeType = dataMimeType;
7441 mDataUri = dataUri;
7442 return this;
7443 }
7444
Alex Hillsfc737de2016-03-23 17:33:02 -04007445 /**
7446 * Get the text to be used for this message, or the fallback text if a type and content
7447 * Uri have been set
7448 */
7449 public CharSequence getText() {
7450 return mText;
7451 }
7452
7453 /**
7454 * Get the time at which this message arrived
7455 */
7456 public long getTimestamp() {
7457 return mTimestamp;
7458 }
7459
7460 /**
Shane Brennan5a871862017-03-11 13:14:17 -08007461 * Get the extras Bundle for this message.
7462 */
7463 public Bundle getExtras() {
7464 return mExtras;
7465 }
7466
7467 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007468 * Get the text used to display the contact's name in the messaging experience
Selim Cinekcb8b9852017-12-15 18:01:52 -08007469 *
7470 * @deprecated use {@link #getSenderPerson()}
Alex Hillsfc737de2016-03-23 17:33:02 -04007471 */
7472 public CharSequence getSender() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007473 return mSender == null ? null : mSender.getName();
7474 }
7475
7476 /**
7477 * Get the sender associated with this message.
7478 */
7479 @Nullable
7480 public Person getSenderPerson() {
Alex Hillsfc737de2016-03-23 17:33:02 -04007481 return mSender;
7482 }
7483
7484 /**
7485 * Get the MIME type of the data pointed to by the Uri
7486 */
7487 public String getDataMimeType() {
7488 return mDataMimeType;
7489 }
7490
7491 /**
koprivaa1a78482018-10-09 10:09:23 -07007492 * Get the Uri pointing to the content of the message. Can be null, in which case
Alex Hillsfc737de2016-03-23 17:33:02 -04007493 * {@see #getText()} is used.
7494 */
7495 public Uri getDataUri() {
7496 return mDataUri;
7497 }
7498
Kenny Guya0f6de82018-04-06 16:20:16 +01007499 /**
7500 * @return True if the message was generated from
7501 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7502 * @hide
7503 */
7504 public boolean isRemoteInputHistory() {
7505 return mRemoteInputHistory;
7506 }
7507
Beverlye98937a2018-11-15 10:18:57 -05007508 /**
7509 * @hide
7510 */
7511 @VisibleForTesting
7512 public Bundle toBundle() {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007513 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007514 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007515 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04007516 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007517 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04007518 if (mSender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007519 // Legacy listeners need this
7520 bundle.putCharSequence(KEY_SENDER, mSender.getName());
7521 bundle.putParcelable(KEY_SENDER_PERSON, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04007522 }
7523 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007524 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04007525 }
7526 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007527 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04007528 }
Shane Brennan5a871862017-03-11 13:14:17 -08007529 if (mExtras != null) {
7530 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
7531 }
Kenny Guya0f6de82018-04-06 16:20:16 +01007532 if (mRemoteInputHistory) {
7533 bundle.putBoolean(KEY_REMOTE_INPUT_HISTORY, mRemoteInputHistory);
7534 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007535 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04007536 }
7537
Alex Hillsd9b04d92016-04-11 16:38:16 -04007538 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
7539 Bundle[] bundles = new Bundle[messages.size()];
7540 final int N = messages.size();
7541 for (int i = 0; i < N; i++) {
7542 bundles[i] = messages.get(i).toBundle();
7543 }
7544 return bundles;
7545 }
7546
Selim Cinek88188f22017-09-19 16:46:56 -07007547 /**
7548 * @return A list of messages read from the bundles.
7549 *
7550 * @hide
7551 */
7552 public static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
7553 if (bundles == null) {
7554 return new ArrayList<>();
7555 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007556 List<Message> messages = new ArrayList<>(bundles.length);
7557 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07007558 if (bundles[i] instanceof Bundle) {
7559 Message message = getMessageFromBundle((Bundle)bundles[i]);
7560 if (message != null) {
7561 messages.add(message);
7562 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007563 }
7564 }
7565 return messages;
7566 }
7567
Selim Cinekb0dc61b2018-05-22 18:49:36 -07007568 /**
7569 * @return The message that is stored in the bundle or null if the message couldn't be
7570 * resolved.
7571 *
7572 * @hide
7573 */
7574 @Nullable
7575 public static Message getMessageFromBundle(Bundle bundle) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007576 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07007577 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007578 return null;
7579 } else {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007580
7581 Person senderPerson = bundle.getParcelable(KEY_SENDER_PERSON);
7582 if (senderPerson == null) {
7583 // Legacy apps that use compat don't actually provide the sender objects
7584 // We need to fix the compat version to provide people / use
7585 // the native api instead
7586 CharSequence senderName = bundle.getCharSequence(KEY_SENDER);
7587 if (senderName != null) {
Selim Cinek9acd6732018-03-23 16:39:02 -07007588 senderPerson = new Person.Builder().setName(senderName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007589 }
7590 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007591 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
Selim Cinekcb8b9852017-12-15 18:01:52 -08007592 bundle.getLong(KEY_TIMESTAMP),
Kenny Guya0f6de82018-04-06 16:20:16 +01007593 senderPerson,
7594 bundle.getBoolean(KEY_REMOTE_INPUT_HISTORY, false));
Alex Hillsd9b04d92016-04-11 16:38:16 -04007595 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
7596 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007597 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
7598 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04007599 }
Shane Brennan5a871862017-03-11 13:14:17 -08007600 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
7601 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
7602 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007603 return message;
7604 }
7605 } catch (ClassCastException e) {
7606 return null;
7607 }
7608 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007609 }
7610 }
7611
7612 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04007613 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08007614 *
Robert Ly91c5ce32014-06-08 15:37:00 -07007615 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04007616 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07007617 * Notification notif = new Notification.Builder(mContext)
7618 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
7619 * .setContentText(subject)
7620 * .setSmallIcon(R.drawable.new_mail)
7621 * .setLargeIcon(aBitmap)
7622 * .setStyle(new Notification.InboxStyle()
7623 * .addLine(str1)
7624 * .addLine(str2)
7625 * .setContentTitle(&quot;&quot;)
7626 * .setSummaryText(&quot;+3 more&quot;))
7627 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04007628 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08007629 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04007630 * @see Notification#bigContentView
7631 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007632 public static class InboxStyle extends Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07007633
7634 /**
7635 * The number of lines of remote input history allowed until we start reducing lines.
7636 */
7637 private static final int NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION = 1;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007638 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
7639
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007640 public InboxStyle() {
7641 }
7642
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007643 /**
7644 * @deprecated use {@code InboxStyle()}.
7645 */
7646 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04007647 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007648 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04007649 }
7650
Chris Wrend6297db2012-05-03 16:20:13 -04007651 /**
7652 * Overrides ContentTitle in the big form of the template.
7653 * This defaults to the value passed to setContentTitle().
7654 */
7655 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007656 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04007657 return this;
7658 }
7659
7660 /**
7661 * Set the first line of text after the detail section in the big form of the template.
7662 */
7663 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007664 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04007665 return this;
7666 }
7667
Chris Wren0bd664d2012-08-01 13:56:56 -04007668 /**
7669 * Append a line to the digest section of the Inbox notification.
7670 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04007671 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007672 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04007673 return this;
7674 }
7675
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007676 /**
7677 * @hide
7678 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05007679 public ArrayList<CharSequence> getLines() {
7680 return mTexts;
7681 }
7682
7683 /**
7684 * @hide
7685 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007686 public void addExtras(Bundle extras) {
7687 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007688
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007689 CharSequence[] a = new CharSequence[mTexts.size()];
7690 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
7691 }
7692
Christoph Studer4600f9b2014-07-22 22:44:43 +02007693 /**
7694 * @hide
7695 */
7696 @Override
7697 protected void restoreFromExtras(Bundle extras) {
7698 super.restoreFromExtras(extras);
7699
7700 mTexts.clear();
7701 if (extras.containsKey(EXTRA_TEXT_LINES)) {
7702 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
7703 }
7704 }
7705
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007706 /**
7707 * @hide
7708 */
7709 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08007710 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02007711 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007712 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
7713 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007714
Selim Cinek384804b2018-04-18 14:31:07 +08007715 TemplateBindResult result = new TemplateBindResult();
7716 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource(), result);
Daniel Sandler619738c2012-06-07 16:33:08 -04007717
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007718 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007719
Chris Wrend6297db2012-05-03 16:20:13 -04007720 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 -04007721 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04007722
Chris Wren4ed80d52012-05-17 09:30:03 -04007723 // Make sure all rows are gone in case we reuse a view.
7724 for (int rowId : rowIds) {
7725 contentView.setViewVisibility(rowId, View.GONE);
7726 }
7727
Daniel Sandler879c5e02012-04-17 16:46:51 -04007728 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07007729 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7730 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08007731 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07007732 int onlyViewId = 0;
7733 int maxRows = rowIds.length;
7734 if (mBuilder.mActions.size() > 0) {
7735 maxRows--;
7736 }
Selim Cinekbee4e072018-05-21 22:06:43 -07007737 CharSequence[] remoteInputHistory = mBuilder.mN.extras.getCharSequenceArray(
7738 EXTRA_REMOTE_INPUT_HISTORY);
7739 if (remoteInputHistory != null
7740 && remoteInputHistory.length > NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION) {
7741 // Let's remove some messages to make room for the remote input history.
7742 // 1 is always able to fit, but let's remove them if they are 2 or 3
7743 int numRemoteInputs = Math.min(remoteInputHistory.length,
7744 MAX_REMOTE_INPUT_HISTORY_LINES);
7745 int totalNumRows = mTexts.size() + numRemoteInputs
7746 - NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION;
7747 if (totalNumRows > maxRows) {
7748 int overflow = totalNumRows - maxRows;
7749 if (mTexts.size() > maxRows) {
7750 // Heuristic: if the Texts don't fit anyway, we'll rather drop the last
7751 // few messages, even with the remote input
7752 maxRows -= overflow;
7753 } else {
7754 // otherwise we drop the first messages
7755 i = overflow;
7756 }
7757 }
7758 }
Selim Cinek07c80172016-04-21 16:40:47 -07007759 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007760 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07007761 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007762 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07007763 contentView.setTextViewText(rowIds[i],
7764 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08007765 mBuilder.setTextViewColorSecondary(contentView, rowIds[i]);
Selim Cinek07c80172016-04-21 16:40:47 -07007766 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek384804b2018-04-18 14:31:07 +08007767 handleInboxImageMargin(contentView, rowIds[i], first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007768 result.getIconMarginEnd());
Selim Cinek07c80172016-04-21 16:40:47 -07007769 if (first) {
7770 onlyViewId = rowIds[i];
7771 } else {
7772 onlyViewId = 0;
7773 }
Selim Cinek247fa012016-02-18 09:50:48 -08007774 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007775 }
7776 i++;
7777 }
Selim Cinek07c80172016-04-21 16:40:47 -07007778 if (onlyViewId != 0) {
7779 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
7780 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7781 R.dimen.notification_text_margin_top);
7782 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
7783 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007784
Daniel Sandler879c5e02012-04-17 16:46:51 -04007785 return contentView;
7786 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007787
Julia Reynolds7217dc92018-03-07 12:12:09 -05007788 /**
7789 * @hide
7790 */
7791 @Override
7792 public boolean areNotificationsVisiblyDifferent(Style other) {
7793 if (other == null || getClass() != other.getClass()) {
7794 return true;
7795 }
7796 InboxStyle newS = (InboxStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04007797
7798 final ArrayList<CharSequence> myLines = getLines();
7799 final ArrayList<CharSequence> newLines = newS.getLines();
7800 final int n = myLines.size();
7801 if (n != newLines.size()) {
7802 return true;
7803 }
7804
7805 for (int i = 0; i < n; i++) {
7806 if (!Objects.equals(
7807 String.valueOf(myLines.get(i)),
7808 String.valueOf(newLines.get(i)))) {
7809 return true;
7810 }
7811 }
7812 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05007813 }
7814
Selim Cinek384804b2018-04-18 14:31:07 +08007815 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007816 int marginEndValue) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08007817 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08007818 if (first) {
7819 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
7820 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
7821 boolean hasProgress = max != 0 || ind;
Selim Cinek384804b2018-04-18 14:31:07 +08007822 if (!hasProgress) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08007823 endMargin = marginEndValue;
Selim Cinek247fa012016-02-18 09:50:48 -08007824 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007825 }
Selim Cinek1c72fa02018-04-23 18:00:54 +08007826 contentView.setViewLayoutMarginEnd(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08007827 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04007828 }
Dan Sandler842dd772014-05-15 09:36:47 -04007829
7830 /**
7831 * Notification style for media playback notifications.
7832 *
7833 * In the expanded form, {@link Notification#bigContentView}, up to 5
7834 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04007835 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04007836 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
7837 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
7838 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08007839 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04007840 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
7841 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01007842 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04007843 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08007844 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007845 * Notifications created with MediaStyle will have their category set to
7846 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
7847 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08007848 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07007849 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
7850 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04007851 * the System UI can identify this as a notification representing an active media session
7852 * and respond accordingly (by showing album artwork in the lockscreen, for example).
7853 *
Selim Cinek99104832017-01-25 14:47:33 -08007854 * <p>
7855 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
7856 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
7857 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
7858 * <p>
7859 *
Dan Sandler842dd772014-05-15 09:36:47 -04007860 * To use this style with your Notification, feed it to
7861 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7862 * <pre class="prettyprint">
7863 * Notification noti = new Notification.Builder()
7864 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01007865 * .setContentTitle(&quot;Track title&quot;)
7866 * .setContentText(&quot;Artist - Album&quot;)
7867 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007868 * .setStyle(<b>new Notification.MediaStyle()</b>
7869 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04007870 * .build();
7871 * </pre>
7872 *
7873 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08007874 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04007875 */
7876 public static class MediaStyle extends Style {
Gus Prevas9cc96602018-10-11 11:24:23 -04007877 // Changing max media buttons requires also changing templates
7878 // (notification_template_material_media and notification_template_material_big_media).
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007879 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04007880 static final int MAX_MEDIA_BUTTONS = 5;
Gus Prevas9cc96602018-10-11 11:24:23 -04007881 @IdRes private static final int[] MEDIA_BUTTON_IDS = {
7882 R.id.action0,
7883 R.id.action1,
7884 R.id.action2,
7885 R.id.action3,
7886 R.id.action4,
7887 };
Dan Sandler842dd772014-05-15 09:36:47 -04007888
7889 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07007890 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04007891
7892 public MediaStyle() {
7893 }
7894
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007895 /**
7896 * @deprecated use {@code MediaStyle()}.
7897 */
7898 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04007899 public MediaStyle(Builder builder) {
7900 setBuilder(builder);
7901 }
7902
7903 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007904 * 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 -04007905 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007906 *
7907 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04007908 */
7909 public MediaStyle setShowActionsInCompactView(int...actions) {
7910 mActionsToShowInCompact = actions;
7911 return this;
7912 }
7913
7914 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07007915 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
7916 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04007917 */
Jeff Browndba34ba2014-06-24 20:46:03 -07007918 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04007919 mToken = token;
7920 return this;
7921 }
7922
Christoph Studer4600f9b2014-07-22 22:44:43 +02007923 /**
7924 * @hide
7925 */
Dan Sandler842dd772014-05-15 09:36:47 -04007926 @Override
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007927 @UnsupportedAppUsage
Dan Sandler842dd772014-05-15 09:36:47 -04007928 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02007929 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007930 if (wip.category == null) {
7931 wip.category = Notification.CATEGORY_TRANSPORT;
7932 }
Dan Sandler842dd772014-05-15 09:36:47 -04007933 return wip;
7934 }
7935
Christoph Studer4600f9b2014-07-22 22:44:43 +02007936 /**
7937 * @hide
7938 */
7939 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007940 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007941 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007942 }
7943
7944 /**
7945 * @hide
7946 */
7947 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007948 public RemoteViews makeBigContentView() {
7949 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007950 }
7951
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007952 /**
7953 * @hide
7954 */
7955 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007956 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007957 RemoteViews expanded = makeMediaBigContentView();
7958 return expanded != null ? expanded : makeMediaContentView();
7959 }
7960
Dan Sandler842dd772014-05-15 09:36:47 -04007961 /** @hide */
7962 @Override
7963 public void addExtras(Bundle extras) {
7964 super.addExtras(extras);
7965
7966 if (mToken != null) {
7967 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
7968 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01007969 if (mActionsToShowInCompact != null) {
7970 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
7971 }
Dan Sandler842dd772014-05-15 09:36:47 -04007972 }
7973
Christoph Studer4600f9b2014-07-22 22:44:43 +02007974 /**
7975 * @hide
7976 */
7977 @Override
7978 protected void restoreFromExtras(Bundle extras) {
7979 super.restoreFromExtras(extras);
7980
7981 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
7982 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
7983 }
7984 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
7985 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
7986 }
7987 }
7988
Julia Reynolds7217dc92018-03-07 12:12:09 -05007989 /**
7990 * @hide
7991 */
7992 @Override
7993 public boolean areNotificationsVisiblyDifferent(Style other) {
7994 if (other == null || getClass() != other.getClass()) {
7995 return true;
7996 }
7997 // All fields to compare are on the Notification object
7998 return false;
7999 }
8000
Gus Prevas9cc96602018-10-11 11:24:23 -04008001 private void bindMediaActionButton(RemoteViews container, @IdRes int buttonId,
8002 Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04008003 final boolean tombstone = (action.actionIntent == null);
Gus Prevas9cc96602018-10-11 11:24:23 -04008004 container.setViewVisibility(buttonId, View.VISIBLE);
8005 container.setImageViewIcon(buttonId, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07008006
8007 // If the action buttons should not be tinted, then just use the default
8008 // notification color. Otherwise, just use the passed-in color.
Gus Prevas9cc96602018-10-11 11:24:23 -04008009 Resources resources = mBuilder.mContext.getResources();
8010 Configuration currentConfig = resources.getConfiguration();
Lucas Dupinf03e7522018-06-25 16:21:13 -07008011 boolean inNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
8012 == Configuration.UI_MODE_NIGHT_YES;
Anthony Chenad4d1582017-04-10 16:07:58 -07008013 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized()
8014 ? color
Lucas Dupina291d192018-06-07 13:59:42 -07008015 : ContrastColorUtil.resolveColor(mBuilder.mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07008016 Notification.COLOR_DEFAULT, inNightMode);
Anthony Chenad4d1582017-04-10 16:07:58 -07008017
Gus Prevas9cc96602018-10-11 11:24:23 -04008018 container.setDrawableTint(buttonId, false, tintColor,
Sunny Goyal5b153922017-09-21 21:00:36 -07008019 PorterDuff.Mode.SRC_ATOP);
Gus Prevas9cc96602018-10-11 11:24:23 -04008020
8021 final TypedArray typedArray = mBuilder.mContext.obtainStyledAttributes(
8022 new int[]{ android.R.attr.colorControlHighlight });
8023 int rippleAlpha = Color.alpha(typedArray.getColor(0, 0));
8024 typedArray.recycle();
8025 int rippleColor = Color.argb(rippleAlpha, Color.red(tintColor), Color.green(tintColor),
8026 Color.blue(tintColor));
8027 container.setRippleDrawableColor(buttonId, ColorStateList.valueOf(rippleColor));
8028
Dan Sandler842dd772014-05-15 09:36:47 -04008029 if (!tombstone) {
Gus Prevas9cc96602018-10-11 11:24:23 -04008030 container.setOnClickPendingIntent(buttonId, action.actionIntent);
Dan Sandler842dd772014-05-15 09:36:47 -04008031 }
Gus Prevas9cc96602018-10-11 11:24:23 -04008032 container.setContentDescription(buttonId, action.title);
Dan Sandler842dd772014-05-15 09:36:47 -04008033 }
8034
8035 private RemoteViews makeMediaContentView() {
8036 RemoteViews view = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08008037 R.layout.notification_template_material_media, false, /* hasProgress */
8038 null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04008039
8040 final int numActions = mBuilder.mActions.size();
Gus Prevas9cc96602018-10-11 11:24:23 -04008041 final int numActionsToShow = mActionsToShowInCompact == null
Dan Sandler842dd772014-05-15 09:36:47 -04008042 ? 0
8043 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Gus Prevas9cc96602018-10-11 11:24:23 -04008044 if (numActionsToShow > numActions) {
8045 throw new IllegalArgumentException(String.format(
8046 "setShowActionsInCompactView: action %d out of bounds (max %d)",
8047 numActions, numActions - 1));
8048 }
8049 for (int i = 0; i < MAX_MEDIA_BUTTONS_IN_COMPACT; i++) {
8050 if (i < numActionsToShow) {
Dan Sandler842dd772014-05-15 09:36:47 -04008051 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Gus Prevas9cc96602018-10-11 11:24:23 -04008052 bindMediaActionButton(view, MEDIA_BUTTON_IDS[i], action, getActionColor());
8053 } else {
8054 view.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04008055 }
8056 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08008057 handleImage(view);
8058 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008059 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07008060 if (mBuilder.mN.hasLargeIcon()) {
Selim Cinek384804b2018-04-18 14:31:07 +08008061 endMargin = R.dimen.notification_media_image_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08008062 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008063 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04008064 return view;
8065 }
8066
Selim Cinekc7f5a822018-03-20 19:32:06 -07008067 private int getActionColor() {
8068 return mBuilder.isColorized() ? mBuilder.getPrimaryTextColor()
8069 : mBuilder.resolveContrastColor();
Selim Cinek99104832017-01-25 14:47:33 -08008070 }
8071
Dan Sandler842dd772014-05-15 09:36:47 -04008072 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008073 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008074 // Dont add an expanded view if there is no more content to be revealed
8075 int actionsInCompact = mActionsToShowInCompact == null
8076 ? 0
8077 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07008078 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008079 return null;
8080 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05008081 RemoteViews big = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08008082 R.layout.notification_template_material_big_media, false, null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04008083
Gus Prevas9cc96602018-10-11 11:24:23 -04008084 for (int i = 0; i < MAX_MEDIA_BUTTONS; i++) {
8085 if (i < actionCount) {
8086 bindMediaActionButton(big, MEDIA_BUTTON_IDS[i], mBuilder.mActions.get(i),
Selim Cinekc7f5a822018-03-20 19:32:06 -07008087 getActionColor());
Gus Prevas9cc96602018-10-11 11:24:23 -04008088 } else {
8089 big.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04008090 }
8091 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05008092 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04008093 return big;
8094 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008095
Selim Cinek5bf069a2015-11-10 19:14:27 -05008096 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07008097 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008098 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
8099 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008100 }
8101 }
8102
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008103 /**
8104 * @hide
8105 */
8106 @Override
8107 protected boolean hasProgress() {
8108 return false;
8109 }
Dan Sandler842dd772014-05-15 09:36:47 -04008110 }
Griff Hazen61a9e862014-05-22 16:05:19 -07008111
Selim Cinek593610c2016-02-16 18:42:57 -08008112 /**
8113 * Notification style for custom views that are decorated by the system
8114 *
8115 * <p>Instead of providing a notification that is completely custom, a developer can set this
8116 * style and still obtain system decorations like the notification header with the expand
8117 * affordance and actions.
8118 *
8119 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8120 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8121 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8122 * corresponding custom views to display.
8123 *
8124 * To use this style with your Notification, feed it to
8125 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8126 * <pre class="prettyprint">
8127 * Notification noti = new Notification.Builder()
8128 * .setSmallIcon(R.drawable.ic_stat_player)
8129 * .setLargeIcon(albumArtBitmap))
8130 * .setCustomContentView(contentView);
8131 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
8132 * .build();
8133 * </pre>
8134 */
8135 public static class DecoratedCustomViewStyle extends Style {
8136
8137 public DecoratedCustomViewStyle() {
8138 }
8139
Selim Cinek593610c2016-02-16 18:42:57 -08008140 /**
8141 * @hide
8142 */
8143 public boolean displayCustomViewInline() {
8144 return true;
8145 }
8146
8147 /**
8148 * @hide
8149 */
8150 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008151 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008152 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
8153 }
8154
8155 /**
8156 * @hide
8157 */
8158 @Override
8159 public RemoteViews makeBigContentView() {
8160 return makeDecoratedBigContentView();
8161 }
8162
8163 /**
8164 * @hide
8165 */
8166 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008167 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008168 return makeDecoratedHeadsUpContentView();
8169 }
8170
Selim Cinek593610c2016-02-16 18:42:57 -08008171 private RemoteViews makeDecoratedHeadsUpContentView() {
8172 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
8173 ? mBuilder.mN.contentView
8174 : mBuilder.mN.headsUpContentView;
8175 if (mBuilder.mActions.size() == 0) {
8176 return makeStandardTemplateWithCustomContent(headsUpContentView);
8177 }
Selim Cinek384804b2018-04-18 14:31:07 +08008178 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008179 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008180 mBuilder.getBigBaseLayoutResource(), result);
8181 buildIntoRemoteViewContent(remoteViews, headsUpContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008182 return remoteViews;
8183 }
8184
Selim Cinek593610c2016-02-16 18:42:57 -08008185 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
Selim Cinek384804b2018-04-18 14:31:07 +08008186 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008187 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08008188 mBuilder.getBaseLayoutResource(), result);
8189 buildIntoRemoteViewContent(remoteViews, customContent, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008190 return remoteViews;
8191 }
8192
Selim Cinek593610c2016-02-16 18:42:57 -08008193 private RemoteViews makeDecoratedBigContentView() {
8194 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
8195 ? mBuilder.mN.contentView
8196 : mBuilder.mN.bigContentView;
8197 if (mBuilder.mActions.size() == 0) {
8198 return makeStandardTemplateWithCustomContent(bigContentView);
8199 }
Selim Cinek384804b2018-04-18 14:31:07 +08008200 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008201 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008202 mBuilder.getBigBaseLayoutResource(), result);
8203 buildIntoRemoteViewContent(remoteViews, bigContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008204 return remoteViews;
8205 }
Selim Cinek247fa012016-02-18 09:50:48 -08008206
8207 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
Selim Cinek384804b2018-04-18 14:31:07 +08008208 RemoteViews customContent, TemplateBindResult result) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08008209 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008210 // Need to clone customContent before adding, because otherwise it can no longer be
8211 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008212 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07008213 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
8214 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Selim Cinekfc8073c2017-08-16 17:50:20 -07008215 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08008216 }
Selim Cinek247fa012016-02-18 09:50:48 -08008217 // also update the end margin if there is an image
Selim Cinek384804b2018-04-18 14:31:07 +08008218 Resources resources = mBuilder.mContext.getResources();
8219 int endMargin = resources.getDimensionPixelSize(
Selim Cinek1c72fa02018-04-23 18:00:54 +08008220 R.dimen.notification_content_margin_end) + result.getIconMarginEnd();
Selim Cinek384804b2018-04-18 14:31:07 +08008221 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08008222 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05008223
8224 /**
8225 * @hide
8226 */
8227 @Override
8228 public boolean areNotificationsVisiblyDifferent(Style other) {
8229 if (other == null || getClass() != other.getClass()) {
8230 return true;
8231 }
8232 // Comparison done for all custom RemoteViews, independent of style
8233 return false;
8234 }
Selim Cinek593610c2016-02-16 18:42:57 -08008235 }
8236
Selim Cinek03eb3b72016-02-18 10:39:45 -08008237 /**
8238 * Notification style for media custom views that are decorated by the system
8239 *
8240 * <p>Instead of providing a media notification that is completely custom, a developer can set
8241 * this style and still obtain system decorations like the notification header with the expand
8242 * affordance and actions.
8243 *
8244 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8245 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8246 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8247 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08008248 * <p>
8249 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
8250 * notification by using {@link Notification.Builder#setColorized(boolean)}.
8251 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08008252 * To use this style with your Notification, feed it to
8253 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8254 * <pre class="prettyprint">
8255 * Notification noti = new Notification.Builder()
8256 * .setSmallIcon(R.drawable.ic_stat_player)
8257 * .setLargeIcon(albumArtBitmap))
8258 * .setCustomContentView(contentView);
8259 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
8260 * .setMediaSession(mySession))
8261 * .build();
8262 * </pre>
8263 *
8264 * @see android.app.Notification.DecoratedCustomViewStyle
8265 * @see android.app.Notification.MediaStyle
8266 */
8267 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
8268
8269 public DecoratedMediaCustomViewStyle() {
8270 }
8271
Selim Cinek03eb3b72016-02-18 10:39:45 -08008272 /**
8273 * @hide
8274 */
8275 public boolean displayCustomViewInline() {
8276 return true;
8277 }
8278
8279 /**
8280 * @hide
8281 */
8282 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008283 public RemoteViews makeContentView(boolean increasedHeight) {
8284 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008285 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8286 mBuilder.mN.contentView);
8287 }
8288
8289 /**
8290 * @hide
8291 */
8292 @Override
8293 public RemoteViews makeBigContentView() {
8294 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
8295 ? mBuilder.mN.bigContentView
8296 : mBuilder.mN.contentView;
8297 return makeBigContentViewWithCustomContent(customRemoteView);
8298 }
8299
8300 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
8301 RemoteViews remoteViews = super.makeBigContentView();
8302 if (remoteViews != null) {
8303 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
8304 customRemoteView);
8305 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08008306 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008307 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8308 customRemoteView);
8309 } else {
8310 return null;
8311 }
8312 }
8313
8314 /**
8315 * @hide
8316 */
8317 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008318 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08008319 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
8320 ? mBuilder.mN.headsUpContentView
8321 : mBuilder.mN.contentView;
8322 return makeBigContentViewWithCustomContent(customRemoteView);
8323 }
8324
Julia Reynolds7217dc92018-03-07 12:12:09 -05008325 /**
8326 * @hide
8327 */
8328 @Override
8329 public boolean areNotificationsVisiblyDifferent(Style other) {
8330 if (other == null || getClass() != other.getClass()) {
8331 return true;
8332 }
8333 // Comparison done for all custom RemoteViews, independent of style
8334 return false;
8335 }
8336
Selim Cinek03eb3b72016-02-18 10:39:45 -08008337 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
8338 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08008339 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008340 // Need to clone customContent before adding, because otherwise it can no longer be
8341 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008342 customContent = customContent.clone();
Selim Cinek87c31532017-08-18 18:53:44 -07008343 customContent.overrideTextColors(mBuilder.getPrimaryTextColor());
Selim Cinekf91017e2016-03-14 12:25:09 -07008344 remoteViews.removeAllViews(id);
8345 remoteViews.addView(id, customContent);
Selim Cinekfc8073c2017-08-16 17:50:20 -07008346 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08008347 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08008348 return remoteViews;
8349 }
8350 }
8351
Christoph Studer4600f9b2014-07-22 22:44:43 +02008352 // When adding a new Style subclass here, don't forget to update
8353 // Builder.getNotificationStyleClass.
8354
Griff Hazen61a9e862014-05-22 16:05:19 -07008355 /**
8356 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
8357 * metadata or change options on a notification builder.
8358 */
8359 public interface Extender {
8360 /**
8361 * Apply this extender to a notification builder.
8362 * @param builder the builder to be modified.
8363 * @return the build object for chaining.
8364 */
8365 public Builder extend(Builder builder);
8366 }
8367
8368 /**
8369 * Helper class to add wearable extensions to notifications.
8370 * <p class="note"> See
8371 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
8372 * for Android Wear</a> for more information on how to use this class.
8373 * <p>
8374 * To create a notification with wearable extensions:
8375 * <ol>
8376 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
8377 * properties.
8378 * <li>Create a {@link android.app.Notification.WearableExtender}.
8379 * <li>Set wearable-specific properties using the
8380 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
8381 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
8382 * notification.
8383 * <li>Post the notification to the notification system with the
8384 * {@code NotificationManager.notify(...)} methods.
8385 * </ol>
8386 *
8387 * <pre class="prettyprint">
8388 * Notification notif = new Notification.Builder(mContext)
8389 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
8390 * .setContentText(subject)
8391 * .setSmallIcon(R.drawable.new_mail)
8392 * .extend(new Notification.WearableExtender()
8393 * .setContentIcon(R.drawable.new_mail))
8394 * .build();
8395 * NotificationManager notificationManger =
8396 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
8397 * notificationManger.notify(0, notif);</pre>
8398 *
8399 * <p>Wearable extensions can be accessed on an existing notification by using the
8400 * {@code WearableExtender(Notification)} constructor,
8401 * and then using the {@code get} methods to access values.
8402 *
8403 * <pre class="prettyprint">
8404 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
8405 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07008406 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008407 */
8408 public static final class WearableExtender implements Extender {
8409 /**
8410 * Sentinel value for an action index that is unset.
8411 */
8412 public static final int UNSET_ACTION_INDEX = -1;
8413
8414 /**
8415 * Size value for use with {@link #setCustomSizePreset} to show this notification with
8416 * default sizing.
8417 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07008418 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07008419 * on their content.
Gus Prevasd7363752018-09-18 14:35:15 -04008420 *
8421 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008422 */
Gus Prevasd7363752018-09-18 14:35:15 -04008423 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008424 public static final int SIZE_DEFAULT = 0;
8425
8426 /**
8427 * Size value for use with {@link #setCustomSizePreset} to show this notification
8428 * with an extra small size.
8429 * <p>This value is only applicable for custom display notifications created using
8430 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008431 *
8432 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008433 */
Gus Prevasd7363752018-09-18 14:35:15 -04008434 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008435 public static final int SIZE_XSMALL = 1;
8436
8437 /**
8438 * Size value for use with {@link #setCustomSizePreset} to show this notification
8439 * with a small size.
8440 * <p>This value is only applicable for custom display notifications created using
8441 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008442 *
8443 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008444 */
Gus Prevasd7363752018-09-18 14:35:15 -04008445 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008446 public static final int SIZE_SMALL = 2;
8447
8448 /**
8449 * Size value for use with {@link #setCustomSizePreset} to show this notification
8450 * with a medium size.
8451 * <p>This value is only applicable for custom display notifications created using
8452 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008453 *
8454 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008455 */
Gus Prevasd7363752018-09-18 14:35:15 -04008456 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008457 public static final int SIZE_MEDIUM = 3;
8458
8459 /**
8460 * Size value for use with {@link #setCustomSizePreset} to show this notification
8461 * with a large size.
8462 * <p>This value is only applicable for custom display notifications created using
8463 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008464 *
8465 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008466 */
Gus Prevasd7363752018-09-18 14:35:15 -04008467 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008468 public static final int SIZE_LARGE = 4;
8469
Griff Hazend5f11f92014-05-27 15:40:09 -07008470 /**
8471 * Size value for use with {@link #setCustomSizePreset} to show this notification
8472 * full screen.
8473 * <p>This value is only applicable for custom display notifications created using
8474 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008475 *
8476 * @deprecated Display intents are no longer supported.
Griff Hazend5f11f92014-05-27 15:40:09 -07008477 */
Gus Prevasd7363752018-09-18 14:35:15 -04008478 @Deprecated
Griff Hazend5f11f92014-05-27 15:40:09 -07008479 public static final int SIZE_FULL_SCREEN = 5;
8480
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008481 /**
8482 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
8483 * short amount of time when this notification is displayed on the screen. This
8484 * is the default value.
Gus Prevasd7363752018-09-18 14:35:15 -04008485 *
8486 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008487 */
Gus Prevasd7363752018-09-18 14:35:15 -04008488 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008489 public static final int SCREEN_TIMEOUT_SHORT = 0;
8490
8491 /**
8492 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
8493 * for a longer amount of time when this notification is displayed on the screen.
Gus Prevasd7363752018-09-18 14:35:15 -04008494 *
8495 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008496 */
Gus Prevasd7363752018-09-18 14:35:15 -04008497 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008498 public static final int SCREEN_TIMEOUT_LONG = -1;
8499
Griff Hazen61a9e862014-05-22 16:05:19 -07008500 /** Notification extra which contains wearable extensions */
8501 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
8502
Pete Gastaf6781d2014-10-07 15:17:05 -04008503 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07008504 private static final String KEY_ACTIONS = "actions";
8505 private static final String KEY_FLAGS = "flags";
8506 private static final String KEY_DISPLAY_INTENT = "displayIntent";
8507 private static final String KEY_PAGES = "pages";
8508 private static final String KEY_BACKGROUND = "background";
8509 private static final String KEY_CONTENT_ICON = "contentIcon";
8510 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
8511 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
8512 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
8513 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
8514 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008515 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04008516 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008517 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07008518
8519 // Flags bitwise-ored to mFlags
8520 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
8521 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
8522 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
8523 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008524 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04008525 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04008526 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07008527
8528 // Default value for flags integer
8529 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
8530
8531 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
8532 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
8533
8534 private ArrayList<Action> mActions = new ArrayList<Action>();
8535 private int mFlags = DEFAULT_FLAGS;
8536 private PendingIntent mDisplayIntent;
8537 private ArrayList<Notification> mPages = new ArrayList<Notification>();
8538 private Bitmap mBackground;
8539 private int mContentIcon;
8540 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
8541 private int mContentActionIndex = UNSET_ACTION_INDEX;
8542 private int mCustomSizePreset = SIZE_DEFAULT;
8543 private int mCustomContentHeight;
8544 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008545 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04008546 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008547 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07008548
8549 /**
8550 * Create a {@link android.app.Notification.WearableExtender} with default
8551 * options.
8552 */
8553 public WearableExtender() {
8554 }
8555
8556 public WearableExtender(Notification notif) {
8557 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
8558 if (wearableBundle != null) {
8559 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
8560 if (actions != null) {
8561 mActions.addAll(actions);
8562 }
8563
8564 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
8565 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
8566
8567 Notification[] pages = getNotificationArrayFromBundle(
8568 wearableBundle, KEY_PAGES);
8569 if (pages != null) {
8570 Collections.addAll(mPages, pages);
8571 }
8572
8573 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
8574 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
8575 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
8576 DEFAULT_CONTENT_ICON_GRAVITY);
8577 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
8578 UNSET_ACTION_INDEX);
8579 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
8580 SIZE_DEFAULT);
8581 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
8582 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008583 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04008584 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008585 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07008586 }
8587 }
8588
8589 /**
8590 * Apply wearable extensions to a notification that is being built. This is typically
8591 * called by the {@link android.app.Notification.Builder#extend} method of
8592 * {@link android.app.Notification.Builder}.
8593 */
8594 @Override
8595 public Notification.Builder extend(Notification.Builder builder) {
8596 Bundle wearableBundle = new Bundle();
8597
8598 if (!mActions.isEmpty()) {
8599 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
8600 }
8601 if (mFlags != DEFAULT_FLAGS) {
8602 wearableBundle.putInt(KEY_FLAGS, mFlags);
8603 }
8604 if (mDisplayIntent != null) {
8605 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
8606 }
8607 if (!mPages.isEmpty()) {
8608 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
8609 new Notification[mPages.size()]));
8610 }
8611 if (mBackground != null) {
8612 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
8613 }
8614 if (mContentIcon != 0) {
8615 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
8616 }
8617 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
8618 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
8619 }
8620 if (mContentActionIndex != UNSET_ACTION_INDEX) {
8621 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
8622 mContentActionIndex);
8623 }
8624 if (mCustomSizePreset != SIZE_DEFAULT) {
8625 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
8626 }
8627 if (mCustomContentHeight != 0) {
8628 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
8629 }
8630 if (mGravity != DEFAULT_GRAVITY) {
8631 wearableBundle.putInt(KEY_GRAVITY, mGravity);
8632 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008633 if (mHintScreenTimeout != 0) {
8634 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
8635 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04008636 if (mDismissalId != null) {
8637 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
8638 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008639 if (mBridgeTag != null) {
8640 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
8641 }
Griff Hazen61a9e862014-05-22 16:05:19 -07008642
8643 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
8644 return builder;
8645 }
8646
8647 @Override
8648 public WearableExtender clone() {
8649 WearableExtender that = new WearableExtender();
8650 that.mActions = new ArrayList<Action>(this.mActions);
8651 that.mFlags = this.mFlags;
8652 that.mDisplayIntent = this.mDisplayIntent;
8653 that.mPages = new ArrayList<Notification>(this.mPages);
8654 that.mBackground = this.mBackground;
8655 that.mContentIcon = this.mContentIcon;
8656 that.mContentIconGravity = this.mContentIconGravity;
8657 that.mContentActionIndex = this.mContentActionIndex;
8658 that.mCustomSizePreset = this.mCustomSizePreset;
8659 that.mCustomContentHeight = this.mCustomContentHeight;
8660 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008661 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04008662 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008663 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07008664 return that;
8665 }
8666
8667 /**
8668 * Add a wearable action to this notification.
8669 *
8670 * <p>When wearable actions are added using this method, the set of actions that
8671 * show on a wearable device splits from devices that only show actions added
8672 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
8673 * of which actions display on different devices.
8674 *
8675 * @param action the action to add to this notification
8676 * @return this object for method chaining
8677 * @see android.app.Notification.Action
8678 */
8679 public WearableExtender addAction(Action action) {
8680 mActions.add(action);
8681 return this;
8682 }
8683
8684 /**
8685 * Adds wearable actions to this notification.
8686 *
8687 * <p>When wearable actions are added using this method, the set of actions that
8688 * show on a wearable device splits from devices that only show actions added
8689 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
8690 * of which actions display on different devices.
8691 *
8692 * @param actions the actions to add to this notification
8693 * @return this object for method chaining
8694 * @see android.app.Notification.Action
8695 */
8696 public WearableExtender addActions(List<Action> actions) {
8697 mActions.addAll(actions);
8698 return this;
8699 }
8700
8701 /**
8702 * Clear all wearable actions present on this builder.
8703 * @return this object for method chaining.
8704 * @see #addAction
8705 */
8706 public WearableExtender clearActions() {
8707 mActions.clear();
8708 return this;
8709 }
8710
8711 /**
8712 * Get the wearable actions present on this notification.
8713 */
8714 public List<Action> getActions() {
8715 return mActions;
8716 }
8717
8718 /**
8719 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07008720 * this notification. The {@link PendingIntent} provided should be for an activity.
8721 *
8722 * <pre class="prettyprint">
8723 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
8724 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
8725 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
8726 * Notification notif = new Notification.Builder(context)
8727 * .extend(new Notification.WearableExtender()
8728 * .setDisplayIntent(displayPendingIntent)
8729 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
8730 * .build();</pre>
8731 *
8732 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07008733 * should have an empty task affinity. It is also recommended to use the device
8734 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07008735 *
8736 * <p>Example AndroidManifest.xml entry:
8737 * <pre class="prettyprint">
8738 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
8739 * android:exported=&quot;true&quot;
8740 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07008741 * android:taskAffinity=&quot;&quot;
8742 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008743 *
8744 * @param intent the {@link PendingIntent} for an activity
8745 * @return this object for method chaining
8746 * @see android.app.Notification.WearableExtender#getDisplayIntent
Gus Prevasd7363752018-09-18 14:35:15 -04008747 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008748 */
Gus Prevasd7363752018-09-18 14:35:15 -04008749 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008750 public WearableExtender setDisplayIntent(PendingIntent intent) {
8751 mDisplayIntent = intent;
8752 return this;
8753 }
8754
8755 /**
8756 * Get the intent to launch inside of an activity view when displaying this
8757 * notification. This {@code PendingIntent} should be for an activity.
Gus Prevasd7363752018-09-18 14:35:15 -04008758 *
8759 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008760 */
Gus Prevasd7363752018-09-18 14:35:15 -04008761 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008762 public PendingIntent getDisplayIntent() {
8763 return mDisplayIntent;
8764 }
8765
8766 /**
8767 * Add an additional page of content to display with this notification. The current
8768 * notification forms the first page, and pages added using this function form
8769 * subsequent pages. This field can be used to separate a notification into multiple
8770 * sections.
8771 *
8772 * @param page the notification to add as another page
8773 * @return this object for method chaining
8774 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04008775 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008776 */
Gus Prevasd7363752018-09-18 14:35:15 -04008777 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008778 public WearableExtender addPage(Notification page) {
8779 mPages.add(page);
8780 return this;
8781 }
8782
8783 /**
8784 * Add additional pages of content to display with this notification. The current
8785 * notification forms the first page, and pages added using this function form
8786 * subsequent pages. This field can be used to separate a notification into multiple
8787 * sections.
8788 *
8789 * @param pages a list of notifications
8790 * @return this object for method chaining
8791 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04008792 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008793 */
Gus Prevasd7363752018-09-18 14:35:15 -04008794 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008795 public WearableExtender addPages(List<Notification> pages) {
8796 mPages.addAll(pages);
8797 return this;
8798 }
8799
8800 /**
8801 * Clear all additional pages present on this builder.
8802 * @return this object for method chaining.
8803 * @see #addPage
Gus Prevasd7363752018-09-18 14:35:15 -04008804 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008805 */
Gus Prevasd7363752018-09-18 14:35:15 -04008806 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008807 public WearableExtender clearPages() {
8808 mPages.clear();
8809 return this;
8810 }
8811
8812 /**
8813 * Get the array of additional pages of content for displaying this notification. The
8814 * current notification forms the first page, and elements within this array form
8815 * subsequent pages. This field can be used to separate a notification into multiple
8816 * sections.
8817 * @return the pages for this notification
Gus Prevasd7363752018-09-18 14:35:15 -04008818 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008819 */
Gus Prevasd7363752018-09-18 14:35:15 -04008820 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008821 public List<Notification> getPages() {
8822 return mPages;
8823 }
8824
8825 /**
8826 * Set a background image to be displayed behind the notification content.
8827 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8828 * will work with any notification style.
8829 *
8830 * @param background the background bitmap
8831 * @return this object for method chaining
8832 * @see android.app.Notification.WearableExtender#getBackground
Gus Prevasd7363752018-09-18 14:35:15 -04008833 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008834 */
Gus Prevasd7363752018-09-18 14:35:15 -04008835 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008836 public WearableExtender setBackground(Bitmap background) {
8837 mBackground = background;
8838 return this;
8839 }
8840
8841 /**
8842 * Get a background image to be displayed behind the notification content.
8843 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8844 * will work with any notification style.
8845 *
8846 * @return the background image
8847 * @see android.app.Notification.WearableExtender#setBackground
Gus Prevasd7363752018-09-18 14:35:15 -04008848 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008849 */
Gus Prevasd7363752018-09-18 14:35:15 -04008850 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008851 public Bitmap getBackground() {
8852 return mBackground;
8853 }
8854
8855 /**
8856 * Set an icon that goes with the content of this notification.
8857 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008858 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008859 public WearableExtender setContentIcon(int icon) {
8860 mContentIcon = icon;
8861 return this;
8862 }
8863
8864 /**
8865 * Get an icon that goes with the content of this notification.
8866 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008867 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008868 public int getContentIcon() {
8869 return mContentIcon;
8870 }
8871
8872 /**
8873 * Set the gravity that the content icon should have within the notification display.
8874 * Supported values include {@link android.view.Gravity#START} and
8875 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8876 * @see #setContentIcon
8877 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008878 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008879 public WearableExtender setContentIconGravity(int contentIconGravity) {
8880 mContentIconGravity = contentIconGravity;
8881 return this;
8882 }
8883
8884 /**
8885 * Get the gravity that the content icon should have within the notification display.
8886 * Supported values include {@link android.view.Gravity#START} and
8887 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8888 * @see #getContentIcon
8889 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008890 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008891 public int getContentIconGravity() {
8892 return mContentIconGravity;
8893 }
8894
8895 /**
Gus Prevasd7363752018-09-18 14:35:15 -04008896 * Set an action from this notification's actions as the primary action. If the action has a
8897 * {@link RemoteInput} associated with it, shortcuts to the options for that input are shown
8898 * directly on the notification.
Griff Hazen14f57992014-05-26 09:07:14 -07008899 *
Gus Prevasd7363752018-09-18 14:35:15 -04008900 * @param actionIndex The index of the primary action.
Griff Hazen14f57992014-05-26 09:07:14 -07008901 * If wearable actions were added to the main notification, this index
8902 * will apply to that list, otherwise it will apply to the regular
8903 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07008904 */
8905 public WearableExtender setContentAction(int actionIndex) {
8906 mContentActionIndex = actionIndex;
8907 return this;
8908 }
8909
8910 /**
Gus Prevasd7363752018-09-18 14:35:15 -04008911 * Get the index of the notification action, if any, that was specified as the primary
8912 * action.
Griff Hazen14f57992014-05-26 09:07:14 -07008913 *
8914 * <p>If wearable specific actions were added to the main notification, this index will
8915 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07008916 *
8917 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07008918 */
8919 public int getContentAction() {
8920 return mContentActionIndex;
8921 }
8922
8923 /**
8924 * Set the gravity that this notification should have within the available viewport space.
8925 * Supported values include {@link android.view.Gravity#TOP},
8926 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8927 * The default value is {@link android.view.Gravity#BOTTOM}.
8928 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008929 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008930 public WearableExtender setGravity(int gravity) {
8931 mGravity = gravity;
8932 return this;
8933 }
8934
8935 /**
8936 * Get the gravity that this notification should have within the available viewport space.
8937 * Supported values include {@link android.view.Gravity#TOP},
8938 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8939 * The default value is {@link android.view.Gravity#BOTTOM}.
8940 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008941 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008942 public int getGravity() {
8943 return mGravity;
8944 }
8945
8946 /**
8947 * Set the custom size preset for the display of this notification out of the available
8948 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
8949 * {@link #SIZE_LARGE}.
8950 * <p>Some custom size presets are only applicable for custom display notifications created
8951 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
8952 * documentation for the preset in question. See also
8953 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
8954 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008955 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008956 public WearableExtender setCustomSizePreset(int sizePreset) {
8957 mCustomSizePreset = sizePreset;
8958 return this;
8959 }
8960
8961 /**
8962 * Get the custom size preset for the display of this notification out of the available
8963 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
8964 * {@link #SIZE_LARGE}.
8965 * <p>Some custom size presets are only applicable for custom display notifications created
8966 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
8967 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
8968 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008969 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008970 public int getCustomSizePreset() {
8971 return mCustomSizePreset;
8972 }
8973
8974 /**
8975 * Set the custom height in pixels for the display of this notification's content.
8976 * <p>This option is only available for custom display notifications created
8977 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
8978 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
8979 * {@link #getCustomContentHeight}.
8980 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008981 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008982 public WearableExtender setCustomContentHeight(int height) {
8983 mCustomContentHeight = height;
8984 return this;
8985 }
8986
8987 /**
8988 * Get the custom height in pixels for the display of this notification's content.
8989 * <p>This option is only available for custom display notifications created
8990 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
8991 * {@link #setCustomContentHeight}.
8992 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008993 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008994 public int getCustomContentHeight() {
8995 return mCustomContentHeight;
8996 }
8997
8998 /**
8999 * Set whether the scrolling position for the contents of this notification should start
9000 * at the bottom of the contents instead of the top when the contents are too long to
9001 * display within the screen. Default is false (start scroll at the top).
9002 */
9003 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
9004 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
9005 return this;
9006 }
9007
9008 /**
9009 * Get whether the scrolling position for the contents of this notification should start
9010 * at the bottom of the contents instead of the top when the contents are too long to
9011 * display within the screen. Default is false (start scroll at the top).
9012 */
9013 public boolean getStartScrollBottom() {
9014 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
9015 }
9016
9017 /**
9018 * Set whether the content intent is available when the wearable device is not connected
9019 * to a companion device. The user can still trigger this intent when the wearable device
9020 * is offline, but a visual hint will indicate that the content intent may not be available.
9021 * Defaults to true.
9022 */
9023 public WearableExtender setContentIntentAvailableOffline(
9024 boolean contentIntentAvailableOffline) {
9025 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
9026 return this;
9027 }
9028
9029 /**
9030 * Get whether the content intent is available when the wearable device is not connected
9031 * to a companion device. The user can still trigger this intent when the wearable device
9032 * is offline, but a visual hint will indicate that the content intent may not be available.
9033 * Defaults to true.
9034 */
9035 public boolean getContentIntentAvailableOffline() {
9036 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
9037 }
9038
9039 /**
9040 * Set a hint that this notification's icon should not be displayed.
9041 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
9042 * @return this object for method chaining
9043 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009044 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009045 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
9046 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
9047 return this;
9048 }
9049
9050 /**
9051 * Get a hint that this notification's icon should not be displayed.
9052 * @return {@code true} if this icon should not be displayed, false otherwise.
9053 * The default value is {@code false} if this was never set.
9054 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009055 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009056 public boolean getHintHideIcon() {
9057 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
9058 }
9059
9060 /**
9061 * Set a visual hint that only the background image of this notification should be
9062 * displayed, and other semantic content should be hidden. This hint is only applicable
9063 * to sub-pages added using {@link #addPage}.
9064 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009065 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009066 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
9067 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
9068 return this;
9069 }
9070
9071 /**
9072 * Get a visual hint that only the background image of this notification should be
9073 * displayed, and other semantic content should be hidden. This hint is only applicable
9074 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
9075 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009076 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009077 public boolean getHintShowBackgroundOnly() {
9078 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
9079 }
9080
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009081 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009082 * Set a hint that this notification's background should not be clipped if possible,
9083 * and should instead be resized to fully display on the screen, retaining the aspect
9084 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009085 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
9086 * @return this object for method chaining
9087 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009088 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009089 public WearableExtender setHintAvoidBackgroundClipping(
9090 boolean hintAvoidBackgroundClipping) {
9091 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
9092 return this;
9093 }
9094
9095 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009096 * Get a hint that this notification's background should not be clipped if possible,
9097 * and should instead be resized to fully display on the screen, retaining the aspect
9098 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009099 * @return {@code true} if it's ok if the background is clipped on the screen, false
9100 * otherwise. The default value is {@code false} if this was never set.
9101 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009102 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009103 public boolean getHintAvoidBackgroundClipping() {
9104 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
9105 }
9106
9107 /**
9108 * Set a hint that the screen should remain on for at least this duration when
9109 * this notification is displayed on the screen.
9110 * @param timeout The requested screen timeout in milliseconds. Can also be either
9111 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9112 * @return this object for method chaining
9113 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009114 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009115 public WearableExtender setHintScreenTimeout(int timeout) {
9116 mHintScreenTimeout = timeout;
9117 return this;
9118 }
9119
9120 /**
9121 * Get the duration, in milliseconds, that the screen should remain on for
9122 * when this notification is displayed.
9123 * @return the duration in milliseconds if > 0, or either one of the sentinel values
9124 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9125 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009126 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009127 public int getHintScreenTimeout() {
9128 return mHintScreenTimeout;
9129 }
9130
Alex Hills9ab3a232016-04-05 14:54:56 -04009131 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04009132 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
9133 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9134 * qr codes, as well as other simple black-and-white tickets.
9135 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
9136 * @return this object for method chaining
Gus Prevasd7363752018-09-18 14:35:15 -04009137 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009138 */
Gus Prevasd7363752018-09-18 14:35:15 -04009139 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009140 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
9141 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
9142 return this;
9143 }
9144
9145 /**
9146 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
9147 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9148 * qr codes, as well as other simple black-and-white tickets.
9149 * @return {@code true} if it should be displayed in ambient, false otherwise
9150 * otherwise. The default value is {@code false} if this was never set.
Gus Prevasd7363752018-09-18 14:35:15 -04009151 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009152 */
Gus Prevasd7363752018-09-18 14:35:15 -04009153 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009154 public boolean getHintAmbientBigPicture() {
9155 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
9156 }
9157
9158 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04009159 * Set a hint that this notification's content intent will launch an {@link Activity}
9160 * directly, telling the platform that it can generate the appropriate transitions.
9161 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
9162 * an activity and transitions should be generated, false otherwise.
9163 * @return this object for method chaining
9164 */
9165 public WearableExtender setHintContentIntentLaunchesActivity(
9166 boolean hintContentIntentLaunchesActivity) {
9167 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
9168 return this;
9169 }
9170
9171 /**
9172 * Get a hint that this notification's content intent will launch an {@link Activity}
9173 * directly, telling the platform that it can generate the appropriate transitions
9174 * @return {@code true} if the content intent will launch an activity and transitions should
9175 * be generated, false otherwise. The default value is {@code false} if this was never set.
9176 */
9177 public boolean getHintContentIntentLaunchesActivity() {
9178 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
9179 }
9180
Nadia Benbernou948627e2016-04-14 14:41:08 -04009181 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009182 * Sets the dismissal id for this notification. If a notification is posted with a
9183 * dismissal id, then when that notification is canceled, notifications on other wearables
9184 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04009185 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009186 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04009187 * @param dismissalId the dismissal id of the notification.
9188 * @return this object for method chaining
9189 */
9190 public WearableExtender setDismissalId(String dismissalId) {
9191 mDismissalId = dismissalId;
9192 return this;
9193 }
9194
9195 /**
9196 * Returns the dismissal id of the notification.
9197 * @return the dismissal id of the notification or null if it has not been set.
9198 */
9199 public String getDismissalId() {
9200 return mDismissalId;
9201 }
9202
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009203 /**
9204 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
9205 * posted from a phone to provide finer-grained control on what notifications are bridged
9206 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
9207 * Features to Notifications</a> for more information.
9208 * @param bridgeTag the bridge tag of the notification.
9209 * @return this object for method chaining
9210 */
9211 public WearableExtender setBridgeTag(String bridgeTag) {
9212 mBridgeTag = bridgeTag;
9213 return this;
9214 }
9215
9216 /**
9217 * Returns the bridge tag of the notification.
9218 * @return the bridge tag or null if not present.
9219 */
9220 public String getBridgeTag() {
9221 return mBridgeTag;
9222 }
9223
Griff Hazen61a9e862014-05-22 16:05:19 -07009224 private void setFlag(int mask, boolean value) {
9225 if (value) {
9226 mFlags |= mask;
9227 } else {
9228 mFlags &= ~mask;
9229 }
9230 }
9231 }
9232
9233 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009234 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
9235 * with car extensions:
9236 *
9237 * <ol>
9238 * <li>Create an {@link Notification.Builder}, setting any desired
9239 * properties.
9240 * <li>Create a {@link CarExtender}.
9241 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
9242 * {@link CarExtender}.
9243 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9244 * to apply the extensions to a notification.
9245 * </ol>
9246 *
9247 * <pre class="prettyprint">
9248 * Notification notification = new Notification.Builder(context)
9249 * ...
9250 * .extend(new CarExtender()
9251 * .set*(...))
9252 * .build();
9253 * </pre>
9254 *
9255 * <p>Car extensions can be accessed on an existing notification by using the
9256 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
9257 * to access values.
9258 */
9259 public static final class CarExtender implements Extender {
9260 private static final String TAG = "CarExtender";
9261
9262 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
9263 private static final String EXTRA_LARGE_ICON = "large_icon";
9264 private static final String EXTRA_CONVERSATION = "car_conversation";
9265 private static final String EXTRA_COLOR = "app_color";
9266
9267 private Bitmap mLargeIcon;
9268 private UnreadConversation mUnreadConversation;
9269 private int mColor = Notification.COLOR_DEFAULT;
9270
9271 /**
9272 * Create a {@link CarExtender} with default options.
9273 */
9274 public CarExtender() {
9275 }
9276
9277 /**
9278 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
9279 *
9280 * @param notif The notification from which to copy options.
9281 */
9282 public CarExtender(Notification notif) {
9283 Bundle carBundle = notif.extras == null ?
9284 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
9285 if (carBundle != null) {
9286 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
9287 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
9288
9289 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
9290 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
9291 }
9292 }
9293
9294 /**
9295 * Apply car extensions to a notification that is being built. This is typically called by
9296 * the {@link Notification.Builder#extend(Notification.Extender)}
9297 * method of {@link Notification.Builder}.
9298 */
9299 @Override
9300 public Notification.Builder extend(Notification.Builder builder) {
9301 Bundle carExtensions = new Bundle();
9302
9303 if (mLargeIcon != null) {
9304 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
9305 }
9306 if (mColor != Notification.COLOR_DEFAULT) {
9307 carExtensions.putInt(EXTRA_COLOR, mColor);
9308 }
9309
9310 if (mUnreadConversation != null) {
9311 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
9312 carExtensions.putBundle(EXTRA_CONVERSATION, b);
9313 }
9314
9315 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
9316 return builder;
9317 }
9318
9319 /**
9320 * Sets the accent color to use when Android Auto presents the notification.
9321 *
9322 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
9323 * to accent the displayed notification. However, not all colors are acceptable in an
9324 * automotive setting. This method can be used to override the color provided in the
9325 * notification in such a situation.
9326 */
Tor Norbye80756e32015-03-02 09:39:27 -08009327 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009328 mColor = color;
9329 return this;
9330 }
9331
9332 /**
9333 * Gets the accent color.
9334 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04009335 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009336 */
Tor Norbye80756e32015-03-02 09:39:27 -08009337 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009338 public int getColor() {
9339 return mColor;
9340 }
9341
9342 /**
9343 * Sets the large icon of the car notification.
9344 *
9345 * If no large icon is set in the extender, Android Auto will display the icon
9346 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
9347 *
9348 * @param largeIcon The large icon to use in the car notification.
9349 * @return This object for method chaining.
9350 */
9351 public CarExtender setLargeIcon(Bitmap largeIcon) {
9352 mLargeIcon = largeIcon;
9353 return this;
9354 }
9355
9356 /**
9357 * Gets the large icon used in this car notification, or null if no icon has been set.
9358 *
9359 * @return The large icon for the car notification.
9360 * @see CarExtender#setLargeIcon
9361 */
9362 public Bitmap getLargeIcon() {
9363 return mLargeIcon;
9364 }
9365
9366 /**
9367 * Sets the unread conversation in a message notification.
9368 *
9369 * @param unreadConversation The unread part of the conversation this notification conveys.
9370 * @return This object for method chaining.
9371 */
9372 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
9373 mUnreadConversation = unreadConversation;
9374 return this;
9375 }
9376
9377 /**
9378 * Returns the unread conversation conveyed by this notification.
9379 * @see #setUnreadConversation(UnreadConversation)
9380 */
9381 public UnreadConversation getUnreadConversation() {
9382 return mUnreadConversation;
9383 }
9384
9385 /**
9386 * A class which holds the unread messages from a conversation.
9387 */
9388 public static class UnreadConversation {
9389 private static final String KEY_AUTHOR = "author";
9390 private static final String KEY_TEXT = "text";
9391 private static final String KEY_MESSAGES = "messages";
9392 private static final String KEY_REMOTE_INPUT = "remote_input";
9393 private static final String KEY_ON_REPLY = "on_reply";
9394 private static final String KEY_ON_READ = "on_read";
9395 private static final String KEY_PARTICIPANTS = "participants";
9396 private static final String KEY_TIMESTAMP = "timestamp";
9397
9398 private final String[] mMessages;
9399 private final RemoteInput mRemoteInput;
9400 private final PendingIntent mReplyPendingIntent;
9401 private final PendingIntent mReadPendingIntent;
9402 private final String[] mParticipants;
9403 private final long mLatestTimestamp;
9404
9405 UnreadConversation(String[] messages, RemoteInput remoteInput,
9406 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
9407 String[] participants, long latestTimestamp) {
9408 mMessages = messages;
9409 mRemoteInput = remoteInput;
9410 mReadPendingIntent = readPendingIntent;
9411 mReplyPendingIntent = replyPendingIntent;
9412 mParticipants = participants;
9413 mLatestTimestamp = latestTimestamp;
9414 }
9415
9416 /**
9417 * Gets the list of messages conveyed by this notification.
9418 */
9419 public String[] getMessages() {
9420 return mMessages;
9421 }
9422
9423 /**
9424 * Gets the remote input that will be used to convey the response to a message list, or
9425 * null if no such remote input exists.
9426 */
9427 public RemoteInput getRemoteInput() {
9428 return mRemoteInput;
9429 }
9430
9431 /**
9432 * Gets the pending intent that will be triggered when the user replies to this
9433 * notification.
9434 */
9435 public PendingIntent getReplyPendingIntent() {
9436 return mReplyPendingIntent;
9437 }
9438
9439 /**
9440 * Gets the pending intent that Android Auto will send after it reads aloud all messages
9441 * in this object's message list.
9442 */
9443 public PendingIntent getReadPendingIntent() {
9444 return mReadPendingIntent;
9445 }
9446
9447 /**
9448 * Gets the participants in the conversation.
9449 */
9450 public String[] getParticipants() {
9451 return mParticipants;
9452 }
9453
9454 /**
9455 * Gets the firs participant in the conversation.
9456 */
9457 public String getParticipant() {
9458 return mParticipants.length > 0 ? mParticipants[0] : null;
9459 }
9460
9461 /**
9462 * Gets the timestamp of the conversation.
9463 */
9464 public long getLatestTimestamp() {
9465 return mLatestTimestamp;
9466 }
9467
9468 Bundle getBundleForUnreadConversation() {
9469 Bundle b = new Bundle();
9470 String author = null;
9471 if (mParticipants != null && mParticipants.length > 1) {
9472 author = mParticipants[0];
9473 }
9474 Parcelable[] messages = new Parcelable[mMessages.length];
9475 for (int i = 0; i < messages.length; i++) {
9476 Bundle m = new Bundle();
9477 m.putString(KEY_TEXT, mMessages[i]);
9478 m.putString(KEY_AUTHOR, author);
9479 messages[i] = m;
9480 }
9481 b.putParcelableArray(KEY_MESSAGES, messages);
9482 if (mRemoteInput != null) {
9483 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
9484 }
9485 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
9486 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
9487 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
9488 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
9489 return b;
9490 }
9491
9492 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
9493 if (b == null) {
9494 return null;
9495 }
9496 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
9497 String[] messages = null;
9498 if (parcelableMessages != null) {
9499 String[] tmp = new String[parcelableMessages.length];
9500 boolean success = true;
9501 for (int i = 0; i < tmp.length; i++) {
9502 if (!(parcelableMessages[i] instanceof Bundle)) {
9503 success = false;
9504 break;
9505 }
9506 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
9507 if (tmp[i] == null) {
9508 success = false;
9509 break;
9510 }
9511 }
9512 if (success) {
9513 messages = tmp;
9514 } else {
9515 return null;
9516 }
9517 }
9518
9519 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
9520 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
9521
9522 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
9523
9524 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
9525 if (participants == null || participants.length != 1) {
9526 return null;
9527 }
9528
9529 return new UnreadConversation(messages,
9530 remoteInput,
9531 onReply,
9532 onRead,
9533 participants, b.getLong(KEY_TIMESTAMP));
9534 }
9535 };
9536
9537 /**
9538 * Builder class for {@link CarExtender.UnreadConversation} objects.
9539 */
9540 public static class Builder {
9541 private final List<String> mMessages = new ArrayList<String>();
9542 private final String mParticipant;
9543 private RemoteInput mRemoteInput;
9544 private PendingIntent mReadPendingIntent;
9545 private PendingIntent mReplyPendingIntent;
9546 private long mLatestTimestamp;
9547
9548 /**
9549 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
9550 *
9551 * @param name The name of the other participant in the conversation.
9552 */
9553 public Builder(String name) {
9554 mParticipant = name;
9555 }
9556
9557 /**
9558 * Appends a new unread message to the list of messages for this conversation.
9559 *
9560 * The messages should be added from oldest to newest.
9561 *
9562 * @param message The text of the new unread message.
9563 * @return This object for method chaining.
9564 */
9565 public Builder addMessage(String message) {
9566 mMessages.add(message);
9567 return this;
9568 }
9569
9570 /**
9571 * Sets the pending intent and remote input which will convey the reply to this
9572 * notification.
9573 *
9574 * @param pendingIntent The pending intent which will be triggered on a reply.
9575 * @param remoteInput The remote input parcelable which will carry the reply.
9576 * @return This object for method chaining.
9577 *
9578 * @see CarExtender.UnreadConversation#getRemoteInput
9579 * @see CarExtender.UnreadConversation#getReplyPendingIntent
9580 */
9581 public Builder setReplyAction(
9582 PendingIntent pendingIntent, RemoteInput remoteInput) {
9583 mRemoteInput = remoteInput;
9584 mReplyPendingIntent = pendingIntent;
9585
9586 return this;
9587 }
9588
9589 /**
9590 * Sets the pending intent that will be sent once the messages in this notification
9591 * are read.
9592 *
9593 * @param pendingIntent The pending intent to use.
9594 * @return This object for method chaining.
9595 */
9596 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
9597 mReadPendingIntent = pendingIntent;
9598 return this;
9599 }
9600
9601 /**
9602 * Sets the timestamp of the most recent message in an unread conversation.
9603 *
9604 * If a messaging notification has been posted by your application and has not
9605 * yet been cancelled, posting a later notification with the same id and tag
9606 * but without a newer timestamp may result in Android Auto not displaying a
9607 * heads up notification for the later notification.
9608 *
9609 * @param timestamp The timestamp of the most recent message in the conversation.
9610 * @return This object for method chaining.
9611 */
9612 public Builder setLatestTimestamp(long timestamp) {
9613 mLatestTimestamp = timestamp;
9614 return this;
9615 }
9616
9617 /**
9618 * Builds a new unread conversation object.
9619 *
9620 * @return The new unread conversation object.
9621 */
9622 public UnreadConversation build() {
9623 String[] messages = mMessages.toArray(new String[mMessages.size()]);
9624 String[] participants = { mParticipant };
9625 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
9626 mReadPendingIntent, participants, mLatestTimestamp);
9627 }
9628 }
9629 }
9630
9631 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009632 * <p>Helper class to add Android TV extensions to notifications. To create a notification
9633 * with a TV extension:
9634 *
9635 * <ol>
9636 * <li>Create an {@link Notification.Builder}, setting any desired properties.
9637 * <li>Create a {@link TvExtender}.
9638 * <li>Set TV-specific properties using the {@code set} methods of
9639 * {@link TvExtender}.
9640 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9641 * to apply the extension to a notification.
9642 * </ol>
9643 *
9644 * <pre class="prettyprint">
9645 * Notification notification = new Notification.Builder(context)
9646 * ...
9647 * .extend(new TvExtender()
9648 * .set*(...))
9649 * .build();
9650 * </pre>
9651 *
9652 * <p>TV extensions can be accessed on an existing notification by using the
9653 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
9654 * to access values.
9655 *
9656 * @hide
9657 */
9658 @SystemApi
9659 public static final class TvExtender implements Extender {
9660 private static final String TAG = "TvExtender";
9661
9662 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
9663 private static final String EXTRA_FLAGS = "flags";
9664 private static final String EXTRA_CONTENT_INTENT = "content_intent";
9665 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009666 private static final String EXTRA_CHANNEL_ID = "channel_id";
Rhiannon Malia1a083932018-01-24 15:02:30 -08009667 private static final String EXTRA_SUPPRESS_SHOW_OVER_APPS = "suppressShowOverApps";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009668
9669 // Flags bitwise-ored to mFlags
9670 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
9671
9672 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009673 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009674 private PendingIntent mContentIntent;
9675 private PendingIntent mDeleteIntent;
Rhiannon Malia1a083932018-01-24 15:02:30 -08009676 private boolean mSuppressShowOverApps;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009677
9678 /**
9679 * Create a {@link TvExtender} with default options.
9680 */
9681 public TvExtender() {
9682 mFlags = FLAG_AVAILABLE_ON_TV;
9683 }
9684
9685 /**
9686 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
9687 *
9688 * @param notif The notification from which to copy options.
9689 */
9690 public TvExtender(Notification notif) {
9691 Bundle bundle = notif.extras == null ?
9692 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
9693 if (bundle != null) {
9694 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009695 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Rhiannon Malia1a083932018-01-24 15:02:30 -08009696 mSuppressShowOverApps = bundle.getBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009697 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
9698 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
9699 }
9700 }
9701
9702 /**
9703 * Apply a TV extension to a notification that is being built. This is typically called by
9704 * the {@link Notification.Builder#extend(Notification.Extender)}
9705 * method of {@link Notification.Builder}.
9706 */
9707 @Override
9708 public Notification.Builder extend(Notification.Builder builder) {
9709 Bundle bundle = new Bundle();
9710
9711 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009712 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Rhiannon Malia1a083932018-01-24 15:02:30 -08009713 bundle.putBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS, mSuppressShowOverApps);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009714 if (mContentIntent != null) {
9715 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
9716 }
9717
9718 if (mDeleteIntent != null) {
9719 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
9720 }
9721
9722 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
9723 return builder;
9724 }
9725
9726 /**
9727 * Returns true if this notification should be shown on TV. This method return true
9728 * if the notification was extended with a TvExtender.
9729 */
9730 public boolean isAvailableOnTv() {
9731 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
9732 }
9733
9734 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009735 * Specifies the channel the notification should be delivered on when shown on TV.
9736 * It can be different from the channel that the notification is delivered to when
9737 * posting on a non-TV device.
9738 */
9739 public TvExtender setChannel(String channelId) {
9740 mChannelId = channelId;
9741 return this;
9742 }
9743
9744 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04009745 * Specifies the channel the notification should be delivered on when shown on TV.
9746 * It can be different from the channel that the notification is delivered to when
9747 * posting on a non-TV device.
9748 */
9749 public TvExtender setChannelId(String channelId) {
9750 mChannelId = channelId;
9751 return this;
9752 }
9753
Jeff Sharkey000ce802017-04-29 13:13:27 -06009754 /** @removed */
9755 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009756 public String getChannel() {
9757 return mChannelId;
9758 }
9759
9760 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04009761 * Returns the id of the channel this notification posts to on TV.
9762 */
9763 public String getChannelId() {
9764 return mChannelId;
9765 }
9766
9767 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009768 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
9769 * If provided, it is used instead of the content intent specified
9770 * at the level of Notification.
9771 */
9772 public TvExtender setContentIntent(PendingIntent intent) {
9773 mContentIntent = intent;
9774 return this;
9775 }
9776
9777 /**
9778 * Returns the TV-specific content intent. If this method returns null, the
9779 * main content intent on the notification should be used.
9780 *
9781 * @see {@link Notification#contentIntent}
9782 */
9783 public PendingIntent getContentIntent() {
9784 return mContentIntent;
9785 }
9786
9787 /**
9788 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
9789 * by the user on TV. If provided, it is used instead of the delete intent specified
9790 * at the level of Notification.
9791 */
9792 public TvExtender setDeleteIntent(PendingIntent intent) {
9793 mDeleteIntent = intent;
9794 return this;
9795 }
9796
9797 /**
9798 * Returns the TV-specific delete intent. If this method returns null, the
9799 * main delete intent on the notification should be used.
9800 *
9801 * @see {@link Notification#deleteIntent}
9802 */
9803 public PendingIntent getDeleteIntent() {
9804 return mDeleteIntent;
9805 }
Rhiannon Malia1a083932018-01-24 15:02:30 -08009806
9807 /**
9808 * Specifies whether this notification should suppress showing a message over top of apps
9809 * outside of the launcher.
9810 */
9811 public TvExtender setSuppressShowOverApps(boolean suppress) {
9812 mSuppressShowOverApps = suppress;
9813 return this;
9814 }
9815
9816 /**
9817 * Returns true if this notification should not show messages over top of apps
9818 * outside of the launcher.
9819 */
9820 public boolean getSuppressShowOverApps() {
9821 return mSuppressShowOverApps;
9822 }
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009823 }
9824
9825 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07009826 * Get an array of Notification objects from a parcelable array bundle field.
9827 * Update the bundle to have a typed array so fetches in the future don't need
9828 * to do an array copy.
9829 */
9830 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
9831 Parcelable[] array = bundle.getParcelableArray(key);
9832 if (array instanceof Notification[] || array == null) {
9833 return (Notification[]) array;
9834 }
9835 Notification[] typedArray = Arrays.copyOf(array, array.length,
9836 Notification[].class);
9837 bundle.putParcelableArray(key, typedArray);
9838 return typedArray;
9839 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02009840
9841 private static class BuilderRemoteViews extends RemoteViews {
9842 public BuilderRemoteViews(Parcel parcel) {
9843 super(parcel);
9844 }
9845
Kenny Guy77320062014-08-27 21:37:15 +01009846 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
9847 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02009848 }
9849
9850 @Override
9851 public BuilderRemoteViews clone() {
9852 Parcel p = Parcel.obtain();
9853 writeToParcel(p, 0);
9854 p.setDataPosition(0);
9855 BuilderRemoteViews brv = new BuilderRemoteViews(p);
9856 p.recycle();
9857 return brv;
9858 }
9859 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08009860
Selim Cinek384804b2018-04-18 14:31:07 +08009861 /**
9862 * A result object where information about the template that was created is saved.
9863 */
9864 private static class TemplateBindResult {
Selim Cinek1c72fa02018-04-23 18:00:54 +08009865 int mIconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +08009866
9867 /**
Selim Cinek1c72fa02018-04-23 18:00:54 +08009868 * Get the margin end that needs to be added to any fields that may overlap
Selim Cinek384804b2018-04-18 14:31:07 +08009869 * with the right actions.
9870 */
Selim Cinek1c72fa02018-04-23 18:00:54 +08009871 public int getIconMarginEnd() {
9872 return mIconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +08009873 }
9874
Selim Cinek1c72fa02018-04-23 18:00:54 +08009875 public void setIconMarginEnd(int iconMarginEnd) {
9876 this.mIconMarginEnd = iconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +08009877 }
9878 }
9879
Adrian Roos70d7aa32017-01-11 15:39:06 -08009880 private static class StandardTemplateParams {
9881 boolean hasProgress = true;
9882 boolean ambient = false;
9883 CharSequence title;
9884 CharSequence text;
Selim Cinekafeed292017-12-12 17:32:44 -08009885 CharSequence headerTextSecondary;
Selim Cinekbee4e072018-05-21 22:06:43 -07009886 int maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek88188f22017-09-19 16:46:56 -07009887 boolean hideLargeIcon;
Selim Cinek384804b2018-04-18 14:31:07 +08009888 boolean hideReplyIcon;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009889
9890 final StandardTemplateParams reset() {
9891 hasProgress = true;
9892 ambient = false;
9893 title = null;
9894 text = null;
Selim Cinekafeed292017-12-12 17:32:44 -08009895 headerTextSecondary = null;
Selim Cinekbee4e072018-05-21 22:06:43 -07009896 maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009897 return this;
9898 }
9899
9900 final StandardTemplateParams hasProgress(boolean hasProgress) {
9901 this.hasProgress = hasProgress;
9902 return this;
9903 }
9904
9905 final StandardTemplateParams title(CharSequence title) {
9906 this.title = title;
9907 return this;
9908 }
9909
9910 final StandardTemplateParams text(CharSequence text) {
9911 this.text = text;
9912 return this;
9913 }
9914
Selim Cinekafeed292017-12-12 17:32:44 -08009915 final StandardTemplateParams headerTextSecondary(CharSequence text) {
9916 this.headerTextSecondary = text;
9917 return this;
9918 }
9919
Selim Cinek384804b2018-04-18 14:31:07 +08009920 final StandardTemplateParams hideLargeIcon(boolean hideLargeIcon) {
9921 this.hideLargeIcon = hideLargeIcon;
Selim Cinek88188f22017-09-19 16:46:56 -07009922 return this;
9923 }
9924
Selim Cinek384804b2018-04-18 14:31:07 +08009925 final StandardTemplateParams hideReplyIcon(boolean hideReplyIcon) {
9926 this.hideReplyIcon = hideReplyIcon;
Selim Cinek88188f22017-09-19 16:46:56 -07009927 return this;
9928 }
9929
Adrian Roos70d7aa32017-01-11 15:39:06 -08009930 final StandardTemplateParams ambient(boolean ambient) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08009931 Preconditions.checkState(title == null && text == null, "must set ambient before text");
Adrian Roos70d7aa32017-01-11 15:39:06 -08009932 this.ambient = ambient;
9933 return this;
9934 }
9935
9936 final StandardTemplateParams fillTextsFrom(Builder b) {
9937 Bundle extras = b.mN.extras;
Lucas Dupin06c5e642017-09-13 16:34:58 -07009938 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE), ambient);
9939
9940 // Big text notifications should contain their content when viewed in ambient mode.
9941 CharSequence text = extras.getCharSequence(EXTRA_BIG_TEXT);
9942 if (!ambient || TextUtils.isEmpty(text)) {
9943 text = extras.getCharSequence(EXTRA_TEXT);
9944 }
9945 this.text = b.processLegacyText(text, ambient);
Adrian Roos70d7aa32017-01-11 15:39:06 -08009946 return this;
9947 }
Selim Cinekbee4e072018-05-21 22:06:43 -07009948
9949 /**
9950 * Set the maximum lines of remote input history lines allowed.
9951 * @param maxRemoteInputHistory The number of lines.
9952 * @return The builder for method chaining.
9953 */
9954 public StandardTemplateParams setMaxRemoteInputHistory(int maxRemoteInputHistory) {
9955 this.maxRemoteInputHistory = maxRemoteInputHistory;
9956 return this;
9957 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08009958 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009959}