blob: 028e3efc9fb837d978e255fcc74409aff343e108 [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;
Griff Hazen959591e2014-05-15 22:26:18 -070087
Tor Norbyed9273d62013-05-30 15:59:53 -070088import java.lang.annotation.Retention;
89import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020090import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050091import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070092import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070093import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070094import java.util.List;
Julia Reynolds7217dc92018-03-07 12:12:09 -050095import java.util.Objects;
Dan Sandler50128532015-12-08 15:42:41 -050096import java.util.Set;
Jeff Sharkey23b31182018-04-18 21:32:12 -060097import java.util.function.Consumer;
Joe Onorato561d3852010-11-20 18:09:34 -080098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099/**
100 * A class that represents how a persistent notification is to be presented to
101 * the user using the {@link android.app.NotificationManager}.
102 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800103 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
104 * easier to construct Notifications.</p>
105 *
Joe Fernandez558459f2011-10-13 16:47:36 -0700106 * <div class="special reference">
107 * <h3>Developer Guides</h3>
108 * <p>For a guide to creating notifications, read the
109 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
110 * developer guide.</p>
111 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 */
113public class Notification implements Parcelable
114{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -0400115 private static final String TAG = "Notification";
116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -0400118 * An activity that provides a user interface for adjusting notification preferences for its
Julia Reynolds3aedded2017-03-31 14:42:09 -0400119 * containing application.
Daniel Sandler01df1c62014-06-09 10:54:01 -0400120 */
121 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
122 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
123 = "android.intent.category.NOTIFICATION_PREFERENCES";
124
125 /**
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500126 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
127 * contain a {@link NotificationChannel#getId() channel id} that can be used to narrow down
Julia Reynolds3aedded2017-03-31 14:42:09 -0400128 * what settings should be shown in the target app.
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500129 */
130 public static final String EXTRA_CHANNEL_ID = "android.intent.extra.CHANNEL_ID";
131
132 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -0400133 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds005c8b92017-08-24 10:35:53 -0400134 * contain a {@link NotificationChannelGroup#getId() group id} that can be used to narrow down
135 * what settings should be shown in the target app.
136 */
137 public static final String EXTRA_CHANNEL_GROUP_ID = "android.intent.extra.CHANNEL_GROUP_ID";
138
139 /**
140 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds3aedded2017-03-31 14:42:09 -0400141 * contain the tag provided to {@link NotificationManager#notify(String, int, Notification)}
142 * that can be used to narrow down what settings should be shown in the target app.
143 */
144 public static final String EXTRA_NOTIFICATION_TAG = "android.intent.extra.NOTIFICATION_TAG";
145
146 /**
147 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
148 * contain the id provided to {@link NotificationManager#notify(String, int, Notification)}
149 * that can be used to narrow down what settings should be shown in the target app.
150 */
151 public static final String EXTRA_NOTIFICATION_ID = "android.intent.extra.NOTIFICATION_ID";
152
153 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 * Use all default values (where applicable).
155 */
156 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 /**
159 * Use the default notification sound. This will ignore any given
160 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500161 *
Chris Wren47c20a12014-06-18 17:27:29 -0400162 * <p>
163 * A notification that is noisy is more likely to be presented as a heads-up notification.
164 * </p>
165 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500167 */
168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 public static final int DEFAULT_SOUND = 1;
170
171 /**
172 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500173 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700174 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500175 *
Chris Wren47c20a12014-06-18 17:27:29 -0400176 * <p>
177 * A notification that vibrates is more likely to be presented as a heads-up notification.
178 * </p>
179 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500181 */
182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 /**
186 * Use the default notification lights. This will ignore the
187 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
188 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500189 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500191 */
192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200196 * Maximum length of CharSequences accepted by Builder and friends.
197 *
198 * <p>
199 * Avoids spamming the system with overly large strings such as full e-mails.
200 */
201 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
202
203 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800204 * Maximum entries of reply text that are accepted by Builder and friends.
205 */
206 private static final int MAX_REPLY_HISTORY = 5;
207
Tony Mak09db2ea2018-06-27 18:12:48 +0100208 /**
Gustav Sennton1463d832018-11-06 16:12:48 +0000209 * Maximum number of (generic) action buttons in a notification (contextual action buttons are
210 * handled separately).
Tony Mak09db2ea2018-06-27 18:12:48 +0100211 * @hide
212 */
213 public static final int MAX_ACTION_BUTTONS = 3;
Selim Cinekde4de0e2018-01-24 16:21:07 -0800214
215 /**
216 * If the notification contained an unsent draft for a RemoteInput when the user clicked on it,
217 * we're adding the draft as a String extra to the {@link #contentIntent} using this key.
218 *
219 * <p>Apps may use this extra to prepopulate text fields in the app, where the user usually
220 * sends messages.</p>
221 */
222 public static final String EXTRA_REMOTE_INPUT_DRAFT = "android.remoteInputDraft";
223
Adrian Roose458aa82015-12-08 16:17:19 -0800224 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500225 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800226 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500227 * Default value: {@link System#currentTimeMillis() Now}.
228 *
229 * Choose a timestamp that will be most relevant to the user. For most finite events, this
230 * corresponds to the time the event happened (or will happen, in the case of events that have
231 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800232 * timestamped according to when the activity began.
233 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500234 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800235 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500236 * <ul>
237 * <li>Notification of a new chat message should be stamped when the message was received.</li>
238 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
239 * <li>Notification of a completed file download should be stamped when the download finished.</li>
240 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
241 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
242 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800243 * </ul>
244 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700245 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
246 * anymore by default and must be opted into by using
247 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 */
249 public long when;
250
251 /**
Selim Cinekb85f36fd2016-04-20 18:46:36 -0700252 * The creation time of the notification
253 */
254 private long creationTime;
255
256 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400258 *
259 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 */
Dan Sandler86647982015-05-13 23:41:13 -0400261 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700262 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 public int icon;
264
265 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800266 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
267 * leave it at its default value of 0.
268 *
269 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700270 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800271 */
272 public int iconLevel;
273
274 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500275 * The number of events that this notification represents. For example, in a new mail
276 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800277 *
Julia Reynolds30331982017-04-27 10:12:50 -0400278 * The system may or may not use this field to modify the appearance of the notification.
Julia Reynolds13d898c2017-02-02 12:22:05 -0500279 * Starting with {@link android.os.Build.VERSION_CODES#O}, the number may be displayed as a
280 * badge icon in Launchers that support badging.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 */
Julia Reynolds30331982017-04-27 10:12:50 -0400282 public int number = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283
284 /**
285 * The intent to execute when the expanded status entry is clicked. If
286 * this is an activity, it must include the
287 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800288 * that you take care of task management as described in the
289 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800290 * Stack</a> document. In particular, make sure to read the notification section
291 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
292 * Notifications</a> for the correct ways to launch an application from a
293 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 */
295 public PendingIntent contentIntent;
296
297 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500298 * The intent to execute when the notification is explicitly dismissed by the user, either with
299 * the "Clear All" button or by swiping it away individually.
300 *
301 * This probably shouldn't be launching an activity since several of those will be sent
302 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 */
304 public PendingIntent deleteIntent;
305
306 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700307 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800308 *
Chris Wren47c20a12014-06-18 17:27:29 -0400309 * <p>
310 * The system UI may choose to display a heads-up notification, instead of
311 * launching this intent, while the user is using the device.
312 * </p>
313 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800314 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400315 */
316 public PendingIntent fullScreenIntent;
317
318 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400319 * Text that summarizes this notification for accessibility services.
320 *
321 * As of the L release, this text is no longer shown on screen, but it is still useful to
322 * accessibility services (where it serves as an audible announcement of the notification's
323 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400324 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800325 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 */
327 public CharSequence tickerText;
328
329 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400330 * Formerly, a view showing the {@link #tickerText}.
331 *
332 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400333 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400334 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800335 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400336
337 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400338 * The view that will represent this notification in the notification list (which is pulled
339 * down from the status bar).
340 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500341 * As of N, this field may be null. The notification view is determined by the inputs
342 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400343 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400345 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 public RemoteViews contentView;
347
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400348 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400349 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400350 * opportunity to show more detail. The system UI may choose to show this
351 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400352 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500353 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400354 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
355 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400356 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400357 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400358 public RemoteViews bigContentView;
359
Chris Wren8fd39ec2014-02-27 17:43:26 -0500360
361 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400362 * A medium-format version of {@link #contentView}, providing the Notification an
363 * opportunity to add action buttons to contentView. At its discretion, the system UI may
364 * choose to show this as a heads-up notification, which will pop up so the user can see
365 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400366 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500367 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400368 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
369 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500370 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400371 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500372 public RemoteViews headsUpContentView;
373
Julia Reynoldsfc640012018-02-21 12:25:27 -0500374 private boolean mUsesStandardHeader;
375
376 private static final ArraySet<Integer> STANDARD_LAYOUTS = new ArraySet<>();
377 static {
378 STANDARD_LAYOUTS.add(R.layout.notification_template_material_base);
379 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_base);
380 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_picture);
381 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_text);
382 STANDARD_LAYOUTS.add(R.layout.notification_template_material_inbox);
383 STANDARD_LAYOUTS.add(R.layout.notification_template_material_messaging);
384 STANDARD_LAYOUTS.add(R.layout.notification_template_material_media);
385 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_media);
Julia Reynoldsfc640012018-02-21 12:25:27 -0500386 STANDARD_LAYOUTS.add(R.layout.notification_template_header);
Julia Reynoldsfc640012018-02-21 12:25:27 -0500387 }
388
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400389 /**
Dan Sandler86647982015-05-13 23:41:13 -0400390 * A large bitmap to be shown in the notification content area.
391 *
392 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 */
Dan Sandler86647982015-05-13 23:41:13 -0400394 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800395 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396
397 /**
398 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500399 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400401 * A notification that is noisy is more likely to be presented as a heads-up notification.
402 * </p>
403 *
404 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500405 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500407 * @deprecated use {@link NotificationChannel#getSound()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500409 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 public Uri sound;
411
412 /**
413 * Use this constant as the value for audioStreamType to request that
414 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700415 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400416 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500417 * @deprecated Use {@link NotificationChannel#getAudioAttributes()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700419 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 public static final int STREAM_DEFAULT = -1;
421
422 /**
423 * The audio stream type to use when playing the sound.
424 * Should be one of the STREAM_ constants from
425 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400426 *
427 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700429 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 public int audioStreamType = STREAM_DEFAULT;
431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400433 * The default value of {@link #audioAttributes}.
434 */
435 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
436 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
437 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
438 .build();
439
440 /**
441 * The {@link AudioAttributes audio attributes} to use when playing the sound.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500442 *
443 * @deprecated use {@link NotificationChannel#getAudioAttributes()} instead.
John Spurlockc0650f022014-07-19 13:22:39 -0400444 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500445 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -0400446 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
447
448 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500449 * The pattern with which to vibrate.
450 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 * <p>
452 * To vibrate the default pattern, see {@link #defaults}.
453 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500454 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 * @see android.os.Vibrator#vibrate(long[],int)
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500456 * @deprecated use {@link NotificationChannel#getVibrationPattern()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500458 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 public long[] vibrate;
460
461 /**
462 * The color of the led. The hardware will do its best approximation.
463 *
464 * @see #FLAG_SHOW_LIGHTS
465 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500466 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 */
Tor Norbye80756e32015-03-02 09:39:27 -0800468 @ColorInt
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500469 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 public int ledARGB;
471
472 /**
473 * The number of milliseconds for the LED to be on while it's flashing.
474 * The hardware will do its best approximation.
475 *
476 * @see #FLAG_SHOW_LIGHTS
477 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500478 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500480 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 public int ledOnMS;
482
483 /**
484 * The number of milliseconds for the LED to be off while it's flashing.
485 * The hardware will do its best approximation.
486 *
487 * @see #FLAG_SHOW_LIGHTS
488 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500489 *
490 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500492 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 public int ledOffMS;
494
495 /**
496 * Specifies which values should be taken from the defaults.
497 * <p>
498 * To set, OR the desired from {@link #DEFAULT_SOUND},
499 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
500 * values, use {@link #DEFAULT_ALL}.
501 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500502 *
503 * @deprecated use {@link NotificationChannel#getSound()} and
504 * {@link NotificationChannel#shouldShowLights()} and
505 * {@link NotificationChannel#shouldVibrate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500507 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 public int defaults;
509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 /**
511 * Bit to be bitwise-ored into the {@link #flags} field that should be
512 * set if you want the LED on for this notification.
513 * <ul>
514 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
515 * or 0 for both ledOnMS and ledOffMS.</li>
516 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
517 * <li>To flash the LED, pass the number of milliseconds that it should
518 * be on and off to ledOnMS and ledOffMS.</li>
519 * </ul>
520 * <p>
521 * Since hardware varies, you are not guaranteed that any of the values
Ricardo Loo Forondaf8f6d0a2018-01-25 08:42:43 -0800522 * you pass are honored exactly. Use the system defaults if possible
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 * because they will be set to values that work on any given hardware.
524 * <p>
525 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500526 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500527 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500529 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
531
532 /**
533 * Bit to be bitwise-ored into the {@link #flags} field that should be
534 * set if this notification is in reference to something that is ongoing,
535 * like a phone call. It should not be set if this notification is in
536 * reference to something that happened at a particular point in time,
537 * like a missed phone call.
538 */
539 public static final int FLAG_ONGOING_EVENT = 0x00000002;
540
541 /**
542 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700543 * the audio will be repeated until the notification is
544 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 */
546 public static final int FLAG_INSISTENT = 0x00000004;
547
548 /**
549 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700550 * set if you would only like the sound, vibrate and ticker to be played
551 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 */
553 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
554
555 /**
556 * Bit to be bitwise-ored into the {@link #flags} field that should be
557 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500558 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 */
560 public static final int FLAG_AUTO_CANCEL = 0x00000010;
561
562 /**
563 * Bit to be bitwise-ored into the {@link #flags} field that should be
564 * set if the notification should not be canceled when the user clicks
565 * the Clear all button.
566 */
567 public static final int FLAG_NO_CLEAR = 0x00000020;
568
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700569 /**
570 * Bit to be bitwise-ored into the {@link #flags} field that should be
571 * set if this notification represents a currently running service. This
572 * will normally be set for you by {@link Service#startForeground}.
573 */
574 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
575
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400576 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500577 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800578 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500579 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400580 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700581 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500582 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400583
Griff Hazendfcb0802014-02-11 12:00:00 -0800584 /**
585 * Bit to be bitswise-ored into the {@link #flags} field that should be
586 * set if this notification is relevant to the current device only
587 * and it is not recommended that it bridge to other devices.
588 */
589 public static final int FLAG_LOCAL_ONLY = 0x00000100;
590
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700591 /**
592 * Bit to be bitswise-ored into the {@link #flags} field that should be
593 * set if this notification is the group summary for a group of notifications.
594 * Grouped notifications may display in a cluster or stack on devices which
595 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
596 */
597 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
598
Julia Reynoldse46bb372016-03-17 11:05:58 -0400599 /**
600 * Bit to be bitswise-ored into the {@link #flags} field that should be
601 * set if this notification is the group summary for an auto-group of notifications.
602 *
603 * @hide
604 */
605 @SystemApi
606 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
607
Julia Reynolds4db59552017-06-30 13:34:01 -0400608 /**
609 * @hide
610 */
611 public static final int FLAG_CAN_COLORIZE = 0x00000800;
612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 public int flags;
614
Tor Norbyed9273d62013-05-30 15:59:53 -0700615 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700616 @IntDef(prefix = { "PRIORITY_" }, value = {
617 PRIORITY_DEFAULT,
618 PRIORITY_LOW,
619 PRIORITY_MIN,
620 PRIORITY_HIGH,
621 PRIORITY_MAX
622 })
Tor Norbyed9273d62013-05-30 15:59:53 -0700623 @Retention(RetentionPolicy.SOURCE)
624 public @interface Priority {}
625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500627 * Default notification {@link #priority}. If your application does not prioritize its own
628 * notifications, use this value for all notifications.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500629 *
630 * @deprecated use {@link NotificationManager#IMPORTANCE_DEFAULT} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500631 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500632 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500633 public static final int PRIORITY_DEFAULT = 0;
634
635 /**
636 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
637 * items smaller, or at a different position in the list, compared with your app's
638 * {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500639 *
640 * @deprecated use {@link NotificationManager#IMPORTANCE_LOW} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500641 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500642 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500643 public static final int PRIORITY_LOW = -1;
644
645 /**
646 * Lowest {@link #priority}; these items might not be shown to the user except under special
647 * circumstances, such as detailed notification logs.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500648 *
649 * @deprecated use {@link NotificationManager#IMPORTANCE_MIN} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500650 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500651 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500652 public static final int PRIORITY_MIN = -2;
653
654 /**
655 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
656 * show these items larger, or at a different position in notification lists, compared with
657 * your app's {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500658 *
659 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500660 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500661 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500662 public static final int PRIORITY_HIGH = 1;
663
664 /**
665 * Highest {@link #priority}, for your application's most important items that require the
666 * user's prompt attention or input.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500667 *
668 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500669 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500670 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500671 public static final int PRIORITY_MAX = 2;
672
673 /**
674 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800675 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500676 * Priority is an indication of how much of the user's valuable attention should be consumed by
677 * this notification. Low-priority notifications may be hidden from the user in certain
678 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500679 * system will make a determination about how to interpret this priority when presenting
680 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400681 *
682 * <p>
683 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
684 * as a heads-up notification.
685 * </p>
686 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500687 * @deprecated use {@link NotificationChannel#getImportance()} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500688 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700689 @Priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500690 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500691 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800692
Dan Sandler26e81cf2014-05-06 10:01:27 -0400693 /**
694 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
695 * to be applied by the standard Style templates when presenting this notification.
696 *
697 * The current template design constructs a colorful header image by overlaying the
698 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
699 * ignored.
700 */
Tor Norbye80756e32015-03-02 09:39:27 -0800701 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400702 public int color = COLOR_DEFAULT;
703
704 /**
705 * Special value of {@link #color} telling the system not to decorate this notification with
706 * any special color but instead use default colors when presenting this notification.
707 */
Tor Norbye80756e32015-03-02 09:39:27 -0800708 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400709 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600710
711 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800712 * Special value of {@link #color} used as a place holder for an invalid color.
Selim Cinek99104832017-01-25 14:47:33 -0800713 * @hide
Adrian Roos4ff3b122016-02-01 12:26:13 -0800714 */
715 @ColorInt
Selim Cinek99104832017-01-25 14:47:33 -0800716 public static final int COLOR_INVALID = 1;
Adrian Roos4ff3b122016-02-01 12:26:13 -0800717
718 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700719 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
720 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600721 * lockscreen).
722 *
723 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
724 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
725 * shown in all situations, but the contents are only available if the device is unlocked for
726 * the appropriate user.
727 *
728 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
729 * can be read even in an "insecure" context (that is, above a secure lockscreen).
730 * To modify the public version of this notification—for example, to redact some portions—see
731 * {@link Builder#setPublicVersion(Notification)}.
732 *
733 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
734 * and ticker until the user has bypassed the lockscreen.
735 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600736 public @Visibility int visibility;
737
738 /** @hide */
739 @IntDef(prefix = { "VISIBILITY_" }, value = {
740 VISIBILITY_PUBLIC,
741 VISIBILITY_PRIVATE,
742 VISIBILITY_SECRET,
743 })
744 @Retention(RetentionPolicy.SOURCE)
745 public @interface Visibility {}
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600746
Griff Hazenfc3922d2014-08-20 11:56:44 -0700747 /**
748 * Notification visibility: Show this notification in its entirety on all lockscreens.
749 *
750 * {@see #visibility}
751 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600752 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700753
754 /**
755 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
756 * private information on secure lockscreens.
757 *
758 * {@see #visibility}
759 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600760 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700761
762 /**
763 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
764 *
765 * {@see #visibility}
766 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600767 public static final int VISIBILITY_SECRET = -1;
768
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500769 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400770 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500771 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400772 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500773
774 /**
Adora Zhangaa90e872018-03-12 14:07:50 -0700775 * Notification category: map turn-by-turn navigation.
776 */
777 public static final String CATEGORY_NAVIGATION = "navigation";
778
779 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400780 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500781 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400782 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500783
784 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400785 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500786 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400787 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500788
789 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400790 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500791 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400792 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500793
794 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400795 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500796 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400797 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500798
799 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400800 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500801 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400802 public static final String CATEGORY_ALARM = "alarm";
803
804 /**
805 * Notification category: progress of a long-running background operation.
806 */
807 public static final String CATEGORY_PROGRESS = "progress";
808
809 /**
810 * Notification category: social network or sharing update.
811 */
812 public static final String CATEGORY_SOCIAL = "social";
813
814 /**
815 * Notification category: error in background operation or authentication status.
816 */
817 public static final String CATEGORY_ERROR = "err";
818
819 /**
820 * Notification category: media transport control for playback.
821 */
822 public static final String CATEGORY_TRANSPORT = "transport";
823
824 /**
825 * Notification category: system or device status update. Reserved for system use.
826 */
827 public static final String CATEGORY_SYSTEM = "sys";
828
829 /**
830 * Notification category: indication of running background service.
831 */
832 public static final String CATEGORY_SERVICE = "service";
833
834 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400835 * Notification category: a specific, timely recommendation for a single thing.
836 * For example, a news app might want to recommend a news story it believes the user will
837 * want to read next.
838 */
839 public static final String CATEGORY_RECOMMENDATION = "recommendation";
840
841 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400842 * Notification category: ongoing information about device or contextual status.
843 */
844 public static final String CATEGORY_STATUS = "status";
845
846 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400847 * Notification category: user-scheduled reminder.
848 */
849 public static final String CATEGORY_REMINDER = "reminder";
850
851 /**
Adora Zhangaa90e872018-03-12 14:07:50 -0700852 * Notification category: extreme car emergencies.
853 * @hide
854 */
855 @SystemApi
856 public static final String CATEGORY_CAR_EMERGENCY = "car_emergency";
857
858 /**
859 * Notification category: car warnings.
860 * @hide
861 */
862 @SystemApi
863 public static final String CATEGORY_CAR_WARNING = "car_warning";
864
865 /**
866 * Notification category: general car system information.
867 * @hide
868 */
869 @SystemApi
870 public static final String CATEGORY_CAR_INFORMATION = "car_information";
871
872 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400873 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
874 * that best describes this Notification. May be used by the system for ranking and filtering.
875 */
876 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500877
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100878 @UnsupportedAppUsage
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700879 private String mGroupKey;
880
881 /**
882 * Get the key used to group this notification into a cluster or stack
883 * with other notifications on devices which support such rendering.
884 */
885 public String getGroup() {
886 return mGroupKey;
887 }
888
889 private String mSortKey;
890
891 /**
892 * Get a sort key that orders this notification among other notifications from the
893 * same package. This can be useful if an external sort was already applied and an app
894 * would like to preserve this. Notifications will be sorted lexicographically using this
895 * value, although providing different priorities in addition to providing sort key may
896 * cause this value to be ignored.
897 *
898 * <p>This sort key can also be used to order members of a notification group. See
899 * {@link Builder#setGroup}.
900 *
901 * @see String#compareTo(String)
902 */
903 public String getSortKey() {
904 return mSortKey;
905 }
906
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500907 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400908 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400909 * <p>
910 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
911 * APIs, and are intended to be used by
912 * {@link android.service.notification.NotificationListenerService} implementations to extract
913 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500914 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400915 public Bundle extras = new Bundle();
916
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400917 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700918 * All pending intents in the notification as the system needs to be able to access them but
919 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700920 * custom parcelable objects.
921 *
922 * @hide
923 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100924 @UnsupportedAppUsage
Felipe Lemedd85da62016-06-28 11:29:54 -0700925 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700926
927 /**
Dianne Hackborn98305522017-05-05 17:53:53 -0700928 * Token identifying the notification that is applying doze/bgcheck whitelisting to the
929 * pending intents inside of it, so only those will get the behavior.
930 *
931 * @hide
932 */
Adrian Roosfb921842017-10-26 14:49:56 +0200933 private IBinder mWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -0700934
935 /**
936 * Must be set by a process to start associating tokens with Notification objects
937 * coming in to it. This is set by NotificationManagerService.
938 *
939 * @hide
940 */
941 static public IBinder processWhitelistToken;
942
943 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400944 * {@link #extras} key: this is the title of the notification,
945 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
946 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500947 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400948
949 /**
950 * {@link #extras} key: this is the title of the notification when shown in expanded form,
951 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
952 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400953 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400954
955 /**
956 * {@link #extras} key: this is the main text payload, as supplied to
957 * {@link Builder#setContentText(CharSequence)}.
958 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500959 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400960
961 /**
962 * {@link #extras} key: this is a third line of text, as supplied to
963 * {@link Builder#setSubText(CharSequence)}.
964 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400965 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400966
967 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800968 * {@link #extras} key: this is the remote input history, as supplied to
969 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700970 *
971 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
972 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
973 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
974 * notifications once the other party has responded).
975 *
976 * The extra with this key is of type CharSequence[] and contains the most recent entry at
977 * the 0 index, the second most recent at the 1 index, etc.
978 *
979 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800980 */
981 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
982
983 /**
Kenny Guya0f6de82018-04-06 16:20:16 +0100984 * {@link #extras} key: boolean as supplied to
985 * {@link Builder#setShowRemoteInputSpinner(boolean)}.
986 *
987 * If set to true, then the view displaying the remote input history from
988 * {@link Builder#setRemoteInputHistory(CharSequence[])} will have a progress spinner.
989 *
990 * @see Builder#setShowRemoteInputSpinner(boolean)
991 * @hide
992 */
993 public static final String EXTRA_SHOW_REMOTE_INPUT_SPINNER = "android.remoteInputSpinner";
994
995 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +0100996 * {@link #extras} key: boolean as supplied to
997 * {@link Builder#setHideSmartReplies(boolean)}.
998 *
999 * If set to true, then any smart reply buttons will be hidden.
1000 *
1001 * @see Builder#setHideSmartReplies(boolean)
1002 * @hide
1003 */
1004 public static final String EXTRA_HIDE_SMART_REPLIES = "android.hideSmartReplies";
1005
1006 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001007 * {@link #extras} key: this is a small piece of additional text as supplied to
1008 * {@link Builder#setContentInfo(CharSequence)}.
1009 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001010 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001011
1012 /**
1013 * {@link #extras} key: this is a line of summary information intended to be shown
1014 * alongside expanded notifications, as supplied to (e.g.)
1015 * {@link BigTextStyle#setSummaryText(CharSequence)}.
1016 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001017 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001018
1019 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02001020 * {@link #extras} key: this is the longer text shown in the big form of a
1021 * {@link BigTextStyle} notification, as supplied to
1022 * {@link BigTextStyle#bigText(CharSequence)}.
1023 */
1024 public static final String EXTRA_BIG_TEXT = "android.bigText";
1025
1026 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001027 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
1028 * supplied to {@link Builder#setSmallIcon(int)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -04001029 *
1030 * @deprecated Use {@link #getSmallIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001031 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04001032 @Deprecated
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001033 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001034
1035 /**
1036 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
1037 * notification payload, as
1038 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -04001039 *
1040 * @deprecated Use {@link #getLargeIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001041 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04001042 @Deprecated
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001043 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001044
1045 /**
1046 * {@link #extras} key: this is a bitmap to be used instead of the one from
1047 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
1048 * shown in its expanded form, as supplied to
1049 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
1050 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001051 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001052
1053 /**
1054 * {@link #extras} key: this is the progress value supplied to
1055 * {@link Builder#setProgress(int, int, boolean)}.
1056 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001057 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001058
1059 /**
1060 * {@link #extras} key: this is the maximum value supplied to
1061 * {@link Builder#setProgress(int, int, boolean)}.
1062 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001063 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001064
1065 /**
1066 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
1067 * {@link Builder#setProgress(int, int, boolean)}.
1068 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001069 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001070
1071 /**
1072 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
1073 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
1074 * {@link Builder#setUsesChronometer(boolean)}.
1075 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001076 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001077
1078 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08001079 * {@link #extras} key: whether the chronometer set on the notification should count down
1080 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -07001081 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -08001082 */
Adrian Roos96b7e202016-05-17 13:50:38 -07001083 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -08001084
1085 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001086 * {@link #extras} key: whether {@link #when} should be shown,
1087 * as supplied to {@link Builder#setShowWhen(boolean)}.
1088 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001089 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001090
1091 /**
1092 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
1093 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
1094 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001095 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001096
1097 /**
1098 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
1099 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
1100 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001101 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -04001102
1103 /**
1104 * {@link #extras} key: A string representing the name of the specific
1105 * {@link android.app.Notification.Style} used to create this notification.
1106 */
Chris Wren91ad5632013-06-05 15:05:57 -04001107 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001108
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001109 /**
Chris Wrene6c48932014-09-29 17:19:27 -04001110 * {@link #extras} key: A String array containing the people that this notification relates to,
1111 * each of which was supplied to {@link Builder#addPerson(String)}.
Selim Cineke7238dd2017-12-14 17:48:32 -08001112 *
1113 * @deprecated the actual objects are now in {@link #EXTRA_PEOPLE_LIST}
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001114 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001115 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001116
1117 /**
Selim Cineke7238dd2017-12-14 17:48:32 -08001118 * {@link #extras} key: An arrayList of {@link Person} objects containing the people that
1119 * this notification relates to.
1120 */
1121 public static final String EXTRA_PEOPLE_LIST = "android.people.list";
1122
1123 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04001124 * Allow certain system-generated notifications to appear before the device is provisioned.
1125 * Only available to notifications coming from the android package.
1126 * @hide
1127 */
Maurice Lam96c10032017-03-29 15:42:38 -07001128 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001129 @RequiresPermission(android.Manifest.permission.NOTIFICATION_DURING_SETUP)
John Spurlockfd7f1e02014-03-18 16:41:57 -04001130 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
1131
1132 /**
Robin Leed107af62018-04-27 13:55:56 +02001133 * {@link #extras} key:
1134 * flat {@link String} representation of a {@link android.content.ContentUris content URI}
1135 * pointing to an image that can be displayed in the background when the notification is
1136 * selected. Used on television platforms. The URI must point to an image stream suitable for
1137 * passing into {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001138 * BitmapFactory.decodeStream}; all other content types will be ignored.
Jose Limae9e3b3b2014-05-18 23:44:50 -07001139 */
1140 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
1141
1142 /**
Dan Sandler842dd772014-05-15 09:36:47 -04001143 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -07001144 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -04001145 * {@link android.app.Notification.MediaStyle} notification.
1146 */
1147 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
1148
1149 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +01001150 * {@link #extras} key: the indices of actions to be shown in the compact view,
1151 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
1152 */
1153 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
1154
Christoph Studer943aa672014-08-03 20:31:16 +02001155 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04001156 * {@link #extras} key: the username to be displayed for all messages sent by the user including
1157 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -07001158 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1159 * {@link CharSequence}
Selim Cinekcb8b9852017-12-15 18:01:52 -08001160 *
1161 * @deprecated use {@link #EXTRA_MESSAGING_PERSON}
Alex Hillsfc737de2016-03-23 17:33:02 -04001162 */
1163 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
1164
1165 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08001166 * {@link #extras} key: the person to be displayed for all messages sent by the user including
1167 * direct replies
1168 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1169 * {@link Person}
1170 */
1171 public static final String EXTRA_MESSAGING_PERSON = "android.messagingUser";
1172
1173 /**
Adrian Roos96b7e202016-05-17 13:50:38 -07001174 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -04001175 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -04001176 */
Alex Hillsd9b04d92016-04-11 16:38:16 -04001177 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -04001178
1179 /**
1180 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
1181 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -07001182 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1183 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -04001184 */
1185 public static final String EXTRA_MESSAGES = "android.messages";
1186
1187 /**
Adrian Roos437cd562017-01-18 15:47:03 -08001188 * {@link #extras} key: an array of
1189 * {@link android.app.Notification.MessagingStyle#addHistoricMessage historic}
1190 * {@link android.app.Notification.MessagingStyle.Message} bundles provided by a
1191 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1192 * array of bundles.
1193 */
1194 public static final String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
1195
1196 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08001197 * {@link #extras} key: whether the {@link android.app.Notification.MessagingStyle} notification
1198 * represents a group conversation.
1199 */
1200 public static final String EXTRA_IS_GROUP_CONVERSATION = "android.isGroupConversation";
1201
1202 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08001203 * {@link #extras} key: whether the notification should be colorized as
1204 * supplied to {@link Builder#setColorized(boolean)}}.
1205 */
1206 public static final String EXTRA_COLORIZED = "android.colorized";
1207
1208 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001209 * @hide
1210 */
1211 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
1212
Selim Cinek247fa012016-02-18 09:50:48 -08001213 /**
1214 * @hide
1215 */
1216 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
1217
Shane Brennan472a3b32016-12-12 15:28:10 -08001218 /**
Selim Cinekd0426622017-07-11 13:19:59 +02001219 * @hide
1220 */
1221 public static final String EXTRA_REDUCED_IMAGES = "android.reduced.images";
1222
1223 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001224 * {@link #extras} key: the audio contents of this notification.
1225 *
1226 * This is for use when rendering the notification on an audio-focused interface;
1227 * the audio contents are a complete sound sample that contains the contents/body of the
1228 * notification. This may be used in substitute of a Text-to-Speech reading of the
1229 * notification. For example if the notification represents a voice message this should point
1230 * to the audio of that message.
1231 *
1232 * The data stored under this key should be a String representation of a Uri that contains the
1233 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1234 *
1235 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1236 * has a field for holding data URI. That field can be used for audio.
1237 * See {@code Message#setData}.
1238 *
1239 * Example usage:
1240 * <pre>
1241 * {@code
1242 * Notification.Builder myBuilder = (build your Notification as normal);
1243 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1244 * }
1245 * </pre>
1246 */
1247 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1248
Dan Sandler80eaa592016-04-14 23:34:54 -04001249 /** @hide */
1250 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001251 @RequiresPermission(android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME)
Dan Sandler732bd6c2016-04-12 14:20:32 -04001252 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1253
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001254 /**
Julia Reynolds3c7de112018-03-28 09:33:13 -04001255 * This is set on the notifications shown by system_server about apps running foreground
1256 * services. It indicates that the notification should be shown
1257 * only if any of the given apps do not already have a properly tagged
1258 * {@link #FLAG_FOREGROUND_SERVICE} notification currently visible to the user.
1259 * This is a string array of all package names of the apps.
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001260 * @hide
1261 */
1262 public static final String EXTRA_FOREGROUND_APPS = "android.foregroundApps";
1263
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001264 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04001265 private Icon mSmallIcon;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001266 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04001267 private Icon mLargeIcon;
1268
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001269 @UnsupportedAppUsage
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001270 private String mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001271 private long mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001272
Julia Reynolds13d898c2017-02-02 12:22:05 -05001273 private String mShortcutId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04001274 private CharSequence mSettingsText;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001275
Mady Mellorc39b4ae2019-01-09 17:11:37 -08001276 private BubbleMetadata mBubbleMetadata;
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04001277
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001278 /** @hide */
1279 @IntDef(prefix = { "GROUP_ALERT_" }, value = {
1280 GROUP_ALERT_ALL, GROUP_ALERT_CHILDREN, GROUP_ALERT_SUMMARY
1281 })
1282 @Retention(RetentionPolicy.SOURCE)
1283 public @interface GroupAlertBehavior {}
1284
1285 /**
1286 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all notifications in a
1287 * group with sound or vibration ought to make sound or vibrate (respectively), so this
1288 * notification will not be muted when it is in a group.
1289 */
1290 public static final int GROUP_ALERT_ALL = 0;
1291
1292 /**
1293 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all children
1294 * notification in a group should be silenced (no sound or vibration) even if they are posted
1295 * to a {@link NotificationChannel} that has sound and/or vibration. Use this constant to
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001296 * mute this notification if this notification is a group child. This must be applied to all
1297 * children notifications you want to mute.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001298 *
1299 * <p> For example, you might want to use this constant if you post a number of children
1300 * notifications at once (say, after a periodic sync), and only need to notify the user
1301 * audibly once.
1302 */
1303 public static final int GROUP_ALERT_SUMMARY = 1;
1304
1305 /**
1306 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that the summary
1307 * notification in a group should be silenced (no sound or vibration) even if they are
1308 * posted to a {@link NotificationChannel} that has sound and/or vibration. Use this constant
1309 * to mute this notification if this notification is a group summary.
1310 *
1311 * <p>For example, you might want to use this constant if only the children notifications
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001312 * in your group have content and the summary is only used to visually group notifications
1313 * rather than to alert the user that new information is available.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001314 */
1315 public static final int GROUP_ALERT_CHILDREN = 2;
1316
Julia Reynolds2f431e22017-06-07 14:12:09 +00001317 private int mGroupAlertBehavior = GROUP_ALERT_ALL;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001318
Julia Reynolds13d898c2017-02-02 12:22:05 -05001319 /**
1320 * If this notification is being shown as a badge, always show as a number.
1321 */
1322 public static final int BADGE_ICON_NONE = 0;
1323
1324 /**
1325 * If this notification is being shown as a badge, use the {@link #getSmallIcon()} to
1326 * represent this notification.
1327 */
1328 public static final int BADGE_ICON_SMALL = 1;
1329
1330 /**
1331 * If this notification is being shown as a badge, use the {@link #getLargeIcon()} to
1332 * represent this notification.
1333 */
1334 public static final int BADGE_ICON_LARGE = 2;
Julia Reynolds7d5b4da2017-03-20 10:18:49 -04001335 private int mBadgeIcon = BADGE_ICON_NONE;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001336
Chris Wren51c75102013-07-16 20:49:17 -04001337 /**
Gustav Sennton761884c2018-11-19 17:40:19 +00001338 * Determines whether the platform can generate contextual actions for a notification.
1339 */
1340 private boolean mAllowSystemGeneratedContextualActions = true;
1341
1342 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001343 * Structure to encapsulate a named action that can be shown as part of this notification.
1344 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1345 * selected by the user.
1346 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001347 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1348 * or {@link Notification.Builder#addAction(Notification.Action)}
1349 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001350 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001351 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001352 /**
1353 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1354 * {@link RemoteInput}s.
1355 *
1356 * This is intended for {@link RemoteInput}s that only accept data, meaning
1357 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
1358 * is null or empty, and {@link RemoteInput#getAllowedDataTypes is non-null and not
1359 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1360 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1361 *
1362 * You can test if a RemoteInput matches these constraints using
1363 * {@link RemoteInput#isDataOnly}.
1364 */
1365 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1366
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001367 /**
1368 * {@link }: No semantic action defined.
1369 */
1370 public static final int SEMANTIC_ACTION_NONE = 0;
1371
1372 /**
1373 * {@code SemanticAction}: Reply to a conversation, chat, group, or wherever replies
1374 * may be appropriate.
1375 */
1376 public static final int SEMANTIC_ACTION_REPLY = 1;
1377
1378 /**
1379 * {@code SemanticAction}: Mark content as read.
1380 */
1381 public static final int SEMANTIC_ACTION_MARK_AS_READ = 2;
1382
1383 /**
1384 * {@code SemanticAction}: Mark content as unread.
1385 */
1386 public static final int SEMANTIC_ACTION_MARK_AS_UNREAD = 3;
1387
1388 /**
1389 * {@code SemanticAction}: Delete the content associated with the notification. This
1390 * could mean deleting an email, message, etc.
1391 */
1392 public static final int SEMANTIC_ACTION_DELETE = 4;
1393
1394 /**
1395 * {@code SemanticAction}: Archive the content associated with the notification. This
1396 * could mean archiving an email, message, etc.
1397 */
1398 public static final int SEMANTIC_ACTION_ARCHIVE = 5;
1399
1400 /**
1401 * {@code SemanticAction}: Mute the content associated with the notification. This could
1402 * mean silencing a conversation or currently playing media.
1403 */
1404 public static final int SEMANTIC_ACTION_MUTE = 6;
1405
1406 /**
1407 * {@code SemanticAction}: Unmute the content associated with the notification. This could
1408 * mean un-silencing a conversation or currently playing media.
1409 */
1410 public static final int SEMANTIC_ACTION_UNMUTE = 7;
1411
1412 /**
1413 * {@code SemanticAction}: Mark content with a thumbs up.
1414 */
1415 public static final int SEMANTIC_ACTION_THUMBS_UP = 8;
1416
1417 /**
1418 * {@code SemanticAction}: Mark content with a thumbs down.
1419 */
1420 public static final int SEMANTIC_ACTION_THUMBS_DOWN = 9;
1421
Kodlee Yincda5b092018-02-15 15:34:53 -08001422 /**
1423 * {@code SemanticAction}: Call a contact, group, etc.
1424 */
1425 public static final int SEMANTIC_ACTION_CALL = 10;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001426
Griff Hazen959591e2014-05-15 22:26:18 -07001427 private final Bundle mExtras;
Mathew Inwood8c854f82018-09-14 12:35:36 +01001428 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Dan Sandler86647982015-05-13 23:41:13 -04001429 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001430 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001431 private boolean mAllowGeneratedReplies = true;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001432 private final @SemanticAction int mSemanticAction;
Gustav Sennton005d7a02019-01-04 13:41:32 +00001433 private final boolean mIsContextual;
Griff Hazen959591e2014-05-15 22:26:18 -07001434
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001435 /**
1436 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001437 *
1438 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001439 */
Dan Sandler86647982015-05-13 23:41:13 -04001440 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001441 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001442
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001443 /**
1444 * Title of the action.
1445 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001446 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001447
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001448 /**
1449 * Intent to send when the user invokes this action. May be null, in which case the action
1450 * may be rendered in a disabled presentation by the system UI.
1451 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001452 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001453
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001454 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001455 if (in.readInt() != 0) {
1456 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001457 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1458 icon = mIcon.getResId();
1459 }
Dan Sandler86647982015-05-13 23:41:13 -04001460 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001461 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1462 if (in.readInt() == 1) {
1463 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1464 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001465 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001466 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001467 mAllowGeneratedReplies = in.readInt() == 1;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001468 mSemanticAction = in.readInt();
Gustav Sennton005d7a02019-01-04 13:41:32 +00001469 mIsContextual = in.readInt() == 1;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001470 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001471
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001472 /**
Dan Sandler86647982015-05-13 23:41:13 -04001473 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001474 */
Dan Sandler86647982015-05-13 23:41:13 -04001475 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001476 public Action(int icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001477 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true,
Gustav Sennton005d7a02019-01-04 13:41:32 +00001478 SEMANTIC_ACTION_NONE, false /* isContextual */);
Griff Hazen959591e2014-05-15 22:26:18 -07001479 }
1480
Adrian Roos7af53622016-10-12 13:44:05 -07001481 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001482 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001483 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
Gustav Sennton005d7a02019-01-04 13:41:32 +00001484 @SemanticAction int semanticAction, boolean isContextual) {
Dan Sandler86647982015-05-13 23:41:13 -04001485 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001486 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1487 this.icon = icon.getResId();
1488 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001489 this.title = title;
1490 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001491 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001492 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001493 this.mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001494 this.mSemanticAction = semanticAction;
Gustav Sennton005d7a02019-01-04 13:41:32 +00001495 this.mIsContextual = isContextual;
Griff Hazen959591e2014-05-15 22:26:18 -07001496 }
1497
1498 /**
Dan Sandler86647982015-05-13 23:41:13 -04001499 * Return an icon representing the action.
1500 */
1501 public Icon getIcon() {
1502 if (mIcon == null && icon != 0) {
1503 // you snuck an icon in here without using the builder; let's try to keep it
1504 mIcon = Icon.createWithResource("", icon);
1505 }
1506 return mIcon;
1507 }
1508
1509 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001510 * Get additional metadata carried around with this Action.
1511 */
1512 public Bundle getExtras() {
1513 return mExtras;
1514 }
1515
1516 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001517 * Return whether the platform should automatically generate possible replies for this
1518 * {@link Action}
1519 */
1520 public boolean getAllowGeneratedReplies() {
1521 return mAllowGeneratedReplies;
1522 }
1523
1524 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001525 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001526 * May return null if no remote inputs were added. Only returns inputs which accept
1527 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001528 */
1529 public RemoteInput[] getRemoteInputs() {
1530 return mRemoteInputs;
1531 }
1532
1533 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001534 * Returns the {@code SemanticAction} associated with this {@link Action}. A
1535 * {@code SemanticAction} denotes what an {@link Action}'s {@link PendingIntent} will do
1536 * (eg. reply, mark as read, delete, etc).
1537 */
1538 public @SemanticAction int getSemanticAction() {
1539 return mSemanticAction;
1540 }
1541
1542 /**
Gustav Sennton005d7a02019-01-04 13:41:32 +00001543 * Returns whether this is a contextual Action, i.e. whether the action is dependent on the
1544 * notification message body. An example of a contextual action could be an action opening a
1545 * map application with an address shown in the notification.
1546 */
1547 public boolean isContextual() {
1548 return mIsContextual;
1549 }
1550
1551 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001552 * Get the list of inputs to be collected from the user that ONLY accept data when this
1553 * action is sent. These remote inputs are guaranteed to return true on a call to
1554 * {@link RemoteInput#isDataOnly}.
1555 *
Shane Brennanf670d6c2017-04-25 13:05:45 -07001556 * Returns null if there are no data-only remote inputs.
Shane Brennan472a3b32016-12-12 15:28:10 -08001557 *
1558 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1559 * of non-textual RemoteInputs do not access these remote inputs.
1560 */
1561 public RemoteInput[] getDataOnlyRemoteInputs() {
1562 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1563 }
1564
1565 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001566 * Builder class for {@link Action} objects.
1567 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001568 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001569 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001570 private final CharSequence mTitle;
1571 private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001572 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001573 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001574 private ArrayList<RemoteInput> mRemoteInputs;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001575 private @SemanticAction int mSemanticAction;
Gustav Sennton005d7a02019-01-04 13:41:32 +00001576 private boolean mIsContextual;
Griff Hazen959591e2014-05-15 22:26:18 -07001577
1578 /**
1579 * Construct a new builder for {@link Action} object.
1580 * @param icon icon to show for this action
1581 * @param title the title of the action
1582 * @param intent the {@link PendingIntent} to fire when users trigger this action
1583 */
Dan Sandler86647982015-05-13 23:41:13 -04001584 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001585 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001586 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001587 }
1588
1589 /**
1590 * Construct a new builder for {@link Action} object.
1591 * @param icon icon to show for this action
1592 * @param title the title of the action
1593 * @param intent the {@link PendingIntent} to fire when users trigger this action
1594 */
1595 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001596 this(icon, title, intent, new Bundle(), null, true, SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001597 }
1598
1599 /**
1600 * Construct a new builder for {@link Action} object using the fields from an
1601 * {@link Action}.
1602 * @param action the action to read fields from.
1603 */
1604 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001605 this(action.getIcon(), action.title, action.actionIntent,
1606 new Bundle(action.mExtras), action.getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001607 action.getAllowGeneratedReplies(), action.getSemanticAction());
Griff Hazen959591e2014-05-15 22:26:18 -07001608 }
1609
Dan Sandler86647982015-05-13 23:41:13 -04001610 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001611 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1612 @SemanticAction int semanticAction) {
Griff Hazen959591e2014-05-15 22:26:18 -07001613 mIcon = icon;
1614 mTitle = title;
1615 mIntent = intent;
1616 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001617 if (remoteInputs != null) {
1618 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1619 Collections.addAll(mRemoteInputs, remoteInputs);
1620 }
Adrian Roos7af53622016-10-12 13:44:05 -07001621 mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001622 mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001623 }
1624
1625 /**
1626 * Merge additional metadata into this builder.
1627 *
1628 * <p>Values within the Bundle will replace existing extras values in this Builder.
1629 *
1630 * @see Notification.Action#extras
1631 */
1632 public Builder addExtras(Bundle extras) {
1633 if (extras != null) {
1634 mExtras.putAll(extras);
1635 }
1636 return this;
1637 }
1638
1639 /**
1640 * Get the metadata Bundle used by this Builder.
1641 *
1642 * <p>The returned Bundle is shared with this Builder.
1643 */
1644 public Bundle getExtras() {
1645 return mExtras;
1646 }
1647
1648 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001649 * Add an input to be collected from the user when this action is sent.
1650 * Response values can be retrieved from the fired intent by using the
1651 * {@link RemoteInput#getResultsFromIntent} function.
1652 * @param remoteInput a {@link RemoteInput} to add to the action
1653 * @return this object for method chaining
1654 */
1655 public Builder addRemoteInput(RemoteInput remoteInput) {
1656 if (mRemoteInputs == null) {
1657 mRemoteInputs = new ArrayList<RemoteInput>();
1658 }
1659 mRemoteInputs.add(remoteInput);
1660 return this;
1661 }
1662
1663 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001664 * Set whether the platform should automatically generate possible replies to add to
1665 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1666 * {@link RemoteInput}, this has no effect.
1667 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1668 * otherwise
1669 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001670 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001671 */
1672 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1673 mAllowGeneratedReplies = allowGeneratedReplies;
1674 return this;
1675 }
1676
1677 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001678 * Sets the {@code SemanticAction} for this {@link Action}. A
1679 * {@code SemanticAction} denotes what an {@link Action}'s
1680 * {@link PendingIntent} will do (eg. reply, mark as read, delete, etc).
1681 * @param semanticAction a SemanticAction defined within {@link Action} with
1682 * {@code SEMANTIC_ACTION_} prefixes
1683 * @return this object for method chaining
1684 */
1685 public Builder setSemanticAction(@SemanticAction int semanticAction) {
1686 mSemanticAction = semanticAction;
1687 return this;
1688 }
1689
1690 /**
Gustav Sennton005d7a02019-01-04 13:41:32 +00001691 * Sets whether this {@link Action} is a contextual action, i.e. whether the action is
1692 * dependent on the notification message body. An example of a contextual action could
1693 * be an action opening a map application with an address shown in the notification.
1694 */
1695 public Builder setContextual(boolean isContextual) {
1696 mIsContextual = isContextual;
1697 return this;
1698 }
1699
1700 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001701 * Apply an extender to this action builder. Extenders may be used to add
1702 * metadata or change options on this builder.
1703 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001704 public Builder extend(Extender extender) {
1705 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001706 return this;
1707 }
1708
1709 /**
Gustav Senntonc98b1632018-11-23 12:26:15 +00001710 * Throws an NPE if we are building a contextual action missing one of the fields
1711 * necessary to display the action.
1712 */
1713 private void checkContextualActionNullFields() {
Gustav Sennton005d7a02019-01-04 13:41:32 +00001714 if (!mIsContextual) return;
Gustav Senntonc98b1632018-11-23 12:26:15 +00001715
1716 if (mIcon == null) {
1717 throw new NullPointerException("Contextual Actions must contain a valid icon");
1718 }
1719
1720 if (mIntent == null) {
1721 throw new NullPointerException(
1722 "Contextual Actions must contain a valid PendingIntent");
1723 }
1724 }
1725
1726 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001727 * Combine all of the options that have been set and return a new {@link Action}
1728 * object.
1729 * @return the built action
1730 */
1731 public Action build() {
Gustav Senntonc98b1632018-11-23 12:26:15 +00001732 checkContextualActionNullFields();
1733
Shane Brennan472a3b32016-12-12 15:28:10 -08001734 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1735 RemoteInput[] previousDataInputs =
1736 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001737 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001738 for (RemoteInput input : previousDataInputs) {
1739 dataOnlyInputs.add(input);
1740 }
1741 }
1742 List<RemoteInput> textInputs = new ArrayList<>();
1743 if (mRemoteInputs != null) {
1744 for (RemoteInput input : mRemoteInputs) {
1745 if (input.isDataOnly()) {
1746 dataOnlyInputs.add(input);
1747 } else {
1748 textInputs.add(input);
1749 }
1750 }
1751 }
1752 if (!dataOnlyInputs.isEmpty()) {
1753 RemoteInput[] dataInputsArr =
1754 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1755 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1756 }
1757 RemoteInput[] textInputsArr = textInputs.isEmpty()
1758 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1759 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Gustav Sennton005d7a02019-01-04 13:41:32 +00001760 mAllowGeneratedReplies, mSemanticAction, mIsContextual);
Griff Hazen959591e2014-05-15 22:26:18 -07001761 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001762 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001763
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001764 @Override
1765 public Action clone() {
1766 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001767 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001768 title,
1769 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001770 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001771 getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001772 getAllowGeneratedReplies(),
Gustav Sennton005d7a02019-01-04 13:41:32 +00001773 getSemanticAction(),
1774 isContextual());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001775 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001776
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001777 @Override
1778 public int describeContents() {
1779 return 0;
1780 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001781
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001782 @Override
1783 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001784 final Icon ic = getIcon();
1785 if (ic != null) {
1786 out.writeInt(1);
1787 ic.writeToParcel(out, 0);
1788 } else {
1789 out.writeInt(0);
1790 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001791 TextUtils.writeToParcel(title, out, flags);
1792 if (actionIntent != null) {
1793 out.writeInt(1);
1794 actionIntent.writeToParcel(out, flags);
1795 } else {
1796 out.writeInt(0);
1797 }
Griff Hazen959591e2014-05-15 22:26:18 -07001798 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001799 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001800 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001801 out.writeInt(mSemanticAction);
Gustav Sennton005d7a02019-01-04 13:41:32 +00001802 out.writeInt(mIsContextual ? 1 : 0);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001803 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001804
Griff Hazen959591e2014-05-15 22:26:18 -07001805 public static final Parcelable.Creator<Action> CREATOR =
1806 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001807 public Action createFromParcel(Parcel in) {
1808 return new Action(in);
1809 }
1810 public Action[] newArray(int size) {
1811 return new Action[size];
1812 }
1813 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001814
1815 /**
1816 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1817 * metadata or change options on an action builder.
1818 */
1819 public interface Extender {
1820 /**
1821 * Apply this extender to a notification action builder.
1822 * @param builder the builder to be modified.
1823 * @return the build object for chaining.
1824 */
1825 public Builder extend(Builder builder);
1826 }
1827
1828 /**
1829 * Wearable extender for notification actions. To add extensions to an action,
1830 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1831 * the {@code WearableExtender()} constructor and apply it to a
1832 * {@link android.app.Notification.Action.Builder} using
1833 * {@link android.app.Notification.Action.Builder#extend}.
1834 *
1835 * <pre class="prettyprint">
1836 * Notification.Action action = new Notification.Action.Builder(
1837 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001838 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001839 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001840 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001841 */
1842 public static final class WearableExtender implements Extender {
1843 /** Notification action extra which contains wearable extensions */
1844 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1845
Pete Gastaf6781d2014-10-07 15:17:05 -04001846 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001847 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001848 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1849 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1850 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001851
1852 // Flags bitwise-ored to mFlags
1853 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001854 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001855 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001856
1857 // Default value for flags integer
1858 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1859
1860 private int mFlags = DEFAULT_FLAGS;
1861
Pete Gastaf6781d2014-10-07 15:17:05 -04001862 private CharSequence mInProgressLabel;
1863 private CharSequence mConfirmLabel;
1864 private CharSequence mCancelLabel;
1865
Griff Hazen61a9e862014-05-22 16:05:19 -07001866 /**
1867 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1868 * options.
1869 */
1870 public WearableExtender() {
1871 }
1872
1873 /**
1874 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1875 * wearable options present in an existing notification action.
1876 * @param action the notification action to inspect.
1877 */
1878 public WearableExtender(Action action) {
1879 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1880 if (wearableBundle != null) {
1881 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001882 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1883 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1884 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001885 }
1886 }
1887
1888 /**
1889 * Apply wearable extensions to a notification action that is being built. This is
1890 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1891 * method of {@link android.app.Notification.Action.Builder}.
1892 */
1893 @Override
1894 public Action.Builder extend(Action.Builder builder) {
1895 Bundle wearableBundle = new Bundle();
1896
1897 if (mFlags != DEFAULT_FLAGS) {
1898 wearableBundle.putInt(KEY_FLAGS, mFlags);
1899 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001900 if (mInProgressLabel != null) {
1901 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1902 }
1903 if (mConfirmLabel != null) {
1904 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1905 }
1906 if (mCancelLabel != null) {
1907 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1908 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001909
1910 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1911 return builder;
1912 }
1913
1914 @Override
1915 public WearableExtender clone() {
1916 WearableExtender that = new WearableExtender();
1917 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001918 that.mInProgressLabel = this.mInProgressLabel;
1919 that.mConfirmLabel = this.mConfirmLabel;
1920 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001921 return that;
1922 }
1923
1924 /**
1925 * Set whether this action is available when the wearable device is not connected to
1926 * a companion device. The user can still trigger this action when the wearable device is
1927 * offline, but a visual hint will indicate that the action may not be available.
1928 * Defaults to true.
1929 */
1930 public WearableExtender setAvailableOffline(boolean availableOffline) {
1931 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1932 return this;
1933 }
1934
1935 /**
1936 * Get whether this action is available when the wearable device is not connected to
1937 * a companion device. The user can still trigger this action when the wearable device is
1938 * offline, but a visual hint will indicate that the action may not be available.
1939 * Defaults to true.
1940 */
1941 public boolean isAvailableOffline() {
1942 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1943 }
1944
1945 private void setFlag(int mask, boolean value) {
1946 if (value) {
1947 mFlags |= mask;
1948 } else {
1949 mFlags &= ~mask;
1950 }
1951 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001952
1953 /**
1954 * Set a label to display while the wearable is preparing to automatically execute the
1955 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1956 *
1957 * @param label the label to display while the action is being prepared to execute
1958 * @return this object for method chaining
1959 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001960 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001961 public WearableExtender setInProgressLabel(CharSequence label) {
1962 mInProgressLabel = label;
1963 return this;
1964 }
1965
1966 /**
1967 * Get the label to display while the wearable is preparing to automatically execute
1968 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1969 *
1970 * @return the label to display while the action is being prepared to execute
1971 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001972 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001973 public CharSequence getInProgressLabel() {
1974 return mInProgressLabel;
1975 }
1976
1977 /**
1978 * Set a label to display to confirm that the action should be executed.
1979 * This is usually an imperative verb like "Send".
1980 *
1981 * @param label the label to confirm the action should be executed
1982 * @return this object for method chaining
1983 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001984 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001985 public WearableExtender setConfirmLabel(CharSequence label) {
1986 mConfirmLabel = label;
1987 return this;
1988 }
1989
1990 /**
1991 * Get the label to display to confirm that the action should be executed.
1992 * This is usually an imperative verb like "Send".
1993 *
1994 * @return the label to confirm the action should be executed
1995 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001996 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001997 public CharSequence getConfirmLabel() {
1998 return mConfirmLabel;
1999 }
2000
2001 /**
2002 * Set a label to display to cancel the action.
2003 * This is usually an imperative verb, like "Cancel".
2004 *
2005 * @param label the label to display to cancel the action
2006 * @return this object for method chaining
2007 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002008 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002009 public WearableExtender setCancelLabel(CharSequence label) {
2010 mCancelLabel = label;
2011 return this;
2012 }
2013
2014 /**
2015 * Get the label to display to cancel the action.
2016 * This is usually an imperative verb like "Cancel".
2017 *
2018 * @return the label to display to cancel the action
2019 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002020 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002021 public CharSequence getCancelLabel() {
2022 return mCancelLabel;
2023 }
Alex Hills9ab3a232016-04-05 14:54:56 -04002024
2025 /**
2026 * Set a hint that this Action will launch an {@link Activity} directly, telling the
2027 * platform that it can generate the appropriate transitions.
2028 * @param hintLaunchesActivity {@code true} if the content intent will launch
2029 * an activity and transitions should be generated, false otherwise.
2030 * @return this object for method chaining
2031 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04002032 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04002033 boolean hintLaunchesActivity) {
2034 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
2035 return this;
2036 }
2037
2038 /**
2039 * Get a hint that this Action will launch an {@link Activity} directly, telling the
2040 * platform that it can generate the appropriate transitions
2041 * @return {@code true} if the content intent will launch an activity and transitions
2042 * should be generated, false otherwise. The default value is {@code false} if this was
2043 * never set.
2044 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04002045 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04002046 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
2047 }
Alex Hills9f087612016-06-07 09:08:59 -04002048
2049 /**
2050 * Set a hint that this Action should be displayed inline.
2051 *
2052 * @param hintDisplayInline {@code true} if action should be displayed inline, false
2053 * otherwise
2054 * @return this object for method chaining
2055 */
2056 public WearableExtender setHintDisplayActionInline(
2057 boolean hintDisplayInline) {
2058 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
2059 return this;
2060 }
2061
2062 /**
2063 * Get a hint that this Action should be displayed inline.
2064 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08002065 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04002066 * otherwise. The default value is {@code false} if this was never set.
2067 */
2068 public boolean getHintDisplayActionInline() {
2069 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
2070 }
Griff Hazen61a9e862014-05-22 16:05:19 -07002071 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002072
2073 /**
2074 * Provides meaning to an {@link Action} that hints at what the associated
2075 * {@link PendingIntent} will do. For example, an {@link Action} with a
2076 * {@link PendingIntent} that replies to a text message notification may have the
2077 * {@link #SEMANTIC_ACTION_REPLY} {@code SemanticAction} set within it.
2078 *
2079 * @hide
2080 */
2081 @IntDef(prefix = { "SEMANTIC_ACTION_" }, value = {
2082 SEMANTIC_ACTION_NONE,
2083 SEMANTIC_ACTION_REPLY,
2084 SEMANTIC_ACTION_MARK_AS_READ,
2085 SEMANTIC_ACTION_MARK_AS_UNREAD,
2086 SEMANTIC_ACTION_DELETE,
2087 SEMANTIC_ACTION_ARCHIVE,
2088 SEMANTIC_ACTION_MUTE,
2089 SEMANTIC_ACTION_UNMUTE,
2090 SEMANTIC_ACTION_THUMBS_UP,
Kodlee Yincda5b092018-02-15 15:34:53 -08002091 SEMANTIC_ACTION_THUMBS_DOWN,
Gustav Sennton005d7a02019-01-04 13:41:32 +00002092 SEMANTIC_ACTION_CALL
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002093 })
2094 @Retention(RetentionPolicy.SOURCE)
2095 public @interface SemanticAction {}
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002096 }
2097
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002098 /**
2099 * Array of all {@link Action} structures attached to this notification by
2100 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
2101 * {@link android.service.notification.NotificationListenerService} that provide an alternative
2102 * interface for invoking actions.
2103 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05002104 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002105
2106 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002107 * Replacement version of this notification whose content will be shown
2108 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
2109 * and {@link #VISIBILITY_PUBLIC}.
2110 */
2111 public Notification publicVersion;
2112
2113 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002114 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08002115 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002116 */
2117 public Notification()
2118 {
2119 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002120 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002121 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002122 }
2123
2124 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002125 * @hide
2126 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002127 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128 public Notification(Context context, int icon, CharSequence tickerText, long when,
2129 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
2130 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04002131 new Builder(context)
2132 .setWhen(when)
2133 .setSmallIcon(icon)
2134 .setTicker(tickerText)
2135 .setContentTitle(contentTitle)
2136 .setContentText(contentText)
2137 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
2138 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002139 }
2140
2141 /**
2142 * Constructs a Notification object with the information needed to
2143 * have a status bar icon without the standard expanded view.
2144 *
2145 * @param icon The resource id of the icon to put in the status bar.
2146 * @param tickerText The text that flows by in the status bar when the notification first
2147 * activates.
2148 * @param when The time to show in the time field. In the System.currentTimeMillis
2149 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08002150 *
2151 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002152 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002153 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002154 public Notification(int icon, CharSequence tickerText, long when)
2155 {
2156 this.icon = icon;
2157 this.tickerText = tickerText;
2158 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002159 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002160 }
2161
2162 /**
2163 * Unflatten the notification from a parcel.
2164 */
Svet Ganovddb94882016-06-23 19:55:24 -07002165 @SuppressWarnings("unchecked")
2166 public Notification(Parcel parcel) {
2167 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
2168 // intents in extras are always written as the last entry.
2169 readFromParcelImpl(parcel);
2170 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002171 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07002172 }
2173
2174 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002175 {
2176 int version = parcel.readInt();
2177
Adrian Roosfb921842017-10-26 14:49:56 +02002178 mWhitelistToken = parcel.readStrongBinder();
2179 if (mWhitelistToken == null) {
2180 mWhitelistToken = processWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -07002181 }
2182 // Propagate this token to all pending intents that are unmarshalled from the parcel.
Adrian Roosfb921842017-10-26 14:49:56 +02002183 parcel.setClassCookie(PendingIntent.class, mWhitelistToken);
Dianne Hackborn98305522017-05-05 17:53:53 -07002184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002185 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002186 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04002187 if (parcel.readInt() != 0) {
2188 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04002189 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
2190 icon = mSmallIcon.getResId();
2191 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002192 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002193 number = parcel.readInt();
2194 if (parcel.readInt() != 0) {
2195 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2196 }
2197 if (parcel.readInt() != 0) {
2198 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2199 }
2200 if (parcel.readInt() != 0) {
2201 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2202 }
2203 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002204 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002205 }
2206 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002207 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
2208 }
Joe Onorato561d3852010-11-20 18:09:34 -08002209 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002210 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08002211 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002212 defaults = parcel.readInt();
2213 flags = parcel.readInt();
2214 if (parcel.readInt() != 0) {
2215 sound = Uri.CREATOR.createFromParcel(parcel);
2216 }
2217
2218 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04002219 if (parcel.readInt() != 0) {
2220 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
2221 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002222 vibrate = parcel.createLongArray();
2223 ledARGB = parcel.readInt();
2224 ledOnMS = parcel.readInt();
2225 ledOffMS = parcel.readInt();
2226 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002227
2228 if (parcel.readInt() != 0) {
2229 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2230 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002231
2232 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002233
John Spurlockfd7f1e02014-03-18 16:41:57 -04002234 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002235
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002236 mGroupKey = parcel.readString();
2237
2238 mSortKey = parcel.readString();
2239
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06002240 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Robin Leead7e72a2017-12-04 15:45:46 +01002241 fixDuplicateExtras();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002242
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002243 actions = parcel.createTypedArray(Action.CREATOR); // may be null
2244
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002245 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002246 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2247 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002248
Chris Wren8fd39ec2014-02-27 17:43:26 -05002249 if (parcel.readInt() != 0) {
2250 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2251 }
2252
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002253 visibility = parcel.readInt();
2254
2255 if (parcel.readInt() != 0) {
2256 publicVersion = Notification.CREATOR.createFromParcel(parcel);
2257 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002258
2259 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002260
2261 if (parcel.readInt() != 0) {
2262 mChannelId = parcel.readString();
2263 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002264 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05002265
2266 if (parcel.readInt() != 0) {
2267 mShortcutId = parcel.readString();
2268 }
2269
2270 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04002271
2272 if (parcel.readInt() != 0) {
2273 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2274 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002275
2276 mGroupAlertBehavior = parcel.readInt();
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002277 if (parcel.readInt() != 0) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002278 mBubbleMetadata = BubbleMetadata.CREATOR.createFromParcel(parcel);
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002279 }
Gustav Sennton761884c2018-11-19 17:40:19 +00002280
2281 mAllowSystemGeneratedContextualActions = parcel.readBoolean();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002282 }
2283
Andy Stadler110988c2010-12-03 14:29:16 -08002284 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07002285 public Notification clone() {
2286 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04002287 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002288 return that;
2289 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002290
Daniel Sandler1a497d32013-04-18 14:52:45 -04002291 /**
2292 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
2293 * of this into that.
2294 * @hide
2295 */
2296 public void cloneInto(Notification that, boolean heavy) {
Adrian Roosfb921842017-10-26 14:49:56 +02002297 that.mWhitelistToken = this.mWhitelistToken;
Joe Onorato18e69df2010-05-17 22:26:12 -07002298 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002299 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002300 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07002301 that.number = this.number;
2302
2303 // PendingIntents are global, so there's no reason (or way) to clone them.
2304 that.contentIntent = this.contentIntent;
2305 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002306 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07002307
2308 if (this.tickerText != null) {
2309 that.tickerText = this.tickerText.toString();
2310 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002311 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002312 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04002313 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002314 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07002315 that.contentView = this.contentView.clone();
2316 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002317 if (heavy && this.mLargeIcon != null) {
2318 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08002319 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01002320 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07002321 that.sound = this.sound; // android.net.Uri is immutable
2322 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002323 if (this.audioAttributes != null) {
2324 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
2325 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002326
2327 final long[] vibrate = this.vibrate;
2328 if (vibrate != null) {
2329 final int N = vibrate.length;
2330 final long[] vib = that.vibrate = new long[N];
2331 System.arraycopy(vibrate, 0, vib, 0, N);
2332 }
2333
2334 that.ledARGB = this.ledARGB;
2335 that.ledOnMS = this.ledOnMS;
2336 that.ledOffMS = this.ledOffMS;
2337 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002338
Joe Onorato18e69df2010-05-17 22:26:12 -07002339 that.flags = this.flags;
2340
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002341 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08002342
John Spurlockfd7f1e02014-03-18 16:41:57 -04002343 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002344
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002345 that.mGroupKey = this.mGroupKey;
2346
2347 that.mSortKey = this.mSortKey;
2348
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002349 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002350 try {
2351 that.extras = new Bundle(this.extras);
2352 // will unparcel
2353 that.extras.size();
2354 } catch (BadParcelableException e) {
2355 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
2356 that.extras = null;
2357 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002358 }
2359
Felipe Lemedd85da62016-06-28 11:29:54 -07002360 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2361 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002362 }
2363
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002364 if (this.actions != null) {
2365 that.actions = new Action[this.actions.length];
2366 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002367 if ( this.actions[i] != null) {
2368 that.actions[i] = this.actions[i].clone();
2369 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002370 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002371 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002372
Daniel Sandler1a497d32013-04-18 14:52:45 -04002373 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002374 that.bigContentView = this.bigContentView.clone();
2375 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002376
Chris Wren8fd39ec2014-02-27 17:43:26 -05002377 if (heavy && this.headsUpContentView != null) {
2378 that.headsUpContentView = this.headsUpContentView.clone();
2379 }
2380
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002381 that.visibility = this.visibility;
2382
2383 if (this.publicVersion != null) {
2384 that.publicVersion = new Notification();
2385 this.publicVersion.cloneInto(that.publicVersion, heavy);
2386 }
2387
Dan Sandler26e81cf2014-05-06 10:01:27 -04002388 that.color = this.color;
2389
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002390 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002391 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002392 that.mShortcutId = this.mShortcutId;
2393 that.mBadgeIcon = this.mBadgeIcon;
2394 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002395 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002396 that.mBubbleMetadata = this.mBubbleMetadata;
Gustav Sennton761884c2018-11-19 17:40:19 +00002397 that.mAllowSystemGeneratedContextualActions = this.mAllowSystemGeneratedContextualActions;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002398
Daniel Sandler1a497d32013-04-18 14:52:45 -04002399 if (!heavy) {
2400 that.lightenPayload(); // will clean out extras
2401 }
2402 }
2403
2404 /**
Jeff Sharkey23b31182018-04-18 21:32:12 -06002405 * Note all {@link Uri} that are referenced internally, with the expectation
2406 * that Uri permission grants will need to be issued to ensure the recipient
2407 * of this object is able to render its contents.
2408 *
2409 * @hide
2410 */
2411 public void visitUris(@NonNull Consumer<Uri> visitor) {
2412 visitor.accept(sound);
2413
2414 if (tickerView != null) tickerView.visitUris(visitor);
2415 if (contentView != null) contentView.visitUris(visitor);
2416 if (bigContentView != null) bigContentView.visitUris(visitor);
2417 if (headsUpContentView != null) headsUpContentView.visitUris(visitor);
2418
2419 if (extras != null) {
2420 visitor.accept(extras.getParcelable(EXTRA_AUDIO_CONTENTS_URI));
Robin Leed107af62018-04-27 13:55:56 +02002421 if (extras.containsKey(EXTRA_BACKGROUND_IMAGE_URI)) {
2422 visitor.accept(Uri.parse(extras.getString(EXTRA_BACKGROUND_IMAGE_URI)));
2423 }
Jeff Sharkey23b31182018-04-18 21:32:12 -06002424 }
2425
2426 if (MessagingStyle.class.equals(getNotificationStyle()) && extras != null) {
2427 final Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
2428 if (!ArrayUtils.isEmpty(messages)) {
2429 for (MessagingStyle.Message message : MessagingStyle.Message
2430 .getMessagesFromBundleArray(messages)) {
2431 visitor.accept(message.getDataUri());
2432 }
2433 }
2434
2435 final Parcelable[] historic = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
2436 if (!ArrayUtils.isEmpty(historic)) {
2437 for (MessagingStyle.Message message : MessagingStyle.Message
2438 .getMessagesFromBundleArray(historic)) {
2439 visitor.accept(message.getDataUri());
2440 }
2441 }
2442 }
2443 }
2444
2445 /**
Daniel Sandler1a497d32013-04-18 14:52:45 -04002446 * Removes heavyweight parts of the Notification object for archival or for sending to
2447 * listeners when the full contents are not necessary.
2448 * @hide
2449 */
2450 public final void lightenPayload() {
2451 tickerView = null;
2452 contentView = null;
2453 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002454 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002455 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002456 if (extras != null && !extras.isEmpty()) {
2457 final Set<String> keyset = extras.keySet();
2458 final int N = keyset.size();
2459 final String[] keys = keyset.toArray(new String[N]);
2460 for (int i=0; i<N; i++) {
2461 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002462 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2463 continue;
2464 }
Dan Sandler50128532015-12-08 15:42:41 -05002465 final Object obj = extras.get(key);
2466 if (obj != null &&
2467 ( obj instanceof Parcelable
2468 || obj instanceof Parcelable[]
2469 || obj instanceof SparseArray
2470 || obj instanceof ArrayList)) {
2471 extras.remove(key);
2472 }
2473 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002474 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002475 }
2476
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002477 /**
2478 * Make sure this CharSequence is safe to put into a bundle, which basically
2479 * means it had better not be some custom Parcelable implementation.
2480 * @hide
2481 */
2482 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002483 if (cs == null) return cs;
2484 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2485 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2486 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002487 if (cs instanceof Parcelable) {
2488 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2489 + " instance is a custom Parcelable and not allowed in Notification");
2490 return cs.toString();
2491 }
Selim Cinek60a54252016-02-26 17:03:25 -08002492 return removeTextSizeSpans(cs);
2493 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002494
Selim Cinek60a54252016-02-26 17:03:25 -08002495 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2496 if (charSequence instanceof Spanned) {
2497 Spanned ss = (Spanned) charSequence;
2498 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2499 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2500 for (Object span : spans) {
2501 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002502 if (resultSpan instanceof CharacterStyle) {
2503 resultSpan = ((CharacterStyle) span).getUnderlying();
2504 }
2505 if (resultSpan instanceof TextAppearanceSpan) {
2506 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002507 resultSpan = new TextAppearanceSpan(
2508 originalSpan.getFamily(),
2509 originalSpan.getTextStyle(),
2510 -1,
2511 originalSpan.getTextColor(),
2512 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002513 } else if (resultSpan instanceof RelativeSizeSpan
2514 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002515 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002516 } else {
2517 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002518 }
2519 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2520 ss.getSpanFlags(span));
2521 }
2522 return builder;
2523 }
2524 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002525 }
2526
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002527 public int describeContents() {
2528 return 0;
2529 }
2530
2531 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002532 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002533 */
Svet Ganovddb94882016-06-23 19:55:24 -07002534 public void writeToParcel(Parcel parcel, int flags) {
2535 // We need to mark all pending intents getting into the notification
2536 // system as being put there to later allow the notification ranker
2537 // to launch them and by doing so add the app to the battery saver white
2538 // list for a short period of time. The problem is that the system
2539 // cannot look into the extras as there may be parcelables there that
2540 // the platform does not know how to handle. To go around that we have
2541 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002542 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002543 if (collectPendingIntents) {
2544 PendingIntent.setOnMarshaledListener(
2545 (PendingIntent intent, Parcel out, int outFlags) -> {
2546 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002547 if (allPendingIntents == null) {
2548 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002549 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002550 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002551 }
2552 });
2553 }
2554 try {
2555 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002556 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002557 writeToParcelImpl(parcel, flags);
2558 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002559 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002560 } finally {
2561 if (collectPendingIntents) {
2562 PendingIntent.setOnMarshaledListener(null);
2563 }
2564 }
2565 }
2566
2567 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002568 parcel.writeInt(1);
2569
Adrian Roosfb921842017-10-26 14:49:56 +02002570 parcel.writeStrongBinder(mWhitelistToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002571 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002572 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002573 if (mSmallIcon == null && icon != 0) {
2574 // you snuck an icon in here without using the builder; let's try to keep it
2575 mSmallIcon = Icon.createWithResource("", icon);
2576 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002577 if (mSmallIcon != null) {
2578 parcel.writeInt(1);
2579 mSmallIcon.writeToParcel(parcel, 0);
2580 } else {
2581 parcel.writeInt(0);
2582 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002583 parcel.writeInt(number);
2584 if (contentIntent != null) {
2585 parcel.writeInt(1);
2586 contentIntent.writeToParcel(parcel, 0);
2587 } else {
2588 parcel.writeInt(0);
2589 }
2590 if (deleteIntent != null) {
2591 parcel.writeInt(1);
2592 deleteIntent.writeToParcel(parcel, 0);
2593 } else {
2594 parcel.writeInt(0);
2595 }
2596 if (tickerText != null) {
2597 parcel.writeInt(1);
2598 TextUtils.writeToParcel(tickerText, parcel, flags);
2599 } else {
2600 parcel.writeInt(0);
2601 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002602 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002603 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002604 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002605 } else {
2606 parcel.writeInt(0);
2607 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002608 if (contentView != null) {
2609 parcel.writeInt(1);
2610 contentView.writeToParcel(parcel, 0);
2611 } else {
2612 parcel.writeInt(0);
2613 }
Selim Cinek279fa862016-06-14 10:57:25 -07002614 if (mLargeIcon == null && largeIcon != null) {
2615 // you snuck an icon in here without using the builder; let's try to keep it
2616 mLargeIcon = Icon.createWithBitmap(largeIcon);
2617 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002618 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002619 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002620 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002621 } else {
2622 parcel.writeInt(0);
2623 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002624
2625 parcel.writeInt(defaults);
2626 parcel.writeInt(this.flags);
2627
2628 if (sound != null) {
2629 parcel.writeInt(1);
2630 sound.writeToParcel(parcel, 0);
2631 } else {
2632 parcel.writeInt(0);
2633 }
2634 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002635
2636 if (audioAttributes != null) {
2637 parcel.writeInt(1);
2638 audioAttributes.writeToParcel(parcel, 0);
2639 } else {
2640 parcel.writeInt(0);
2641 }
2642
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002643 parcel.writeLongArray(vibrate);
2644 parcel.writeInt(ledARGB);
2645 parcel.writeInt(ledOnMS);
2646 parcel.writeInt(ledOffMS);
2647 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002648
2649 if (fullScreenIntent != null) {
2650 parcel.writeInt(1);
2651 fullScreenIntent.writeToParcel(parcel, 0);
2652 } else {
2653 parcel.writeInt(0);
2654 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002655
2656 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002657
John Spurlockfd7f1e02014-03-18 16:41:57 -04002658 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002659
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002660 parcel.writeString(mGroupKey);
2661
2662 parcel.writeString(mSortKey);
2663
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002664 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002665
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002666 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002667
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002668 if (bigContentView != null) {
2669 parcel.writeInt(1);
2670 bigContentView.writeToParcel(parcel, 0);
2671 } else {
2672 parcel.writeInt(0);
2673 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002674
Chris Wren8fd39ec2014-02-27 17:43:26 -05002675 if (headsUpContentView != null) {
2676 parcel.writeInt(1);
2677 headsUpContentView.writeToParcel(parcel, 0);
2678 } else {
2679 parcel.writeInt(0);
2680 }
2681
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002682 parcel.writeInt(visibility);
2683
2684 if (publicVersion != null) {
2685 parcel.writeInt(1);
2686 publicVersion.writeToParcel(parcel, 0);
2687 } else {
2688 parcel.writeInt(0);
2689 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002690
2691 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002692
2693 if (mChannelId != null) {
2694 parcel.writeInt(1);
2695 parcel.writeString(mChannelId);
2696 } else {
2697 parcel.writeInt(0);
2698 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002699 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002700
2701 if (mShortcutId != null) {
2702 parcel.writeInt(1);
2703 parcel.writeString(mShortcutId);
2704 } else {
2705 parcel.writeInt(0);
2706 }
2707
2708 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002709
2710 if (mSettingsText != null) {
2711 parcel.writeInt(1);
2712 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2713 } else {
2714 parcel.writeInt(0);
2715 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002716
2717 parcel.writeInt(mGroupAlertBehavior);
Julia Reynoldsfc640012018-02-21 12:25:27 -05002718
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002719 if (mBubbleMetadata != null) {
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002720 parcel.writeInt(1);
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002721 mBubbleMetadata.writeToParcel(parcel, 0);
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002722 } else {
2723 parcel.writeInt(0);
2724 }
2725
Gustav Sennton761884c2018-11-19 17:40:19 +00002726 parcel.writeBoolean(mAllowSystemGeneratedContextualActions);
2727
Julia Reynoldsfc640012018-02-21 12:25:27 -05002728 // mUsesStandardHeader is not written because it should be recomputed in listeners
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002729 }
2730
2731 /**
2732 * Parcelable.Creator that instantiates Notification objects
2733 */
2734 public static final Parcelable.Creator<Notification> CREATOR
2735 = new Parcelable.Creator<Notification>()
2736 {
2737 public Notification createFromParcel(Parcel parcel)
2738 {
2739 return new Notification(parcel);
2740 }
2741
2742 public Notification[] newArray(int size)
2743 {
2744 return new Notification[size];
2745 }
2746 };
2747
2748 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05002749 * @hide
2750 */
2751 public static boolean areActionsVisiblyDifferent(Notification first, Notification second) {
2752 Notification.Action[] firstAs = first.actions;
2753 Notification.Action[] secondAs = second.actions;
2754 if (firstAs == null && secondAs != null || firstAs != null && secondAs == null) {
2755 return true;
2756 }
2757 if (firstAs != null && secondAs != null) {
2758 if (firstAs.length != secondAs.length) {
2759 return true;
2760 }
2761 for (int i = 0; i < firstAs.length; i++) {
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -04002762 if (!Objects.equals(String.valueOf(firstAs[i].title),
2763 String.valueOf(secondAs[i].title))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05002764 return true;
2765 }
2766 RemoteInput[] firstRs = firstAs[i].getRemoteInputs();
2767 RemoteInput[] secondRs = secondAs[i].getRemoteInputs();
2768 if (firstRs == null) {
2769 firstRs = new RemoteInput[0];
2770 }
2771 if (secondRs == null) {
2772 secondRs = new RemoteInput[0];
2773 }
2774 if (firstRs.length != secondRs.length) {
2775 return true;
2776 }
2777 for (int j = 0; j < firstRs.length; j++) {
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -04002778 if (!Objects.equals(String.valueOf(firstRs[j].getLabel()),
2779 String.valueOf(secondRs[j].getLabel()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05002780 return true;
2781 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05002782 }
2783 }
2784 }
2785 return false;
2786 }
2787
2788 /**
2789 * @hide
2790 */
2791 public static boolean areStyledNotificationsVisiblyDifferent(Builder first, Builder second) {
2792 if (first.getStyle() == null) {
2793 return second.getStyle() != null;
2794 }
2795 if (second.getStyle() == null) {
2796 return true;
2797 }
2798 return first.getStyle().areNotificationsVisiblyDifferent(second.getStyle());
2799 }
2800
2801 /**
2802 * @hide
2803 */
2804 public static boolean areRemoteViewsChanged(Builder first, Builder second) {
Julia Reynoldse5c60452018-04-30 14:41:36 -04002805 if (!Objects.equals(first.usesStandardHeader(), second.usesStandardHeader())) {
2806 return true;
2807 }
2808
2809 if (areRemoteViewsChanged(first.mN.contentView, second.mN.contentView)) {
2810 return true;
2811 }
2812 if (areRemoteViewsChanged(first.mN.bigContentView, second.mN.bigContentView)) {
2813 return true;
2814 }
2815 if (areRemoteViewsChanged(first.mN.headsUpContentView, second.mN.headsUpContentView)) {
2816 return true;
2817 }
2818
2819 return false;
2820 }
2821
2822 private static boolean areRemoteViewsChanged(RemoteViews first, RemoteViews second) {
2823 if (first == null && second == null) {
2824 return false;
2825 }
2826 if (first == null && second != null || first != null && second == null) {
2827 return true;
2828 }
2829
2830 if (!Objects.equals(first.getLayoutId(), second.getLayoutId())) {
2831 return true;
2832 }
2833
2834 if (!Objects.equals(first.getSequenceNumber(), second.getSequenceNumber())) {
2835 return true;
2836 }
2837
2838 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05002839 }
2840
2841 /**
Robin Leead7e72a2017-12-04 15:45:46 +01002842 * Parcelling creates multiple copies of objects in {@code extras}. Fix them.
2843 * <p>
2844 * For backwards compatibility {@code extras} holds some references to "real" member data such
2845 * as {@link getLargeIcon()} which is mirrored by {@link #EXTRA_LARGE_ICON}. This is mostly
2846 * fine as long as the object stays in one process.
2847 * <p>
2848 * However, once the notification goes into a parcel each reference gets marshalled separately,
2849 * wasting memory. Especially with large images on Auto and TV, this is worth fixing.
2850 */
2851 private void fixDuplicateExtras() {
2852 if (extras != null) {
Robin Leead7e72a2017-12-04 15:45:46 +01002853 fixDuplicateExtra(mLargeIcon, EXTRA_LARGE_ICON);
2854 }
2855 }
2856
2857 /**
2858 * If we find an extra that's exactly the same as one of the "real" fields but refers to a
2859 * separate object, replace it with the field's version to avoid holding duplicate copies.
2860 */
2861 private void fixDuplicateExtra(@Nullable Parcelable original, @NonNull String extraName) {
2862 if (original != null && extras.getParcelable(extraName) != null) {
2863 extras.putParcelable(extraName, original);
2864 }
2865 }
2866
2867 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002868 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2869 * layout.
2870 *
2871 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2872 * in the view.</p>
2873 * @param context The context for your application / activity.
2874 * @param contentTitle The title that goes in the expanded entry.
2875 * @param contentText The text that goes in the expanded entry.
2876 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2877 * If this is an activity, it must include the
2878 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002879 * that you take care of task management as described in the
2880 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2881 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002882 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002883 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002884 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002885 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002886 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002887 public void setLatestEventInfo(Context context,
2888 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002889 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2890 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2891 new Throwable());
2892 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002893
Selim Cinek4ac6f602016-06-13 15:47:03 -07002894 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2895 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2896 }
2897
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002898 // ensure that any information already set directly is preserved
2899 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002900
2901 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002902 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002903 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002904 }
2905 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002906 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002907 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002908 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002909
2910 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002911 }
2912
Julia Reynoldsda303542015-11-23 14:00:20 -05002913 /**
2914 * @hide
2915 */
2916 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002917 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002918 }
2919
2920 /**
2921 * @hide
2922 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002923 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002924 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002925 }
2926
Yi Jin6b514142017-10-30 14:54:12 -07002927 /**
2928 * @hide
2929 */
2930 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2931 long token = proto.start(fieldId);
2932 proto.write(NotificationProto.CHANNEL_ID, getChannelId());
2933 proto.write(NotificationProto.HAS_TICKER_TEXT, this.tickerText != null);
2934 proto.write(NotificationProto.FLAGS, this.flags);
2935 proto.write(NotificationProto.COLOR, this.color);
2936 proto.write(NotificationProto.CATEGORY, this.category);
2937 proto.write(NotificationProto.GROUP_KEY, this.mGroupKey);
2938 proto.write(NotificationProto.SORT_KEY, this.mSortKey);
2939 if (this.actions != null) {
2940 proto.write(NotificationProto.ACTION_LENGTH, this.actions.length);
2941 }
2942 if (this.visibility >= VISIBILITY_SECRET && this.visibility <= VISIBILITY_PUBLIC) {
2943 proto.write(NotificationProto.VISIBILITY, this.visibility);
2944 }
2945 if (publicVersion != null) {
2946 publicVersion.writeToProto(proto, NotificationProto.PUBLIC_VERSION);
2947 }
2948 proto.end(token);
2949 }
2950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951 @Override
2952 public String toString() {
2953 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002954 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04002955 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002956 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002957 sb.append(priority);
2958 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002959 if (contentView != null) {
2960 sb.append(contentView.getPackage());
2961 sb.append("/0x");
2962 sb.append(Integer.toHexString(contentView.getLayoutId()));
2963 } else {
2964 sb.append("null");
2965 }
2966 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002967 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2968 sb.append("default");
2969 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002970 int N = this.vibrate.length-1;
2971 sb.append("[");
2972 for (int i=0; i<N; i++) {
2973 sb.append(this.vibrate[i]);
2974 sb.append(',');
2975 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002976 if (N != -1) {
2977 sb.append(this.vibrate[N]);
2978 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002979 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002980 } else {
2981 sb.append("null");
2982 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002983 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002984 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002985 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002986 } else if (this.sound != null) {
2987 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002988 } else {
2989 sb.append("null");
2990 }
Chris Wren365b6d32015-07-16 10:39:26 -04002991 if (this.tickerText != null) {
2992 sb.append(" tick");
2993 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002994 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002995 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002996 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002997 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002998 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002999 if (this.category != null) {
3000 sb.append(" category=");
3001 sb.append(this.category);
3002 }
3003 if (this.mGroupKey != null) {
3004 sb.append(" groupKey=");
3005 sb.append(this.mGroupKey);
3006 }
3007 if (this.mSortKey != null) {
3008 sb.append(" sortKey=");
3009 sb.append(this.mSortKey);
3010 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003011 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04003012 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003013 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04003014 }
3015 sb.append(" vis=");
3016 sb.append(visibilityToString(this.visibility));
3017 if (this.publicVersion != null) {
3018 sb.append(" publicVersion=");
3019 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003020 }
3021 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003022 return sb.toString();
3023 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003024
Dan Sandler1b718782014-07-18 12:43:45 -04003025 /**
3026 * {@hide}
3027 */
3028 public static String visibilityToString(int vis) {
3029 switch (vis) {
3030 case VISIBILITY_PRIVATE:
3031 return "PRIVATE";
3032 case VISIBILITY_PUBLIC:
3033 return "PUBLIC";
3034 case VISIBILITY_SECRET:
3035 return "SECRET";
3036 default:
3037 return "UNKNOWN(" + String.valueOf(vis) + ")";
3038 }
3039 }
3040
Joe Onoratocb109a02011-01-18 17:57:41 -08003041 /**
John Spurlock1d881a12015-03-18 19:21:54 -04003042 * {@hide}
3043 */
3044 public static String priorityToString(@Priority int pri) {
3045 switch (pri) {
3046 case PRIORITY_MIN:
3047 return "MIN";
3048 case PRIORITY_LOW:
3049 return "LOW";
3050 case PRIORITY_DEFAULT:
3051 return "DEFAULT";
3052 case PRIORITY_HIGH:
3053 return "HIGH";
3054 case PRIORITY_MAX:
3055 return "MAX";
3056 default:
3057 return "UNKNOWN(" + String.valueOf(pri) + ")";
3058 }
3059 }
3060
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04003061 /**
3062 * @hide
3063 */
3064 public boolean hasCompletedProgress() {
3065 // not a progress notification; can't be complete
3066 if (!extras.containsKey(EXTRA_PROGRESS)
3067 || !extras.containsKey(EXTRA_PROGRESS_MAX)) {
3068 return false;
3069 }
3070 // many apps use max 0 for 'indeterminate'; not complete
3071 if (extras.getInt(EXTRA_PROGRESS_MAX) == 0) {
3072 return false;
3073 }
3074 return extras.getInt(EXTRA_PROGRESS) == extras.getInt(EXTRA_PROGRESS_MAX);
3075 }
3076
Jeff Sharkey000ce802017-04-29 13:13:27 -06003077 /** @removed */
3078 @Deprecated
Julia Reynolds37856052016-11-11 09:20:07 -05003079 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003080 return mChannelId;
3081 }
3082
3083 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003084 * Returns the id of the channel this notification posts to.
3085 */
3086 public String getChannelId() {
3087 return mChannelId;
3088 }
3089
Jeff Sharkey000ce802017-04-29 13:13:27 -06003090 /** @removed */
3091 @Deprecated
Julia Reynolds2a128742016-11-28 14:29:25 -05003092 public long getTimeout() {
3093 return mTimeout;
3094 }
3095
3096 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003097 * Returns the duration from posting after which this notification should be canceled by the
3098 * system, if it's not canceled already.
3099 */
3100 public long getTimeoutAfter() {
3101 return mTimeout;
3102 }
3103
3104 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003105 * Returns what icon should be shown for this notification if it is being displayed in a
3106 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
3107 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
3108 */
3109 public int getBadgeIconType() {
3110 return mBadgeIcon;
3111 }
3112
3113 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003114 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04003115 *
3116 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
3117 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003118 */
3119 public String getShortcutId() {
3120 return mShortcutId;
3121 }
3122
Julia Reynolds3aedded2017-03-31 14:42:09 -04003123
3124 /**
3125 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
3126 */
3127 public CharSequence getSettingsText() {
3128 return mSettingsText;
3129 }
3130
Julia Reynolds13d898c2017-02-02 12:22:05 -05003131 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003132 * Returns which type of notifications in a group are responsible for audibly alerting the
3133 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
3134 * {@link #GROUP_ALERT_SUMMARY}.
3135 */
3136 public @GroupAlertBehavior int getGroupAlertBehavior() {
3137 return mGroupAlertBehavior;
3138 }
3139
3140 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003141 * Returns the bubble metadata that will be used to display app content in a floating window
3142 * over the existing foreground activity.
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003143 */
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003144 public BubbleMetadata getBubbleMetadata() {
3145 return mBubbleMetadata;
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003146 }
3147
Gustav Sennton79ebf4b2018-12-19 10:31:51 +00003148 /**
3149 * Returns whether the platform is allowed (by the app developer) to generate contextual actions
3150 * for this notification.
3151 */
Gustav Sennton761884c2018-11-19 17:40:19 +00003152 public boolean getAllowSystemGeneratedContextualActions() {
3153 return mAllowSystemGeneratedContextualActions;
3154 }
3155
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003156 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003157 * The small icon representing this notification in the status bar and content view.
3158 *
3159 * @return the small icon representing this notification.
3160 *
3161 * @see Builder#getSmallIcon()
3162 * @see Builder#setSmallIcon(Icon)
3163 */
3164 public Icon getSmallIcon() {
3165 return mSmallIcon;
3166 }
3167
3168 /**
3169 * Used when notifying to clean up legacy small icons.
3170 * @hide
3171 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003172 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04003173 public void setSmallIcon(Icon icon) {
3174 mSmallIcon = icon;
3175 }
3176
3177 /**
3178 * The large icon shown in this notification's content view.
3179 * @see Builder#getLargeIcon()
3180 * @see Builder#setLargeIcon(Icon)
3181 */
3182 public Icon getLargeIcon() {
3183 return mLargeIcon;
3184 }
3185
3186 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02003187 * @hide
3188 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003189 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003190 public boolean isGroupSummary() {
3191 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
3192 }
3193
3194 /**
3195 * @hide
3196 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003197 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003198 public boolean isGroupChild() {
3199 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
3200 }
3201
3202 /**
Julia Reynolds30203152017-05-26 13:36:31 -04003203 * @hide
3204 */
3205 public boolean suppressAlertingDueToGrouping() {
3206 if (isGroupSummary()
3207 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
3208 return true;
3209 } else if (isGroupChild()
3210 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
3211 return true;
3212 }
3213 return false;
3214 }
3215
Tony Mak638430e2018-10-08 19:19:10 +01003216
3217 /**
3218 * Finds and returns a remote input and its corresponding action.
3219 *
3220 * @param requiresFreeform requires the remoteinput to allow freeform or not.
3221 * @return the result pair, {@code null} if no result is found.
3222 *
3223 * @hide
3224 */
3225 @Nullable
3226 public Pair<RemoteInput, Action> findRemoteInputActionPair(boolean requiresFreeform) {
3227 if (actions == null) {
3228 return null;
3229 }
3230 for (Notification.Action action : actions) {
3231 if (action.getRemoteInputs() == null) {
3232 continue;
3233 }
3234 RemoteInput resultRemoteInput = null;
3235 for (RemoteInput remoteInput : action.getRemoteInputs()) {
3236 if (remoteInput.getAllowFreeFormInput() || !requiresFreeform) {
3237 resultRemoteInput = remoteInput;
3238 }
3239 }
3240 if (resultRemoteInput != null) {
3241 return Pair.create(resultRemoteInput, action);
3242 }
3243 }
3244 return null;
3245 }
3246
Julia Reynolds30203152017-05-26 13:36:31 -04003247 /**
Gustav Sennton005d7a02019-01-04 13:41:32 +00003248 * Returns the actions that are contextual out of the actions in this notification.
Gustav Senntoneab53682018-11-01 16:30:23 +00003249 *
3250 * @hide
3251 */
3252 public List<Notification.Action> getContextualActions() {
3253 if (actions == null) return Collections.emptyList();
3254
3255 List<Notification.Action> contextualActions = new ArrayList<>();
3256 for (Notification.Action action : actions) {
Gustav Sennton005d7a02019-01-04 13:41:32 +00003257 if (action.isContextual()) {
Gustav Senntoneab53682018-11-01 16:30:23 +00003258 contextualActions.add(action);
3259 }
3260 }
3261 return contextualActions;
3262 }
3263
3264 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003265 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08003266 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003267 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07003268 * content views using the platform's notification layout template. If your app supports
3269 * versions of Android as old as API level 4, you can instead use
3270 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
3271 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
3272 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08003273 *
Scott Main183bf112012-08-13 19:12:13 -07003274 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08003275 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003276 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07003277 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003278 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3279 * .setContentText(subject)
3280 * .setSmallIcon(R.drawable.new_mail)
3281 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003282 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003283 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08003284 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003285 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003286 /**
3287 * @hide
3288 */
3289 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
3290 "android.rebuild.contentViewActionCount";
3291 /**
3292 * @hide
3293 */
3294 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
3295 = "android.rebuild.bigViewActionCount";
3296 /**
3297 * @hide
3298 */
3299 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
3300 = "android.rebuild.hudViewActionCount";
3301
Selim Cinek6743c0b2017-01-18 18:24:01 -08003302 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
3303 SystemProperties.getBoolean("notifications.only_title", true);
3304
Selim Cinek389edcd2017-05-11 19:16:44 -07003305 /**
3306 * The lightness difference that has to be added to the primary text color to obtain the
3307 * secondary text color when the background is light.
3308 */
3309 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
3310
3311 /**
3312 * The lightness difference that has to be added to the primary text color to obtain the
3313 * secondary text color when the background is dark.
3314 * A bit less then the above value, since it looks better on dark backgrounds.
3315 */
3316 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
3317
Joe Onorato46439ce2010-11-19 13:56:21 -08003318 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003319 private Notification mN;
3320 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003321 private Style mStyle;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003322 @UnsupportedAppUsage
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003323 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Selim Cineke7238dd2017-12-14 17:48:32 -08003324 private ArrayList<Person> mPersonList = new ArrayList<>();
Lucas Dupina291d192018-06-07 13:59:42 -07003325 private ContrastColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08003326 private boolean mIsLegacy;
3327 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02003328
3329 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08003330 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
3331 */
3332 private int mCachedContrastColor = COLOR_INVALID;
3333 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08003334 /**
Selim Cinek4717d862018-04-19 09:19:15 +08003335 * Caches a ambient version of {@link #mCachedAmbientColorIsFor}.
Adrian Roos487374f2017-01-11 15:48:14 -08003336 */
3337 private int mCachedAmbientColor = COLOR_INVALID;
3338 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Selim Cinek4717d862018-04-19 09:19:15 +08003339 /**
3340 * A neutral color color that can be used for icons.
3341 */
3342 private int mNeutralColor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08003343
3344 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08003345 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
3346 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
3347 */
3348 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08003349 private int mTextColorsAreForBackground = COLOR_INVALID;
3350 private int mPrimaryTextColor = COLOR_INVALID;
3351 private int mSecondaryTextColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07003352 private int mBackgroundColor = COLOR_INVALID;
3353 private int mForegroundColor = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07003354 /**
3355 * A temporary location where actions are stored. If != null the view originally has action
3356 * but doesn't have any for this inflation.
3357 */
3358 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07003359 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003360
Anthony Chenad4d1582017-04-10 16:07:58 -07003361 private boolean mTintActionButtons;
3362 private boolean mInNightMode;
3363
Adrian Roos70d7aa32017-01-11 15:39:06 -08003364 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003365 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08003366 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003367 * @param context
3368 * A {@link Context} that will be used by the Builder to construct the
3369 * RemoteViews. The Context will not be held past the lifetime of this Builder
3370 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003371 * @param channelId
3372 * The constructed Notification will be posted on this
3373 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
3374 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003375 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003376 public Builder(Context context, String channelId) {
3377 this(context, (Notification) null);
3378 mN.mChannelId = channelId;
3379 }
3380
3381 /**
3382 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
3383 * instead. All posted Notifications must specify a NotificationChannel Id.
3384 */
3385 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003386 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003387 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003388 }
3389
Joe Onoratocb109a02011-01-18 17:57:41 -08003390 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003391 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02003392 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003393 public Builder(Context context, Notification toAdopt) {
3394 mContext = context;
Anthony Chenad4d1582017-04-10 16:07:58 -07003395 Resources res = mContext.getResources();
3396 mTintActionButtons = res.getBoolean(R.bool.config_tintNotificationActionButtons);
3397
3398 if (res.getBoolean(R.bool.config_enableNightMode)) {
3399 Configuration currentConfig = res.getConfiguration();
3400 mInNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
3401 == Configuration.UI_MODE_NIGHT_YES;
3402 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003403
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003404 if (toAdopt == null) {
3405 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003406 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
3407 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
3408 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003409 mN.priority = PRIORITY_DEFAULT;
3410 mN.visibility = VISIBILITY_PRIVATE;
3411 } else {
3412 mN = toAdopt;
3413 if (mN.actions != null) {
3414 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003415 }
3416
Selim Cineke7238dd2017-12-14 17:48:32 -08003417 if (mN.extras.containsKey(EXTRA_PEOPLE_LIST)) {
3418 ArrayList<Person> people = mN.extras.getParcelableArrayList(EXTRA_PEOPLE_LIST);
3419 mPersonList.addAll(people);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003420 }
3421
Selim Cinek4ac6f602016-06-13 15:47:03 -07003422 if (mN.getSmallIcon() == null && mN.icon != 0) {
3423 setSmallIcon(mN.icon);
3424 }
3425
3426 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
3427 setLargeIcon(mN.largeIcon);
3428 }
3429
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003430 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
3431 if (!TextUtils.isEmpty(templateClass)) {
3432 final Class<? extends Style> styleClass
3433 = getNotificationStyleClass(templateClass);
3434 if (styleClass == null) {
3435 Log.d(TAG, "Unknown style class: " + templateClass);
3436 } else {
3437 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003438 final Constructor<? extends Style> ctor =
3439 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003440 ctor.setAccessible(true);
3441 final Style style = ctor.newInstance();
3442 style.restoreFromExtras(mN.extras);
3443
3444 if (style != null) {
3445 setStyle(style);
3446 }
3447 } catch (Throwable t) {
3448 Log.e(TAG, "Could not create Style", t);
3449 }
3450 }
3451 }
3452
3453 }
3454 }
3455
Lucas Dupina291d192018-06-07 13:59:42 -07003456 private ContrastColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08003457 if (mColorUtil == null) {
Lucas Dupina291d192018-06-07 13:59:42 -07003458 mColorUtil = ContrastColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003459 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003460 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003461 }
3462
3463 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003464 * If this notification is duplicative of a Launcher shortcut, sets the
3465 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
3466 * the shortcut.
3467 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04003468 * This field will be ignored by Launchers that don't support badging, don't show
3469 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003470 *
3471 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
3472 * supersedes
3473 */
3474 public Builder setShortcutId(String shortcutId) {
3475 mN.mShortcutId = shortcutId;
3476 return this;
3477 }
3478
3479 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003480 * Sets which icon to display as a badge for this notification.
3481 *
3482 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
3483 * {@link #BADGE_ICON_LARGE}.
3484 *
3485 * Note: This value might be ignored, for launchers that don't support badge icons.
3486 */
Julia Reynolds612beb22017-03-30 10:48:30 -04003487 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04003488 mN.mBadgeIcon = icon;
3489 return this;
3490 }
3491
3492 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003493 * Sets the group alert behavior for this notification. Use this method to mute this
3494 * notification if alerts for this notification's group should be handled by a different
3495 * notification. This is only applicable for notifications that belong to a
Julia Reynolds399d9bf2017-08-11 12:52:14 -04003496 * {@link #setGroup(String) group}. This must be called on all notifications you want to
3497 * mute. For example, if you want only the summary of your group to make noise, all
3498 * children in the group should have the group alert behavior {@link #GROUP_ALERT_SUMMARY}.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003499 *
3500 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
3501 */
3502 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
3503 mN.mGroupAlertBehavior = groupAlertBehavior;
3504 return this;
3505 }
3506
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003507 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003508 * Sets the {@link BubbleMetadata} that will be used to display app content in a floating
3509 * window over the existing foreground activity.
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003510 *
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003511 * <p>This data will be ignored unless the notification is posted to a channel that
3512 * allows {@link NotificationChannel#canBubble() bubbles}.</p>
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003513 *
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003514 * <b>Notifications with a valid and allowed bubble metadata will display in collapsed state
3515 * outside of the notification shade on unlocked devices. When a user interacts with the
3516 * collapsed state, the bubble intent will be invoked and displayed.</b>
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003517 */
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003518 public Builder setBubbleMetadata(BubbleMetadata data) {
3519 mN.mBubbleMetadata = data;
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003520 return this;
3521 }
3522
Jeff Sharkey000ce802017-04-29 13:13:27 -06003523 /** @removed */
3524 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003525 public Builder setChannel(String channelId) {
3526 mN.mChannelId = channelId;
3527 return this;
3528 }
3529
3530 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003531 * Specifies the channel the notification should be delivered on.
3532 */
3533 public Builder setChannelId(String channelId) {
3534 mN.mChannelId = channelId;
3535 return this;
3536 }
3537
Jeff Sharkey000ce802017-04-29 13:13:27 -06003538 /** @removed */
3539 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05003540 public Builder setTimeout(long durationMs) {
3541 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05003542 return this;
3543 }
3544
3545 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003546 * Specifies a duration in milliseconds after which this notification should be canceled,
3547 * if it is not already canceled.
3548 */
3549 public Builder setTimeoutAfter(long durationMs) {
3550 mN.mTimeout = durationMs;
3551 return this;
3552 }
3553
3554 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003555 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003556 *
3557 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
3558 * shown anymore by default and must be opted into by using
3559 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003560 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003561 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08003562 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003563 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003564 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08003565 return this;
3566 }
3567
Joe Onoratocb109a02011-01-18 17:57:41 -08003568 /**
Griff Hazen50c11652014-05-16 09:46:31 -07003569 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07003570 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003571 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
3572 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07003573 */
3574 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003575 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07003576 return this;
3577 }
3578
3579 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003580 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08003581 *
3582 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003583 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003584 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003585 * Useful when showing an elapsed time (like an ongoing phone call).
3586 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08003587 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07003588 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08003589 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003590 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003591 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07003592 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003593 */
3594 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003595 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003596 return this;
3597 }
3598
3599 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08003600 * Sets the Chronometer to count down instead of counting up.
3601 *
3602 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
3603 * If it isn't set the chronometer will count up.
3604 *
3605 * @see #setUsesChronometer(boolean)
3606 */
Adrian Roos96b7e202016-05-17 13:50:38 -07003607 public Builder setChronometerCountDown(boolean countDown) {
3608 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003609 return this;
3610 }
3611
3612 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003613 * Set the small icon resource, which will be used to represent the notification in the
3614 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003615 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003616
3617 * The platform template for the expanded view will draw this icon in the left, unless a
3618 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3619 * icon will be moved to the right-hand side.
3620 *
3621
3622 * @param icon
3623 * A resource ID in the application's package of the drawable to use.
3624 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003625 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003626 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003627 return setSmallIcon(icon != 0
3628 ? Icon.createWithResource(mContext, icon)
3629 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003630 }
3631
Joe Onoratocb109a02011-01-18 17:57:41 -08003632 /**
3633 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3634 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3635 * LevelListDrawable}.
3636 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003637 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003638 * @param level The level to use for the icon.
3639 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003640 * @see Notification#icon
3641 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003642 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003643 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003644 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003645 return setSmallIcon(icon);
3646 }
3647
3648 /**
3649 * Set the small icon, which will be used to represent the notification in the
koprivadebd4ee2018-09-13 10:59:46 -07003650 * status bar and content view (unless overridden there by a
Dan Sandlerd63f9322015-05-06 15:18:49 -04003651 * {@link #setLargeIcon(Bitmap) large icon}).
3652 *
3653 * @param icon An Icon object to use.
3654 * @see Notification#icon
3655 */
3656 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003657 mN.setSmallIcon(icon);
3658 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3659 mN.icon = icon.getResId();
3660 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003661 return this;
3662 }
3663
Joe Onoratocb109a02011-01-18 17:57:41 -08003664 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003665 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003666 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003667 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003668 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003669 return this;
3670 }
3671
Joe Onoratocb109a02011-01-18 17:57:41 -08003672 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003673 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003674 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003675 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003676 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003677 return this;
3678 }
3679
Joe Onoratocb109a02011-01-18 17:57:41 -08003680 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003681 * This provides some additional information that is displayed in the notification. No
3682 * guarantees are given where exactly it is displayed.
3683 *
3684 * <p>This information should only be provided if it provides an essential
3685 * benefit to the understanding of the notification. The more text you provide the
3686 * less readable it becomes. For example, an email client should only provide the account
3687 * name here if more than one email account has been added.</p>
3688 *
3689 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3690 * notification header area.
3691 *
3692 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3693 * this will be shown in the third line of text in the platform notification template.
3694 * You should not be using {@link #setProgress(int, int, boolean)} at the
3695 * same time on those versions; they occupy the same place.
3696 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003697 */
3698 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003699 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003700 return this;
3701 }
3702
3703 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003704 * Provides text that will appear as a link to your application's settings.
3705 *
3706 * <p>This text does not appear within notification {@link Style templates} but may
3707 * appear when the user uses an affordance to learn more about the notification.
3708 * Additionally, this text will not appear unless you provide a valid link target by
3709 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3710 *
3711 * <p>This text is meant to be concise description about what the user can customize
3712 * when they click on this link. The recommended maximum length is 40 characters.
3713 * @param text
3714 * @return
3715 */
3716 public Builder setSettingsText(CharSequence text) {
3717 mN.mSettingsText = safeCharSequence(text);
3718 return this;
3719 }
3720
3721 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003722 * Set the remote input history.
3723 *
3724 * This should be set to the most recent inputs that have been sent
3725 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3726 * longer relevant (e.g. for chat notifications once the other party has responded).
3727 *
3728 * The most recent input must be stored at the 0 index, the second most recent at the
3729 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3730 * and how much of each individual input is shown.
3731 *
3732 * <p>Note: The reply text will only be shown on notifications that have least one action
3733 * with a {@code RemoteInput}.</p>
3734 */
3735 public Builder setRemoteInputHistory(CharSequence[] text) {
3736 if (text == null) {
3737 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3738 } else {
3739 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3740 CharSequence[] safe = new CharSequence[N];
3741 for (int i = 0; i < N; i++) {
3742 safe[i] = safeCharSequence(text[i]);
3743 }
3744 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3745 }
3746 return this;
3747 }
3748
3749 /**
Kenny Guya0f6de82018-04-06 16:20:16 +01003750 * Sets whether remote history entries view should have a spinner.
3751 * @hide
3752 */
3753 public Builder setShowRemoteInputSpinner(boolean showSpinner) {
3754 mN.extras.putBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER, showSpinner);
3755 return this;
3756 }
3757
3758 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +01003759 * Sets whether smart reply buttons should be hidden.
3760 * @hide
3761 */
3762 public Builder setHideSmartReplies(boolean hideSmartReplies) {
3763 mN.extras.putBoolean(EXTRA_HIDE_SMART_REPLIES, hideSmartReplies);
3764 return this;
3765 }
3766
3767 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003768 * Sets the number of items this notification represents. May be displayed as a badge count
3769 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003770 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08003771 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003772 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003773 return this;
3774 }
3775
Joe Onoratocb109a02011-01-18 17:57:41 -08003776 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003777 * A small piece of additional information pertaining to this notification.
3778 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003779 * The platform template will draw this on the last line of the notification, at the far
3780 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003781 *
3782 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3783 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3784 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003785 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003786 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003787 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003788 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003789 return this;
3790 }
3791
Joe Onoratocb109a02011-01-18 17:57:41 -08003792 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003793 * Set the progress this notification represents.
3794 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003795 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003796 */
3797 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003798 mN.extras.putInt(EXTRA_PROGRESS, progress);
3799 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3800 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003801 return this;
3802 }
3803
3804 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003805 * Supply a custom RemoteViews to use instead of the platform template.
3806 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003807 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003808 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003809 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003810 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003811 return setCustomContentView(views);
3812 }
3813
3814 /**
3815 * Supply custom RemoteViews to use instead of the platform template.
3816 *
3817 * This will override the layout that would otherwise be constructed by this Builder
3818 * object.
3819 */
3820 public Builder setCustomContentView(RemoteViews contentView) {
3821 mN.contentView = contentView;
3822 return this;
3823 }
3824
3825 /**
3826 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3827 *
3828 * This will override the expanded layout that would otherwise be constructed by this
3829 * Builder object.
3830 */
3831 public Builder setCustomBigContentView(RemoteViews contentView) {
3832 mN.bigContentView = contentView;
3833 return this;
3834 }
3835
3836 /**
3837 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3838 *
3839 * This will override the heads-up layout that would otherwise be constructed by this
3840 * Builder object.
3841 */
3842 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3843 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003844 return this;
3845 }
3846
Joe Onoratocb109a02011-01-18 17:57:41 -08003847 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003848 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3849 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003850 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3851 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3852 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003853 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003854 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003855 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003856 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003857 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003858 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003859 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003860 return this;
3861 }
3862
Joe Onoratocb109a02011-01-18 17:57:41 -08003863 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003864 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3865 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003866 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003867 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003868 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003869 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003870 return this;
3871 }
3872
Joe Onoratocb109a02011-01-18 17:57:41 -08003873 /**
3874 * An intent to launch instead of posting the notification to the status bar.
3875 * Only for use with extremely high-priority notifications demanding the user's
3876 * <strong>immediate</strong> attention, such as an incoming phone call or
3877 * alarm clock that the user has explicitly set to a particular time.
3878 * If this facility is used for something else, please give the user an option
3879 * to turn it off and use a normal notification, as this can be extremely
3880 * disruptive.
3881 *
Chris Wren47c20a12014-06-18 17:27:29 -04003882 * <p>
3883 * The system UI may choose to display a heads-up notification, instead of
3884 * launching this intent, while the user is using the device.
3885 * </p>
Julia Reynolds47fd15f2018-11-28 10:16:00 -05003886 * <p>Apps targeting {@link Build.VERSION_CODES#Q} and above will have to request
3887 * a permission ({@link android.Manifest.permission#USE_FULL_SCREEN_INTENT}) in order to
3888 * use full screen intents.</p>
Chris Wren47c20a12014-06-18 17:27:29 -04003889 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003890 * @param intent The pending intent to launch.
3891 * @param highPriority Passing true will cause this notification to be sent
3892 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003893 *
3894 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003895 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003896 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003897 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003898 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3899 return this;
3900 }
3901
Joe Onoratocb109a02011-01-18 17:57:41 -08003902 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003903 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003904 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003905 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003906 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003907 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003908 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003909 return this;
3910 }
3911
Joe Onoratocb109a02011-01-18 17:57:41 -08003912 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003913 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003914 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003915 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003916 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003917 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003918 setTicker(tickerText);
3919 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003920 return this;
3921 }
3922
Joe Onoratocb109a02011-01-18 17:57:41 -08003923 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003924 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003925 *
3926 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04003927 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3928 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04003929 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003930 public Builder setLargeIcon(Bitmap b) {
3931 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3932 }
3933
3934 /**
3935 * Add a large icon to the notification content view.
3936 *
3937 * In the platform template, this image will be shown on the left of the notification view
3938 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3939 * badge atop the large icon).
3940 */
3941 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003942 mN.mLargeIcon = icon;
3943 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08003944 return this;
3945 }
3946
Joe Onoratocb109a02011-01-18 17:57:41 -08003947 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003948 * Set the sound to play.
3949 *
John Spurlockc0650f022014-07-19 13:22:39 -04003950 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
3951 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003952 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003953 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003954 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003955 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08003956 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003957 mN.sound = sound;
3958 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08003959 return this;
3960 }
3961
Joe Onoratocb109a02011-01-18 17:57:41 -08003962 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003963 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08003964 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003965 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
3966 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003967 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003968 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07003969 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003970 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08003971 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003972 mN.sound = sound;
3973 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08003974 return this;
3975 }
3976
Joe Onoratocb109a02011-01-18 17:57:41 -08003977 /**
John Spurlockc0650f022014-07-19 13:22:39 -04003978 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
3979 * use during playback.
3980 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003981 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04003982 * @see Notification#sound
3983 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003984 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04003985 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003986 mN.sound = sound;
3987 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04003988 return this;
3989 }
3990
3991 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003992 * Set the vibration pattern to use.
3993 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003994 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
3995 * <code>pattern</code> parameter.
3996 *
Chris Wren47c20a12014-06-18 17:27:29 -04003997 * <p>
3998 * A notification that vibrates is more likely to be presented as a heads-up notification.
3999 * </p>
4000 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004001 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004002 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08004003 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004004 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004005 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004006 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08004007 return this;
4008 }
4009
Joe Onoratocb109a02011-01-18 17:57:41 -08004010 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004011 * Set the desired color for the indicator LED on the device, as well as the
4012 * blink duty cycle (specified in milliseconds).
4013 *
4014
4015 * Not all devices will honor all (or even any) of these values.
4016 *
Julia Reynolds529e3322017-02-06 08:33:01 -05004017 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004018 * @see Notification#ledARGB
4019 * @see Notification#ledOnMS
4020 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08004021 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004022 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08004023 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004024 mN.ledARGB = argb;
4025 mN.ledOnMS = onMs;
4026 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004027 if (onMs != 0 || offMs != 0) {
4028 mN.flags |= FLAG_SHOW_LIGHTS;
4029 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004030 return this;
4031 }
4032
Joe Onoratocb109a02011-01-18 17:57:41 -08004033 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004034 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08004035 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004036
4037 * Ongoing notifications cannot be dismissed by the user, so your application or service
4038 * must take care of canceling them.
4039 *
4040
4041 * They are typically used to indicate a background task that the user is actively engaged
4042 * with (e.g., playing music) or is pending in some way and therefore occupying the device
4043 * (e.g., a file download, sync operation, active network connection).
4044 *
4045
4046 * @see Notification#FLAG_ONGOING_EVENT
Joe Onoratocb109a02011-01-18 17:57:41 -08004047 */
Joe Onorato46439ce2010-11-19 13:56:21 -08004048 public Builder setOngoing(boolean ongoing) {
4049 setFlag(FLAG_ONGOING_EVENT, ongoing);
4050 return this;
4051 }
4052
Joe Onoratocb109a02011-01-18 17:57:41 -08004053 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08004054 * Set whether this notification should be colorized. When set, the color set with
4055 * {@link #setColor(int)} will be used as the background color of this notification.
4056 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08004057 * This should only be used for high priority ongoing tasks like navigation, an ongoing
4058 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08004059 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07004060 * For most styles, the coloring will only be applied if the notification is for a
4061 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08004062 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07004063 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08004064 *
Aurimas Liutikas7f695332018-05-31 21:07:32 -07004065 * @see #setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08004066 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08004067 */
4068 public Builder setColorized(boolean colorize) {
4069 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
4070 return this;
4071 }
4072
4073 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08004074 * Set this flag if you would only like the sound, vibrate
4075 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004076 *
4077 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08004078 */
Joe Onorato46439ce2010-11-19 13:56:21 -08004079 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
4080 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
4081 return this;
4082 }
4083
Joe Onoratocb109a02011-01-18 17:57:41 -08004084 /**
Julia Reynolds04499532016-09-13 14:04:53 -04004085 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004086 *
4087 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08004088 */
Joe Onorato46439ce2010-11-19 13:56:21 -08004089 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08004090 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08004091 return this;
4092 }
4093
Joe Onoratocb109a02011-01-18 17:57:41 -08004094 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08004095 * Set whether or not this notification should not bridge to other devices.
4096 *
4097 * <p>Some notifications can be bridged to other devices for remote display.
4098 * This hint can be set to recommend this notification not be bridged.
4099 */
4100 public Builder setLocalOnly(boolean localOnly) {
4101 setFlag(FLAG_LOCAL_ONLY, localOnly);
4102 return this;
4103 }
4104
4105 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004106 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08004107 * <p>
4108 * The value should be one or more of the following fields combined with
4109 * bitwise-or:
4110 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
4111 * <p>
4112 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004113 *
4114 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05004115 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004116 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08004117 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004118 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004119 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004120 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08004121 return this;
4122 }
4123
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004124 /**
4125 * Set the priority of this notification.
4126 *
4127 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004128 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004129 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004130 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07004131 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004132 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004133 return this;
4134 }
Joe Malin8d40d042012-11-05 11:36:40 -08004135
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004136 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04004137 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08004138 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04004139 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004140 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04004141 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004142 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004143 return this;
4144 }
4145
4146 /**
Chris Wrendde75302014-03-26 17:24:15 -04004147 * Add a person that is relevant to this notification.
4148 *
Chris Wrene6c48932014-09-29 17:19:27 -04004149 * <P>
4150 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04004151 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4152 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4153 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04004154 * </P>
4155 *
4156 * <P>
4157 * The person should be specified by the {@code String} representation of a
4158 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
4159 * </P>
4160 *
4161 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
4162 * URIs. The path part of these URIs must exist in the contacts database, in the
4163 * appropriate column, or the reference will be discarded as invalid. Telephone schema
4164 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
Selim Cineke7238dd2017-12-14 17:48:32 -08004165 * It is also possible to provide a URI with the schema {@code name:} in order to uniquely
4166 * identify a person without an entry in the contacts database.
Chris Wrene6c48932014-09-29 17:19:27 -04004167 * </P>
4168 *
4169 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04004170 * @see Notification#EXTRA_PEOPLE
Selim Cineke7238dd2017-12-14 17:48:32 -08004171 * @deprecated use {@link #addPerson(Person)}
Chris Wrendde75302014-03-26 17:24:15 -04004172 */
Chris Wrene6c48932014-09-29 17:19:27 -04004173 public Builder addPerson(String uri) {
Selim Cinek9acd6732018-03-23 16:39:02 -07004174 addPerson(new Person.Builder().setUri(uri).build());
Selim Cineke7238dd2017-12-14 17:48:32 -08004175 return this;
4176 }
4177
4178 /**
4179 * Add a person that is relevant to this notification.
4180 *
4181 * <P>
4182 * Depending on user preferences, this annotation may allow the notification to pass
4183 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4184 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4185 * appear more prominently in the user interface.
4186 * </P>
4187 *
4188 * <P>
4189 * A person should usually contain a uri in order to benefit from the ranking boost.
4190 * However, even if no uri is provided, it's beneficial to provide other people in the
4191 * notification, such that listeners and voice only devices can announce and handle them
4192 * properly.
4193 * </P>
4194 *
4195 * @param person the person to add.
4196 * @see Notification#EXTRA_PEOPLE_LIST
4197 */
4198 public Builder addPerson(Person person) {
4199 mPersonList.add(person);
Chris Wrendde75302014-03-26 17:24:15 -04004200 return this;
4201 }
4202
4203 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004204 * Set this notification to be part of a group of notifications sharing the same key.
4205 * Grouped notifications may display in a cluster or stack on devices which
4206 * support such rendering.
4207 *
4208 * <p>To make this notification the summary for its group, also call
4209 * {@link #setGroupSummary}. A sort order can be specified for group members by using
4210 * {@link #setSortKey}.
4211 * @param groupKey The group key of the group.
4212 * @return this object for method chaining
4213 */
4214 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004215 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004216 return this;
4217 }
4218
4219 /**
4220 * Set this notification to be the group summary for a group of notifications.
4221 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04004222 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
4223 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004224 * @param isGroupSummary Whether this notification should be a group summary.
4225 * @return this object for method chaining
4226 */
4227 public Builder setGroupSummary(boolean isGroupSummary) {
4228 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
4229 return this;
4230 }
4231
4232 /**
4233 * Set a sort key that orders this notification among other notifications from the
4234 * same package. This can be useful if an external sort was already applied and an app
4235 * would like to preserve this. Notifications will be sorted lexicographically using this
4236 * value, although providing different priorities in addition to providing sort key may
4237 * cause this value to be ignored.
4238 *
4239 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07004240 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004241 *
4242 * @see String#compareTo(String)
4243 */
4244 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004245 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004246 return this;
4247 }
4248
4249 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004250 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004251 *
Griff Hazen720042b2014-02-24 15:46:56 -08004252 * <p>Values within the Bundle will replace existing extras values in this Builder.
4253 *
4254 * @see Notification#extras
4255 */
Griff Hazen959591e2014-05-15 22:26:18 -07004256 public Builder addExtras(Bundle extras) {
4257 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004258 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08004259 }
4260 return this;
4261 }
4262
4263 /**
4264 * Set metadata for this notification.
4265 *
4266 * <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 -04004267 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004268 * called.
4269 *
Griff Hazen720042b2014-02-24 15:46:56 -08004270 * <p>Replaces any existing extras values with those from the provided Bundle.
4271 * Use {@link #addExtras} to merge in metadata instead.
4272 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004273 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004274 */
Griff Hazen959591e2014-05-15 22:26:18 -07004275 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004276 if (extras != null) {
4277 mUserExtras = extras;
4278 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004279 return this;
4280 }
4281
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004282 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004283 * Get the current metadata Bundle used by this notification Builder.
4284 *
4285 * <p>The returned Bundle is shared with this Builder.
4286 *
4287 * <p>The current contents of this Bundle are copied into the Notification each time
4288 * {@link #build()} is called.
4289 *
4290 * @see Notification#extras
4291 */
4292 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004293 return mUserExtras;
4294 }
4295
4296 private Bundle getAllExtras() {
4297 final Bundle saveExtras = (Bundle) mUserExtras.clone();
4298 saveExtras.putAll(mN.extras);
4299 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08004300 }
4301
4302 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004303 * Add an action to this notification. Actions are typically displayed by
4304 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004305 * <p>
4306 * Every action must have an icon (32dp square and matching the
4307 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4308 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4309 * <p>
4310 * A notification in its expanded form can display up to 3 actions, from left to right in
4311 * the order they were added. Actions will not be displayed when the notification is
4312 * collapsed, however, so be sure that any essential functions may be accessed by the user
4313 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004314 *
4315 * @param icon Resource ID of a drawable that represents the action.
4316 * @param title Text describing the action.
4317 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04004318 *
4319 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004320 */
Dan Sandler86647982015-05-13 23:41:13 -04004321 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004322 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004323 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004324 return this;
4325 }
4326
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004327 /**
Griff Hazen959591e2014-05-15 22:26:18 -07004328 * Add an action to this notification. Actions are typically displayed by
4329 * the system as a button adjacent to the notification content.
4330 * <p>
4331 * Every action must have an icon (32dp square and matching the
4332 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4333 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4334 * <p>
4335 * A notification in its expanded form can display up to 3 actions, from left to right in
4336 * the order they were added. Actions will not be displayed when the notification is
4337 * collapsed, however, so be sure that any essential functions may be accessed by the user
4338 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
4339 *
4340 * @param action The action to add.
4341 */
4342 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08004343 if (action != null) {
4344 mActions.add(action);
4345 }
Griff Hazen959591e2014-05-15 22:26:18 -07004346 return this;
4347 }
4348
4349 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004350 * Alter the complete list of actions attached to this notification.
4351 * @see #addAction(Action).
4352 *
4353 * @param actions
4354 * @return
4355 */
4356 public Builder setActions(Action... actions) {
4357 mActions.clear();
4358 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08004359 if (actions[i] != null) {
4360 mActions.add(actions[i]);
4361 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004362 }
4363 return this;
4364 }
4365
4366 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004367 * Add a rich notification style to be applied at build time.
4368 *
4369 * @param style Object responsible for modifying the notification style.
4370 */
4371 public Builder setStyle(Style style) {
4372 if (mStyle != style) {
4373 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004374 if (mStyle != null) {
4375 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004376 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
4377 } else {
4378 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004379 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004380 }
4381 return this;
4382 }
4383
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004384 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05004385 * Returns the style set by {@link #setStyle(Style)}.
4386 */
4387 public Style getStyle() {
4388 return mStyle;
4389 }
4390
4391 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004392 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07004393 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004394 * @return The same Builder.
4395 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06004396 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004397 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004398 return this;
4399 }
4400
4401 /**
4402 * Supply a replacement Notification whose contents should be shown in insecure contexts
4403 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
4404 * @param n A replacement notification, presumably with some or all info redacted.
4405 * @return The same Builder.
4406 */
4407 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004408 if (n != null) {
4409 mN.publicVersion = new Notification();
4410 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
4411 } else {
4412 mN.publicVersion = null;
4413 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004414 return this;
4415 }
4416
Griff Hazenb720abe2014-05-20 13:15:30 -07004417 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004418 * Apply an extender to this notification builder. Extenders may be used to add
4419 * metadata or change options on this builder.
4420 */
Griff Hazen61a9e862014-05-22 16:05:19 -07004421 public Builder extend(Extender extender) {
4422 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004423 return this;
4424 }
4425
Dan Sandler4e787062015-06-17 15:09:48 -04004426 /**
4427 * @hide
4428 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04004429 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08004430 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004431 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004432 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004433 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004434 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04004435 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08004436 }
4437
Dan Sandler26e81cf2014-05-06 10:01:27 -04004438 /**
4439 * Sets {@link Notification#color}.
4440 *
4441 * @param argb The accent color to use
4442 *
4443 * @return The same Builder.
4444 */
Tor Norbye80756e32015-03-02 09:39:27 -08004445 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004446 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004447 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04004448 return this;
4449 }
4450
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004451 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04004452 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
4453 // This user can never be a badged profile,
4454 // and also includes USER_ALL system notifications.
4455 return null;
4456 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02004457 // Note: This assumes that the current user can read the profile badge of the
4458 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08004459 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05004460 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004461 }
4462
4463 private Bitmap getProfileBadge() {
4464 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01004465 if (badge == null) {
4466 return null;
4467 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004468 final int size = mContext.getResources().getDimensionPixelSize(
4469 R.dimen.notification_badge_size);
4470 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004471 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004472 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004473 badge.draw(canvas);
4474 return bitmap;
4475 }
4476
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004477 private void bindProfileBadge(RemoteViews contentView, StandardTemplateParams p) {
Kenny Guy98193ea2014-07-24 19:54:37 +01004478 Bitmap profileBadge = getProfileBadge();
4479
Kenny Guy98193ea2014-07-24 19:54:37 +01004480 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004481 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
4482 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004483 if (isColorized(p)) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004484 contentView.setDrawableTint(R.id.profile_badge, false,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004485 getPrimaryTextColor(p), PorterDuff.Mode.SRC_ATOP);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004486 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004487 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004488 }
4489
Gus Prevasbae5ba32018-11-28 15:45:13 -05004490 private void bindAlertedIcon(RemoteViews contentView, StandardTemplateParams p) {
4491 contentView.setDrawableTint(
4492 R.id.alerted_icon,
4493 false /* targetBackground */,
4494 getNeutralColor(p),
4495 PorterDuff.Mode.SRC_ATOP);
4496 }
4497
Julia Reynoldsfc640012018-02-21 12:25:27 -05004498 /**
4499 * @hide
4500 */
4501 public boolean usesStandardHeader() {
4502 if (mN.mUsesStandardHeader) {
4503 return true;
4504 }
4505 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.N) {
4506 if (mN.contentView == null && mN.bigContentView == null) {
4507 return true;
4508 }
4509 }
4510 boolean contentViewUsesHeader = mN.contentView == null
4511 || STANDARD_LAYOUTS.contains(mN.contentView.getLayoutId());
4512 boolean bigContentViewUsesHeader = mN.bigContentView == null
4513 || STANDARD_LAYOUTS.contains(mN.bigContentView.getLayoutId());
4514 return contentViewUsesHeader && bigContentViewUsesHeader;
4515 }
4516
Christoph Studerfe718432014-09-01 18:21:18 +02004517 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004518 resetNotificationHeader(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02004519 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08004520 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004521 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08004522 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004523 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08004524 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08004525 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004526 }
4527
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004528 /**
4529 * Resets the notification header to its original state
4530 */
4531 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07004532 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
4533 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08004534 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004535 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02004536 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004537 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07004538 contentView.setTextViewText(R.id.header_text, null);
Selim Cinekafeed292017-12-12 17:32:44 -08004539 contentView.setViewVisibility(R.id.header_text_secondary, View.GONE);
4540 contentView.setTextViewText(R.id.header_text_secondary, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004541 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinekafeed292017-12-12 17:32:44 -08004542 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004543 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004544 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004545 contentView.setImageViewIcon(R.id.profile_badge, null);
4546 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Gus Prevasa3226492018-10-23 11:10:09 -04004547 contentView.setViewVisibility(R.id.alerted_icon, View.GONE);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004548 mN.mUsesStandardHeader = false;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004549 }
4550
Selim Cinek384804b2018-04-18 14:31:07 +08004551 private RemoteViews applyStandardTemplate(int resId, TemplateBindResult result) {
4552 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this),
4553 result);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004554 }
4555
Selim Cinek384804b2018-04-18 14:31:07 +08004556 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p,
4557 TemplateBindResult result) {
Kenny Guy77320062014-08-27 21:37:15 +01004558 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04004559
Christoph Studerfe718432014-09-01 18:21:18 +02004560 resetStandardTemplate(contentView);
4561
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004562 final Bundle ex = mN.extras;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004563 updateBackgroundColor(contentView, p);
4564 bindNotificationHeader(contentView, p);
Selim Cinek384804b2018-04-18 14:31:07 +08004565 bindLargeIconAndReply(contentView, p, result);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004566 boolean showProgress = handleProgressBar(contentView, ex, p);
Adrian Roos70d7aa32017-01-11 15:39:06 -08004567 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08004568 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004569 contentView.setTextViewText(R.id.title, processTextSpans(p.title));
Lucas Dupin00be88f2019-01-03 17:50:52 -08004570 setTextViewColorPrimary(contentView, R.id.title, p);
Selim Cinek954cc232016-05-20 13:29:23 -07004571 contentView.setViewLayoutWidth(R.id.title, showProgress
4572 ? ViewGroup.LayoutParams.WRAP_CONTENT
4573 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08004574 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08004575 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08004576 int textId = showProgress ? com.android.internal.R.id.text_line_1
4577 : com.android.internal.R.id.text;
Selim Cinek48f66b72017-08-18 16:17:51 -07004578 contentView.setTextViewText(textId, processTextSpans(p.text));
Lucas Dupin00be88f2019-01-03 17:50:52 -08004579 setTextViewColorSecondary(contentView, textId, p);
Selim Cinek41598732016-01-11 16:58:37 -08004580 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08004581 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08004582
Selim Cinek279fa862016-06-14 10:57:25 -07004583 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004584
Selim Cinek29603462015-11-17 19:04:39 -08004585 return contentView;
4586 }
4587
Selim Cinek48f66b72017-08-18 16:17:51 -07004588 private CharSequence processTextSpans(CharSequence text) {
Lucas Dupind3c99322018-09-27 10:22:29 -07004589 if (hasForegroundColor() || mInNightMode) {
Lucas Dupina291d192018-06-07 13:59:42 -07004590 return ContrastColorUtil.clearColorSpans(text);
Selim Cinek48f66b72017-08-18 16:17:51 -07004591 }
4592 return text;
4593 }
4594
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004595 private void setTextViewColorPrimary(RemoteViews contentView, int id,
4596 StandardTemplateParams p) {
4597 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004598 contentView.setTextColor(id, mPrimaryTextColor);
4599 }
4600
Selim Cinek48f66b72017-08-18 16:17:51 -07004601 private boolean hasForegroundColor() {
4602 return mForegroundColor != COLOR_INVALID;
4603 }
4604
Selim Cinek389edcd2017-05-11 19:16:44 -07004605 /**
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004606 * Return the primary text color using the existing template params
Selim Cinek389edcd2017-05-11 19:16:44 -07004607 * @hide
4608 */
4609 @VisibleForTesting
4610 public int getPrimaryTextColor() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004611 return getPrimaryTextColor(mParams);
4612 }
4613
4614 /**
4615 * @param p the template params to inflate this with
4616 * @return the primary text color
4617 * @hide
4618 */
4619 @VisibleForTesting
4620 public int getPrimaryTextColor(StandardTemplateParams p) {
4621 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004622 return mPrimaryTextColor;
4623 }
4624
Selim Cinek389edcd2017-05-11 19:16:44 -07004625 /**
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004626 * Return the secondary text color using the existing template params
Selim Cinek389edcd2017-05-11 19:16:44 -07004627 * @hide
4628 */
4629 @VisibleForTesting
4630 public int getSecondaryTextColor() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004631 return getSecondaryTextColor(mParams);
4632 }
4633
4634 /**
4635 * @param p the template params to inflate this with
4636 * @return the secondary text color
4637 * @hide
4638 */
4639 @VisibleForTesting
4640 public int getSecondaryTextColor(StandardTemplateParams p) {
4641 ensureColors(p);
Selim Cinek389edcd2017-05-11 19:16:44 -07004642 return mSecondaryTextColor;
4643 }
4644
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004645 private void setTextViewColorSecondary(RemoteViews contentView, int id,
4646 StandardTemplateParams p) {
4647 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004648 contentView.setTextColor(id, mSecondaryTextColor);
4649 }
4650
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004651 private void ensureColors(StandardTemplateParams p) {
4652 int backgroundColor = getBackgroundColor(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004653 if (mPrimaryTextColor == COLOR_INVALID
4654 || mSecondaryTextColor == COLOR_INVALID
Selim Cinek7b9605b2017-01-19 17:36:00 -08004655 || mTextColorsAreForBackground != backgroundColor) {
4656 mTextColorsAreForBackground = backgroundColor;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004657 if (!hasForegroundColor() || !isColorized(p)) {
Lucas Dupina291d192018-06-07 13:59:42 -07004658 mPrimaryTextColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004659 backgroundColor, mInNightMode);
Lucas Dupina291d192018-06-07 13:59:42 -07004660 mSecondaryTextColor = ContrastColorUtil.resolveSecondaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004661 backgroundColor, mInNightMode);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004662 if (backgroundColor != COLOR_DEFAULT && isColorized(p)) {
Lucas Dupina291d192018-06-07 13:59:42 -07004663 mPrimaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004664 mPrimaryTextColor, backgroundColor, 4.5);
Lucas Dupina291d192018-06-07 13:59:42 -07004665 mSecondaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004666 mSecondaryTextColor, backgroundColor, 4.5);
4667 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07004668 } else {
Lucas Dupina291d192018-06-07 13:59:42 -07004669 double backLum = ContrastColorUtil.calculateLuminance(backgroundColor);
4670 double textLum = ContrastColorUtil.calculateLuminance(mForegroundColor);
4671 double contrast = ContrastColorUtil.calculateContrast(mForegroundColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004672 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07004673 // We only respect the given colors if worst case Black or White still has
4674 // contrast
4675 boolean backgroundLight = backLum > textLum
4676 && satisfiesTextContrast(backgroundColor, Color.BLACK)
4677 || backLum <= textLum
4678 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004679 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07004680 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004681 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004682 mForegroundColor,
4683 backgroundColor,
4684 true /* findFG */,
4685 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004686 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004687 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004688 } else {
4689 mSecondaryTextColor =
Lucas Dupina291d192018-06-07 13:59:42 -07004690 ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004691 mForegroundColor,
4692 backgroundColor,
4693 true /* findFG */,
4694 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004695 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004696 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004697 }
4698 } else {
4699 mPrimaryTextColor = mForegroundColor;
Lucas Dupina291d192018-06-07 13:59:42 -07004700 mSecondaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004701 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4702 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Lucas Dupina291d192018-06-07 13:59:42 -07004703 if (ContrastColorUtil.calculateContrast(mSecondaryTextColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004704 backgroundColor) < 4.5f) {
4705 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07004706 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004707 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004708 mSecondaryTextColor,
4709 backgroundColor,
4710 true /* findFG */,
4711 4.5f);
4712 } else {
4713 mSecondaryTextColor
Lucas Dupina291d192018-06-07 13:59:42 -07004714 = ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004715 mSecondaryTextColor,
4716 backgroundColor,
4717 true /* findFG */,
4718 4.5f);
4719 }
Lucas Dupina291d192018-06-07 13:59:42 -07004720 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004721 mSecondaryTextColor, backgroundLight
4722 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4723 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004724 }
4725 }
4726 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004727 }
4728 }
4729
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004730 private void updateBackgroundColor(RemoteViews contentView,
4731 StandardTemplateParams p) {
4732 if (isColorized(p)) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004733 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004734 getBackgroundColor(p));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004735 } else {
4736 // Clear it!
4737 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4738 0);
4739 }
4740 }
4741
Selim Cinek860b6da2015-12-16 19:02:19 -08004742 /**
4743 * @param remoteView the remote view to update the minheight in
4744 * @param hasMinHeight does it have a mimHeight
4745 * @hide
4746 */
4747 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4748 int minHeight = 0;
4749 if (hasMinHeight) {
4750 // we need to set the minHeight of the notification
4751 minHeight = mContext.getResources().getDimensionPixelSize(
4752 com.android.internal.R.dimen.notification_min_content_height);
4753 }
4754 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4755 }
4756
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004757 private boolean handleProgressBar(RemoteViews contentView, Bundle ex,
4758 StandardTemplateParams p) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004759 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4760 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4761 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004762 if (p.hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004763 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004764 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004765 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004766 contentView.setProgressBackgroundTintList(
4767 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4768 R.color.notification_progress_background_color)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004769 if (getRawColor(p) != COLOR_DEFAULT) {
4770 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor(p));
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004771 contentView.setProgressTintList(R.id.progress, colorStateList);
4772 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004773 }
Selim Cinek29603462015-11-17 19:04:39 -08004774 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004775 } else {
4776 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004777 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004778 }
Joe Onorato561d3852010-11-20 18:09:34 -08004779 }
4780
Selim Cinek384804b2018-04-18 14:31:07 +08004781 private void bindLargeIconAndReply(RemoteViews contentView, StandardTemplateParams p,
4782 TemplateBindResult result) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004783 boolean largeIconShown = bindLargeIcon(contentView, p);
4784 boolean replyIconShown = bindReplyIcon(contentView, p);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004785 boolean iconContainerVisible = largeIconShown || replyIconShown;
Selim Cinek384804b2018-04-18 14:31:07 +08004786 contentView.setViewVisibility(R.id.right_icon_container,
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004787 iconContainerVisible ? View.VISIBLE : View.GONE);
Selim Cinek1c72fa02018-04-23 18:00:54 +08004788 int marginEnd = calculateMarginEnd(largeIconShown, replyIconShown);
4789 contentView.setViewLayoutMarginEnd(R.id.line1, marginEnd);
4790 contentView.setViewLayoutMarginEnd(R.id.text, marginEnd);
4791 contentView.setViewLayoutMarginEnd(R.id.progress, marginEnd);
Selim Cinek384804b2018-04-18 14:31:07 +08004792 if (result != null) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08004793 result.setIconMarginEnd(marginEnd);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004794 result.setRightIconContainerVisible(iconContainerVisible);
Selim Cinek384804b2018-04-18 14:31:07 +08004795 }
4796 }
4797
Selim Cinek1c72fa02018-04-23 18:00:54 +08004798 private int calculateMarginEnd(boolean largeIconShown, boolean replyIconShown) {
4799 int marginEnd = 0;
4800 int contentMargin = mContext.getResources().getDimensionPixelSize(
4801 R.dimen.notification_content_margin_end);
4802 int iconSize = mContext.getResources().getDimensionPixelSize(
4803 R.dimen.notification_right_icon_size);
4804 if (replyIconShown) {
4805 // The size of the reply icon
4806 marginEnd += iconSize;
4807
4808 int replyInset = mContext.getResources().getDimensionPixelSize(
4809 R.dimen.notification_reply_inset);
4810 // We're subtracting the inset of the reply icon to make sure it's
4811 // aligned nicely on the right, and remove it from the following padding
4812 marginEnd -= replyInset * 2;
4813 }
4814 if (largeIconShown) {
4815 // adding size of the right icon
4816 marginEnd += iconSize;
4817
4818 if (replyIconShown) {
4819 // We also add some padding to the reply icon if it's around
4820 marginEnd += contentMargin;
4821 }
4822 }
4823 if (replyIconShown || largeIconShown) {
4824 // The padding to the content
4825 marginEnd += contentMargin;
4826 }
4827 return marginEnd;
4828 }
4829
Selim Cinek384804b2018-04-18 14:31:07 +08004830 /**
4831 * Bind the large icon.
4832 * @return if the largeIcon is visible
4833 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004834 private boolean bindLargeIcon(RemoteViews contentView, StandardTemplateParams p) {
Selim Cinek279fa862016-06-14 10:57:25 -07004835 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4836 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4837 }
Lucas Dupin00be88f2019-01-03 17:50:52 -08004838 boolean showLargeIcon = mN.mLargeIcon != null && !p.hideLargeIcon;
Selim Cinek88188f22017-09-19 16:46:56 -07004839 if (showLargeIcon) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004840 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4841 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004842 processLargeLegacyIcon(mN.mLargeIcon, contentView, p);
Selim Cinek88188f22017-09-19 16:46:56 -07004843 }
Selim Cinek384804b2018-04-18 14:31:07 +08004844 return showLargeIcon;
4845 }
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004846
Selim Cinek384804b2018-04-18 14:31:07 +08004847 /**
4848 * Bind the reply icon.
4849 * @return if the reply icon is visible
4850 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004851 private boolean bindReplyIcon(RemoteViews contentView, StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08004852 boolean actionVisible = !p.hideReplyIcon;
Selim Cinek384804b2018-04-18 14:31:07 +08004853 Action action = null;
Selim Cinek88188f22017-09-19 16:46:56 -07004854 if (actionVisible) {
Selim Cinek384804b2018-04-18 14:31:07 +08004855 action = findReplyAction();
4856 actionVisible = action != null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004857 }
Selim Cinek384804b2018-04-18 14:31:07 +08004858 if (actionVisible) {
4859 contentView.setViewVisibility(R.id.reply_icon_action, View.VISIBLE);
4860 contentView.setDrawableTint(R.id.reply_icon_action,
4861 false /* targetBackground */,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004862 getNeutralColor(p),
Selim Cinek384804b2018-04-18 14:31:07 +08004863 PorterDuff.Mode.SRC_ATOP);
4864 contentView.setOnClickPendingIntent(R.id.reply_icon_action, action.actionIntent);
4865 contentView.setRemoteInputs(R.id.reply_icon_action, action.mRemoteInputs);
4866 } else {
4867 contentView.setRemoteInputs(R.id.reply_icon_action, null);
4868 }
4869 contentView.setViewVisibility(R.id.reply_icon_action,
4870 actionVisible ? View.VISIBLE : View.GONE);
4871 return actionVisible;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004872 }
4873
4874 private Action findReplyAction() {
4875 ArrayList<Action> actions = mActions;
4876 if (mOriginalActions != null) {
4877 actions = mOriginalActions;
4878 }
4879 int numActions = actions.size();
4880 for (int i = 0; i < numActions; i++) {
4881 Action action = actions.get(i);
4882 if (hasValidRemoteInput(action)) {
4883 return action;
4884 }
4885 }
4886 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004887 }
4888
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004889 private void bindNotificationHeader(RemoteViews contentView, StandardTemplateParams p) {
4890 bindSmallIcon(contentView, p);
4891 bindHeaderAppName(contentView, p);
Lucas Dupin00be88f2019-01-03 17:50:52 -08004892 bindHeaderText(contentView, p);
4893 bindHeaderTextSecondary(contentView, p);
4894 bindHeaderChronometerAndTime(contentView, p);
4895 bindProfileBadge(contentView, p);
4896 bindAlertedIcon(contentView, p);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004897 bindActivePermissions(contentView, p);
4898 bindExpandButton(contentView, p);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004899 mN.mUsesStandardHeader = true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004900 }
4901
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004902 private void bindActivePermissions(RemoteViews contentView, StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08004903 int color = getNeutralColor(p);
Julia Reynoldsb887b232018-04-10 16:38:08 -04004904 contentView.setDrawableTint(R.id.camera, false, color, PorterDuff.Mode.SRC_ATOP);
4905 contentView.setDrawableTint(R.id.mic, false, color, PorterDuff.Mode.SRC_ATOP);
4906 contentView.setDrawableTint(R.id.overlay, false, color, PorterDuff.Mode.SRC_ATOP);
4907 }
4908
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004909 private void bindExpandButton(RemoteViews contentView, StandardTemplateParams p) {
4910 int color = isColorized(p) ? getPrimaryTextColor(p) : getSecondaryTextColor(p);
Sunny Goyal5b153922017-09-21 21:00:36 -07004911 contentView.setDrawableTint(R.id.expand_button, false, color,
4912 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08004913 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08004914 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004915 }
4916
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004917 private void bindHeaderChronometerAndTime(RemoteViews contentView,
4918 StandardTemplateParams p) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004919 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08004920 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004921 setTextViewColorSecondary(contentView, R.id.time_divider, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004922 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
4923 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
4924 contentView.setLong(R.id.chronometer, "setBase",
4925 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
4926 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07004927 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07004928 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004929 setTextViewColorSecondary(contentView, R.id.chronometer, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004930 } else {
4931 contentView.setViewVisibility(R.id.time, View.VISIBLE);
4932 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004933 setTextViewColorSecondary(contentView, R.id.time, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004934 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004935 } else {
4936 // We still want a time to be set but gone, such that we can show and hide it
4937 // on demand in case it's a child notification without anything in the header
4938 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004939 }
4940 }
4941
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004942 private void bindHeaderText(RemoteViews contentView, StandardTemplateParams p) {
4943 CharSequence summaryText = p.summaryText;
4944 if (summaryText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05004945 && mStyle.hasSummaryInHeader()) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004946 summaryText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05004947 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004948 if (summaryText == null
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004949 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4950 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004951 summaryText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004952 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004953 if (summaryText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004954 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek48f66b72017-08-18 16:17:51 -07004955 contentView.setTextViewText(R.id.header_text, processTextSpans(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004956 processLegacyText(summaryText)));
4957 setTextViewColorSecondary(contentView, R.id.header_text, p);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004958 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
4959 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004960 setTextViewColorSecondary(contentView, R.id.header_text_divider, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004961 }
4962 }
4963
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004964 private void bindHeaderTextSecondary(RemoteViews contentView, StandardTemplateParams p) {
4965 if (!TextUtils.isEmpty(p.headerTextSecondary)) {
Selim Cinekafeed292017-12-12 17:32:44 -08004966 contentView.setTextViewText(R.id.header_text_secondary, processTextSpans(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004967 processLegacyText(p.headerTextSecondary)));
4968 setTextViewColorSecondary(contentView, R.id.header_text_secondary, p);
Selim Cinekafeed292017-12-12 17:32:44 -08004969 contentView.setViewVisibility(R.id.header_text_secondary, View.VISIBLE);
4970 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004971 setTextViewColorSecondary(contentView, R.id.header_text_secondary_divider, p);
Selim Cinekafeed292017-12-12 17:32:44 -08004972 }
4973 }
4974
Adrian Rooseba05822016-04-22 17:09:27 -07004975 /**
4976 * @hide
4977 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01004978 @UnsupportedAppUsage
Adrian Rooseba05822016-04-22 17:09:27 -07004979 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004980 CharSequence name = null;
4981 final PackageManager pm = mContext.getPackageManager();
4982 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
4983 // only system packages which lump together a bunch of unrelated stuff
4984 // may substitute a different name to make the purpose of the
4985 // notification more clear. the correct package label should always
4986 // be accessible via SystemUI.
4987 final String pkg = mContext.getPackageName();
4988 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
4989 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
4990 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
4991 name = subName;
4992 } else {
4993 Log.w(TAG, "warning: pkg "
4994 + pkg + " attempting to substitute app name '" + subName
4995 + "' without holding perm "
4996 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
4997 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004998 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04004999 if (TextUtils.isEmpty(name)) {
5000 name = pm.getApplicationLabel(mContext.getApplicationInfo());
5001 }
5002 if (TextUtils.isEmpty(name)) {
5003 // still nothing?
5004 return null;
5005 }
5006
5007 return String.valueOf(name);
5008 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005009 private void bindHeaderAppName(RemoteViews contentView, StandardTemplateParams p) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04005010 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005011 if (isColorized(p)) {
5012 setTextViewColorPrimary(contentView, R.id.app_name_text, p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005013 } else {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005014 contentView.setTextColor(R.id.app_name_text, getSecondaryTextColor(p));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005015 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005016 }
5017
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005018 private boolean isColorized(StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005019 return p.allowColorization && mN.isColorized();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005020 }
5021
5022 private void bindSmallIcon(RemoteViews contentView, StandardTemplateParams p) {
Selim Cinek279fa862016-06-14 10:57:25 -07005023 if (mN.mSmallIcon == null && mN.icon != 0) {
5024 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
5025 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005026 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Sunny Goyal5b153922017-09-21 21:00:36 -07005027 contentView.setInt(R.id.icon, "setImageLevel", mN.iconLevel);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005028 processSmallIconColor(mN.mSmallIcon, contentView, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005029 }
5030
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005031 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005032 * @return true if the built notification will show the time or the chronometer; false
5033 * otherwise
5034 */
5035 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07005036 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005037 }
5038
Christoph Studerfe718432014-09-01 18:21:18 +02005039 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07005040 // actions_container is only reset when there are no actions to avoid focus issues with
5041 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02005042 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02005043 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08005044
5045 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
5046 big.setTextViewText(R.id.notification_material_reply_text_1, null);
Kenny Guya0f6de82018-04-06 16:20:16 +01005047 big.setViewVisibility(R.id.notification_material_reply_text_1_container, View.GONE);
5048 big.setViewVisibility(R.id.notification_material_reply_progress, View.GONE);
Adrian Roose458aa82015-12-08 16:17:19 -08005049
5050 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
5051 big.setTextViewText(R.id.notification_material_reply_text_2, null);
5052 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
5053 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07005054
Selim Cineked64a142018-02-06 18:06:01 -08005055 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
5056 R.dimen.notification_content_margin);
Christoph Studerfe718432014-09-01 18:21:18 +02005057 }
5058
Selim Cinek384804b2018-04-18 14:31:07 +08005059 private RemoteViews applyStandardTemplateWithActions(int layoutId,
5060 TemplateBindResult result) {
5061 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this),
5062 result);
Adrian Roos48d746a2016-04-12 14:57:28 -07005063 }
5064
Gustav Sennton1463d832018-11-06 16:12:48 +00005065 private static List<Notification.Action> filterOutContextualActions(
5066 List<Notification.Action> actions) {
5067 List<Notification.Action> nonContextualActions = new ArrayList<>();
5068 for (Notification.Action action : actions) {
Gustav Sennton005d7a02019-01-04 13:41:32 +00005069 if (!action.isContextual()) {
Gustav Sennton1463d832018-11-06 16:12:48 +00005070 nonContextualActions.add(action);
5071 }
5072 }
5073 return nonContextualActions;
5074 }
5075
Adrian Roos70d7aa32017-01-11 15:39:06 -08005076 private RemoteViews applyStandardTemplateWithActions(int layoutId,
Selim Cinek384804b2018-04-18 14:31:07 +08005077 StandardTemplateParams p, TemplateBindResult result) {
5078 RemoteViews big = applyStandardTemplate(layoutId, p, result);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005079
Christoph Studerfe718432014-09-01 18:21:18 +02005080 resetStandardTemplateWithActions(big);
5081
Adrian Roose458aa82015-12-08 16:17:19 -08005082 boolean validRemoteInput = false;
5083
Gustav Sennton1463d832018-11-06 16:12:48 +00005084 // In the UI contextual actions appear separately from the standard actions, so we
5085 // filter them out here.
5086 List<Notification.Action> nonContextualActions = filterOutContextualActions(mActions);
5087
5088 int N = nonContextualActions.size();
Lucas Dupin00be88f2019-01-03 17:50:52 -08005089 boolean emphazisedMode = mN.fullScreenIntent != null;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005090 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005091 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08005092 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005093 big.setViewVisibility(R.id.actions, View.VISIBLE);
Selim Cineked64a142018-02-06 18:06:01 -08005094 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Daniel Sandler8680bf82012-05-15 16:52:52 -04005095 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005096 for (int i=0; i<N; i++) {
Gustav Sennton1463d832018-11-06 16:12:48 +00005097 Action action = nonContextualActions.get(i);
5098
Selim Cinekffcc7cf2018-02-06 17:43:51 -08005099 boolean actionHasValidInput = hasValidRemoteInput(action);
5100 validRemoteInput |= actionHasValidInput;
Adrian Roose458aa82015-12-08 16:17:19 -08005101
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005102 final RemoteViews button = generateActionButton(action, emphazisedMode, p);
Selim Cinek396caca2018-04-10 17:46:46 -07005103 if (actionHasValidInput && !emphazisedMode) {
Selim Cinekffcc7cf2018-02-06 17:43:51 -08005104 // Clear the drawable
5105 button.setInt(R.id.action0, "setBackgroundResource", 0);
5106 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005107 big.addView(R.id.actions, button);
5108 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07005109 } else {
5110 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005111 }
Adrian Roose458aa82015-12-08 16:17:19 -08005112
5113 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Lucas Dupin00be88f2019-01-03 17:50:52 -08005114 if (validRemoteInput && replyText != null
Selim Cinekbee4e072018-05-21 22:06:43 -07005115 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])
5116 && p.maxRemoteInputHistory > 0) {
Kenny Guya0f6de82018-04-06 16:20:16 +01005117 boolean showSpinner = mN.extras.getBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER);
Adrian Roose458aa82015-12-08 16:17:19 -08005118 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Kenny Guya0f6de82018-04-06 16:20:16 +01005119 big.setViewVisibility(R.id.notification_material_reply_text_1_container,
5120 View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005121 big.setTextViewText(R.id.notification_material_reply_text_1,
5122 processTextSpans(replyText[0]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005123 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1, p);
Kenny Guya0f6de82018-04-06 16:20:16 +01005124 big.setViewVisibility(R.id.notification_material_reply_progress,
5125 showSpinner ? View.VISIBLE : View.GONE);
5126 big.setProgressIndeterminateTintList(
5127 R.id.notification_material_reply_progress,
5128 ColorStateList.valueOf(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005129 isColorized(p) ? getPrimaryTextColor(p) : resolveContrastColor(p)));
Adrian Roose458aa82015-12-08 16:17:19 -08005130
Selim Cinekbee4e072018-05-21 22:06:43 -07005131 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])
5132 && p.maxRemoteInputHistory > 1) {
Adrian Roose458aa82015-12-08 16:17:19 -08005133 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005134 big.setTextViewText(R.id.notification_material_reply_text_2,
5135 processTextSpans(replyText[1]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005136 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2, p);
Adrian Roose458aa82015-12-08 16:17:19 -08005137
Selim Cinekbee4e072018-05-21 22:06:43 -07005138 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])
5139 && p.maxRemoteInputHistory > 2) {
Adrian Roose458aa82015-12-08 16:17:19 -08005140 big.setViewVisibility(
5141 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005142 big.setTextViewText(R.id.notification_material_reply_text_3,
5143 processTextSpans(replyText[2]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005144 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3, p);
Adrian Roose458aa82015-12-08 16:17:19 -08005145 }
5146 }
5147 }
5148
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005149 return big;
5150 }
5151
Adrian Roose458aa82015-12-08 16:17:19 -08005152 private boolean hasValidRemoteInput(Action action) {
5153 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
5154 // Weird actions
5155 return false;
5156 }
5157
5158 RemoteInput[] remoteInputs = action.getRemoteInputs();
5159 if (remoteInputs == null) {
5160 return false;
5161 }
5162
5163 for (RemoteInput r : remoteInputs) {
5164 CharSequence[] choices = r.getChoices();
5165 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
5166 return true;
5167 }
5168 }
5169 return false;
5170 }
5171
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005172 /**
5173 * Construct a RemoteViews for the final 1U notification layout. In order:
5174 * 1. Custom contentView from the caller
5175 * 2. Style's proposed content view
5176 * 3. Standard template view
5177 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005178 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005179 return createContentView(false /* increasedheight */ );
5180 }
5181
5182 /**
5183 * Construct a RemoteViews for the smaller content view.
5184 *
5185 * @param increasedHeight true if this layout be created with an increased height. Some
5186 * styles may support showing more then just that basic 1U size
5187 * and the system may decide to render important notifications
5188 * slightly bigger even when collapsed.
5189 *
5190 * @hide
5191 */
5192 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005193 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005194 return mN.contentView;
5195 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005196 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005197 if (styleView != null) {
5198 return styleView;
5199 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005200 }
Selim Cinek384804b2018-04-18 14:31:07 +08005201 return applyStandardTemplate(getBaseLayoutResource(), null /* result */);
Joe Onorato46439ce2010-11-19 13:56:21 -08005202 }
5203
Selim Cineka7679b62017-05-10 16:33:25 -07005204 private boolean useExistingRemoteView() {
5205 return mStyle == null || (!mStyle.displayCustomViewInline()
5206 && !mRebuildStyledRemoteViews);
5207 }
5208
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005209 /**
5210 * Construct a RemoteViews for the final big notification layout.
5211 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005212 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05005213 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07005214 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005215 return mN.bigContentView;
5216 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05005217 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08005218 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005219 } else if (mActions.size() != 0) {
Selim Cinek384804b2018-04-18 14:31:07 +08005220 result = applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5221 null /* result */);
Selim Cinek850a8542015-11-11 11:48:36 -05005222 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08005223 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05005224 return result;
5225 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005226
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005227 /**
Selim Cinek414ad332017-02-24 19:06:12 -08005228 * Construct a RemoteViews for the final notification header only. This will not be
5229 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005230 *
5231 * @hide
5232 */
Lucas Dupin00be88f2019-01-03 17:50:52 -08005233 public RemoteViews makeNotificationHeader() {
5234 return makeNotificationHeader(mParams.reset().fillTextsFrom(this));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005235 }
5236
5237 /**
5238 * Construct a RemoteViews for the final notification header only. This will not be
5239 * colorized.
5240 *
5241 * @param p the template params to inflate this with
5242 */
5243 private RemoteViews makeNotificationHeader(StandardTemplateParams p) {
5244 // Headers on their own are never colorized
5245 p.disallowColorization();
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005246 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Lucas Dupin00be88f2019-01-03 17:50:52 -08005247 R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005248 resetNotificationHeader(header);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005249 bindNotificationHeader(header, p);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005250 return header;
5251 }
5252
Adrian Roos487374f2017-01-11 15:48:14 -08005253 /**
5254 * Construct a RemoteViews for the ambient version of the notification.
5255 *
5256 * @hide
5257 */
5258 public RemoteViews makeAmbientNotification() {
Selim Cinek0e069942019-01-24 16:09:41 -08005259 RemoteViews headsUpContentView = createHeadsUpContentView(false /* increasedHeight */);
5260 if (headsUpContentView != null) {
5261 return headsUpContentView;
5262 }
5263 return createContentView();
Adrian Roos487374f2017-01-11 15:48:14 -08005264 }
5265
Selim Cinek29603462015-11-17 19:04:39 -08005266 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005267 if (result != null) {
5268 result.setViewVisibility(R.id.text_line_1, View.GONE);
5269 }
Selim Cinek29603462015-11-17 19:04:39 -08005270 }
5271
Selim Cinek6743c0b2017-01-18 18:24:01 -08005272 /**
5273 * Adapt the Notification header if this view is used as an expanded view.
5274 *
5275 * @hide
5276 */
5277 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005278 if (result != null) {
5279 result.setBoolean(R.id.notification_header, "setExpanded", true);
5280 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005281 }
5282
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005283 /**
5284 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005285 *
5286 * @param increasedHeight true if this layout be created with an increased height. Some
5287 * styles may support showing more then just that basic 1U size
5288 * and the system may decide to render important notifications
5289 * slightly bigger even when collapsed.
5290 *
5291 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005292 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005293 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005294 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005295 return mN.headsUpContentView;
5296 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08005297 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
5298 if (styleView != null) {
5299 return styleView;
5300 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05005301 } else if (mActions.size() == 0) {
5302 return null;
5303 }
5304
Selim Cinekbee4e072018-05-21 22:06:43 -07005305 // We only want at most a single remote input history to be shown here, otherwise
5306 // the content would become squished.
5307 StandardTemplateParams p = mParams.reset().fillTextsFrom(this)
5308 .setMaxRemoteInputHistory(1);
5309 return applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5310 p,
5311 null /* result */);
Chris Wren8fd39ec2014-02-27 17:43:26 -05005312 }
5313
Selim Cinek624c02db2015-12-14 21:00:02 -08005314 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08005315 * Construct a RemoteViews for the final heads-up notification layout.
5316 */
5317 public RemoteViews createHeadsUpContentView() {
5318 return createHeadsUpContentView(false /* useIncreasedHeight */);
5319 }
5320
5321 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08005322 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5323 *
5324 * @hide
5325 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005326 @UnsupportedAppUsage
Selim Cinek624c02db2015-12-14 21:00:02 -08005327 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005328 return makePublicView(false /* ambient */);
5329 }
5330
5331 /**
5332 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5333 *
5334 * @hide
5335 */
5336 public RemoteViews makePublicAmbientNotification() {
5337 return makePublicView(true /* ambient */);
5338 }
5339
5340 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08005341 if (mN.publicVersion != null) {
5342 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005343 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08005344 }
5345 Bundle savedBundle = mN.extras;
5346 Style style = mStyle;
5347 mStyle = null;
5348 Icon largeIcon = mN.mLargeIcon;
5349 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07005350 Bitmap largeIconLegacy = mN.largeIcon;
5351 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005352 ArrayList<Action> actions = mActions;
5353 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08005354 Bundle publicExtras = new Bundle();
5355 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
5356 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
5357 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
5358 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07005359 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
5360 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cineked07b962018-04-30 14:39:35 -07005361 String appName = savedBundle.getString(EXTRA_SUBSTITUTE_APP_NAME);
5362 if (appName != null) {
5363 publicExtras.putString(EXTRA_SUBSTITUTE_APP_NAME, appName);
5364 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005365 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07005366 RemoteViews view;
Lucas Dupin00be88f2019-01-03 17:50:52 -08005367 view = makeNotificationHeader();
5368 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
Selim Cinek624c02db2015-12-14 21:00:02 -08005369 mN.extras = savedBundle;
5370 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07005371 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005372 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08005373 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005374 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08005375 }
5376
Selim Cinek6743c0b2017-01-18 18:24:01 -08005377 /**
5378 * Construct a content view for the display when low - priority
5379 *
5380 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
5381 * a new subtext is created consisting of the content of the
5382 * notification.
5383 * @hide
5384 */
5385 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005386 StandardTemplateParams p = mParams.reset()
5387 .forceDefaultColor()
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005388 .fillTextsFrom(this);
5389 if (!useRegularSubtext || TextUtils.isEmpty(mParams.summaryText)) {
5390 p.summaryText(createSummaryText());
Selim Cinek6743c0b2017-01-18 18:24:01 -08005391 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005392 RemoteViews header = makeNotificationHeader(p);
Selim Cinek1b554392017-02-28 17:22:49 -08005393 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08005394 return header;
5395 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005396
Selim Cinek6743c0b2017-01-18 18:24:01 -08005397 private CharSequence createSummaryText() {
5398 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
5399 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
5400 return titleText;
5401 }
5402 SpannableStringBuilder summary = new SpannableStringBuilder();
5403 if (titleText == null) {
5404 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
5405 }
5406 BidiFormatter bidi = BidiFormatter.getInstance();
5407 if (titleText != null) {
5408 summary.append(bidi.unicodeWrap(titleText));
5409 }
5410 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
5411 if (titleText != null && contentText != null) {
5412 summary.append(bidi.unicodeWrap(mContext.getText(
5413 R.string.notification_header_divider_symbol_with_spaces)));
5414 }
5415 if (contentText != null) {
5416 summary.append(bidi.unicodeWrap(contentText));
5417 }
5418 return summary;
5419 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05005420
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005421 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005422 StandardTemplateParams p) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04005423 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07005424 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005425 emphazisedMode ? getEmphasizedActionLayoutResource()
5426 : tombstone ? getActionTombstoneLayoutResource()
5427 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04005428 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04005429 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04005430 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005431 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005432 if (action.mRemoteInputs != null) {
5433 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
5434 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005435 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07005436 // change the background bgColor
Selim Cinek981962e2016-07-20 20:41:58 -07005437 CharSequence title = action.title;
5438 ColorStateList[] outResultColor = null;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005439 int background = resolveBackgroundColor(p);
Selim Cinek981962e2016-07-20 20:41:58 -07005440 if (isLegacy()) {
Lucas Dupina291d192018-06-07 13:59:42 -07005441 title = ContrastColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07005442 } else {
5443 outResultColor = new ColorStateList[1];
Selim Cinek396caca2018-04-10 17:46:46 -07005444 title = ensureColorSpanContrast(title, background, outResultColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005445 }
Selim Cinek48f66b72017-08-18 16:17:51 -07005446 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005447 setTextViewColorPrimary(button, R.id.action0, p);
Selim Cinek396caca2018-04-10 17:46:46 -07005448 int rippleColor;
5449 boolean hasColorOverride = outResultColor != null && outResultColor[0] != null;
5450 if (hasColorOverride) {
5451 // There's a span spanning the full text, let's take it and use it as the
5452 // background color
5453 background = outResultColor[0].getDefaultColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005454 int textColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07005455 background, mInNightMode);
Selim Cinek396caca2018-04-10 17:46:46 -07005456 button.setTextColor(R.id.action0, textColor);
5457 rippleColor = textColor;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005458 } else if (getRawColor(p) != COLOR_DEFAULT && !isColorized(p)
5459 && mTintActionButtons) {
5460 rippleColor = resolveContrastColor(p);
Selim Cinek396caca2018-04-10 17:46:46 -07005461 button.setTextColor(R.id.action0, rippleColor);
5462 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005463 rippleColor = getPrimaryTextColor(p);
Selim Cinek981962e2016-07-20 20:41:58 -07005464 }
Selim Cinek396caca2018-04-10 17:46:46 -07005465 // We only want about 20% alpha for the ripple
5466 rippleColor = (rippleColor & 0x00ffffff) | 0x33000000;
5467 button.setColorStateList(R.id.action0, "setRippleColor",
5468 ColorStateList.valueOf(rippleColor));
5469 button.setColorStateList(R.id.action0, "setButtonBackground",
5470 ColorStateList.valueOf(background));
5471 button.setBoolean(R.id.action0, "setHasStroke", !hasColorOverride);
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005472 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07005473 button.setTextViewText(R.id.action0, processTextSpans(
5474 processLegacyText(action.title)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005475 if (isColorized(p)) {
5476 setTextViewColorPrimary(button, R.id.action0, p);
5477 } else if (getRawColor(p) != COLOR_DEFAULT && mTintActionButtons) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005478 button.setTextColor(R.id.action0, resolveContrastColor(p));
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005479 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005480 }
Tony Mak7d4b3a52018-11-27 17:29:36 +00005481 button.setIntTag(R.id.action0, R.id.notification_action_index_tag,
5482 mActions.indexOf(action));
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005483 return button;
5484 }
5485
Joe Onoratocb109a02011-01-18 17:57:41 -08005486 /**
Selim Cinek981962e2016-07-20 20:41:58 -07005487 * Ensures contrast on color spans against a background color. also returns the color of the
5488 * text if a span was found that spans over the whole text.
5489 *
5490 * @param charSequence the charSequence on which the spans are
5491 * @param background the background color to ensure the contrast against
5492 * @param outResultColor an array in which a color will be returned as the first element if
5493 * there exists a full length color span.
5494 * @return the contrasted charSequence
5495 */
5496 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
5497 ColorStateList[] outResultColor) {
5498 if (charSequence instanceof Spanned) {
5499 Spanned ss = (Spanned) charSequence;
5500 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
5501 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
5502 for (Object span : spans) {
5503 Object resultSpan = span;
5504 int spanStart = ss.getSpanStart(span);
5505 int spanEnd = ss.getSpanEnd(span);
5506 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
5507 if (resultSpan instanceof CharacterStyle) {
5508 resultSpan = ((CharacterStyle) span).getUnderlying();
5509 }
5510 if (resultSpan instanceof TextAppearanceSpan) {
5511 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
5512 ColorStateList textColor = originalSpan.getTextColor();
5513 if (textColor != null) {
5514 int[] colors = textColor.getColors();
5515 int[] newColors = new int[colors.length];
5516 for (int i = 0; i < newColors.length; i++) {
Lucas Dupina291d192018-06-07 13:59:42 -07005517 newColors[i] = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005518 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005519 }
5520 textColor = new ColorStateList(textColor.getStates().clone(),
5521 newColors);
Selim Cinek396caca2018-04-10 17:46:46 -07005522 if (fullLength) {
5523 outResultColor[0] = textColor;
5524 // Let's drop the color from the span
5525 textColor = null;
5526 }
Selim Cinek981962e2016-07-20 20:41:58 -07005527 resultSpan = new TextAppearanceSpan(
5528 originalSpan.getFamily(),
5529 originalSpan.getTextStyle(),
5530 originalSpan.getTextSize(),
5531 textColor,
5532 originalSpan.getLinkTextColor());
Selim Cinek981962e2016-07-20 20:41:58 -07005533 }
5534 } else if (resultSpan instanceof ForegroundColorSpan) {
5535 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
5536 int foregroundColor = originalSpan.getForegroundColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005537 foregroundColor = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005538 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005539 if (fullLength) {
5540 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
Selim Cinek396caca2018-04-10 17:46:46 -07005541 resultSpan = null;
5542 } else {
5543 resultSpan = new ForegroundColorSpan(foregroundColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005544 }
5545 } else {
5546 resultSpan = span;
5547 }
Selim Cinek396caca2018-04-10 17:46:46 -07005548 if (resultSpan != null) {
5549 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
5550 }
Selim Cinek981962e2016-07-20 20:41:58 -07005551 }
5552 return builder;
5553 }
5554 return charSequence;
5555 }
5556
5557 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005558 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07005559 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005560 */
5561 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005562 if (!mIsLegacyInitialized) {
5563 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
5564 < Build.VERSION_CODES.LOLLIPOP;
5565 mIsLegacyInitialized = true;
5566 }
5567 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005568 }
5569
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005570 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08005571 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
Lucas Dupin00be88f2019-01-03 17:50:52 -08005572 if (isAlreadyLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005573 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005574 } else {
5575 return charSequence;
5576 }
5577 }
5578
Dan Sandler26e81cf2014-05-06 10:01:27 -04005579 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005580 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04005581 */
Adrian Roos487374f2017-01-11 15:48:14 -08005582 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005583 StandardTemplateParams p) {
Selim Cinekea4bef72015-12-02 15:51:10 -08005584 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005585 int color;
Lucas Dupin00be88f2019-01-03 17:50:52 -08005586 if (isColorized(p)) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005587 color = getPrimaryTextColor(p);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005588 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005589 color = resolveContrastColor(p);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005590 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005591 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07005592 contentView.setDrawableTint(R.id.icon, false, color,
5593 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005594
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005595 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005596 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08005597 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005598 }
5599
Dan Sandler26e81cf2014-05-06 10:01:27 -04005600 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005601 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04005602 * if it's grayscale).
5603 */
5604 // TODO: also check bounds, transparency, that sort of thing.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005605 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView,
5606 StandardTemplateParams p) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04005607 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005608 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005609 // resolve color will fall back to the default when legacy
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005610 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(p),
Sunny Goyal5b153922017-09-21 21:00:36 -07005611 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01005612 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005613 }
5614
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05005615 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005616 if (mN.color != COLOR_DEFAULT) {
5617 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02005618 }
Jorim Jaggi74419312014-06-10 20:57:21 +02005619 }
5620
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005621 int resolveContrastColor(StandardTemplateParams p) {
5622 int rawColor = getRawColor(p);
5623 if (mCachedContrastColorIsFor == rawColor && mCachedContrastColor != COLOR_INVALID) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08005624 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005625 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005626
Selim Cinekac5f0272017-05-02 16:05:41 -07005627 int color;
Selim Cinekc7f5a822018-03-20 19:32:06 -07005628 int background = mContext.getColor(
5629 com.android.internal.R.color.notification_material_background_color);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005630 if (rawColor == COLOR_DEFAULT) {
5631 ensureColors(p);
Lucas Dupinf03e7522018-06-25 16:21:13 -07005632 color = ContrastColorUtil.resolveDefaultColor(mContext, background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005633 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005634 color = ContrastColorUtil.resolveContrastColor(mContext, rawColor,
Anthony Chenad4d1582017-04-10 16:07:58 -07005635 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005636 }
5637 if (Color.alpha(color) < 255) {
5638 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005639 color = ContrastColorUtil.compositeColors(color, background);
Selim Cinekac5f0272017-05-02 16:05:41 -07005640 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005641 mCachedContrastColorIsFor = rawColor;
Selim Cinekac5f0272017-05-02 16:05:41 -07005642 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005643 }
5644
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005645 /**
5646 * Return the raw color of this Notification, which doesn't necessarily satisfy contrast.
5647 *
5648 * @see #resolveContrastColor(StandardTemplateParams) for the contrasted color
5649 * @param p the template params to inflate this with
5650 */
5651 private int getRawColor(StandardTemplateParams p) {
5652 if (p.forceDefaultColor) {
5653 return COLOR_DEFAULT;
5654 }
5655 return mN.color;
5656 }
5657
Selim Cinek4717d862018-04-19 09:19:15 +08005658 int resolveNeutralColor() {
5659 if (mNeutralColor != COLOR_INVALID) {
5660 return mNeutralColor;
5661 }
5662 int background = mContext.getColor(
5663 com.android.internal.R.color.notification_material_background_color);
Lucas Dupinf03e7522018-06-25 16:21:13 -07005664 mNeutralColor = ContrastColorUtil.resolveDefaultColor(mContext, background,
5665 mInNightMode);
Selim Cinek4717d862018-04-19 09:19:15 +08005666 if (Color.alpha(mNeutralColor) < 255) {
5667 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005668 mNeutralColor = ContrastColorUtil.compositeColors(mNeutralColor, background);
Selim Cinek4717d862018-04-19 09:19:15 +08005669 }
5670 return mNeutralColor;
5671 }
5672
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005673 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005674 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005675 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08005676 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005677 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005678 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005679 mN.actions = new Action[mActions.size()];
5680 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005681 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005682 if (!mPersonList.isEmpty()) {
Selim Cineke7238dd2017-12-14 17:48:32 -08005683 mN.extras.putParcelableArrayList(EXTRA_PEOPLE_LIST, mPersonList);
Dan Sandler0bf2ed82013-12-21 23:33:41 -06005684 }
Selim Cinek247fa012016-02-18 09:50:48 -08005685 if (mN.bigContentView != null || mN.contentView != null
5686 || mN.headsUpContentView != null) {
5687 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
5688 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005689 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08005690 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005691
Julia Reynolds3b848122016-02-26 10:45:32 -05005692 /**
5693 * Creates a Builder from an existing notification so further changes can be made.
5694 * @param context The context for your application / activity.
5695 * @param n The notification to create a Builder from.
5696 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005697 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005698 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005699 ApplicationInfo applicationInfo = n.extras.getParcelable(
5700 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005701 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05005702 if (applicationInfo != null) {
5703 try {
5704 builderContext = context.createApplicationContext(applicationInfo,
5705 Context.CONTEXT_RESTRICTED);
5706 } catch (NameNotFoundException e) {
5707 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
5708 builderContext = context; // try with our context
5709 }
5710 } else {
5711 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02005712 }
5713
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005714 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005715 }
5716
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005717 /**
Gustav Sennton761884c2018-11-19 17:40:19 +00005718 * Determines whether the platform can generate contextual actions for a notification.
5719 * By default this is true.
5720 */
5721 public Builder setAllowSystemGeneratedContextualActions(boolean allowed) {
5722 mN.mAllowSystemGeneratedContextualActions = allowed;
5723 return this;
5724 }
5725
5726 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005727 * @deprecated Use {@link #build()} instead.
5728 */
5729 @Deprecated
5730 public Notification getNotification() {
5731 return build();
5732 }
5733
5734 /**
5735 * Combine all of the options that have been set and return a new {@link Notification}
5736 * object.
5737 */
5738 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005739 // first, add any extras from the calling code
5740 if (mUserExtras != null) {
5741 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005742 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005743
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005744 mN.creationTime = System.currentTimeMillis();
5745
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005746 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05005747 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02005748
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005749 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005750
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005751 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02005752 mStyle.reduceImageSizes(mContext);
5753 mStyle.purgeResources();
Selim Cinek90343862018-02-01 11:07:11 -08005754 mStyle.validate(mContext);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005755 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005756 }
Selim Cinekd0426622017-07-11 13:19:59 +02005757 mN.reduceImageSizes(mContext);
5758
Adrian Roos5081c0d2016-02-26 16:04:19 -08005759 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07005760 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005761 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005762 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005763 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
5764 mN.contentView.getSequenceNumber());
5765 }
5766 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005767 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005768 if (mN.bigContentView != null) {
5769 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
5770 mN.bigContentView.getSequenceNumber());
5771 }
5772 }
5773 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005774 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005775 if (mN.headsUpContentView != null) {
5776 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
5777 mN.headsUpContentView.getSequenceNumber());
5778 }
5779 }
5780 }
5781
Julia Reynolds4c0c2022016-02-02 15:11:59 -05005782 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
5783 mN.flags |= FLAG_SHOW_LIGHTS;
5784 }
5785
Adrian Roosfb921842017-10-26 14:49:56 +02005786 mN.allPendingIntents = null;
5787
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005788 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005789 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005790
5791 /**
5792 * Apply this Builder to an existing {@link Notification} object.
5793 *
5794 * @hide
5795 */
5796 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005797 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005798 return n;
5799 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005800
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005801 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005802 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005803 * change. Also removes extenders on low ram devices, as
5804 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005805 *
5806 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5807 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005808 * @hide
5809 */
Kristian Monsen05f34792018-04-09 10:27:16 +02005810 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam,
5811 Context context) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005812 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005813
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005814 // Only strip views for known Styles because we won't know how to
5815 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005816 if (!isLowRam
5817 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005818 && getNotificationStyleClass(templateClass) == null) {
5819 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005820 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005821
5822 // Only strip unmodified BuilderRemoteViews.
5823 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005824 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005825 n.contentView.getSequenceNumber();
5826 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005827 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005828 n.bigContentView.getSequenceNumber();
5829 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005830 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005831 n.headsUpContentView.getSequenceNumber();
5832
5833 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005834 if (!isLowRam
5835 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005836 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005837 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005838
5839 Notification clone = n.clone();
5840 if (stripContentView) {
5841 clone.contentView = null;
5842 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5843 }
5844 if (stripBigContentView) {
5845 clone.bigContentView = null;
5846 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5847 }
5848 if (stripHeadsUpContentView) {
5849 clone.headsUpContentView = null;
5850 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5851 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005852 if (isLowRam) {
Kristian Monsen05f34792018-04-09 10:27:16 +02005853 String[] allowedServices = context.getResources().getStringArray(
5854 R.array.config_allowedManagedServicesOnLowRamDevices);
5855 if (allowedServices.length == 0) {
5856 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5857 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5858 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5859 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005860 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005861 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005862 }
5863
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005864 @UnsupportedAppUsage
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005865 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005866 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005867 }
5868
5869 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005870 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005871 }
5872
5873 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005874 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005875 }
5876
5877 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005878 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005879 }
5880
5881 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005882 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005883 }
5884
Adrian Roosc1a80b02016-04-05 14:54:55 -07005885 private int getMessagingLayoutResource() {
5886 return R.layout.notification_template_material_messaging;
5887 }
5888
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005889 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005890 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005891 }
5892
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005893 private int getEmphasizedActionLayoutResource() {
5894 return R.layout.notification_material_action_emphasized;
5895 }
5896
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005897 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005898 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005899 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005900
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005901 private int getBackgroundColor(StandardTemplateParams p) {
5902 if (isColorized(p)) {
5903 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : getRawColor(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005904 } else {
Selim Cinekc7f5a822018-03-20 19:32:06 -07005905 return COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005906 }
5907 }
5908
Selim Cinek396caca2018-04-10 17:46:46 -07005909 /**
Selim Cinek4717d862018-04-19 09:19:15 +08005910 * Gets a neutral color that can be used for icons or similar that should not stand out.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005911 * @param p the template params to inflate this with
Selim Cinek4717d862018-04-19 09:19:15 +08005912 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005913 private int getNeutralColor(StandardTemplateParams p) {
5914 if (isColorized(p)) {
5915 return getSecondaryTextColor(p);
Selim Cinek4717d862018-04-19 09:19:15 +08005916 } else {
5917 return resolveNeutralColor();
5918 }
5919 }
5920
5921 /**
Selim Cinek396caca2018-04-10 17:46:46 -07005922 * Same as getBackgroundColor but also resolved the default color to the background.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005923 * @param p the template params to inflate this with
Selim Cinek396caca2018-04-10 17:46:46 -07005924 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005925 private int resolveBackgroundColor(StandardTemplateParams p) {
5926 int backgroundColor = getBackgroundColor(p);
Selim Cinek396caca2018-04-10 17:46:46 -07005927 if (backgroundColor == COLOR_DEFAULT) {
5928 backgroundColor = mContext.getColor(
5929 com.android.internal.R.color.notification_material_background_color);
5930 }
5931 return backgroundColor;
5932 }
5933
Anthony Chenad4d1582017-04-10 16:07:58 -07005934 private boolean shouldTintActionButtons() {
5935 return mTintActionButtons;
5936 }
5937
Selim Cinek99104832017-01-25 14:47:33 -08005938 private boolean textColorsNeedInversion() {
5939 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
5940 return false;
5941 }
5942 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
5943 return targetSdkVersion > Build.VERSION_CODES.M
5944 && targetSdkVersion < Build.VERSION_CODES.O;
5945 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005946
5947 /**
5948 * Set a color palette to be used as the background and textColors
5949 *
5950 * @param backgroundColor the color to be used as the background
5951 * @param foregroundColor the color to be used as the foreground
5952 *
5953 * @hide
5954 */
5955 public void setColorPalette(int backgroundColor, int foregroundColor) {
5956 mBackgroundColor = backgroundColor;
5957 mForegroundColor = foregroundColor;
5958 mTextColorsAreForBackground = COLOR_INVALID;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005959 ensureColors(mParams.reset().fillTextsFrom(this));
Selim Cinek5fb73f82017-04-20 16:55:38 -07005960 }
Selim Cinekac5f0272017-05-02 16:05:41 -07005961
5962 /**
Selim Cineka7679b62017-05-10 16:33:25 -07005963 * Forces all styled remoteViews to be built from scratch and not use any cached
5964 * RemoteViews.
5965 * This is needed for legacy apps that are baking in their remoteviews into the
5966 * notification.
5967 *
5968 * @hide
5969 */
5970 public void setRebuildStyledRemoteViews(boolean rebuild) {
5971 mRebuildStyledRemoteViews = rebuild;
5972 }
Selim Cinekaa9db1f2018-02-27 17:35:47 -08005973
5974 /**
5975 * Get the text that should be displayed in the statusBar when heads upped. This is
5976 * usually just the app name, but may be different depending on the style.
5977 *
5978 * @param publicMode If true, return a text that is safe to display in public.
5979 *
5980 * @hide
5981 */
5982 public CharSequence getHeadsUpStatusBarText(boolean publicMode) {
5983 if (mStyle != null && !publicMode) {
5984 CharSequence text = mStyle.getHeadsUpStatusBarText();
5985 if (!TextUtils.isEmpty(text)) {
5986 return text;
5987 }
5988 }
5989 return loadHeaderAppName();
5990 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005991 }
5992
5993 /**
Selim Cinekd0426622017-07-11 13:19:59 +02005994 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
5995 * remote views.
5996 *
5997 * @hide
5998 */
5999 void reduceImageSizes(Context context) {
6000 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
6001 return;
6002 }
Selim Cineka8cb1262017-08-15 16:53:44 -07006003 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006004 if (mLargeIcon != null || largeIcon != null) {
6005 Resources resources = context.getResources();
6006 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07006007 int maxWidth = resources.getDimensionPixelSize(isLowRam
6008 ? R.dimen.notification_right_icon_size_low_ram
6009 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006010 int maxHeight = maxWidth;
6011 if (MediaStyle.class.equals(style)
6012 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006013 maxHeight = resources.getDimensionPixelSize(isLowRam
6014 ? R.dimen.notification_media_image_max_height_low_ram
6015 : R.dimen.notification_media_image_max_height);
6016 maxWidth = resources.getDimensionPixelSize(isLowRam
6017 ? R.dimen.notification_media_image_max_width_low_ram
6018 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006019 }
6020 if (mLargeIcon != null) {
6021 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
6022 }
6023 if (largeIcon != null) {
6024 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
6025 }
6026 }
Selim Cineka8cb1262017-08-15 16:53:44 -07006027 reduceImageSizesForRemoteView(contentView, context, isLowRam);
6028 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
6029 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02006030 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
6031 }
6032
Selim Cineka8cb1262017-08-15 16:53:44 -07006033 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
6034 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02006035 if (remoteView != null) {
6036 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006037 int maxWidth = resources.getDimensionPixelSize(isLowRam
6038 ? R.dimen.notification_custom_view_max_image_width_low_ram
6039 : R.dimen.notification_custom_view_max_image_width);
6040 int maxHeight = resources.getDimensionPixelSize(isLowRam
6041 ? R.dimen.notification_custom_view_max_image_height_low_ram
6042 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02006043 remoteView.reduceImageSizes(maxWidth, maxHeight);
6044 }
6045 }
6046
6047 /**
Selim Cinek22714f12017-04-13 16:23:53 -07006048 * @return whether this notification is a foreground service notification
Gus Prevasb0c1a462018-11-05 11:06:15 -05006049 * @hide
Selim Cinek7b9605b2017-01-19 17:36:00 -08006050 */
Gus Prevasb0c1a462018-11-05 11:06:15 -05006051 public boolean isForegroundService() {
Selim Cinek22714f12017-04-13 16:23:53 -07006052 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08006053 }
6054
6055 /**
Selim Cinek99104832017-01-25 14:47:33 -08006056 * @return whether this notification has a media session attached
6057 * @hide
6058 */
6059 public boolean hasMediaSession() {
6060 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
6061 }
6062
6063 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006064 * @return the style class of this notification
6065 * @hide
6066 */
6067 public Class<? extends Notification.Style> getNotificationStyle() {
6068 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
6069
6070 if (!TextUtils.isEmpty(templateClass)) {
6071 return Notification.getNotificationStyleClass(templateClass);
6072 }
6073 return null;
6074 }
6075
6076 /**
Selim Cinek22714f12017-04-13 16:23:53 -07006077 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08006078 *
6079 * @hide
6080 */
6081 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07006082 if (isColorizedMedia()) {
6083 return true;
6084 }
Julia Reynolds4db59552017-06-30 13:34:01 -04006085 return extras.getBoolean(EXTRA_COLORIZED)
6086 && (hasColorizedPermission() || isForegroundService());
6087 }
6088
6089 /**
6090 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
6091 * permission. The permission is checked when a notification is enqueued.
6092 */
6093 private boolean hasColorizedPermission() {
6094 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07006095 }
6096
6097 /**
6098 * @return true if this notification is colorized and it is a media notification
6099 *
6100 * @hide
6101 */
6102 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08006103 Class<? extends Style> style = getNotificationStyle();
6104 if (MediaStyle.class.equals(style)) {
6105 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
6106 if ((colorized == null || colorized) && hasMediaSession()) {
6107 return true;
6108 }
6109 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6110 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
6111 return true;
6112 }
6113 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07006114 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006115 }
6116
Selim Cinek0847acd2017-04-24 19:48:29 -07006117
6118 /**
6119 * @return true if this is a media notification
6120 *
6121 * @hide
6122 */
6123 public boolean isMediaNotification() {
6124 Class<? extends Style> style = getNotificationStyle();
6125 if (MediaStyle.class.equals(style)) {
6126 return true;
6127 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6128 return true;
6129 }
6130 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006131 }
6132
Selim Cinek279fa862016-06-14 10:57:25 -07006133 private boolean hasLargeIcon() {
6134 return mLargeIcon != null || largeIcon != null;
6135 }
6136
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006137 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006138 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006139 * @hide
6140 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07006141 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006142 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
6143 }
6144
6145 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006146 * @return true if the notification will show a chronometer; false otherwise
6147 * @hide
6148 */
6149 public boolean showsChronometer() {
6150 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
6151 }
6152
6153 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04006154 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05006155 */
6156 @SystemApi
6157 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
6158 Class<? extends Style>[] classes = new Class[] {
6159 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
6160 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
6161 MessagingStyle.class };
6162 for (Class<? extends Style> innerClass : classes) {
6163 if (templateClass.equals(innerClass.getName())) {
6164 return innerClass;
6165 }
6166 }
6167 return null;
6168 }
6169
6170 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006171 * An object that can apply a rich notification style to a {@link Notification.Builder}
6172 * object.
6173 */
Griff Hazendfcb0802014-02-11 12:00:00 -08006174 public static abstract class Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07006175
6176 /**
6177 * The number of items allowed simulatanously in the remote input history.
6178 * @hide
6179 */
6180 static final int MAX_REMOTE_INPUT_HISTORY_LINES = 3;
Chris Wrend6297db2012-05-03 16:20:13 -04006181 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006182
6183 /**
6184 * @hide
6185 */
6186 protected CharSequence mSummaryText = null;
6187
6188 /**
6189 * @hide
6190 */
6191 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04006192
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006193 protected Builder mBuilder;
6194
Chris Wrend6297db2012-05-03 16:20:13 -04006195 /**
6196 * Overrides ContentTitle in the big form of the template.
6197 * This defaults to the value passed to setContentTitle().
6198 */
6199 protected void internalSetBigContentTitle(CharSequence title) {
6200 mBigContentTitle = title;
6201 }
6202
6203 /**
6204 * Set the first line of text after the detail section in the big form of the template.
6205 */
6206 protected void internalSetSummaryText(CharSequence cs) {
6207 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04006208 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04006209 }
6210
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006211 public void setBuilder(Builder builder) {
6212 if (mBuilder != builder) {
6213 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07006214 if (mBuilder != null) {
6215 mBuilder.setStyle(this);
6216 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006217 }
6218 }
6219
Chris Wrend6297db2012-05-03 16:20:13 -04006220 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006221 if (mBuilder == null) {
6222 throw new IllegalArgumentException("Style requires a valid Builder object");
6223 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006224 }
Chris Wrend6297db2012-05-03 16:20:13 -04006225
6226 protected RemoteViews getStandardView(int layoutId) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006227 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder);
6228 return getStandardView(layoutId, p, null);
Selim Cinek384804b2018-04-18 14:31:07 +08006229 }
6230
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006231
Selim Cinek384804b2018-04-18 14:31:07 +08006232 /**
6233 * Get the standard view for this style.
6234 *
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006235 * @param layoutId The layout id to use.
6236 * @param p the params for this inflation.
Selim Cinek384804b2018-04-18 14:31:07 +08006237 * @param result The result where template bind information is saved.
6238 * @return A remoteView for this style.
6239 * @hide
6240 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006241 protected RemoteViews getStandardView(int layoutId, StandardTemplateParams p,
6242 TemplateBindResult result) {
Chris Wrend6297db2012-05-03 16:20:13 -04006243 checkBuilder();
6244
6245 if (mBigContentTitle != null) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006246 p.title = mBigContentTitle;
Chris Wrend6297db2012-05-03 16:20:13 -04006247 }
6248
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006249 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId, p,
6250 result);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006251
Chris Wrend6297db2012-05-03 16:20:13 -04006252 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
6253 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04006254 } else {
6255 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04006256 }
6257
Chris Wrend6297db2012-05-03 16:20:13 -04006258 return contentView;
6259 }
6260
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006261 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006262 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006263 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08006264 *
6265 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006266 * @hide
6267 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08006268 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006269 return null;
6270 }
6271
6272 /**
6273 * Construct a Style-specific RemoteViews for the final big notification layout.
6274 * @hide
6275 */
6276 public RemoteViews makeBigContentView() {
6277 return null;
6278 }
6279
6280 /**
6281 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08006282 *
6283 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006284 * @hide
6285 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006286 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006287 return null;
6288 }
6289
6290 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006291 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006292 * @hide
6293 */
6294 public void addExtras(Bundle extras) {
6295 if (mSummaryTextSet) {
6296 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
6297 }
6298 if (mBigContentTitle != null) {
6299 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
6300 }
Chris Wren91ad5632013-06-05 15:05:57 -04006301 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006302 }
6303
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006304 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006305 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006306 * @hide
6307 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02006308 protected void restoreFromExtras(Bundle extras) {
6309 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
6310 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
6311 mSummaryTextSet = true;
6312 }
6313 if (extras.containsKey(EXTRA_TITLE_BIG)) {
6314 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
6315 }
6316 }
6317
6318
6319 /**
6320 * @hide
6321 */
6322 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006323 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006324 return wip;
6325 }
6326
Daniel Sandler0ec46202015-06-24 01:27:05 -04006327 /**
6328 * @hide
6329 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006330 public void purgeResources() {}
6331
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006332 /**
6333 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
6334 * attached to.
6335 *
6336 * @return the fully constructed Notification.
6337 */
6338 public Notification build() {
6339 checkBuilder();
6340 return mBuilder.build();
6341 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006342
6343 /**
6344 * @hide
6345 * @return true if the style positions the progress bar on the second line; false if the
6346 * style hides the progress bar
6347 */
6348 protected boolean hasProgress() {
6349 return true;
6350 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006351
6352 /**
6353 * @hide
6354 * @return Whether we should put the summary be put into the notification header
6355 */
6356 public boolean hasSummaryInHeader() {
6357 return true;
6358 }
Selim Cinek593610c2016-02-16 18:42:57 -08006359
6360 /**
6361 * @hide
6362 * @return Whether custom content views are displayed inline in the style
6363 */
6364 public boolean displayCustomViewInline() {
6365 return false;
6366 }
Selim Cinekd0426622017-07-11 13:19:59 +02006367
6368 /**
6369 * Reduces the image sizes contained in this style.
6370 *
6371 * @hide
6372 */
6373 public void reduceImageSizes(Context context) {
6374 }
Selim Cinek90343862018-02-01 11:07:11 -08006375
6376 /**
6377 * Validate that this style was properly composed. This is called at build time.
6378 * @hide
6379 */
6380 public void validate(Context context) {
6381 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006382
6383 /**
6384 * @hide
6385 */
6386 public abstract boolean areNotificationsVisiblyDifferent(Style other);
Selim Cinekc7f5a822018-03-20 19:32:06 -07006387
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006388 /**
koprivaa1a78482018-10-09 10:09:23 -07006389 * @return the text that should be displayed in the statusBar when heads-upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006390 * If {@code null} is returned, the default implementation will be used.
6391 *
6392 * @hide
6393 */
6394 public CharSequence getHeadsUpStatusBarText() {
6395 return null;
6396 }
Joe Onorato46439ce2010-11-19 13:56:21 -08006397 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006398
6399 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006400 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08006401 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006402 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006403 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006404 * Notification notif = new Notification.Builder(mContext)
6405 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
6406 * .setContentText(subject)
6407 * .setSmallIcon(R.drawable.new_post)
6408 * .setLargeIcon(aBitmap)
6409 * .setStyle(new Notification.BigPictureStyle()
6410 * .bigPicture(aBigBitmap))
6411 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006412 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006413 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006414 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006415 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006416 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006417 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006418 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006419 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006420
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006421 public BigPictureStyle() {
6422 }
6423
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006424 /**
6425 * @deprecated use {@code BigPictureStyle()}.
6426 */
6427 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006428 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006429 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006430 }
6431
Chris Wrend6297db2012-05-03 16:20:13 -04006432 /**
6433 * Overrides ContentTitle in the big form of the template.
6434 * This defaults to the value passed to setContentTitle().
6435 */
6436 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006437 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006438 return this;
6439 }
6440
6441 /**
6442 * Set the first line of text after the detail section in the big form of the template.
6443 */
6444 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006445 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006446 return this;
6447 }
6448
Chris Wren0bd664d2012-08-01 13:56:56 -04006449 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05006450 * @hide
6451 */
6452 public Bitmap getBigPicture() {
6453 return mPicture;
6454 }
6455
6456 /**
Chris Wren0bd664d2012-08-01 13:56:56 -04006457 * Provide the bitmap to be used as the payload for the BigPicture notification.
6458 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006459 public BigPictureStyle bigPicture(Bitmap b) {
6460 mPicture = b;
6461 return this;
6462 }
6463
Chris Wren3745a3d2012-05-22 15:11:52 -04006464 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04006465 * Override the large icon when the big notification is shown.
6466 */
6467 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04006468 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
6469 }
6470
6471 /**
6472 * Override the large icon when the big notification is shown.
6473 */
6474 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04006475 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006476 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006477 return this;
6478 }
6479
Riley Andrews0394a0c2015-11-03 23:36:52 -08006480 /** @hide */
6481 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
6482
Daniel Sandler0ec46202015-06-24 01:27:05 -04006483 /**
6484 * @hide
6485 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006486 @Override
6487 public void purgeResources() {
6488 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08006489 if (mPicture != null &&
6490 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08006491 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006492 mPicture = mPicture.createAshmemBitmap();
6493 }
6494 if (mBigLargeIcon != null) {
6495 mBigLargeIcon.convertToAshmem();
6496 }
6497 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01006498
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006499 /**
6500 * @hide
6501 */
Selim Cinekd0426622017-07-11 13:19:59 +02006502 @Override
6503 public void reduceImageSizes(Context context) {
6504 super.reduceImageSizes(context);
6505 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006506 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006507 if (mPicture != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006508 int maxPictureWidth = resources.getDimensionPixelSize(isLowRam
6509 ? R.dimen.notification_big_picture_max_height_low_ram
6510 : R.dimen.notification_big_picture_max_height);
6511 int maxPictureHeight = resources.getDimensionPixelSize(isLowRam
6512 ? R.dimen.notification_big_picture_max_width_low_ram
6513 : R.dimen.notification_big_picture_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006514 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
6515 }
6516 if (mBigLargeIcon != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006517 int rightIconSize = resources.getDimensionPixelSize(isLowRam
6518 ? R.dimen.notification_right_icon_size_low_ram
6519 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006520 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
6521 }
6522 }
6523
6524 /**
6525 * @hide
6526 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006527 public RemoteViews makeBigContentView() {
6528 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01006529 // This covers the following cases:
6530 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006531 // mN.mLargeIcon
6532 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04006533 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07006534 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006535 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006536 oldLargeIcon = mBuilder.mN.mLargeIcon;
6537 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006538 // The legacy largeIcon might not allow us to clear the image, as it's taken in
6539 // replacement if the other one is null. Because we're restoring these legacy icons
6540 // for old listeners, this is in general non-null.
6541 largeIconLegacy = mBuilder.mN.largeIcon;
6542 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006543 }
6544
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006545 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder);
Selim Cinek384804b2018-04-18 14:31:07 +08006546 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource(),
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006547 p, null /* result */);
Selim Cinek03d0d652015-11-13 13:18:09 -05006548 if (mSummaryTextSet) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006549 contentView.setTextViewText(R.id.text, mBuilder.processTextSpans(
6550 mBuilder.processLegacyText(mSummaryText)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006551 mBuilder.setTextViewColorSecondary(contentView, R.id.text, p);
Selim Cinekc848c3a2016-01-13 15:27:30 -08006552 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05006553 }
Selim Cinek279fa862016-06-14 10:57:25 -07006554 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08006555
Christoph Studer5c510ee2014-12-15 16:32:27 +01006556 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006557 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006558 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006559 }
6560
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006561 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006562 return contentView;
6563 }
6564
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006565 /**
6566 * @hide
6567 */
6568 public void addExtras(Bundle extras) {
6569 super.addExtras(extras);
6570
6571 if (mBigLargeIconSet) {
6572 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
6573 }
6574 extras.putParcelable(EXTRA_PICTURE, mPicture);
6575 }
6576
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006577 /**
6578 * @hide
6579 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006580 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02006581 protected void restoreFromExtras(Bundle extras) {
6582 super.restoreFromExtras(extras);
6583
6584 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01006585 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02006586 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04006587 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006588 mPicture = extras.getParcelable(EXTRA_PICTURE);
6589 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006590
6591 /**
6592 * @hide
6593 */
6594 @Override
6595 public boolean hasSummaryInHeader() {
6596 return false;
6597 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006598
6599 /**
6600 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006601 * Note that we aren't actually comparing the contents of the bitmaps here, so this
6602 * is only doing a cursory inspection. Bitmaps of equal size will appear the same.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006603 */
6604 @Override
6605 public boolean areNotificationsVisiblyDifferent(Style other) {
6606 if (other == null || getClass() != other.getClass()) {
6607 return true;
6608 }
6609 BigPictureStyle otherS = (BigPictureStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006610 return areBitmapsObviouslyDifferent(getBigPicture(), otherS.getBigPicture());
6611 }
6612
6613 private static boolean areBitmapsObviouslyDifferent(Bitmap a, Bitmap b) {
6614 if (a == b) {
6615 return false;
6616 }
6617 if (a == null || b == null) {
6618 return true;
6619 }
6620 return a.getWidth() != b.getWidth()
6621 || a.getHeight() != b.getHeight()
6622 || a.getConfig() != b.getConfig()
6623 || a.getGenerationId() != b.getGenerationId();
Julia Reynolds7217dc92018-03-07 12:12:09 -05006624 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006625 }
6626
6627 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006628 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08006629 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006630 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006631 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006632 * Notification notif = new Notification.Builder(mContext)
6633 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
6634 * .setContentText(subject)
6635 * .setSmallIcon(R.drawable.new_mail)
6636 * .setLargeIcon(aBitmap)
6637 * .setStyle(new Notification.BigTextStyle()
6638 * .bigText(aVeryLongString))
6639 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006640 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006641 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006642 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006643 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006644 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006645
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006646 private CharSequence mBigText;
6647
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006648 public BigTextStyle() {
6649 }
6650
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006651 /**
6652 * @deprecated use {@code BigTextStyle()}.
6653 */
6654 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006655 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006656 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006657 }
6658
Chris Wrend6297db2012-05-03 16:20:13 -04006659 /**
6660 * Overrides ContentTitle in the big form of the template.
6661 * This defaults to the value passed to setContentTitle().
6662 */
6663 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006664 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006665 return this;
6666 }
6667
6668 /**
6669 * Set the first line of text after the detail section in the big form of the template.
6670 */
6671 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006672 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006673 return this;
6674 }
6675
Chris Wren0bd664d2012-08-01 13:56:56 -04006676 /**
6677 * Provide the longer text to be displayed in the big form of the
6678 * template in place of the content text.
6679 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006680 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006681 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006682 return this;
6683 }
6684
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006685 /**
6686 * @hide
6687 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05006688 public CharSequence getBigText() {
6689 return mBigText;
6690 }
6691
6692 /**
6693 * @hide
6694 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006695 public void addExtras(Bundle extras) {
6696 super.addExtras(extras);
6697
Christoph Studer4600f9b2014-07-22 22:44:43 +02006698 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
6699 }
6700
6701 /**
6702 * @hide
6703 */
6704 @Override
6705 protected void restoreFromExtras(Bundle extras) {
6706 super.restoreFromExtras(extras);
6707
6708 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006709 }
6710
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006711 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006712 * @param increasedHeight true if this layout be created with an increased height.
6713 *
6714 * @hide
6715 */
6716 @Override
6717 public RemoteViews makeContentView(boolean increasedHeight) {
6718 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006719 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006720 mBuilder.mActions = new ArrayList<>();
6721 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006722 mBuilder.mActions = mBuilder.mOriginalActions;
6723 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006724 return remoteViews;
6725 }
6726 return super.makeContentView(increasedHeight);
6727 }
6728
6729 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006730 * @hide
6731 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006732 @Override
6733 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
6734 if (increasedHeight && mBuilder.mActions.size() > 0) {
6735 return makeBigContentView();
6736 }
6737 return super.makeHeadsUpContentView(increasedHeight);
6738 }
6739
6740 /**
6741 * @hide
6742 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006743 public RemoteViews makeBigContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006744 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder).text(null);
Selim Cinek384804b2018-04-18 14:31:07 +08006745 TemplateBindResult result = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006746 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource(), p,
6747 result);
Selim Cinek1c72fa02018-04-23 18:00:54 +08006748 contentView.setInt(R.id.big_text, "setImageEndMargin", result.getIconMarginEnd());
Joe Malin8d40d042012-11-05 11:36:40 -08006749
Selim Cinek3a2c4b92015-12-17 17:01:17 -08006750 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07006751 if (TextUtils.isEmpty(bigTextText)) {
6752 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
6753 // experience
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006754 bigTextText = mBuilder.processLegacyText(
6755 mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT));
Selim Cinek75998782016-04-26 10:39:17 -07006756 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006757 contentView.setTextViewText(R.id.big_text, mBuilder.processTextSpans(bigTextText));
6758 mBuilder.setTextViewColorSecondary(contentView, R.id.big_text, p);
6759 contentView.setViewVisibility(R.id.big_text,
6760 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08006761 contentView.setBoolean(R.id.big_text, "setHasImage",
6762 result.isRightIconContainerVisible());
Selim Cinek4fb12d32015-11-19 18:10:48 -08006763
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006764 return contentView;
6765 }
6766
Julia Reynolds7217dc92018-03-07 12:12:09 -05006767 /**
6768 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006769 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006770 */
6771 @Override
6772 public boolean areNotificationsVisiblyDifferent(Style other) {
6773 if (other == null || getClass() != other.getClass()) {
6774 return true;
6775 }
6776 BigTextStyle newS = (BigTextStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006777 return !Objects.equals(String.valueOf(getBigText()), String.valueOf(newS.getBigText()));
Julia Reynolds7217dc92018-03-07 12:12:09 -05006778 }
6779
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006780 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006781
6782 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006783 * Helper class for generating large-format notifications that include multiple back-and-forth
6784 * messages of varying types between any number of people.
6785 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006786 * <p>
Alex Hillsfc737de2016-03-23 17:33:02 -04006787 * If the platform does not provide large-format notifications, this method has no effect. The
6788 * user will always see the normal notification view.
Selim Cinekce8794f2018-05-23 16:46:05 -07006789 *
6790 * <p>
6791 * If the app is targeting Android P and above, it is required to use the {@link Person}
6792 * class in order to get an optimal rendering of the notification and its avatars. For
6793 * conversations involving multiple people, the app should also make sure that it marks the
6794 * conversation as a group with {@link #setGroupConversation(boolean)}.
6795 *
6796 * <p>
6797 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior.
6798 * Here's an example of how this may be used:
Alex Hillsfc737de2016-03-23 17:33:02 -04006799 * <pre class="prettyprint">
6800 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006801 * Person user = new Person.Builder().setIcon(userIcon).setName(userName).build();
6802 * MessagingStyle style = new MessagingStyle(user)
6803 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getPerson())
6804 * .addMessage(messages[2].getText(), messages[2].getTime(), messages[2].getPerson())
6805 * .setGroupConversation(hasMultiplePeople());
6806 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006807 * Notification noti = new Notification.Builder()
Selim Cinekce8794f2018-05-23 16:46:05 -07006808 * .setContentTitle(&quot;2 new messages with &quot; + sender.toString())
Alex Hillsfc737de2016-03-23 17:33:02 -04006809 * .setContentText(subject)
6810 * .setSmallIcon(R.drawable.new_message)
6811 * .setLargeIcon(aBitmap)
Selim Cinekce8794f2018-05-23 16:46:05 -07006812 * .setStyle(style)
Alex Hillsfc737de2016-03-23 17:33:02 -04006813 * .build();
6814 * </pre>
6815 */
6816 public static class MessagingStyle extends Style {
6817
6818 /**
6819 * The maximum number of messages that will be retained in the Notification itself (the
6820 * number displayed is up to the platform).
6821 */
6822 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
6823
Selim Cinekcb8b9852017-12-15 18:01:52 -08006824 @NonNull Person mUser;
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006825 @Nullable CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04006826 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08006827 List<Message> mHistoricMessages = new ArrayList<>();
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006828 boolean mIsGroupConversation;
Alex Hillsfc737de2016-03-23 17:33:02 -04006829
6830 MessagingStyle() {
6831 }
6832
6833 /**
Alex Hillsfd590442016-10-07 09:52:44 -04006834 * @param userDisplayName Required - the name to be displayed for any replies sent by the
6835 * user before the posting app reposts the notification with those messages after they've
6836 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04006837 * {@link #addMessage(Notification.MessagingStyle.Message)}
Selim Cinekcb8b9852017-12-15 18:01:52 -08006838 *
6839 * @deprecated use {@code MessagingStyle(Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006840 */
Alex Hillsfd590442016-10-07 09:52:44 -04006841 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Selim Cinek9acd6732018-03-23 16:39:02 -07006842 this(new Person.Builder().setName(userDisplayName).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006843 }
6844
6845 /**
6846 * @param user Required - The person displayed for any messages that are sent by the
6847 * user. Any messages added with {@link #addMessage(Notification.MessagingStyle.Message)}
6848 * who don't have a Person associated with it will be displayed as if they were sent
Selim Cinek90343862018-02-01 11:07:11 -08006849 * by this user. The user also needs to have a valid name associated with it, which will
6850 * be enforced starting in Android P.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006851 */
6852 public MessagingStyle(@NonNull Person user) {
6853 mUser = user;
Selim Cinek90343862018-02-01 11:07:11 -08006854 }
6855
6856 /**
6857 * Validate that this style was properly composed. This is called at build time.
6858 * @hide
6859 */
6860 @Override
6861 public void validate(Context context) {
6862 super.validate(context);
6863 if (context.getApplicationInfo().targetSdkVersion
6864 >= Build.VERSION_CODES.P && (mUser == null || mUser.getName() == null)) {
6865 throw new RuntimeException("User must be valid and have a name.");
Selim Cinekcb8b9852017-12-15 18:01:52 -08006866 }
6867 }
6868
6869 /**
koprivaa1a78482018-10-09 10:09:23 -07006870 * @return the text that should be displayed in the statusBar when heads upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006871 * If {@code null} is returned, the default implementation will be used.
6872 *
6873 * @hide
6874 */
6875 @Override
6876 public CharSequence getHeadsUpStatusBarText() {
6877 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
6878 ? super.mBigContentTitle
6879 : mConversationTitle;
6880 if (!TextUtils.isEmpty(conversationTitle) && !hasOnlyWhiteSpaceSenders()) {
6881 return conversationTitle;
6882 }
6883 return null;
6884 }
6885
6886 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08006887 * @return the user to be displayed for any replies sent by the user
6888 */
Selim Cinekeb53c222018-04-06 12:56:09 -07006889 @NonNull
Selim Cinekcb8b9852017-12-15 18:01:52 -08006890 public Person getUser() {
6891 return mUser;
Alex Hillsfc737de2016-03-23 17:33:02 -04006892 }
6893
6894 /**
6895 * Returns the name to be displayed for any replies sent by the user
Selim Cinekcb8b9852017-12-15 18:01:52 -08006896 *
6897 * @deprecated use {@link #getUser()} instead
Alex Hillsfc737de2016-03-23 17:33:02 -04006898 */
6899 public CharSequence getUserDisplayName() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006900 return mUser.getName();
Alex Hillsfc737de2016-03-23 17:33:02 -04006901 }
6902
6903 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006904 * Sets the title to be displayed on this conversation. May be set to {@code null}.
6905 *
Kodlee Yin14656422017-12-22 17:00:46 -08006906 * <p>This API's behavior was changed in SDK version {@link Build.VERSION_CODES#P}. If your
6907 * application's target version is less than {@link Build.VERSION_CODES#P}, setting a
6908 * conversation title to a non-null value will make {@link #isGroupConversation()} return
6909 * {@code true} and passing {@code null} will make it return {@code false}. In
6910 * {@link Build.VERSION_CODES#P} and beyond, use {@link #setGroupConversation(boolean)}
6911 * to set group conversation status.
6912 *
6913 * @param conversationTitle Title displayed for this conversation
6914 * @return this object for method chaining
Alex Hillsfc737de2016-03-23 17:33:02 -04006915 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006916 public MessagingStyle setConversationTitle(@Nullable CharSequence conversationTitle) {
Alex Hillsfc737de2016-03-23 17:33:02 -04006917 mConversationTitle = conversationTitle;
6918 return this;
6919 }
6920
6921 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006922 * Return the title to be displayed on this conversation. May return {@code null}.
Alex Hillsfc737de2016-03-23 17:33:02 -04006923 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006924 @Nullable
Alex Hillsfc737de2016-03-23 17:33:02 -04006925 public CharSequence getConversationTitle() {
6926 return mConversationTitle;
6927 }
6928
6929 /**
6930 * Adds a message for display by this notification. Convenience call for a simple
6931 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6932 * @param text A {@link CharSequence} to be displayed as the message content
6933 * @param timestamp Time at which the message arrived
6934 * @param sender A {@link CharSequence} to be used for displaying the name of the
6935 * sender. Should be <code>null</code> for messages by the current user, in which case
6936 * the platform will insert {@link #getUserDisplayName()}.
6937 * Should be unique amongst all individuals in the conversation, and should be
6938 * consistent during re-posts of the notification.
6939 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07006940 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Alex Hillsfc737de2016-03-23 17:33:02 -04006941 *
6942 * @return this object for method chaining
Selim Cinekcb8b9852017-12-15 18:01:52 -08006943 *
6944 * @deprecated use {@link #addMessage(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006945 */
6946 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006947 return addMessage(text, timestamp,
Selim Cinek9acd6732018-03-23 16:39:02 -07006948 sender == null ? null : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006949 }
6950
6951 /**
6952 * Adds a message for display by this notification. Convenience call for a simple
6953 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6954 * @param text A {@link CharSequence} to be displayed as the message content
6955 * @param timestamp Time at which the message arrived
6956 * @param sender The {@link Person} who sent the message.
6957 * Should be <code>null</code> for messages by the current user, in which case
6958 * the platform will insert the user set in {@code MessagingStyle(Person)}.
6959 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07006960 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Selim Cinekcb8b9852017-12-15 18:01:52 -08006961 *
6962 * @return this object for method chaining
6963 */
Selim Cinekeb53c222018-04-06 12:56:09 -07006964 public MessagingStyle addMessage(@NonNull CharSequence text, long timestamp,
6965 @Nullable Person sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08006966 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04006967 }
6968
6969 /**
6970 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08006971 *
6972 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6973 * the newest last.
6974 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006975 * @param message The {@link Message} to be displayed
6976 * @return this object for method chaining
6977 */
6978 public MessagingStyle addMessage(Message message) {
6979 mMessages.add(message);
6980 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6981 mMessages.remove(0);
6982 }
6983 return this;
6984 }
6985
6986 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006987 * Adds a {@link Message} for historic context in this notification.
6988 *
6989 * <p>Messages should be added as historic if they are not the main subject of the
6990 * notification but may give context to a conversation. The system may choose to present
6991 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
6992 *
6993 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6994 * the newest last.
6995 *
6996 * @param message The historic {@link Message} to be added
6997 * @return this object for method chaining
6998 */
6999 public MessagingStyle addHistoricMessage(Message message) {
7000 mHistoricMessages.add(message);
7001 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
7002 mHistoricMessages.remove(0);
7003 }
7004 return this;
7005 }
7006
7007 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007008 * Gets the list of {@code Message} objects that represent the notification
7009 */
7010 public List<Message> getMessages() {
7011 return mMessages;
7012 }
7013
7014 /**
Adrian Roos437cd562017-01-18 15:47:03 -08007015 * Gets the list of historic {@code Message}s in the notification.
7016 */
7017 public List<Message> getHistoricMessages() {
7018 return mHistoricMessages;
7019 }
7020
7021 /**
Selim Cinekce8794f2018-05-23 16:46:05 -07007022 * Sets whether this conversation notification represents a group. If the app is targeting
7023 * Android P, this is required if the app wants to display the largeIcon set with
7024 * {@link Notification.Builder#setLargeIcon(Bitmap)}, otherwise it will be hidden.
Kodlee Yin14656422017-12-22 17:00:46 -08007025 *
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007026 * @param isGroupConversation {@code true} if the conversation represents a group,
7027 * {@code false} otherwise.
7028 * @return this object for method chaining
7029 */
7030 public MessagingStyle setGroupConversation(boolean isGroupConversation) {
7031 mIsGroupConversation = isGroupConversation;
7032 return this;
7033 }
7034
7035 /**
Kodlee Yin14656422017-12-22 17:00:46 -08007036 * Returns {@code true} if this notification represents a group conversation, otherwise
7037 * {@code false}.
7038 *
7039 * <p> If the application that generated this {@link MessagingStyle} targets an SDK version
7040 * less than {@link Build.VERSION_CODES#P}, this method becomes dependent on whether or
7041 * not the conversation title is set; returning {@code true} if the conversation title is
7042 * a non-null value, or {@code false} otherwise. From {@link Build.VERSION_CODES#P} forward,
7043 * this method returns what's set by {@link #setGroupConversation(boolean)} allowing for
7044 * named, non-group conversations.
7045 *
7046 * @see #setConversationTitle(CharSequence)
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007047 */
7048 public boolean isGroupConversation() {
Kodlee Yin14656422017-12-22 17:00:46 -08007049 // When target SDK version is < P, a non-null conversation title dictates if this is
7050 // as group conversation.
7051 if (mBuilder != null
7052 && mBuilder.mContext.getApplicationInfo().targetSdkVersion
7053 < Build.VERSION_CODES.P) {
7054 return mConversationTitle != null;
7055 }
7056
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007057 return mIsGroupConversation;
7058 }
7059
7060 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007061 * @hide
7062 */
7063 @Override
7064 public void addExtras(Bundle extras) {
7065 super.addExtras(extras);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007066 if (mUser != null) {
7067 // For legacy usages
7068 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUser.getName());
7069 extras.putParcelable(EXTRA_MESSAGING_PERSON, mUser);
Alex Hillsfc737de2016-03-23 17:33:02 -04007070 }
7071 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007072 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04007073 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007074 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
7075 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04007076 }
Adrian Roos437cd562017-01-18 15:47:03 -08007077 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
7078 Message.getBundleArrayForMessages(mHistoricMessages));
7079 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007080
7081 fixTitleAndTextExtras(extras);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007082 extras.putBoolean(EXTRA_IS_GROUP_CONVERSATION, mIsGroupConversation);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007083 }
7084
7085 private void fixTitleAndTextExtras(Bundle extras) {
7086 Message m = findLatestIncomingMessage();
7087 CharSequence text = (m == null) ? null : m.mText;
7088 CharSequence sender = m == null ? null
Selim Cinekcb8b9852017-12-15 18:01:52 -08007089 : m.mSender == null || TextUtils.isEmpty(m.mSender.getName())
7090 ? mUser.getName() : m.mSender.getName();
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007091 CharSequence title;
7092 if (!TextUtils.isEmpty(mConversationTitle)) {
7093 if (!TextUtils.isEmpty(sender)) {
7094 BidiFormatter bidi = BidiFormatter.getInstance();
7095 title = mBuilder.mContext.getString(
7096 com.android.internal.R.string.notification_messaging_title_template,
Selim Cinekcb8b9852017-12-15 18:01:52 -08007097 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(sender));
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007098 } else {
7099 title = mConversationTitle;
7100 }
7101 } else {
7102 title = sender;
7103 }
7104
7105 if (title != null) {
7106 extras.putCharSequence(EXTRA_TITLE, title);
7107 }
7108 if (text != null) {
7109 extras.putCharSequence(EXTRA_TEXT, text);
7110 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007111 }
7112
7113 /**
7114 * @hide
7115 */
7116 @Override
7117 protected void restoreFromExtras(Bundle extras) {
7118 super.restoreFromExtras(extras);
7119
Selim Cinekcb8b9852017-12-15 18:01:52 -08007120 mUser = extras.getParcelable(EXTRA_MESSAGING_PERSON);
7121 if (mUser == null) {
7122 CharSequence displayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
Selim Cinek9acd6732018-03-23 16:39:02 -07007123 mUser = new Person.Builder().setName(displayName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007124 }
Adrian Roos96b7e202016-05-17 13:50:38 -07007125 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08007126 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007127 mMessages = Message.getMessagesFromBundleArray(messages);
Adrian Roos437cd562017-01-18 15:47:03 -08007128 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007129 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007130 mIsGroupConversation = extras.getBoolean(EXTRA_IS_GROUP_CONVERSATION);
Alex Hillsfc737de2016-03-23 17:33:02 -04007131 }
7132
7133 /**
7134 * @hide
7135 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07007136 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007137 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07007138 mBuilder.mOriginalActions = mBuilder.mActions;
7139 mBuilder.mActions = new ArrayList<>();
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007140 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007141 false /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007142 mBuilder.mActions = mBuilder.mOriginalActions;
7143 mBuilder.mOriginalActions = null;
7144 return remoteViews;
Adrian Roosc1a80b02016-04-05 14:54:55 -07007145 }
7146
Julia Reynolds7217dc92018-03-07 12:12:09 -05007147 /**
7148 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04007149 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05007150 */
7151 @Override
7152 public boolean areNotificationsVisiblyDifferent(Style other) {
7153 if (other == null || getClass() != other.getClass()) {
7154 return true;
7155 }
7156 MessagingStyle newS = (MessagingStyle) other;
7157 List<MessagingStyle.Message> oldMs = getMessages();
7158 List<MessagingStyle.Message> newMs = newS.getMessages();
7159
Dan Sandler7d67bd42018-05-15 14:06:38 -04007160 if (oldMs == null || newMs == null) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007161 newMs = new ArrayList<>();
7162 }
7163
7164 int n = oldMs.size();
7165 if (n != newMs.size()) {
7166 return true;
7167 }
7168 for (int i = 0; i < n; i++) {
7169 MessagingStyle.Message oldM = oldMs.get(i);
7170 MessagingStyle.Message newM = newMs.get(i);
Dan Sandler7d67bd42018-05-15 14:06:38 -04007171 if (!Objects.equals(
7172 String.valueOf(oldM.getText()),
7173 String.valueOf(newM.getText()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007174 return true;
7175 }
7176 if (!Objects.equals(oldM.getDataUri(), newM.getDataUri())) {
7177 return true;
7178 }
Dan Sandler7d67bd42018-05-15 14:06:38 -04007179 String oldSender = String.valueOf(oldM.getSenderPerson() == null
7180 ? oldM.getSender()
7181 : oldM.getSenderPerson().getName());
7182 String newSender = String.valueOf(newM.getSenderPerson() == null
7183 ? newM.getSender()
7184 : newM.getSenderPerson().getName());
Julia Reynolds7217dc92018-03-07 12:12:09 -05007185 if (!Objects.equals(oldSender, newSender)) {
7186 return true;
7187 }
7188
7189 String oldKey = oldM.getSenderPerson() == null
7190 ? null : oldM.getSenderPerson().getKey();
7191 String newKey = newM.getSenderPerson() == null
7192 ? null : newM.getSenderPerson().getKey();
7193 if (!Objects.equals(oldKey, newKey)) {
7194 return true;
7195 }
7196 // Other fields (like timestamp) intentionally excluded
7197 }
7198 return false;
7199 }
7200
Adrian Roosc1a80b02016-04-05 14:54:55 -07007201 private Message findLatestIncomingMessage() {
Selim Cinek88188f22017-09-19 16:46:56 -07007202 return findLatestIncomingMessage(mMessages);
7203 }
7204
7205 /**
7206 * @hide
7207 */
7208 @Nullable
7209 public static Message findLatestIncomingMessage(
7210 List<Message> messages) {
7211 for (int i = messages.size() - 1; i >= 0; i--) {
7212 Message m = messages.get(i);
Adrian Roosc1a80b02016-04-05 14:54:55 -07007213 // Incoming messages have a non-empty sender.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007214 if (m.mSender != null && !TextUtils.isEmpty(m.mSender.getName())) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07007215 return m;
7216 }
7217 }
Selim Cinek88188f22017-09-19 16:46:56 -07007218 if (!messages.isEmpty()) {
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007219 // No incoming messages, fall back to outgoing message
Selim Cinek88188f22017-09-19 16:46:56 -07007220 return messages.get(messages.size() - 1);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007221 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07007222 return null;
7223 }
7224
7225 /**
7226 * @hide
7227 */
7228 @Override
7229 public RemoteViews makeBigContentView() {
Selim Cinek384804b2018-04-18 14:31:07 +08007230 return makeMessagingView(false /* displayImagesAtEnd */, true /* hideLargeIcon */);
Selim Cinekafeed292017-12-12 17:32:44 -08007231 }
7232
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007233 /**
7234 * Create a messaging layout.
7235 *
7236 * @param displayImagesAtEnd should images be displayed at the end of the content instead
7237 * of inline.
Selim Cinek384804b2018-04-18 14:31:07 +08007238 * @param hideRightIcons Should the reply affordance be shown at the end of the notification
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007239 * @return the created remoteView.
7240 */
Selim Cinekafeed292017-12-12 17:32:44 -08007241 @NonNull
Selim Cinek384804b2018-04-18 14:31:07 +08007242 private RemoteViews makeMessagingView(boolean displayImagesAtEnd, boolean hideRightIcons) {
Selim Cinek88188f22017-09-19 16:46:56 -07007243 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
Adrian Roosc1a80b02016-04-05 14:54:55 -07007244 ? super.mBigContentTitle
7245 : mConversationTitle;
Selim Cinekce8794f2018-05-23 16:46:05 -07007246 boolean atLeastP = mBuilder.mContext.getApplicationInfo().targetSdkVersion
7247 >= Build.VERSION_CODES.P;
7248 boolean isOneToOne;
Selim Cinek2dd3e722018-01-19 11:06:06 -08007249 CharSequence nameReplacement = null;
Selim Cinekce8794f2018-05-23 16:46:05 -07007250 Icon avatarReplacement = null;
7251 if (!atLeastP) {
7252 isOneToOne = TextUtils.isEmpty(conversationTitle);
7253 avatarReplacement = mBuilder.mN.mLargeIcon;
7254 if (hasOnlyWhiteSpaceSenders()) {
7255 isOneToOne = true;
7256 nameReplacement = conversationTitle;
7257 conversationTitle = null;
7258 }
7259 } else {
7260 isOneToOne = !isGroupConversation();
Adrian Roosb1f427c2016-05-26 12:27:15 -07007261 }
Selim Cinek384804b2018-04-18 14:31:07 +08007262 TemplateBindResult bindResult = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007263 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).title(
7264 conversationTitle).text(null)
7265 .hideLargeIcon(hideRightIcons || isOneToOne)
7266 .hideReplyIcon(hideRightIcons)
7267 .headerTextSecondary(conversationTitle);
Adrian Roos48d746a2016-04-12 14:57:28 -07007268 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07007269 mBuilder.getMessagingLayoutResource(),
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007270 p,
Selim Cinek384804b2018-04-18 14:31:07 +08007271 bindResult);
Selim Cinek88188f22017-09-19 16:46:56 -07007272 addExtras(mBuilder.mN.extras);
Selim Cinekafeed292017-12-12 17:32:44 -08007273 // also update the end margin if there is an image
Selim Cinek1c72fa02018-04-23 18:00:54 +08007274 contentView.setViewLayoutMarginEnd(R.id.notification_messaging,
7275 bindResult.getIconMarginEnd());
Selim Cinek88188f22017-09-19 16:46:56 -07007276 contentView.setInt(R.id.status_bar_latest_event_content, "setLayoutColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007277 mBuilder.isColorized(p) ? mBuilder.getPrimaryTextColor(p)
7278 : mBuilder.resolveContrastColor(p));
Kenny Guy14d035c2018-05-02 19:10:36 +01007279 contentView.setInt(R.id.status_bar_latest_event_content, "setSenderTextColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007280 mBuilder.getPrimaryTextColor(p));
Kenny Guy14d035c2018-05-02 19:10:36 +01007281 contentView.setInt(R.id.status_bar_latest_event_content, "setMessageTextColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007282 mBuilder.getSecondaryTextColor(p));
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007283 contentView.setBoolean(R.id.status_bar_latest_event_content, "setDisplayImagesAtEnd",
7284 displayImagesAtEnd);
Selim Cinekce8794f2018-05-23 16:46:05 -07007285 contentView.setIcon(R.id.status_bar_latest_event_content, "setAvatarReplacement",
7286 avatarReplacement);
Selim Cinek2dd3e722018-01-19 11:06:06 -08007287 contentView.setCharSequence(R.id.status_bar_latest_event_content, "setNameReplacement",
7288 nameReplacement);
Selim Cinek88188f22017-09-19 16:46:56 -07007289 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsOneToOne",
7290 isOneToOne);
7291 contentView.setBundle(R.id.status_bar_latest_event_content, "setData",
7292 mBuilder.mN.extras);
Alex Hillsfc737de2016-03-23 17:33:02 -04007293 return contentView;
7294 }
7295
Selim Cinekf7409db2017-10-24 16:17:14 -07007296 private boolean hasOnlyWhiteSpaceSenders() {
7297 for (int i = 0; i < mMessages.size(); i++) {
7298 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007299 Person sender = m.getSenderPerson();
7300 if (sender != null && !isWhiteSpace(sender.getName())) {
Selim Cinekf7409db2017-10-24 16:17:14 -07007301 return false;
7302 }
7303 }
7304 return true;
7305 }
7306
7307 private boolean isWhiteSpace(CharSequence sender) {
7308 if (TextUtils.isEmpty(sender)) {
7309 return true;
7310 }
7311 if (sender.toString().matches("^\\s*$")) {
7312 return true;
7313 }
7314 // Let's check if we only have 0 whitespace chars. Some apps did this as a workaround
7315 // For the presentation that we had.
7316 for (int i = 0; i < sender.length(); i++) {
7317 char c = sender.charAt(i);
7318 if (c != '\u200B') {
7319 return false;
7320 }
7321 }
7322 return true;
7323 }
7324
Selim Cinek88188f22017-09-19 16:46:56 -07007325 private CharSequence createConversationTitleFromMessages() {
7326 ArraySet<CharSequence> names = new ArraySet<>();
7327 for (int i = 0; i < mMessages.size(); i++) {
7328 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007329 Person sender = m.getSenderPerson();
Selim Cinek88188f22017-09-19 16:46:56 -07007330 if (sender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007331 names.add(sender.getName());
Selim Cinek88188f22017-09-19 16:46:56 -07007332 }
7333 }
7334 SpannableStringBuilder title = new SpannableStringBuilder();
7335 int size = names.size();
7336 for (int i = 0; i < size; i++) {
7337 CharSequence name = names.valueAt(i);
7338 if (!TextUtils.isEmpty(title)) {
7339 title.append(", ");
7340 }
7341 title.append(BidiFormatter.getInstance().unicodeWrap(name));
7342 }
7343 return title;
7344 }
7345
Adrian Roosdedd1df2016-04-26 16:38:47 -07007346 /**
7347 * @hide
7348 */
7349 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007350 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007351 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007352 true /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007353 remoteViews.setInt(R.id.notification_messaging, "setMaxDisplayedLines", 1);
7354 return remoteViews;
Adrian Roosdedd1df2016-04-26 16:38:47 -07007355 }
7356
Adrian Roosc1a80b02016-04-05 14:54:55 -07007357 private static TextAppearanceSpan makeFontColorSpan(int color) {
7358 return new TextAppearanceSpan(null, 0, 0,
7359 ColorStateList.valueOf(color), null);
7360 }
7361
Alex Hillsd9b04d92016-04-11 16:38:16 -04007362 public static final class Message {
Tony Mak09db2ea2018-06-27 18:12:48 +01007363 /** @hide */
7364 public static final String KEY_TEXT = "text";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007365 static final String KEY_TIMESTAMP = "time";
7366 static final String KEY_SENDER = "sender";
Selim Cinekcb8b9852017-12-15 18:01:52 -08007367 static final String KEY_SENDER_PERSON = "sender_person";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007368 static final String KEY_DATA_MIME_TYPE = "type";
7369 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08007370 static final String KEY_EXTRAS_BUNDLE = "extras";
Kenny Guya0f6de82018-04-06 16:20:16 +01007371 static final String KEY_REMOTE_INPUT_HISTORY = "remote_input_history";
Alex Hillsfc737de2016-03-23 17:33:02 -04007372
7373 private final CharSequence mText;
7374 private final long mTimestamp;
Selim Cinekcb8b9852017-12-15 18:01:52 -08007375 @Nullable
7376 private final Person mSender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007377 /** True if this message was generated from the extra
7378 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}
7379 */
7380 private final boolean mRemoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007381
Shane Brennan5a871862017-03-11 13:14:17 -08007382 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007383 private String mDataMimeType;
7384 private Uri mDataUri;
7385
7386 /**
7387 * Constructor
7388 * @param text A {@link CharSequence} to be displayed as the message content
7389 * @param timestamp Time at which the message arrived
7390 * @param sender A {@link CharSequence} to be used for displaying the name of the
7391 * sender. Should be <code>null</code> for messages by the current user, in which case
7392 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
7393 * Should be unique amongst all individuals in the conversation, and should be
7394 * consistent during re-posts of the notification.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007395 *
7396 * @deprecated use {@code Message(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04007397 */
7398 public Message(CharSequence text, long timestamp, CharSequence sender){
Selim Cinek9acd6732018-03-23 16:39:02 -07007399 this(text, timestamp, sender == null ? null
7400 : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08007401 }
7402
7403 /**
7404 * Constructor
7405 * @param text A {@link CharSequence} to be displayed as the message content
7406 * @param timestamp Time at which the message arrived
7407 * @param sender The {@link Person} who sent the message.
7408 * Should be <code>null</code> for messages by the current user, in which case
7409 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7410 * <p>
Selim Cinek9acd6732018-03-23 16:39:02 -07007411 * The person provided should contain an Icon, set with
7412 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7413 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7414 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7415 * to differentiate between the different users.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007416 * </p>
7417 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007418 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender) {
Kenny Guya0f6de82018-04-06 16:20:16 +01007419 this(text, timestamp, sender, false /* remoteHistory */);
7420 }
7421
7422 /**
7423 * Constructor
7424 * @param text A {@link CharSequence} to be displayed as the message content
7425 * @param timestamp Time at which the message arrived
7426 * @param sender The {@link Person} who sent the message.
7427 * Should be <code>null</code> for messages by the current user, in which case
7428 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7429 * @param remoteInputHistory True if the messages was generated from the extra
7430 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7431 * <p>
7432 * The person provided should contain an Icon, set with
7433 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7434 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7435 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7436 * to differentiate between the different users.
7437 * </p>
7438 * @hide
7439 */
7440 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender,
7441 boolean remoteInputHistory) {
Alex Hillsfc737de2016-03-23 17:33:02 -04007442 mText = text;
7443 mTimestamp = timestamp;
7444 mSender = sender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007445 mRemoteInputHistory = remoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007446 }
7447
7448 /**
7449 * Sets a binary blob of data and an associated MIME type for a message. In the case
7450 * where the platform doesn't support the MIME type, the original text provided in the
7451 * constructor will be used.
7452 * @param dataMimeType The MIME type of the content. See
7453 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
7454 * types on Android and Android Wear.
7455 * @param dataUri The uri containing the content whose type is given by the MIME type.
7456 * <p class="note">
7457 * <ol>
7458 * <li>Notification Listeners including the System UI need permission to access the
7459 * data the Uri points to. The recommended ways to do this are:</li>
7460 * <li>Store the data in your own ContentProvider, making sure that other apps have
7461 * the correct permission to access your provider. The preferred mechanism for
7462 * providing access is to use per-URI permissions which are temporary and only
7463 * grant access to the receiving application. An easy way to create a
7464 * ContentProvider like this is to use the FileProvider helper class.</li>
7465 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
7466 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
7467 * also store non-media types (see MediaStore.Files for more info). Files can be
7468 * inserted into the MediaStore using scanFile() after which a content:// style
7469 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
7470 * Note that once added to the system MediaStore the content is accessible to any
7471 * app on the device.</li>
7472 * </ol>
7473 * @return this object for method chaining
7474 */
7475 public Message setData(String dataMimeType, Uri dataUri) {
7476 mDataMimeType = dataMimeType;
7477 mDataUri = dataUri;
7478 return this;
7479 }
7480
Alex Hillsfc737de2016-03-23 17:33:02 -04007481 /**
7482 * Get the text to be used for this message, or the fallback text if a type and content
7483 * Uri have been set
7484 */
7485 public CharSequence getText() {
7486 return mText;
7487 }
7488
7489 /**
7490 * Get the time at which this message arrived
7491 */
7492 public long getTimestamp() {
7493 return mTimestamp;
7494 }
7495
7496 /**
Shane Brennan5a871862017-03-11 13:14:17 -08007497 * Get the extras Bundle for this message.
7498 */
7499 public Bundle getExtras() {
7500 return mExtras;
7501 }
7502
7503 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007504 * Get the text used to display the contact's name in the messaging experience
Selim Cinekcb8b9852017-12-15 18:01:52 -08007505 *
7506 * @deprecated use {@link #getSenderPerson()}
Alex Hillsfc737de2016-03-23 17:33:02 -04007507 */
7508 public CharSequence getSender() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007509 return mSender == null ? null : mSender.getName();
7510 }
7511
7512 /**
7513 * Get the sender associated with this message.
7514 */
7515 @Nullable
7516 public Person getSenderPerson() {
Alex Hillsfc737de2016-03-23 17:33:02 -04007517 return mSender;
7518 }
7519
7520 /**
7521 * Get the MIME type of the data pointed to by the Uri
7522 */
7523 public String getDataMimeType() {
7524 return mDataMimeType;
7525 }
7526
7527 /**
koprivaa1a78482018-10-09 10:09:23 -07007528 * Get the Uri pointing to the content of the message. Can be null, in which case
Alex Hillsfc737de2016-03-23 17:33:02 -04007529 * {@see #getText()} is used.
7530 */
7531 public Uri getDataUri() {
7532 return mDataUri;
7533 }
7534
Kenny Guya0f6de82018-04-06 16:20:16 +01007535 /**
7536 * @return True if the message was generated from
7537 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7538 * @hide
7539 */
7540 public boolean isRemoteInputHistory() {
7541 return mRemoteInputHistory;
7542 }
7543
Beverlye98937a2018-11-15 10:18:57 -05007544 /**
7545 * @hide
7546 */
7547 @VisibleForTesting
7548 public Bundle toBundle() {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007549 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007550 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007551 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04007552 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007553 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04007554 if (mSender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007555 // Legacy listeners need this
7556 bundle.putCharSequence(KEY_SENDER, mSender.getName());
7557 bundle.putParcelable(KEY_SENDER_PERSON, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04007558 }
7559 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007560 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04007561 }
7562 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007563 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04007564 }
Shane Brennan5a871862017-03-11 13:14:17 -08007565 if (mExtras != null) {
7566 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
7567 }
Kenny Guya0f6de82018-04-06 16:20:16 +01007568 if (mRemoteInputHistory) {
7569 bundle.putBoolean(KEY_REMOTE_INPUT_HISTORY, mRemoteInputHistory);
7570 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007571 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04007572 }
7573
Alex Hillsd9b04d92016-04-11 16:38:16 -04007574 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
7575 Bundle[] bundles = new Bundle[messages.size()];
7576 final int N = messages.size();
7577 for (int i = 0; i < N; i++) {
7578 bundles[i] = messages.get(i).toBundle();
7579 }
7580 return bundles;
7581 }
7582
Selim Cinek88188f22017-09-19 16:46:56 -07007583 /**
7584 * @return A list of messages read from the bundles.
7585 *
7586 * @hide
7587 */
7588 public static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
7589 if (bundles == null) {
7590 return new ArrayList<>();
7591 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007592 List<Message> messages = new ArrayList<>(bundles.length);
7593 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07007594 if (bundles[i] instanceof Bundle) {
7595 Message message = getMessageFromBundle((Bundle)bundles[i]);
7596 if (message != null) {
7597 messages.add(message);
7598 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007599 }
7600 }
7601 return messages;
7602 }
7603
Selim Cinekb0dc61b2018-05-22 18:49:36 -07007604 /**
7605 * @return The message that is stored in the bundle or null if the message couldn't be
7606 * resolved.
7607 *
7608 * @hide
7609 */
7610 @Nullable
7611 public static Message getMessageFromBundle(Bundle bundle) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007612 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07007613 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007614 return null;
7615 } else {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007616
7617 Person senderPerson = bundle.getParcelable(KEY_SENDER_PERSON);
7618 if (senderPerson == null) {
7619 // Legacy apps that use compat don't actually provide the sender objects
7620 // We need to fix the compat version to provide people / use
7621 // the native api instead
7622 CharSequence senderName = bundle.getCharSequence(KEY_SENDER);
7623 if (senderName != null) {
Selim Cinek9acd6732018-03-23 16:39:02 -07007624 senderPerson = new Person.Builder().setName(senderName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007625 }
7626 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007627 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
Selim Cinekcb8b9852017-12-15 18:01:52 -08007628 bundle.getLong(KEY_TIMESTAMP),
Kenny Guya0f6de82018-04-06 16:20:16 +01007629 senderPerson,
7630 bundle.getBoolean(KEY_REMOTE_INPUT_HISTORY, false));
Alex Hillsd9b04d92016-04-11 16:38:16 -04007631 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
7632 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007633 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
7634 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04007635 }
Shane Brennan5a871862017-03-11 13:14:17 -08007636 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
7637 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
7638 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007639 return message;
7640 }
7641 } catch (ClassCastException e) {
7642 return null;
7643 }
7644 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007645 }
7646 }
7647
7648 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04007649 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08007650 *
Robert Ly91c5ce32014-06-08 15:37:00 -07007651 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04007652 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07007653 * Notification notif = new Notification.Builder(mContext)
7654 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
7655 * .setContentText(subject)
7656 * .setSmallIcon(R.drawable.new_mail)
7657 * .setLargeIcon(aBitmap)
7658 * .setStyle(new Notification.InboxStyle()
7659 * .addLine(str1)
7660 * .addLine(str2)
7661 * .setContentTitle(&quot;&quot;)
7662 * .setSummaryText(&quot;+3 more&quot;))
7663 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04007664 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08007665 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04007666 * @see Notification#bigContentView
7667 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007668 public static class InboxStyle extends Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07007669
7670 /**
7671 * The number of lines of remote input history allowed until we start reducing lines.
7672 */
7673 private static final int NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION = 1;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007674 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
7675
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007676 public InboxStyle() {
7677 }
7678
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007679 /**
7680 * @deprecated use {@code InboxStyle()}.
7681 */
7682 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04007683 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007684 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04007685 }
7686
Chris Wrend6297db2012-05-03 16:20:13 -04007687 /**
7688 * Overrides ContentTitle in the big form of the template.
7689 * This defaults to the value passed to setContentTitle().
7690 */
7691 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007692 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04007693 return this;
7694 }
7695
7696 /**
7697 * Set the first line of text after the detail section in the big form of the template.
7698 */
7699 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007700 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04007701 return this;
7702 }
7703
Chris Wren0bd664d2012-08-01 13:56:56 -04007704 /**
7705 * Append a line to the digest section of the Inbox notification.
7706 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04007707 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007708 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04007709 return this;
7710 }
7711
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007712 /**
7713 * @hide
7714 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05007715 public ArrayList<CharSequence> getLines() {
7716 return mTexts;
7717 }
7718
7719 /**
7720 * @hide
7721 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007722 public void addExtras(Bundle extras) {
7723 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007724
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007725 CharSequence[] a = new CharSequence[mTexts.size()];
7726 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
7727 }
7728
Christoph Studer4600f9b2014-07-22 22:44:43 +02007729 /**
7730 * @hide
7731 */
7732 @Override
7733 protected void restoreFromExtras(Bundle extras) {
7734 super.restoreFromExtras(extras);
7735
7736 mTexts.clear();
7737 if (extras.containsKey(EXTRA_TEXT_LINES)) {
7738 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
7739 }
7740 }
7741
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007742 /**
7743 * @hide
7744 */
7745 public RemoteViews makeBigContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007746 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder).text(null);
Selim Cinek384804b2018-04-18 14:31:07 +08007747 TemplateBindResult result = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007748 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource(), p, result);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007749
Chris Wrend6297db2012-05-03 16:20:13 -04007750 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 -04007751 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04007752
Chris Wren4ed80d52012-05-17 09:30:03 -04007753 // Make sure all rows are gone in case we reuse a view.
7754 for (int rowId : rowIds) {
7755 contentView.setViewVisibility(rowId, View.GONE);
7756 }
7757
Daniel Sandler879c5e02012-04-17 16:46:51 -04007758 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07007759 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7760 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08007761 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07007762 int onlyViewId = 0;
7763 int maxRows = rowIds.length;
7764 if (mBuilder.mActions.size() > 0) {
7765 maxRows--;
7766 }
Selim Cinekbee4e072018-05-21 22:06:43 -07007767 CharSequence[] remoteInputHistory = mBuilder.mN.extras.getCharSequenceArray(
7768 EXTRA_REMOTE_INPUT_HISTORY);
7769 if (remoteInputHistory != null
7770 && remoteInputHistory.length > NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION) {
7771 // Let's remove some messages to make room for the remote input history.
7772 // 1 is always able to fit, but let's remove them if they are 2 or 3
7773 int numRemoteInputs = Math.min(remoteInputHistory.length,
7774 MAX_REMOTE_INPUT_HISTORY_LINES);
7775 int totalNumRows = mTexts.size() + numRemoteInputs
7776 - NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION;
7777 if (totalNumRows > maxRows) {
7778 int overflow = totalNumRows - maxRows;
7779 if (mTexts.size() > maxRows) {
7780 // Heuristic: if the Texts don't fit anyway, we'll rather drop the last
7781 // few messages, even with the remote input
7782 maxRows -= overflow;
7783 } else {
7784 // otherwise we drop the first messages
7785 i = overflow;
7786 }
7787 }
7788 }
Selim Cinek07c80172016-04-21 16:40:47 -07007789 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007790 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07007791 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007792 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07007793 contentView.setTextViewText(rowIds[i],
7794 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007795 mBuilder.setTextViewColorSecondary(contentView, rowIds[i], p);
Selim Cinek07c80172016-04-21 16:40:47 -07007796 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek384804b2018-04-18 14:31:07 +08007797 handleInboxImageMargin(contentView, rowIds[i], first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007798 result.getIconMarginEnd());
Selim Cinek07c80172016-04-21 16:40:47 -07007799 if (first) {
7800 onlyViewId = rowIds[i];
7801 } else {
7802 onlyViewId = 0;
7803 }
Selim Cinek247fa012016-02-18 09:50:48 -08007804 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007805 }
7806 i++;
7807 }
Selim Cinek07c80172016-04-21 16:40:47 -07007808 if (onlyViewId != 0) {
7809 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
7810 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7811 R.dimen.notification_text_margin_top);
7812 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
7813 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007814
Daniel Sandler879c5e02012-04-17 16:46:51 -04007815 return contentView;
7816 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007817
Julia Reynolds7217dc92018-03-07 12:12:09 -05007818 /**
7819 * @hide
7820 */
7821 @Override
7822 public boolean areNotificationsVisiblyDifferent(Style other) {
7823 if (other == null || getClass() != other.getClass()) {
7824 return true;
7825 }
7826 InboxStyle newS = (InboxStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04007827
7828 final ArrayList<CharSequence> myLines = getLines();
7829 final ArrayList<CharSequence> newLines = newS.getLines();
7830 final int n = myLines.size();
7831 if (n != newLines.size()) {
7832 return true;
7833 }
7834
7835 for (int i = 0; i < n; i++) {
7836 if (!Objects.equals(
7837 String.valueOf(myLines.get(i)),
7838 String.valueOf(newLines.get(i)))) {
7839 return true;
7840 }
7841 }
7842 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05007843 }
7844
Selim Cinek384804b2018-04-18 14:31:07 +08007845 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007846 int marginEndValue) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08007847 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08007848 if (first) {
7849 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
7850 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
7851 boolean hasProgress = max != 0 || ind;
Selim Cinek384804b2018-04-18 14:31:07 +08007852 if (!hasProgress) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08007853 endMargin = marginEndValue;
Selim Cinek247fa012016-02-18 09:50:48 -08007854 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007855 }
Selim Cinek1c72fa02018-04-23 18:00:54 +08007856 contentView.setViewLayoutMarginEnd(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08007857 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04007858 }
Dan Sandler842dd772014-05-15 09:36:47 -04007859
7860 /**
7861 * Notification style for media playback notifications.
7862 *
7863 * In the expanded form, {@link Notification#bigContentView}, up to 5
7864 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04007865 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04007866 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
7867 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
7868 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08007869 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04007870 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
7871 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01007872 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04007873 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08007874 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007875 * Notifications created with MediaStyle will have their category set to
7876 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
7877 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08007878 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07007879 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
7880 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04007881 * the System UI can identify this as a notification representing an active media session
7882 * and respond accordingly (by showing album artwork in the lockscreen, for example).
7883 *
Selim Cinek99104832017-01-25 14:47:33 -08007884 * <p>
7885 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
7886 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
7887 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
7888 * <p>
7889 *
Dan Sandler842dd772014-05-15 09:36:47 -04007890 * To use this style with your Notification, feed it to
7891 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7892 * <pre class="prettyprint">
7893 * Notification noti = new Notification.Builder()
7894 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01007895 * .setContentTitle(&quot;Track title&quot;)
7896 * .setContentText(&quot;Artist - Album&quot;)
7897 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007898 * .setStyle(<b>new Notification.MediaStyle()</b>
7899 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04007900 * .build();
7901 * </pre>
7902 *
7903 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08007904 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04007905 */
7906 public static class MediaStyle extends Style {
Gus Prevas9cc96602018-10-11 11:24:23 -04007907 // Changing max media buttons requires also changing templates
7908 // (notification_template_material_media and notification_template_material_big_media).
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007909 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04007910 static final int MAX_MEDIA_BUTTONS = 5;
Gus Prevas9cc96602018-10-11 11:24:23 -04007911 @IdRes private static final int[] MEDIA_BUTTON_IDS = {
7912 R.id.action0,
7913 R.id.action1,
7914 R.id.action2,
7915 R.id.action3,
7916 R.id.action4,
7917 };
Dan Sandler842dd772014-05-15 09:36:47 -04007918
7919 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07007920 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04007921
7922 public MediaStyle() {
7923 }
7924
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007925 /**
7926 * @deprecated use {@code MediaStyle()}.
7927 */
7928 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04007929 public MediaStyle(Builder builder) {
7930 setBuilder(builder);
7931 }
7932
7933 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007934 * 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 -04007935 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007936 *
7937 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04007938 */
7939 public MediaStyle setShowActionsInCompactView(int...actions) {
7940 mActionsToShowInCompact = actions;
7941 return this;
7942 }
7943
7944 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07007945 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
7946 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04007947 */
Jeff Browndba34ba2014-06-24 20:46:03 -07007948 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04007949 mToken = token;
7950 return this;
7951 }
7952
Christoph Studer4600f9b2014-07-22 22:44:43 +02007953 /**
7954 * @hide
7955 */
Dan Sandler842dd772014-05-15 09:36:47 -04007956 @Override
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007957 @UnsupportedAppUsage
Dan Sandler842dd772014-05-15 09:36:47 -04007958 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02007959 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007960 if (wip.category == null) {
7961 wip.category = Notification.CATEGORY_TRANSPORT;
7962 }
Dan Sandler842dd772014-05-15 09:36:47 -04007963 return wip;
7964 }
7965
Christoph Studer4600f9b2014-07-22 22:44:43 +02007966 /**
7967 * @hide
7968 */
7969 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007970 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007971 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007972 }
7973
7974 /**
7975 * @hide
7976 */
7977 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007978 public RemoteViews makeBigContentView() {
7979 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007980 }
7981
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007982 /**
7983 * @hide
7984 */
7985 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007986 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007987 RemoteViews expanded = makeMediaBigContentView();
7988 return expanded != null ? expanded : makeMediaContentView();
7989 }
7990
Dan Sandler842dd772014-05-15 09:36:47 -04007991 /** @hide */
7992 @Override
7993 public void addExtras(Bundle extras) {
7994 super.addExtras(extras);
7995
7996 if (mToken != null) {
7997 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
7998 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01007999 if (mActionsToShowInCompact != null) {
8000 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
8001 }
Dan Sandler842dd772014-05-15 09:36:47 -04008002 }
8003
Christoph Studer4600f9b2014-07-22 22:44:43 +02008004 /**
8005 * @hide
8006 */
8007 @Override
8008 protected void restoreFromExtras(Bundle extras) {
8009 super.restoreFromExtras(extras);
8010
8011 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
8012 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
8013 }
8014 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
8015 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
8016 }
8017 }
8018
Julia Reynolds7217dc92018-03-07 12:12:09 -05008019 /**
8020 * @hide
8021 */
8022 @Override
8023 public boolean areNotificationsVisiblyDifferent(Style other) {
8024 if (other == null || getClass() != other.getClass()) {
8025 return true;
8026 }
8027 // All fields to compare are on the Notification object
8028 return false;
8029 }
8030
Gus Prevas9cc96602018-10-11 11:24:23 -04008031 private void bindMediaActionButton(RemoteViews container, @IdRes int buttonId,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008032 Action action, StandardTemplateParams p) {
Dan Sandler842dd772014-05-15 09:36:47 -04008033 final boolean tombstone = (action.actionIntent == null);
Gus Prevas9cc96602018-10-11 11:24:23 -04008034 container.setViewVisibility(buttonId, View.VISIBLE);
8035 container.setImageViewIcon(buttonId, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07008036
8037 // If the action buttons should not be tinted, then just use the default
8038 // notification color. Otherwise, just use the passed-in color.
Gus Prevas9cc96602018-10-11 11:24:23 -04008039 Resources resources = mBuilder.mContext.getResources();
8040 Configuration currentConfig = resources.getConfiguration();
Lucas Dupinf03e7522018-06-25 16:21:13 -07008041 boolean inNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
8042 == Configuration.UI_MODE_NIGHT_YES;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008043 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized(p)
8044 ? getActionColor(p)
Lucas Dupina291d192018-06-07 13:59:42 -07008045 : ContrastColorUtil.resolveColor(mBuilder.mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07008046 Notification.COLOR_DEFAULT, inNightMode);
Anthony Chenad4d1582017-04-10 16:07:58 -07008047
Gus Prevas9cc96602018-10-11 11:24:23 -04008048 container.setDrawableTint(buttonId, false, tintColor,
Sunny Goyal5b153922017-09-21 21:00:36 -07008049 PorterDuff.Mode.SRC_ATOP);
Gus Prevas9cc96602018-10-11 11:24:23 -04008050
8051 final TypedArray typedArray = mBuilder.mContext.obtainStyledAttributes(
8052 new int[]{ android.R.attr.colorControlHighlight });
8053 int rippleAlpha = Color.alpha(typedArray.getColor(0, 0));
8054 typedArray.recycle();
8055 int rippleColor = Color.argb(rippleAlpha, Color.red(tintColor), Color.green(tintColor),
8056 Color.blue(tintColor));
8057 container.setRippleDrawableColor(buttonId, ColorStateList.valueOf(rippleColor));
8058
Dan Sandler842dd772014-05-15 09:36:47 -04008059 if (!tombstone) {
Gus Prevas9cc96602018-10-11 11:24:23 -04008060 container.setOnClickPendingIntent(buttonId, action.actionIntent);
Dan Sandler842dd772014-05-15 09:36:47 -04008061 }
Gus Prevas9cc96602018-10-11 11:24:23 -04008062 container.setContentDescription(buttonId, action.title);
Dan Sandler842dd772014-05-15 09:36:47 -04008063 }
8064
8065 private RemoteViews makeMediaContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008066 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).fillTextsFrom(
8067 mBuilder);
Dan Sandler842dd772014-05-15 09:36:47 -04008068 RemoteViews view = mBuilder.applyStandardTemplate(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008069 R.layout.notification_template_material_media, p,
Selim Cinek384804b2018-04-18 14:31:07 +08008070 null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04008071
8072 final int numActions = mBuilder.mActions.size();
Gus Prevas9cc96602018-10-11 11:24:23 -04008073 final int numActionsToShow = mActionsToShowInCompact == null
Dan Sandler842dd772014-05-15 09:36:47 -04008074 ? 0
8075 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Gus Prevas9cc96602018-10-11 11:24:23 -04008076 if (numActionsToShow > numActions) {
8077 throw new IllegalArgumentException(String.format(
8078 "setShowActionsInCompactView: action %d out of bounds (max %d)",
8079 numActions, numActions - 1));
8080 }
8081 for (int i = 0; i < MAX_MEDIA_BUTTONS_IN_COMPACT; i++) {
8082 if (i < numActionsToShow) {
Dan Sandler842dd772014-05-15 09:36:47 -04008083 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008084 bindMediaActionButton(view, MEDIA_BUTTON_IDS[i], action, p);
Gus Prevas9cc96602018-10-11 11:24:23 -04008085 } else {
8086 view.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04008087 }
8088 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08008089 handleImage(view);
8090 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008091 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07008092 if (mBuilder.mN.hasLargeIcon()) {
Selim Cinek384804b2018-04-18 14:31:07 +08008093 endMargin = R.dimen.notification_media_image_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08008094 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008095 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04008096 return view;
8097 }
8098
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008099 private int getActionColor(StandardTemplateParams p) {
8100 return mBuilder.isColorized(p) ? mBuilder.getPrimaryTextColor(p)
8101 : mBuilder.resolveContrastColor(p);
Selim Cinek99104832017-01-25 14:47:33 -08008102 }
8103
Dan Sandler842dd772014-05-15 09:36:47 -04008104 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008105 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008106 // Dont add an expanded view if there is no more content to be revealed
8107 int actionsInCompact = mActionsToShowInCompact == null
8108 ? 0
8109 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07008110 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008111 return null;
8112 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008113 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).fillTextsFrom(
8114 mBuilder);
Selim Cinek5bf069a2015-11-10 19:14:27 -05008115 RemoteViews big = mBuilder.applyStandardTemplate(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008116 R.layout.notification_template_material_big_media, p , null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04008117
Gus Prevas9cc96602018-10-11 11:24:23 -04008118 for (int i = 0; i < MAX_MEDIA_BUTTONS; i++) {
8119 if (i < actionCount) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008120 bindMediaActionButton(big, MEDIA_BUTTON_IDS[i], mBuilder.mActions.get(i), p);
Gus Prevas9cc96602018-10-11 11:24:23 -04008121 } else {
8122 big.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04008123 }
8124 }
linanson2bcd4032019-01-14 15:22:04 +08008125 bindMediaActionButton(big, R.id.media_seamless, new Action(R.drawable.ic_media_seamless,
8126 mBuilder.mContext.getString(
8127 com.android.internal.R.string.ext_media_seamless_action), null), p);
8128 big.setViewVisibility(R.id.media_seamless, View.GONE);
Selim Cinek5bf069a2015-11-10 19:14:27 -05008129 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04008130 return big;
8131 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008132
Selim Cinek5bf069a2015-11-10 19:14:27 -05008133 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07008134 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008135 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
8136 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008137 }
8138 }
8139
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008140 /**
8141 * @hide
8142 */
8143 @Override
8144 protected boolean hasProgress() {
8145 return false;
8146 }
Dan Sandler842dd772014-05-15 09:36:47 -04008147 }
Griff Hazen61a9e862014-05-22 16:05:19 -07008148
Selim Cinek593610c2016-02-16 18:42:57 -08008149 /**
8150 * Notification style for custom views that are decorated by the system
8151 *
8152 * <p>Instead of providing a notification that is completely custom, a developer can set this
8153 * style and still obtain system decorations like the notification header with the expand
8154 * affordance and actions.
8155 *
8156 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8157 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8158 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8159 * corresponding custom views to display.
8160 *
8161 * To use this style with your Notification, feed it to
8162 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8163 * <pre class="prettyprint">
8164 * Notification noti = new Notification.Builder()
8165 * .setSmallIcon(R.drawable.ic_stat_player)
8166 * .setLargeIcon(albumArtBitmap))
8167 * .setCustomContentView(contentView);
8168 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
8169 * .build();
8170 * </pre>
8171 */
8172 public static class DecoratedCustomViewStyle extends Style {
8173
8174 public DecoratedCustomViewStyle() {
8175 }
8176
Selim Cinek593610c2016-02-16 18:42:57 -08008177 /**
8178 * @hide
8179 */
8180 public boolean displayCustomViewInline() {
8181 return true;
8182 }
8183
8184 /**
8185 * @hide
8186 */
8187 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008188 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008189 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
8190 }
8191
8192 /**
8193 * @hide
8194 */
8195 @Override
8196 public RemoteViews makeBigContentView() {
8197 return makeDecoratedBigContentView();
8198 }
8199
8200 /**
8201 * @hide
8202 */
8203 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008204 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008205 return makeDecoratedHeadsUpContentView();
8206 }
8207
Selim Cinek593610c2016-02-16 18:42:57 -08008208 private RemoteViews makeDecoratedHeadsUpContentView() {
8209 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
8210 ? mBuilder.mN.contentView
8211 : mBuilder.mN.headsUpContentView;
8212 if (mBuilder.mActions.size() == 0) {
8213 return makeStandardTemplateWithCustomContent(headsUpContentView);
8214 }
Selim Cinek384804b2018-04-18 14:31:07 +08008215 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008216 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008217 mBuilder.getBigBaseLayoutResource(), result);
8218 buildIntoRemoteViewContent(remoteViews, headsUpContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008219 return remoteViews;
8220 }
8221
Selim Cinek593610c2016-02-16 18:42:57 -08008222 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
Selim Cinek384804b2018-04-18 14:31:07 +08008223 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008224 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08008225 mBuilder.getBaseLayoutResource(), result);
8226 buildIntoRemoteViewContent(remoteViews, customContent, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008227 return remoteViews;
8228 }
8229
Selim Cinek593610c2016-02-16 18:42:57 -08008230 private RemoteViews makeDecoratedBigContentView() {
8231 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
8232 ? mBuilder.mN.contentView
8233 : mBuilder.mN.bigContentView;
8234 if (mBuilder.mActions.size() == 0) {
8235 return makeStandardTemplateWithCustomContent(bigContentView);
8236 }
Selim Cinek384804b2018-04-18 14:31:07 +08008237 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008238 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008239 mBuilder.getBigBaseLayoutResource(), result);
8240 buildIntoRemoteViewContent(remoteViews, bigContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008241 return remoteViews;
8242 }
Selim Cinek247fa012016-02-18 09:50:48 -08008243
8244 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
Selim Cinek384804b2018-04-18 14:31:07 +08008245 RemoteViews customContent, TemplateBindResult result) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08008246 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008247 // Need to clone customContent before adding, because otherwise it can no longer be
8248 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008249 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07008250 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
8251 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Sunny Goyalc12d31c2018-11-12 16:29:18 -08008252 remoteViews.addFlags(RemoteViews.FLAG_REAPPLY_DISALLOWED);
Adrian Roos5081c0d2016-02-26 16:04:19 -08008253 }
Selim Cinek247fa012016-02-18 09:50:48 -08008254 // also update the end margin if there is an image
Selim Cinek384804b2018-04-18 14:31:07 +08008255 Resources resources = mBuilder.mContext.getResources();
8256 int endMargin = resources.getDimensionPixelSize(
Selim Cinek1c72fa02018-04-23 18:00:54 +08008257 R.dimen.notification_content_margin_end) + result.getIconMarginEnd();
Selim Cinek384804b2018-04-18 14:31:07 +08008258 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08008259 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05008260
8261 /**
8262 * @hide
8263 */
8264 @Override
8265 public boolean areNotificationsVisiblyDifferent(Style other) {
8266 if (other == null || getClass() != other.getClass()) {
8267 return true;
8268 }
8269 // Comparison done for all custom RemoteViews, independent of style
8270 return false;
8271 }
Selim Cinek593610c2016-02-16 18:42:57 -08008272 }
8273
Selim Cinek03eb3b72016-02-18 10:39:45 -08008274 /**
8275 * Notification style for media custom views that are decorated by the system
8276 *
8277 * <p>Instead of providing a media notification that is completely custom, a developer can set
8278 * this style and still obtain system decorations like the notification header with the expand
8279 * affordance and actions.
8280 *
8281 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8282 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8283 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8284 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08008285 * <p>
8286 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
8287 * notification by using {@link Notification.Builder#setColorized(boolean)}.
8288 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08008289 * To use this style with your Notification, feed it to
8290 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8291 * <pre class="prettyprint">
8292 * Notification noti = new Notification.Builder()
8293 * .setSmallIcon(R.drawable.ic_stat_player)
8294 * .setLargeIcon(albumArtBitmap))
8295 * .setCustomContentView(contentView);
8296 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
8297 * .setMediaSession(mySession))
8298 * .build();
8299 * </pre>
8300 *
8301 * @see android.app.Notification.DecoratedCustomViewStyle
8302 * @see android.app.Notification.MediaStyle
8303 */
8304 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
8305
8306 public DecoratedMediaCustomViewStyle() {
8307 }
8308
Selim Cinek03eb3b72016-02-18 10:39:45 -08008309 /**
8310 * @hide
8311 */
8312 public boolean displayCustomViewInline() {
8313 return true;
8314 }
8315
8316 /**
8317 * @hide
8318 */
8319 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008320 public RemoteViews makeContentView(boolean increasedHeight) {
8321 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008322 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8323 mBuilder.mN.contentView);
8324 }
8325
8326 /**
8327 * @hide
8328 */
8329 @Override
8330 public RemoteViews makeBigContentView() {
8331 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
8332 ? mBuilder.mN.bigContentView
8333 : mBuilder.mN.contentView;
8334 return makeBigContentViewWithCustomContent(customRemoteView);
8335 }
8336
8337 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
8338 RemoteViews remoteViews = super.makeBigContentView();
8339 if (remoteViews != null) {
8340 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
8341 customRemoteView);
8342 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08008343 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008344 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8345 customRemoteView);
8346 } else {
8347 return null;
8348 }
8349 }
8350
8351 /**
8352 * @hide
8353 */
8354 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008355 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08008356 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
8357 ? mBuilder.mN.headsUpContentView
8358 : mBuilder.mN.contentView;
8359 return makeBigContentViewWithCustomContent(customRemoteView);
8360 }
8361
Julia Reynolds7217dc92018-03-07 12:12:09 -05008362 /**
8363 * @hide
8364 */
8365 @Override
8366 public boolean areNotificationsVisiblyDifferent(Style other) {
8367 if (other == null || getClass() != other.getClass()) {
8368 return true;
8369 }
8370 // Comparison done for all custom RemoteViews, independent of style
8371 return false;
8372 }
8373
Selim Cinek03eb3b72016-02-18 10:39:45 -08008374 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
8375 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08008376 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008377 // Need to clone customContent before adding, because otherwise it can no longer be
8378 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008379 customContent = customContent.clone();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008380 customContent.overrideTextColors(mBuilder.getPrimaryTextColor(mBuilder.mParams));
Selim Cinekf91017e2016-03-14 12:25:09 -07008381 remoteViews.removeAllViews(id);
8382 remoteViews.addView(id, customContent);
Sunny Goyalc12d31c2018-11-12 16:29:18 -08008383 remoteViews.addFlags(RemoteViews.FLAG_REAPPLY_DISALLOWED);
Adrian Roos5081c0d2016-02-26 16:04:19 -08008384 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08008385 return remoteViews;
8386 }
8387 }
8388
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008389 /**
8390 * Encapsulates the information needed to display a notification as a bubble.
8391 *
8392 * <p>A bubble is used to display app content in a floating window over the existing
8393 * foreground activity. A bubble has a collapsed state represented by an icon,
8394 * {@link BubbleMetadata.Builder#setIcon(Icon)} and an expanded state which is populated
8395 * via {@link BubbleMetadata.Builder#setIntent(PendingIntent)}.</p>
8396 *
8397 * <b>Notifications with a valid and allowed bubble will display in collapsed state
8398 * outside of the notification shade on unlocked devices. When a user interacts with the
8399 * collapsed bubble, the bubble intent will be invoked and displayed.</b>
8400 *
8401 * @see Notification.Builder#setBubbleMetadata(BubbleMetadata)
8402 */
8403 public static final class BubbleMetadata implements Parcelable {
8404
8405 private PendingIntent mPendingIntent;
8406 private CharSequence mTitle;
8407 private Icon mIcon;
8408 private int mDesiredHeight;
Mady Mellor0157ff22019-01-24 15:31:48 -08008409 private int mFlags;
8410
8411 /**
8412 * If set and the app creating the bubble is in the foreground, the bubble will be posted
8413 * in its expanded state, with the contents of {@link #getIntent()} in a floating window.
8414 *
8415 * <p>If the app creating the bubble is not in the foreground this flag has no effect.</p>
8416 *
8417 * <p>Generally this flag should only be set if the user has performed an action to request
8418 * or create a bubble.</p>
8419 */
8420 private static final int FLAG_AUTO_EXPAND_BUBBLE = 0x00000001;
8421
8422 /**
8423 * If set and the app creating the bubble is in the foreground, the bubble will be posted
8424 * <b>without</b> the associated notification in the notification shade. Subsequent update
8425 * notifications to this bubble will post a notification in the shade.
8426 *
8427 * <p>If the app creating the bubble is not in the foreground this flag has no effect.</p>
8428 *
8429 * <p>Generally this flag should only be set if the user has performed an action to request
8430 * or create a bubble.</p>
8431 */
8432 private static final int FLAG_SUPPRESS_INITIAL_NOTIFICATION = 0x00000002;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008433
8434 private BubbleMetadata(PendingIntent intent, CharSequence title, Icon icon, int height) {
8435 mPendingIntent = intent;
8436 mTitle = title;
8437 mIcon = icon;
8438 mDesiredHeight = height;
8439 }
8440
8441 private BubbleMetadata(Parcel in) {
8442 mPendingIntent = PendingIntent.CREATOR.createFromParcel(in);
8443 mTitle = in.readCharSequence();
8444 mIcon = Icon.CREATOR.createFromParcel(in);
8445 mDesiredHeight = in.readInt();
Mady Mellor0157ff22019-01-24 15:31:48 -08008446 mFlags = in.readInt();
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008447 }
8448
8449 /**
8450 * @return the pending intent used to populate the floating window for this bubble.
8451 */
8452 public PendingIntent getIntent() {
8453 return mPendingIntent;
8454 }
8455
8456 /**
8457 * @return the title that will appear along with the app content defined by
8458 * {@link #getIntent()} for this bubble.
8459 */
8460 public CharSequence getTitle() {
8461 return mTitle;
8462 }
8463
8464 /**
8465 * @return the icon that will be displayed for this bubble when it is collapsed.
8466 */
8467 public Icon getIcon() {
8468 return mIcon;
8469 }
8470
8471 /**
8472 * @return the ideal height for the floating window that app content defined by
8473 * {@link #getIntent()} for this bubble.
8474 */
8475 public int getDesiredHeight() {
8476 return mDesiredHeight;
8477 }
8478
Mady Mellor0157ff22019-01-24 15:31:48 -08008479 /**
8480 * @return whether this bubble should auto expand when it is posted.
8481 *
8482 * @see BubbleMetadata.Builder#setAutoExpandBubble(boolean)
8483 */
8484 public boolean getAutoExpandBubble() {
8485 return (mFlags & FLAG_AUTO_EXPAND_BUBBLE) != 0;
8486 }
8487
8488 /**
8489 * @return whether this bubble should suppress the initial notification when it is posted.
8490 *
8491 * @see BubbleMetadata.Builder#setSuppressInitialNotification(boolean)
8492 */
8493 public boolean getSuppressInitialNotification() {
8494 return (mFlags & FLAG_SUPPRESS_INITIAL_NOTIFICATION) != 0;
8495 }
8496
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008497 public static final Parcelable.Creator<BubbleMetadata> CREATOR =
8498 new Parcelable.Creator<BubbleMetadata>() {
8499
8500 @Override
8501 public BubbleMetadata createFromParcel(Parcel source) {
8502 return new BubbleMetadata(source);
8503 }
8504
8505 @Override
8506 public BubbleMetadata[] newArray(int size) {
8507 return new BubbleMetadata[size];
8508 }
8509 };
8510
8511 @Override
8512 public int describeContents() {
8513 return 0;
8514 }
8515
8516 @Override
8517 public void writeToParcel(Parcel out, int flags) {
8518 mPendingIntent.writeToParcel(out, 0);
8519 out.writeCharSequence(mTitle);
8520 mIcon.writeToParcel(out, 0);
8521 out.writeInt(mDesiredHeight);
Mady Mellor0157ff22019-01-24 15:31:48 -08008522 out.writeInt(mFlags);
8523 }
8524
8525 private void setFlags(int flags) {
8526 mFlags = flags;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008527 }
8528
8529 /**
8530 * Builder to construct a {@link BubbleMetadata} object.
8531 */
8532 public static class Builder {
8533
8534 private PendingIntent mPendingIntent;
8535 private CharSequence mTitle;
8536 private Icon mIcon;
8537 private int mDesiredHeight;
Mady Mellor0157ff22019-01-24 15:31:48 -08008538 private int mFlags;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008539
8540 /**
8541 * Constructs a new builder object.
8542 */
8543 public Builder() {
8544 }
8545
8546 /**
8547 * Sets the intent that will be used when the bubble is expanded. This will display the
8548 * app content in a floating window over the existing foreground activity.
8549 */
8550 public BubbleMetadata.Builder setIntent(PendingIntent intent) {
8551 if (intent == null) {
8552 throw new IllegalArgumentException("Bubble requires non-null pending intent");
8553 }
8554 mPendingIntent = intent;
8555 return this;
8556 }
8557
8558 /**
8559 * Sets the title that will appear along with the app content for this bubble.
8560 *
8561 * <p>A title is required and should expect to fit on a single line and make sense when
8562 * shown with the content defined by {@link #setIntent(PendingIntent)}.</p>
8563 */
8564 public BubbleMetadata.Builder setTitle(CharSequence title) {
8565 if (TextUtils.isEmpty(title)) {
8566 throw new IllegalArgumentException("Bubbles require non-null or empty title");
8567 }
8568 mTitle = title;
8569 return this;
8570 }
8571
8572 /**
8573 * Sets the icon that will represent the bubble when it is collapsed.
8574 *
8575 * <p>An icon is required and should be representative of the content within the bubble.
8576 * If your app produces multiple bubbles, the image should be unique for each of them.
8577 * </p>
8578 */
8579 public BubbleMetadata.Builder setIcon(Icon icon) {
8580 if (icon == null) {
8581 throw new IllegalArgumentException("Bubbles require non-null icon");
8582 }
8583 mIcon = icon;
8584 return this;
8585 }
8586
8587 /**
8588 * Sets the desired height for the app content defined by
8589 * {@link #setIntent(PendingIntent)}, this height may not be respected if there is not
8590 * enough space on the screen or if the provided height is too small to be useful.
8591 */
8592 public BubbleMetadata.Builder setDesiredHeight(int height) {
8593 mDesiredHeight = Math.max(height, 0);
8594 return this;
8595 }
8596
8597 /**
Mady Mellor0157ff22019-01-24 15:31:48 -08008598 * If set and the app creating the bubble is in the foreground, the bubble will be
8599 * posted in its expanded state, with the contents of {@link #getIntent()} in a
8600 * floating window.
8601 *
8602 * <p>If the app creating the bubble is not in the foreground this flag has no effect.
8603 * </p>
8604 *
8605 * <p>Generally this flag should only be set if the user has performed an action to
8606 * request or create a bubble.</p>
8607 */
8608 public BubbleMetadata.Builder setAutoExpandBubble(boolean shouldExpand) {
8609 setFlag(FLAG_AUTO_EXPAND_BUBBLE, shouldExpand);
8610 return this;
8611 }
8612
8613 /**
8614 * If set and the app creating the bubble is in the foreground, the bubble will be
8615 * posted <b>without</b> the associated notification in the notification shade.
8616 * Subsequent update notifications to this bubble will post a notification in the shade.
8617 *
8618 * <p>If the app creating the bubble is not in the foreground this flag has no effect.
8619 * </p>
8620 *
8621 * <p>Generally this flag should only be set if the user has performed an action to
8622 * request or create a bubble.</p>
8623 */
8624 public BubbleMetadata.Builder setSuppressInitialNotification(
8625 boolean shouldSupressNotif) {
8626 setFlag(FLAG_SUPPRESS_INITIAL_NOTIFICATION, shouldSupressNotif);
8627 return this;
8628 }
8629
8630 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008631 * Creates the {@link BubbleMetadata} defined by this builder.
8632 * <p>Will throw {@link IllegalStateException} if required fields have not been set
8633 * on this builder.</p>
8634 */
8635 public BubbleMetadata build() {
8636 if (mPendingIntent == null) {
8637 throw new IllegalStateException("Must supply pending intent to bubble");
8638 }
8639 if (TextUtils.isEmpty(mTitle)) {
8640 throw new IllegalStateException("Must supply a title for the bubble");
8641 }
8642 if (mIcon == null) {
8643 throw new IllegalStateException("Must supply an icon for the bubble");
8644 }
Mady Mellor0157ff22019-01-24 15:31:48 -08008645 BubbleMetadata data = new BubbleMetadata(mPendingIntent, mTitle, mIcon,
8646 mDesiredHeight);
8647 data.setFlags(mFlags);
8648 return data;
8649 }
8650
8651 /**
8652 * @hide
8653 */
8654 public BubbleMetadata.Builder setFlag(int mask, boolean value) {
8655 if (value) {
8656 mFlags |= mask;
8657 } else {
8658 mFlags &= ~mask;
8659 }
8660 return this;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008661 }
8662 }
8663 }
8664
8665
Christoph Studer4600f9b2014-07-22 22:44:43 +02008666 // When adding a new Style subclass here, don't forget to update
8667 // Builder.getNotificationStyleClass.
8668
Griff Hazen61a9e862014-05-22 16:05:19 -07008669 /**
8670 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
8671 * metadata or change options on a notification builder.
8672 */
8673 public interface Extender {
8674 /**
8675 * Apply this extender to a notification builder.
8676 * @param builder the builder to be modified.
8677 * @return the build object for chaining.
8678 */
8679 public Builder extend(Builder builder);
8680 }
8681
8682 /**
8683 * Helper class to add wearable extensions to notifications.
8684 * <p class="note"> See
8685 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
8686 * for Android Wear</a> for more information on how to use this class.
8687 * <p>
8688 * To create a notification with wearable extensions:
8689 * <ol>
8690 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
8691 * properties.
8692 * <li>Create a {@link android.app.Notification.WearableExtender}.
8693 * <li>Set wearable-specific properties using the
8694 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
8695 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
8696 * notification.
8697 * <li>Post the notification to the notification system with the
8698 * {@code NotificationManager.notify(...)} methods.
8699 * </ol>
8700 *
8701 * <pre class="prettyprint">
8702 * Notification notif = new Notification.Builder(mContext)
8703 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
8704 * .setContentText(subject)
8705 * .setSmallIcon(R.drawable.new_mail)
8706 * .extend(new Notification.WearableExtender()
8707 * .setContentIcon(R.drawable.new_mail))
8708 * .build();
8709 * NotificationManager notificationManger =
8710 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
8711 * notificationManger.notify(0, notif);</pre>
8712 *
8713 * <p>Wearable extensions can be accessed on an existing notification by using the
8714 * {@code WearableExtender(Notification)} constructor,
8715 * and then using the {@code get} methods to access values.
8716 *
8717 * <pre class="prettyprint">
8718 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
8719 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07008720 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008721 */
8722 public static final class WearableExtender implements Extender {
8723 /**
8724 * Sentinel value for an action index that is unset.
8725 */
8726 public static final int UNSET_ACTION_INDEX = -1;
8727
8728 /**
8729 * Size value for use with {@link #setCustomSizePreset} to show this notification with
8730 * default sizing.
8731 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07008732 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07008733 * on their content.
Gus Prevasd7363752018-09-18 14:35:15 -04008734 *
8735 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008736 */
Gus Prevasd7363752018-09-18 14:35:15 -04008737 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008738 public static final int SIZE_DEFAULT = 0;
8739
8740 /**
8741 * Size value for use with {@link #setCustomSizePreset} to show this notification
8742 * with an extra small size.
8743 * <p>This value is only applicable for custom display notifications created using
8744 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008745 *
8746 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008747 */
Gus Prevasd7363752018-09-18 14:35:15 -04008748 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008749 public static final int SIZE_XSMALL = 1;
8750
8751 /**
8752 * Size value for use with {@link #setCustomSizePreset} to show this notification
8753 * with a small size.
8754 * <p>This value is only applicable for custom display notifications created using
8755 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008756 *
8757 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008758 */
Gus Prevasd7363752018-09-18 14:35:15 -04008759 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008760 public static final int SIZE_SMALL = 2;
8761
8762 /**
8763 * Size value for use with {@link #setCustomSizePreset} to show this notification
8764 * with a medium size.
8765 * <p>This value is only applicable for custom display notifications created using
8766 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008767 *
8768 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008769 */
Gus Prevasd7363752018-09-18 14:35:15 -04008770 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008771 public static final int SIZE_MEDIUM = 3;
8772
8773 /**
8774 * Size value for use with {@link #setCustomSizePreset} to show this notification
8775 * with a large size.
8776 * <p>This value is only applicable for custom display notifications created using
8777 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008778 *
8779 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008780 */
Gus Prevasd7363752018-09-18 14:35:15 -04008781 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008782 public static final int SIZE_LARGE = 4;
8783
Griff Hazend5f11f92014-05-27 15:40:09 -07008784 /**
8785 * Size value for use with {@link #setCustomSizePreset} to show this notification
8786 * full screen.
8787 * <p>This value is only applicable for custom display notifications created using
8788 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008789 *
8790 * @deprecated Display intents are no longer supported.
Griff Hazend5f11f92014-05-27 15:40:09 -07008791 */
Gus Prevasd7363752018-09-18 14:35:15 -04008792 @Deprecated
Griff Hazend5f11f92014-05-27 15:40:09 -07008793 public static final int SIZE_FULL_SCREEN = 5;
8794
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008795 /**
8796 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
8797 * short amount of time when this notification is displayed on the screen. This
8798 * is the default value.
Gus Prevasd7363752018-09-18 14:35:15 -04008799 *
8800 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008801 */
Gus Prevasd7363752018-09-18 14:35:15 -04008802 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008803 public static final int SCREEN_TIMEOUT_SHORT = 0;
8804
8805 /**
8806 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
8807 * for a longer amount of time when this notification is displayed on the screen.
Gus Prevasd7363752018-09-18 14:35:15 -04008808 *
8809 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008810 */
Gus Prevasd7363752018-09-18 14:35:15 -04008811 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008812 public static final int SCREEN_TIMEOUT_LONG = -1;
8813
Griff Hazen61a9e862014-05-22 16:05:19 -07008814 /** Notification extra which contains wearable extensions */
8815 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
8816
Pete Gastaf6781d2014-10-07 15:17:05 -04008817 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07008818 private static final String KEY_ACTIONS = "actions";
8819 private static final String KEY_FLAGS = "flags";
8820 private static final String KEY_DISPLAY_INTENT = "displayIntent";
8821 private static final String KEY_PAGES = "pages";
8822 private static final String KEY_BACKGROUND = "background";
8823 private static final String KEY_CONTENT_ICON = "contentIcon";
8824 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
8825 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
8826 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
8827 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
8828 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008829 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04008830 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008831 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07008832
8833 // Flags bitwise-ored to mFlags
8834 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
8835 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
8836 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
8837 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008838 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04008839 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04008840 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07008841
8842 // Default value for flags integer
8843 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
8844
8845 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
8846 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
8847
8848 private ArrayList<Action> mActions = new ArrayList<Action>();
8849 private int mFlags = DEFAULT_FLAGS;
8850 private PendingIntent mDisplayIntent;
8851 private ArrayList<Notification> mPages = new ArrayList<Notification>();
8852 private Bitmap mBackground;
8853 private int mContentIcon;
8854 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
8855 private int mContentActionIndex = UNSET_ACTION_INDEX;
8856 private int mCustomSizePreset = SIZE_DEFAULT;
8857 private int mCustomContentHeight;
8858 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008859 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04008860 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008861 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07008862
8863 /**
8864 * Create a {@link android.app.Notification.WearableExtender} with default
8865 * options.
8866 */
8867 public WearableExtender() {
8868 }
8869
8870 public WearableExtender(Notification notif) {
8871 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
8872 if (wearableBundle != null) {
8873 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
8874 if (actions != null) {
8875 mActions.addAll(actions);
8876 }
8877
8878 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
8879 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
8880
8881 Notification[] pages = getNotificationArrayFromBundle(
8882 wearableBundle, KEY_PAGES);
8883 if (pages != null) {
8884 Collections.addAll(mPages, pages);
8885 }
8886
8887 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
8888 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
8889 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
8890 DEFAULT_CONTENT_ICON_GRAVITY);
8891 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
8892 UNSET_ACTION_INDEX);
8893 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
8894 SIZE_DEFAULT);
8895 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
8896 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008897 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04008898 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008899 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07008900 }
8901 }
8902
8903 /**
8904 * Apply wearable extensions to a notification that is being built. This is typically
8905 * called by the {@link android.app.Notification.Builder#extend} method of
8906 * {@link android.app.Notification.Builder}.
8907 */
8908 @Override
8909 public Notification.Builder extend(Notification.Builder builder) {
8910 Bundle wearableBundle = new Bundle();
8911
8912 if (!mActions.isEmpty()) {
8913 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
8914 }
8915 if (mFlags != DEFAULT_FLAGS) {
8916 wearableBundle.putInt(KEY_FLAGS, mFlags);
8917 }
8918 if (mDisplayIntent != null) {
8919 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
8920 }
8921 if (!mPages.isEmpty()) {
8922 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
8923 new Notification[mPages.size()]));
8924 }
8925 if (mBackground != null) {
8926 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
8927 }
8928 if (mContentIcon != 0) {
8929 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
8930 }
8931 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
8932 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
8933 }
8934 if (mContentActionIndex != UNSET_ACTION_INDEX) {
8935 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
8936 mContentActionIndex);
8937 }
8938 if (mCustomSizePreset != SIZE_DEFAULT) {
8939 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
8940 }
8941 if (mCustomContentHeight != 0) {
8942 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
8943 }
8944 if (mGravity != DEFAULT_GRAVITY) {
8945 wearableBundle.putInt(KEY_GRAVITY, mGravity);
8946 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008947 if (mHintScreenTimeout != 0) {
8948 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
8949 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04008950 if (mDismissalId != null) {
8951 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
8952 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008953 if (mBridgeTag != null) {
8954 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
8955 }
Griff Hazen61a9e862014-05-22 16:05:19 -07008956
8957 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
8958 return builder;
8959 }
8960
8961 @Override
8962 public WearableExtender clone() {
8963 WearableExtender that = new WearableExtender();
8964 that.mActions = new ArrayList<Action>(this.mActions);
8965 that.mFlags = this.mFlags;
8966 that.mDisplayIntent = this.mDisplayIntent;
8967 that.mPages = new ArrayList<Notification>(this.mPages);
8968 that.mBackground = this.mBackground;
8969 that.mContentIcon = this.mContentIcon;
8970 that.mContentIconGravity = this.mContentIconGravity;
8971 that.mContentActionIndex = this.mContentActionIndex;
8972 that.mCustomSizePreset = this.mCustomSizePreset;
8973 that.mCustomContentHeight = this.mCustomContentHeight;
8974 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008975 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04008976 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008977 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07008978 return that;
8979 }
8980
8981 /**
8982 * Add a wearable action to this notification.
8983 *
8984 * <p>When wearable actions are added using this method, the set of actions that
8985 * show on a wearable device splits from devices that only show actions added
8986 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
8987 * of which actions display on different devices.
8988 *
8989 * @param action the action to add to this notification
8990 * @return this object for method chaining
8991 * @see android.app.Notification.Action
8992 */
8993 public WearableExtender addAction(Action action) {
8994 mActions.add(action);
8995 return this;
8996 }
8997
8998 /**
8999 * Adds wearable actions to this notification.
9000 *
9001 * <p>When wearable actions are added using this method, the set of actions that
9002 * show on a wearable device splits from devices that only show actions added
9003 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
9004 * of which actions display on different devices.
9005 *
9006 * @param actions the actions to add to this notification
9007 * @return this object for method chaining
9008 * @see android.app.Notification.Action
9009 */
9010 public WearableExtender addActions(List<Action> actions) {
9011 mActions.addAll(actions);
9012 return this;
9013 }
9014
9015 /**
9016 * Clear all wearable actions present on this builder.
9017 * @return this object for method chaining.
9018 * @see #addAction
9019 */
9020 public WearableExtender clearActions() {
9021 mActions.clear();
9022 return this;
9023 }
9024
9025 /**
9026 * Get the wearable actions present on this notification.
9027 */
9028 public List<Action> getActions() {
9029 return mActions;
9030 }
9031
9032 /**
9033 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07009034 * this notification. The {@link PendingIntent} provided should be for an activity.
9035 *
9036 * <pre class="prettyprint">
9037 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
9038 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
9039 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
9040 * Notification notif = new Notification.Builder(context)
9041 * .extend(new Notification.WearableExtender()
9042 * .setDisplayIntent(displayPendingIntent)
9043 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
9044 * .build();</pre>
9045 *
9046 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07009047 * should have an empty task affinity. It is also recommended to use the device
9048 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07009049 *
9050 * <p>Example AndroidManifest.xml entry:
9051 * <pre class="prettyprint">
9052 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
9053 * android:exported=&quot;true&quot;
9054 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07009055 * android:taskAffinity=&quot;&quot;
9056 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07009057 *
9058 * @param intent the {@link PendingIntent} for an activity
9059 * @return this object for method chaining
9060 * @see android.app.Notification.WearableExtender#getDisplayIntent
Gus Prevasd7363752018-09-18 14:35:15 -04009061 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009062 */
Gus Prevasd7363752018-09-18 14:35:15 -04009063 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009064 public WearableExtender setDisplayIntent(PendingIntent intent) {
9065 mDisplayIntent = intent;
9066 return this;
9067 }
9068
9069 /**
9070 * Get the intent to launch inside of an activity view when displaying this
9071 * notification. This {@code PendingIntent} should be for an activity.
Gus Prevasd7363752018-09-18 14:35:15 -04009072 *
9073 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009074 */
Gus Prevasd7363752018-09-18 14:35:15 -04009075 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009076 public PendingIntent getDisplayIntent() {
9077 return mDisplayIntent;
9078 }
9079
9080 /**
9081 * Add an additional page of content to display with this notification. The current
9082 * notification forms the first page, and pages added using this function form
9083 * subsequent pages. This field can be used to separate a notification into multiple
9084 * sections.
9085 *
9086 * @param page the notification to add as another page
9087 * @return this object for method chaining
9088 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04009089 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009090 */
Gus Prevasd7363752018-09-18 14:35:15 -04009091 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009092 public WearableExtender addPage(Notification page) {
9093 mPages.add(page);
9094 return this;
9095 }
9096
9097 /**
9098 * Add additional pages of content to display with this notification. The current
9099 * notification forms the first page, and pages added using this function form
9100 * subsequent pages. This field can be used to separate a notification into multiple
9101 * sections.
9102 *
9103 * @param pages a list of notifications
9104 * @return this object for method chaining
9105 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04009106 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009107 */
Gus Prevasd7363752018-09-18 14:35:15 -04009108 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009109 public WearableExtender addPages(List<Notification> pages) {
9110 mPages.addAll(pages);
9111 return this;
9112 }
9113
9114 /**
9115 * Clear all additional pages present on this builder.
9116 * @return this object for method chaining.
9117 * @see #addPage
Gus Prevasd7363752018-09-18 14:35:15 -04009118 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009119 */
Gus Prevasd7363752018-09-18 14:35:15 -04009120 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009121 public WearableExtender clearPages() {
9122 mPages.clear();
9123 return this;
9124 }
9125
9126 /**
9127 * Get the array of additional pages of content for displaying this notification. The
9128 * current notification forms the first page, and elements within this array form
9129 * subsequent pages. This field can be used to separate a notification into multiple
9130 * sections.
9131 * @return the pages for this notification
Gus Prevasd7363752018-09-18 14:35:15 -04009132 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009133 */
Gus Prevasd7363752018-09-18 14:35:15 -04009134 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009135 public List<Notification> getPages() {
9136 return mPages;
9137 }
9138
9139 /**
9140 * Set a background image to be displayed behind the notification content.
9141 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
9142 * will work with any notification style.
9143 *
9144 * @param background the background bitmap
9145 * @return this object for method chaining
9146 * @see android.app.Notification.WearableExtender#getBackground
Gus Prevasd7363752018-09-18 14:35:15 -04009147 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009148 */
Gus Prevasd7363752018-09-18 14:35:15 -04009149 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009150 public WearableExtender setBackground(Bitmap background) {
9151 mBackground = background;
9152 return this;
9153 }
9154
9155 /**
9156 * Get a background image to be displayed behind the notification content.
9157 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
9158 * will work with any notification style.
9159 *
9160 * @return the background image
9161 * @see android.app.Notification.WearableExtender#setBackground
Gus Prevasd7363752018-09-18 14:35:15 -04009162 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009163 */
Gus Prevasd7363752018-09-18 14:35:15 -04009164 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009165 public Bitmap getBackground() {
9166 return mBackground;
9167 }
9168
9169 /**
9170 * Set an icon that goes with the content of this notification.
9171 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009172 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009173 public WearableExtender setContentIcon(int icon) {
9174 mContentIcon = icon;
9175 return this;
9176 }
9177
9178 /**
9179 * Get an icon that goes with the content of this notification.
9180 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009181 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009182 public int getContentIcon() {
9183 return mContentIcon;
9184 }
9185
9186 /**
9187 * Set the gravity that the content icon should have within the notification display.
9188 * Supported values include {@link android.view.Gravity#START} and
9189 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
9190 * @see #setContentIcon
9191 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009192 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009193 public WearableExtender setContentIconGravity(int contentIconGravity) {
9194 mContentIconGravity = contentIconGravity;
9195 return this;
9196 }
9197
9198 /**
9199 * Get the gravity that the content icon should have within the notification display.
9200 * Supported values include {@link android.view.Gravity#START} and
9201 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
9202 * @see #getContentIcon
9203 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009204 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009205 public int getContentIconGravity() {
9206 return mContentIconGravity;
9207 }
9208
9209 /**
Gus Prevasd7363752018-09-18 14:35:15 -04009210 * Set an action from this notification's actions as the primary action. If the action has a
9211 * {@link RemoteInput} associated with it, shortcuts to the options for that input are shown
9212 * directly on the notification.
Griff Hazen14f57992014-05-26 09:07:14 -07009213 *
Gus Prevasd7363752018-09-18 14:35:15 -04009214 * @param actionIndex The index of the primary action.
Griff Hazen14f57992014-05-26 09:07:14 -07009215 * If wearable actions were added to the main notification, this index
9216 * will apply to that list, otherwise it will apply to the regular
9217 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07009218 */
9219 public WearableExtender setContentAction(int actionIndex) {
9220 mContentActionIndex = actionIndex;
9221 return this;
9222 }
9223
9224 /**
Gus Prevasd7363752018-09-18 14:35:15 -04009225 * Get the index of the notification action, if any, that was specified as the primary
9226 * action.
Griff Hazen14f57992014-05-26 09:07:14 -07009227 *
9228 * <p>If wearable specific actions were added to the main notification, this index will
9229 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07009230 *
9231 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07009232 */
9233 public int getContentAction() {
9234 return mContentActionIndex;
9235 }
9236
9237 /**
9238 * Set the gravity that this notification should have within the available viewport space.
9239 * Supported values include {@link android.view.Gravity#TOP},
9240 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
9241 * The default value is {@link android.view.Gravity#BOTTOM}.
9242 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009243 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009244 public WearableExtender setGravity(int gravity) {
9245 mGravity = gravity;
9246 return this;
9247 }
9248
9249 /**
9250 * Get the gravity that this notification should have within the available viewport space.
9251 * Supported values include {@link android.view.Gravity#TOP},
9252 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
9253 * The default value is {@link android.view.Gravity#BOTTOM}.
9254 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009255 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009256 public int getGravity() {
9257 return mGravity;
9258 }
9259
9260 /**
9261 * Set the custom size preset for the display of this notification out of the available
9262 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
9263 * {@link #SIZE_LARGE}.
9264 * <p>Some custom size presets are only applicable for custom display notifications created
9265 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
9266 * documentation for the preset in question. See also
9267 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
9268 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009269 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009270 public WearableExtender setCustomSizePreset(int sizePreset) {
9271 mCustomSizePreset = sizePreset;
9272 return this;
9273 }
9274
9275 /**
9276 * Get the custom size preset for the display of this notification out of the available
9277 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
9278 * {@link #SIZE_LARGE}.
9279 * <p>Some custom size presets are only applicable for custom display notifications created
9280 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
9281 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
9282 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009283 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009284 public int getCustomSizePreset() {
9285 return mCustomSizePreset;
9286 }
9287
9288 /**
9289 * Set the custom height in pixels for the display of this notification's content.
9290 * <p>This option is only available for custom display notifications created
9291 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
9292 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
9293 * {@link #getCustomContentHeight}.
9294 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009295 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009296 public WearableExtender setCustomContentHeight(int height) {
9297 mCustomContentHeight = height;
9298 return this;
9299 }
9300
9301 /**
9302 * Get the custom height in pixels for the display of this notification's content.
9303 * <p>This option is only available for custom display notifications created
9304 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
9305 * {@link #setCustomContentHeight}.
9306 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009307 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009308 public int getCustomContentHeight() {
9309 return mCustomContentHeight;
9310 }
9311
9312 /**
9313 * Set whether the scrolling position for the contents of this notification should start
9314 * at the bottom of the contents instead of the top when the contents are too long to
9315 * display within the screen. Default is false (start scroll at the top).
9316 */
9317 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
9318 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
9319 return this;
9320 }
9321
9322 /**
9323 * Get whether the scrolling position for the contents of this notification should start
9324 * at the bottom of the contents instead of the top when the contents are too long to
9325 * display within the screen. Default is false (start scroll at the top).
9326 */
9327 public boolean getStartScrollBottom() {
9328 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
9329 }
9330
9331 /**
9332 * Set whether the content intent is available when the wearable device is not connected
9333 * to a companion device. The user can still trigger this intent when the wearable device
9334 * is offline, but a visual hint will indicate that the content intent may not be available.
9335 * Defaults to true.
9336 */
9337 public WearableExtender setContentIntentAvailableOffline(
9338 boolean contentIntentAvailableOffline) {
9339 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
9340 return this;
9341 }
9342
9343 /**
9344 * Get whether the content intent is available when the wearable device is not connected
9345 * to a companion device. The user can still trigger this intent when the wearable device
9346 * is offline, but a visual hint will indicate that the content intent may not be available.
9347 * Defaults to true.
9348 */
9349 public boolean getContentIntentAvailableOffline() {
9350 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
9351 }
9352
9353 /**
9354 * Set a hint that this notification's icon should not be displayed.
9355 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
9356 * @return this object for method chaining
9357 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009358 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009359 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
9360 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
9361 return this;
9362 }
9363
9364 /**
9365 * Get a hint that this notification's icon should not be displayed.
9366 * @return {@code true} if this icon should not be displayed, false otherwise.
9367 * The default value is {@code false} if this was never set.
9368 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009369 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009370 public boolean getHintHideIcon() {
9371 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
9372 }
9373
9374 /**
9375 * Set a visual hint that only the background image of this notification should be
9376 * displayed, and other semantic content should be hidden. This hint is only applicable
9377 * to sub-pages added using {@link #addPage}.
9378 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009379 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009380 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
9381 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
9382 return this;
9383 }
9384
9385 /**
9386 * Get a visual hint that only the background image of this notification should be
9387 * displayed, and other semantic content should be hidden. This hint is only applicable
9388 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
9389 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009390 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009391 public boolean getHintShowBackgroundOnly() {
9392 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
9393 }
9394
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009395 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009396 * Set a hint that this notification's background should not be clipped if possible,
9397 * and should instead be resized to fully display on the screen, retaining the aspect
9398 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009399 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
9400 * @return this object for method chaining
9401 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009402 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009403 public WearableExtender setHintAvoidBackgroundClipping(
9404 boolean hintAvoidBackgroundClipping) {
9405 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
9406 return this;
9407 }
9408
9409 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009410 * Get a hint that this notification's background should not be clipped if possible,
9411 * and should instead be resized to fully display on the screen, retaining the aspect
9412 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009413 * @return {@code true} if it's ok if the background is clipped on the screen, false
9414 * otherwise. The default value is {@code false} if this was never set.
9415 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009416 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009417 public boolean getHintAvoidBackgroundClipping() {
9418 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
9419 }
9420
9421 /**
9422 * Set a hint that the screen should remain on for at least this duration when
9423 * this notification is displayed on the screen.
9424 * @param timeout The requested screen timeout in milliseconds. Can also be either
9425 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9426 * @return this object for method chaining
9427 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009428 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009429 public WearableExtender setHintScreenTimeout(int timeout) {
9430 mHintScreenTimeout = timeout;
9431 return this;
9432 }
9433
9434 /**
9435 * Get the duration, in milliseconds, that the screen should remain on for
9436 * when this notification is displayed.
9437 * @return the duration in milliseconds if > 0, or either one of the sentinel values
9438 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9439 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009440 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009441 public int getHintScreenTimeout() {
9442 return mHintScreenTimeout;
9443 }
9444
Alex Hills9ab3a232016-04-05 14:54:56 -04009445 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04009446 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
9447 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9448 * qr codes, as well as other simple black-and-white tickets.
9449 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
9450 * @return this object for method chaining
Gus Prevasd7363752018-09-18 14:35:15 -04009451 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009452 */
Gus Prevasd7363752018-09-18 14:35:15 -04009453 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009454 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
9455 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
9456 return this;
9457 }
9458
9459 /**
9460 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
9461 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9462 * qr codes, as well as other simple black-and-white tickets.
9463 * @return {@code true} if it should be displayed in ambient, false otherwise
9464 * otherwise. The default value is {@code false} if this was never set.
Gus Prevasd7363752018-09-18 14:35:15 -04009465 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009466 */
Gus Prevasd7363752018-09-18 14:35:15 -04009467 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009468 public boolean getHintAmbientBigPicture() {
9469 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
9470 }
9471
9472 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04009473 * Set a hint that this notification's content intent will launch an {@link Activity}
9474 * directly, telling the platform that it can generate the appropriate transitions.
9475 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
9476 * an activity and transitions should be generated, false otherwise.
9477 * @return this object for method chaining
9478 */
9479 public WearableExtender setHintContentIntentLaunchesActivity(
9480 boolean hintContentIntentLaunchesActivity) {
9481 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
9482 return this;
9483 }
9484
9485 /**
9486 * Get a hint that this notification's content intent will launch an {@link Activity}
9487 * directly, telling the platform that it can generate the appropriate transitions
9488 * @return {@code true} if the content intent will launch an activity and transitions should
9489 * be generated, false otherwise. The default value is {@code false} if this was never set.
9490 */
9491 public boolean getHintContentIntentLaunchesActivity() {
9492 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
9493 }
9494
Nadia Benbernou948627e2016-04-14 14:41:08 -04009495 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009496 * Sets the dismissal id for this notification. If a notification is posted with a
9497 * dismissal id, then when that notification is canceled, notifications on other wearables
9498 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04009499 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009500 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04009501 * @param dismissalId the dismissal id of the notification.
9502 * @return this object for method chaining
9503 */
9504 public WearableExtender setDismissalId(String dismissalId) {
9505 mDismissalId = dismissalId;
9506 return this;
9507 }
9508
9509 /**
9510 * Returns the dismissal id of the notification.
9511 * @return the dismissal id of the notification or null if it has not been set.
9512 */
9513 public String getDismissalId() {
9514 return mDismissalId;
9515 }
9516
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009517 /**
9518 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
9519 * posted from a phone to provide finer-grained control on what notifications are bridged
9520 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
9521 * Features to Notifications</a> for more information.
9522 * @param bridgeTag the bridge tag of the notification.
9523 * @return this object for method chaining
9524 */
9525 public WearableExtender setBridgeTag(String bridgeTag) {
9526 mBridgeTag = bridgeTag;
9527 return this;
9528 }
9529
9530 /**
9531 * Returns the bridge tag of the notification.
9532 * @return the bridge tag or null if not present.
9533 */
9534 public String getBridgeTag() {
9535 return mBridgeTag;
9536 }
9537
Griff Hazen61a9e862014-05-22 16:05:19 -07009538 private void setFlag(int mask, boolean value) {
9539 if (value) {
9540 mFlags |= mask;
9541 } else {
9542 mFlags &= ~mask;
9543 }
9544 }
9545 }
9546
9547 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009548 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
9549 * with car extensions:
9550 *
9551 * <ol>
9552 * <li>Create an {@link Notification.Builder}, setting any desired
9553 * properties.
9554 * <li>Create a {@link CarExtender}.
9555 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
9556 * {@link CarExtender}.
9557 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9558 * to apply the extensions to a notification.
9559 * </ol>
9560 *
9561 * <pre class="prettyprint">
9562 * Notification notification = new Notification.Builder(context)
9563 * ...
9564 * .extend(new CarExtender()
9565 * .set*(...))
9566 * .build();
9567 * </pre>
9568 *
9569 * <p>Car extensions can be accessed on an existing notification by using the
9570 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
9571 * to access values.
9572 */
9573 public static final class CarExtender implements Extender {
9574 private static final String TAG = "CarExtender";
9575
9576 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
9577 private static final String EXTRA_LARGE_ICON = "large_icon";
9578 private static final String EXTRA_CONVERSATION = "car_conversation";
9579 private static final String EXTRA_COLOR = "app_color";
9580
9581 private Bitmap mLargeIcon;
9582 private UnreadConversation mUnreadConversation;
9583 private int mColor = Notification.COLOR_DEFAULT;
9584
9585 /**
9586 * Create a {@link CarExtender} with default options.
9587 */
9588 public CarExtender() {
9589 }
9590
9591 /**
9592 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
9593 *
9594 * @param notif The notification from which to copy options.
9595 */
9596 public CarExtender(Notification notif) {
9597 Bundle carBundle = notif.extras == null ?
9598 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
9599 if (carBundle != null) {
9600 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
9601 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
9602
9603 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
9604 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
9605 }
9606 }
9607
9608 /**
9609 * Apply car extensions to a notification that is being built. This is typically called by
9610 * the {@link Notification.Builder#extend(Notification.Extender)}
9611 * method of {@link Notification.Builder}.
9612 */
9613 @Override
9614 public Notification.Builder extend(Notification.Builder builder) {
9615 Bundle carExtensions = new Bundle();
9616
9617 if (mLargeIcon != null) {
9618 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
9619 }
9620 if (mColor != Notification.COLOR_DEFAULT) {
9621 carExtensions.putInt(EXTRA_COLOR, mColor);
9622 }
9623
9624 if (mUnreadConversation != null) {
9625 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
9626 carExtensions.putBundle(EXTRA_CONVERSATION, b);
9627 }
9628
9629 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
9630 return builder;
9631 }
9632
9633 /**
9634 * Sets the accent color to use when Android Auto presents the notification.
9635 *
9636 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
9637 * to accent the displayed notification. However, not all colors are acceptable in an
9638 * automotive setting. This method can be used to override the color provided in the
9639 * notification in such a situation.
9640 */
Tor Norbye80756e32015-03-02 09:39:27 -08009641 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009642 mColor = color;
9643 return this;
9644 }
9645
9646 /**
9647 * Gets the accent color.
9648 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04009649 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009650 */
Tor Norbye80756e32015-03-02 09:39:27 -08009651 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009652 public int getColor() {
9653 return mColor;
9654 }
9655
9656 /**
9657 * Sets the large icon of the car notification.
9658 *
9659 * If no large icon is set in the extender, Android Auto will display the icon
9660 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
9661 *
9662 * @param largeIcon The large icon to use in the car notification.
9663 * @return This object for method chaining.
9664 */
9665 public CarExtender setLargeIcon(Bitmap largeIcon) {
9666 mLargeIcon = largeIcon;
9667 return this;
9668 }
9669
9670 /**
9671 * Gets the large icon used in this car notification, or null if no icon has been set.
9672 *
9673 * @return The large icon for the car notification.
9674 * @see CarExtender#setLargeIcon
9675 */
9676 public Bitmap getLargeIcon() {
9677 return mLargeIcon;
9678 }
9679
9680 /**
9681 * Sets the unread conversation in a message notification.
9682 *
9683 * @param unreadConversation The unread part of the conversation this notification conveys.
9684 * @return This object for method chaining.
9685 */
9686 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
9687 mUnreadConversation = unreadConversation;
9688 return this;
9689 }
9690
9691 /**
9692 * Returns the unread conversation conveyed by this notification.
9693 * @see #setUnreadConversation(UnreadConversation)
9694 */
9695 public UnreadConversation getUnreadConversation() {
9696 return mUnreadConversation;
9697 }
9698
9699 /**
9700 * A class which holds the unread messages from a conversation.
9701 */
9702 public static class UnreadConversation {
9703 private static final String KEY_AUTHOR = "author";
9704 private static final String KEY_TEXT = "text";
9705 private static final String KEY_MESSAGES = "messages";
9706 private static final String KEY_REMOTE_INPUT = "remote_input";
9707 private static final String KEY_ON_REPLY = "on_reply";
9708 private static final String KEY_ON_READ = "on_read";
9709 private static final String KEY_PARTICIPANTS = "participants";
9710 private static final String KEY_TIMESTAMP = "timestamp";
9711
9712 private final String[] mMessages;
9713 private final RemoteInput mRemoteInput;
9714 private final PendingIntent mReplyPendingIntent;
9715 private final PendingIntent mReadPendingIntent;
9716 private final String[] mParticipants;
9717 private final long mLatestTimestamp;
9718
9719 UnreadConversation(String[] messages, RemoteInput remoteInput,
9720 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
9721 String[] participants, long latestTimestamp) {
9722 mMessages = messages;
9723 mRemoteInput = remoteInput;
9724 mReadPendingIntent = readPendingIntent;
9725 mReplyPendingIntent = replyPendingIntent;
9726 mParticipants = participants;
9727 mLatestTimestamp = latestTimestamp;
9728 }
9729
9730 /**
9731 * Gets the list of messages conveyed by this notification.
9732 */
9733 public String[] getMessages() {
9734 return mMessages;
9735 }
9736
9737 /**
9738 * Gets the remote input that will be used to convey the response to a message list, or
9739 * null if no such remote input exists.
9740 */
9741 public RemoteInput getRemoteInput() {
9742 return mRemoteInput;
9743 }
9744
9745 /**
9746 * Gets the pending intent that will be triggered when the user replies to this
9747 * notification.
9748 */
9749 public PendingIntent getReplyPendingIntent() {
9750 return mReplyPendingIntent;
9751 }
9752
9753 /**
9754 * Gets the pending intent that Android Auto will send after it reads aloud all messages
9755 * in this object's message list.
9756 */
9757 public PendingIntent getReadPendingIntent() {
9758 return mReadPendingIntent;
9759 }
9760
9761 /**
9762 * Gets the participants in the conversation.
9763 */
9764 public String[] getParticipants() {
9765 return mParticipants;
9766 }
9767
9768 /**
9769 * Gets the firs participant in the conversation.
9770 */
9771 public String getParticipant() {
9772 return mParticipants.length > 0 ? mParticipants[0] : null;
9773 }
9774
9775 /**
9776 * Gets the timestamp of the conversation.
9777 */
9778 public long getLatestTimestamp() {
9779 return mLatestTimestamp;
9780 }
9781
9782 Bundle getBundleForUnreadConversation() {
9783 Bundle b = new Bundle();
9784 String author = null;
9785 if (mParticipants != null && mParticipants.length > 1) {
9786 author = mParticipants[0];
9787 }
9788 Parcelable[] messages = new Parcelable[mMessages.length];
9789 for (int i = 0; i < messages.length; i++) {
9790 Bundle m = new Bundle();
9791 m.putString(KEY_TEXT, mMessages[i]);
9792 m.putString(KEY_AUTHOR, author);
9793 messages[i] = m;
9794 }
9795 b.putParcelableArray(KEY_MESSAGES, messages);
9796 if (mRemoteInput != null) {
9797 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
9798 }
9799 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
9800 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
9801 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
9802 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
9803 return b;
9804 }
9805
9806 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
9807 if (b == null) {
9808 return null;
9809 }
9810 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
9811 String[] messages = null;
9812 if (parcelableMessages != null) {
9813 String[] tmp = new String[parcelableMessages.length];
9814 boolean success = true;
9815 for (int i = 0; i < tmp.length; i++) {
9816 if (!(parcelableMessages[i] instanceof Bundle)) {
9817 success = false;
9818 break;
9819 }
9820 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
9821 if (tmp[i] == null) {
9822 success = false;
9823 break;
9824 }
9825 }
9826 if (success) {
9827 messages = tmp;
9828 } else {
9829 return null;
9830 }
9831 }
9832
9833 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
9834 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
9835
9836 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
9837
9838 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
9839 if (participants == null || participants.length != 1) {
9840 return null;
9841 }
9842
9843 return new UnreadConversation(messages,
9844 remoteInput,
9845 onReply,
9846 onRead,
9847 participants, b.getLong(KEY_TIMESTAMP));
9848 }
9849 };
9850
9851 /**
9852 * Builder class for {@link CarExtender.UnreadConversation} objects.
9853 */
9854 public static class Builder {
9855 private final List<String> mMessages = new ArrayList<String>();
9856 private final String mParticipant;
9857 private RemoteInput mRemoteInput;
9858 private PendingIntent mReadPendingIntent;
9859 private PendingIntent mReplyPendingIntent;
9860 private long mLatestTimestamp;
9861
9862 /**
9863 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
9864 *
9865 * @param name The name of the other participant in the conversation.
9866 */
9867 public Builder(String name) {
9868 mParticipant = name;
9869 }
9870
9871 /**
9872 * Appends a new unread message to the list of messages for this conversation.
9873 *
9874 * The messages should be added from oldest to newest.
9875 *
9876 * @param message The text of the new unread message.
9877 * @return This object for method chaining.
9878 */
9879 public Builder addMessage(String message) {
9880 mMessages.add(message);
9881 return this;
9882 }
9883
9884 /**
9885 * Sets the pending intent and remote input which will convey the reply to this
9886 * notification.
9887 *
9888 * @param pendingIntent The pending intent which will be triggered on a reply.
9889 * @param remoteInput The remote input parcelable which will carry the reply.
9890 * @return This object for method chaining.
9891 *
9892 * @see CarExtender.UnreadConversation#getRemoteInput
9893 * @see CarExtender.UnreadConversation#getReplyPendingIntent
9894 */
9895 public Builder setReplyAction(
9896 PendingIntent pendingIntent, RemoteInput remoteInput) {
9897 mRemoteInput = remoteInput;
9898 mReplyPendingIntent = pendingIntent;
9899
9900 return this;
9901 }
9902
9903 /**
9904 * Sets the pending intent that will be sent once the messages in this notification
9905 * are read.
9906 *
9907 * @param pendingIntent The pending intent to use.
9908 * @return This object for method chaining.
9909 */
9910 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
9911 mReadPendingIntent = pendingIntent;
9912 return this;
9913 }
9914
9915 /**
9916 * Sets the timestamp of the most recent message in an unread conversation.
9917 *
9918 * If a messaging notification has been posted by your application and has not
9919 * yet been cancelled, posting a later notification with the same id and tag
9920 * but without a newer timestamp may result in Android Auto not displaying a
9921 * heads up notification for the later notification.
9922 *
9923 * @param timestamp The timestamp of the most recent message in the conversation.
9924 * @return This object for method chaining.
9925 */
9926 public Builder setLatestTimestamp(long timestamp) {
9927 mLatestTimestamp = timestamp;
9928 return this;
9929 }
9930
9931 /**
9932 * Builds a new unread conversation object.
9933 *
9934 * @return The new unread conversation object.
9935 */
9936 public UnreadConversation build() {
9937 String[] messages = mMessages.toArray(new String[mMessages.size()]);
9938 String[] participants = { mParticipant };
9939 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
9940 mReadPendingIntent, participants, mLatestTimestamp);
9941 }
9942 }
9943 }
9944
9945 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009946 * <p>Helper class to add Android TV extensions to notifications. To create a notification
9947 * with a TV extension:
9948 *
9949 * <ol>
9950 * <li>Create an {@link Notification.Builder}, setting any desired properties.
9951 * <li>Create a {@link TvExtender}.
9952 * <li>Set TV-specific properties using the {@code set} methods of
9953 * {@link TvExtender}.
9954 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9955 * to apply the extension to a notification.
9956 * </ol>
9957 *
9958 * <pre class="prettyprint">
9959 * Notification notification = new Notification.Builder(context)
9960 * ...
9961 * .extend(new TvExtender()
9962 * .set*(...))
9963 * .build();
9964 * </pre>
9965 *
9966 * <p>TV extensions can be accessed on an existing notification by using the
9967 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
9968 * to access values.
9969 *
9970 * @hide
9971 */
9972 @SystemApi
9973 public static final class TvExtender implements Extender {
9974 private static final String TAG = "TvExtender";
9975
9976 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
9977 private static final String EXTRA_FLAGS = "flags";
9978 private static final String EXTRA_CONTENT_INTENT = "content_intent";
9979 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009980 private static final String EXTRA_CHANNEL_ID = "channel_id";
Rhiannon Malia1a083932018-01-24 15:02:30 -08009981 private static final String EXTRA_SUPPRESS_SHOW_OVER_APPS = "suppressShowOverApps";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009982
9983 // Flags bitwise-ored to mFlags
9984 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
9985
9986 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009987 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009988 private PendingIntent mContentIntent;
9989 private PendingIntent mDeleteIntent;
Rhiannon Malia1a083932018-01-24 15:02:30 -08009990 private boolean mSuppressShowOverApps;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009991
9992 /**
9993 * Create a {@link TvExtender} with default options.
9994 */
9995 public TvExtender() {
9996 mFlags = FLAG_AVAILABLE_ON_TV;
9997 }
9998
9999 /**
10000 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
10001 *
10002 * @param notif The notification from which to copy options.
10003 */
10004 public TvExtender(Notification notif) {
10005 Bundle bundle = notif.extras == null ?
10006 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
10007 if (bundle != null) {
10008 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010009 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Rhiannon Malia1a083932018-01-24 15:02:30 -080010010 mSuppressShowOverApps = bundle.getBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010011 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
10012 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
10013 }
10014 }
10015
10016 /**
10017 * Apply a TV extension to a notification that is being built. This is typically called by
10018 * the {@link Notification.Builder#extend(Notification.Extender)}
10019 * method of {@link Notification.Builder}.
10020 */
10021 @Override
10022 public Notification.Builder extend(Notification.Builder builder) {
10023 Bundle bundle = new Bundle();
10024
10025 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010026 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Rhiannon Malia1a083932018-01-24 15:02:30 -080010027 bundle.putBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS, mSuppressShowOverApps);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010028 if (mContentIntent != null) {
10029 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
10030 }
10031
10032 if (mDeleteIntent != null) {
10033 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
10034 }
10035
10036 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
10037 return builder;
10038 }
10039
10040 /**
10041 * Returns true if this notification should be shown on TV. This method return true
10042 * if the notification was extended with a TvExtender.
10043 */
10044 public boolean isAvailableOnTv() {
10045 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
10046 }
10047
10048 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010049 * Specifies the channel the notification should be delivered on when shown on TV.
10050 * It can be different from the channel that the notification is delivered to when
10051 * posting on a non-TV device.
10052 */
10053 public TvExtender setChannel(String channelId) {
10054 mChannelId = channelId;
10055 return this;
10056 }
10057
10058 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -040010059 * Specifies the channel the notification should be delivered on when shown on TV.
10060 * It can be different from the channel that the notification is delivered to when
10061 * posting on a non-TV device.
10062 */
10063 public TvExtender setChannelId(String channelId) {
10064 mChannelId = channelId;
10065 return this;
10066 }
10067
Jeff Sharkey000ce802017-04-29 13:13:27 -060010068 /** @removed */
10069 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010070 public String getChannel() {
10071 return mChannelId;
10072 }
10073
10074 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -040010075 * Returns the id of the channel this notification posts to on TV.
10076 */
10077 public String getChannelId() {
10078 return mChannelId;
10079 }
10080
10081 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010082 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
10083 * If provided, it is used instead of the content intent specified
10084 * at the level of Notification.
10085 */
10086 public TvExtender setContentIntent(PendingIntent intent) {
10087 mContentIntent = intent;
10088 return this;
10089 }
10090
10091 /**
10092 * Returns the TV-specific content intent. If this method returns null, the
10093 * main content intent on the notification should be used.
10094 *
10095 * @see {@link Notification#contentIntent}
10096 */
10097 public PendingIntent getContentIntent() {
10098 return mContentIntent;
10099 }
10100
10101 /**
10102 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
10103 * by the user on TV. If provided, it is used instead of the delete intent specified
10104 * at the level of Notification.
10105 */
10106 public TvExtender setDeleteIntent(PendingIntent intent) {
10107 mDeleteIntent = intent;
10108 return this;
10109 }
10110
10111 /**
10112 * Returns the TV-specific delete intent. If this method returns null, the
10113 * main delete intent on the notification should be used.
10114 *
10115 * @see {@link Notification#deleteIntent}
10116 */
10117 public PendingIntent getDeleteIntent() {
10118 return mDeleteIntent;
10119 }
Rhiannon Malia1a083932018-01-24 15:02:30 -080010120
10121 /**
10122 * Specifies whether this notification should suppress showing a message over top of apps
10123 * outside of the launcher.
10124 */
10125 public TvExtender setSuppressShowOverApps(boolean suppress) {
10126 mSuppressShowOverApps = suppress;
10127 return this;
10128 }
10129
10130 /**
10131 * Returns true if this notification should not show messages over top of apps
10132 * outside of the launcher.
10133 */
10134 public boolean getSuppressShowOverApps() {
10135 return mSuppressShowOverApps;
10136 }
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010137 }
10138
10139 /**
Griff Hazen61a9e862014-05-22 16:05:19 -070010140 * Get an array of Notification objects from a parcelable array bundle field.
10141 * Update the bundle to have a typed array so fetches in the future don't need
10142 * to do an array copy.
10143 */
10144 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
10145 Parcelable[] array = bundle.getParcelableArray(key);
10146 if (array instanceof Notification[] || array == null) {
10147 return (Notification[]) array;
10148 }
10149 Notification[] typedArray = Arrays.copyOf(array, array.length,
10150 Notification[].class);
10151 bundle.putParcelableArray(key, typedArray);
10152 return typedArray;
10153 }
Christoph Studer4600f9b2014-07-22 22:44:43 +020010154
10155 private static class BuilderRemoteViews extends RemoteViews {
10156 public BuilderRemoteViews(Parcel parcel) {
10157 super(parcel);
10158 }
10159
Kenny Guy77320062014-08-27 21:37:15 +010010160 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
10161 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +020010162 }
10163
10164 @Override
10165 public BuilderRemoteViews clone() {
10166 Parcel p = Parcel.obtain();
10167 writeToParcel(p, 0);
10168 p.setDataPosition(0);
10169 BuilderRemoteViews brv = new BuilderRemoteViews(p);
10170 p.recycle();
10171 return brv;
10172 }
10173 }
Adrian Roos70d7aa32017-01-11 15:39:06 -080010174
Selim Cinek384804b2018-04-18 14:31:07 +080010175 /**
10176 * A result object where information about the template that was created is saved.
10177 */
10178 private static class TemplateBindResult {
Selim Cinek1c72fa02018-04-23 18:00:54 +080010179 int mIconMarginEnd;
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010180 boolean mRightIconContainerVisible;
Selim Cinek384804b2018-04-18 14:31:07 +080010181
10182 /**
Selim Cinek1c72fa02018-04-23 18:00:54 +080010183 * Get the margin end that needs to be added to any fields that may overlap
Selim Cinek384804b2018-04-18 14:31:07 +080010184 * with the right actions.
10185 */
Selim Cinek1c72fa02018-04-23 18:00:54 +080010186 public int getIconMarginEnd() {
10187 return mIconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +080010188 }
10189
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010190 /**
10191 * Is the icon container visible on the right size because of the reply button or the
10192 * right icon.
10193 */
10194 public boolean isRightIconContainerVisible() {
10195 return mRightIconContainerVisible;
10196 }
10197
Selim Cinek1c72fa02018-04-23 18:00:54 +080010198 public void setIconMarginEnd(int iconMarginEnd) {
10199 this.mIconMarginEnd = iconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +080010200 }
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010201
10202 public void setRightIconContainerVisible(boolean iconContainerVisible) {
10203 mRightIconContainerVisible = iconContainerVisible;
10204 }
Selim Cinek384804b2018-04-18 14:31:07 +080010205 }
10206
Adrian Roos70d7aa32017-01-11 15:39:06 -080010207 private static class StandardTemplateParams {
10208 boolean hasProgress = true;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010209 CharSequence title;
10210 CharSequence text;
Selim Cinekafeed292017-12-12 17:32:44 -080010211 CharSequence headerTextSecondary;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010212 CharSequence summaryText;
Selim Cinekbee4e072018-05-21 22:06:43 -070010213 int maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek88188f22017-09-19 16:46:56 -070010214 boolean hideLargeIcon;
Selim Cinek384804b2018-04-18 14:31:07 +080010215 boolean hideReplyIcon;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010216 boolean allowColorization = true;
10217 boolean forceDefaultColor = false;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010218
10219 final StandardTemplateParams reset() {
10220 hasProgress = true;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010221 title = null;
10222 text = null;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010223 summaryText = null;
Selim Cinekafeed292017-12-12 17:32:44 -080010224 headerTextSecondary = null;
Selim Cinekbee4e072018-05-21 22:06:43 -070010225 maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010226 allowColorization = true;
10227 forceDefaultColor = false;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010228 return this;
10229 }
10230
10231 final StandardTemplateParams hasProgress(boolean hasProgress) {
10232 this.hasProgress = hasProgress;
10233 return this;
10234 }
10235
10236 final StandardTemplateParams title(CharSequence title) {
10237 this.title = title;
10238 return this;
10239 }
10240
10241 final StandardTemplateParams text(CharSequence text) {
10242 this.text = text;
10243 return this;
10244 }
10245
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010246 final StandardTemplateParams summaryText(CharSequence text) {
10247 this.summaryText = text;
10248 return this;
10249 }
10250
Selim Cinekafeed292017-12-12 17:32:44 -080010251 final StandardTemplateParams headerTextSecondary(CharSequence text) {
10252 this.headerTextSecondary = text;
10253 return this;
10254 }
10255
Selim Cinek384804b2018-04-18 14:31:07 +080010256 final StandardTemplateParams hideLargeIcon(boolean hideLargeIcon) {
10257 this.hideLargeIcon = hideLargeIcon;
Selim Cinek88188f22017-09-19 16:46:56 -070010258 return this;
10259 }
10260
Selim Cinek384804b2018-04-18 14:31:07 +080010261 final StandardTemplateParams hideReplyIcon(boolean hideReplyIcon) {
10262 this.hideReplyIcon = hideReplyIcon;
Selim Cinek88188f22017-09-19 16:46:56 -070010263 return this;
10264 }
10265
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010266 final StandardTemplateParams disallowColorization() {
10267 this.allowColorization = false;
10268 return this;
10269 }
10270
10271 final StandardTemplateParams forceDefaultColor() {
10272 this.forceDefaultColor = true;
10273 return this;
10274 }
10275
Adrian Roos70d7aa32017-01-11 15:39:06 -080010276 final StandardTemplateParams fillTextsFrom(Builder b) {
10277 Bundle extras = b.mN.extras;
Lucas Dupin00be88f2019-01-03 17:50:52 -080010278 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE));
Lucas Dupin06c5e642017-09-13 16:34:58 -070010279
Lucas Dupin06c5e642017-09-13 16:34:58 -070010280 CharSequence text = extras.getCharSequence(EXTRA_BIG_TEXT);
Lucas Dupin00be88f2019-01-03 17:50:52 -080010281 if (TextUtils.isEmpty(text)) {
Lucas Dupin06c5e642017-09-13 16:34:58 -070010282 text = extras.getCharSequence(EXTRA_TEXT);
10283 }
Lucas Dupin00be88f2019-01-03 17:50:52 -080010284 this.text = b.processLegacyText(text);
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010285 this.summaryText = extras.getCharSequence(EXTRA_SUB_TEXT);
Adrian Roos70d7aa32017-01-11 15:39:06 -080010286 return this;
10287 }
Selim Cinekbee4e072018-05-21 22:06:43 -070010288
10289 /**
10290 * Set the maximum lines of remote input history lines allowed.
10291 * @param maxRemoteInputHistory The number of lines.
10292 * @return The builder for method chaining.
10293 */
10294 public StandardTemplateParams setMaxRemoteInputHistory(int maxRemoteInputHistory) {
10295 this.maxRemoteInputHistory = maxRemoteInputHistory;
10296 return this;
10297 }
Adrian Roos70d7aa32017-01-11 15:39:06 -080010298 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010299}