blob: b657a916604b180defcdaa8743eefca33f49e63f [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
Griff Hazen959591e2014-05-15 22:26:18 -07001430 private final Bundle mExtras;
Mathew Inwood8c854f82018-09-14 12:35:36 +01001431 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Dan Sandler86647982015-05-13 23:41:13 -04001432 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001433 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001434 private boolean mAllowGeneratedReplies = true;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001435 private final @SemanticAction int mSemanticAction;
Gustav Sennton005d7a02019-01-04 13:41:32 +00001436 private final boolean mIsContextual;
Griff Hazen959591e2014-05-15 22:26:18 -07001437
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001438 /**
1439 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001440 *
1441 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001442 */
Dan Sandler86647982015-05-13 23:41:13 -04001443 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001444 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001445
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001446 /**
1447 * Title of the action.
1448 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001449 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001450
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001451 /**
1452 * Intent to send when the user invokes this action. May be null, in which case the action
1453 * may be rendered in a disabled presentation by the system UI.
1454 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001455 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001456
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001457 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001458 if (in.readInt() != 0) {
1459 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001460 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1461 icon = mIcon.getResId();
1462 }
Dan Sandler86647982015-05-13 23:41:13 -04001463 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001464 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1465 if (in.readInt() == 1) {
1466 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1467 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001468 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001469 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001470 mAllowGeneratedReplies = in.readInt() == 1;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001471 mSemanticAction = in.readInt();
Gustav Sennton005d7a02019-01-04 13:41:32 +00001472 mIsContextual = in.readInt() == 1;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001473 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001474
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001475 /**
Dan Sandler86647982015-05-13 23:41:13 -04001476 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001477 */
Dan Sandler86647982015-05-13 23:41:13 -04001478 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001479 public Action(int icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001480 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true,
Gustav Sennton005d7a02019-01-04 13:41:32 +00001481 SEMANTIC_ACTION_NONE, false /* isContextual */);
Griff Hazen959591e2014-05-15 22:26:18 -07001482 }
1483
Adrian Roos7af53622016-10-12 13:44:05 -07001484 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001485 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001486 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
Gustav Sennton005d7a02019-01-04 13:41:32 +00001487 @SemanticAction int semanticAction, boolean isContextual) {
Dan Sandler86647982015-05-13 23:41:13 -04001488 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001489 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1490 this.icon = icon.getResId();
1491 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001492 this.title = title;
1493 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001494 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001495 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001496 this.mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001497 this.mSemanticAction = semanticAction;
Gustav Sennton005d7a02019-01-04 13:41:32 +00001498 this.mIsContextual = isContextual;
Griff Hazen959591e2014-05-15 22:26:18 -07001499 }
1500
1501 /**
Dan Sandler86647982015-05-13 23:41:13 -04001502 * Return an icon representing the action.
1503 */
1504 public Icon getIcon() {
1505 if (mIcon == null && icon != 0) {
1506 // you snuck an icon in here without using the builder; let's try to keep it
1507 mIcon = Icon.createWithResource("", icon);
1508 }
1509 return mIcon;
1510 }
1511
1512 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001513 * Get additional metadata carried around with this Action.
1514 */
1515 public Bundle getExtras() {
1516 return mExtras;
1517 }
1518
1519 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001520 * Return whether the platform should automatically generate possible replies for this
1521 * {@link Action}
1522 */
1523 public boolean getAllowGeneratedReplies() {
1524 return mAllowGeneratedReplies;
1525 }
1526
1527 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001528 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001529 * May return null if no remote inputs were added. Only returns inputs which accept
1530 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001531 */
1532 public RemoteInput[] getRemoteInputs() {
1533 return mRemoteInputs;
1534 }
1535
1536 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001537 * Returns the {@code SemanticAction} associated with this {@link Action}. A
1538 * {@code SemanticAction} denotes what an {@link Action}'s {@link PendingIntent} will do
1539 * (eg. reply, mark as read, delete, etc).
1540 */
1541 public @SemanticAction int getSemanticAction() {
1542 return mSemanticAction;
1543 }
1544
1545 /**
Gustav Sennton005d7a02019-01-04 13:41:32 +00001546 * Returns whether this is a contextual Action, i.e. whether the action is dependent on the
1547 * notification message body. An example of a contextual action could be an action opening a
1548 * map application with an address shown in the notification.
1549 */
1550 public boolean isContextual() {
1551 return mIsContextual;
1552 }
1553
1554 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001555 * Get the list of inputs to be collected from the user that ONLY accept data when this
1556 * action is sent. These remote inputs are guaranteed to return true on a call to
1557 * {@link RemoteInput#isDataOnly}.
1558 *
Shane Brennanf670d6c2017-04-25 13:05:45 -07001559 * Returns null if there are no data-only remote inputs.
Shane Brennan472a3b32016-12-12 15:28:10 -08001560 *
1561 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1562 * of non-textual RemoteInputs do not access these remote inputs.
1563 */
1564 public RemoteInput[] getDataOnlyRemoteInputs() {
1565 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1566 }
1567
1568 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001569 * Builder class for {@link Action} objects.
1570 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001571 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001572 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001573 private final CharSequence mTitle;
1574 private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001575 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001576 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001577 private ArrayList<RemoteInput> mRemoteInputs;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001578 private @SemanticAction int mSemanticAction;
Gustav Sennton005d7a02019-01-04 13:41:32 +00001579 private boolean mIsContextual;
Griff Hazen959591e2014-05-15 22:26:18 -07001580
1581 /**
1582 * Construct a new builder for {@link Action} object.
1583 * @param icon icon to show for this action
1584 * @param title the title of the action
1585 * @param intent the {@link PendingIntent} to fire when users trigger this action
1586 */
Dan Sandler86647982015-05-13 23:41:13 -04001587 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001588 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001589 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001590 }
1591
1592 /**
1593 * Construct a new builder for {@link Action} object.
1594 * @param icon icon to show for this action
1595 * @param title the title of the action
1596 * @param intent the {@link PendingIntent} to fire when users trigger this action
1597 */
1598 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001599 this(icon, title, intent, new Bundle(), null, true, SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001600 }
1601
1602 /**
1603 * Construct a new builder for {@link Action} object using the fields from an
1604 * {@link Action}.
1605 * @param action the action to read fields from.
1606 */
1607 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001608 this(action.getIcon(), action.title, action.actionIntent,
1609 new Bundle(action.mExtras), action.getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001610 action.getAllowGeneratedReplies(), action.getSemanticAction());
Griff Hazen959591e2014-05-15 22:26:18 -07001611 }
1612
Dan Sandler86647982015-05-13 23:41:13 -04001613 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001614 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1615 @SemanticAction int semanticAction) {
Griff Hazen959591e2014-05-15 22:26:18 -07001616 mIcon = icon;
1617 mTitle = title;
1618 mIntent = intent;
1619 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001620 if (remoteInputs != null) {
1621 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1622 Collections.addAll(mRemoteInputs, remoteInputs);
1623 }
Adrian Roos7af53622016-10-12 13:44:05 -07001624 mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001625 mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001626 }
1627
1628 /**
1629 * Merge additional metadata into this builder.
1630 *
1631 * <p>Values within the Bundle will replace existing extras values in this Builder.
1632 *
1633 * @see Notification.Action#extras
1634 */
1635 public Builder addExtras(Bundle extras) {
1636 if (extras != null) {
1637 mExtras.putAll(extras);
1638 }
1639 return this;
1640 }
1641
1642 /**
1643 * Get the metadata Bundle used by this Builder.
1644 *
1645 * <p>The returned Bundle is shared with this Builder.
1646 */
1647 public Bundle getExtras() {
1648 return mExtras;
1649 }
1650
1651 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001652 * Add an input to be collected from the user when this action is sent.
1653 * Response values can be retrieved from the fired intent by using the
1654 * {@link RemoteInput#getResultsFromIntent} function.
1655 * @param remoteInput a {@link RemoteInput} to add to the action
1656 * @return this object for method chaining
1657 */
1658 public Builder addRemoteInput(RemoteInput remoteInput) {
1659 if (mRemoteInputs == null) {
1660 mRemoteInputs = new ArrayList<RemoteInput>();
1661 }
1662 mRemoteInputs.add(remoteInput);
1663 return this;
1664 }
1665
1666 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001667 * Set whether the platform should automatically generate possible replies to add to
1668 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1669 * {@link RemoteInput}, this has no effect.
1670 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1671 * otherwise
1672 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001673 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001674 */
1675 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1676 mAllowGeneratedReplies = allowGeneratedReplies;
1677 return this;
1678 }
1679
1680 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001681 * Sets the {@code SemanticAction} for this {@link Action}. A
1682 * {@code SemanticAction} denotes what an {@link Action}'s
1683 * {@link PendingIntent} will do (eg. reply, mark as read, delete, etc).
1684 * @param semanticAction a SemanticAction defined within {@link Action} with
1685 * {@code SEMANTIC_ACTION_} prefixes
1686 * @return this object for method chaining
1687 */
1688 public Builder setSemanticAction(@SemanticAction int semanticAction) {
1689 mSemanticAction = semanticAction;
1690 return this;
1691 }
1692
1693 /**
Gustav Sennton005d7a02019-01-04 13:41:32 +00001694 * Sets whether this {@link Action} is a contextual action, i.e. whether the action is
1695 * dependent on the notification message body. An example of a contextual action could
1696 * be an action opening a map application with an address shown in the notification.
1697 */
1698 public Builder setContextual(boolean isContextual) {
1699 mIsContextual = isContextual;
1700 return this;
1701 }
1702
1703 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001704 * Apply an extender to this action builder. Extenders may be used to add
1705 * metadata or change options on this builder.
1706 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001707 public Builder extend(Extender extender) {
1708 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001709 return this;
1710 }
1711
1712 /**
Gustav Senntonc98b1632018-11-23 12:26:15 +00001713 * Throws an NPE if we are building a contextual action missing one of the fields
1714 * necessary to display the action.
1715 */
1716 private void checkContextualActionNullFields() {
Gustav Sennton005d7a02019-01-04 13:41:32 +00001717 if (!mIsContextual) return;
Gustav Senntonc98b1632018-11-23 12:26:15 +00001718
1719 if (mIcon == null) {
1720 throw new NullPointerException("Contextual Actions must contain a valid icon");
1721 }
1722
1723 if (mIntent == null) {
1724 throw new NullPointerException(
1725 "Contextual Actions must contain a valid PendingIntent");
1726 }
1727 }
1728
1729 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001730 * Combine all of the options that have been set and return a new {@link Action}
1731 * object.
1732 * @return the built action
1733 */
1734 public Action build() {
Gustav Senntonc98b1632018-11-23 12:26:15 +00001735 checkContextualActionNullFields();
1736
Shane Brennan472a3b32016-12-12 15:28:10 -08001737 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1738 RemoteInput[] previousDataInputs =
1739 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001740 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001741 for (RemoteInput input : previousDataInputs) {
1742 dataOnlyInputs.add(input);
1743 }
1744 }
1745 List<RemoteInput> textInputs = new ArrayList<>();
1746 if (mRemoteInputs != null) {
1747 for (RemoteInput input : mRemoteInputs) {
1748 if (input.isDataOnly()) {
1749 dataOnlyInputs.add(input);
1750 } else {
1751 textInputs.add(input);
1752 }
1753 }
1754 }
1755 if (!dataOnlyInputs.isEmpty()) {
1756 RemoteInput[] dataInputsArr =
1757 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1758 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1759 }
1760 RemoteInput[] textInputsArr = textInputs.isEmpty()
1761 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1762 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Gustav Sennton005d7a02019-01-04 13:41:32 +00001763 mAllowGeneratedReplies, mSemanticAction, mIsContextual);
Griff Hazen959591e2014-05-15 22:26:18 -07001764 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001765 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001766
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001767 @Override
1768 public Action clone() {
1769 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001770 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001771 title,
1772 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001773 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001774 getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001775 getAllowGeneratedReplies(),
Gustav Sennton005d7a02019-01-04 13:41:32 +00001776 getSemanticAction(),
1777 isContextual());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001778 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001779
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001780 @Override
1781 public int describeContents() {
1782 return 0;
1783 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001784
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001785 @Override
1786 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001787 final Icon ic = getIcon();
1788 if (ic != null) {
1789 out.writeInt(1);
1790 ic.writeToParcel(out, 0);
1791 } else {
1792 out.writeInt(0);
1793 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001794 TextUtils.writeToParcel(title, out, flags);
1795 if (actionIntent != null) {
1796 out.writeInt(1);
1797 actionIntent.writeToParcel(out, flags);
1798 } else {
1799 out.writeInt(0);
1800 }
Griff Hazen959591e2014-05-15 22:26:18 -07001801 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001802 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001803 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001804 out.writeInt(mSemanticAction);
Gustav Sennton005d7a02019-01-04 13:41:32 +00001805 out.writeInt(mIsContextual ? 1 : 0);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001806 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001807
Griff Hazen959591e2014-05-15 22:26:18 -07001808 public static final Parcelable.Creator<Action> CREATOR =
1809 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001810 public Action createFromParcel(Parcel in) {
1811 return new Action(in);
1812 }
1813 public Action[] newArray(int size) {
1814 return new Action[size];
1815 }
1816 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001817
1818 /**
1819 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1820 * metadata or change options on an action builder.
1821 */
1822 public interface Extender {
1823 /**
1824 * Apply this extender to a notification action builder.
1825 * @param builder the builder to be modified.
1826 * @return the build object for chaining.
1827 */
1828 public Builder extend(Builder builder);
1829 }
1830
1831 /**
1832 * Wearable extender for notification actions. To add extensions to an action,
1833 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1834 * the {@code WearableExtender()} constructor and apply it to a
1835 * {@link android.app.Notification.Action.Builder} using
1836 * {@link android.app.Notification.Action.Builder#extend}.
1837 *
1838 * <pre class="prettyprint">
1839 * Notification.Action action = new Notification.Action.Builder(
1840 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001841 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001842 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001843 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001844 */
1845 public static final class WearableExtender implements Extender {
1846 /** Notification action extra which contains wearable extensions */
1847 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1848
Pete Gastaf6781d2014-10-07 15:17:05 -04001849 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001850 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001851 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1852 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1853 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001854
1855 // Flags bitwise-ored to mFlags
1856 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001857 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001858 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001859
1860 // Default value for flags integer
1861 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1862
1863 private int mFlags = DEFAULT_FLAGS;
1864
Pete Gastaf6781d2014-10-07 15:17:05 -04001865 private CharSequence mInProgressLabel;
1866 private CharSequence mConfirmLabel;
1867 private CharSequence mCancelLabel;
1868
Griff Hazen61a9e862014-05-22 16:05:19 -07001869 /**
1870 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1871 * options.
1872 */
1873 public WearableExtender() {
1874 }
1875
1876 /**
1877 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1878 * wearable options present in an existing notification action.
1879 * @param action the notification action to inspect.
1880 */
1881 public WearableExtender(Action action) {
1882 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1883 if (wearableBundle != null) {
1884 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001885 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1886 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1887 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001888 }
1889 }
1890
1891 /**
1892 * Apply wearable extensions to a notification action that is being built. This is
1893 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1894 * method of {@link android.app.Notification.Action.Builder}.
1895 */
1896 @Override
1897 public Action.Builder extend(Action.Builder builder) {
1898 Bundle wearableBundle = new Bundle();
1899
1900 if (mFlags != DEFAULT_FLAGS) {
1901 wearableBundle.putInt(KEY_FLAGS, mFlags);
1902 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001903 if (mInProgressLabel != null) {
1904 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1905 }
1906 if (mConfirmLabel != null) {
1907 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1908 }
1909 if (mCancelLabel != null) {
1910 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1911 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001912
1913 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1914 return builder;
1915 }
1916
1917 @Override
1918 public WearableExtender clone() {
1919 WearableExtender that = new WearableExtender();
1920 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001921 that.mInProgressLabel = this.mInProgressLabel;
1922 that.mConfirmLabel = this.mConfirmLabel;
1923 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001924 return that;
1925 }
1926
1927 /**
1928 * Set whether this action is available when the wearable device is not connected to
1929 * a companion device. The user can still trigger this action when the wearable device is
1930 * offline, but a visual hint will indicate that the action may not be available.
1931 * Defaults to true.
1932 */
1933 public WearableExtender setAvailableOffline(boolean availableOffline) {
1934 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1935 return this;
1936 }
1937
1938 /**
1939 * Get whether this action is available when the wearable device is not connected to
1940 * a companion device. The user can still trigger this action when the wearable device is
1941 * offline, but a visual hint will indicate that the action may not be available.
1942 * Defaults to true.
1943 */
1944 public boolean isAvailableOffline() {
1945 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1946 }
1947
1948 private void setFlag(int mask, boolean value) {
1949 if (value) {
1950 mFlags |= mask;
1951 } else {
1952 mFlags &= ~mask;
1953 }
1954 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001955
1956 /**
1957 * Set a label to display while the wearable is preparing to automatically execute the
1958 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1959 *
1960 * @param label the label to display while the action is being prepared to execute
1961 * @return this object for method chaining
1962 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001963 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001964 public WearableExtender setInProgressLabel(CharSequence label) {
1965 mInProgressLabel = label;
1966 return this;
1967 }
1968
1969 /**
1970 * Get the label to display while the wearable is preparing to automatically execute
1971 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1972 *
1973 * @return the label to display while the action is being prepared to execute
1974 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001975 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001976 public CharSequence getInProgressLabel() {
1977 return mInProgressLabel;
1978 }
1979
1980 /**
1981 * Set a label to display to confirm that the action should be executed.
1982 * This is usually an imperative verb like "Send".
1983 *
1984 * @param label the label to confirm the action should be executed
1985 * @return this object for method chaining
1986 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001987 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001988 public WearableExtender setConfirmLabel(CharSequence label) {
1989 mConfirmLabel = label;
1990 return this;
1991 }
1992
1993 /**
1994 * Get the label to display to confirm that the action should be executed.
1995 * This is usually an imperative verb like "Send".
1996 *
1997 * @return the label to confirm the action should be executed
1998 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001999 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002000 public CharSequence getConfirmLabel() {
2001 return mConfirmLabel;
2002 }
2003
2004 /**
2005 * Set a label to display to cancel the action.
2006 * This is usually an imperative verb, like "Cancel".
2007 *
2008 * @param label the label to display to cancel the action
2009 * @return this object for method chaining
2010 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002011 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002012 public WearableExtender setCancelLabel(CharSequence label) {
2013 mCancelLabel = label;
2014 return this;
2015 }
2016
2017 /**
2018 * Get the label to display to cancel the action.
2019 * This is usually an imperative verb like "Cancel".
2020 *
2021 * @return the label to display to cancel the action
2022 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002023 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002024 public CharSequence getCancelLabel() {
2025 return mCancelLabel;
2026 }
Alex Hills9ab3a232016-04-05 14:54:56 -04002027
2028 /**
2029 * Set a hint that this Action will launch an {@link Activity} directly, telling the
2030 * platform that it can generate the appropriate transitions.
2031 * @param hintLaunchesActivity {@code true} if the content intent will launch
2032 * an activity and transitions should be generated, false otherwise.
2033 * @return this object for method chaining
2034 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04002035 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04002036 boolean hintLaunchesActivity) {
2037 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
2038 return this;
2039 }
2040
2041 /**
2042 * Get a hint that this Action will launch an {@link Activity} directly, telling the
2043 * platform that it can generate the appropriate transitions
2044 * @return {@code true} if the content intent will launch an activity and transitions
2045 * should be generated, false otherwise. The default value is {@code false} if this was
2046 * never set.
2047 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04002048 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04002049 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
2050 }
Alex Hills9f087612016-06-07 09:08:59 -04002051
2052 /**
2053 * Set a hint that this Action should be displayed inline.
2054 *
2055 * @param hintDisplayInline {@code true} if action should be displayed inline, false
2056 * otherwise
2057 * @return this object for method chaining
2058 */
2059 public WearableExtender setHintDisplayActionInline(
2060 boolean hintDisplayInline) {
2061 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
2062 return this;
2063 }
2064
2065 /**
2066 * Get a hint that this Action should be displayed inline.
2067 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08002068 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04002069 * otherwise. The default value is {@code false} if this was never set.
2070 */
2071 public boolean getHintDisplayActionInline() {
2072 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
2073 }
Griff Hazen61a9e862014-05-22 16:05:19 -07002074 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002075
2076 /**
2077 * Provides meaning to an {@link Action} that hints at what the associated
2078 * {@link PendingIntent} will do. For example, an {@link Action} with a
2079 * {@link PendingIntent} that replies to a text message notification may have the
2080 * {@link #SEMANTIC_ACTION_REPLY} {@code SemanticAction} set within it.
2081 *
2082 * @hide
2083 */
2084 @IntDef(prefix = { "SEMANTIC_ACTION_" }, value = {
2085 SEMANTIC_ACTION_NONE,
2086 SEMANTIC_ACTION_REPLY,
2087 SEMANTIC_ACTION_MARK_AS_READ,
2088 SEMANTIC_ACTION_MARK_AS_UNREAD,
2089 SEMANTIC_ACTION_DELETE,
2090 SEMANTIC_ACTION_ARCHIVE,
2091 SEMANTIC_ACTION_MUTE,
2092 SEMANTIC_ACTION_UNMUTE,
2093 SEMANTIC_ACTION_THUMBS_UP,
Kodlee Yincda5b092018-02-15 15:34:53 -08002094 SEMANTIC_ACTION_THUMBS_DOWN,
Gustav Sennton005d7a02019-01-04 13:41:32 +00002095 SEMANTIC_ACTION_CALL
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002096 })
2097 @Retention(RetentionPolicy.SOURCE)
2098 public @interface SemanticAction {}
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002099 }
2100
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002101 /**
2102 * Array of all {@link Action} structures attached to this notification by
2103 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
2104 * {@link android.service.notification.NotificationListenerService} that provide an alternative
2105 * interface for invoking actions.
2106 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05002107 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002108
2109 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002110 * Replacement version of this notification whose content will be shown
2111 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
2112 * and {@link #VISIBILITY_PUBLIC}.
2113 */
2114 public Notification publicVersion;
2115
2116 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002117 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08002118 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119 */
2120 public Notification()
2121 {
2122 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002123 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002124 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002125 }
2126
2127 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128 * @hide
2129 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002130 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002131 public Notification(Context context, int icon, CharSequence tickerText, long when,
2132 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
2133 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04002134 new Builder(context)
2135 .setWhen(when)
2136 .setSmallIcon(icon)
2137 .setTicker(tickerText)
2138 .setContentTitle(contentTitle)
2139 .setContentText(contentText)
2140 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
2141 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002142 }
2143
2144 /**
2145 * Constructs a Notification object with the information needed to
2146 * have a status bar icon without the standard expanded view.
2147 *
2148 * @param icon The resource id of the icon to put in the status bar.
2149 * @param tickerText The text that flows by in the status bar when the notification first
2150 * activates.
2151 * @param when The time to show in the time field. In the System.currentTimeMillis
2152 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08002153 *
2154 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002155 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002156 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157 public Notification(int icon, CharSequence tickerText, long when)
2158 {
2159 this.icon = icon;
2160 this.tickerText = tickerText;
2161 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002162 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002163 }
2164
2165 /**
2166 * Unflatten the notification from a parcel.
2167 */
Svet Ganovddb94882016-06-23 19:55:24 -07002168 @SuppressWarnings("unchecked")
2169 public Notification(Parcel parcel) {
2170 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
2171 // intents in extras are always written as the last entry.
2172 readFromParcelImpl(parcel);
2173 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002174 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07002175 }
2176
2177 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178 {
2179 int version = parcel.readInt();
2180
Adrian Roosfb921842017-10-26 14:49:56 +02002181 mWhitelistToken = parcel.readStrongBinder();
2182 if (mWhitelistToken == null) {
2183 mWhitelistToken = processWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -07002184 }
2185 // Propagate this token to all pending intents that are unmarshalled from the parcel.
Adrian Roosfb921842017-10-26 14:49:56 +02002186 parcel.setClassCookie(PendingIntent.class, mWhitelistToken);
Dianne Hackborn98305522017-05-05 17:53:53 -07002187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002188 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002189 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04002190 if (parcel.readInt() != 0) {
2191 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04002192 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
2193 icon = mSmallIcon.getResId();
2194 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002195 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196 number = parcel.readInt();
2197 if (parcel.readInt() != 0) {
2198 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2199 }
2200 if (parcel.readInt() != 0) {
2201 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2202 }
2203 if (parcel.readInt() != 0) {
2204 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2205 }
2206 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002207 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002208 }
2209 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002210 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
2211 }
Joe Onorato561d3852010-11-20 18:09:34 -08002212 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002213 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08002214 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 defaults = parcel.readInt();
2216 flags = parcel.readInt();
2217 if (parcel.readInt() != 0) {
2218 sound = Uri.CREATOR.createFromParcel(parcel);
2219 }
2220
2221 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04002222 if (parcel.readInt() != 0) {
2223 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
2224 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002225 vibrate = parcel.createLongArray();
2226 ledARGB = parcel.readInt();
2227 ledOnMS = parcel.readInt();
2228 ledOffMS = parcel.readInt();
2229 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002230
2231 if (parcel.readInt() != 0) {
2232 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2233 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002234
2235 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002236
John Spurlockfd7f1e02014-03-18 16:41:57 -04002237 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002238
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002239 mGroupKey = parcel.readString();
2240
2241 mSortKey = parcel.readString();
2242
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06002243 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Robin Leead7e72a2017-12-04 15:45:46 +01002244 fixDuplicateExtras();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002245
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002246 actions = parcel.createTypedArray(Action.CREATOR); // may be null
2247
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002248 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002249 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2250 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002251
Chris Wren8fd39ec2014-02-27 17:43:26 -05002252 if (parcel.readInt() != 0) {
2253 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2254 }
2255
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002256 visibility = parcel.readInt();
2257
2258 if (parcel.readInt() != 0) {
2259 publicVersion = Notification.CREATOR.createFromParcel(parcel);
2260 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002261
2262 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002263
2264 if (parcel.readInt() != 0) {
2265 mChannelId = parcel.readString();
2266 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002267 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05002268
2269 if (parcel.readInt() != 0) {
2270 mShortcutId = parcel.readString();
2271 }
2272
2273 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04002274
2275 if (parcel.readInt() != 0) {
2276 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2277 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002278
2279 mGroupAlertBehavior = parcel.readInt();
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002280 if (parcel.readInt() != 0) {
2281 mAppOverlayIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2282 }
Gustav Sennton761884c2018-11-19 17:40:19 +00002283
2284 mAllowSystemGeneratedContextualActions = parcel.readBoolean();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 }
2286
Andy Stadler110988c2010-12-03 14:29:16 -08002287 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07002288 public Notification clone() {
2289 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04002290 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002291 return that;
2292 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002293
Daniel Sandler1a497d32013-04-18 14:52:45 -04002294 /**
2295 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
2296 * of this into that.
2297 * @hide
2298 */
2299 public void cloneInto(Notification that, boolean heavy) {
Adrian Roosfb921842017-10-26 14:49:56 +02002300 that.mWhitelistToken = this.mWhitelistToken;
Joe Onorato18e69df2010-05-17 22:26:12 -07002301 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002302 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002303 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07002304 that.number = this.number;
2305
2306 // PendingIntents are global, so there's no reason (or way) to clone them.
2307 that.contentIntent = this.contentIntent;
2308 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002309 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07002310
2311 if (this.tickerText != null) {
2312 that.tickerText = this.tickerText.toString();
2313 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002314 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002315 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04002316 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002317 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07002318 that.contentView = this.contentView.clone();
2319 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002320 if (heavy && this.mLargeIcon != null) {
2321 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08002322 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01002323 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07002324 that.sound = this.sound; // android.net.Uri is immutable
2325 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002326 if (this.audioAttributes != null) {
2327 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
2328 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002329
2330 final long[] vibrate = this.vibrate;
2331 if (vibrate != null) {
2332 final int N = vibrate.length;
2333 final long[] vib = that.vibrate = new long[N];
2334 System.arraycopy(vibrate, 0, vib, 0, N);
2335 }
2336
2337 that.ledARGB = this.ledARGB;
2338 that.ledOnMS = this.ledOnMS;
2339 that.ledOffMS = this.ledOffMS;
2340 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002341
Joe Onorato18e69df2010-05-17 22:26:12 -07002342 that.flags = this.flags;
2343
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002344 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08002345
John Spurlockfd7f1e02014-03-18 16:41:57 -04002346 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002347
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002348 that.mGroupKey = this.mGroupKey;
2349
2350 that.mSortKey = this.mSortKey;
2351
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002352 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002353 try {
2354 that.extras = new Bundle(this.extras);
2355 // will unparcel
2356 that.extras.size();
2357 } catch (BadParcelableException e) {
2358 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
2359 that.extras = null;
2360 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002361 }
2362
Felipe Lemedd85da62016-06-28 11:29:54 -07002363 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2364 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002365 }
2366
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002367 if (this.actions != null) {
2368 that.actions = new Action[this.actions.length];
2369 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002370 if ( this.actions[i] != null) {
2371 that.actions[i] = this.actions[i].clone();
2372 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002373 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002374 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002375
Daniel Sandler1a497d32013-04-18 14:52:45 -04002376 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002377 that.bigContentView = this.bigContentView.clone();
2378 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002379
Chris Wren8fd39ec2014-02-27 17:43:26 -05002380 if (heavy && this.headsUpContentView != null) {
2381 that.headsUpContentView = this.headsUpContentView.clone();
2382 }
2383
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002384 that.visibility = this.visibility;
2385
2386 if (this.publicVersion != null) {
2387 that.publicVersion = new Notification();
2388 this.publicVersion.cloneInto(that.publicVersion, heavy);
2389 }
2390
Dan Sandler26e81cf2014-05-06 10:01:27 -04002391 that.color = this.color;
2392
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002393 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002394 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002395 that.mShortcutId = this.mShortcutId;
2396 that.mBadgeIcon = this.mBadgeIcon;
2397 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002398 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002399 that.mAppOverlayIntent = this.mAppOverlayIntent;
Gustav Sennton761884c2018-11-19 17:40:19 +00002400 that.mAllowSystemGeneratedContextualActions = this.mAllowSystemGeneratedContextualActions;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002401
Daniel Sandler1a497d32013-04-18 14:52:45 -04002402 if (!heavy) {
2403 that.lightenPayload(); // will clean out extras
2404 }
2405 }
2406
2407 /**
Jeff Sharkey23b31182018-04-18 21:32:12 -06002408 * Note all {@link Uri} that are referenced internally, with the expectation
2409 * that Uri permission grants will need to be issued to ensure the recipient
2410 * of this object is able to render its contents.
2411 *
2412 * @hide
2413 */
2414 public void visitUris(@NonNull Consumer<Uri> visitor) {
2415 visitor.accept(sound);
2416
2417 if (tickerView != null) tickerView.visitUris(visitor);
2418 if (contentView != null) contentView.visitUris(visitor);
2419 if (bigContentView != null) bigContentView.visitUris(visitor);
2420 if (headsUpContentView != null) headsUpContentView.visitUris(visitor);
2421
2422 if (extras != null) {
2423 visitor.accept(extras.getParcelable(EXTRA_AUDIO_CONTENTS_URI));
Robin Leed107af62018-04-27 13:55:56 +02002424 if (extras.containsKey(EXTRA_BACKGROUND_IMAGE_URI)) {
2425 visitor.accept(Uri.parse(extras.getString(EXTRA_BACKGROUND_IMAGE_URI)));
2426 }
Jeff Sharkey23b31182018-04-18 21:32:12 -06002427 }
2428
2429 if (MessagingStyle.class.equals(getNotificationStyle()) && extras != null) {
2430 final Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
2431 if (!ArrayUtils.isEmpty(messages)) {
2432 for (MessagingStyle.Message message : MessagingStyle.Message
2433 .getMessagesFromBundleArray(messages)) {
2434 visitor.accept(message.getDataUri());
2435 }
2436 }
2437
2438 final Parcelable[] historic = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
2439 if (!ArrayUtils.isEmpty(historic)) {
2440 for (MessagingStyle.Message message : MessagingStyle.Message
2441 .getMessagesFromBundleArray(historic)) {
2442 visitor.accept(message.getDataUri());
2443 }
2444 }
2445 }
2446 }
2447
2448 /**
Daniel Sandler1a497d32013-04-18 14:52:45 -04002449 * Removes heavyweight parts of the Notification object for archival or for sending to
2450 * listeners when the full contents are not necessary.
2451 * @hide
2452 */
2453 public final void lightenPayload() {
2454 tickerView = null;
2455 contentView = null;
2456 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002457 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002458 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002459 if (extras != null && !extras.isEmpty()) {
2460 final Set<String> keyset = extras.keySet();
2461 final int N = keyset.size();
2462 final String[] keys = keyset.toArray(new String[N]);
2463 for (int i=0; i<N; i++) {
2464 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002465 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2466 continue;
2467 }
Dan Sandler50128532015-12-08 15:42:41 -05002468 final Object obj = extras.get(key);
2469 if (obj != null &&
2470 ( obj instanceof Parcelable
2471 || obj instanceof Parcelable[]
2472 || obj instanceof SparseArray
2473 || obj instanceof ArrayList)) {
2474 extras.remove(key);
2475 }
2476 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002477 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002478 }
2479
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002480 /**
2481 * Make sure this CharSequence is safe to put into a bundle, which basically
2482 * means it had better not be some custom Parcelable implementation.
2483 * @hide
2484 */
2485 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002486 if (cs == null) return cs;
2487 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2488 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2489 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002490 if (cs instanceof Parcelable) {
2491 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2492 + " instance is a custom Parcelable and not allowed in Notification");
2493 return cs.toString();
2494 }
Selim Cinek60a54252016-02-26 17:03:25 -08002495 return removeTextSizeSpans(cs);
2496 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002497
Selim Cinek60a54252016-02-26 17:03:25 -08002498 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2499 if (charSequence instanceof Spanned) {
2500 Spanned ss = (Spanned) charSequence;
2501 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2502 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2503 for (Object span : spans) {
2504 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002505 if (resultSpan instanceof CharacterStyle) {
2506 resultSpan = ((CharacterStyle) span).getUnderlying();
2507 }
2508 if (resultSpan instanceof TextAppearanceSpan) {
2509 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002510 resultSpan = new TextAppearanceSpan(
2511 originalSpan.getFamily(),
2512 originalSpan.getTextStyle(),
2513 -1,
2514 originalSpan.getTextColor(),
2515 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002516 } else if (resultSpan instanceof RelativeSizeSpan
2517 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002518 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002519 } else {
2520 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002521 }
2522 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2523 ss.getSpanFlags(span));
2524 }
2525 return builder;
2526 }
2527 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002528 }
2529
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002530 public int describeContents() {
2531 return 0;
2532 }
2533
2534 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002535 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002536 */
Svet Ganovddb94882016-06-23 19:55:24 -07002537 public void writeToParcel(Parcel parcel, int flags) {
2538 // We need to mark all pending intents getting into the notification
2539 // system as being put there to later allow the notification ranker
2540 // to launch them and by doing so add the app to the battery saver white
2541 // list for a short period of time. The problem is that the system
2542 // cannot look into the extras as there may be parcelables there that
2543 // the platform does not know how to handle. To go around that we have
2544 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002545 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002546 if (collectPendingIntents) {
2547 PendingIntent.setOnMarshaledListener(
2548 (PendingIntent intent, Parcel out, int outFlags) -> {
2549 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002550 if (allPendingIntents == null) {
2551 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002552 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002553 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002554 }
2555 });
2556 }
2557 try {
2558 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002559 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002560 writeToParcelImpl(parcel, flags);
2561 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002562 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002563 } finally {
2564 if (collectPendingIntents) {
2565 PendingIntent.setOnMarshaledListener(null);
2566 }
2567 }
2568 }
2569
2570 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002571 parcel.writeInt(1);
2572
Adrian Roosfb921842017-10-26 14:49:56 +02002573 parcel.writeStrongBinder(mWhitelistToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002574 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002575 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002576 if (mSmallIcon == null && icon != 0) {
2577 // you snuck an icon in here without using the builder; let's try to keep it
2578 mSmallIcon = Icon.createWithResource("", icon);
2579 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002580 if (mSmallIcon != null) {
2581 parcel.writeInt(1);
2582 mSmallIcon.writeToParcel(parcel, 0);
2583 } else {
2584 parcel.writeInt(0);
2585 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002586 parcel.writeInt(number);
2587 if (contentIntent != null) {
2588 parcel.writeInt(1);
2589 contentIntent.writeToParcel(parcel, 0);
2590 } else {
2591 parcel.writeInt(0);
2592 }
2593 if (deleteIntent != null) {
2594 parcel.writeInt(1);
2595 deleteIntent.writeToParcel(parcel, 0);
2596 } else {
2597 parcel.writeInt(0);
2598 }
2599 if (tickerText != null) {
2600 parcel.writeInt(1);
2601 TextUtils.writeToParcel(tickerText, parcel, flags);
2602 } else {
2603 parcel.writeInt(0);
2604 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002605 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002606 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002607 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002608 } else {
2609 parcel.writeInt(0);
2610 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002611 if (contentView != null) {
2612 parcel.writeInt(1);
2613 contentView.writeToParcel(parcel, 0);
2614 } else {
2615 parcel.writeInt(0);
2616 }
Selim Cinek279fa862016-06-14 10:57:25 -07002617 if (mLargeIcon == null && largeIcon != null) {
2618 // you snuck an icon in here without using the builder; let's try to keep it
2619 mLargeIcon = Icon.createWithBitmap(largeIcon);
2620 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002621 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002622 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002623 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002624 } else {
2625 parcel.writeInt(0);
2626 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002627
2628 parcel.writeInt(defaults);
2629 parcel.writeInt(this.flags);
2630
2631 if (sound != null) {
2632 parcel.writeInt(1);
2633 sound.writeToParcel(parcel, 0);
2634 } else {
2635 parcel.writeInt(0);
2636 }
2637 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002638
2639 if (audioAttributes != null) {
2640 parcel.writeInt(1);
2641 audioAttributes.writeToParcel(parcel, 0);
2642 } else {
2643 parcel.writeInt(0);
2644 }
2645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002646 parcel.writeLongArray(vibrate);
2647 parcel.writeInt(ledARGB);
2648 parcel.writeInt(ledOnMS);
2649 parcel.writeInt(ledOffMS);
2650 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002651
2652 if (fullScreenIntent != null) {
2653 parcel.writeInt(1);
2654 fullScreenIntent.writeToParcel(parcel, 0);
2655 } else {
2656 parcel.writeInt(0);
2657 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002658
2659 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002660
John Spurlockfd7f1e02014-03-18 16:41:57 -04002661 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002662
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002663 parcel.writeString(mGroupKey);
2664
2665 parcel.writeString(mSortKey);
2666
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002667 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002668
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002669 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002670
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002671 if (bigContentView != null) {
2672 parcel.writeInt(1);
2673 bigContentView.writeToParcel(parcel, 0);
2674 } else {
2675 parcel.writeInt(0);
2676 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002677
Chris Wren8fd39ec2014-02-27 17:43:26 -05002678 if (headsUpContentView != null) {
2679 parcel.writeInt(1);
2680 headsUpContentView.writeToParcel(parcel, 0);
2681 } else {
2682 parcel.writeInt(0);
2683 }
2684
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002685 parcel.writeInt(visibility);
2686
2687 if (publicVersion != null) {
2688 parcel.writeInt(1);
2689 publicVersion.writeToParcel(parcel, 0);
2690 } else {
2691 parcel.writeInt(0);
2692 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002693
2694 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002695
2696 if (mChannelId != null) {
2697 parcel.writeInt(1);
2698 parcel.writeString(mChannelId);
2699 } else {
2700 parcel.writeInt(0);
2701 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002702 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002703
2704 if (mShortcutId != null) {
2705 parcel.writeInt(1);
2706 parcel.writeString(mShortcutId);
2707 } else {
2708 parcel.writeInt(0);
2709 }
2710
2711 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002712
2713 if (mSettingsText != null) {
2714 parcel.writeInt(1);
2715 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2716 } else {
2717 parcel.writeInt(0);
2718 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002719
2720 parcel.writeInt(mGroupAlertBehavior);
Julia Reynoldsfc640012018-02-21 12:25:27 -05002721
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002722 if (mAppOverlayIntent != null) {
2723 parcel.writeInt(1);
2724 mAppOverlayIntent.writeToParcel(parcel, 0);
2725 } else {
2726 parcel.writeInt(0);
2727 }
2728
Gustav Sennton761884c2018-11-19 17:40:19 +00002729 parcel.writeBoolean(mAllowSystemGeneratedContextualActions);
2730
Julia Reynoldsfc640012018-02-21 12:25:27 -05002731 // mUsesStandardHeader is not written because it should be recomputed in listeners
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732 }
2733
2734 /**
2735 * Parcelable.Creator that instantiates Notification objects
2736 */
2737 public static final Parcelable.Creator<Notification> CREATOR
2738 = new Parcelable.Creator<Notification>()
2739 {
2740 public Notification createFromParcel(Parcel parcel)
2741 {
2742 return new Notification(parcel);
2743 }
2744
2745 public Notification[] newArray(int size)
2746 {
2747 return new Notification[size];
2748 }
2749 };
2750
2751 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05002752 * @hide
2753 */
2754 public static boolean areActionsVisiblyDifferent(Notification first, Notification second) {
2755 Notification.Action[] firstAs = first.actions;
2756 Notification.Action[] secondAs = second.actions;
2757 if (firstAs == null && secondAs != null || firstAs != null && secondAs == null) {
2758 return true;
2759 }
2760 if (firstAs != null && secondAs != null) {
2761 if (firstAs.length != secondAs.length) {
2762 return true;
2763 }
2764 for (int i = 0; i < firstAs.length; i++) {
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -04002765 if (!Objects.equals(String.valueOf(firstAs[i].title),
2766 String.valueOf(secondAs[i].title))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05002767 return true;
2768 }
2769 RemoteInput[] firstRs = firstAs[i].getRemoteInputs();
2770 RemoteInput[] secondRs = secondAs[i].getRemoteInputs();
2771 if (firstRs == null) {
2772 firstRs = new RemoteInput[0];
2773 }
2774 if (secondRs == null) {
2775 secondRs = new RemoteInput[0];
2776 }
2777 if (firstRs.length != secondRs.length) {
2778 return true;
2779 }
2780 for (int j = 0; j < firstRs.length; j++) {
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -04002781 if (!Objects.equals(String.valueOf(firstRs[j].getLabel()),
2782 String.valueOf(secondRs[j].getLabel()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05002783 return true;
2784 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05002785 }
2786 }
2787 }
2788 return false;
2789 }
2790
2791 /**
2792 * @hide
2793 */
2794 public static boolean areStyledNotificationsVisiblyDifferent(Builder first, Builder second) {
2795 if (first.getStyle() == null) {
2796 return second.getStyle() != null;
2797 }
2798 if (second.getStyle() == null) {
2799 return true;
2800 }
2801 return first.getStyle().areNotificationsVisiblyDifferent(second.getStyle());
2802 }
2803
2804 /**
2805 * @hide
2806 */
2807 public static boolean areRemoteViewsChanged(Builder first, Builder second) {
Julia Reynoldse5c60452018-04-30 14:41:36 -04002808 if (!Objects.equals(first.usesStandardHeader(), second.usesStandardHeader())) {
2809 return true;
2810 }
2811
2812 if (areRemoteViewsChanged(first.mN.contentView, second.mN.contentView)) {
2813 return true;
2814 }
2815 if (areRemoteViewsChanged(first.mN.bigContentView, second.mN.bigContentView)) {
2816 return true;
2817 }
2818 if (areRemoteViewsChanged(first.mN.headsUpContentView, second.mN.headsUpContentView)) {
2819 return true;
2820 }
2821
2822 return false;
2823 }
2824
2825 private static boolean areRemoteViewsChanged(RemoteViews first, RemoteViews second) {
2826 if (first == null && second == null) {
2827 return false;
2828 }
2829 if (first == null && second != null || first != null && second == null) {
2830 return true;
2831 }
2832
2833 if (!Objects.equals(first.getLayoutId(), second.getLayoutId())) {
2834 return true;
2835 }
2836
2837 if (!Objects.equals(first.getSequenceNumber(), second.getSequenceNumber())) {
2838 return true;
2839 }
2840
2841 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05002842 }
2843
2844 /**
Robin Leead7e72a2017-12-04 15:45:46 +01002845 * Parcelling creates multiple copies of objects in {@code extras}. Fix them.
2846 * <p>
2847 * For backwards compatibility {@code extras} holds some references to "real" member data such
2848 * as {@link getLargeIcon()} which is mirrored by {@link #EXTRA_LARGE_ICON}. This is mostly
2849 * fine as long as the object stays in one process.
2850 * <p>
2851 * However, once the notification goes into a parcel each reference gets marshalled separately,
2852 * wasting memory. Especially with large images on Auto and TV, this is worth fixing.
2853 */
2854 private void fixDuplicateExtras() {
2855 if (extras != null) {
Robin Leead7e72a2017-12-04 15:45:46 +01002856 fixDuplicateExtra(mLargeIcon, EXTRA_LARGE_ICON);
2857 }
2858 }
2859
2860 /**
2861 * If we find an extra that's exactly the same as one of the "real" fields but refers to a
2862 * separate object, replace it with the field's version to avoid holding duplicate copies.
2863 */
2864 private void fixDuplicateExtra(@Nullable Parcelable original, @NonNull String extraName) {
2865 if (original != null && extras.getParcelable(extraName) != null) {
2866 extras.putParcelable(extraName, original);
2867 }
2868 }
2869
2870 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002871 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2872 * layout.
2873 *
2874 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2875 * in the view.</p>
2876 * @param context The context for your application / activity.
2877 * @param contentTitle The title that goes in the expanded entry.
2878 * @param contentText The text that goes in the expanded entry.
2879 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2880 * If this is an activity, it must include the
2881 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002882 * that you take care of task management as described in the
2883 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2884 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002885 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002886 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002887 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002888 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002889 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002890 public void setLatestEventInfo(Context context,
2891 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002892 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2893 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2894 new Throwable());
2895 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002896
Selim Cinek4ac6f602016-06-13 15:47:03 -07002897 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2898 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2899 }
2900
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002901 // ensure that any information already set directly is preserved
2902 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002903
2904 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002905 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002906 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002907 }
2908 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002909 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002910 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002911 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002912
2913 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002914 }
2915
Julia Reynoldsda303542015-11-23 14:00:20 -05002916 /**
2917 * @hide
2918 */
2919 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002920 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002921 }
2922
2923 /**
2924 * @hide
2925 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002926 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002927 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002928 }
2929
Yi Jin6b514142017-10-30 14:54:12 -07002930 /**
2931 * @hide
2932 */
2933 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2934 long token = proto.start(fieldId);
2935 proto.write(NotificationProto.CHANNEL_ID, getChannelId());
2936 proto.write(NotificationProto.HAS_TICKER_TEXT, this.tickerText != null);
2937 proto.write(NotificationProto.FLAGS, this.flags);
2938 proto.write(NotificationProto.COLOR, this.color);
2939 proto.write(NotificationProto.CATEGORY, this.category);
2940 proto.write(NotificationProto.GROUP_KEY, this.mGroupKey);
2941 proto.write(NotificationProto.SORT_KEY, this.mSortKey);
2942 if (this.actions != null) {
2943 proto.write(NotificationProto.ACTION_LENGTH, this.actions.length);
2944 }
2945 if (this.visibility >= VISIBILITY_SECRET && this.visibility <= VISIBILITY_PUBLIC) {
2946 proto.write(NotificationProto.VISIBILITY, this.visibility);
2947 }
2948 if (publicVersion != null) {
2949 publicVersion.writeToProto(proto, NotificationProto.PUBLIC_VERSION);
2950 }
2951 proto.end(token);
2952 }
2953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002954 @Override
2955 public String toString() {
2956 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002957 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04002958 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002959 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002960 sb.append(priority);
2961 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002962 if (contentView != null) {
2963 sb.append(contentView.getPackage());
2964 sb.append("/0x");
2965 sb.append(Integer.toHexString(contentView.getLayoutId()));
2966 } else {
2967 sb.append("null");
2968 }
2969 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002970 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2971 sb.append("default");
2972 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002973 int N = this.vibrate.length-1;
2974 sb.append("[");
2975 for (int i=0; i<N; i++) {
2976 sb.append(this.vibrate[i]);
2977 sb.append(',');
2978 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002979 if (N != -1) {
2980 sb.append(this.vibrate[N]);
2981 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002982 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002983 } else {
2984 sb.append("null");
2985 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002986 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002987 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002988 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002989 } else if (this.sound != null) {
2990 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002991 } else {
2992 sb.append("null");
2993 }
Chris Wren365b6d32015-07-16 10:39:26 -04002994 if (this.tickerText != null) {
2995 sb.append(" tick");
2996 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002997 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002998 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002999 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04003000 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04003001 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003002 if (this.category != null) {
3003 sb.append(" category=");
3004 sb.append(this.category);
3005 }
3006 if (this.mGroupKey != null) {
3007 sb.append(" groupKey=");
3008 sb.append(this.mGroupKey);
3009 }
3010 if (this.mSortKey != null) {
3011 sb.append(" sortKey=");
3012 sb.append(this.mSortKey);
3013 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003014 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04003015 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003016 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04003017 }
3018 sb.append(" vis=");
3019 sb.append(visibilityToString(this.visibility));
3020 if (this.publicVersion != null) {
3021 sb.append(" publicVersion=");
3022 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003023 }
3024 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003025 return sb.toString();
3026 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003027
Dan Sandler1b718782014-07-18 12:43:45 -04003028 /**
3029 * {@hide}
3030 */
3031 public static String visibilityToString(int vis) {
3032 switch (vis) {
3033 case VISIBILITY_PRIVATE:
3034 return "PRIVATE";
3035 case VISIBILITY_PUBLIC:
3036 return "PUBLIC";
3037 case VISIBILITY_SECRET:
3038 return "SECRET";
3039 default:
3040 return "UNKNOWN(" + String.valueOf(vis) + ")";
3041 }
3042 }
3043
Joe Onoratocb109a02011-01-18 17:57:41 -08003044 /**
John Spurlock1d881a12015-03-18 19:21:54 -04003045 * {@hide}
3046 */
3047 public static String priorityToString(@Priority int pri) {
3048 switch (pri) {
3049 case PRIORITY_MIN:
3050 return "MIN";
3051 case PRIORITY_LOW:
3052 return "LOW";
3053 case PRIORITY_DEFAULT:
3054 return "DEFAULT";
3055 case PRIORITY_HIGH:
3056 return "HIGH";
3057 case PRIORITY_MAX:
3058 return "MAX";
3059 default:
3060 return "UNKNOWN(" + String.valueOf(pri) + ")";
3061 }
3062 }
3063
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04003064 /**
3065 * @hide
3066 */
3067 public boolean hasCompletedProgress() {
3068 // not a progress notification; can't be complete
3069 if (!extras.containsKey(EXTRA_PROGRESS)
3070 || !extras.containsKey(EXTRA_PROGRESS_MAX)) {
3071 return false;
3072 }
3073 // many apps use max 0 for 'indeterminate'; not complete
3074 if (extras.getInt(EXTRA_PROGRESS_MAX) == 0) {
3075 return false;
3076 }
3077 return extras.getInt(EXTRA_PROGRESS) == extras.getInt(EXTRA_PROGRESS_MAX);
3078 }
3079
Jeff Sharkey000ce802017-04-29 13:13:27 -06003080 /** @removed */
3081 @Deprecated
Julia Reynolds37856052016-11-11 09:20:07 -05003082 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003083 return mChannelId;
3084 }
3085
3086 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003087 * Returns the id of the channel this notification posts to.
3088 */
3089 public String getChannelId() {
3090 return mChannelId;
3091 }
3092
Jeff Sharkey000ce802017-04-29 13:13:27 -06003093 /** @removed */
3094 @Deprecated
Julia Reynolds2a128742016-11-28 14:29:25 -05003095 public long getTimeout() {
3096 return mTimeout;
3097 }
3098
3099 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003100 * Returns the duration from posting after which this notification should be canceled by the
3101 * system, if it's not canceled already.
3102 */
3103 public long getTimeoutAfter() {
3104 return mTimeout;
3105 }
3106
3107 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003108 * Returns what icon should be shown for this notification if it is being displayed in a
3109 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
3110 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
3111 */
3112 public int getBadgeIconType() {
3113 return mBadgeIcon;
3114 }
3115
3116 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003117 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04003118 *
3119 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
3120 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003121 */
3122 public String getShortcutId() {
3123 return mShortcutId;
3124 }
3125
Julia Reynolds3aedded2017-03-31 14:42:09 -04003126
3127 /**
3128 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
3129 */
3130 public CharSequence getSettingsText() {
3131 return mSettingsText;
3132 }
3133
Julia Reynolds13d898c2017-02-02 12:22:05 -05003134 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003135 * Returns which type of notifications in a group are responsible for audibly alerting the
3136 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
3137 * {@link #GROUP_ALERT_SUMMARY}.
3138 */
3139 public @GroupAlertBehavior int getGroupAlertBehavior() {
3140 return mGroupAlertBehavior;
3141 }
3142
3143 /**
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003144 * Returns the intent that will be used to display app content in a floating window over the
3145 * existing foreground activity.
3146 */
3147 public PendingIntent getAppOverlayIntent() {
3148 return mAppOverlayIntent;
3149 }
3150
Gustav Sennton79ebf4b2018-12-19 10:31:51 +00003151 /**
3152 * Returns whether the platform is allowed (by the app developer) to generate contextual actions
3153 * for this notification.
3154 */
Gustav Sennton761884c2018-11-19 17:40:19 +00003155 public boolean getAllowSystemGeneratedContextualActions() {
3156 return mAllowSystemGeneratedContextualActions;
3157 }
3158
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003159 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003160 * The small icon representing this notification in the status bar and content view.
3161 *
3162 * @return the small icon representing this notification.
3163 *
3164 * @see Builder#getSmallIcon()
3165 * @see Builder#setSmallIcon(Icon)
3166 */
3167 public Icon getSmallIcon() {
3168 return mSmallIcon;
3169 }
3170
3171 /**
3172 * Used when notifying to clean up legacy small icons.
3173 * @hide
3174 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003175 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04003176 public void setSmallIcon(Icon icon) {
3177 mSmallIcon = icon;
3178 }
3179
3180 /**
3181 * The large icon shown in this notification's content view.
3182 * @see Builder#getLargeIcon()
3183 * @see Builder#setLargeIcon(Icon)
3184 */
3185 public Icon getLargeIcon() {
3186 return mLargeIcon;
3187 }
3188
3189 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02003190 * @hide
3191 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003192 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003193 public boolean isGroupSummary() {
3194 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
3195 }
3196
3197 /**
3198 * @hide
3199 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003200 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003201 public boolean isGroupChild() {
3202 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
3203 }
3204
3205 /**
Julia Reynolds30203152017-05-26 13:36:31 -04003206 * @hide
3207 */
3208 public boolean suppressAlertingDueToGrouping() {
3209 if (isGroupSummary()
3210 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
3211 return true;
3212 } else if (isGroupChild()
3213 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
3214 return true;
3215 }
3216 return false;
3217 }
3218
Tony Mak638430e2018-10-08 19:19:10 +01003219
3220 /**
3221 * Finds and returns a remote input and its corresponding action.
3222 *
3223 * @param requiresFreeform requires the remoteinput to allow freeform or not.
3224 * @return the result pair, {@code null} if no result is found.
3225 *
3226 * @hide
3227 */
3228 @Nullable
3229 public Pair<RemoteInput, Action> findRemoteInputActionPair(boolean requiresFreeform) {
3230 if (actions == null) {
3231 return null;
3232 }
3233 for (Notification.Action action : actions) {
3234 if (action.getRemoteInputs() == null) {
3235 continue;
3236 }
3237 RemoteInput resultRemoteInput = null;
3238 for (RemoteInput remoteInput : action.getRemoteInputs()) {
3239 if (remoteInput.getAllowFreeFormInput() || !requiresFreeform) {
3240 resultRemoteInput = remoteInput;
3241 }
3242 }
3243 if (resultRemoteInput != null) {
3244 return Pair.create(resultRemoteInput, action);
3245 }
3246 }
3247 return null;
3248 }
3249
Julia Reynolds30203152017-05-26 13:36:31 -04003250 /**
Gustav Sennton005d7a02019-01-04 13:41:32 +00003251 * Returns the actions that are contextual out of the actions in this notification.
Gustav Senntoneab53682018-11-01 16:30:23 +00003252 *
3253 * @hide
3254 */
3255 public List<Notification.Action> getContextualActions() {
3256 if (actions == null) return Collections.emptyList();
3257
3258 List<Notification.Action> contextualActions = new ArrayList<>();
3259 for (Notification.Action action : actions) {
Gustav Sennton005d7a02019-01-04 13:41:32 +00003260 if (action.isContextual()) {
Gustav Senntoneab53682018-11-01 16:30:23 +00003261 contextualActions.add(action);
3262 }
3263 }
3264 return contextualActions;
3265 }
3266
3267 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003268 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08003269 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003270 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07003271 * content views using the platform's notification layout template. If your app supports
3272 * versions of Android as old as API level 4, you can instead use
3273 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
3274 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
3275 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08003276 *
Scott Main183bf112012-08-13 19:12:13 -07003277 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08003278 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003279 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07003280 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003281 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3282 * .setContentText(subject)
3283 * .setSmallIcon(R.drawable.new_mail)
3284 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003285 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003286 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08003287 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003288 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003289 /**
3290 * @hide
3291 */
3292 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
3293 "android.rebuild.contentViewActionCount";
3294 /**
3295 * @hide
3296 */
3297 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
3298 = "android.rebuild.bigViewActionCount";
3299 /**
3300 * @hide
3301 */
3302 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
3303 = "android.rebuild.hudViewActionCount";
3304
Selim Cinek6743c0b2017-01-18 18:24:01 -08003305 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
3306 SystemProperties.getBoolean("notifications.only_title", true);
3307
Selim Cinek389edcd2017-05-11 19:16:44 -07003308 /**
3309 * The lightness difference that has to be added to the primary text color to obtain the
3310 * secondary text color when the background is light.
3311 */
3312 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
3313
3314 /**
3315 * The lightness difference that has to be added to the primary text color to obtain the
3316 * secondary text color when the background is dark.
3317 * A bit less then the above value, since it looks better on dark backgrounds.
3318 */
3319 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
3320
Joe Onorato46439ce2010-11-19 13:56:21 -08003321 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003322 private Notification mN;
3323 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003324 private Style mStyle;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003325 @UnsupportedAppUsage
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003326 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Selim Cineke7238dd2017-12-14 17:48:32 -08003327 private ArrayList<Person> mPersonList = new ArrayList<>();
Lucas Dupina291d192018-06-07 13:59:42 -07003328 private ContrastColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08003329 private boolean mIsLegacy;
3330 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02003331
3332 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08003333 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
3334 */
3335 private int mCachedContrastColor = COLOR_INVALID;
3336 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08003337 /**
Selim Cinek4717d862018-04-19 09:19:15 +08003338 * Caches a ambient version of {@link #mCachedAmbientColorIsFor}.
Adrian Roos487374f2017-01-11 15:48:14 -08003339 */
3340 private int mCachedAmbientColor = COLOR_INVALID;
3341 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Selim Cinek4717d862018-04-19 09:19:15 +08003342 /**
3343 * A neutral color color that can be used for icons.
3344 */
3345 private int mNeutralColor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08003346
3347 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08003348 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
3349 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
3350 */
3351 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08003352 private int mTextColorsAreForBackground = COLOR_INVALID;
3353 private int mPrimaryTextColor = COLOR_INVALID;
3354 private int mSecondaryTextColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07003355 private int mBackgroundColor = COLOR_INVALID;
3356 private int mForegroundColor = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07003357 /**
3358 * A temporary location where actions are stored. If != null the view originally has action
3359 * but doesn't have any for this inflation.
3360 */
3361 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07003362 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003363
Anthony Chenad4d1582017-04-10 16:07:58 -07003364 private boolean mTintActionButtons;
3365 private boolean mInNightMode;
3366
Adrian Roos70d7aa32017-01-11 15:39:06 -08003367 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003368 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08003369 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003370 * @param context
3371 * A {@link Context} that will be used by the Builder to construct the
3372 * RemoteViews. The Context will not be held past the lifetime of this Builder
3373 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003374 * @param channelId
3375 * The constructed Notification will be posted on this
3376 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
3377 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003378 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003379 public Builder(Context context, String channelId) {
3380 this(context, (Notification) null);
3381 mN.mChannelId = channelId;
3382 }
3383
3384 /**
3385 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
3386 * instead. All posted Notifications must specify a NotificationChannel Id.
3387 */
3388 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003389 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003390 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003391 }
3392
Joe Onoratocb109a02011-01-18 17:57:41 -08003393 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003394 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02003395 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003396 public Builder(Context context, Notification toAdopt) {
3397 mContext = context;
Anthony Chenad4d1582017-04-10 16:07:58 -07003398 Resources res = mContext.getResources();
3399 mTintActionButtons = res.getBoolean(R.bool.config_tintNotificationActionButtons);
3400
3401 if (res.getBoolean(R.bool.config_enableNightMode)) {
3402 Configuration currentConfig = res.getConfiguration();
3403 mInNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
3404 == Configuration.UI_MODE_NIGHT_YES;
3405 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003406
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003407 if (toAdopt == null) {
3408 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003409 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
3410 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
3411 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003412 mN.priority = PRIORITY_DEFAULT;
3413 mN.visibility = VISIBILITY_PRIVATE;
3414 } else {
3415 mN = toAdopt;
3416 if (mN.actions != null) {
3417 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003418 }
3419
Selim Cineke7238dd2017-12-14 17:48:32 -08003420 if (mN.extras.containsKey(EXTRA_PEOPLE_LIST)) {
3421 ArrayList<Person> people = mN.extras.getParcelableArrayList(EXTRA_PEOPLE_LIST);
3422 mPersonList.addAll(people);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003423 }
3424
Selim Cinek4ac6f602016-06-13 15:47:03 -07003425 if (mN.getSmallIcon() == null && mN.icon != 0) {
3426 setSmallIcon(mN.icon);
3427 }
3428
3429 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
3430 setLargeIcon(mN.largeIcon);
3431 }
3432
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003433 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
3434 if (!TextUtils.isEmpty(templateClass)) {
3435 final Class<? extends Style> styleClass
3436 = getNotificationStyleClass(templateClass);
3437 if (styleClass == null) {
3438 Log.d(TAG, "Unknown style class: " + templateClass);
3439 } else {
3440 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003441 final Constructor<? extends Style> ctor =
3442 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003443 ctor.setAccessible(true);
3444 final Style style = ctor.newInstance();
3445 style.restoreFromExtras(mN.extras);
3446
3447 if (style != null) {
3448 setStyle(style);
3449 }
3450 } catch (Throwable t) {
3451 Log.e(TAG, "Could not create Style", t);
3452 }
3453 }
3454 }
3455
3456 }
3457 }
3458
Lucas Dupina291d192018-06-07 13:59:42 -07003459 private ContrastColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08003460 if (mColorUtil == null) {
Lucas Dupina291d192018-06-07 13:59:42 -07003461 mColorUtil = ContrastColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003462 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003463 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003464 }
3465
3466 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003467 * If this notification is duplicative of a Launcher shortcut, sets the
3468 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
3469 * the shortcut.
3470 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04003471 * This field will be ignored by Launchers that don't support badging, don't show
3472 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003473 *
3474 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
3475 * supersedes
3476 */
3477 public Builder setShortcutId(String shortcutId) {
3478 mN.mShortcutId = shortcutId;
3479 return this;
3480 }
3481
3482 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003483 * Sets which icon to display as a badge for this notification.
3484 *
3485 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
3486 * {@link #BADGE_ICON_LARGE}.
3487 *
3488 * Note: This value might be ignored, for launchers that don't support badge icons.
3489 */
Julia Reynolds612beb22017-03-30 10:48:30 -04003490 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04003491 mN.mBadgeIcon = icon;
3492 return this;
3493 }
3494
3495 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003496 * Sets the group alert behavior for this notification. Use this method to mute this
3497 * notification if alerts for this notification's group should be handled by a different
3498 * notification. This is only applicable for notifications that belong to a
Julia Reynolds399d9bf2017-08-11 12:52:14 -04003499 * {@link #setGroup(String) group}. This must be called on all notifications you want to
3500 * mute. For example, if you want only the summary of your group to make noise, all
3501 * children in the group should have the group alert behavior {@link #GROUP_ALERT_SUMMARY}.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003502 *
3503 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
3504 */
3505 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
3506 mN.mGroupAlertBehavior = groupAlertBehavior;
3507 return this;
3508 }
3509
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003510 /**
3511 * Sets the intent that will be used to display app content in a floating window
3512 * over the existing foreground activity.
3513 *
3514 * <p>This intent will be ignored unless this notification is posted to a channel that
3515 * allows {@link NotificationChannel#canOverlayApps() app overlays}.</p>
3516 *
3517 * <p>Notifications with a valid and allowed app overlay intent will be displayed as
3518 * floating windows outside of the notification shade on unlocked devices. When a user
3519 * interacts with one of these windows, this app overlay intent will be invoked and
3520 * displayed.</p>
3521 */
3522 public Builder setAppOverlayIntent(PendingIntent intent) {
3523 mN.mAppOverlayIntent = intent;
3524 return this;
3525 }
3526
Jeff Sharkey000ce802017-04-29 13:13:27 -06003527 /** @removed */
3528 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003529 public Builder setChannel(String channelId) {
3530 mN.mChannelId = channelId;
3531 return this;
3532 }
3533
3534 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003535 * Specifies the channel the notification should be delivered on.
3536 */
3537 public Builder setChannelId(String channelId) {
3538 mN.mChannelId = channelId;
3539 return this;
3540 }
3541
Jeff Sharkey000ce802017-04-29 13:13:27 -06003542 /** @removed */
3543 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05003544 public Builder setTimeout(long durationMs) {
3545 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05003546 return this;
3547 }
3548
3549 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003550 * Specifies a duration in milliseconds after which this notification should be canceled,
3551 * if it is not already canceled.
3552 */
3553 public Builder setTimeoutAfter(long durationMs) {
3554 mN.mTimeout = durationMs;
3555 return this;
3556 }
3557
3558 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003559 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003560 *
3561 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
3562 * shown anymore by default and must be opted into by using
3563 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003564 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003565 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08003566 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003567 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003568 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08003569 return this;
3570 }
3571
Joe Onoratocb109a02011-01-18 17:57:41 -08003572 /**
Griff Hazen50c11652014-05-16 09:46:31 -07003573 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07003574 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003575 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
3576 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07003577 */
3578 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003579 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07003580 return this;
3581 }
3582
3583 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003584 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08003585 *
3586 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003587 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003588 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003589 * Useful when showing an elapsed time (like an ongoing phone call).
3590 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08003591 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07003592 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08003593 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003594 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003595 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07003596 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003597 */
3598 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003599 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003600 return this;
3601 }
3602
3603 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08003604 * Sets the Chronometer to count down instead of counting up.
3605 *
3606 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
3607 * If it isn't set the chronometer will count up.
3608 *
3609 * @see #setUsesChronometer(boolean)
3610 */
Adrian Roos96b7e202016-05-17 13:50:38 -07003611 public Builder setChronometerCountDown(boolean countDown) {
3612 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003613 return this;
3614 }
3615
3616 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003617 * Set the small icon resource, which will be used to represent the notification in the
3618 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003619 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003620
3621 * The platform template for the expanded view will draw this icon in the left, unless a
3622 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3623 * icon will be moved to the right-hand side.
3624 *
3625
3626 * @param icon
3627 * A resource ID in the application's package of the drawable to use.
3628 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003629 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003630 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003631 return setSmallIcon(icon != 0
3632 ? Icon.createWithResource(mContext, icon)
3633 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003634 }
3635
Joe Onoratocb109a02011-01-18 17:57:41 -08003636 /**
3637 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3638 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3639 * LevelListDrawable}.
3640 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003641 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003642 * @param level The level to use for the icon.
3643 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003644 * @see Notification#icon
3645 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003646 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003647 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003648 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003649 return setSmallIcon(icon);
3650 }
3651
3652 /**
3653 * Set the small icon, which will be used to represent the notification in the
koprivadebd4ee2018-09-13 10:59:46 -07003654 * status bar and content view (unless overridden there by a
Dan Sandlerd63f9322015-05-06 15:18:49 -04003655 * {@link #setLargeIcon(Bitmap) large icon}).
3656 *
3657 * @param icon An Icon object to use.
3658 * @see Notification#icon
3659 */
3660 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003661 mN.setSmallIcon(icon);
3662 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3663 mN.icon = icon.getResId();
3664 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003665 return this;
3666 }
3667
Joe Onoratocb109a02011-01-18 17:57:41 -08003668 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003669 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003670 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003671 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003672 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003673 return this;
3674 }
3675
Joe Onoratocb109a02011-01-18 17:57:41 -08003676 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003677 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003678 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003679 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003680 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003681 return this;
3682 }
3683
Joe Onoratocb109a02011-01-18 17:57:41 -08003684 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003685 * This provides some additional information that is displayed in the notification. No
3686 * guarantees are given where exactly it is displayed.
3687 *
3688 * <p>This information should only be provided if it provides an essential
3689 * benefit to the understanding of the notification. The more text you provide the
3690 * less readable it becomes. For example, an email client should only provide the account
3691 * name here if more than one email account has been added.</p>
3692 *
3693 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3694 * notification header area.
3695 *
3696 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3697 * this will be shown in the third line of text in the platform notification template.
3698 * You should not be using {@link #setProgress(int, int, boolean)} at the
3699 * same time on those versions; they occupy the same place.
3700 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003701 */
3702 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003703 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003704 return this;
3705 }
3706
3707 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003708 * Provides text that will appear as a link to your application's settings.
3709 *
3710 * <p>This text does not appear within notification {@link Style templates} but may
3711 * appear when the user uses an affordance to learn more about the notification.
3712 * Additionally, this text will not appear unless you provide a valid link target by
3713 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3714 *
3715 * <p>This text is meant to be concise description about what the user can customize
3716 * when they click on this link. The recommended maximum length is 40 characters.
3717 * @param text
3718 * @return
3719 */
3720 public Builder setSettingsText(CharSequence text) {
3721 mN.mSettingsText = safeCharSequence(text);
3722 return this;
3723 }
3724
3725 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003726 * Set the remote input history.
3727 *
3728 * This should be set to the most recent inputs that have been sent
3729 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3730 * longer relevant (e.g. for chat notifications once the other party has responded).
3731 *
3732 * The most recent input must be stored at the 0 index, the second most recent at the
3733 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3734 * and how much of each individual input is shown.
3735 *
3736 * <p>Note: The reply text will only be shown on notifications that have least one action
3737 * with a {@code RemoteInput}.</p>
3738 */
3739 public Builder setRemoteInputHistory(CharSequence[] text) {
3740 if (text == null) {
3741 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3742 } else {
3743 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3744 CharSequence[] safe = new CharSequence[N];
3745 for (int i = 0; i < N; i++) {
3746 safe[i] = safeCharSequence(text[i]);
3747 }
3748 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3749 }
3750 return this;
3751 }
3752
3753 /**
Kenny Guya0f6de82018-04-06 16:20:16 +01003754 * Sets whether remote history entries view should have a spinner.
3755 * @hide
3756 */
3757 public Builder setShowRemoteInputSpinner(boolean showSpinner) {
3758 mN.extras.putBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER, showSpinner);
3759 return this;
3760 }
3761
3762 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +01003763 * Sets whether smart reply buttons should be hidden.
3764 * @hide
3765 */
3766 public Builder setHideSmartReplies(boolean hideSmartReplies) {
3767 mN.extras.putBoolean(EXTRA_HIDE_SMART_REPLIES, hideSmartReplies);
3768 return this;
3769 }
3770
3771 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003772 * Sets the number of items this notification represents. May be displayed as a badge count
3773 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003774 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08003775 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003776 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003777 return this;
3778 }
3779
Joe Onoratocb109a02011-01-18 17:57:41 -08003780 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003781 * A small piece of additional information pertaining to this notification.
3782 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003783 * The platform template will draw this on the last line of the notification, at the far
3784 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003785 *
3786 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3787 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3788 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003789 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003790 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003791 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003792 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003793 return this;
3794 }
3795
Joe Onoratocb109a02011-01-18 17:57:41 -08003796 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003797 * Set the progress this notification represents.
3798 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003799 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003800 */
3801 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003802 mN.extras.putInt(EXTRA_PROGRESS, progress);
3803 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3804 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003805 return this;
3806 }
3807
3808 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003809 * Supply a custom RemoteViews to use instead of the platform template.
3810 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003811 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003812 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003813 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003814 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003815 return setCustomContentView(views);
3816 }
3817
3818 /**
3819 * Supply custom RemoteViews to use instead of the platform template.
3820 *
3821 * This will override the layout that would otherwise be constructed by this Builder
3822 * object.
3823 */
3824 public Builder setCustomContentView(RemoteViews contentView) {
3825 mN.contentView = contentView;
3826 return this;
3827 }
3828
3829 /**
3830 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3831 *
3832 * This will override the expanded layout that would otherwise be constructed by this
3833 * Builder object.
3834 */
3835 public Builder setCustomBigContentView(RemoteViews contentView) {
3836 mN.bigContentView = contentView;
3837 return this;
3838 }
3839
3840 /**
3841 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3842 *
3843 * This will override the heads-up layout that would otherwise be constructed by this
3844 * Builder object.
3845 */
3846 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3847 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003848 return this;
3849 }
3850
Joe Onoratocb109a02011-01-18 17:57:41 -08003851 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003852 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3853 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003854 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3855 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3856 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003857 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003858 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003859 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003860 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003861 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003862 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003863 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003864 return this;
3865 }
3866
Joe Onoratocb109a02011-01-18 17:57:41 -08003867 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003868 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3869 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003870 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003871 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003872 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003873 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003874 return this;
3875 }
3876
Joe Onoratocb109a02011-01-18 17:57:41 -08003877 /**
3878 * An intent to launch instead of posting the notification to the status bar.
3879 * Only for use with extremely high-priority notifications demanding the user's
3880 * <strong>immediate</strong> attention, such as an incoming phone call or
3881 * alarm clock that the user has explicitly set to a particular time.
3882 * If this facility is used for something else, please give the user an option
3883 * to turn it off and use a normal notification, as this can be extremely
3884 * disruptive.
3885 *
Chris Wren47c20a12014-06-18 17:27:29 -04003886 * <p>
3887 * The system UI may choose to display a heads-up notification, instead of
3888 * launching this intent, while the user is using the device.
3889 * </p>
Julia Reynolds47fd15f2018-11-28 10:16:00 -05003890 * <p>Apps targeting {@link Build.VERSION_CODES#Q} and above will have to request
3891 * a permission ({@link android.Manifest.permission#USE_FULL_SCREEN_INTENT}) in order to
3892 * use full screen intents.</p>
Chris Wren47c20a12014-06-18 17:27:29 -04003893 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003894 * @param intent The pending intent to launch.
3895 * @param highPriority Passing true will cause this notification to be sent
3896 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003897 *
3898 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003899 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003900 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003901 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003902 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3903 return this;
3904 }
3905
Joe Onoratocb109a02011-01-18 17:57:41 -08003906 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003907 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003908 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003909 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003910 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003911 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003912 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003913 return this;
3914 }
3915
Joe Onoratocb109a02011-01-18 17:57:41 -08003916 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003917 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003918 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003919 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003920 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003921 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003922 setTicker(tickerText);
3923 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003924 return this;
3925 }
3926
Joe Onoratocb109a02011-01-18 17:57:41 -08003927 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003928 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003929 *
3930 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04003931 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3932 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04003933 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003934 public Builder setLargeIcon(Bitmap b) {
3935 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3936 }
3937
3938 /**
3939 * Add a large icon to the notification content view.
3940 *
3941 * In the platform template, this image will be shown on the left of the notification view
3942 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3943 * badge atop the large icon).
3944 */
3945 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003946 mN.mLargeIcon = icon;
3947 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08003948 return this;
3949 }
3950
Joe Onoratocb109a02011-01-18 17:57:41 -08003951 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003952 * Set the sound to play.
3953 *
John Spurlockc0650f022014-07-19 13:22:39 -04003954 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
3955 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003956 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003957 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003958 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003959 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08003960 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003961 mN.sound = sound;
3962 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08003963 return this;
3964 }
3965
Joe Onoratocb109a02011-01-18 17:57:41 -08003966 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003967 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08003968 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003969 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
3970 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003971 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003972 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07003973 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003974 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08003975 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003976 mN.sound = sound;
3977 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08003978 return this;
3979 }
3980
Joe Onoratocb109a02011-01-18 17:57:41 -08003981 /**
John Spurlockc0650f022014-07-19 13:22:39 -04003982 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
3983 * use during playback.
3984 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003985 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04003986 * @see Notification#sound
3987 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003988 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04003989 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003990 mN.sound = sound;
3991 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04003992 return this;
3993 }
3994
3995 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003996 * Set the vibration pattern to use.
3997 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003998 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
3999 * <code>pattern</code> parameter.
4000 *
Chris Wren47c20a12014-06-18 17:27:29 -04004001 * <p>
4002 * A notification that vibrates is more likely to be presented as a heads-up notification.
4003 * </p>
4004 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004005 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004006 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08004007 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004008 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004009 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004010 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08004011 return this;
4012 }
4013
Joe Onoratocb109a02011-01-18 17:57:41 -08004014 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004015 * Set the desired color for the indicator LED on the device, as well as the
4016 * blink duty cycle (specified in milliseconds).
4017 *
4018
4019 * Not all devices will honor all (or even any) of these values.
4020 *
Julia Reynolds529e3322017-02-06 08:33:01 -05004021 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004022 * @see Notification#ledARGB
4023 * @see Notification#ledOnMS
4024 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08004025 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004026 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08004027 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004028 mN.ledARGB = argb;
4029 mN.ledOnMS = onMs;
4030 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004031 if (onMs != 0 || offMs != 0) {
4032 mN.flags |= FLAG_SHOW_LIGHTS;
4033 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004034 return this;
4035 }
4036
Joe Onoratocb109a02011-01-18 17:57:41 -08004037 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004038 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08004039 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004040
4041 * Ongoing notifications cannot be dismissed by the user, so your application or service
4042 * must take care of canceling them.
4043 *
4044
4045 * They are typically used to indicate a background task that the user is actively engaged
4046 * with (e.g., playing music) or is pending in some way and therefore occupying the device
4047 * (e.g., a file download, sync operation, active network connection).
4048 *
4049
4050 * @see Notification#FLAG_ONGOING_EVENT
Joe Onoratocb109a02011-01-18 17:57:41 -08004051 */
Joe Onorato46439ce2010-11-19 13:56:21 -08004052 public Builder setOngoing(boolean ongoing) {
4053 setFlag(FLAG_ONGOING_EVENT, ongoing);
4054 return this;
4055 }
4056
Joe Onoratocb109a02011-01-18 17:57:41 -08004057 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08004058 * Set whether this notification should be colorized. When set, the color set with
4059 * {@link #setColor(int)} will be used as the background color of this notification.
4060 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08004061 * This should only be used for high priority ongoing tasks like navigation, an ongoing
4062 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08004063 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07004064 * For most styles, the coloring will only be applied if the notification is for a
4065 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08004066 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07004067 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08004068 *
Aurimas Liutikas7f695332018-05-31 21:07:32 -07004069 * @see #setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08004070 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08004071 */
4072 public Builder setColorized(boolean colorize) {
4073 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
4074 return this;
4075 }
4076
4077 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08004078 * Set this flag if you would only like the sound, vibrate
4079 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004080 *
4081 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08004082 */
Joe Onorato46439ce2010-11-19 13:56:21 -08004083 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
4084 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
4085 return this;
4086 }
4087
Joe Onoratocb109a02011-01-18 17:57:41 -08004088 /**
Julia Reynolds04499532016-09-13 14:04:53 -04004089 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004090 *
4091 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08004092 */
Joe Onorato46439ce2010-11-19 13:56:21 -08004093 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08004094 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08004095 return this;
4096 }
4097
Joe Onoratocb109a02011-01-18 17:57:41 -08004098 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08004099 * Set whether or not this notification should not bridge to other devices.
4100 *
4101 * <p>Some notifications can be bridged to other devices for remote display.
4102 * This hint can be set to recommend this notification not be bridged.
4103 */
4104 public Builder setLocalOnly(boolean localOnly) {
4105 setFlag(FLAG_LOCAL_ONLY, localOnly);
4106 return this;
4107 }
4108
4109 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004110 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08004111 * <p>
4112 * The value should be one or more of the following fields combined with
4113 * bitwise-or:
4114 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
4115 * <p>
4116 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004117 *
4118 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05004119 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004120 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08004121 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004122 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004123 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004124 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08004125 return this;
4126 }
4127
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004128 /**
4129 * Set the priority of this notification.
4130 *
4131 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004132 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004133 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004134 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07004135 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004136 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004137 return this;
4138 }
Joe Malin8d40d042012-11-05 11:36:40 -08004139
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004140 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04004141 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08004142 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04004143 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004144 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04004145 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004146 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004147 return this;
4148 }
4149
4150 /**
Chris Wrendde75302014-03-26 17:24:15 -04004151 * Add a person that is relevant to this notification.
4152 *
Chris Wrene6c48932014-09-29 17:19:27 -04004153 * <P>
4154 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04004155 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4156 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4157 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04004158 * </P>
4159 *
4160 * <P>
4161 * The person should be specified by the {@code String} representation of a
4162 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
4163 * </P>
4164 *
4165 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
4166 * URIs. The path part of these URIs must exist in the contacts database, in the
4167 * appropriate column, or the reference will be discarded as invalid. Telephone schema
4168 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
Selim Cineke7238dd2017-12-14 17:48:32 -08004169 * It is also possible to provide a URI with the schema {@code name:} in order to uniquely
4170 * identify a person without an entry in the contacts database.
Chris Wrene6c48932014-09-29 17:19:27 -04004171 * </P>
4172 *
4173 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04004174 * @see Notification#EXTRA_PEOPLE
Selim Cineke7238dd2017-12-14 17:48:32 -08004175 * @deprecated use {@link #addPerson(Person)}
Chris Wrendde75302014-03-26 17:24:15 -04004176 */
Chris Wrene6c48932014-09-29 17:19:27 -04004177 public Builder addPerson(String uri) {
Selim Cinek9acd6732018-03-23 16:39:02 -07004178 addPerson(new Person.Builder().setUri(uri).build());
Selim Cineke7238dd2017-12-14 17:48:32 -08004179 return this;
4180 }
4181
4182 /**
4183 * Add a person that is relevant to this notification.
4184 *
4185 * <P>
4186 * Depending on user preferences, this annotation may allow the notification to pass
4187 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4188 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4189 * appear more prominently in the user interface.
4190 * </P>
4191 *
4192 * <P>
4193 * A person should usually contain a uri in order to benefit from the ranking boost.
4194 * However, even if no uri is provided, it's beneficial to provide other people in the
4195 * notification, such that listeners and voice only devices can announce and handle them
4196 * properly.
4197 * </P>
4198 *
4199 * @param person the person to add.
4200 * @see Notification#EXTRA_PEOPLE_LIST
4201 */
4202 public Builder addPerson(Person person) {
4203 mPersonList.add(person);
Chris Wrendde75302014-03-26 17:24:15 -04004204 return this;
4205 }
4206
4207 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004208 * Set this notification to be part of a group of notifications sharing the same key.
4209 * Grouped notifications may display in a cluster or stack on devices which
4210 * support such rendering.
4211 *
4212 * <p>To make this notification the summary for its group, also call
4213 * {@link #setGroupSummary}. A sort order can be specified for group members by using
4214 * {@link #setSortKey}.
4215 * @param groupKey The group key of the group.
4216 * @return this object for method chaining
4217 */
4218 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004219 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004220 return this;
4221 }
4222
4223 /**
4224 * Set this notification to be the group summary for a group of notifications.
4225 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04004226 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
4227 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004228 * @param isGroupSummary Whether this notification should be a group summary.
4229 * @return this object for method chaining
4230 */
4231 public Builder setGroupSummary(boolean isGroupSummary) {
4232 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
4233 return this;
4234 }
4235
4236 /**
4237 * Set a sort key that orders this notification among other notifications from the
4238 * same package. This can be useful if an external sort was already applied and an app
4239 * would like to preserve this. Notifications will be sorted lexicographically using this
4240 * value, although providing different priorities in addition to providing sort key may
4241 * cause this value to be ignored.
4242 *
4243 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07004244 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004245 *
4246 * @see String#compareTo(String)
4247 */
4248 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004249 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004250 return this;
4251 }
4252
4253 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004254 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004255 *
Griff Hazen720042b2014-02-24 15:46:56 -08004256 * <p>Values within the Bundle will replace existing extras values in this Builder.
4257 *
4258 * @see Notification#extras
4259 */
Griff Hazen959591e2014-05-15 22:26:18 -07004260 public Builder addExtras(Bundle extras) {
4261 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004262 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08004263 }
4264 return this;
4265 }
4266
4267 /**
4268 * Set metadata for this notification.
4269 *
4270 * <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 -04004271 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004272 * called.
4273 *
Griff Hazen720042b2014-02-24 15:46:56 -08004274 * <p>Replaces any existing extras values with those from the provided Bundle.
4275 * Use {@link #addExtras} to merge in metadata instead.
4276 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004277 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004278 */
Griff Hazen959591e2014-05-15 22:26:18 -07004279 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004280 if (extras != null) {
4281 mUserExtras = extras;
4282 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004283 return this;
4284 }
4285
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004286 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004287 * Get the current metadata Bundle used by this notification Builder.
4288 *
4289 * <p>The returned Bundle is shared with this Builder.
4290 *
4291 * <p>The current contents of this Bundle are copied into the Notification each time
4292 * {@link #build()} is called.
4293 *
4294 * @see Notification#extras
4295 */
4296 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004297 return mUserExtras;
4298 }
4299
4300 private Bundle getAllExtras() {
4301 final Bundle saveExtras = (Bundle) mUserExtras.clone();
4302 saveExtras.putAll(mN.extras);
4303 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08004304 }
4305
4306 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004307 * Add an action to this notification. Actions are typically displayed by
4308 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004309 * <p>
4310 * Every action must have an icon (32dp square and matching the
4311 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4312 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4313 * <p>
4314 * A notification in its expanded form can display up to 3 actions, from left to right in
4315 * the order they were added. Actions will not be displayed when the notification is
4316 * collapsed, however, so be sure that any essential functions may be accessed by the user
4317 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004318 *
4319 * @param icon Resource ID of a drawable that represents the action.
4320 * @param title Text describing the action.
4321 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04004322 *
4323 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004324 */
Dan Sandler86647982015-05-13 23:41:13 -04004325 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004326 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004327 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004328 return this;
4329 }
4330
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004331 /**
Griff Hazen959591e2014-05-15 22:26:18 -07004332 * Add an action to this notification. Actions are typically displayed by
4333 * the system as a button adjacent to the notification content.
4334 * <p>
4335 * Every action must have an icon (32dp square and matching the
4336 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4337 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4338 * <p>
4339 * A notification in its expanded form can display up to 3 actions, from left to right in
4340 * the order they were added. Actions will not be displayed when the notification is
4341 * collapsed, however, so be sure that any essential functions may be accessed by the user
4342 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
4343 *
4344 * @param action The action to add.
4345 */
4346 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08004347 if (action != null) {
4348 mActions.add(action);
4349 }
Griff Hazen959591e2014-05-15 22:26:18 -07004350 return this;
4351 }
4352
4353 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004354 * Alter the complete list of actions attached to this notification.
4355 * @see #addAction(Action).
4356 *
4357 * @param actions
4358 * @return
4359 */
4360 public Builder setActions(Action... actions) {
4361 mActions.clear();
4362 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08004363 if (actions[i] != null) {
4364 mActions.add(actions[i]);
4365 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004366 }
4367 return this;
4368 }
4369
4370 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004371 * Add a rich notification style to be applied at build time.
4372 *
4373 * @param style Object responsible for modifying the notification style.
4374 */
4375 public Builder setStyle(Style style) {
4376 if (mStyle != style) {
4377 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004378 if (mStyle != null) {
4379 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004380 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
4381 } else {
4382 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004383 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004384 }
4385 return this;
4386 }
4387
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004388 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05004389 * Returns the style set by {@link #setStyle(Style)}.
4390 */
4391 public Style getStyle() {
4392 return mStyle;
4393 }
4394
4395 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004396 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07004397 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004398 * @return The same Builder.
4399 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06004400 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004401 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004402 return this;
4403 }
4404
4405 /**
4406 * Supply a replacement Notification whose contents should be shown in insecure contexts
4407 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
4408 * @param n A replacement notification, presumably with some or all info redacted.
4409 * @return The same Builder.
4410 */
4411 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004412 if (n != null) {
4413 mN.publicVersion = new Notification();
4414 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
4415 } else {
4416 mN.publicVersion = null;
4417 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004418 return this;
4419 }
4420
Griff Hazenb720abe2014-05-20 13:15:30 -07004421 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004422 * Apply an extender to this notification builder. Extenders may be used to add
4423 * metadata or change options on this builder.
4424 */
Griff Hazen61a9e862014-05-22 16:05:19 -07004425 public Builder extend(Extender extender) {
4426 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004427 return this;
4428 }
4429
Dan Sandler4e787062015-06-17 15:09:48 -04004430 /**
4431 * @hide
4432 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04004433 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08004434 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004435 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004436 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004437 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004438 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04004439 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08004440 }
4441
Dan Sandler26e81cf2014-05-06 10:01:27 -04004442 /**
4443 * Sets {@link Notification#color}.
4444 *
4445 * @param argb The accent color to use
4446 *
4447 * @return The same Builder.
4448 */
Tor Norbye80756e32015-03-02 09:39:27 -08004449 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004450 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004451 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04004452 return this;
4453 }
4454
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004455 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04004456 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
4457 // This user can never be a badged profile,
4458 // and also includes USER_ALL system notifications.
4459 return null;
4460 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02004461 // Note: This assumes that the current user can read the profile badge of the
4462 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08004463 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05004464 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004465 }
4466
4467 private Bitmap getProfileBadge() {
4468 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01004469 if (badge == null) {
4470 return null;
4471 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004472 final int size = mContext.getResources().getDimensionPixelSize(
4473 R.dimen.notification_badge_size);
4474 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004475 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004476 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004477 badge.draw(canvas);
4478 return bitmap;
4479 }
4480
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004481 private void bindProfileBadge(RemoteViews contentView, StandardTemplateParams p) {
Kenny Guy98193ea2014-07-24 19:54:37 +01004482 Bitmap profileBadge = getProfileBadge();
4483
Kenny Guy98193ea2014-07-24 19:54:37 +01004484 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004485 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
4486 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004487 if (isColorized(p)) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004488 contentView.setDrawableTint(R.id.profile_badge, false,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004489 getPrimaryTextColor(p), PorterDuff.Mode.SRC_ATOP);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004490 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004491 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004492 }
4493
Gus Prevasbae5ba32018-11-28 15:45:13 -05004494 private void bindAlertedIcon(RemoteViews contentView, StandardTemplateParams p) {
4495 contentView.setDrawableTint(
4496 R.id.alerted_icon,
4497 false /* targetBackground */,
4498 getNeutralColor(p),
4499 PorterDuff.Mode.SRC_ATOP);
4500 }
4501
Julia Reynoldsfc640012018-02-21 12:25:27 -05004502 /**
4503 * @hide
4504 */
4505 public boolean usesStandardHeader() {
4506 if (mN.mUsesStandardHeader) {
4507 return true;
4508 }
4509 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.N) {
4510 if (mN.contentView == null && mN.bigContentView == null) {
4511 return true;
4512 }
4513 }
4514 boolean contentViewUsesHeader = mN.contentView == null
4515 || STANDARD_LAYOUTS.contains(mN.contentView.getLayoutId());
4516 boolean bigContentViewUsesHeader = mN.bigContentView == null
4517 || STANDARD_LAYOUTS.contains(mN.bigContentView.getLayoutId());
4518 return contentViewUsesHeader && bigContentViewUsesHeader;
4519 }
4520
Christoph Studerfe718432014-09-01 18:21:18 +02004521 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004522 resetNotificationHeader(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02004523 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08004524 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004525 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08004526 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004527 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08004528 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08004529 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004530 }
4531
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004532 /**
4533 * Resets the notification header to its original state
4534 */
4535 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07004536 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
4537 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08004538 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004539 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02004540 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004541 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07004542 contentView.setTextViewText(R.id.header_text, null);
Selim Cinekafeed292017-12-12 17:32:44 -08004543 contentView.setViewVisibility(R.id.header_text_secondary, View.GONE);
4544 contentView.setTextViewText(R.id.header_text_secondary, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004545 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinekafeed292017-12-12 17:32:44 -08004546 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004547 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004548 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004549 contentView.setImageViewIcon(R.id.profile_badge, null);
4550 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Gus Prevasa3226492018-10-23 11:10:09 -04004551 contentView.setViewVisibility(R.id.alerted_icon, View.GONE);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004552 mN.mUsesStandardHeader = false;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004553 }
4554
Selim Cinek384804b2018-04-18 14:31:07 +08004555 private RemoteViews applyStandardTemplate(int resId, TemplateBindResult result) {
4556 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this),
4557 result);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004558 }
4559
Selim Cinek384804b2018-04-18 14:31:07 +08004560 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p,
4561 TemplateBindResult result) {
Kenny Guy77320062014-08-27 21:37:15 +01004562 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04004563
Christoph Studerfe718432014-09-01 18:21:18 +02004564 resetStandardTemplate(contentView);
4565
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004566 final Bundle ex = mN.extras;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004567 updateBackgroundColor(contentView, p);
4568 bindNotificationHeader(contentView, p);
Selim Cinek384804b2018-04-18 14:31:07 +08004569 bindLargeIconAndReply(contentView, p, result);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004570 boolean showProgress = handleProgressBar(contentView, ex, p);
Adrian Roos70d7aa32017-01-11 15:39:06 -08004571 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08004572 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004573 contentView.setTextViewText(R.id.title, processTextSpans(p.title));
Adrian Roos72171622017-01-27 10:32:06 -08004574 if (!p.ambient) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004575 setTextViewColorPrimary(contentView, R.id.title, p);
Adrian Roos72171622017-01-27 10:32:06 -08004576 }
Selim Cinek954cc232016-05-20 13:29:23 -07004577 contentView.setViewLayoutWidth(R.id.title, showProgress
4578 ? ViewGroup.LayoutParams.WRAP_CONTENT
4579 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08004580 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08004581 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08004582 int textId = showProgress ? com.android.internal.R.id.text_line_1
4583 : com.android.internal.R.id.text;
Selim Cinek48f66b72017-08-18 16:17:51 -07004584 contentView.setTextViewText(textId, processTextSpans(p.text));
Adrian Roos72171622017-01-27 10:32:06 -08004585 if (!p.ambient) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004586 setTextViewColorSecondary(contentView, textId, p);
Adrian Roos72171622017-01-27 10:32:06 -08004587 }
Selim Cinek41598732016-01-11 16:58:37 -08004588 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08004589 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08004590
Selim Cinek279fa862016-06-14 10:57:25 -07004591 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004592
Selim Cinek29603462015-11-17 19:04:39 -08004593 return contentView;
4594 }
4595
Selim Cinek48f66b72017-08-18 16:17:51 -07004596 private CharSequence processTextSpans(CharSequence text) {
Lucas Dupind3c99322018-09-27 10:22:29 -07004597 if (hasForegroundColor() || mInNightMode) {
Lucas Dupina291d192018-06-07 13:59:42 -07004598 return ContrastColorUtil.clearColorSpans(text);
Selim Cinek48f66b72017-08-18 16:17:51 -07004599 }
4600 return text;
4601 }
4602
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004603 private void setTextViewColorPrimary(RemoteViews contentView, int id,
4604 StandardTemplateParams p) {
4605 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004606 contentView.setTextColor(id, mPrimaryTextColor);
4607 }
4608
Selim Cinek48f66b72017-08-18 16:17:51 -07004609 private boolean hasForegroundColor() {
4610 return mForegroundColor != COLOR_INVALID;
4611 }
4612
Selim Cinek389edcd2017-05-11 19:16:44 -07004613 /**
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004614 * Return the primary text color using the existing template params
Selim Cinek389edcd2017-05-11 19:16:44 -07004615 * @hide
4616 */
4617 @VisibleForTesting
4618 public int getPrimaryTextColor() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004619 return getPrimaryTextColor(mParams);
4620 }
4621
4622 /**
4623 * @param p the template params to inflate this with
4624 * @return the primary text color
4625 * @hide
4626 */
4627 @VisibleForTesting
4628 public int getPrimaryTextColor(StandardTemplateParams p) {
4629 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004630 return mPrimaryTextColor;
4631 }
4632
Selim Cinek389edcd2017-05-11 19:16:44 -07004633 /**
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004634 * Return the secondary text color using the existing template params
Selim Cinek389edcd2017-05-11 19:16:44 -07004635 * @hide
4636 */
4637 @VisibleForTesting
4638 public int getSecondaryTextColor() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004639 return getSecondaryTextColor(mParams);
4640 }
4641
4642 /**
4643 * @param p the template params to inflate this with
4644 * @return the secondary text color
4645 * @hide
4646 */
4647 @VisibleForTesting
4648 public int getSecondaryTextColor(StandardTemplateParams p) {
4649 ensureColors(p);
Selim Cinek389edcd2017-05-11 19:16:44 -07004650 return mSecondaryTextColor;
4651 }
4652
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004653 private void setTextViewColorSecondary(RemoteViews contentView, int id,
4654 StandardTemplateParams p) {
4655 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004656 contentView.setTextColor(id, mSecondaryTextColor);
4657 }
4658
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004659 private void ensureColors(StandardTemplateParams p) {
4660 int backgroundColor = getBackgroundColor(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004661 if (mPrimaryTextColor == COLOR_INVALID
4662 || mSecondaryTextColor == COLOR_INVALID
Selim Cinek7b9605b2017-01-19 17:36:00 -08004663 || mTextColorsAreForBackground != backgroundColor) {
4664 mTextColorsAreForBackground = backgroundColor;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004665 if (!hasForegroundColor() || !isColorized(p)) {
Lucas Dupina291d192018-06-07 13:59:42 -07004666 mPrimaryTextColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004667 backgroundColor, mInNightMode);
Lucas Dupina291d192018-06-07 13:59:42 -07004668 mSecondaryTextColor = ContrastColorUtil.resolveSecondaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004669 backgroundColor, mInNightMode);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004670 if (backgroundColor != COLOR_DEFAULT && isColorized(p)) {
Lucas Dupina291d192018-06-07 13:59:42 -07004671 mPrimaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004672 mPrimaryTextColor, backgroundColor, 4.5);
Lucas Dupina291d192018-06-07 13:59:42 -07004673 mSecondaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004674 mSecondaryTextColor, backgroundColor, 4.5);
4675 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07004676 } else {
Lucas Dupina291d192018-06-07 13:59:42 -07004677 double backLum = ContrastColorUtil.calculateLuminance(backgroundColor);
4678 double textLum = ContrastColorUtil.calculateLuminance(mForegroundColor);
4679 double contrast = ContrastColorUtil.calculateContrast(mForegroundColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004680 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07004681 // We only respect the given colors if worst case Black or White still has
4682 // contrast
4683 boolean backgroundLight = backLum > textLum
4684 && satisfiesTextContrast(backgroundColor, Color.BLACK)
4685 || backLum <= textLum
4686 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004687 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07004688 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004689 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004690 mForegroundColor,
4691 backgroundColor,
4692 true /* findFG */,
4693 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004694 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004695 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004696 } else {
4697 mSecondaryTextColor =
Lucas Dupina291d192018-06-07 13:59:42 -07004698 ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004699 mForegroundColor,
4700 backgroundColor,
4701 true /* findFG */,
4702 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004703 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004704 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004705 }
4706 } else {
4707 mPrimaryTextColor = mForegroundColor;
Lucas Dupina291d192018-06-07 13:59:42 -07004708 mSecondaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004709 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4710 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Lucas Dupina291d192018-06-07 13:59:42 -07004711 if (ContrastColorUtil.calculateContrast(mSecondaryTextColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004712 backgroundColor) < 4.5f) {
4713 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07004714 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004715 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004716 mSecondaryTextColor,
4717 backgroundColor,
4718 true /* findFG */,
4719 4.5f);
4720 } else {
4721 mSecondaryTextColor
Lucas Dupina291d192018-06-07 13:59:42 -07004722 = ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004723 mSecondaryTextColor,
4724 backgroundColor,
4725 true /* findFG */,
4726 4.5f);
4727 }
Lucas Dupina291d192018-06-07 13:59:42 -07004728 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004729 mSecondaryTextColor, backgroundLight
4730 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4731 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004732 }
4733 }
4734 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004735 }
4736 }
4737
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004738 private void updateBackgroundColor(RemoteViews contentView,
4739 StandardTemplateParams p) {
4740 if (isColorized(p)) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004741 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004742 getBackgroundColor(p));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004743 } else {
4744 // Clear it!
4745 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4746 0);
4747 }
4748 }
4749
Selim Cinek860b6da2015-12-16 19:02:19 -08004750 /**
4751 * @param remoteView the remote view to update the minheight in
4752 * @param hasMinHeight does it have a mimHeight
4753 * @hide
4754 */
4755 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4756 int minHeight = 0;
4757 if (hasMinHeight) {
4758 // we need to set the minHeight of the notification
4759 minHeight = mContext.getResources().getDimensionPixelSize(
4760 com.android.internal.R.dimen.notification_min_content_height);
4761 }
4762 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4763 }
4764
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004765 private boolean handleProgressBar(RemoteViews contentView, Bundle ex,
4766 StandardTemplateParams p) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004767 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4768 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4769 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004770 if (p.hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004771 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004772 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004773 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004774 contentView.setProgressBackgroundTintList(
4775 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4776 R.color.notification_progress_background_color)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004777 if (getRawColor(p) != COLOR_DEFAULT) {
4778 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor(p));
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004779 contentView.setProgressTintList(R.id.progress, colorStateList);
4780 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004781 }
Selim Cinek29603462015-11-17 19:04:39 -08004782 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004783 } else {
4784 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004785 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004786 }
Joe Onorato561d3852010-11-20 18:09:34 -08004787 }
4788
Selim Cinek384804b2018-04-18 14:31:07 +08004789 private void bindLargeIconAndReply(RemoteViews contentView, StandardTemplateParams p,
4790 TemplateBindResult result) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004791 boolean largeIconShown = bindLargeIcon(contentView, p);
4792 boolean replyIconShown = bindReplyIcon(contentView, p);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004793 boolean iconContainerVisible = largeIconShown || replyIconShown;
Selim Cinek384804b2018-04-18 14:31:07 +08004794 contentView.setViewVisibility(R.id.right_icon_container,
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004795 iconContainerVisible ? View.VISIBLE : View.GONE);
Selim Cinek1c72fa02018-04-23 18:00:54 +08004796 int marginEnd = calculateMarginEnd(largeIconShown, replyIconShown);
4797 contentView.setViewLayoutMarginEnd(R.id.line1, marginEnd);
4798 contentView.setViewLayoutMarginEnd(R.id.text, marginEnd);
4799 contentView.setViewLayoutMarginEnd(R.id.progress, marginEnd);
Selim Cinek384804b2018-04-18 14:31:07 +08004800 if (result != null) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08004801 result.setIconMarginEnd(marginEnd);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004802 result.setRightIconContainerVisible(iconContainerVisible);
Selim Cinek384804b2018-04-18 14:31:07 +08004803 }
4804 }
4805
Selim Cinek1c72fa02018-04-23 18:00:54 +08004806 private int calculateMarginEnd(boolean largeIconShown, boolean replyIconShown) {
4807 int marginEnd = 0;
4808 int contentMargin = mContext.getResources().getDimensionPixelSize(
4809 R.dimen.notification_content_margin_end);
4810 int iconSize = mContext.getResources().getDimensionPixelSize(
4811 R.dimen.notification_right_icon_size);
4812 if (replyIconShown) {
4813 // The size of the reply icon
4814 marginEnd += iconSize;
4815
4816 int replyInset = mContext.getResources().getDimensionPixelSize(
4817 R.dimen.notification_reply_inset);
4818 // We're subtracting the inset of the reply icon to make sure it's
4819 // aligned nicely on the right, and remove it from the following padding
4820 marginEnd -= replyInset * 2;
4821 }
4822 if (largeIconShown) {
4823 // adding size of the right icon
4824 marginEnd += iconSize;
4825
4826 if (replyIconShown) {
4827 // We also add some padding to the reply icon if it's around
4828 marginEnd += contentMargin;
4829 }
4830 }
4831 if (replyIconShown || largeIconShown) {
4832 // The padding to the content
4833 marginEnd += contentMargin;
4834 }
4835 return marginEnd;
4836 }
4837
Selim Cinek384804b2018-04-18 14:31:07 +08004838 /**
4839 * Bind the large icon.
4840 * @return if the largeIcon is visible
4841 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004842 private boolean bindLargeIcon(RemoteViews contentView, StandardTemplateParams p) {
Selim Cinek279fa862016-06-14 10:57:25 -07004843 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4844 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4845 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004846 boolean showLargeIcon = mN.mLargeIcon != null && !p.hideLargeIcon && !p.ambient;
Selim Cinek88188f22017-09-19 16:46:56 -07004847 if (showLargeIcon) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004848 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4849 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004850 processLargeLegacyIcon(mN.mLargeIcon, contentView, p);
Selim Cinek88188f22017-09-19 16:46:56 -07004851 }
Selim Cinek384804b2018-04-18 14:31:07 +08004852 return showLargeIcon;
4853 }
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004854
Selim Cinek384804b2018-04-18 14:31:07 +08004855 /**
4856 * Bind the reply icon.
4857 * @return if the reply icon is visible
4858 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004859 private boolean bindReplyIcon(RemoteViews contentView, StandardTemplateParams p) {
4860 boolean actionVisible = !p.hideReplyIcon && !p.ambient;
Selim Cinek384804b2018-04-18 14:31:07 +08004861 Action action = null;
Selim Cinek88188f22017-09-19 16:46:56 -07004862 if (actionVisible) {
Selim Cinek384804b2018-04-18 14:31:07 +08004863 action = findReplyAction();
4864 actionVisible = action != null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004865 }
Selim Cinek384804b2018-04-18 14:31:07 +08004866 if (actionVisible) {
4867 contentView.setViewVisibility(R.id.reply_icon_action, View.VISIBLE);
4868 contentView.setDrawableTint(R.id.reply_icon_action,
4869 false /* targetBackground */,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004870 getNeutralColor(p),
Selim Cinek384804b2018-04-18 14:31:07 +08004871 PorterDuff.Mode.SRC_ATOP);
4872 contentView.setOnClickPendingIntent(R.id.reply_icon_action, action.actionIntent);
4873 contentView.setRemoteInputs(R.id.reply_icon_action, action.mRemoteInputs);
4874 } else {
4875 contentView.setRemoteInputs(R.id.reply_icon_action, null);
4876 }
4877 contentView.setViewVisibility(R.id.reply_icon_action,
4878 actionVisible ? View.VISIBLE : View.GONE);
4879 return actionVisible;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004880 }
4881
4882 private Action findReplyAction() {
4883 ArrayList<Action> actions = mActions;
4884 if (mOriginalActions != null) {
4885 actions = mOriginalActions;
4886 }
4887 int numActions = actions.size();
4888 for (int i = 0; i < numActions; i++) {
4889 Action action = actions.get(i);
4890 if (hasValidRemoteInput(action)) {
4891 return action;
4892 }
4893 }
4894 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004895 }
4896
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004897 private void bindNotificationHeader(RemoteViews contentView, StandardTemplateParams p) {
4898 bindSmallIcon(contentView, p);
4899 bindHeaderAppName(contentView, p);
4900 if (!p.ambient) {
Adrian Roos487374f2017-01-11 15:48:14 -08004901 // Ambient view does not have these
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004902 bindHeaderText(contentView, p);
4903 bindHeaderTextSecondary(contentView, p);
4904 bindHeaderChronometerAndTime(contentView, p);
4905 bindProfileBadge(contentView, p);
Gus Prevasbae5ba32018-11-28 15:45:13 -05004906 bindAlertedIcon(contentView, p);
Adrian Roos487374f2017-01-11 15:48:14 -08004907 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004908 bindActivePermissions(contentView, p);
4909 bindExpandButton(contentView, p);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004910 mN.mUsesStandardHeader = true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004911 }
4912
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004913 private void bindActivePermissions(RemoteViews contentView, StandardTemplateParams p) {
4914 int color = p.ambient ? resolveAmbientColor(p) : getNeutralColor(p);
Julia Reynoldsb887b232018-04-10 16:38:08 -04004915 contentView.setDrawableTint(R.id.camera, false, color, PorterDuff.Mode.SRC_ATOP);
4916 contentView.setDrawableTint(R.id.mic, false, color, PorterDuff.Mode.SRC_ATOP);
4917 contentView.setDrawableTint(R.id.overlay, false, color, PorterDuff.Mode.SRC_ATOP);
4918 }
4919
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004920 private void bindExpandButton(RemoteViews contentView, StandardTemplateParams p) {
4921 int color = isColorized(p) ? getPrimaryTextColor(p) : getSecondaryTextColor(p);
Sunny Goyal5b153922017-09-21 21:00:36 -07004922 contentView.setDrawableTint(R.id.expand_button, false, color,
4923 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08004924 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08004925 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004926 }
4927
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004928 private void bindHeaderChronometerAndTime(RemoteViews contentView,
4929 StandardTemplateParams p) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004930 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08004931 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004932 setTextViewColorSecondary(contentView, R.id.time_divider, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004933 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
4934 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
4935 contentView.setLong(R.id.chronometer, "setBase",
4936 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
4937 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07004938 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07004939 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004940 setTextViewColorSecondary(contentView, R.id.chronometer, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004941 } else {
4942 contentView.setViewVisibility(R.id.time, View.VISIBLE);
4943 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004944 setTextViewColorSecondary(contentView, R.id.time, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004945 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004946 } else {
4947 // We still want a time to be set but gone, such that we can show and hide it
4948 // on demand in case it's a child notification without anything in the header
4949 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004950 }
4951 }
4952
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004953 private void bindHeaderText(RemoteViews contentView, StandardTemplateParams p) {
4954 CharSequence summaryText = p.summaryText;
4955 if (summaryText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05004956 && mStyle.hasSummaryInHeader()) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004957 summaryText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05004958 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004959 if (summaryText == null
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004960 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4961 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004962 summaryText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004963 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004964 if (summaryText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004965 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek48f66b72017-08-18 16:17:51 -07004966 contentView.setTextViewText(R.id.header_text, processTextSpans(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004967 processLegacyText(summaryText)));
4968 setTextViewColorSecondary(contentView, R.id.header_text, p);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004969 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
4970 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004971 setTextViewColorSecondary(contentView, R.id.header_text_divider, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004972 }
4973 }
4974
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004975 private void bindHeaderTextSecondary(RemoteViews contentView, StandardTemplateParams p) {
4976 if (!TextUtils.isEmpty(p.headerTextSecondary)) {
Selim Cinekafeed292017-12-12 17:32:44 -08004977 contentView.setTextViewText(R.id.header_text_secondary, processTextSpans(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004978 processLegacyText(p.headerTextSecondary)));
4979 setTextViewColorSecondary(contentView, R.id.header_text_secondary, p);
Selim Cinekafeed292017-12-12 17:32:44 -08004980 contentView.setViewVisibility(R.id.header_text_secondary, View.VISIBLE);
4981 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004982 setTextViewColorSecondary(contentView, R.id.header_text_secondary_divider, p);
Selim Cinekafeed292017-12-12 17:32:44 -08004983 }
4984 }
4985
Adrian Rooseba05822016-04-22 17:09:27 -07004986 /**
4987 * @hide
4988 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01004989 @UnsupportedAppUsage
Adrian Rooseba05822016-04-22 17:09:27 -07004990 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004991 CharSequence name = null;
4992 final PackageManager pm = mContext.getPackageManager();
4993 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
4994 // only system packages which lump together a bunch of unrelated stuff
4995 // may substitute a different name to make the purpose of the
4996 // notification more clear. the correct package label should always
4997 // be accessible via SystemUI.
4998 final String pkg = mContext.getPackageName();
4999 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
5000 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
5001 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
5002 name = subName;
5003 } else {
5004 Log.w(TAG, "warning: pkg "
5005 + pkg + " attempting to substitute app name '" + subName
5006 + "' without holding perm "
5007 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
5008 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005009 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04005010 if (TextUtils.isEmpty(name)) {
5011 name = pm.getApplicationLabel(mContext.getApplicationInfo());
5012 }
5013 if (TextUtils.isEmpty(name)) {
5014 // still nothing?
5015 return null;
5016 }
5017
5018 return String.valueOf(name);
5019 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005020 private void bindHeaderAppName(RemoteViews contentView, StandardTemplateParams p) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04005021 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005022 if (isColorized(p)) {
5023 setTextViewColorPrimary(contentView, R.id.app_name_text, p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005024 } else {
5025 contentView.setTextColor(R.id.app_name_text,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005026 p.ambient ? resolveAmbientColor(p) : getSecondaryTextColor(p));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005027 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005028 }
5029
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005030 private boolean isColorized(StandardTemplateParams p) {
5031 return p.allowColorization && !p.ambient && mN.isColorized();
5032 }
5033
5034 private void bindSmallIcon(RemoteViews contentView, StandardTemplateParams p) {
Selim Cinek279fa862016-06-14 10:57:25 -07005035 if (mN.mSmallIcon == null && mN.icon != 0) {
5036 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
5037 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005038 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Sunny Goyal5b153922017-09-21 21:00:36 -07005039 contentView.setInt(R.id.icon, "setImageLevel", mN.iconLevel);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005040 processSmallIconColor(mN.mSmallIcon, contentView, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005041 }
5042
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005043 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005044 * @return true if the built notification will show the time or the chronometer; false
5045 * otherwise
5046 */
5047 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07005048 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005049 }
5050
Christoph Studerfe718432014-09-01 18:21:18 +02005051 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07005052 // actions_container is only reset when there are no actions to avoid focus issues with
5053 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02005054 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02005055 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08005056
5057 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
5058 big.setTextViewText(R.id.notification_material_reply_text_1, null);
Kenny Guya0f6de82018-04-06 16:20:16 +01005059 big.setViewVisibility(R.id.notification_material_reply_text_1_container, View.GONE);
5060 big.setViewVisibility(R.id.notification_material_reply_progress, View.GONE);
Adrian Roose458aa82015-12-08 16:17:19 -08005061
5062 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
5063 big.setTextViewText(R.id.notification_material_reply_text_2, null);
5064 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
5065 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07005066
Selim Cineked64a142018-02-06 18:06:01 -08005067 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
5068 R.dimen.notification_content_margin);
Christoph Studerfe718432014-09-01 18:21:18 +02005069 }
5070
Selim Cinek384804b2018-04-18 14:31:07 +08005071 private RemoteViews applyStandardTemplateWithActions(int layoutId,
5072 TemplateBindResult result) {
5073 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this),
5074 result);
Adrian Roos48d746a2016-04-12 14:57:28 -07005075 }
5076
Gustav Sennton1463d832018-11-06 16:12:48 +00005077 private static List<Notification.Action> filterOutContextualActions(
5078 List<Notification.Action> actions) {
5079 List<Notification.Action> nonContextualActions = new ArrayList<>();
5080 for (Notification.Action action : actions) {
Gustav Sennton005d7a02019-01-04 13:41:32 +00005081 if (!action.isContextual()) {
Gustav Sennton1463d832018-11-06 16:12:48 +00005082 nonContextualActions.add(action);
5083 }
5084 }
5085 return nonContextualActions;
5086 }
5087
Adrian Roos70d7aa32017-01-11 15:39:06 -08005088 private RemoteViews applyStandardTemplateWithActions(int layoutId,
Selim Cinek384804b2018-04-18 14:31:07 +08005089 StandardTemplateParams p, TemplateBindResult result) {
5090 RemoteViews big = applyStandardTemplate(layoutId, p, result);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005091
Christoph Studerfe718432014-09-01 18:21:18 +02005092 resetStandardTemplateWithActions(big);
5093
Adrian Roose458aa82015-12-08 16:17:19 -08005094 boolean validRemoteInput = false;
5095
Gustav Sennton1463d832018-11-06 16:12:48 +00005096 // In the UI contextual actions appear separately from the standard actions, so we
5097 // filter them out here.
5098 List<Notification.Action> nonContextualActions = filterOutContextualActions(mActions);
5099
5100 int N = nonContextualActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08005101 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005102 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005103 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08005104 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005105 big.setViewVisibility(R.id.actions, View.VISIBLE);
Selim Cineked64a142018-02-06 18:06:01 -08005106 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Daniel Sandler8680bf82012-05-15 16:52:52 -04005107 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005108 for (int i=0; i<N; i++) {
Gustav Sennton1463d832018-11-06 16:12:48 +00005109 Action action = nonContextualActions.get(i);
5110
Selim Cinekffcc7cf2018-02-06 17:43:51 -08005111 boolean actionHasValidInput = hasValidRemoteInput(action);
5112 validRemoteInput |= actionHasValidInput;
Adrian Roose458aa82015-12-08 16:17:19 -08005113
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005114 final RemoteViews button = generateActionButton(action, emphazisedMode, p);
Selim Cinek396caca2018-04-10 17:46:46 -07005115 if (actionHasValidInput && !emphazisedMode) {
Selim Cinekffcc7cf2018-02-06 17:43:51 -08005116 // Clear the drawable
5117 button.setInt(R.id.action0, "setBackgroundResource", 0);
5118 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005119 big.addView(R.id.actions, button);
5120 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07005121 } else {
5122 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005123 }
Adrian Roose458aa82015-12-08 16:17:19 -08005124
5125 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08005126 if (!p.ambient && validRemoteInput && replyText != null
Selim Cinekbee4e072018-05-21 22:06:43 -07005127 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])
5128 && p.maxRemoteInputHistory > 0) {
Kenny Guya0f6de82018-04-06 16:20:16 +01005129 boolean showSpinner = mN.extras.getBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER);
Adrian Roose458aa82015-12-08 16:17:19 -08005130 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Kenny Guya0f6de82018-04-06 16:20:16 +01005131 big.setViewVisibility(R.id.notification_material_reply_text_1_container,
5132 View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005133 big.setTextViewText(R.id.notification_material_reply_text_1,
5134 processTextSpans(replyText[0]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005135 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1, p);
Kenny Guya0f6de82018-04-06 16:20:16 +01005136 big.setViewVisibility(R.id.notification_material_reply_progress,
5137 showSpinner ? View.VISIBLE : View.GONE);
5138 big.setProgressIndeterminateTintList(
5139 R.id.notification_material_reply_progress,
5140 ColorStateList.valueOf(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005141 isColorized(p) ? getPrimaryTextColor(p) : resolveContrastColor(p)));
Adrian Roose458aa82015-12-08 16:17:19 -08005142
Selim Cinekbee4e072018-05-21 22:06:43 -07005143 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])
5144 && p.maxRemoteInputHistory > 1) {
Adrian Roose458aa82015-12-08 16:17:19 -08005145 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005146 big.setTextViewText(R.id.notification_material_reply_text_2,
5147 processTextSpans(replyText[1]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005148 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2, p);
Adrian Roose458aa82015-12-08 16:17:19 -08005149
Selim Cinekbee4e072018-05-21 22:06:43 -07005150 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])
5151 && p.maxRemoteInputHistory > 2) {
Adrian Roose458aa82015-12-08 16:17:19 -08005152 big.setViewVisibility(
5153 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005154 big.setTextViewText(R.id.notification_material_reply_text_3,
5155 processTextSpans(replyText[2]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005156 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3, p);
Adrian Roose458aa82015-12-08 16:17:19 -08005157 }
5158 }
5159 }
5160
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005161 return big;
5162 }
5163
Adrian Roose458aa82015-12-08 16:17:19 -08005164 private boolean hasValidRemoteInput(Action action) {
5165 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
5166 // Weird actions
5167 return false;
5168 }
5169
5170 RemoteInput[] remoteInputs = action.getRemoteInputs();
5171 if (remoteInputs == null) {
5172 return false;
5173 }
5174
5175 for (RemoteInput r : remoteInputs) {
5176 CharSequence[] choices = r.getChoices();
5177 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
5178 return true;
5179 }
5180 }
5181 return false;
5182 }
5183
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005184 /**
5185 * Construct a RemoteViews for the final 1U notification layout. In order:
5186 * 1. Custom contentView from the caller
5187 * 2. Style's proposed content view
5188 * 3. Standard template view
5189 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005190 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005191 return createContentView(false /* increasedheight */ );
5192 }
5193
5194 /**
5195 * Construct a RemoteViews for the smaller content view.
5196 *
5197 * @param increasedHeight true if this layout be created with an increased height. Some
5198 * styles may support showing more then just that basic 1U size
5199 * and the system may decide to render important notifications
5200 * slightly bigger even when collapsed.
5201 *
5202 * @hide
5203 */
5204 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005205 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005206 return mN.contentView;
5207 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005208 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005209 if (styleView != null) {
5210 return styleView;
5211 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005212 }
Selim Cinek384804b2018-04-18 14:31:07 +08005213 return applyStandardTemplate(getBaseLayoutResource(), null /* result */);
Joe Onorato46439ce2010-11-19 13:56:21 -08005214 }
5215
Selim Cineka7679b62017-05-10 16:33:25 -07005216 private boolean useExistingRemoteView() {
5217 return mStyle == null || (!mStyle.displayCustomViewInline()
5218 && !mRebuildStyledRemoteViews);
5219 }
5220
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005221 /**
5222 * Construct a RemoteViews for the final big notification layout.
5223 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005224 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05005225 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07005226 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005227 return mN.bigContentView;
5228 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05005229 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08005230 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005231 } else if (mActions.size() != 0) {
Selim Cinek384804b2018-04-18 14:31:07 +08005232 result = applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5233 null /* result */);
Selim Cinek850a8542015-11-11 11:48:36 -05005234 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08005235 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05005236 return result;
5237 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005238
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005239 /**
Selim Cinek414ad332017-02-24 19:06:12 -08005240 * Construct a RemoteViews for the final notification header only. This will not be
5241 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005242 *
Adrian Roos6f6e1592017-05-02 16:22:53 -07005243 * @param ambient if true, generate the header for the ambient display layout.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005244 * @hide
5245 */
Adrian Roos6f6e1592017-05-02 16:22:53 -07005246 public RemoteViews makeNotificationHeader(boolean ambient) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005247 return makeNotificationHeader(mParams.reset().ambient(ambient).fillTextsFrom(this));
5248 }
5249
5250 /**
5251 * Construct a RemoteViews for the final notification header only. This will not be
5252 * colorized.
5253 *
5254 * @param p the template params to inflate this with
5255 */
5256 private RemoteViews makeNotificationHeader(StandardTemplateParams p) {
5257 // Headers on their own are never colorized
5258 p.disallowColorization();
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005259 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005260 p.ambient ? R.layout.notification_template_ambient_header
Adrian Roos6f6e1592017-05-02 16:22:53 -07005261 : R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005262 resetNotificationHeader(header);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005263 bindNotificationHeader(header, p);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005264 return header;
5265 }
5266
Adrian Roos487374f2017-01-11 15:48:14 -08005267 /**
5268 * Construct a RemoteViews for the ambient version of the notification.
5269 *
5270 * @hide
5271 */
5272 public RemoteViews makeAmbientNotification() {
5273 RemoteViews ambient = applyStandardTemplateWithActions(
5274 R.layout.notification_template_material_ambient,
Selim Cinek384804b2018-04-18 14:31:07 +08005275 mParams.reset().ambient(true).fillTextsFrom(this).hasProgress(false),
5276 null /* result */);
Adrian Roos487374f2017-01-11 15:48:14 -08005277 return ambient;
5278 }
5279
Selim Cinek29603462015-11-17 19:04:39 -08005280 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005281 if (result != null) {
5282 result.setViewVisibility(R.id.text_line_1, View.GONE);
5283 }
Selim Cinek29603462015-11-17 19:04:39 -08005284 }
5285
Selim Cinek6743c0b2017-01-18 18:24:01 -08005286 /**
5287 * Adapt the Notification header if this view is used as an expanded view.
5288 *
5289 * @hide
5290 */
5291 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005292 if (result != null) {
5293 result.setBoolean(R.id.notification_header, "setExpanded", true);
5294 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005295 }
5296
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005297 /**
5298 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005299 *
5300 * @param increasedHeight true if this layout be created with an increased height. Some
5301 * styles may support showing more then just that basic 1U size
5302 * and the system may decide to render important notifications
5303 * slightly bigger even when collapsed.
5304 *
5305 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005306 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005307 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005308 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005309 return mN.headsUpContentView;
5310 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08005311 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
5312 if (styleView != null) {
5313 return styleView;
5314 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05005315 } else if (mActions.size() == 0) {
5316 return null;
5317 }
5318
Selim Cinekbee4e072018-05-21 22:06:43 -07005319 // We only want at most a single remote input history to be shown here, otherwise
5320 // the content would become squished.
5321 StandardTemplateParams p = mParams.reset().fillTextsFrom(this)
5322 .setMaxRemoteInputHistory(1);
5323 return applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5324 p,
5325 null /* result */);
Chris Wren8fd39ec2014-02-27 17:43:26 -05005326 }
5327
Selim Cinek624c02db2015-12-14 21:00:02 -08005328 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08005329 * Construct a RemoteViews for the final heads-up notification layout.
5330 */
5331 public RemoteViews createHeadsUpContentView() {
5332 return createHeadsUpContentView(false /* useIncreasedHeight */);
5333 }
5334
5335 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08005336 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5337 *
5338 * @hide
5339 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005340 @UnsupportedAppUsage
Selim Cinek624c02db2015-12-14 21:00:02 -08005341 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005342 return makePublicView(false /* ambient */);
5343 }
5344
5345 /**
5346 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5347 *
5348 * @hide
5349 */
5350 public RemoteViews makePublicAmbientNotification() {
5351 return makePublicView(true /* ambient */);
5352 }
5353
5354 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08005355 if (mN.publicVersion != null) {
5356 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005357 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08005358 }
5359 Bundle savedBundle = mN.extras;
5360 Style style = mStyle;
5361 mStyle = null;
5362 Icon largeIcon = mN.mLargeIcon;
5363 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07005364 Bitmap largeIconLegacy = mN.largeIcon;
5365 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005366 ArrayList<Action> actions = mActions;
5367 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08005368 Bundle publicExtras = new Bundle();
5369 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
5370 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
5371 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
5372 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07005373 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
5374 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cineked07b962018-04-30 14:39:35 -07005375 String appName = savedBundle.getString(EXTRA_SUBSTITUTE_APP_NAME);
5376 if (appName != null) {
5377 publicExtras.putString(EXTRA_SUBSTITUTE_APP_NAME, appName);
5378 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005379 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07005380 RemoteViews view;
5381 if (ambient) {
5382 publicExtras.putCharSequence(EXTRA_TITLE,
5383 mContext.getString(com.android.internal.R.string.notification_hidden_text));
5384 view = makeAmbientNotification();
5385 } else{
5386 view = makeNotificationHeader(false /* ambient */);
5387 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
5388 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005389 mN.extras = savedBundle;
5390 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07005391 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005392 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08005393 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005394 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08005395 }
5396
Selim Cinek6743c0b2017-01-18 18:24:01 -08005397 /**
5398 * Construct a content view for the display when low - priority
5399 *
5400 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
5401 * a new subtext is created consisting of the content of the
5402 * notification.
5403 * @hide
5404 */
5405 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005406 StandardTemplateParams p = mParams.reset()
5407 .forceDefaultColor()
5408 .ambient(false)
5409 .fillTextsFrom(this);
5410 if (!useRegularSubtext || TextUtils.isEmpty(mParams.summaryText)) {
5411 p.summaryText(createSummaryText());
Selim Cinek6743c0b2017-01-18 18:24:01 -08005412 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005413 RemoteViews header = makeNotificationHeader(p);
Selim Cinek1b554392017-02-28 17:22:49 -08005414 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08005415 return header;
5416 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005417
Selim Cinek6743c0b2017-01-18 18:24:01 -08005418 private CharSequence createSummaryText() {
5419 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
5420 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
5421 return titleText;
5422 }
5423 SpannableStringBuilder summary = new SpannableStringBuilder();
5424 if (titleText == null) {
5425 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
5426 }
5427 BidiFormatter bidi = BidiFormatter.getInstance();
5428 if (titleText != null) {
5429 summary.append(bidi.unicodeWrap(titleText));
5430 }
5431 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
5432 if (titleText != null && contentText != null) {
5433 summary.append(bidi.unicodeWrap(mContext.getText(
5434 R.string.notification_header_divider_symbol_with_spaces)));
5435 }
5436 if (contentText != null) {
5437 summary.append(bidi.unicodeWrap(contentText));
5438 }
5439 return summary;
5440 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05005441
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005442 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005443 StandardTemplateParams p) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04005444 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07005445 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005446 emphazisedMode ? getEmphasizedActionLayoutResource()
5447 : tombstone ? getActionTombstoneLayoutResource()
5448 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04005449 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04005450 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04005451 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005452 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005453 if (action.mRemoteInputs != null) {
5454 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
5455 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005456 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07005457 // change the background bgColor
Selim Cinek981962e2016-07-20 20:41:58 -07005458 CharSequence title = action.title;
5459 ColorStateList[] outResultColor = null;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005460 int background = resolveBackgroundColor(p);
Selim Cinek981962e2016-07-20 20:41:58 -07005461 if (isLegacy()) {
Lucas Dupina291d192018-06-07 13:59:42 -07005462 title = ContrastColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07005463 } else {
5464 outResultColor = new ColorStateList[1];
Selim Cinek396caca2018-04-10 17:46:46 -07005465 title = ensureColorSpanContrast(title, background, outResultColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005466 }
Selim Cinek48f66b72017-08-18 16:17:51 -07005467 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005468 setTextViewColorPrimary(button, R.id.action0, p);
Selim Cinek396caca2018-04-10 17:46:46 -07005469 int rippleColor;
5470 boolean hasColorOverride = outResultColor != null && outResultColor[0] != null;
5471 if (hasColorOverride) {
5472 // There's a span spanning the full text, let's take it and use it as the
5473 // background color
5474 background = outResultColor[0].getDefaultColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005475 int textColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07005476 background, mInNightMode);
Selim Cinek396caca2018-04-10 17:46:46 -07005477 button.setTextColor(R.id.action0, textColor);
5478 rippleColor = textColor;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005479 } else if (getRawColor(p) != COLOR_DEFAULT && !isColorized(p)
5480 && mTintActionButtons) {
5481 rippleColor = resolveContrastColor(p);
Selim Cinek396caca2018-04-10 17:46:46 -07005482 button.setTextColor(R.id.action0, rippleColor);
5483 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005484 rippleColor = getPrimaryTextColor(p);
Selim Cinek981962e2016-07-20 20:41:58 -07005485 }
Selim Cinek396caca2018-04-10 17:46:46 -07005486 // We only want about 20% alpha for the ripple
5487 rippleColor = (rippleColor & 0x00ffffff) | 0x33000000;
5488 button.setColorStateList(R.id.action0, "setRippleColor",
5489 ColorStateList.valueOf(rippleColor));
5490 button.setColorStateList(R.id.action0, "setButtonBackground",
5491 ColorStateList.valueOf(background));
5492 button.setBoolean(R.id.action0, "setHasStroke", !hasColorOverride);
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005493 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07005494 button.setTextViewText(R.id.action0, processTextSpans(
5495 processLegacyText(action.title)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005496 if (isColorized(p)) {
5497 setTextViewColorPrimary(button, R.id.action0, p);
5498 } else if (getRawColor(p) != COLOR_DEFAULT && mTintActionButtons) {
Adrian Roos487374f2017-01-11 15:48:14 -08005499 button.setTextColor(R.id.action0,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005500 p.ambient ? resolveAmbientColor(p) : resolveContrastColor(p));
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005501 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005502 }
Tony Mak7d4b3a52018-11-27 17:29:36 +00005503 button.setIntTag(R.id.action0, R.id.notification_action_index_tag,
5504 mActions.indexOf(action));
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005505 return button;
5506 }
5507
Joe Onoratocb109a02011-01-18 17:57:41 -08005508 /**
Selim Cinek981962e2016-07-20 20:41:58 -07005509 * Ensures contrast on color spans against a background color. also returns the color of the
5510 * text if a span was found that spans over the whole text.
5511 *
5512 * @param charSequence the charSequence on which the spans are
5513 * @param background the background color to ensure the contrast against
5514 * @param outResultColor an array in which a color will be returned as the first element if
5515 * there exists a full length color span.
5516 * @return the contrasted charSequence
5517 */
5518 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
5519 ColorStateList[] outResultColor) {
5520 if (charSequence instanceof Spanned) {
5521 Spanned ss = (Spanned) charSequence;
5522 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
5523 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
5524 for (Object span : spans) {
5525 Object resultSpan = span;
5526 int spanStart = ss.getSpanStart(span);
5527 int spanEnd = ss.getSpanEnd(span);
5528 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
5529 if (resultSpan instanceof CharacterStyle) {
5530 resultSpan = ((CharacterStyle) span).getUnderlying();
5531 }
5532 if (resultSpan instanceof TextAppearanceSpan) {
5533 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
5534 ColorStateList textColor = originalSpan.getTextColor();
5535 if (textColor != null) {
5536 int[] colors = textColor.getColors();
5537 int[] newColors = new int[colors.length];
5538 for (int i = 0; i < newColors.length; i++) {
Lucas Dupina291d192018-06-07 13:59:42 -07005539 newColors[i] = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005540 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005541 }
5542 textColor = new ColorStateList(textColor.getStates().clone(),
5543 newColors);
Selim Cinek396caca2018-04-10 17:46:46 -07005544 if (fullLength) {
5545 outResultColor[0] = textColor;
5546 // Let's drop the color from the span
5547 textColor = null;
5548 }
Selim Cinek981962e2016-07-20 20:41:58 -07005549 resultSpan = new TextAppearanceSpan(
5550 originalSpan.getFamily(),
5551 originalSpan.getTextStyle(),
5552 originalSpan.getTextSize(),
5553 textColor,
5554 originalSpan.getLinkTextColor());
Selim Cinek981962e2016-07-20 20:41:58 -07005555 }
5556 } else if (resultSpan instanceof ForegroundColorSpan) {
5557 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
5558 int foregroundColor = originalSpan.getForegroundColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005559 foregroundColor = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005560 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005561 if (fullLength) {
5562 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
Selim Cinek396caca2018-04-10 17:46:46 -07005563 resultSpan = null;
5564 } else {
5565 resultSpan = new ForegroundColorSpan(foregroundColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005566 }
5567 } else {
5568 resultSpan = span;
5569 }
Selim Cinek396caca2018-04-10 17:46:46 -07005570 if (resultSpan != null) {
5571 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
5572 }
Selim Cinek981962e2016-07-20 20:41:58 -07005573 }
5574 return builder;
5575 }
5576 return charSequence;
5577 }
5578
5579 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005580 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07005581 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005582 */
5583 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005584 if (!mIsLegacyInitialized) {
5585 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
5586 < Build.VERSION_CODES.LOLLIPOP;
5587 mIsLegacyInitialized = true;
5588 }
5589 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005590 }
5591
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005592 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08005593 return processLegacyText(charSequence, false /* ambient */);
5594 }
5595
5596 private CharSequence processLegacyText(CharSequence charSequence, boolean ambient) {
5597 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
5598 boolean wantLightText = ambient;
5599 if (isAlreadyLightText != wantLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005600 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005601 } else {
5602 return charSequence;
5603 }
5604 }
5605
Dan Sandler26e81cf2014-05-06 10:01:27 -04005606 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005607 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04005608 */
Adrian Roos487374f2017-01-11 15:48:14 -08005609 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005610 StandardTemplateParams p) {
Selim Cinekea4bef72015-12-02 15:51:10 -08005611 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005612 int color;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005613 if (p.ambient) {
5614 color = resolveAmbientColor(p);
5615 } else if (isColorized(p)) {
5616 color = getPrimaryTextColor(p);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005617 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005618 color = resolveContrastColor(p);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005619 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005620 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07005621 contentView.setDrawableTint(R.id.icon, false, color,
5622 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005623
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005624 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005625 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08005626 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005627 }
5628
Dan Sandler26e81cf2014-05-06 10:01:27 -04005629 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005630 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04005631 * if it's grayscale).
5632 */
5633 // TODO: also check bounds, transparency, that sort of thing.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005634 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView,
5635 StandardTemplateParams p) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04005636 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005637 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005638 // resolve color will fall back to the default when legacy
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005639 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(p),
Sunny Goyal5b153922017-09-21 21:00:36 -07005640 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01005641 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005642 }
5643
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05005644 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005645 if (mN.color != COLOR_DEFAULT) {
5646 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02005647 }
Jorim Jaggi74419312014-06-10 20:57:21 +02005648 }
5649
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005650 int resolveContrastColor(StandardTemplateParams p) {
5651 int rawColor = getRawColor(p);
5652 if (mCachedContrastColorIsFor == rawColor && mCachedContrastColor != COLOR_INVALID) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08005653 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005654 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005655
Selim Cinekac5f0272017-05-02 16:05:41 -07005656 int color;
Selim Cinekc7f5a822018-03-20 19:32:06 -07005657 int background = mContext.getColor(
5658 com.android.internal.R.color.notification_material_background_color);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005659 if (rawColor == COLOR_DEFAULT) {
5660 ensureColors(p);
Lucas Dupinf03e7522018-06-25 16:21:13 -07005661 color = ContrastColorUtil.resolveDefaultColor(mContext, background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005662 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005663 color = ContrastColorUtil.resolveContrastColor(mContext, rawColor,
Anthony Chenad4d1582017-04-10 16:07:58 -07005664 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005665 }
5666 if (Color.alpha(color) < 255) {
5667 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005668 color = ContrastColorUtil.compositeColors(color, background);
Selim Cinekac5f0272017-05-02 16:05:41 -07005669 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005670 mCachedContrastColorIsFor = rawColor;
Selim Cinekac5f0272017-05-02 16:05:41 -07005671 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005672 }
5673
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005674 /**
5675 * Return the raw color of this Notification, which doesn't necessarily satisfy contrast.
5676 *
5677 * @see #resolveContrastColor(StandardTemplateParams) for the contrasted color
5678 * @param p the template params to inflate this with
5679 */
5680 private int getRawColor(StandardTemplateParams p) {
5681 if (p.forceDefaultColor) {
5682 return COLOR_DEFAULT;
5683 }
5684 return mN.color;
5685 }
5686
Selim Cinek4717d862018-04-19 09:19:15 +08005687 int resolveNeutralColor() {
5688 if (mNeutralColor != COLOR_INVALID) {
5689 return mNeutralColor;
5690 }
5691 int background = mContext.getColor(
5692 com.android.internal.R.color.notification_material_background_color);
Lucas Dupinf03e7522018-06-25 16:21:13 -07005693 mNeutralColor = ContrastColorUtil.resolveDefaultColor(mContext, background,
5694 mInNightMode);
Selim Cinek4717d862018-04-19 09:19:15 +08005695 if (Color.alpha(mNeutralColor) < 255) {
5696 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005697 mNeutralColor = ContrastColorUtil.compositeColors(mNeutralColor, background);
Selim Cinek4717d862018-04-19 09:19:15 +08005698 }
5699 return mNeutralColor;
5700 }
5701
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005702 int resolveAmbientColor(StandardTemplateParams p) {
5703 int rawColor = getRawColor(p);
5704 if (mCachedAmbientColorIsFor == rawColor && mCachedAmbientColorIsFor != COLOR_INVALID) {
Adrian Roos487374f2017-01-11 15:48:14 -08005705 return mCachedAmbientColor;
5706 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005707 final int contrasted = ContrastColorUtil.resolveAmbientColor(mContext, rawColor);
Adrian Roos487374f2017-01-11 15:48:14 -08005708
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005709 mCachedAmbientColorIsFor = rawColor;
Adrian Roos487374f2017-01-11 15:48:14 -08005710 return mCachedAmbientColor = contrasted;
5711 }
5712
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005713 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005714 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005715 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08005716 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005717 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005718 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005719 mN.actions = new Action[mActions.size()];
5720 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005721 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005722 if (!mPersonList.isEmpty()) {
Selim Cineke7238dd2017-12-14 17:48:32 -08005723 mN.extras.putParcelableArrayList(EXTRA_PEOPLE_LIST, mPersonList);
Dan Sandler0bf2ed82013-12-21 23:33:41 -06005724 }
Selim Cinek247fa012016-02-18 09:50:48 -08005725 if (mN.bigContentView != null || mN.contentView != null
5726 || mN.headsUpContentView != null) {
5727 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
5728 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005729 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08005730 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005731
Julia Reynolds3b848122016-02-26 10:45:32 -05005732 /**
5733 * Creates a Builder from an existing notification so further changes can be made.
5734 * @param context The context for your application / activity.
5735 * @param n The notification to create a Builder from.
5736 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005737 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005738 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005739 ApplicationInfo applicationInfo = n.extras.getParcelable(
5740 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005741 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05005742 if (applicationInfo != null) {
5743 try {
5744 builderContext = context.createApplicationContext(applicationInfo,
5745 Context.CONTEXT_RESTRICTED);
5746 } catch (NameNotFoundException e) {
5747 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
5748 builderContext = context; // try with our context
5749 }
5750 } else {
5751 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02005752 }
5753
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005754 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005755 }
5756
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005757 /**
Gustav Sennton761884c2018-11-19 17:40:19 +00005758 * Determines whether the platform can generate contextual actions for a notification.
5759 * By default this is true.
5760 */
5761 public Builder setAllowSystemGeneratedContextualActions(boolean allowed) {
5762 mN.mAllowSystemGeneratedContextualActions = allowed;
5763 return this;
5764 }
5765
5766 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005767 * @deprecated Use {@link #build()} instead.
5768 */
5769 @Deprecated
5770 public Notification getNotification() {
5771 return build();
5772 }
5773
5774 /**
5775 * Combine all of the options that have been set and return a new {@link Notification}
5776 * object.
5777 */
5778 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005779 // first, add any extras from the calling code
5780 if (mUserExtras != null) {
5781 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005782 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005783
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005784 mN.creationTime = System.currentTimeMillis();
5785
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005786 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05005787 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02005788
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005789 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005790
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005791 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02005792 mStyle.reduceImageSizes(mContext);
5793 mStyle.purgeResources();
Selim Cinek90343862018-02-01 11:07:11 -08005794 mStyle.validate(mContext);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005795 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005796 }
Selim Cinekd0426622017-07-11 13:19:59 +02005797 mN.reduceImageSizes(mContext);
5798
Adrian Roos5081c0d2016-02-26 16:04:19 -08005799 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07005800 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005801 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005802 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005803 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
5804 mN.contentView.getSequenceNumber());
5805 }
5806 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005807 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005808 if (mN.bigContentView != null) {
5809 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
5810 mN.bigContentView.getSequenceNumber());
5811 }
5812 }
5813 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005814 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005815 if (mN.headsUpContentView != null) {
5816 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
5817 mN.headsUpContentView.getSequenceNumber());
5818 }
5819 }
5820 }
5821
Julia Reynolds4c0c2022016-02-02 15:11:59 -05005822 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
5823 mN.flags |= FLAG_SHOW_LIGHTS;
5824 }
5825
Adrian Roosfb921842017-10-26 14:49:56 +02005826 mN.allPendingIntents = null;
5827
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005828 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005829 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005830
5831 /**
5832 * Apply this Builder to an existing {@link Notification} object.
5833 *
5834 * @hide
5835 */
5836 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005837 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005838 return n;
5839 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005840
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005841 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005842 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005843 * change. Also removes extenders on low ram devices, as
5844 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005845 *
5846 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5847 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005848 * @hide
5849 */
Kristian Monsen05f34792018-04-09 10:27:16 +02005850 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam,
5851 Context context) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005852 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005853
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005854 // Only strip views for known Styles because we won't know how to
5855 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005856 if (!isLowRam
5857 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005858 && getNotificationStyleClass(templateClass) == null) {
5859 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005860 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005861
5862 // Only strip unmodified BuilderRemoteViews.
5863 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005864 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005865 n.contentView.getSequenceNumber();
5866 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005867 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005868 n.bigContentView.getSequenceNumber();
5869 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005870 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005871 n.headsUpContentView.getSequenceNumber();
5872
5873 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005874 if (!isLowRam
5875 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005876 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005877 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005878
5879 Notification clone = n.clone();
5880 if (stripContentView) {
5881 clone.contentView = null;
5882 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5883 }
5884 if (stripBigContentView) {
5885 clone.bigContentView = null;
5886 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5887 }
5888 if (stripHeadsUpContentView) {
5889 clone.headsUpContentView = null;
5890 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5891 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005892 if (isLowRam) {
Kristian Monsen05f34792018-04-09 10:27:16 +02005893 String[] allowedServices = context.getResources().getStringArray(
5894 R.array.config_allowedManagedServicesOnLowRamDevices);
5895 if (allowedServices.length == 0) {
5896 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5897 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5898 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5899 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005900 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005901 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005902 }
5903
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005904 @UnsupportedAppUsage
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005905 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005906 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005907 }
5908
5909 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005910 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005911 }
5912
5913 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005914 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005915 }
5916
5917 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005918 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005919 }
5920
5921 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005922 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005923 }
5924
Adrian Roosc1a80b02016-04-05 14:54:55 -07005925 private int getMessagingLayoutResource() {
5926 return R.layout.notification_template_material_messaging;
5927 }
5928
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005929 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005930 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005931 }
5932
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005933 private int getEmphasizedActionLayoutResource() {
5934 return R.layout.notification_material_action_emphasized;
5935 }
5936
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005937 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005938 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005939 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005940
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005941 private int getBackgroundColor(StandardTemplateParams p) {
5942 if (isColorized(p)) {
5943 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : getRawColor(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005944 } else {
Selim Cinekc7f5a822018-03-20 19:32:06 -07005945 return COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005946 }
5947 }
5948
Selim Cinek396caca2018-04-10 17:46:46 -07005949 /**
Selim Cinek4717d862018-04-19 09:19:15 +08005950 * Gets a neutral color that can be used for icons or similar that should not stand out.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005951 * @param p the template params to inflate this with
Selim Cinek4717d862018-04-19 09:19:15 +08005952 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005953 private int getNeutralColor(StandardTemplateParams p) {
5954 if (isColorized(p)) {
5955 return getSecondaryTextColor(p);
Selim Cinek4717d862018-04-19 09:19:15 +08005956 } else {
5957 return resolveNeutralColor();
5958 }
5959 }
5960
5961 /**
Selim Cinek396caca2018-04-10 17:46:46 -07005962 * Same as getBackgroundColor but also resolved the default color to the background.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005963 * @param p the template params to inflate this with
Selim Cinek396caca2018-04-10 17:46:46 -07005964 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005965 private int resolveBackgroundColor(StandardTemplateParams p) {
5966 int backgroundColor = getBackgroundColor(p);
Selim Cinek396caca2018-04-10 17:46:46 -07005967 if (backgroundColor == COLOR_DEFAULT) {
5968 backgroundColor = mContext.getColor(
5969 com.android.internal.R.color.notification_material_background_color);
5970 }
5971 return backgroundColor;
5972 }
5973
Anthony Chenad4d1582017-04-10 16:07:58 -07005974 private boolean shouldTintActionButtons() {
5975 return mTintActionButtons;
5976 }
5977
Selim Cinek99104832017-01-25 14:47:33 -08005978 private boolean textColorsNeedInversion() {
5979 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
5980 return false;
5981 }
5982 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
5983 return targetSdkVersion > Build.VERSION_CODES.M
5984 && targetSdkVersion < Build.VERSION_CODES.O;
5985 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005986
5987 /**
5988 * Set a color palette to be used as the background and textColors
5989 *
5990 * @param backgroundColor the color to be used as the background
5991 * @param foregroundColor the color to be used as the foreground
5992 *
5993 * @hide
5994 */
5995 public void setColorPalette(int backgroundColor, int foregroundColor) {
5996 mBackgroundColor = backgroundColor;
5997 mForegroundColor = foregroundColor;
5998 mTextColorsAreForBackground = COLOR_INVALID;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005999 ensureColors(mParams.reset().fillTextsFrom(this));
Selim Cinek5fb73f82017-04-20 16:55:38 -07006000 }
Selim Cinekac5f0272017-05-02 16:05:41 -07006001
6002 /**
Selim Cineka7679b62017-05-10 16:33:25 -07006003 * Forces all styled remoteViews to be built from scratch and not use any cached
6004 * RemoteViews.
6005 * This is needed for legacy apps that are baking in their remoteviews into the
6006 * notification.
6007 *
6008 * @hide
6009 */
6010 public void setRebuildStyledRemoteViews(boolean rebuild) {
6011 mRebuildStyledRemoteViews = rebuild;
6012 }
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006013
6014 /**
6015 * Get the text that should be displayed in the statusBar when heads upped. This is
6016 * usually just the app name, but may be different depending on the style.
6017 *
6018 * @param publicMode If true, return a text that is safe to display in public.
6019 *
6020 * @hide
6021 */
6022 public CharSequence getHeadsUpStatusBarText(boolean publicMode) {
6023 if (mStyle != null && !publicMode) {
6024 CharSequence text = mStyle.getHeadsUpStatusBarText();
6025 if (!TextUtils.isEmpty(text)) {
6026 return text;
6027 }
6028 }
6029 return loadHeaderAppName();
6030 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08006031 }
6032
6033 /**
Selim Cinekd0426622017-07-11 13:19:59 +02006034 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
6035 * remote views.
6036 *
6037 * @hide
6038 */
6039 void reduceImageSizes(Context context) {
6040 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
6041 return;
6042 }
Selim Cineka8cb1262017-08-15 16:53:44 -07006043 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006044 if (mLargeIcon != null || largeIcon != null) {
6045 Resources resources = context.getResources();
6046 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07006047 int maxWidth = resources.getDimensionPixelSize(isLowRam
6048 ? R.dimen.notification_right_icon_size_low_ram
6049 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006050 int maxHeight = maxWidth;
6051 if (MediaStyle.class.equals(style)
6052 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006053 maxHeight = resources.getDimensionPixelSize(isLowRam
6054 ? R.dimen.notification_media_image_max_height_low_ram
6055 : R.dimen.notification_media_image_max_height);
6056 maxWidth = resources.getDimensionPixelSize(isLowRam
6057 ? R.dimen.notification_media_image_max_width_low_ram
6058 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006059 }
6060 if (mLargeIcon != null) {
6061 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
6062 }
6063 if (largeIcon != null) {
6064 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
6065 }
6066 }
Selim Cineka8cb1262017-08-15 16:53:44 -07006067 reduceImageSizesForRemoteView(contentView, context, isLowRam);
6068 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
6069 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02006070 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
6071 }
6072
Selim Cineka8cb1262017-08-15 16:53:44 -07006073 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
6074 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02006075 if (remoteView != null) {
6076 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006077 int maxWidth = resources.getDimensionPixelSize(isLowRam
6078 ? R.dimen.notification_custom_view_max_image_width_low_ram
6079 : R.dimen.notification_custom_view_max_image_width);
6080 int maxHeight = resources.getDimensionPixelSize(isLowRam
6081 ? R.dimen.notification_custom_view_max_image_height_low_ram
6082 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02006083 remoteView.reduceImageSizes(maxWidth, maxHeight);
6084 }
6085 }
6086
6087 /**
Selim Cinek22714f12017-04-13 16:23:53 -07006088 * @return whether this notification is a foreground service notification
Gus Prevasb0c1a462018-11-05 11:06:15 -05006089 * @hide
Selim Cinek7b9605b2017-01-19 17:36:00 -08006090 */
Gus Prevasb0c1a462018-11-05 11:06:15 -05006091 public boolean isForegroundService() {
Selim Cinek22714f12017-04-13 16:23:53 -07006092 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08006093 }
6094
6095 /**
Selim Cinek99104832017-01-25 14:47:33 -08006096 * @return whether this notification has a media session attached
6097 * @hide
6098 */
6099 public boolean hasMediaSession() {
6100 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
6101 }
6102
6103 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006104 * @return the style class of this notification
6105 * @hide
6106 */
6107 public Class<? extends Notification.Style> getNotificationStyle() {
6108 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
6109
6110 if (!TextUtils.isEmpty(templateClass)) {
6111 return Notification.getNotificationStyleClass(templateClass);
6112 }
6113 return null;
6114 }
6115
6116 /**
Selim Cinek22714f12017-04-13 16:23:53 -07006117 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08006118 *
6119 * @hide
6120 */
6121 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07006122 if (isColorizedMedia()) {
6123 return true;
6124 }
Julia Reynolds4db59552017-06-30 13:34:01 -04006125 return extras.getBoolean(EXTRA_COLORIZED)
6126 && (hasColorizedPermission() || isForegroundService());
6127 }
6128
6129 /**
6130 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
6131 * permission. The permission is checked when a notification is enqueued.
6132 */
6133 private boolean hasColorizedPermission() {
6134 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07006135 }
6136
6137 /**
6138 * @return true if this notification is colorized and it is a media notification
6139 *
6140 * @hide
6141 */
6142 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08006143 Class<? extends Style> style = getNotificationStyle();
6144 if (MediaStyle.class.equals(style)) {
6145 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
6146 if ((colorized == null || colorized) && hasMediaSession()) {
6147 return true;
6148 }
6149 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6150 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
6151 return true;
6152 }
6153 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07006154 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006155 }
6156
Selim Cinek0847acd2017-04-24 19:48:29 -07006157
6158 /**
6159 * @return true if this is a media notification
6160 *
6161 * @hide
6162 */
6163 public boolean isMediaNotification() {
6164 Class<? extends Style> style = getNotificationStyle();
6165 if (MediaStyle.class.equals(style)) {
6166 return true;
6167 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6168 return true;
6169 }
6170 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006171 }
6172
Selim Cinek279fa862016-06-14 10:57:25 -07006173 private boolean hasLargeIcon() {
6174 return mLargeIcon != null || largeIcon != null;
6175 }
6176
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006177 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006178 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006179 * @hide
6180 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07006181 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006182 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
6183 }
6184
6185 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006186 * @return true if the notification will show a chronometer; false otherwise
6187 * @hide
6188 */
6189 public boolean showsChronometer() {
6190 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
6191 }
6192
6193 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04006194 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05006195 */
6196 @SystemApi
6197 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
6198 Class<? extends Style>[] classes = new Class[] {
6199 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
6200 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
6201 MessagingStyle.class };
6202 for (Class<? extends Style> innerClass : classes) {
6203 if (templateClass.equals(innerClass.getName())) {
6204 return innerClass;
6205 }
6206 }
6207 return null;
6208 }
6209
6210 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006211 * An object that can apply a rich notification style to a {@link Notification.Builder}
6212 * object.
6213 */
Griff Hazendfcb0802014-02-11 12:00:00 -08006214 public static abstract class Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07006215
6216 /**
6217 * The number of items allowed simulatanously in the remote input history.
6218 * @hide
6219 */
6220 static final int MAX_REMOTE_INPUT_HISTORY_LINES = 3;
Chris Wrend6297db2012-05-03 16:20:13 -04006221 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006222
6223 /**
6224 * @hide
6225 */
6226 protected CharSequence mSummaryText = null;
6227
6228 /**
6229 * @hide
6230 */
6231 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04006232
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006233 protected Builder mBuilder;
6234
Chris Wrend6297db2012-05-03 16:20:13 -04006235 /**
6236 * Overrides ContentTitle in the big form of the template.
6237 * This defaults to the value passed to setContentTitle().
6238 */
6239 protected void internalSetBigContentTitle(CharSequence title) {
6240 mBigContentTitle = title;
6241 }
6242
6243 /**
6244 * Set the first line of text after the detail section in the big form of the template.
6245 */
6246 protected void internalSetSummaryText(CharSequence cs) {
6247 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04006248 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04006249 }
6250
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006251 public void setBuilder(Builder builder) {
6252 if (mBuilder != builder) {
6253 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07006254 if (mBuilder != null) {
6255 mBuilder.setStyle(this);
6256 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006257 }
6258 }
6259
Chris Wrend6297db2012-05-03 16:20:13 -04006260 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006261 if (mBuilder == null) {
6262 throw new IllegalArgumentException("Style requires a valid Builder object");
6263 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006264 }
Chris Wrend6297db2012-05-03 16:20:13 -04006265
6266 protected RemoteViews getStandardView(int layoutId) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006267 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder);
6268 return getStandardView(layoutId, p, null);
Selim Cinek384804b2018-04-18 14:31:07 +08006269 }
6270
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006271
Selim Cinek384804b2018-04-18 14:31:07 +08006272 /**
6273 * Get the standard view for this style.
6274 *
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006275 * @param layoutId The layout id to use.
6276 * @param p the params for this inflation.
Selim Cinek384804b2018-04-18 14:31:07 +08006277 * @param result The result where template bind information is saved.
6278 * @return A remoteView for this style.
6279 * @hide
6280 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006281 protected RemoteViews getStandardView(int layoutId, StandardTemplateParams p,
6282 TemplateBindResult result) {
Chris Wrend6297db2012-05-03 16:20:13 -04006283 checkBuilder();
6284
6285 if (mBigContentTitle != null) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006286 p.title = mBigContentTitle;
Chris Wrend6297db2012-05-03 16:20:13 -04006287 }
6288
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006289 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId, p,
6290 result);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006291
Chris Wrend6297db2012-05-03 16:20:13 -04006292 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
6293 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04006294 } else {
6295 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04006296 }
6297
Chris Wrend6297db2012-05-03 16:20:13 -04006298 return contentView;
6299 }
6300
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006301 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006302 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006303 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08006304 *
6305 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006306 * @hide
6307 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08006308 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006309 return null;
6310 }
6311
6312 /**
6313 * Construct a Style-specific RemoteViews for the final big notification layout.
6314 * @hide
6315 */
6316 public RemoteViews makeBigContentView() {
6317 return null;
6318 }
6319
6320 /**
6321 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08006322 *
6323 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006324 * @hide
6325 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006326 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006327 return null;
6328 }
6329
6330 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006331 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006332 * @hide
6333 */
6334 public void addExtras(Bundle extras) {
6335 if (mSummaryTextSet) {
6336 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
6337 }
6338 if (mBigContentTitle != null) {
6339 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
6340 }
Chris Wren91ad5632013-06-05 15:05:57 -04006341 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006342 }
6343
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006344 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006345 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006346 * @hide
6347 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02006348 protected void restoreFromExtras(Bundle extras) {
6349 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
6350 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
6351 mSummaryTextSet = true;
6352 }
6353 if (extras.containsKey(EXTRA_TITLE_BIG)) {
6354 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
6355 }
6356 }
6357
6358
6359 /**
6360 * @hide
6361 */
6362 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006363 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006364 return wip;
6365 }
6366
Daniel Sandler0ec46202015-06-24 01:27:05 -04006367 /**
6368 * @hide
6369 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006370 public void purgeResources() {}
6371
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006372 /**
6373 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
6374 * attached to.
6375 *
6376 * @return the fully constructed Notification.
6377 */
6378 public Notification build() {
6379 checkBuilder();
6380 return mBuilder.build();
6381 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006382
6383 /**
6384 * @hide
6385 * @return true if the style positions the progress bar on the second line; false if the
6386 * style hides the progress bar
6387 */
6388 protected boolean hasProgress() {
6389 return true;
6390 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006391
6392 /**
6393 * @hide
6394 * @return Whether we should put the summary be put into the notification header
6395 */
6396 public boolean hasSummaryInHeader() {
6397 return true;
6398 }
Selim Cinek593610c2016-02-16 18:42:57 -08006399
6400 /**
6401 * @hide
6402 * @return Whether custom content views are displayed inline in the style
6403 */
6404 public boolean displayCustomViewInline() {
6405 return false;
6406 }
Selim Cinekd0426622017-07-11 13:19:59 +02006407
6408 /**
6409 * Reduces the image sizes contained in this style.
6410 *
6411 * @hide
6412 */
6413 public void reduceImageSizes(Context context) {
6414 }
Selim Cinek90343862018-02-01 11:07:11 -08006415
6416 /**
6417 * Validate that this style was properly composed. This is called at build time.
6418 * @hide
6419 */
6420 public void validate(Context context) {
6421 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006422
6423 /**
6424 * @hide
6425 */
6426 public abstract boolean areNotificationsVisiblyDifferent(Style other);
Selim Cinekc7f5a822018-03-20 19:32:06 -07006427
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006428 /**
koprivaa1a78482018-10-09 10:09:23 -07006429 * @return the text that should be displayed in the statusBar when heads-upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006430 * If {@code null} is returned, the default implementation will be used.
6431 *
6432 * @hide
6433 */
6434 public CharSequence getHeadsUpStatusBarText() {
6435 return null;
6436 }
Joe Onorato46439ce2010-11-19 13:56:21 -08006437 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006438
6439 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006440 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08006441 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006442 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006443 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006444 * Notification notif = new Notification.Builder(mContext)
6445 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
6446 * .setContentText(subject)
6447 * .setSmallIcon(R.drawable.new_post)
6448 * .setLargeIcon(aBitmap)
6449 * .setStyle(new Notification.BigPictureStyle()
6450 * .bigPicture(aBigBitmap))
6451 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006452 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006453 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006454 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006455 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006456 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006457 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006458 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006459 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006460
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006461 public BigPictureStyle() {
6462 }
6463
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006464 /**
6465 * @deprecated use {@code BigPictureStyle()}.
6466 */
6467 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006468 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006469 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006470 }
6471
Chris Wrend6297db2012-05-03 16:20:13 -04006472 /**
6473 * Overrides ContentTitle in the big form of the template.
6474 * This defaults to the value passed to setContentTitle().
6475 */
6476 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006477 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006478 return this;
6479 }
6480
6481 /**
6482 * Set the first line of text after the detail section in the big form of the template.
6483 */
6484 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006485 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006486 return this;
6487 }
6488
Chris Wren0bd664d2012-08-01 13:56:56 -04006489 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05006490 * @hide
6491 */
6492 public Bitmap getBigPicture() {
6493 return mPicture;
6494 }
6495
6496 /**
Chris Wren0bd664d2012-08-01 13:56:56 -04006497 * Provide the bitmap to be used as the payload for the BigPicture notification.
6498 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006499 public BigPictureStyle bigPicture(Bitmap b) {
6500 mPicture = b;
6501 return this;
6502 }
6503
Chris Wren3745a3d2012-05-22 15:11:52 -04006504 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04006505 * Override the large icon when the big notification is shown.
6506 */
6507 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04006508 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
6509 }
6510
6511 /**
6512 * Override the large icon when the big notification is shown.
6513 */
6514 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04006515 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006516 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006517 return this;
6518 }
6519
Riley Andrews0394a0c2015-11-03 23:36:52 -08006520 /** @hide */
6521 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
6522
Daniel Sandler0ec46202015-06-24 01:27:05 -04006523 /**
6524 * @hide
6525 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006526 @Override
6527 public void purgeResources() {
6528 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08006529 if (mPicture != null &&
6530 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08006531 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006532 mPicture = mPicture.createAshmemBitmap();
6533 }
6534 if (mBigLargeIcon != null) {
6535 mBigLargeIcon.convertToAshmem();
6536 }
6537 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01006538
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006539 /**
6540 * @hide
6541 */
Selim Cinekd0426622017-07-11 13:19:59 +02006542 @Override
6543 public void reduceImageSizes(Context context) {
6544 super.reduceImageSizes(context);
6545 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006546 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006547 if (mPicture != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006548 int maxPictureWidth = resources.getDimensionPixelSize(isLowRam
6549 ? R.dimen.notification_big_picture_max_height_low_ram
6550 : R.dimen.notification_big_picture_max_height);
6551 int maxPictureHeight = resources.getDimensionPixelSize(isLowRam
6552 ? R.dimen.notification_big_picture_max_width_low_ram
6553 : R.dimen.notification_big_picture_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006554 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
6555 }
6556 if (mBigLargeIcon != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006557 int rightIconSize = resources.getDimensionPixelSize(isLowRam
6558 ? R.dimen.notification_right_icon_size_low_ram
6559 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006560 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
6561 }
6562 }
6563
6564 /**
6565 * @hide
6566 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006567 public RemoteViews makeBigContentView() {
6568 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01006569 // This covers the following cases:
6570 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006571 // mN.mLargeIcon
6572 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04006573 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07006574 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006575 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006576 oldLargeIcon = mBuilder.mN.mLargeIcon;
6577 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006578 // The legacy largeIcon might not allow us to clear the image, as it's taken in
6579 // replacement if the other one is null. Because we're restoring these legacy icons
6580 // for old listeners, this is in general non-null.
6581 largeIconLegacy = mBuilder.mN.largeIcon;
6582 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006583 }
6584
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006585 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder);
Selim Cinek384804b2018-04-18 14:31:07 +08006586 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource(),
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006587 p, null /* result */);
Selim Cinek03d0d652015-11-13 13:18:09 -05006588 if (mSummaryTextSet) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006589 contentView.setTextViewText(R.id.text, mBuilder.processTextSpans(
6590 mBuilder.processLegacyText(mSummaryText)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006591 mBuilder.setTextViewColorSecondary(contentView, R.id.text, p);
Selim Cinekc848c3a2016-01-13 15:27:30 -08006592 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05006593 }
Selim Cinek279fa862016-06-14 10:57:25 -07006594 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08006595
Christoph Studer5c510ee2014-12-15 16:32:27 +01006596 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006597 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006598 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006599 }
6600
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006601 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006602 return contentView;
6603 }
6604
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006605 /**
6606 * @hide
6607 */
6608 public void addExtras(Bundle extras) {
6609 super.addExtras(extras);
6610
6611 if (mBigLargeIconSet) {
6612 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
6613 }
6614 extras.putParcelable(EXTRA_PICTURE, mPicture);
6615 }
6616
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006617 /**
6618 * @hide
6619 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006620 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02006621 protected void restoreFromExtras(Bundle extras) {
6622 super.restoreFromExtras(extras);
6623
6624 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01006625 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02006626 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04006627 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006628 mPicture = extras.getParcelable(EXTRA_PICTURE);
6629 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006630
6631 /**
6632 * @hide
6633 */
6634 @Override
6635 public boolean hasSummaryInHeader() {
6636 return false;
6637 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006638
6639 /**
6640 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006641 * Note that we aren't actually comparing the contents of the bitmaps here, so this
6642 * is only doing a cursory inspection. Bitmaps of equal size will appear the same.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006643 */
6644 @Override
6645 public boolean areNotificationsVisiblyDifferent(Style other) {
6646 if (other == null || getClass() != other.getClass()) {
6647 return true;
6648 }
6649 BigPictureStyle otherS = (BigPictureStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006650 return areBitmapsObviouslyDifferent(getBigPicture(), otherS.getBigPicture());
6651 }
6652
6653 private static boolean areBitmapsObviouslyDifferent(Bitmap a, Bitmap b) {
6654 if (a == b) {
6655 return false;
6656 }
6657 if (a == null || b == null) {
6658 return true;
6659 }
6660 return a.getWidth() != b.getWidth()
6661 || a.getHeight() != b.getHeight()
6662 || a.getConfig() != b.getConfig()
6663 || a.getGenerationId() != b.getGenerationId();
Julia Reynolds7217dc92018-03-07 12:12:09 -05006664 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006665 }
6666
6667 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006668 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08006669 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006670 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006671 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006672 * Notification notif = new Notification.Builder(mContext)
6673 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
6674 * .setContentText(subject)
6675 * .setSmallIcon(R.drawable.new_mail)
6676 * .setLargeIcon(aBitmap)
6677 * .setStyle(new Notification.BigTextStyle()
6678 * .bigText(aVeryLongString))
6679 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006680 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006681 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006682 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006683 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006684 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006685
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006686 private CharSequence mBigText;
6687
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006688 public BigTextStyle() {
6689 }
6690
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006691 /**
6692 * @deprecated use {@code BigTextStyle()}.
6693 */
6694 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006695 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006696 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006697 }
6698
Chris Wrend6297db2012-05-03 16:20:13 -04006699 /**
6700 * Overrides ContentTitle in the big form of the template.
6701 * This defaults to the value passed to setContentTitle().
6702 */
6703 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006704 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006705 return this;
6706 }
6707
6708 /**
6709 * Set the first line of text after the detail section in the big form of the template.
6710 */
6711 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006712 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006713 return this;
6714 }
6715
Chris Wren0bd664d2012-08-01 13:56:56 -04006716 /**
6717 * Provide the longer text to be displayed in the big form of the
6718 * template in place of the content text.
6719 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006720 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006721 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006722 return this;
6723 }
6724
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006725 /**
6726 * @hide
6727 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05006728 public CharSequence getBigText() {
6729 return mBigText;
6730 }
6731
6732 /**
6733 * @hide
6734 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006735 public void addExtras(Bundle extras) {
6736 super.addExtras(extras);
6737
Christoph Studer4600f9b2014-07-22 22:44:43 +02006738 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
6739 }
6740
6741 /**
6742 * @hide
6743 */
6744 @Override
6745 protected void restoreFromExtras(Bundle extras) {
6746 super.restoreFromExtras(extras);
6747
6748 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006749 }
6750
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006751 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006752 * @param increasedHeight true if this layout be created with an increased height.
6753 *
6754 * @hide
6755 */
6756 @Override
6757 public RemoteViews makeContentView(boolean increasedHeight) {
6758 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006759 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006760 mBuilder.mActions = new ArrayList<>();
6761 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006762 mBuilder.mActions = mBuilder.mOriginalActions;
6763 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006764 return remoteViews;
6765 }
6766 return super.makeContentView(increasedHeight);
6767 }
6768
6769 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006770 * @hide
6771 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006772 @Override
6773 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
6774 if (increasedHeight && mBuilder.mActions.size() > 0) {
6775 return makeBigContentView();
6776 }
6777 return super.makeHeadsUpContentView(increasedHeight);
6778 }
6779
6780 /**
6781 * @hide
6782 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006783 public RemoteViews makeBigContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006784 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder).text(null);
Selim Cinek384804b2018-04-18 14:31:07 +08006785 TemplateBindResult result = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006786 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource(), p,
6787 result);
Selim Cinek1c72fa02018-04-23 18:00:54 +08006788 contentView.setInt(R.id.big_text, "setImageEndMargin", result.getIconMarginEnd());
Joe Malin8d40d042012-11-05 11:36:40 -08006789
Selim Cinek3a2c4b92015-12-17 17:01:17 -08006790 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07006791 if (TextUtils.isEmpty(bigTextText)) {
6792 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
6793 // experience
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006794 bigTextText = mBuilder.processLegacyText(
6795 mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT));
Selim Cinek75998782016-04-26 10:39:17 -07006796 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006797 contentView.setTextViewText(R.id.big_text, mBuilder.processTextSpans(bigTextText));
6798 mBuilder.setTextViewColorSecondary(contentView, R.id.big_text, p);
6799 contentView.setViewVisibility(R.id.big_text,
6800 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08006801 contentView.setBoolean(R.id.big_text, "setHasImage",
6802 result.isRightIconContainerVisible());
Selim Cinek4fb12d32015-11-19 18:10:48 -08006803
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006804 return contentView;
6805 }
6806
Julia Reynolds7217dc92018-03-07 12:12:09 -05006807 /**
6808 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006809 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006810 */
6811 @Override
6812 public boolean areNotificationsVisiblyDifferent(Style other) {
6813 if (other == null || getClass() != other.getClass()) {
6814 return true;
6815 }
6816 BigTextStyle newS = (BigTextStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006817 return !Objects.equals(String.valueOf(getBigText()), String.valueOf(newS.getBigText()));
Julia Reynolds7217dc92018-03-07 12:12:09 -05006818 }
6819
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006820 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006821
6822 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006823 * Helper class for generating large-format notifications that include multiple back-and-forth
6824 * messages of varying types between any number of people.
6825 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006826 * <p>
Alex Hillsfc737de2016-03-23 17:33:02 -04006827 * If the platform does not provide large-format notifications, this method has no effect. The
6828 * user will always see the normal notification view.
Selim Cinekce8794f2018-05-23 16:46:05 -07006829 *
6830 * <p>
6831 * If the app is targeting Android P and above, it is required to use the {@link Person}
6832 * class in order to get an optimal rendering of the notification and its avatars. For
6833 * conversations involving multiple people, the app should also make sure that it marks the
6834 * conversation as a group with {@link #setGroupConversation(boolean)}.
6835 *
6836 * <p>
6837 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior.
6838 * Here's an example of how this may be used:
Alex Hillsfc737de2016-03-23 17:33:02 -04006839 * <pre class="prettyprint">
6840 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006841 * Person user = new Person.Builder().setIcon(userIcon).setName(userName).build();
6842 * MessagingStyle style = new MessagingStyle(user)
6843 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getPerson())
6844 * .addMessage(messages[2].getText(), messages[2].getTime(), messages[2].getPerson())
6845 * .setGroupConversation(hasMultiplePeople());
6846 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006847 * Notification noti = new Notification.Builder()
Selim Cinekce8794f2018-05-23 16:46:05 -07006848 * .setContentTitle(&quot;2 new messages with &quot; + sender.toString())
Alex Hillsfc737de2016-03-23 17:33:02 -04006849 * .setContentText(subject)
6850 * .setSmallIcon(R.drawable.new_message)
6851 * .setLargeIcon(aBitmap)
Selim Cinekce8794f2018-05-23 16:46:05 -07006852 * .setStyle(style)
Alex Hillsfc737de2016-03-23 17:33:02 -04006853 * .build();
6854 * </pre>
6855 */
6856 public static class MessagingStyle extends Style {
6857
6858 /**
6859 * The maximum number of messages that will be retained in the Notification itself (the
6860 * number displayed is up to the platform).
6861 */
6862 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
6863
Selim Cinekcb8b9852017-12-15 18:01:52 -08006864 @NonNull Person mUser;
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006865 @Nullable CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04006866 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08006867 List<Message> mHistoricMessages = new ArrayList<>();
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006868 boolean mIsGroupConversation;
Alex Hillsfc737de2016-03-23 17:33:02 -04006869
6870 MessagingStyle() {
6871 }
6872
6873 /**
Alex Hillsfd590442016-10-07 09:52:44 -04006874 * @param userDisplayName Required - the name to be displayed for any replies sent by the
6875 * user before the posting app reposts the notification with those messages after they've
6876 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04006877 * {@link #addMessage(Notification.MessagingStyle.Message)}
Selim Cinekcb8b9852017-12-15 18:01:52 -08006878 *
6879 * @deprecated use {@code MessagingStyle(Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006880 */
Alex Hillsfd590442016-10-07 09:52:44 -04006881 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Selim Cinek9acd6732018-03-23 16:39:02 -07006882 this(new Person.Builder().setName(userDisplayName).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006883 }
6884
6885 /**
6886 * @param user Required - The person displayed for any messages that are sent by the
6887 * user. Any messages added with {@link #addMessage(Notification.MessagingStyle.Message)}
6888 * who don't have a Person associated with it will be displayed as if they were sent
Selim Cinek90343862018-02-01 11:07:11 -08006889 * by this user. The user also needs to have a valid name associated with it, which will
6890 * be enforced starting in Android P.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006891 */
6892 public MessagingStyle(@NonNull Person user) {
6893 mUser = user;
Selim Cinek90343862018-02-01 11:07:11 -08006894 }
6895
6896 /**
6897 * Validate that this style was properly composed. This is called at build time.
6898 * @hide
6899 */
6900 @Override
6901 public void validate(Context context) {
6902 super.validate(context);
6903 if (context.getApplicationInfo().targetSdkVersion
6904 >= Build.VERSION_CODES.P && (mUser == null || mUser.getName() == null)) {
6905 throw new RuntimeException("User must be valid and have a name.");
Selim Cinekcb8b9852017-12-15 18:01:52 -08006906 }
6907 }
6908
6909 /**
koprivaa1a78482018-10-09 10:09:23 -07006910 * @return the text that should be displayed in the statusBar when heads upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006911 * If {@code null} is returned, the default implementation will be used.
6912 *
6913 * @hide
6914 */
6915 @Override
6916 public CharSequence getHeadsUpStatusBarText() {
6917 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
6918 ? super.mBigContentTitle
6919 : mConversationTitle;
6920 if (!TextUtils.isEmpty(conversationTitle) && !hasOnlyWhiteSpaceSenders()) {
6921 return conversationTitle;
6922 }
6923 return null;
6924 }
6925
6926 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08006927 * @return the user to be displayed for any replies sent by the user
6928 */
Selim Cinekeb53c222018-04-06 12:56:09 -07006929 @NonNull
Selim Cinekcb8b9852017-12-15 18:01:52 -08006930 public Person getUser() {
6931 return mUser;
Alex Hillsfc737de2016-03-23 17:33:02 -04006932 }
6933
6934 /**
6935 * Returns the name to be displayed for any replies sent by the user
Selim Cinekcb8b9852017-12-15 18:01:52 -08006936 *
6937 * @deprecated use {@link #getUser()} instead
Alex Hillsfc737de2016-03-23 17:33:02 -04006938 */
6939 public CharSequence getUserDisplayName() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006940 return mUser.getName();
Alex Hillsfc737de2016-03-23 17:33:02 -04006941 }
6942
6943 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006944 * Sets the title to be displayed on this conversation. May be set to {@code null}.
6945 *
Kodlee Yin14656422017-12-22 17:00:46 -08006946 * <p>This API's behavior was changed in SDK version {@link Build.VERSION_CODES#P}. If your
6947 * application's target version is less than {@link Build.VERSION_CODES#P}, setting a
6948 * conversation title to a non-null value will make {@link #isGroupConversation()} return
6949 * {@code true} and passing {@code null} will make it return {@code false}. In
6950 * {@link Build.VERSION_CODES#P} and beyond, use {@link #setGroupConversation(boolean)}
6951 * to set group conversation status.
6952 *
6953 * @param conversationTitle Title displayed for this conversation
6954 * @return this object for method chaining
Alex Hillsfc737de2016-03-23 17:33:02 -04006955 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006956 public MessagingStyle setConversationTitle(@Nullable CharSequence conversationTitle) {
Alex Hillsfc737de2016-03-23 17:33:02 -04006957 mConversationTitle = conversationTitle;
6958 return this;
6959 }
6960
6961 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006962 * Return the title to be displayed on this conversation. May return {@code null}.
Alex Hillsfc737de2016-03-23 17:33:02 -04006963 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006964 @Nullable
Alex Hillsfc737de2016-03-23 17:33:02 -04006965 public CharSequence getConversationTitle() {
6966 return mConversationTitle;
6967 }
6968
6969 /**
6970 * Adds a message for display by this notification. Convenience call for a simple
6971 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6972 * @param text A {@link CharSequence} to be displayed as the message content
6973 * @param timestamp Time at which the message arrived
6974 * @param sender A {@link CharSequence} to be used for displaying the name of the
6975 * sender. Should be <code>null</code> for messages by the current user, in which case
6976 * the platform will insert {@link #getUserDisplayName()}.
6977 * Should be unique amongst all individuals in the conversation, and should be
6978 * consistent during re-posts of the notification.
6979 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07006980 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Alex Hillsfc737de2016-03-23 17:33:02 -04006981 *
6982 * @return this object for method chaining
Selim Cinekcb8b9852017-12-15 18:01:52 -08006983 *
6984 * @deprecated use {@link #addMessage(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006985 */
6986 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006987 return addMessage(text, timestamp,
Selim Cinek9acd6732018-03-23 16:39:02 -07006988 sender == null ? null : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006989 }
6990
6991 /**
6992 * Adds a message for display by this notification. Convenience call for a simple
6993 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6994 * @param text A {@link CharSequence} to be displayed as the message content
6995 * @param timestamp Time at which the message arrived
6996 * @param sender The {@link Person} who sent the message.
6997 * Should be <code>null</code> for messages by the current user, in which case
6998 * the platform will insert the user set in {@code MessagingStyle(Person)}.
6999 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07007000 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Selim Cinekcb8b9852017-12-15 18:01:52 -08007001 *
7002 * @return this object for method chaining
7003 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007004 public MessagingStyle addMessage(@NonNull CharSequence text, long timestamp,
7005 @Nullable Person sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08007006 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04007007 }
7008
7009 /**
7010 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08007011 *
7012 * <p>The messages should be added in chronologic order, i.e. the oldest first,
7013 * the newest last.
7014 *
Alex Hillsfc737de2016-03-23 17:33:02 -04007015 * @param message The {@link Message} to be displayed
7016 * @return this object for method chaining
7017 */
7018 public MessagingStyle addMessage(Message message) {
7019 mMessages.add(message);
7020 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
7021 mMessages.remove(0);
7022 }
7023 return this;
7024 }
7025
7026 /**
Adrian Roos437cd562017-01-18 15:47:03 -08007027 * Adds a {@link Message} for historic context in this notification.
7028 *
7029 * <p>Messages should be added as historic if they are not the main subject of the
7030 * notification but may give context to a conversation. The system may choose to present
7031 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
7032 *
7033 * <p>The messages should be added in chronologic order, i.e. the oldest first,
7034 * the newest last.
7035 *
7036 * @param message The historic {@link Message} to be added
7037 * @return this object for method chaining
7038 */
7039 public MessagingStyle addHistoricMessage(Message message) {
7040 mHistoricMessages.add(message);
7041 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
7042 mHistoricMessages.remove(0);
7043 }
7044 return this;
7045 }
7046
7047 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007048 * Gets the list of {@code Message} objects that represent the notification
7049 */
7050 public List<Message> getMessages() {
7051 return mMessages;
7052 }
7053
7054 /**
Adrian Roos437cd562017-01-18 15:47:03 -08007055 * Gets the list of historic {@code Message}s in the notification.
7056 */
7057 public List<Message> getHistoricMessages() {
7058 return mHistoricMessages;
7059 }
7060
7061 /**
Selim Cinekce8794f2018-05-23 16:46:05 -07007062 * Sets whether this conversation notification represents a group. If the app is targeting
7063 * Android P, this is required if the app wants to display the largeIcon set with
7064 * {@link Notification.Builder#setLargeIcon(Bitmap)}, otherwise it will be hidden.
Kodlee Yin14656422017-12-22 17:00:46 -08007065 *
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007066 * @param isGroupConversation {@code true} if the conversation represents a group,
7067 * {@code false} otherwise.
7068 * @return this object for method chaining
7069 */
7070 public MessagingStyle setGroupConversation(boolean isGroupConversation) {
7071 mIsGroupConversation = isGroupConversation;
7072 return this;
7073 }
7074
7075 /**
Kodlee Yin14656422017-12-22 17:00:46 -08007076 * Returns {@code true} if this notification represents a group conversation, otherwise
7077 * {@code false}.
7078 *
7079 * <p> If the application that generated this {@link MessagingStyle} targets an SDK version
7080 * less than {@link Build.VERSION_CODES#P}, this method becomes dependent on whether or
7081 * not the conversation title is set; returning {@code true} if the conversation title is
7082 * a non-null value, or {@code false} otherwise. From {@link Build.VERSION_CODES#P} forward,
7083 * this method returns what's set by {@link #setGroupConversation(boolean)} allowing for
7084 * named, non-group conversations.
7085 *
7086 * @see #setConversationTitle(CharSequence)
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007087 */
7088 public boolean isGroupConversation() {
Kodlee Yin14656422017-12-22 17:00:46 -08007089 // When target SDK version is < P, a non-null conversation title dictates if this is
7090 // as group conversation.
7091 if (mBuilder != null
7092 && mBuilder.mContext.getApplicationInfo().targetSdkVersion
7093 < Build.VERSION_CODES.P) {
7094 return mConversationTitle != null;
7095 }
7096
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007097 return mIsGroupConversation;
7098 }
7099
7100 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007101 * @hide
7102 */
7103 @Override
7104 public void addExtras(Bundle extras) {
7105 super.addExtras(extras);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007106 if (mUser != null) {
7107 // For legacy usages
7108 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUser.getName());
7109 extras.putParcelable(EXTRA_MESSAGING_PERSON, mUser);
Alex Hillsfc737de2016-03-23 17:33:02 -04007110 }
7111 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007112 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04007113 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007114 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
7115 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04007116 }
Adrian Roos437cd562017-01-18 15:47:03 -08007117 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
7118 Message.getBundleArrayForMessages(mHistoricMessages));
7119 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007120
7121 fixTitleAndTextExtras(extras);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007122 extras.putBoolean(EXTRA_IS_GROUP_CONVERSATION, mIsGroupConversation);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007123 }
7124
7125 private void fixTitleAndTextExtras(Bundle extras) {
7126 Message m = findLatestIncomingMessage();
7127 CharSequence text = (m == null) ? null : m.mText;
7128 CharSequence sender = m == null ? null
Selim Cinekcb8b9852017-12-15 18:01:52 -08007129 : m.mSender == null || TextUtils.isEmpty(m.mSender.getName())
7130 ? mUser.getName() : m.mSender.getName();
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007131 CharSequence title;
7132 if (!TextUtils.isEmpty(mConversationTitle)) {
7133 if (!TextUtils.isEmpty(sender)) {
7134 BidiFormatter bidi = BidiFormatter.getInstance();
7135 title = mBuilder.mContext.getString(
7136 com.android.internal.R.string.notification_messaging_title_template,
Selim Cinekcb8b9852017-12-15 18:01:52 -08007137 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(sender));
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007138 } else {
7139 title = mConversationTitle;
7140 }
7141 } else {
7142 title = sender;
7143 }
7144
7145 if (title != null) {
7146 extras.putCharSequence(EXTRA_TITLE, title);
7147 }
7148 if (text != null) {
7149 extras.putCharSequence(EXTRA_TEXT, text);
7150 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007151 }
7152
7153 /**
7154 * @hide
7155 */
7156 @Override
7157 protected void restoreFromExtras(Bundle extras) {
7158 super.restoreFromExtras(extras);
7159
Selim Cinekcb8b9852017-12-15 18:01:52 -08007160 mUser = extras.getParcelable(EXTRA_MESSAGING_PERSON);
7161 if (mUser == null) {
7162 CharSequence displayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
Selim Cinek9acd6732018-03-23 16:39:02 -07007163 mUser = new Person.Builder().setName(displayName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007164 }
Adrian Roos96b7e202016-05-17 13:50:38 -07007165 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08007166 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007167 mMessages = Message.getMessagesFromBundleArray(messages);
Adrian Roos437cd562017-01-18 15:47:03 -08007168 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007169 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007170 mIsGroupConversation = extras.getBoolean(EXTRA_IS_GROUP_CONVERSATION);
Alex Hillsfc737de2016-03-23 17:33:02 -04007171 }
7172
7173 /**
7174 * @hide
7175 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07007176 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007177 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07007178 mBuilder.mOriginalActions = mBuilder.mActions;
7179 mBuilder.mActions = new ArrayList<>();
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007180 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007181 false /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007182 mBuilder.mActions = mBuilder.mOriginalActions;
7183 mBuilder.mOriginalActions = null;
7184 return remoteViews;
Adrian Roosc1a80b02016-04-05 14:54:55 -07007185 }
7186
Julia Reynolds7217dc92018-03-07 12:12:09 -05007187 /**
7188 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04007189 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05007190 */
7191 @Override
7192 public boolean areNotificationsVisiblyDifferent(Style other) {
7193 if (other == null || getClass() != other.getClass()) {
7194 return true;
7195 }
7196 MessagingStyle newS = (MessagingStyle) other;
7197 List<MessagingStyle.Message> oldMs = getMessages();
7198 List<MessagingStyle.Message> newMs = newS.getMessages();
7199
Dan Sandler7d67bd42018-05-15 14:06:38 -04007200 if (oldMs == null || newMs == null) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007201 newMs = new ArrayList<>();
7202 }
7203
7204 int n = oldMs.size();
7205 if (n != newMs.size()) {
7206 return true;
7207 }
7208 for (int i = 0; i < n; i++) {
7209 MessagingStyle.Message oldM = oldMs.get(i);
7210 MessagingStyle.Message newM = newMs.get(i);
Dan Sandler7d67bd42018-05-15 14:06:38 -04007211 if (!Objects.equals(
7212 String.valueOf(oldM.getText()),
7213 String.valueOf(newM.getText()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007214 return true;
7215 }
7216 if (!Objects.equals(oldM.getDataUri(), newM.getDataUri())) {
7217 return true;
7218 }
Dan Sandler7d67bd42018-05-15 14:06:38 -04007219 String oldSender = String.valueOf(oldM.getSenderPerson() == null
7220 ? oldM.getSender()
7221 : oldM.getSenderPerson().getName());
7222 String newSender = String.valueOf(newM.getSenderPerson() == null
7223 ? newM.getSender()
7224 : newM.getSenderPerson().getName());
Julia Reynolds7217dc92018-03-07 12:12:09 -05007225 if (!Objects.equals(oldSender, newSender)) {
7226 return true;
7227 }
7228
7229 String oldKey = oldM.getSenderPerson() == null
7230 ? null : oldM.getSenderPerson().getKey();
7231 String newKey = newM.getSenderPerson() == null
7232 ? null : newM.getSenderPerson().getKey();
7233 if (!Objects.equals(oldKey, newKey)) {
7234 return true;
7235 }
7236 // Other fields (like timestamp) intentionally excluded
7237 }
7238 return false;
7239 }
7240
Adrian Roosc1a80b02016-04-05 14:54:55 -07007241 private Message findLatestIncomingMessage() {
Selim Cinek88188f22017-09-19 16:46:56 -07007242 return findLatestIncomingMessage(mMessages);
7243 }
7244
7245 /**
7246 * @hide
7247 */
7248 @Nullable
7249 public static Message findLatestIncomingMessage(
7250 List<Message> messages) {
7251 for (int i = messages.size() - 1; i >= 0; i--) {
7252 Message m = messages.get(i);
Adrian Roosc1a80b02016-04-05 14:54:55 -07007253 // Incoming messages have a non-empty sender.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007254 if (m.mSender != null && !TextUtils.isEmpty(m.mSender.getName())) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07007255 return m;
7256 }
7257 }
Selim Cinek88188f22017-09-19 16:46:56 -07007258 if (!messages.isEmpty()) {
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007259 // No incoming messages, fall back to outgoing message
Selim Cinek88188f22017-09-19 16:46:56 -07007260 return messages.get(messages.size() - 1);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007261 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07007262 return null;
7263 }
7264
7265 /**
7266 * @hide
7267 */
7268 @Override
7269 public RemoteViews makeBigContentView() {
Selim Cinek384804b2018-04-18 14:31:07 +08007270 return makeMessagingView(false /* displayImagesAtEnd */, true /* hideLargeIcon */);
Selim Cinekafeed292017-12-12 17:32:44 -08007271 }
7272
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007273 /**
7274 * Create a messaging layout.
7275 *
7276 * @param displayImagesAtEnd should images be displayed at the end of the content instead
7277 * of inline.
Selim Cinek384804b2018-04-18 14:31:07 +08007278 * @param hideRightIcons Should the reply affordance be shown at the end of the notification
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007279 * @return the created remoteView.
7280 */
Selim Cinekafeed292017-12-12 17:32:44 -08007281 @NonNull
Selim Cinek384804b2018-04-18 14:31:07 +08007282 private RemoteViews makeMessagingView(boolean displayImagesAtEnd, boolean hideRightIcons) {
Selim Cinek88188f22017-09-19 16:46:56 -07007283 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
Adrian Roosc1a80b02016-04-05 14:54:55 -07007284 ? super.mBigContentTitle
7285 : mConversationTitle;
Selim Cinekce8794f2018-05-23 16:46:05 -07007286 boolean atLeastP = mBuilder.mContext.getApplicationInfo().targetSdkVersion
7287 >= Build.VERSION_CODES.P;
7288 boolean isOneToOne;
Selim Cinek2dd3e722018-01-19 11:06:06 -08007289 CharSequence nameReplacement = null;
Selim Cinekce8794f2018-05-23 16:46:05 -07007290 Icon avatarReplacement = null;
7291 if (!atLeastP) {
7292 isOneToOne = TextUtils.isEmpty(conversationTitle);
7293 avatarReplacement = mBuilder.mN.mLargeIcon;
7294 if (hasOnlyWhiteSpaceSenders()) {
7295 isOneToOne = true;
7296 nameReplacement = conversationTitle;
7297 conversationTitle = null;
7298 }
7299 } else {
7300 isOneToOne = !isGroupConversation();
Adrian Roosb1f427c2016-05-26 12:27:15 -07007301 }
Selim Cinek384804b2018-04-18 14:31:07 +08007302 TemplateBindResult bindResult = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007303 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).title(
7304 conversationTitle).text(null)
7305 .hideLargeIcon(hideRightIcons || isOneToOne)
7306 .hideReplyIcon(hideRightIcons)
7307 .headerTextSecondary(conversationTitle);
Adrian Roos48d746a2016-04-12 14:57:28 -07007308 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07007309 mBuilder.getMessagingLayoutResource(),
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007310 p,
Selim Cinek384804b2018-04-18 14:31:07 +08007311 bindResult);
Selim Cinek88188f22017-09-19 16:46:56 -07007312 addExtras(mBuilder.mN.extras);
Selim Cinekafeed292017-12-12 17:32:44 -08007313 // also update the end margin if there is an image
Selim Cinek1c72fa02018-04-23 18:00:54 +08007314 contentView.setViewLayoutMarginEnd(R.id.notification_messaging,
7315 bindResult.getIconMarginEnd());
Selim Cinek88188f22017-09-19 16:46:56 -07007316 contentView.setInt(R.id.status_bar_latest_event_content, "setLayoutColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007317 mBuilder.isColorized(p) ? mBuilder.getPrimaryTextColor(p)
7318 : mBuilder.resolveContrastColor(p));
Kenny Guy14d035c2018-05-02 19:10:36 +01007319 contentView.setInt(R.id.status_bar_latest_event_content, "setSenderTextColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007320 mBuilder.getPrimaryTextColor(p));
Kenny Guy14d035c2018-05-02 19:10:36 +01007321 contentView.setInt(R.id.status_bar_latest_event_content, "setMessageTextColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007322 mBuilder.getSecondaryTextColor(p));
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007323 contentView.setBoolean(R.id.status_bar_latest_event_content, "setDisplayImagesAtEnd",
7324 displayImagesAtEnd);
Selim Cinekce8794f2018-05-23 16:46:05 -07007325 contentView.setIcon(R.id.status_bar_latest_event_content, "setAvatarReplacement",
7326 avatarReplacement);
Selim Cinek2dd3e722018-01-19 11:06:06 -08007327 contentView.setCharSequence(R.id.status_bar_latest_event_content, "setNameReplacement",
7328 nameReplacement);
Selim Cinek88188f22017-09-19 16:46:56 -07007329 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsOneToOne",
7330 isOneToOne);
7331 contentView.setBundle(R.id.status_bar_latest_event_content, "setData",
7332 mBuilder.mN.extras);
Alex Hillsfc737de2016-03-23 17:33:02 -04007333 return contentView;
7334 }
7335
Selim Cinekf7409db2017-10-24 16:17:14 -07007336 private boolean hasOnlyWhiteSpaceSenders() {
7337 for (int i = 0; i < mMessages.size(); i++) {
7338 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007339 Person sender = m.getSenderPerson();
7340 if (sender != null && !isWhiteSpace(sender.getName())) {
Selim Cinekf7409db2017-10-24 16:17:14 -07007341 return false;
7342 }
7343 }
7344 return true;
7345 }
7346
7347 private boolean isWhiteSpace(CharSequence sender) {
7348 if (TextUtils.isEmpty(sender)) {
7349 return true;
7350 }
7351 if (sender.toString().matches("^\\s*$")) {
7352 return true;
7353 }
7354 // Let's check if we only have 0 whitespace chars. Some apps did this as a workaround
7355 // For the presentation that we had.
7356 for (int i = 0; i < sender.length(); i++) {
7357 char c = sender.charAt(i);
7358 if (c != '\u200B') {
7359 return false;
7360 }
7361 }
7362 return true;
7363 }
7364
Selim Cinek88188f22017-09-19 16:46:56 -07007365 private CharSequence createConversationTitleFromMessages() {
7366 ArraySet<CharSequence> names = new ArraySet<>();
7367 for (int i = 0; i < mMessages.size(); i++) {
7368 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007369 Person sender = m.getSenderPerson();
Selim Cinek88188f22017-09-19 16:46:56 -07007370 if (sender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007371 names.add(sender.getName());
Selim Cinek88188f22017-09-19 16:46:56 -07007372 }
7373 }
7374 SpannableStringBuilder title = new SpannableStringBuilder();
7375 int size = names.size();
7376 for (int i = 0; i < size; i++) {
7377 CharSequence name = names.valueAt(i);
7378 if (!TextUtils.isEmpty(title)) {
7379 title.append(", ");
7380 }
7381 title.append(BidiFormatter.getInstance().unicodeWrap(name));
7382 }
7383 return title;
7384 }
7385
Adrian Roosdedd1df2016-04-26 16:38:47 -07007386 /**
7387 * @hide
7388 */
7389 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007390 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007391 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007392 true /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007393 remoteViews.setInt(R.id.notification_messaging, "setMaxDisplayedLines", 1);
7394 return remoteViews;
Adrian Roosdedd1df2016-04-26 16:38:47 -07007395 }
7396
Adrian Roosc1a80b02016-04-05 14:54:55 -07007397 private static TextAppearanceSpan makeFontColorSpan(int color) {
7398 return new TextAppearanceSpan(null, 0, 0,
7399 ColorStateList.valueOf(color), null);
7400 }
7401
Alex Hillsd9b04d92016-04-11 16:38:16 -04007402 public static final class Message {
Tony Mak09db2ea2018-06-27 18:12:48 +01007403 /** @hide */
7404 public static final String KEY_TEXT = "text";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007405 static final String KEY_TIMESTAMP = "time";
7406 static final String KEY_SENDER = "sender";
Selim Cinekcb8b9852017-12-15 18:01:52 -08007407 static final String KEY_SENDER_PERSON = "sender_person";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007408 static final String KEY_DATA_MIME_TYPE = "type";
7409 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08007410 static final String KEY_EXTRAS_BUNDLE = "extras";
Kenny Guya0f6de82018-04-06 16:20:16 +01007411 static final String KEY_REMOTE_INPUT_HISTORY = "remote_input_history";
Alex Hillsfc737de2016-03-23 17:33:02 -04007412
7413 private final CharSequence mText;
7414 private final long mTimestamp;
Selim Cinekcb8b9852017-12-15 18:01:52 -08007415 @Nullable
7416 private final Person mSender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007417 /** True if this message was generated from the extra
7418 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}
7419 */
7420 private final boolean mRemoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007421
Shane Brennan5a871862017-03-11 13:14:17 -08007422 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007423 private String mDataMimeType;
7424 private Uri mDataUri;
7425
7426 /**
7427 * Constructor
7428 * @param text A {@link CharSequence} to be displayed as the message content
7429 * @param timestamp Time at which the message arrived
7430 * @param sender A {@link CharSequence} to be used for displaying the name of the
7431 * sender. Should be <code>null</code> for messages by the current user, in which case
7432 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
7433 * Should be unique amongst all individuals in the conversation, and should be
7434 * consistent during re-posts of the notification.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007435 *
7436 * @deprecated use {@code Message(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04007437 */
7438 public Message(CharSequence text, long timestamp, CharSequence sender){
Selim Cinek9acd6732018-03-23 16:39:02 -07007439 this(text, timestamp, sender == null ? null
7440 : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08007441 }
7442
7443 /**
7444 * Constructor
7445 * @param text A {@link CharSequence} to be displayed as the message content
7446 * @param timestamp Time at which the message arrived
7447 * @param sender The {@link Person} who sent the message.
7448 * Should be <code>null</code> for messages by the current user, in which case
7449 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7450 * <p>
Selim Cinek9acd6732018-03-23 16:39:02 -07007451 * The person provided should contain an Icon, set with
7452 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7453 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7454 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7455 * to differentiate between the different users.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007456 * </p>
7457 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007458 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender) {
Kenny Guya0f6de82018-04-06 16:20:16 +01007459 this(text, timestamp, sender, false /* remoteHistory */);
7460 }
7461
7462 /**
7463 * Constructor
7464 * @param text A {@link CharSequence} to be displayed as the message content
7465 * @param timestamp Time at which the message arrived
7466 * @param sender The {@link Person} who sent the message.
7467 * Should be <code>null</code> for messages by the current user, in which case
7468 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7469 * @param remoteInputHistory True if the messages was generated from the extra
7470 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7471 * <p>
7472 * The person provided should contain an Icon, set with
7473 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7474 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7475 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7476 * to differentiate between the different users.
7477 * </p>
7478 * @hide
7479 */
7480 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender,
7481 boolean remoteInputHistory) {
Alex Hillsfc737de2016-03-23 17:33:02 -04007482 mText = text;
7483 mTimestamp = timestamp;
7484 mSender = sender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007485 mRemoteInputHistory = remoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007486 }
7487
7488 /**
7489 * Sets a binary blob of data and an associated MIME type for a message. In the case
7490 * where the platform doesn't support the MIME type, the original text provided in the
7491 * constructor will be used.
7492 * @param dataMimeType The MIME type of the content. See
7493 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
7494 * types on Android and Android Wear.
7495 * @param dataUri The uri containing the content whose type is given by the MIME type.
7496 * <p class="note">
7497 * <ol>
7498 * <li>Notification Listeners including the System UI need permission to access the
7499 * data the Uri points to. The recommended ways to do this are:</li>
7500 * <li>Store the data in your own ContentProvider, making sure that other apps have
7501 * the correct permission to access your provider. The preferred mechanism for
7502 * providing access is to use per-URI permissions which are temporary and only
7503 * grant access to the receiving application. An easy way to create a
7504 * ContentProvider like this is to use the FileProvider helper class.</li>
7505 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
7506 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
7507 * also store non-media types (see MediaStore.Files for more info). Files can be
7508 * inserted into the MediaStore using scanFile() after which a content:// style
7509 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
7510 * Note that once added to the system MediaStore the content is accessible to any
7511 * app on the device.</li>
7512 * </ol>
7513 * @return this object for method chaining
7514 */
7515 public Message setData(String dataMimeType, Uri dataUri) {
7516 mDataMimeType = dataMimeType;
7517 mDataUri = dataUri;
7518 return this;
7519 }
7520
Alex Hillsfc737de2016-03-23 17:33:02 -04007521 /**
7522 * Get the text to be used for this message, or the fallback text if a type and content
7523 * Uri have been set
7524 */
7525 public CharSequence getText() {
7526 return mText;
7527 }
7528
7529 /**
7530 * Get the time at which this message arrived
7531 */
7532 public long getTimestamp() {
7533 return mTimestamp;
7534 }
7535
7536 /**
Shane Brennan5a871862017-03-11 13:14:17 -08007537 * Get the extras Bundle for this message.
7538 */
7539 public Bundle getExtras() {
7540 return mExtras;
7541 }
7542
7543 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007544 * Get the text used to display the contact's name in the messaging experience
Selim Cinekcb8b9852017-12-15 18:01:52 -08007545 *
7546 * @deprecated use {@link #getSenderPerson()}
Alex Hillsfc737de2016-03-23 17:33:02 -04007547 */
7548 public CharSequence getSender() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007549 return mSender == null ? null : mSender.getName();
7550 }
7551
7552 /**
7553 * Get the sender associated with this message.
7554 */
7555 @Nullable
7556 public Person getSenderPerson() {
Alex Hillsfc737de2016-03-23 17:33:02 -04007557 return mSender;
7558 }
7559
7560 /**
7561 * Get the MIME type of the data pointed to by the Uri
7562 */
7563 public String getDataMimeType() {
7564 return mDataMimeType;
7565 }
7566
7567 /**
koprivaa1a78482018-10-09 10:09:23 -07007568 * Get the Uri pointing to the content of the message. Can be null, in which case
Alex Hillsfc737de2016-03-23 17:33:02 -04007569 * {@see #getText()} is used.
7570 */
7571 public Uri getDataUri() {
7572 return mDataUri;
7573 }
7574
Kenny Guya0f6de82018-04-06 16:20:16 +01007575 /**
7576 * @return True if the message was generated from
7577 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7578 * @hide
7579 */
7580 public boolean isRemoteInputHistory() {
7581 return mRemoteInputHistory;
7582 }
7583
Beverlye98937a2018-11-15 10:18:57 -05007584 /**
7585 * @hide
7586 */
7587 @VisibleForTesting
7588 public Bundle toBundle() {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007589 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007590 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007591 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04007592 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007593 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04007594 if (mSender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007595 // Legacy listeners need this
7596 bundle.putCharSequence(KEY_SENDER, mSender.getName());
7597 bundle.putParcelable(KEY_SENDER_PERSON, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04007598 }
7599 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007600 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04007601 }
7602 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007603 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04007604 }
Shane Brennan5a871862017-03-11 13:14:17 -08007605 if (mExtras != null) {
7606 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
7607 }
Kenny Guya0f6de82018-04-06 16:20:16 +01007608 if (mRemoteInputHistory) {
7609 bundle.putBoolean(KEY_REMOTE_INPUT_HISTORY, mRemoteInputHistory);
7610 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007611 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04007612 }
7613
Alex Hillsd9b04d92016-04-11 16:38:16 -04007614 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
7615 Bundle[] bundles = new Bundle[messages.size()];
7616 final int N = messages.size();
7617 for (int i = 0; i < N; i++) {
7618 bundles[i] = messages.get(i).toBundle();
7619 }
7620 return bundles;
7621 }
7622
Selim Cinek88188f22017-09-19 16:46:56 -07007623 /**
7624 * @return A list of messages read from the bundles.
7625 *
7626 * @hide
7627 */
7628 public static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
7629 if (bundles == null) {
7630 return new ArrayList<>();
7631 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007632 List<Message> messages = new ArrayList<>(bundles.length);
7633 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07007634 if (bundles[i] instanceof Bundle) {
7635 Message message = getMessageFromBundle((Bundle)bundles[i]);
7636 if (message != null) {
7637 messages.add(message);
7638 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007639 }
7640 }
7641 return messages;
7642 }
7643
Selim Cinekb0dc61b2018-05-22 18:49:36 -07007644 /**
7645 * @return The message that is stored in the bundle or null if the message couldn't be
7646 * resolved.
7647 *
7648 * @hide
7649 */
7650 @Nullable
7651 public static Message getMessageFromBundle(Bundle bundle) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007652 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07007653 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007654 return null;
7655 } else {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007656
7657 Person senderPerson = bundle.getParcelable(KEY_SENDER_PERSON);
7658 if (senderPerson == null) {
7659 // Legacy apps that use compat don't actually provide the sender objects
7660 // We need to fix the compat version to provide people / use
7661 // the native api instead
7662 CharSequence senderName = bundle.getCharSequence(KEY_SENDER);
7663 if (senderName != null) {
Selim Cinek9acd6732018-03-23 16:39:02 -07007664 senderPerson = new Person.Builder().setName(senderName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007665 }
7666 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007667 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
Selim Cinekcb8b9852017-12-15 18:01:52 -08007668 bundle.getLong(KEY_TIMESTAMP),
Kenny Guya0f6de82018-04-06 16:20:16 +01007669 senderPerson,
7670 bundle.getBoolean(KEY_REMOTE_INPUT_HISTORY, false));
Alex Hillsd9b04d92016-04-11 16:38:16 -04007671 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
7672 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007673 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
7674 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04007675 }
Shane Brennan5a871862017-03-11 13:14:17 -08007676 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
7677 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
7678 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007679 return message;
7680 }
7681 } catch (ClassCastException e) {
7682 return null;
7683 }
7684 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007685 }
7686 }
7687
7688 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04007689 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08007690 *
Robert Ly91c5ce32014-06-08 15:37:00 -07007691 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04007692 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07007693 * Notification notif = new Notification.Builder(mContext)
7694 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
7695 * .setContentText(subject)
7696 * .setSmallIcon(R.drawable.new_mail)
7697 * .setLargeIcon(aBitmap)
7698 * .setStyle(new Notification.InboxStyle()
7699 * .addLine(str1)
7700 * .addLine(str2)
7701 * .setContentTitle(&quot;&quot;)
7702 * .setSummaryText(&quot;+3 more&quot;))
7703 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04007704 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08007705 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04007706 * @see Notification#bigContentView
7707 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007708 public static class InboxStyle extends Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07007709
7710 /**
7711 * The number of lines of remote input history allowed until we start reducing lines.
7712 */
7713 private static final int NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION = 1;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007714 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
7715
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007716 public InboxStyle() {
7717 }
7718
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007719 /**
7720 * @deprecated use {@code InboxStyle()}.
7721 */
7722 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04007723 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007724 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04007725 }
7726
Chris Wrend6297db2012-05-03 16:20:13 -04007727 /**
7728 * Overrides ContentTitle in the big form of the template.
7729 * This defaults to the value passed to setContentTitle().
7730 */
7731 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007732 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04007733 return this;
7734 }
7735
7736 /**
7737 * Set the first line of text after the detail section in the big form of the template.
7738 */
7739 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007740 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04007741 return this;
7742 }
7743
Chris Wren0bd664d2012-08-01 13:56:56 -04007744 /**
7745 * Append a line to the digest section of the Inbox notification.
7746 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04007747 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007748 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04007749 return this;
7750 }
7751
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007752 /**
7753 * @hide
7754 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05007755 public ArrayList<CharSequence> getLines() {
7756 return mTexts;
7757 }
7758
7759 /**
7760 * @hide
7761 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007762 public void addExtras(Bundle extras) {
7763 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007764
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007765 CharSequence[] a = new CharSequence[mTexts.size()];
7766 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
7767 }
7768
Christoph Studer4600f9b2014-07-22 22:44:43 +02007769 /**
7770 * @hide
7771 */
7772 @Override
7773 protected void restoreFromExtras(Bundle extras) {
7774 super.restoreFromExtras(extras);
7775
7776 mTexts.clear();
7777 if (extras.containsKey(EXTRA_TEXT_LINES)) {
7778 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
7779 }
7780 }
7781
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007782 /**
7783 * @hide
7784 */
7785 public RemoteViews makeBigContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007786 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder).text(null);
Selim Cinek384804b2018-04-18 14:31:07 +08007787 TemplateBindResult result = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007788 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource(), p, result);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007789
Chris Wrend6297db2012-05-03 16:20:13 -04007790 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 -04007791 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04007792
Chris Wren4ed80d52012-05-17 09:30:03 -04007793 // Make sure all rows are gone in case we reuse a view.
7794 for (int rowId : rowIds) {
7795 contentView.setViewVisibility(rowId, View.GONE);
7796 }
7797
Daniel Sandler879c5e02012-04-17 16:46:51 -04007798 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07007799 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7800 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08007801 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07007802 int onlyViewId = 0;
7803 int maxRows = rowIds.length;
7804 if (mBuilder.mActions.size() > 0) {
7805 maxRows--;
7806 }
Selim Cinekbee4e072018-05-21 22:06:43 -07007807 CharSequence[] remoteInputHistory = mBuilder.mN.extras.getCharSequenceArray(
7808 EXTRA_REMOTE_INPUT_HISTORY);
7809 if (remoteInputHistory != null
7810 && remoteInputHistory.length > NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION) {
7811 // Let's remove some messages to make room for the remote input history.
7812 // 1 is always able to fit, but let's remove them if they are 2 or 3
7813 int numRemoteInputs = Math.min(remoteInputHistory.length,
7814 MAX_REMOTE_INPUT_HISTORY_LINES);
7815 int totalNumRows = mTexts.size() + numRemoteInputs
7816 - NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION;
7817 if (totalNumRows > maxRows) {
7818 int overflow = totalNumRows - maxRows;
7819 if (mTexts.size() > maxRows) {
7820 // Heuristic: if the Texts don't fit anyway, we'll rather drop the last
7821 // few messages, even with the remote input
7822 maxRows -= overflow;
7823 } else {
7824 // otherwise we drop the first messages
7825 i = overflow;
7826 }
7827 }
7828 }
Selim Cinek07c80172016-04-21 16:40:47 -07007829 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007830 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07007831 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007832 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07007833 contentView.setTextViewText(rowIds[i],
7834 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007835 mBuilder.setTextViewColorSecondary(contentView, rowIds[i], p);
Selim Cinek07c80172016-04-21 16:40:47 -07007836 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek384804b2018-04-18 14:31:07 +08007837 handleInboxImageMargin(contentView, rowIds[i], first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007838 result.getIconMarginEnd());
Selim Cinek07c80172016-04-21 16:40:47 -07007839 if (first) {
7840 onlyViewId = rowIds[i];
7841 } else {
7842 onlyViewId = 0;
7843 }
Selim Cinek247fa012016-02-18 09:50:48 -08007844 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007845 }
7846 i++;
7847 }
Selim Cinek07c80172016-04-21 16:40:47 -07007848 if (onlyViewId != 0) {
7849 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
7850 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7851 R.dimen.notification_text_margin_top);
7852 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
7853 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007854
Daniel Sandler879c5e02012-04-17 16:46:51 -04007855 return contentView;
7856 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007857
Julia Reynolds7217dc92018-03-07 12:12:09 -05007858 /**
7859 * @hide
7860 */
7861 @Override
7862 public boolean areNotificationsVisiblyDifferent(Style other) {
7863 if (other == null || getClass() != other.getClass()) {
7864 return true;
7865 }
7866 InboxStyle newS = (InboxStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04007867
7868 final ArrayList<CharSequence> myLines = getLines();
7869 final ArrayList<CharSequence> newLines = newS.getLines();
7870 final int n = myLines.size();
7871 if (n != newLines.size()) {
7872 return true;
7873 }
7874
7875 for (int i = 0; i < n; i++) {
7876 if (!Objects.equals(
7877 String.valueOf(myLines.get(i)),
7878 String.valueOf(newLines.get(i)))) {
7879 return true;
7880 }
7881 }
7882 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05007883 }
7884
Selim Cinek384804b2018-04-18 14:31:07 +08007885 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007886 int marginEndValue) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08007887 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08007888 if (first) {
7889 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
7890 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
7891 boolean hasProgress = max != 0 || ind;
Selim Cinek384804b2018-04-18 14:31:07 +08007892 if (!hasProgress) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08007893 endMargin = marginEndValue;
Selim Cinek247fa012016-02-18 09:50:48 -08007894 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007895 }
Selim Cinek1c72fa02018-04-23 18:00:54 +08007896 contentView.setViewLayoutMarginEnd(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08007897 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04007898 }
Dan Sandler842dd772014-05-15 09:36:47 -04007899
7900 /**
7901 * Notification style for media playback notifications.
7902 *
7903 * In the expanded form, {@link Notification#bigContentView}, up to 5
7904 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04007905 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04007906 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
7907 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
7908 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08007909 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04007910 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
7911 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01007912 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04007913 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08007914 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007915 * Notifications created with MediaStyle will have their category set to
7916 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
7917 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08007918 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07007919 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
7920 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04007921 * the System UI can identify this as a notification representing an active media session
7922 * and respond accordingly (by showing album artwork in the lockscreen, for example).
7923 *
Selim Cinek99104832017-01-25 14:47:33 -08007924 * <p>
7925 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
7926 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
7927 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
7928 * <p>
7929 *
Dan Sandler842dd772014-05-15 09:36:47 -04007930 * To use this style with your Notification, feed it to
7931 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7932 * <pre class="prettyprint">
7933 * Notification noti = new Notification.Builder()
7934 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01007935 * .setContentTitle(&quot;Track title&quot;)
7936 * .setContentText(&quot;Artist - Album&quot;)
7937 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007938 * .setStyle(<b>new Notification.MediaStyle()</b>
7939 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04007940 * .build();
7941 * </pre>
7942 *
7943 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08007944 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04007945 */
7946 public static class MediaStyle extends Style {
Gus Prevas9cc96602018-10-11 11:24:23 -04007947 // Changing max media buttons requires also changing templates
7948 // (notification_template_material_media and notification_template_material_big_media).
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007949 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04007950 static final int MAX_MEDIA_BUTTONS = 5;
Gus Prevas9cc96602018-10-11 11:24:23 -04007951 @IdRes private static final int[] MEDIA_BUTTON_IDS = {
7952 R.id.action0,
7953 R.id.action1,
7954 R.id.action2,
7955 R.id.action3,
7956 R.id.action4,
7957 };
Dan Sandler842dd772014-05-15 09:36:47 -04007958
7959 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07007960 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04007961
7962 public MediaStyle() {
7963 }
7964
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007965 /**
7966 * @deprecated use {@code MediaStyle()}.
7967 */
7968 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04007969 public MediaStyle(Builder builder) {
7970 setBuilder(builder);
7971 }
7972
7973 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007974 * 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 -04007975 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007976 *
7977 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04007978 */
7979 public MediaStyle setShowActionsInCompactView(int...actions) {
7980 mActionsToShowInCompact = actions;
7981 return this;
7982 }
7983
7984 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07007985 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
7986 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04007987 */
Jeff Browndba34ba2014-06-24 20:46:03 -07007988 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04007989 mToken = token;
7990 return this;
7991 }
7992
Christoph Studer4600f9b2014-07-22 22:44:43 +02007993 /**
7994 * @hide
7995 */
Dan Sandler842dd772014-05-15 09:36:47 -04007996 @Override
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007997 @UnsupportedAppUsage
Dan Sandler842dd772014-05-15 09:36:47 -04007998 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02007999 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01008000 if (wip.category == null) {
8001 wip.category = Notification.CATEGORY_TRANSPORT;
8002 }
Dan Sandler842dd772014-05-15 09:36:47 -04008003 return wip;
8004 }
8005
Christoph Studer4600f9b2014-07-22 22:44:43 +02008006 /**
8007 * @hide
8008 */
8009 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008010 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04008011 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02008012 }
8013
8014 /**
8015 * @hide
8016 */
8017 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04008018 public RemoteViews makeBigContentView() {
8019 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02008020 }
8021
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008022 /**
8023 * @hide
8024 */
8025 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008026 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008027 RemoteViews expanded = makeMediaBigContentView();
8028 return expanded != null ? expanded : makeMediaContentView();
8029 }
8030
Dan Sandler842dd772014-05-15 09:36:47 -04008031 /** @hide */
8032 @Override
8033 public void addExtras(Bundle extras) {
8034 super.addExtras(extras);
8035
8036 if (mToken != null) {
8037 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
8038 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01008039 if (mActionsToShowInCompact != null) {
8040 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
8041 }
Dan Sandler842dd772014-05-15 09:36:47 -04008042 }
8043
Christoph Studer4600f9b2014-07-22 22:44:43 +02008044 /**
8045 * @hide
8046 */
8047 @Override
8048 protected void restoreFromExtras(Bundle extras) {
8049 super.restoreFromExtras(extras);
8050
8051 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
8052 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
8053 }
8054 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
8055 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
8056 }
8057 }
8058
Julia Reynolds7217dc92018-03-07 12:12:09 -05008059 /**
8060 * @hide
8061 */
8062 @Override
8063 public boolean areNotificationsVisiblyDifferent(Style other) {
8064 if (other == null || getClass() != other.getClass()) {
8065 return true;
8066 }
8067 // All fields to compare are on the Notification object
8068 return false;
8069 }
8070
Gus Prevas9cc96602018-10-11 11:24:23 -04008071 private void bindMediaActionButton(RemoteViews container, @IdRes int buttonId,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008072 Action action, StandardTemplateParams p) {
Dan Sandler842dd772014-05-15 09:36:47 -04008073 final boolean tombstone = (action.actionIntent == null);
Gus Prevas9cc96602018-10-11 11:24:23 -04008074 container.setViewVisibility(buttonId, View.VISIBLE);
8075 container.setImageViewIcon(buttonId, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07008076
8077 // If the action buttons should not be tinted, then just use the default
8078 // notification color. Otherwise, just use the passed-in color.
Gus Prevas9cc96602018-10-11 11:24:23 -04008079 Resources resources = mBuilder.mContext.getResources();
8080 Configuration currentConfig = resources.getConfiguration();
Lucas Dupinf03e7522018-06-25 16:21:13 -07008081 boolean inNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
8082 == Configuration.UI_MODE_NIGHT_YES;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008083 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized(p)
8084 ? getActionColor(p)
Lucas Dupina291d192018-06-07 13:59:42 -07008085 : ContrastColorUtil.resolveColor(mBuilder.mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07008086 Notification.COLOR_DEFAULT, inNightMode);
Anthony Chenad4d1582017-04-10 16:07:58 -07008087
Gus Prevas9cc96602018-10-11 11:24:23 -04008088 container.setDrawableTint(buttonId, false, tintColor,
Sunny Goyal5b153922017-09-21 21:00:36 -07008089 PorterDuff.Mode.SRC_ATOP);
Gus Prevas9cc96602018-10-11 11:24:23 -04008090
8091 final TypedArray typedArray = mBuilder.mContext.obtainStyledAttributes(
8092 new int[]{ android.R.attr.colorControlHighlight });
8093 int rippleAlpha = Color.alpha(typedArray.getColor(0, 0));
8094 typedArray.recycle();
8095 int rippleColor = Color.argb(rippleAlpha, Color.red(tintColor), Color.green(tintColor),
8096 Color.blue(tintColor));
8097 container.setRippleDrawableColor(buttonId, ColorStateList.valueOf(rippleColor));
8098
Dan Sandler842dd772014-05-15 09:36:47 -04008099 if (!tombstone) {
Gus Prevas9cc96602018-10-11 11:24:23 -04008100 container.setOnClickPendingIntent(buttonId, action.actionIntent);
Dan Sandler842dd772014-05-15 09:36:47 -04008101 }
Gus Prevas9cc96602018-10-11 11:24:23 -04008102 container.setContentDescription(buttonId, action.title);
Dan Sandler842dd772014-05-15 09:36:47 -04008103 }
8104
8105 private RemoteViews makeMediaContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008106 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).fillTextsFrom(
8107 mBuilder);
Dan Sandler842dd772014-05-15 09:36:47 -04008108 RemoteViews view = mBuilder.applyStandardTemplate(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008109 R.layout.notification_template_material_media, p,
Selim Cinek384804b2018-04-18 14:31:07 +08008110 null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04008111
8112 final int numActions = mBuilder.mActions.size();
Gus Prevas9cc96602018-10-11 11:24:23 -04008113 final int numActionsToShow = mActionsToShowInCompact == null
Dan Sandler842dd772014-05-15 09:36:47 -04008114 ? 0
8115 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Gus Prevas9cc96602018-10-11 11:24:23 -04008116 if (numActionsToShow > numActions) {
8117 throw new IllegalArgumentException(String.format(
8118 "setShowActionsInCompactView: action %d out of bounds (max %d)",
8119 numActions, numActions - 1));
8120 }
8121 for (int i = 0; i < MAX_MEDIA_BUTTONS_IN_COMPACT; i++) {
8122 if (i < numActionsToShow) {
Dan Sandler842dd772014-05-15 09:36:47 -04008123 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008124 bindMediaActionButton(view, MEDIA_BUTTON_IDS[i], action, p);
Gus Prevas9cc96602018-10-11 11:24:23 -04008125 } else {
8126 view.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04008127 }
8128 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08008129 handleImage(view);
8130 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008131 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07008132 if (mBuilder.mN.hasLargeIcon()) {
Selim Cinek384804b2018-04-18 14:31:07 +08008133 endMargin = R.dimen.notification_media_image_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08008134 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008135 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04008136 return view;
8137 }
8138
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008139 private int getActionColor(StandardTemplateParams p) {
8140 return mBuilder.isColorized(p) ? mBuilder.getPrimaryTextColor(p)
8141 : mBuilder.resolveContrastColor(p);
Selim Cinek99104832017-01-25 14:47:33 -08008142 }
8143
Dan Sandler842dd772014-05-15 09:36:47 -04008144 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008145 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008146 // Dont add an expanded view if there is no more content to be revealed
8147 int actionsInCompact = mActionsToShowInCompact == null
8148 ? 0
8149 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07008150 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008151 return null;
8152 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008153 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).fillTextsFrom(
8154 mBuilder);
Selim Cinek5bf069a2015-11-10 19:14:27 -05008155 RemoteViews big = mBuilder.applyStandardTemplate(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008156 R.layout.notification_template_material_big_media, p , null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04008157
Gus Prevas9cc96602018-10-11 11:24:23 -04008158 for (int i = 0; i < MAX_MEDIA_BUTTONS; i++) {
8159 if (i < actionCount) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008160 bindMediaActionButton(big, MEDIA_BUTTON_IDS[i], mBuilder.mActions.get(i), p);
Gus Prevas9cc96602018-10-11 11:24:23 -04008161 } else {
8162 big.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04008163 }
8164 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05008165 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04008166 return big;
8167 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008168
Selim Cinek5bf069a2015-11-10 19:14:27 -05008169 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07008170 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008171 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
8172 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008173 }
8174 }
8175
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008176 /**
8177 * @hide
8178 */
8179 @Override
8180 protected boolean hasProgress() {
8181 return false;
8182 }
Dan Sandler842dd772014-05-15 09:36:47 -04008183 }
Griff Hazen61a9e862014-05-22 16:05:19 -07008184
Selim Cinek593610c2016-02-16 18:42:57 -08008185 /**
8186 * Notification style for custom views that are decorated by the system
8187 *
8188 * <p>Instead of providing a notification that is completely custom, a developer can set this
8189 * style and still obtain system decorations like the notification header with the expand
8190 * affordance and actions.
8191 *
8192 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8193 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8194 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8195 * corresponding custom views to display.
8196 *
8197 * To use this style with your Notification, feed it to
8198 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8199 * <pre class="prettyprint">
8200 * Notification noti = new Notification.Builder()
8201 * .setSmallIcon(R.drawable.ic_stat_player)
8202 * .setLargeIcon(albumArtBitmap))
8203 * .setCustomContentView(contentView);
8204 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
8205 * .build();
8206 * </pre>
8207 */
8208 public static class DecoratedCustomViewStyle extends Style {
8209
8210 public DecoratedCustomViewStyle() {
8211 }
8212
Selim Cinek593610c2016-02-16 18:42:57 -08008213 /**
8214 * @hide
8215 */
8216 public boolean displayCustomViewInline() {
8217 return true;
8218 }
8219
8220 /**
8221 * @hide
8222 */
8223 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008224 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008225 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
8226 }
8227
8228 /**
8229 * @hide
8230 */
8231 @Override
8232 public RemoteViews makeBigContentView() {
8233 return makeDecoratedBigContentView();
8234 }
8235
8236 /**
8237 * @hide
8238 */
8239 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008240 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008241 return makeDecoratedHeadsUpContentView();
8242 }
8243
Selim Cinek593610c2016-02-16 18:42:57 -08008244 private RemoteViews makeDecoratedHeadsUpContentView() {
8245 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
8246 ? mBuilder.mN.contentView
8247 : mBuilder.mN.headsUpContentView;
8248 if (mBuilder.mActions.size() == 0) {
8249 return makeStandardTemplateWithCustomContent(headsUpContentView);
8250 }
Selim Cinek384804b2018-04-18 14:31:07 +08008251 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008252 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008253 mBuilder.getBigBaseLayoutResource(), result);
8254 buildIntoRemoteViewContent(remoteViews, headsUpContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008255 return remoteViews;
8256 }
8257
Selim Cinek593610c2016-02-16 18:42:57 -08008258 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
Selim Cinek384804b2018-04-18 14:31:07 +08008259 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008260 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08008261 mBuilder.getBaseLayoutResource(), result);
8262 buildIntoRemoteViewContent(remoteViews, customContent, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008263 return remoteViews;
8264 }
8265
Selim Cinek593610c2016-02-16 18:42:57 -08008266 private RemoteViews makeDecoratedBigContentView() {
8267 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
8268 ? mBuilder.mN.contentView
8269 : mBuilder.mN.bigContentView;
8270 if (mBuilder.mActions.size() == 0) {
8271 return makeStandardTemplateWithCustomContent(bigContentView);
8272 }
Selim Cinek384804b2018-04-18 14:31:07 +08008273 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008274 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008275 mBuilder.getBigBaseLayoutResource(), result);
8276 buildIntoRemoteViewContent(remoteViews, bigContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008277 return remoteViews;
8278 }
Selim Cinek247fa012016-02-18 09:50:48 -08008279
8280 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
Selim Cinek384804b2018-04-18 14:31:07 +08008281 RemoteViews customContent, TemplateBindResult result) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08008282 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008283 // Need to clone customContent before adding, because otherwise it can no longer be
8284 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008285 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07008286 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
8287 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Sunny Goyalc12d31c2018-11-12 16:29:18 -08008288 remoteViews.addFlags(RemoteViews.FLAG_REAPPLY_DISALLOWED);
Adrian Roos5081c0d2016-02-26 16:04:19 -08008289 }
Selim Cinek247fa012016-02-18 09:50:48 -08008290 // also update the end margin if there is an image
Selim Cinek384804b2018-04-18 14:31:07 +08008291 Resources resources = mBuilder.mContext.getResources();
8292 int endMargin = resources.getDimensionPixelSize(
Selim Cinek1c72fa02018-04-23 18:00:54 +08008293 R.dimen.notification_content_margin_end) + result.getIconMarginEnd();
Selim Cinek384804b2018-04-18 14:31:07 +08008294 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08008295 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05008296
8297 /**
8298 * @hide
8299 */
8300 @Override
8301 public boolean areNotificationsVisiblyDifferent(Style other) {
8302 if (other == null || getClass() != other.getClass()) {
8303 return true;
8304 }
8305 // Comparison done for all custom RemoteViews, independent of style
8306 return false;
8307 }
Selim Cinek593610c2016-02-16 18:42:57 -08008308 }
8309
Selim Cinek03eb3b72016-02-18 10:39:45 -08008310 /**
8311 * Notification style for media custom views that are decorated by the system
8312 *
8313 * <p>Instead of providing a media notification that is completely custom, a developer can set
8314 * this style and still obtain system decorations like the notification header with the expand
8315 * affordance and actions.
8316 *
8317 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8318 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8319 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8320 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08008321 * <p>
8322 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
8323 * notification by using {@link Notification.Builder#setColorized(boolean)}.
8324 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08008325 * To use this style with your Notification, feed it to
8326 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8327 * <pre class="prettyprint">
8328 * Notification noti = new Notification.Builder()
8329 * .setSmallIcon(R.drawable.ic_stat_player)
8330 * .setLargeIcon(albumArtBitmap))
8331 * .setCustomContentView(contentView);
8332 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
8333 * .setMediaSession(mySession))
8334 * .build();
8335 * </pre>
8336 *
8337 * @see android.app.Notification.DecoratedCustomViewStyle
8338 * @see android.app.Notification.MediaStyle
8339 */
8340 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
8341
8342 public DecoratedMediaCustomViewStyle() {
8343 }
8344
Selim Cinek03eb3b72016-02-18 10:39:45 -08008345 /**
8346 * @hide
8347 */
8348 public boolean displayCustomViewInline() {
8349 return true;
8350 }
8351
8352 /**
8353 * @hide
8354 */
8355 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008356 public RemoteViews makeContentView(boolean increasedHeight) {
8357 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008358 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8359 mBuilder.mN.contentView);
8360 }
8361
8362 /**
8363 * @hide
8364 */
8365 @Override
8366 public RemoteViews makeBigContentView() {
8367 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
8368 ? mBuilder.mN.bigContentView
8369 : mBuilder.mN.contentView;
8370 return makeBigContentViewWithCustomContent(customRemoteView);
8371 }
8372
8373 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
8374 RemoteViews remoteViews = super.makeBigContentView();
8375 if (remoteViews != null) {
8376 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
8377 customRemoteView);
8378 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08008379 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008380 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8381 customRemoteView);
8382 } else {
8383 return null;
8384 }
8385 }
8386
8387 /**
8388 * @hide
8389 */
8390 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008391 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08008392 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
8393 ? mBuilder.mN.headsUpContentView
8394 : mBuilder.mN.contentView;
8395 return makeBigContentViewWithCustomContent(customRemoteView);
8396 }
8397
Julia Reynolds7217dc92018-03-07 12:12:09 -05008398 /**
8399 * @hide
8400 */
8401 @Override
8402 public boolean areNotificationsVisiblyDifferent(Style other) {
8403 if (other == null || getClass() != other.getClass()) {
8404 return true;
8405 }
8406 // Comparison done for all custom RemoteViews, independent of style
8407 return false;
8408 }
8409
Selim Cinek03eb3b72016-02-18 10:39:45 -08008410 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
8411 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08008412 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008413 // Need to clone customContent before adding, because otherwise it can no longer be
8414 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008415 customContent = customContent.clone();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008416 customContent.overrideTextColors(mBuilder.getPrimaryTextColor(mBuilder.mParams));
Selim Cinekf91017e2016-03-14 12:25:09 -07008417 remoteViews.removeAllViews(id);
8418 remoteViews.addView(id, customContent);
Sunny Goyalc12d31c2018-11-12 16:29:18 -08008419 remoteViews.addFlags(RemoteViews.FLAG_REAPPLY_DISALLOWED);
Adrian Roos5081c0d2016-02-26 16:04:19 -08008420 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08008421 return remoteViews;
8422 }
8423 }
8424
Christoph Studer4600f9b2014-07-22 22:44:43 +02008425 // When adding a new Style subclass here, don't forget to update
8426 // Builder.getNotificationStyleClass.
8427
Griff Hazen61a9e862014-05-22 16:05:19 -07008428 /**
8429 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
8430 * metadata or change options on a notification builder.
8431 */
8432 public interface Extender {
8433 /**
8434 * Apply this extender to a notification builder.
8435 * @param builder the builder to be modified.
8436 * @return the build object for chaining.
8437 */
8438 public Builder extend(Builder builder);
8439 }
8440
8441 /**
8442 * Helper class to add wearable extensions to notifications.
8443 * <p class="note"> See
8444 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
8445 * for Android Wear</a> for more information on how to use this class.
8446 * <p>
8447 * To create a notification with wearable extensions:
8448 * <ol>
8449 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
8450 * properties.
8451 * <li>Create a {@link android.app.Notification.WearableExtender}.
8452 * <li>Set wearable-specific properties using the
8453 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
8454 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
8455 * notification.
8456 * <li>Post the notification to the notification system with the
8457 * {@code NotificationManager.notify(...)} methods.
8458 * </ol>
8459 *
8460 * <pre class="prettyprint">
8461 * Notification notif = new Notification.Builder(mContext)
8462 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
8463 * .setContentText(subject)
8464 * .setSmallIcon(R.drawable.new_mail)
8465 * .extend(new Notification.WearableExtender()
8466 * .setContentIcon(R.drawable.new_mail))
8467 * .build();
8468 * NotificationManager notificationManger =
8469 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
8470 * notificationManger.notify(0, notif);</pre>
8471 *
8472 * <p>Wearable extensions can be accessed on an existing notification by using the
8473 * {@code WearableExtender(Notification)} constructor,
8474 * and then using the {@code get} methods to access values.
8475 *
8476 * <pre class="prettyprint">
8477 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
8478 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07008479 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008480 */
8481 public static final class WearableExtender implements Extender {
8482 /**
8483 * Sentinel value for an action index that is unset.
8484 */
8485 public static final int UNSET_ACTION_INDEX = -1;
8486
8487 /**
8488 * Size value for use with {@link #setCustomSizePreset} to show this notification with
8489 * default sizing.
8490 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07008491 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07008492 * on their content.
Gus Prevasd7363752018-09-18 14:35:15 -04008493 *
8494 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008495 */
Gus Prevasd7363752018-09-18 14:35:15 -04008496 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008497 public static final int SIZE_DEFAULT = 0;
8498
8499 /**
8500 * Size value for use with {@link #setCustomSizePreset} to show this notification
8501 * with an extra small size.
8502 * <p>This value is only applicable for custom display notifications created using
8503 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008504 *
8505 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008506 */
Gus Prevasd7363752018-09-18 14:35:15 -04008507 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008508 public static final int SIZE_XSMALL = 1;
8509
8510 /**
8511 * Size value for use with {@link #setCustomSizePreset} to show this notification
8512 * with a small size.
8513 * <p>This value is only applicable for custom display notifications created using
8514 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008515 *
8516 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008517 */
Gus Prevasd7363752018-09-18 14:35:15 -04008518 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008519 public static final int SIZE_SMALL = 2;
8520
8521 /**
8522 * Size value for use with {@link #setCustomSizePreset} to show this notification
8523 * with a medium size.
8524 * <p>This value is only applicable for custom display notifications created using
8525 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008526 *
8527 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008528 */
Gus Prevasd7363752018-09-18 14:35:15 -04008529 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008530 public static final int SIZE_MEDIUM = 3;
8531
8532 /**
8533 * Size value for use with {@link #setCustomSizePreset} to show this notification
8534 * with a large size.
8535 * <p>This value is only applicable for custom display notifications created using
8536 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008537 *
8538 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008539 */
Gus Prevasd7363752018-09-18 14:35:15 -04008540 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008541 public static final int SIZE_LARGE = 4;
8542
Griff Hazend5f11f92014-05-27 15:40:09 -07008543 /**
8544 * Size value for use with {@link #setCustomSizePreset} to show this notification
8545 * full screen.
8546 * <p>This value is only applicable for custom display notifications created using
8547 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008548 *
8549 * @deprecated Display intents are no longer supported.
Griff Hazend5f11f92014-05-27 15:40:09 -07008550 */
Gus Prevasd7363752018-09-18 14:35:15 -04008551 @Deprecated
Griff Hazend5f11f92014-05-27 15:40:09 -07008552 public static final int SIZE_FULL_SCREEN = 5;
8553
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008554 /**
8555 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
8556 * short amount of time when this notification is displayed on the screen. This
8557 * is the default value.
Gus Prevasd7363752018-09-18 14:35:15 -04008558 *
8559 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008560 */
Gus Prevasd7363752018-09-18 14:35:15 -04008561 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008562 public static final int SCREEN_TIMEOUT_SHORT = 0;
8563
8564 /**
8565 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
8566 * for a longer amount of time when this notification is displayed on the screen.
Gus Prevasd7363752018-09-18 14:35:15 -04008567 *
8568 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008569 */
Gus Prevasd7363752018-09-18 14:35:15 -04008570 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008571 public static final int SCREEN_TIMEOUT_LONG = -1;
8572
Griff Hazen61a9e862014-05-22 16:05:19 -07008573 /** Notification extra which contains wearable extensions */
8574 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
8575
Pete Gastaf6781d2014-10-07 15:17:05 -04008576 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07008577 private static final String KEY_ACTIONS = "actions";
8578 private static final String KEY_FLAGS = "flags";
8579 private static final String KEY_DISPLAY_INTENT = "displayIntent";
8580 private static final String KEY_PAGES = "pages";
8581 private static final String KEY_BACKGROUND = "background";
8582 private static final String KEY_CONTENT_ICON = "contentIcon";
8583 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
8584 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
8585 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
8586 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
8587 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008588 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04008589 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008590 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07008591
8592 // Flags bitwise-ored to mFlags
8593 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
8594 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
8595 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
8596 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008597 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04008598 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04008599 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07008600
8601 // Default value for flags integer
8602 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
8603
8604 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
8605 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
8606
8607 private ArrayList<Action> mActions = new ArrayList<Action>();
8608 private int mFlags = DEFAULT_FLAGS;
8609 private PendingIntent mDisplayIntent;
8610 private ArrayList<Notification> mPages = new ArrayList<Notification>();
8611 private Bitmap mBackground;
8612 private int mContentIcon;
8613 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
8614 private int mContentActionIndex = UNSET_ACTION_INDEX;
8615 private int mCustomSizePreset = SIZE_DEFAULT;
8616 private int mCustomContentHeight;
8617 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008618 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04008619 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008620 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07008621
8622 /**
8623 * Create a {@link android.app.Notification.WearableExtender} with default
8624 * options.
8625 */
8626 public WearableExtender() {
8627 }
8628
8629 public WearableExtender(Notification notif) {
8630 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
8631 if (wearableBundle != null) {
8632 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
8633 if (actions != null) {
8634 mActions.addAll(actions);
8635 }
8636
8637 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
8638 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
8639
8640 Notification[] pages = getNotificationArrayFromBundle(
8641 wearableBundle, KEY_PAGES);
8642 if (pages != null) {
8643 Collections.addAll(mPages, pages);
8644 }
8645
8646 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
8647 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
8648 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
8649 DEFAULT_CONTENT_ICON_GRAVITY);
8650 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
8651 UNSET_ACTION_INDEX);
8652 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
8653 SIZE_DEFAULT);
8654 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
8655 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008656 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04008657 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008658 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07008659 }
8660 }
8661
8662 /**
8663 * Apply wearable extensions to a notification that is being built. This is typically
8664 * called by the {@link android.app.Notification.Builder#extend} method of
8665 * {@link android.app.Notification.Builder}.
8666 */
8667 @Override
8668 public Notification.Builder extend(Notification.Builder builder) {
8669 Bundle wearableBundle = new Bundle();
8670
8671 if (!mActions.isEmpty()) {
8672 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
8673 }
8674 if (mFlags != DEFAULT_FLAGS) {
8675 wearableBundle.putInt(KEY_FLAGS, mFlags);
8676 }
8677 if (mDisplayIntent != null) {
8678 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
8679 }
8680 if (!mPages.isEmpty()) {
8681 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
8682 new Notification[mPages.size()]));
8683 }
8684 if (mBackground != null) {
8685 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
8686 }
8687 if (mContentIcon != 0) {
8688 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
8689 }
8690 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
8691 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
8692 }
8693 if (mContentActionIndex != UNSET_ACTION_INDEX) {
8694 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
8695 mContentActionIndex);
8696 }
8697 if (mCustomSizePreset != SIZE_DEFAULT) {
8698 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
8699 }
8700 if (mCustomContentHeight != 0) {
8701 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
8702 }
8703 if (mGravity != DEFAULT_GRAVITY) {
8704 wearableBundle.putInt(KEY_GRAVITY, mGravity);
8705 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008706 if (mHintScreenTimeout != 0) {
8707 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
8708 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04008709 if (mDismissalId != null) {
8710 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
8711 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008712 if (mBridgeTag != null) {
8713 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
8714 }
Griff Hazen61a9e862014-05-22 16:05:19 -07008715
8716 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
8717 return builder;
8718 }
8719
8720 @Override
8721 public WearableExtender clone() {
8722 WearableExtender that = new WearableExtender();
8723 that.mActions = new ArrayList<Action>(this.mActions);
8724 that.mFlags = this.mFlags;
8725 that.mDisplayIntent = this.mDisplayIntent;
8726 that.mPages = new ArrayList<Notification>(this.mPages);
8727 that.mBackground = this.mBackground;
8728 that.mContentIcon = this.mContentIcon;
8729 that.mContentIconGravity = this.mContentIconGravity;
8730 that.mContentActionIndex = this.mContentActionIndex;
8731 that.mCustomSizePreset = this.mCustomSizePreset;
8732 that.mCustomContentHeight = this.mCustomContentHeight;
8733 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008734 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04008735 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008736 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07008737 return that;
8738 }
8739
8740 /**
8741 * Add a wearable action to this notification.
8742 *
8743 * <p>When wearable actions are added using this method, the set of actions that
8744 * show on a wearable device splits from devices that only show actions added
8745 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
8746 * of which actions display on different devices.
8747 *
8748 * @param action the action to add to this notification
8749 * @return this object for method chaining
8750 * @see android.app.Notification.Action
8751 */
8752 public WearableExtender addAction(Action action) {
8753 mActions.add(action);
8754 return this;
8755 }
8756
8757 /**
8758 * Adds wearable actions to this notification.
8759 *
8760 * <p>When wearable actions are added using this method, the set of actions that
8761 * show on a wearable device splits from devices that only show actions added
8762 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
8763 * of which actions display on different devices.
8764 *
8765 * @param actions the actions to add to this notification
8766 * @return this object for method chaining
8767 * @see android.app.Notification.Action
8768 */
8769 public WearableExtender addActions(List<Action> actions) {
8770 mActions.addAll(actions);
8771 return this;
8772 }
8773
8774 /**
8775 * Clear all wearable actions present on this builder.
8776 * @return this object for method chaining.
8777 * @see #addAction
8778 */
8779 public WearableExtender clearActions() {
8780 mActions.clear();
8781 return this;
8782 }
8783
8784 /**
8785 * Get the wearable actions present on this notification.
8786 */
8787 public List<Action> getActions() {
8788 return mActions;
8789 }
8790
8791 /**
8792 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07008793 * this notification. The {@link PendingIntent} provided should be for an activity.
8794 *
8795 * <pre class="prettyprint">
8796 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
8797 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
8798 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
8799 * Notification notif = new Notification.Builder(context)
8800 * .extend(new Notification.WearableExtender()
8801 * .setDisplayIntent(displayPendingIntent)
8802 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
8803 * .build();</pre>
8804 *
8805 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07008806 * should have an empty task affinity. It is also recommended to use the device
8807 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07008808 *
8809 * <p>Example AndroidManifest.xml entry:
8810 * <pre class="prettyprint">
8811 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
8812 * android:exported=&quot;true&quot;
8813 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07008814 * android:taskAffinity=&quot;&quot;
8815 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008816 *
8817 * @param intent the {@link PendingIntent} for an activity
8818 * @return this object for method chaining
8819 * @see android.app.Notification.WearableExtender#getDisplayIntent
Gus Prevasd7363752018-09-18 14:35:15 -04008820 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008821 */
Gus Prevasd7363752018-09-18 14:35:15 -04008822 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008823 public WearableExtender setDisplayIntent(PendingIntent intent) {
8824 mDisplayIntent = intent;
8825 return this;
8826 }
8827
8828 /**
8829 * Get the intent to launch inside of an activity view when displaying this
8830 * notification. This {@code PendingIntent} should be for an activity.
Gus Prevasd7363752018-09-18 14:35:15 -04008831 *
8832 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008833 */
Gus Prevasd7363752018-09-18 14:35:15 -04008834 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008835 public PendingIntent getDisplayIntent() {
8836 return mDisplayIntent;
8837 }
8838
8839 /**
8840 * Add an additional page of content to display with this notification. The current
8841 * notification forms the first page, and pages added using this function form
8842 * subsequent pages. This field can be used to separate a notification into multiple
8843 * sections.
8844 *
8845 * @param page the notification to add as another page
8846 * @return this object for method chaining
8847 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04008848 * @deprecated Multiple content pages 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 WearableExtender addPage(Notification page) {
8852 mPages.add(page);
8853 return this;
8854 }
8855
8856 /**
8857 * Add additional pages of content to display with this notification. The current
8858 * notification forms the first page, and pages added using this function form
8859 * subsequent pages. This field can be used to separate a notification into multiple
8860 * sections.
8861 *
8862 * @param pages a list of notifications
8863 * @return this object for method chaining
8864 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04008865 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008866 */
Gus Prevasd7363752018-09-18 14:35:15 -04008867 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008868 public WearableExtender addPages(List<Notification> pages) {
8869 mPages.addAll(pages);
8870 return this;
8871 }
8872
8873 /**
8874 * Clear all additional pages present on this builder.
8875 * @return this object for method chaining.
8876 * @see #addPage
Gus Prevasd7363752018-09-18 14:35:15 -04008877 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008878 */
Gus Prevasd7363752018-09-18 14:35:15 -04008879 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008880 public WearableExtender clearPages() {
8881 mPages.clear();
8882 return this;
8883 }
8884
8885 /**
8886 * Get the array of additional pages of content for displaying this notification. The
8887 * current notification forms the first page, and elements within this array form
8888 * subsequent pages. This field can be used to separate a notification into multiple
8889 * sections.
8890 * @return the pages for this notification
Gus Prevasd7363752018-09-18 14:35:15 -04008891 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008892 */
Gus Prevasd7363752018-09-18 14:35:15 -04008893 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008894 public List<Notification> getPages() {
8895 return mPages;
8896 }
8897
8898 /**
8899 * Set a background image to be displayed behind the notification content.
8900 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8901 * will work with any notification style.
8902 *
8903 * @param background the background bitmap
8904 * @return this object for method chaining
8905 * @see android.app.Notification.WearableExtender#getBackground
Gus Prevasd7363752018-09-18 14:35:15 -04008906 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008907 */
Gus Prevasd7363752018-09-18 14:35:15 -04008908 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008909 public WearableExtender setBackground(Bitmap background) {
8910 mBackground = background;
8911 return this;
8912 }
8913
8914 /**
8915 * Get a background image to be displayed behind the notification content.
8916 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8917 * will work with any notification style.
8918 *
8919 * @return the background image
8920 * @see android.app.Notification.WearableExtender#setBackground
Gus Prevasd7363752018-09-18 14:35:15 -04008921 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008922 */
Gus Prevasd7363752018-09-18 14:35:15 -04008923 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008924 public Bitmap getBackground() {
8925 return mBackground;
8926 }
8927
8928 /**
8929 * Set an icon that goes with the content of this notification.
8930 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008931 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008932 public WearableExtender setContentIcon(int icon) {
8933 mContentIcon = icon;
8934 return this;
8935 }
8936
8937 /**
8938 * Get an icon that goes with the content of this notification.
8939 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008940 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008941 public int getContentIcon() {
8942 return mContentIcon;
8943 }
8944
8945 /**
8946 * Set the gravity that the content icon should have within the notification display.
8947 * Supported values include {@link android.view.Gravity#START} and
8948 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8949 * @see #setContentIcon
8950 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008951 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008952 public WearableExtender setContentIconGravity(int contentIconGravity) {
8953 mContentIconGravity = contentIconGravity;
8954 return this;
8955 }
8956
8957 /**
8958 * Get the gravity that the content icon should have within the notification display.
8959 * Supported values include {@link android.view.Gravity#START} and
8960 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8961 * @see #getContentIcon
8962 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008963 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008964 public int getContentIconGravity() {
8965 return mContentIconGravity;
8966 }
8967
8968 /**
Gus Prevasd7363752018-09-18 14:35:15 -04008969 * Set an action from this notification's actions as the primary action. If the action has a
8970 * {@link RemoteInput} associated with it, shortcuts to the options for that input are shown
8971 * directly on the notification.
Griff Hazen14f57992014-05-26 09:07:14 -07008972 *
Gus Prevasd7363752018-09-18 14:35:15 -04008973 * @param actionIndex The index of the primary action.
Griff Hazen14f57992014-05-26 09:07:14 -07008974 * If wearable actions were added to the main notification, this index
8975 * will apply to that list, otherwise it will apply to the regular
8976 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07008977 */
8978 public WearableExtender setContentAction(int actionIndex) {
8979 mContentActionIndex = actionIndex;
8980 return this;
8981 }
8982
8983 /**
Gus Prevasd7363752018-09-18 14:35:15 -04008984 * Get the index of the notification action, if any, that was specified as the primary
8985 * action.
Griff Hazen14f57992014-05-26 09:07:14 -07008986 *
8987 * <p>If wearable specific actions were added to the main notification, this index will
8988 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07008989 *
8990 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07008991 */
8992 public int getContentAction() {
8993 return mContentActionIndex;
8994 }
8995
8996 /**
8997 * Set the gravity that this notification should have within the available viewport space.
8998 * Supported values include {@link android.view.Gravity#TOP},
8999 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
9000 * The default value is {@link android.view.Gravity#BOTTOM}.
9001 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009002 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009003 public WearableExtender setGravity(int gravity) {
9004 mGravity = gravity;
9005 return this;
9006 }
9007
9008 /**
9009 * Get the gravity that this notification should have within the available viewport space.
9010 * Supported values include {@link android.view.Gravity#TOP},
9011 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
9012 * The default value is {@link android.view.Gravity#BOTTOM}.
9013 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009014 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009015 public int getGravity() {
9016 return mGravity;
9017 }
9018
9019 /**
9020 * Set the custom size preset for the display of this notification out of the available
9021 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
9022 * {@link #SIZE_LARGE}.
9023 * <p>Some custom size presets are only applicable for custom display notifications created
9024 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
9025 * documentation for the preset in question. See also
9026 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
9027 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009028 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009029 public WearableExtender setCustomSizePreset(int sizePreset) {
9030 mCustomSizePreset = sizePreset;
9031 return this;
9032 }
9033
9034 /**
9035 * Get the custom size preset for the display of this notification out of the available
9036 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
9037 * {@link #SIZE_LARGE}.
9038 * <p>Some custom size presets are only applicable for custom display notifications created
9039 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
9040 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
9041 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009042 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009043 public int getCustomSizePreset() {
9044 return mCustomSizePreset;
9045 }
9046
9047 /**
9048 * Set the custom height in pixels for the display of this notification's content.
9049 * <p>This option is only available for custom display notifications created
9050 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
9051 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
9052 * {@link #getCustomContentHeight}.
9053 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009054 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009055 public WearableExtender setCustomContentHeight(int height) {
9056 mCustomContentHeight = height;
9057 return this;
9058 }
9059
9060 /**
9061 * Get the custom height in pixels for the display of this notification's content.
9062 * <p>This option is only available for custom display notifications created
9063 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
9064 * {@link #setCustomContentHeight}.
9065 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009066 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009067 public int getCustomContentHeight() {
9068 return mCustomContentHeight;
9069 }
9070
9071 /**
9072 * Set whether the scrolling position for the contents of this notification should start
9073 * at the bottom of the contents instead of the top when the contents are too long to
9074 * display within the screen. Default is false (start scroll at the top).
9075 */
9076 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
9077 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
9078 return this;
9079 }
9080
9081 /**
9082 * Get whether the scrolling position for the contents of this notification should start
9083 * at the bottom of the contents instead of the top when the contents are too long to
9084 * display within the screen. Default is false (start scroll at the top).
9085 */
9086 public boolean getStartScrollBottom() {
9087 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
9088 }
9089
9090 /**
9091 * Set whether the content intent is available when the wearable device is not connected
9092 * to a companion device. The user can still trigger this intent when the wearable device
9093 * is offline, but a visual hint will indicate that the content intent may not be available.
9094 * Defaults to true.
9095 */
9096 public WearableExtender setContentIntentAvailableOffline(
9097 boolean contentIntentAvailableOffline) {
9098 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
9099 return this;
9100 }
9101
9102 /**
9103 * Get whether the content intent is available when the wearable device is not connected
9104 * to a companion device. The user can still trigger this intent when the wearable device
9105 * is offline, but a visual hint will indicate that the content intent may not be available.
9106 * Defaults to true.
9107 */
9108 public boolean getContentIntentAvailableOffline() {
9109 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
9110 }
9111
9112 /**
9113 * Set a hint that this notification's icon should not be displayed.
9114 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
9115 * @return this object for method chaining
9116 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009117 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009118 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
9119 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
9120 return this;
9121 }
9122
9123 /**
9124 * Get a hint that this notification's icon should not be displayed.
9125 * @return {@code true} if this icon should not be displayed, false otherwise.
9126 * The default value is {@code false} if this was never set.
9127 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009128 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009129 public boolean getHintHideIcon() {
9130 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
9131 }
9132
9133 /**
9134 * Set a visual hint that only the background image of this notification should be
9135 * displayed, and other semantic content should be hidden. This hint is only applicable
9136 * to sub-pages added using {@link #addPage}.
9137 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009138 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009139 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
9140 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
9141 return this;
9142 }
9143
9144 /**
9145 * Get a visual hint that only the background image of this notification should be
9146 * displayed, and other semantic content should be hidden. This hint is only applicable
9147 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
9148 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009149 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009150 public boolean getHintShowBackgroundOnly() {
9151 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
9152 }
9153
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009154 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009155 * Set a hint that this notification's background should not be clipped if possible,
9156 * and should instead be resized to fully display on the screen, retaining the aspect
9157 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009158 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
9159 * @return this object for method chaining
9160 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009161 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009162 public WearableExtender setHintAvoidBackgroundClipping(
9163 boolean hintAvoidBackgroundClipping) {
9164 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
9165 return this;
9166 }
9167
9168 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009169 * Get a hint that this notification's background should not be clipped if possible,
9170 * and should instead be resized to fully display on the screen, retaining the aspect
9171 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009172 * @return {@code true} if it's ok if the background is clipped on the screen, false
9173 * otherwise. The default value is {@code false} if this was never set.
9174 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009175 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009176 public boolean getHintAvoidBackgroundClipping() {
9177 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
9178 }
9179
9180 /**
9181 * Set a hint that the screen should remain on for at least this duration when
9182 * this notification is displayed on the screen.
9183 * @param timeout The requested screen timeout in milliseconds. Can also be either
9184 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9185 * @return this object for method chaining
9186 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009187 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009188 public WearableExtender setHintScreenTimeout(int timeout) {
9189 mHintScreenTimeout = timeout;
9190 return this;
9191 }
9192
9193 /**
9194 * Get the duration, in milliseconds, that the screen should remain on for
9195 * when this notification is displayed.
9196 * @return the duration in milliseconds if > 0, or either one of the sentinel values
9197 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9198 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009199 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009200 public int getHintScreenTimeout() {
9201 return mHintScreenTimeout;
9202 }
9203
Alex Hills9ab3a232016-04-05 14:54:56 -04009204 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04009205 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
9206 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9207 * qr codes, as well as other simple black-and-white tickets.
9208 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
9209 * @return this object for method chaining
Gus Prevasd7363752018-09-18 14:35:15 -04009210 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009211 */
Gus Prevasd7363752018-09-18 14:35:15 -04009212 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009213 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
9214 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
9215 return this;
9216 }
9217
9218 /**
9219 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
9220 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9221 * qr codes, as well as other simple black-and-white tickets.
9222 * @return {@code true} if it should be displayed in ambient, false otherwise
9223 * otherwise. The default value is {@code false} if this was never set.
Gus Prevasd7363752018-09-18 14:35:15 -04009224 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009225 */
Gus Prevasd7363752018-09-18 14:35:15 -04009226 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009227 public boolean getHintAmbientBigPicture() {
9228 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
9229 }
9230
9231 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04009232 * Set a hint that this notification's content intent will launch an {@link Activity}
9233 * directly, telling the platform that it can generate the appropriate transitions.
9234 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
9235 * an activity and transitions should be generated, false otherwise.
9236 * @return this object for method chaining
9237 */
9238 public WearableExtender setHintContentIntentLaunchesActivity(
9239 boolean hintContentIntentLaunchesActivity) {
9240 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
9241 return this;
9242 }
9243
9244 /**
9245 * Get a hint that this notification's content intent will launch an {@link Activity}
9246 * directly, telling the platform that it can generate the appropriate transitions
9247 * @return {@code true} if the content intent will launch an activity and transitions should
9248 * be generated, false otherwise. The default value is {@code false} if this was never set.
9249 */
9250 public boolean getHintContentIntentLaunchesActivity() {
9251 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
9252 }
9253
Nadia Benbernou948627e2016-04-14 14:41:08 -04009254 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009255 * Sets the dismissal id for this notification. If a notification is posted with a
9256 * dismissal id, then when that notification is canceled, notifications on other wearables
9257 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04009258 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009259 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04009260 * @param dismissalId the dismissal id of the notification.
9261 * @return this object for method chaining
9262 */
9263 public WearableExtender setDismissalId(String dismissalId) {
9264 mDismissalId = dismissalId;
9265 return this;
9266 }
9267
9268 /**
9269 * Returns the dismissal id of the notification.
9270 * @return the dismissal id of the notification or null if it has not been set.
9271 */
9272 public String getDismissalId() {
9273 return mDismissalId;
9274 }
9275
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009276 /**
9277 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
9278 * posted from a phone to provide finer-grained control on what notifications are bridged
9279 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
9280 * Features to Notifications</a> for more information.
9281 * @param bridgeTag the bridge tag of the notification.
9282 * @return this object for method chaining
9283 */
9284 public WearableExtender setBridgeTag(String bridgeTag) {
9285 mBridgeTag = bridgeTag;
9286 return this;
9287 }
9288
9289 /**
9290 * Returns the bridge tag of the notification.
9291 * @return the bridge tag or null if not present.
9292 */
9293 public String getBridgeTag() {
9294 return mBridgeTag;
9295 }
9296
Griff Hazen61a9e862014-05-22 16:05:19 -07009297 private void setFlag(int mask, boolean value) {
9298 if (value) {
9299 mFlags |= mask;
9300 } else {
9301 mFlags &= ~mask;
9302 }
9303 }
9304 }
9305
9306 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009307 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
9308 * with car extensions:
9309 *
9310 * <ol>
9311 * <li>Create an {@link Notification.Builder}, setting any desired
9312 * properties.
9313 * <li>Create a {@link CarExtender}.
9314 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
9315 * {@link CarExtender}.
9316 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9317 * to apply the extensions to a notification.
9318 * </ol>
9319 *
9320 * <pre class="prettyprint">
9321 * Notification notification = new Notification.Builder(context)
9322 * ...
9323 * .extend(new CarExtender()
9324 * .set*(...))
9325 * .build();
9326 * </pre>
9327 *
9328 * <p>Car extensions can be accessed on an existing notification by using the
9329 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
9330 * to access values.
9331 */
9332 public static final class CarExtender implements Extender {
9333 private static final String TAG = "CarExtender";
9334
9335 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
9336 private static final String EXTRA_LARGE_ICON = "large_icon";
9337 private static final String EXTRA_CONVERSATION = "car_conversation";
9338 private static final String EXTRA_COLOR = "app_color";
9339
9340 private Bitmap mLargeIcon;
9341 private UnreadConversation mUnreadConversation;
9342 private int mColor = Notification.COLOR_DEFAULT;
9343
9344 /**
9345 * Create a {@link CarExtender} with default options.
9346 */
9347 public CarExtender() {
9348 }
9349
9350 /**
9351 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
9352 *
9353 * @param notif The notification from which to copy options.
9354 */
9355 public CarExtender(Notification notif) {
9356 Bundle carBundle = notif.extras == null ?
9357 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
9358 if (carBundle != null) {
9359 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
9360 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
9361
9362 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
9363 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
9364 }
9365 }
9366
9367 /**
9368 * Apply car extensions to a notification that is being built. This is typically called by
9369 * the {@link Notification.Builder#extend(Notification.Extender)}
9370 * method of {@link Notification.Builder}.
9371 */
9372 @Override
9373 public Notification.Builder extend(Notification.Builder builder) {
9374 Bundle carExtensions = new Bundle();
9375
9376 if (mLargeIcon != null) {
9377 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
9378 }
9379 if (mColor != Notification.COLOR_DEFAULT) {
9380 carExtensions.putInt(EXTRA_COLOR, mColor);
9381 }
9382
9383 if (mUnreadConversation != null) {
9384 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
9385 carExtensions.putBundle(EXTRA_CONVERSATION, b);
9386 }
9387
9388 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
9389 return builder;
9390 }
9391
9392 /**
9393 * Sets the accent color to use when Android Auto presents the notification.
9394 *
9395 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
9396 * to accent the displayed notification. However, not all colors are acceptable in an
9397 * automotive setting. This method can be used to override the color provided in the
9398 * notification in such a situation.
9399 */
Tor Norbye80756e32015-03-02 09:39:27 -08009400 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009401 mColor = color;
9402 return this;
9403 }
9404
9405 /**
9406 * Gets the accent color.
9407 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04009408 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009409 */
Tor Norbye80756e32015-03-02 09:39:27 -08009410 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009411 public int getColor() {
9412 return mColor;
9413 }
9414
9415 /**
9416 * Sets the large icon of the car notification.
9417 *
9418 * If no large icon is set in the extender, Android Auto will display the icon
9419 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
9420 *
9421 * @param largeIcon The large icon to use in the car notification.
9422 * @return This object for method chaining.
9423 */
9424 public CarExtender setLargeIcon(Bitmap largeIcon) {
9425 mLargeIcon = largeIcon;
9426 return this;
9427 }
9428
9429 /**
9430 * Gets the large icon used in this car notification, or null if no icon has been set.
9431 *
9432 * @return The large icon for the car notification.
9433 * @see CarExtender#setLargeIcon
9434 */
9435 public Bitmap getLargeIcon() {
9436 return mLargeIcon;
9437 }
9438
9439 /**
9440 * Sets the unread conversation in a message notification.
9441 *
9442 * @param unreadConversation The unread part of the conversation this notification conveys.
9443 * @return This object for method chaining.
9444 */
9445 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
9446 mUnreadConversation = unreadConversation;
9447 return this;
9448 }
9449
9450 /**
9451 * Returns the unread conversation conveyed by this notification.
9452 * @see #setUnreadConversation(UnreadConversation)
9453 */
9454 public UnreadConversation getUnreadConversation() {
9455 return mUnreadConversation;
9456 }
9457
9458 /**
9459 * A class which holds the unread messages from a conversation.
9460 */
9461 public static class UnreadConversation {
9462 private static final String KEY_AUTHOR = "author";
9463 private static final String KEY_TEXT = "text";
9464 private static final String KEY_MESSAGES = "messages";
9465 private static final String KEY_REMOTE_INPUT = "remote_input";
9466 private static final String KEY_ON_REPLY = "on_reply";
9467 private static final String KEY_ON_READ = "on_read";
9468 private static final String KEY_PARTICIPANTS = "participants";
9469 private static final String KEY_TIMESTAMP = "timestamp";
9470
9471 private final String[] mMessages;
9472 private final RemoteInput mRemoteInput;
9473 private final PendingIntent mReplyPendingIntent;
9474 private final PendingIntent mReadPendingIntent;
9475 private final String[] mParticipants;
9476 private final long mLatestTimestamp;
9477
9478 UnreadConversation(String[] messages, RemoteInput remoteInput,
9479 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
9480 String[] participants, long latestTimestamp) {
9481 mMessages = messages;
9482 mRemoteInput = remoteInput;
9483 mReadPendingIntent = readPendingIntent;
9484 mReplyPendingIntent = replyPendingIntent;
9485 mParticipants = participants;
9486 mLatestTimestamp = latestTimestamp;
9487 }
9488
9489 /**
9490 * Gets the list of messages conveyed by this notification.
9491 */
9492 public String[] getMessages() {
9493 return mMessages;
9494 }
9495
9496 /**
9497 * Gets the remote input that will be used to convey the response to a message list, or
9498 * null if no such remote input exists.
9499 */
9500 public RemoteInput getRemoteInput() {
9501 return mRemoteInput;
9502 }
9503
9504 /**
9505 * Gets the pending intent that will be triggered when the user replies to this
9506 * notification.
9507 */
9508 public PendingIntent getReplyPendingIntent() {
9509 return mReplyPendingIntent;
9510 }
9511
9512 /**
9513 * Gets the pending intent that Android Auto will send after it reads aloud all messages
9514 * in this object's message list.
9515 */
9516 public PendingIntent getReadPendingIntent() {
9517 return mReadPendingIntent;
9518 }
9519
9520 /**
9521 * Gets the participants in the conversation.
9522 */
9523 public String[] getParticipants() {
9524 return mParticipants;
9525 }
9526
9527 /**
9528 * Gets the firs participant in the conversation.
9529 */
9530 public String getParticipant() {
9531 return mParticipants.length > 0 ? mParticipants[0] : null;
9532 }
9533
9534 /**
9535 * Gets the timestamp of the conversation.
9536 */
9537 public long getLatestTimestamp() {
9538 return mLatestTimestamp;
9539 }
9540
9541 Bundle getBundleForUnreadConversation() {
9542 Bundle b = new Bundle();
9543 String author = null;
9544 if (mParticipants != null && mParticipants.length > 1) {
9545 author = mParticipants[0];
9546 }
9547 Parcelable[] messages = new Parcelable[mMessages.length];
9548 for (int i = 0; i < messages.length; i++) {
9549 Bundle m = new Bundle();
9550 m.putString(KEY_TEXT, mMessages[i]);
9551 m.putString(KEY_AUTHOR, author);
9552 messages[i] = m;
9553 }
9554 b.putParcelableArray(KEY_MESSAGES, messages);
9555 if (mRemoteInput != null) {
9556 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
9557 }
9558 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
9559 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
9560 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
9561 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
9562 return b;
9563 }
9564
9565 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
9566 if (b == null) {
9567 return null;
9568 }
9569 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
9570 String[] messages = null;
9571 if (parcelableMessages != null) {
9572 String[] tmp = new String[parcelableMessages.length];
9573 boolean success = true;
9574 for (int i = 0; i < tmp.length; i++) {
9575 if (!(parcelableMessages[i] instanceof Bundle)) {
9576 success = false;
9577 break;
9578 }
9579 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
9580 if (tmp[i] == null) {
9581 success = false;
9582 break;
9583 }
9584 }
9585 if (success) {
9586 messages = tmp;
9587 } else {
9588 return null;
9589 }
9590 }
9591
9592 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
9593 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
9594
9595 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
9596
9597 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
9598 if (participants == null || participants.length != 1) {
9599 return null;
9600 }
9601
9602 return new UnreadConversation(messages,
9603 remoteInput,
9604 onReply,
9605 onRead,
9606 participants, b.getLong(KEY_TIMESTAMP));
9607 }
9608 };
9609
9610 /**
9611 * Builder class for {@link CarExtender.UnreadConversation} objects.
9612 */
9613 public static class Builder {
9614 private final List<String> mMessages = new ArrayList<String>();
9615 private final String mParticipant;
9616 private RemoteInput mRemoteInput;
9617 private PendingIntent mReadPendingIntent;
9618 private PendingIntent mReplyPendingIntent;
9619 private long mLatestTimestamp;
9620
9621 /**
9622 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
9623 *
9624 * @param name The name of the other participant in the conversation.
9625 */
9626 public Builder(String name) {
9627 mParticipant = name;
9628 }
9629
9630 /**
9631 * Appends a new unread message to the list of messages for this conversation.
9632 *
9633 * The messages should be added from oldest to newest.
9634 *
9635 * @param message The text of the new unread message.
9636 * @return This object for method chaining.
9637 */
9638 public Builder addMessage(String message) {
9639 mMessages.add(message);
9640 return this;
9641 }
9642
9643 /**
9644 * Sets the pending intent and remote input which will convey the reply to this
9645 * notification.
9646 *
9647 * @param pendingIntent The pending intent which will be triggered on a reply.
9648 * @param remoteInput The remote input parcelable which will carry the reply.
9649 * @return This object for method chaining.
9650 *
9651 * @see CarExtender.UnreadConversation#getRemoteInput
9652 * @see CarExtender.UnreadConversation#getReplyPendingIntent
9653 */
9654 public Builder setReplyAction(
9655 PendingIntent pendingIntent, RemoteInput remoteInput) {
9656 mRemoteInput = remoteInput;
9657 mReplyPendingIntent = pendingIntent;
9658
9659 return this;
9660 }
9661
9662 /**
9663 * Sets the pending intent that will be sent once the messages in this notification
9664 * are read.
9665 *
9666 * @param pendingIntent The pending intent to use.
9667 * @return This object for method chaining.
9668 */
9669 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
9670 mReadPendingIntent = pendingIntent;
9671 return this;
9672 }
9673
9674 /**
9675 * Sets the timestamp of the most recent message in an unread conversation.
9676 *
9677 * If a messaging notification has been posted by your application and has not
9678 * yet been cancelled, posting a later notification with the same id and tag
9679 * but without a newer timestamp may result in Android Auto not displaying a
9680 * heads up notification for the later notification.
9681 *
9682 * @param timestamp The timestamp of the most recent message in the conversation.
9683 * @return This object for method chaining.
9684 */
9685 public Builder setLatestTimestamp(long timestamp) {
9686 mLatestTimestamp = timestamp;
9687 return this;
9688 }
9689
9690 /**
9691 * Builds a new unread conversation object.
9692 *
9693 * @return The new unread conversation object.
9694 */
9695 public UnreadConversation build() {
9696 String[] messages = mMessages.toArray(new String[mMessages.size()]);
9697 String[] participants = { mParticipant };
9698 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
9699 mReadPendingIntent, participants, mLatestTimestamp);
9700 }
9701 }
9702 }
9703
9704 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009705 * <p>Helper class to add Android TV extensions to notifications. To create a notification
9706 * with a TV extension:
9707 *
9708 * <ol>
9709 * <li>Create an {@link Notification.Builder}, setting any desired properties.
9710 * <li>Create a {@link TvExtender}.
9711 * <li>Set TV-specific properties using the {@code set} methods of
9712 * {@link TvExtender}.
9713 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9714 * to apply the extension to a notification.
9715 * </ol>
9716 *
9717 * <pre class="prettyprint">
9718 * Notification notification = new Notification.Builder(context)
9719 * ...
9720 * .extend(new TvExtender()
9721 * .set*(...))
9722 * .build();
9723 * </pre>
9724 *
9725 * <p>TV extensions can be accessed on an existing notification by using the
9726 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
9727 * to access values.
9728 *
9729 * @hide
9730 */
9731 @SystemApi
9732 public static final class TvExtender implements Extender {
9733 private static final String TAG = "TvExtender";
9734
9735 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
9736 private static final String EXTRA_FLAGS = "flags";
9737 private static final String EXTRA_CONTENT_INTENT = "content_intent";
9738 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009739 private static final String EXTRA_CHANNEL_ID = "channel_id";
Rhiannon Malia1a083932018-01-24 15:02:30 -08009740 private static final String EXTRA_SUPPRESS_SHOW_OVER_APPS = "suppressShowOverApps";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009741
9742 // Flags bitwise-ored to mFlags
9743 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
9744
9745 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009746 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009747 private PendingIntent mContentIntent;
9748 private PendingIntent mDeleteIntent;
Rhiannon Malia1a083932018-01-24 15:02:30 -08009749 private boolean mSuppressShowOverApps;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009750
9751 /**
9752 * Create a {@link TvExtender} with default options.
9753 */
9754 public TvExtender() {
9755 mFlags = FLAG_AVAILABLE_ON_TV;
9756 }
9757
9758 /**
9759 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
9760 *
9761 * @param notif The notification from which to copy options.
9762 */
9763 public TvExtender(Notification notif) {
9764 Bundle bundle = notif.extras == null ?
9765 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
9766 if (bundle != null) {
9767 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009768 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Rhiannon Malia1a083932018-01-24 15:02:30 -08009769 mSuppressShowOverApps = bundle.getBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009770 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
9771 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
9772 }
9773 }
9774
9775 /**
9776 * Apply a TV extension to a notification that is being built. This is typically called by
9777 * the {@link Notification.Builder#extend(Notification.Extender)}
9778 * method of {@link Notification.Builder}.
9779 */
9780 @Override
9781 public Notification.Builder extend(Notification.Builder builder) {
9782 Bundle bundle = new Bundle();
9783
9784 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009785 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Rhiannon Malia1a083932018-01-24 15:02:30 -08009786 bundle.putBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS, mSuppressShowOverApps);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009787 if (mContentIntent != null) {
9788 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
9789 }
9790
9791 if (mDeleteIntent != null) {
9792 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
9793 }
9794
9795 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
9796 return builder;
9797 }
9798
9799 /**
9800 * Returns true if this notification should be shown on TV. This method return true
9801 * if the notification was extended with a TvExtender.
9802 */
9803 public boolean isAvailableOnTv() {
9804 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
9805 }
9806
9807 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009808 * Specifies the channel the notification should be delivered on when shown on TV.
9809 * It can be different from the channel that the notification is delivered to when
9810 * posting on a non-TV device.
9811 */
9812 public TvExtender setChannel(String channelId) {
9813 mChannelId = channelId;
9814 return this;
9815 }
9816
9817 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04009818 * Specifies the channel the notification should be delivered on when shown on TV.
9819 * It can be different from the channel that the notification is delivered to when
9820 * posting on a non-TV device.
9821 */
9822 public TvExtender setChannelId(String channelId) {
9823 mChannelId = channelId;
9824 return this;
9825 }
9826
Jeff Sharkey000ce802017-04-29 13:13:27 -06009827 /** @removed */
9828 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009829 public String getChannel() {
9830 return mChannelId;
9831 }
9832
9833 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04009834 * Returns the id of the channel this notification posts to on TV.
9835 */
9836 public String getChannelId() {
9837 return mChannelId;
9838 }
9839
9840 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009841 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
9842 * If provided, it is used instead of the content intent specified
9843 * at the level of Notification.
9844 */
9845 public TvExtender setContentIntent(PendingIntent intent) {
9846 mContentIntent = intent;
9847 return this;
9848 }
9849
9850 /**
9851 * Returns the TV-specific content intent. If this method returns null, the
9852 * main content intent on the notification should be used.
9853 *
9854 * @see {@link Notification#contentIntent}
9855 */
9856 public PendingIntent getContentIntent() {
9857 return mContentIntent;
9858 }
9859
9860 /**
9861 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
9862 * by the user on TV. If provided, it is used instead of the delete intent specified
9863 * at the level of Notification.
9864 */
9865 public TvExtender setDeleteIntent(PendingIntent intent) {
9866 mDeleteIntent = intent;
9867 return this;
9868 }
9869
9870 /**
9871 * Returns the TV-specific delete intent. If this method returns null, the
9872 * main delete intent on the notification should be used.
9873 *
9874 * @see {@link Notification#deleteIntent}
9875 */
9876 public PendingIntent getDeleteIntent() {
9877 return mDeleteIntent;
9878 }
Rhiannon Malia1a083932018-01-24 15:02:30 -08009879
9880 /**
9881 * Specifies whether this notification should suppress showing a message over top of apps
9882 * outside of the launcher.
9883 */
9884 public TvExtender setSuppressShowOverApps(boolean suppress) {
9885 mSuppressShowOverApps = suppress;
9886 return this;
9887 }
9888
9889 /**
9890 * Returns true if this notification should not show messages over top of apps
9891 * outside of the launcher.
9892 */
9893 public boolean getSuppressShowOverApps() {
9894 return mSuppressShowOverApps;
9895 }
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009896 }
9897
9898 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07009899 * Get an array of Notification objects from a parcelable array bundle field.
9900 * Update the bundle to have a typed array so fetches in the future don't need
9901 * to do an array copy.
9902 */
9903 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
9904 Parcelable[] array = bundle.getParcelableArray(key);
9905 if (array instanceof Notification[] || array == null) {
9906 return (Notification[]) array;
9907 }
9908 Notification[] typedArray = Arrays.copyOf(array, array.length,
9909 Notification[].class);
9910 bundle.putParcelableArray(key, typedArray);
9911 return typedArray;
9912 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02009913
9914 private static class BuilderRemoteViews extends RemoteViews {
9915 public BuilderRemoteViews(Parcel parcel) {
9916 super(parcel);
9917 }
9918
Kenny Guy77320062014-08-27 21:37:15 +01009919 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
9920 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02009921 }
9922
9923 @Override
9924 public BuilderRemoteViews clone() {
9925 Parcel p = Parcel.obtain();
9926 writeToParcel(p, 0);
9927 p.setDataPosition(0);
9928 BuilderRemoteViews brv = new BuilderRemoteViews(p);
9929 p.recycle();
9930 return brv;
9931 }
9932 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08009933
Selim Cinek384804b2018-04-18 14:31:07 +08009934 /**
9935 * A result object where information about the template that was created is saved.
9936 */
9937 private static class TemplateBindResult {
Selim Cinek1c72fa02018-04-23 18:00:54 +08009938 int mIconMarginEnd;
Selim Cinek6e41b0e2018-12-06 18:10:20 -08009939 boolean mRightIconContainerVisible;
Selim Cinek384804b2018-04-18 14:31:07 +08009940
9941 /**
Selim Cinek1c72fa02018-04-23 18:00:54 +08009942 * Get the margin end that needs to be added to any fields that may overlap
Selim Cinek384804b2018-04-18 14:31:07 +08009943 * with the right actions.
9944 */
Selim Cinek1c72fa02018-04-23 18:00:54 +08009945 public int getIconMarginEnd() {
9946 return mIconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +08009947 }
9948
Selim Cinek6e41b0e2018-12-06 18:10:20 -08009949 /**
9950 * Is the icon container visible on the right size because of the reply button or the
9951 * right icon.
9952 */
9953 public boolean isRightIconContainerVisible() {
9954 return mRightIconContainerVisible;
9955 }
9956
Selim Cinek1c72fa02018-04-23 18:00:54 +08009957 public void setIconMarginEnd(int iconMarginEnd) {
9958 this.mIconMarginEnd = iconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +08009959 }
Selim Cinek6e41b0e2018-12-06 18:10:20 -08009960
9961 public void setRightIconContainerVisible(boolean iconContainerVisible) {
9962 mRightIconContainerVisible = iconContainerVisible;
9963 }
Selim Cinek384804b2018-04-18 14:31:07 +08009964 }
9965
Adrian Roos70d7aa32017-01-11 15:39:06 -08009966 private static class StandardTemplateParams {
9967 boolean hasProgress = true;
9968 boolean ambient = false;
9969 CharSequence title;
9970 CharSequence text;
Selim Cinekafeed292017-12-12 17:32:44 -08009971 CharSequence headerTextSecondary;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08009972 CharSequence summaryText;
Selim Cinekbee4e072018-05-21 22:06:43 -07009973 int maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek88188f22017-09-19 16:46:56 -07009974 boolean hideLargeIcon;
Selim Cinek384804b2018-04-18 14:31:07 +08009975 boolean hideReplyIcon;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08009976 boolean allowColorization = true;
9977 boolean forceDefaultColor = false;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009978
9979 final StandardTemplateParams reset() {
9980 hasProgress = true;
9981 ambient = false;
9982 title = null;
9983 text = null;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08009984 summaryText = null;
Selim Cinekafeed292017-12-12 17:32:44 -08009985 headerTextSecondary = null;
Selim Cinekbee4e072018-05-21 22:06:43 -07009986 maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08009987 allowColorization = true;
9988 forceDefaultColor = false;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009989 return this;
9990 }
9991
9992 final StandardTemplateParams hasProgress(boolean hasProgress) {
9993 this.hasProgress = hasProgress;
9994 return this;
9995 }
9996
9997 final StandardTemplateParams title(CharSequence title) {
9998 this.title = title;
9999 return this;
10000 }
10001
10002 final StandardTemplateParams text(CharSequence text) {
10003 this.text = text;
10004 return this;
10005 }
10006
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010007 final StandardTemplateParams summaryText(CharSequence text) {
10008 this.summaryText = text;
10009 return this;
10010 }
10011
Selim Cinekafeed292017-12-12 17:32:44 -080010012 final StandardTemplateParams headerTextSecondary(CharSequence text) {
10013 this.headerTextSecondary = text;
10014 return this;
10015 }
10016
Selim Cinek384804b2018-04-18 14:31:07 +080010017 final StandardTemplateParams hideLargeIcon(boolean hideLargeIcon) {
10018 this.hideLargeIcon = hideLargeIcon;
Selim Cinek88188f22017-09-19 16:46:56 -070010019 return this;
10020 }
10021
Selim Cinek384804b2018-04-18 14:31:07 +080010022 final StandardTemplateParams hideReplyIcon(boolean hideReplyIcon) {
10023 this.hideReplyIcon = hideReplyIcon;
Selim Cinek88188f22017-09-19 16:46:56 -070010024 return this;
10025 }
10026
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010027 final StandardTemplateParams disallowColorization() {
10028 this.allowColorization = false;
10029 return this;
10030 }
10031
10032 final StandardTemplateParams forceDefaultColor() {
10033 this.forceDefaultColor = true;
10034 return this;
10035 }
10036
Adrian Roos70d7aa32017-01-11 15:39:06 -080010037 final StandardTemplateParams ambient(boolean ambient) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -080010038 Preconditions.checkState(title == null && text == null, "must set ambient before text");
Adrian Roos70d7aa32017-01-11 15:39:06 -080010039 this.ambient = ambient;
10040 return this;
10041 }
10042
10043 final StandardTemplateParams fillTextsFrom(Builder b) {
10044 Bundle extras = b.mN.extras;
Lucas Dupin06c5e642017-09-13 16:34:58 -070010045 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE), ambient);
10046
10047 // Big text notifications should contain their content when viewed in ambient mode.
10048 CharSequence text = extras.getCharSequence(EXTRA_BIG_TEXT);
10049 if (!ambient || TextUtils.isEmpty(text)) {
10050 text = extras.getCharSequence(EXTRA_TEXT);
10051 }
10052 this.text = b.processLegacyText(text, ambient);
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010053 this.summaryText = extras.getCharSequence(EXTRA_SUB_TEXT);
Adrian Roos70d7aa32017-01-11 15:39:06 -080010054 return this;
10055 }
Selim Cinekbee4e072018-05-21 22:06:43 -070010056
10057 /**
10058 * Set the maximum lines of remote input history lines allowed.
10059 * @param maxRemoteInputHistory The number of lines.
10060 * @return The builder for method chaining.
10061 */
10062 public StandardTemplateParams setMaxRemoteInputHistory(int maxRemoteInputHistory) {
10063 this.maxRemoteInputHistory = maxRemoteInputHistory;
10064 return this;
10065 }
Adrian Roos70d7aa32017-01-11 15:39:06 -080010066 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010067}