blob: 639416aa46d7627ea9734d537489cc6c48bd9d9e [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 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001632 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07001633 public Builder addExtras(Bundle extras) {
1634 if (extras != null) {
1635 mExtras.putAll(extras);
1636 }
1637 return this;
1638 }
1639
1640 /**
1641 * Get the metadata Bundle used by this Builder.
1642 *
1643 * <p>The returned Bundle is shared with this Builder.
1644 */
1645 public Bundle getExtras() {
1646 return mExtras;
1647 }
1648
1649 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001650 * Add an input to be collected from the user when this action is sent.
1651 * Response values can be retrieved from the fired intent by using the
1652 * {@link RemoteInput#getResultsFromIntent} function.
1653 * @param remoteInput a {@link RemoteInput} to add to the action
1654 * @return this object for method chaining
1655 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001656 @NonNull
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001657 public Builder addRemoteInput(RemoteInput remoteInput) {
1658 if (mRemoteInputs == null) {
1659 mRemoteInputs = new ArrayList<RemoteInput>();
1660 }
1661 mRemoteInputs.add(remoteInput);
1662 return this;
1663 }
1664
1665 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001666 * Set whether the platform should automatically generate possible replies to add to
1667 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1668 * {@link RemoteInput}, this has no effect.
1669 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1670 * otherwise
1671 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001672 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001673 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001674 @NonNull
Alex Hills42b0c4d2016-04-26 13:35:36 -04001675 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1676 mAllowGeneratedReplies = allowGeneratedReplies;
1677 return this;
1678 }
1679
1680 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001681 * Sets the {@code SemanticAction} for this {@link Action}. A
1682 * {@code SemanticAction} denotes what an {@link Action}'s
1683 * {@link PendingIntent} will do (eg. reply, mark as read, delete, etc).
1684 * @param semanticAction a SemanticAction defined within {@link Action} with
1685 * {@code SEMANTIC_ACTION_} prefixes
1686 * @return this object for method chaining
1687 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001688 @NonNull
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001689 public Builder setSemanticAction(@SemanticAction int semanticAction) {
1690 mSemanticAction = semanticAction;
1691 return this;
1692 }
1693
1694 /**
Gustav Sennton005d7a02019-01-04 13:41:32 +00001695 * Sets whether this {@link Action} is a contextual action, i.e. whether the action is
1696 * dependent on the notification message body. An example of a contextual action could
1697 * be an action opening a map application with an address shown in the notification.
1698 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001699 @NonNull
Gustav Sennton005d7a02019-01-04 13:41:32 +00001700 public Builder setContextual(boolean isContextual) {
1701 mIsContextual = isContextual;
1702 return this;
1703 }
1704
1705 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001706 * Apply an extender to this action builder. Extenders may be used to add
1707 * metadata or change options on this builder.
1708 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001709 @NonNull
Griff Hazen61a9e862014-05-22 16:05:19 -07001710 public Builder extend(Extender extender) {
1711 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001712 return this;
1713 }
1714
1715 /**
Gustav Senntonc98b1632018-11-23 12:26:15 +00001716 * Throws an NPE if we are building a contextual action missing one of the fields
1717 * necessary to display the action.
1718 */
1719 private void checkContextualActionNullFields() {
Gustav Sennton005d7a02019-01-04 13:41:32 +00001720 if (!mIsContextual) return;
Gustav Senntonc98b1632018-11-23 12:26:15 +00001721
1722 if (mIcon == null) {
1723 throw new NullPointerException("Contextual Actions must contain a valid icon");
1724 }
1725
1726 if (mIntent == null) {
1727 throw new NullPointerException(
1728 "Contextual Actions must contain a valid PendingIntent");
1729 }
1730 }
1731
1732 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001733 * Combine all of the options that have been set and return a new {@link Action}
1734 * object.
1735 * @return the built action
1736 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001737 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07001738 public Action build() {
Gustav Senntonc98b1632018-11-23 12:26:15 +00001739 checkContextualActionNullFields();
1740
Shane Brennan472a3b32016-12-12 15:28:10 -08001741 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1742 RemoteInput[] previousDataInputs =
1743 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001744 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001745 for (RemoteInput input : previousDataInputs) {
1746 dataOnlyInputs.add(input);
1747 }
1748 }
1749 List<RemoteInput> textInputs = new ArrayList<>();
1750 if (mRemoteInputs != null) {
1751 for (RemoteInput input : mRemoteInputs) {
1752 if (input.isDataOnly()) {
1753 dataOnlyInputs.add(input);
1754 } else {
1755 textInputs.add(input);
1756 }
1757 }
1758 }
1759 if (!dataOnlyInputs.isEmpty()) {
1760 RemoteInput[] dataInputsArr =
1761 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1762 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1763 }
1764 RemoteInput[] textInputsArr = textInputs.isEmpty()
1765 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1766 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Gustav Sennton005d7a02019-01-04 13:41:32 +00001767 mAllowGeneratedReplies, mSemanticAction, mIsContextual);
Griff Hazen959591e2014-05-15 22:26:18 -07001768 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001769 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001770
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001771 @Override
1772 public Action clone() {
1773 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001774 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001775 title,
1776 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001777 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001778 getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001779 getAllowGeneratedReplies(),
Gustav Sennton005d7a02019-01-04 13:41:32 +00001780 getSemanticAction(),
1781 isContextual());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001782 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001783
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001784 @Override
1785 public int describeContents() {
1786 return 0;
1787 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001788
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001789 @Override
1790 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001791 final Icon ic = getIcon();
1792 if (ic != null) {
1793 out.writeInt(1);
1794 ic.writeToParcel(out, 0);
1795 } else {
1796 out.writeInt(0);
1797 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001798 TextUtils.writeToParcel(title, out, flags);
1799 if (actionIntent != null) {
1800 out.writeInt(1);
1801 actionIntent.writeToParcel(out, flags);
1802 } else {
1803 out.writeInt(0);
1804 }
Griff Hazen959591e2014-05-15 22:26:18 -07001805 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001806 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001807 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001808 out.writeInt(mSemanticAction);
Gustav Sennton005d7a02019-01-04 13:41:32 +00001809 out.writeInt(mIsContextual ? 1 : 0);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001810 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001811
Griff Hazen959591e2014-05-15 22:26:18 -07001812 public static final Parcelable.Creator<Action> CREATOR =
1813 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001814 public Action createFromParcel(Parcel in) {
1815 return new Action(in);
1816 }
1817 public Action[] newArray(int size) {
1818 return new Action[size];
1819 }
1820 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001821
1822 /**
1823 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1824 * metadata or change options on an action builder.
1825 */
1826 public interface Extender {
1827 /**
1828 * Apply this extender to a notification action builder.
1829 * @param builder the builder to be modified.
1830 * @return the build object for chaining.
1831 */
1832 public Builder extend(Builder builder);
1833 }
1834
1835 /**
1836 * Wearable extender for notification actions. To add extensions to an action,
1837 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1838 * the {@code WearableExtender()} constructor and apply it to a
1839 * {@link android.app.Notification.Action.Builder} using
1840 * {@link android.app.Notification.Action.Builder#extend}.
1841 *
1842 * <pre class="prettyprint">
1843 * Notification.Action action = new Notification.Action.Builder(
1844 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001845 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001846 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001847 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001848 */
1849 public static final class WearableExtender implements Extender {
1850 /** Notification action extra which contains wearable extensions */
1851 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1852
Pete Gastaf6781d2014-10-07 15:17:05 -04001853 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001854 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001855 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1856 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1857 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001858
1859 // Flags bitwise-ored to mFlags
1860 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001861 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001862 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001863
1864 // Default value for flags integer
1865 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1866
1867 private int mFlags = DEFAULT_FLAGS;
1868
Pete Gastaf6781d2014-10-07 15:17:05 -04001869 private CharSequence mInProgressLabel;
1870 private CharSequence mConfirmLabel;
1871 private CharSequence mCancelLabel;
1872
Griff Hazen61a9e862014-05-22 16:05:19 -07001873 /**
1874 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1875 * options.
1876 */
1877 public WearableExtender() {
1878 }
1879
1880 /**
1881 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1882 * wearable options present in an existing notification action.
1883 * @param action the notification action to inspect.
1884 */
1885 public WearableExtender(Action action) {
1886 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1887 if (wearableBundle != null) {
1888 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001889 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1890 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1891 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001892 }
1893 }
1894
1895 /**
1896 * Apply wearable extensions to a notification action that is being built. This is
1897 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1898 * method of {@link android.app.Notification.Action.Builder}.
1899 */
1900 @Override
1901 public Action.Builder extend(Action.Builder builder) {
1902 Bundle wearableBundle = new Bundle();
1903
1904 if (mFlags != DEFAULT_FLAGS) {
1905 wearableBundle.putInt(KEY_FLAGS, mFlags);
1906 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001907 if (mInProgressLabel != null) {
1908 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1909 }
1910 if (mConfirmLabel != null) {
1911 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1912 }
1913 if (mCancelLabel != null) {
1914 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1915 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001916
1917 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1918 return builder;
1919 }
1920
1921 @Override
1922 public WearableExtender clone() {
1923 WearableExtender that = new WearableExtender();
1924 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001925 that.mInProgressLabel = this.mInProgressLabel;
1926 that.mConfirmLabel = this.mConfirmLabel;
1927 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001928 return that;
1929 }
1930
1931 /**
1932 * Set whether this action is available when the wearable device is not connected to
1933 * a companion device. The user can still trigger this action when the wearable device is
1934 * offline, but a visual hint will indicate that the action may not be available.
1935 * Defaults to true.
1936 */
1937 public WearableExtender setAvailableOffline(boolean availableOffline) {
1938 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1939 return this;
1940 }
1941
1942 /**
1943 * Get whether this action is available when the wearable device is not connected to
1944 * a companion device. The user can still trigger this action when the wearable device is
1945 * offline, but a visual hint will indicate that the action may not be available.
1946 * Defaults to true.
1947 */
1948 public boolean isAvailableOffline() {
1949 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1950 }
1951
1952 private void setFlag(int mask, boolean value) {
1953 if (value) {
1954 mFlags |= mask;
1955 } else {
1956 mFlags &= ~mask;
1957 }
1958 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001959
1960 /**
1961 * Set a label to display while the wearable is preparing to automatically execute the
1962 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1963 *
1964 * @param label the label to display while the action is being prepared to execute
1965 * @return this object for method chaining
1966 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001967 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001968 public WearableExtender setInProgressLabel(CharSequence label) {
1969 mInProgressLabel = label;
1970 return this;
1971 }
1972
1973 /**
1974 * Get the label to display while the wearable is preparing to automatically execute
1975 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1976 *
1977 * @return the label to display while the action is being prepared to execute
1978 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001979 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001980 public CharSequence getInProgressLabel() {
1981 return mInProgressLabel;
1982 }
1983
1984 /**
1985 * Set a label to display to confirm that the action should be executed.
1986 * This is usually an imperative verb like "Send".
1987 *
1988 * @param label the label to confirm the action should be executed
1989 * @return this object for method chaining
1990 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001991 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001992 public WearableExtender setConfirmLabel(CharSequence label) {
1993 mConfirmLabel = label;
1994 return this;
1995 }
1996
1997 /**
1998 * Get the label to display to confirm that the action should be executed.
1999 * This is usually an imperative verb like "Send".
2000 *
2001 * @return the label to confirm the action should be executed
2002 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002003 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002004 public CharSequence getConfirmLabel() {
2005 return mConfirmLabel;
2006 }
2007
2008 /**
2009 * Set a label to display to cancel the action.
2010 * This is usually an imperative verb, like "Cancel".
2011 *
2012 * @param label the label to display to cancel the action
2013 * @return this object for method chaining
2014 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002015 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002016 public WearableExtender setCancelLabel(CharSequence label) {
2017 mCancelLabel = label;
2018 return this;
2019 }
2020
2021 /**
2022 * Get the label to display to cancel the action.
2023 * This is usually an imperative verb like "Cancel".
2024 *
2025 * @return the label to display to cancel the action
2026 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002027 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002028 public CharSequence getCancelLabel() {
2029 return mCancelLabel;
2030 }
Alex Hills9ab3a232016-04-05 14:54:56 -04002031
2032 /**
2033 * Set a hint that this Action will launch an {@link Activity} directly, telling the
2034 * platform that it can generate the appropriate transitions.
2035 * @param hintLaunchesActivity {@code true} if the content intent will launch
2036 * an activity and transitions should be generated, false otherwise.
2037 * @return this object for method chaining
2038 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04002039 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04002040 boolean hintLaunchesActivity) {
2041 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
2042 return this;
2043 }
2044
2045 /**
2046 * Get a hint that this Action will launch an {@link Activity} directly, telling the
2047 * platform that it can generate the appropriate transitions
2048 * @return {@code true} if the content intent will launch an activity and transitions
2049 * should be generated, false otherwise. The default value is {@code false} if this was
2050 * never set.
2051 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04002052 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04002053 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
2054 }
Alex Hills9f087612016-06-07 09:08:59 -04002055
2056 /**
2057 * Set a hint that this Action should be displayed inline.
2058 *
2059 * @param hintDisplayInline {@code true} if action should be displayed inline, false
2060 * otherwise
2061 * @return this object for method chaining
2062 */
2063 public WearableExtender setHintDisplayActionInline(
2064 boolean hintDisplayInline) {
2065 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
2066 return this;
2067 }
2068
2069 /**
2070 * Get a hint that this Action should be displayed inline.
2071 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08002072 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04002073 * otherwise. The default value is {@code false} if this was never set.
2074 */
2075 public boolean getHintDisplayActionInline() {
2076 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
2077 }
Griff Hazen61a9e862014-05-22 16:05:19 -07002078 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002079
2080 /**
2081 * Provides meaning to an {@link Action} that hints at what the associated
2082 * {@link PendingIntent} will do. For example, an {@link Action} with a
2083 * {@link PendingIntent} that replies to a text message notification may have the
2084 * {@link #SEMANTIC_ACTION_REPLY} {@code SemanticAction} set within it.
2085 *
2086 * @hide
2087 */
2088 @IntDef(prefix = { "SEMANTIC_ACTION_" }, value = {
2089 SEMANTIC_ACTION_NONE,
2090 SEMANTIC_ACTION_REPLY,
2091 SEMANTIC_ACTION_MARK_AS_READ,
2092 SEMANTIC_ACTION_MARK_AS_UNREAD,
2093 SEMANTIC_ACTION_DELETE,
2094 SEMANTIC_ACTION_ARCHIVE,
2095 SEMANTIC_ACTION_MUTE,
2096 SEMANTIC_ACTION_UNMUTE,
2097 SEMANTIC_ACTION_THUMBS_UP,
Kodlee Yincda5b092018-02-15 15:34:53 -08002098 SEMANTIC_ACTION_THUMBS_DOWN,
Gustav Sennton005d7a02019-01-04 13:41:32 +00002099 SEMANTIC_ACTION_CALL
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002100 })
2101 @Retention(RetentionPolicy.SOURCE)
2102 public @interface SemanticAction {}
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002103 }
2104
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002105 /**
2106 * Array of all {@link Action} structures attached to this notification by
2107 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
2108 * {@link android.service.notification.NotificationListenerService} that provide an alternative
2109 * interface for invoking actions.
2110 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05002111 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002112
2113 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002114 * Replacement version of this notification whose content will be shown
2115 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
2116 * and {@link #VISIBILITY_PUBLIC}.
2117 */
2118 public Notification publicVersion;
2119
2120 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002121 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08002122 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002123 */
2124 public Notification()
2125 {
2126 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002127 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002128 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002129 }
2130
2131 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002132 * @hide
2133 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002134 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 public Notification(Context context, int icon, CharSequence tickerText, long when,
2136 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
2137 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04002138 new Builder(context)
2139 .setWhen(when)
2140 .setSmallIcon(icon)
2141 .setTicker(tickerText)
2142 .setContentTitle(contentTitle)
2143 .setContentText(contentText)
2144 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
2145 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146 }
2147
2148 /**
2149 * Constructs a Notification object with the information needed to
2150 * have a status bar icon without the standard expanded view.
2151 *
2152 * @param icon The resource id of the icon to put in the status bar.
2153 * @param tickerText The text that flows by in the status bar when the notification first
2154 * activates.
2155 * @param when The time to show in the time field. In the System.currentTimeMillis
2156 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08002157 *
2158 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002160 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002161 public Notification(int icon, CharSequence tickerText, long when)
2162 {
2163 this.icon = icon;
2164 this.tickerText = tickerText;
2165 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002166 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002167 }
2168
2169 /**
2170 * Unflatten the notification from a parcel.
2171 */
Svet Ganovddb94882016-06-23 19:55:24 -07002172 @SuppressWarnings("unchecked")
2173 public Notification(Parcel parcel) {
2174 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
2175 // intents in extras are always written as the last entry.
2176 readFromParcelImpl(parcel);
2177 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002178 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07002179 }
2180
2181 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002182 {
2183 int version = parcel.readInt();
2184
Adrian Roosfb921842017-10-26 14:49:56 +02002185 mWhitelistToken = parcel.readStrongBinder();
2186 if (mWhitelistToken == null) {
2187 mWhitelistToken = processWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -07002188 }
2189 // Propagate this token to all pending intents that are unmarshalled from the parcel.
Adrian Roosfb921842017-10-26 14:49:56 +02002190 parcel.setClassCookie(PendingIntent.class, mWhitelistToken);
Dianne Hackborn98305522017-05-05 17:53:53 -07002191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002192 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002193 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04002194 if (parcel.readInt() != 0) {
2195 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04002196 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
2197 icon = mSmallIcon.getResId();
2198 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002199 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200 number = parcel.readInt();
2201 if (parcel.readInt() != 0) {
2202 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2203 }
2204 if (parcel.readInt() != 0) {
2205 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2206 }
2207 if (parcel.readInt() != 0) {
2208 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2209 }
2210 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002211 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002212 }
2213 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002214 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
2215 }
Joe Onorato561d3852010-11-20 18:09:34 -08002216 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002217 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08002218 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002219 defaults = parcel.readInt();
2220 flags = parcel.readInt();
2221 if (parcel.readInt() != 0) {
2222 sound = Uri.CREATOR.createFromParcel(parcel);
2223 }
2224
2225 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04002226 if (parcel.readInt() != 0) {
2227 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
2228 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002229 vibrate = parcel.createLongArray();
2230 ledARGB = parcel.readInt();
2231 ledOnMS = parcel.readInt();
2232 ledOffMS = parcel.readInt();
2233 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002234
2235 if (parcel.readInt() != 0) {
2236 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2237 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002238
2239 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002240
John Spurlockfd7f1e02014-03-18 16:41:57 -04002241 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002242
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002243 mGroupKey = parcel.readString();
2244
2245 mSortKey = parcel.readString();
2246
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06002247 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Robin Leead7e72a2017-12-04 15:45:46 +01002248 fixDuplicateExtras();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002249
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002250 actions = parcel.createTypedArray(Action.CREATOR); // may be null
2251
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002252 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002253 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2254 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002255
Chris Wren8fd39ec2014-02-27 17:43:26 -05002256 if (parcel.readInt() != 0) {
2257 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2258 }
2259
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002260 visibility = parcel.readInt();
2261
2262 if (parcel.readInt() != 0) {
2263 publicVersion = Notification.CREATOR.createFromParcel(parcel);
2264 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002265
2266 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002267
2268 if (parcel.readInt() != 0) {
2269 mChannelId = parcel.readString();
2270 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002271 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05002272
2273 if (parcel.readInt() != 0) {
2274 mShortcutId = parcel.readString();
2275 }
2276
2277 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04002278
2279 if (parcel.readInt() != 0) {
2280 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2281 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002282
2283 mGroupAlertBehavior = parcel.readInt();
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002284 if (parcel.readInt() != 0) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002285 mBubbleMetadata = BubbleMetadata.CREATOR.createFromParcel(parcel);
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002286 }
Gustav Sennton761884c2018-11-19 17:40:19 +00002287
2288 mAllowSystemGeneratedContextualActions = parcel.readBoolean();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002289 }
2290
Andy Stadler110988c2010-12-03 14:29:16 -08002291 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07002292 public Notification clone() {
2293 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04002294 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002295 return that;
2296 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002297
Daniel Sandler1a497d32013-04-18 14:52:45 -04002298 /**
2299 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
2300 * of this into that.
2301 * @hide
2302 */
2303 public void cloneInto(Notification that, boolean heavy) {
Adrian Roosfb921842017-10-26 14:49:56 +02002304 that.mWhitelistToken = this.mWhitelistToken;
Joe Onorato18e69df2010-05-17 22:26:12 -07002305 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002306 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002307 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07002308 that.number = this.number;
2309
2310 // PendingIntents are global, so there's no reason (or way) to clone them.
2311 that.contentIntent = this.contentIntent;
2312 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002313 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07002314
2315 if (this.tickerText != null) {
2316 that.tickerText = this.tickerText.toString();
2317 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002318 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002319 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04002320 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002321 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07002322 that.contentView = this.contentView.clone();
2323 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002324 if (heavy && this.mLargeIcon != null) {
2325 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08002326 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01002327 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07002328 that.sound = this.sound; // android.net.Uri is immutable
2329 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002330 if (this.audioAttributes != null) {
2331 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
2332 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002333
2334 final long[] vibrate = this.vibrate;
2335 if (vibrate != null) {
2336 final int N = vibrate.length;
2337 final long[] vib = that.vibrate = new long[N];
2338 System.arraycopy(vibrate, 0, vib, 0, N);
2339 }
2340
2341 that.ledARGB = this.ledARGB;
2342 that.ledOnMS = this.ledOnMS;
2343 that.ledOffMS = this.ledOffMS;
2344 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002345
Joe Onorato18e69df2010-05-17 22:26:12 -07002346 that.flags = this.flags;
2347
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002348 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08002349
John Spurlockfd7f1e02014-03-18 16:41:57 -04002350 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002351
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002352 that.mGroupKey = this.mGroupKey;
2353
2354 that.mSortKey = this.mSortKey;
2355
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002356 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002357 try {
2358 that.extras = new Bundle(this.extras);
2359 // will unparcel
2360 that.extras.size();
2361 } catch (BadParcelableException e) {
2362 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
2363 that.extras = null;
2364 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002365 }
2366
Felipe Lemedd85da62016-06-28 11:29:54 -07002367 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2368 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002369 }
2370
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002371 if (this.actions != null) {
2372 that.actions = new Action[this.actions.length];
2373 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002374 if ( this.actions[i] != null) {
2375 that.actions[i] = this.actions[i].clone();
2376 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002377 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002378 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002379
Daniel Sandler1a497d32013-04-18 14:52:45 -04002380 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002381 that.bigContentView = this.bigContentView.clone();
2382 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002383
Chris Wren8fd39ec2014-02-27 17:43:26 -05002384 if (heavy && this.headsUpContentView != null) {
2385 that.headsUpContentView = this.headsUpContentView.clone();
2386 }
2387
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002388 that.visibility = this.visibility;
2389
2390 if (this.publicVersion != null) {
2391 that.publicVersion = new Notification();
2392 this.publicVersion.cloneInto(that.publicVersion, heavy);
2393 }
2394
Dan Sandler26e81cf2014-05-06 10:01:27 -04002395 that.color = this.color;
2396
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002397 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002398 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002399 that.mShortcutId = this.mShortcutId;
2400 that.mBadgeIcon = this.mBadgeIcon;
2401 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002402 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002403 that.mBubbleMetadata = this.mBubbleMetadata;
Gustav Sennton761884c2018-11-19 17:40:19 +00002404 that.mAllowSystemGeneratedContextualActions = this.mAllowSystemGeneratedContextualActions;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002405
Daniel Sandler1a497d32013-04-18 14:52:45 -04002406 if (!heavy) {
2407 that.lightenPayload(); // will clean out extras
2408 }
2409 }
2410
2411 /**
Jeff Sharkey23b31182018-04-18 21:32:12 -06002412 * Note all {@link Uri} that are referenced internally, with the expectation
2413 * that Uri permission grants will need to be issued to ensure the recipient
2414 * of this object is able to render its contents.
2415 *
2416 * @hide
2417 */
2418 public void visitUris(@NonNull Consumer<Uri> visitor) {
2419 visitor.accept(sound);
2420
2421 if (tickerView != null) tickerView.visitUris(visitor);
2422 if (contentView != null) contentView.visitUris(visitor);
2423 if (bigContentView != null) bigContentView.visitUris(visitor);
2424 if (headsUpContentView != null) headsUpContentView.visitUris(visitor);
2425
2426 if (extras != null) {
2427 visitor.accept(extras.getParcelable(EXTRA_AUDIO_CONTENTS_URI));
Robin Leed107af62018-04-27 13:55:56 +02002428 if (extras.containsKey(EXTRA_BACKGROUND_IMAGE_URI)) {
2429 visitor.accept(Uri.parse(extras.getString(EXTRA_BACKGROUND_IMAGE_URI)));
2430 }
Jeff Sharkey23b31182018-04-18 21:32:12 -06002431 }
2432
2433 if (MessagingStyle.class.equals(getNotificationStyle()) && extras != null) {
2434 final Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
2435 if (!ArrayUtils.isEmpty(messages)) {
2436 for (MessagingStyle.Message message : MessagingStyle.Message
2437 .getMessagesFromBundleArray(messages)) {
2438 visitor.accept(message.getDataUri());
2439 }
2440 }
2441
2442 final Parcelable[] historic = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
2443 if (!ArrayUtils.isEmpty(historic)) {
2444 for (MessagingStyle.Message message : MessagingStyle.Message
2445 .getMessagesFromBundleArray(historic)) {
2446 visitor.accept(message.getDataUri());
2447 }
2448 }
2449 }
2450 }
2451
2452 /**
Daniel Sandler1a497d32013-04-18 14:52:45 -04002453 * Removes heavyweight parts of the Notification object for archival or for sending to
2454 * listeners when the full contents are not necessary.
2455 * @hide
2456 */
2457 public final void lightenPayload() {
2458 tickerView = null;
2459 contentView = null;
2460 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002461 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002462 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002463 if (extras != null && !extras.isEmpty()) {
2464 final Set<String> keyset = extras.keySet();
2465 final int N = keyset.size();
2466 final String[] keys = keyset.toArray(new String[N]);
2467 for (int i=0; i<N; i++) {
2468 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002469 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2470 continue;
2471 }
Dan Sandler50128532015-12-08 15:42:41 -05002472 final Object obj = extras.get(key);
2473 if (obj != null &&
2474 ( obj instanceof Parcelable
2475 || obj instanceof Parcelable[]
2476 || obj instanceof SparseArray
2477 || obj instanceof ArrayList)) {
2478 extras.remove(key);
2479 }
2480 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002481 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002482 }
2483
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002484 /**
2485 * Make sure this CharSequence is safe to put into a bundle, which basically
2486 * means it had better not be some custom Parcelable implementation.
2487 * @hide
2488 */
2489 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002490 if (cs == null) return cs;
2491 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2492 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2493 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002494 if (cs instanceof Parcelable) {
2495 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2496 + " instance is a custom Parcelable and not allowed in Notification");
2497 return cs.toString();
2498 }
Selim Cinek60a54252016-02-26 17:03:25 -08002499 return removeTextSizeSpans(cs);
2500 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002501
Selim Cinek60a54252016-02-26 17:03:25 -08002502 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2503 if (charSequence instanceof Spanned) {
2504 Spanned ss = (Spanned) charSequence;
2505 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2506 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2507 for (Object span : spans) {
2508 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002509 if (resultSpan instanceof CharacterStyle) {
2510 resultSpan = ((CharacterStyle) span).getUnderlying();
2511 }
2512 if (resultSpan instanceof TextAppearanceSpan) {
2513 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002514 resultSpan = new TextAppearanceSpan(
2515 originalSpan.getFamily(),
2516 originalSpan.getTextStyle(),
2517 -1,
2518 originalSpan.getTextColor(),
2519 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002520 } else if (resultSpan instanceof RelativeSizeSpan
2521 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002522 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002523 } else {
2524 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002525 }
2526 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2527 ss.getSpanFlags(span));
2528 }
2529 return builder;
2530 }
2531 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002532 }
2533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002534 public int describeContents() {
2535 return 0;
2536 }
2537
2538 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002539 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002540 */
Svet Ganovddb94882016-06-23 19:55:24 -07002541 public void writeToParcel(Parcel parcel, int flags) {
2542 // We need to mark all pending intents getting into the notification
2543 // system as being put there to later allow the notification ranker
2544 // to launch them and by doing so add the app to the battery saver white
2545 // list for a short period of time. The problem is that the system
2546 // cannot look into the extras as there may be parcelables there that
2547 // the platform does not know how to handle. To go around that we have
2548 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002549 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002550 if (collectPendingIntents) {
2551 PendingIntent.setOnMarshaledListener(
2552 (PendingIntent intent, Parcel out, int outFlags) -> {
2553 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002554 if (allPendingIntents == null) {
2555 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002556 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002557 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002558 }
2559 });
2560 }
2561 try {
2562 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002563 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002564 writeToParcelImpl(parcel, flags);
2565 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002566 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002567 } finally {
2568 if (collectPendingIntents) {
2569 PendingIntent.setOnMarshaledListener(null);
2570 }
2571 }
2572 }
2573
2574 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002575 parcel.writeInt(1);
2576
Adrian Roosfb921842017-10-26 14:49:56 +02002577 parcel.writeStrongBinder(mWhitelistToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002578 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002579 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002580 if (mSmallIcon == null && icon != 0) {
2581 // you snuck an icon in here without using the builder; let's try to keep it
2582 mSmallIcon = Icon.createWithResource("", icon);
2583 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002584 if (mSmallIcon != null) {
2585 parcel.writeInt(1);
2586 mSmallIcon.writeToParcel(parcel, 0);
2587 } else {
2588 parcel.writeInt(0);
2589 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002590 parcel.writeInt(number);
2591 if (contentIntent != null) {
2592 parcel.writeInt(1);
2593 contentIntent.writeToParcel(parcel, 0);
2594 } else {
2595 parcel.writeInt(0);
2596 }
2597 if (deleteIntent != null) {
2598 parcel.writeInt(1);
2599 deleteIntent.writeToParcel(parcel, 0);
2600 } else {
2601 parcel.writeInt(0);
2602 }
2603 if (tickerText != null) {
2604 parcel.writeInt(1);
2605 TextUtils.writeToParcel(tickerText, parcel, flags);
2606 } else {
2607 parcel.writeInt(0);
2608 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002609 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002610 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002611 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002612 } else {
2613 parcel.writeInt(0);
2614 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002615 if (contentView != null) {
2616 parcel.writeInt(1);
2617 contentView.writeToParcel(parcel, 0);
2618 } else {
2619 parcel.writeInt(0);
2620 }
Selim Cinek279fa862016-06-14 10:57:25 -07002621 if (mLargeIcon == null && largeIcon != null) {
2622 // you snuck an icon in here without using the builder; let's try to keep it
2623 mLargeIcon = Icon.createWithBitmap(largeIcon);
2624 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002625 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002626 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002627 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002628 } else {
2629 parcel.writeInt(0);
2630 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002631
2632 parcel.writeInt(defaults);
2633 parcel.writeInt(this.flags);
2634
2635 if (sound != null) {
2636 parcel.writeInt(1);
2637 sound.writeToParcel(parcel, 0);
2638 } else {
2639 parcel.writeInt(0);
2640 }
2641 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002642
2643 if (audioAttributes != null) {
2644 parcel.writeInt(1);
2645 audioAttributes.writeToParcel(parcel, 0);
2646 } else {
2647 parcel.writeInt(0);
2648 }
2649
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002650 parcel.writeLongArray(vibrate);
2651 parcel.writeInt(ledARGB);
2652 parcel.writeInt(ledOnMS);
2653 parcel.writeInt(ledOffMS);
2654 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002655
2656 if (fullScreenIntent != null) {
2657 parcel.writeInt(1);
2658 fullScreenIntent.writeToParcel(parcel, 0);
2659 } else {
2660 parcel.writeInt(0);
2661 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002662
2663 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002664
John Spurlockfd7f1e02014-03-18 16:41:57 -04002665 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002666
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002667 parcel.writeString(mGroupKey);
2668
2669 parcel.writeString(mSortKey);
2670
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002671 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002672
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002673 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002674
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002675 if (bigContentView != null) {
2676 parcel.writeInt(1);
2677 bigContentView.writeToParcel(parcel, 0);
2678 } else {
2679 parcel.writeInt(0);
2680 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002681
Chris Wren8fd39ec2014-02-27 17:43:26 -05002682 if (headsUpContentView != null) {
2683 parcel.writeInt(1);
2684 headsUpContentView.writeToParcel(parcel, 0);
2685 } else {
2686 parcel.writeInt(0);
2687 }
2688
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002689 parcel.writeInt(visibility);
2690
2691 if (publicVersion != null) {
2692 parcel.writeInt(1);
2693 publicVersion.writeToParcel(parcel, 0);
2694 } else {
2695 parcel.writeInt(0);
2696 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002697
2698 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002699
2700 if (mChannelId != null) {
2701 parcel.writeInt(1);
2702 parcel.writeString(mChannelId);
2703 } else {
2704 parcel.writeInt(0);
2705 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002706 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002707
2708 if (mShortcutId != null) {
2709 parcel.writeInt(1);
2710 parcel.writeString(mShortcutId);
2711 } else {
2712 parcel.writeInt(0);
2713 }
2714
2715 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002716
2717 if (mSettingsText != null) {
2718 parcel.writeInt(1);
2719 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2720 } else {
2721 parcel.writeInt(0);
2722 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002723
2724 parcel.writeInt(mGroupAlertBehavior);
Julia Reynoldsfc640012018-02-21 12:25:27 -05002725
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002726 if (mBubbleMetadata != null) {
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002727 parcel.writeInt(1);
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002728 mBubbleMetadata.writeToParcel(parcel, 0);
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002729 } else {
2730 parcel.writeInt(0);
2731 }
2732
Gustav Sennton761884c2018-11-19 17:40:19 +00002733 parcel.writeBoolean(mAllowSystemGeneratedContextualActions);
2734
Julia Reynoldsfc640012018-02-21 12:25:27 -05002735 // mUsesStandardHeader is not written because it should be recomputed in listeners
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002736 }
2737
2738 /**
2739 * Parcelable.Creator that instantiates Notification objects
2740 */
2741 public static final Parcelable.Creator<Notification> CREATOR
2742 = new Parcelable.Creator<Notification>()
2743 {
2744 public Notification createFromParcel(Parcel parcel)
2745 {
2746 return new Notification(parcel);
2747 }
2748
2749 public Notification[] newArray(int size)
2750 {
2751 return new Notification[size];
2752 }
2753 };
2754
2755 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05002756 * @hide
2757 */
2758 public static boolean areActionsVisiblyDifferent(Notification first, Notification second) {
2759 Notification.Action[] firstAs = first.actions;
2760 Notification.Action[] secondAs = second.actions;
2761 if (firstAs == null && secondAs != null || firstAs != null && secondAs == null) {
2762 return true;
2763 }
2764 if (firstAs != null && secondAs != null) {
2765 if (firstAs.length != secondAs.length) {
2766 return true;
2767 }
2768 for (int i = 0; i < firstAs.length; i++) {
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -04002769 if (!Objects.equals(String.valueOf(firstAs[i].title),
2770 String.valueOf(secondAs[i].title))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05002771 return true;
2772 }
2773 RemoteInput[] firstRs = firstAs[i].getRemoteInputs();
2774 RemoteInput[] secondRs = secondAs[i].getRemoteInputs();
2775 if (firstRs == null) {
2776 firstRs = new RemoteInput[0];
2777 }
2778 if (secondRs == null) {
2779 secondRs = new RemoteInput[0];
2780 }
2781 if (firstRs.length != secondRs.length) {
2782 return true;
2783 }
2784 for (int j = 0; j < firstRs.length; j++) {
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -04002785 if (!Objects.equals(String.valueOf(firstRs[j].getLabel()),
2786 String.valueOf(secondRs[j].getLabel()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05002787 return true;
2788 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05002789 }
2790 }
2791 }
2792 return false;
2793 }
2794
2795 /**
2796 * @hide
2797 */
2798 public static boolean areStyledNotificationsVisiblyDifferent(Builder first, Builder second) {
2799 if (first.getStyle() == null) {
2800 return second.getStyle() != null;
2801 }
2802 if (second.getStyle() == null) {
2803 return true;
2804 }
2805 return first.getStyle().areNotificationsVisiblyDifferent(second.getStyle());
2806 }
2807
2808 /**
2809 * @hide
2810 */
2811 public static boolean areRemoteViewsChanged(Builder first, Builder second) {
Julia Reynoldse5c60452018-04-30 14:41:36 -04002812 if (!Objects.equals(first.usesStandardHeader(), second.usesStandardHeader())) {
2813 return true;
2814 }
2815
2816 if (areRemoteViewsChanged(first.mN.contentView, second.mN.contentView)) {
2817 return true;
2818 }
2819 if (areRemoteViewsChanged(first.mN.bigContentView, second.mN.bigContentView)) {
2820 return true;
2821 }
2822 if (areRemoteViewsChanged(first.mN.headsUpContentView, second.mN.headsUpContentView)) {
2823 return true;
2824 }
2825
2826 return false;
2827 }
2828
2829 private static boolean areRemoteViewsChanged(RemoteViews first, RemoteViews second) {
2830 if (first == null && second == null) {
2831 return false;
2832 }
2833 if (first == null && second != null || first != null && second == null) {
2834 return true;
2835 }
2836
2837 if (!Objects.equals(first.getLayoutId(), second.getLayoutId())) {
2838 return true;
2839 }
2840
2841 if (!Objects.equals(first.getSequenceNumber(), second.getSequenceNumber())) {
2842 return true;
2843 }
2844
2845 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05002846 }
2847
2848 /**
Robin Leead7e72a2017-12-04 15:45:46 +01002849 * Parcelling creates multiple copies of objects in {@code extras}. Fix them.
2850 * <p>
2851 * For backwards compatibility {@code extras} holds some references to "real" member data such
2852 * as {@link getLargeIcon()} which is mirrored by {@link #EXTRA_LARGE_ICON}. This is mostly
2853 * fine as long as the object stays in one process.
2854 * <p>
2855 * However, once the notification goes into a parcel each reference gets marshalled separately,
2856 * wasting memory. Especially with large images on Auto and TV, this is worth fixing.
2857 */
2858 private void fixDuplicateExtras() {
2859 if (extras != null) {
Robin Leead7e72a2017-12-04 15:45:46 +01002860 fixDuplicateExtra(mLargeIcon, EXTRA_LARGE_ICON);
2861 }
2862 }
2863
2864 /**
2865 * If we find an extra that's exactly the same as one of the "real" fields but refers to a
2866 * separate object, replace it with the field's version to avoid holding duplicate copies.
2867 */
2868 private void fixDuplicateExtra(@Nullable Parcelable original, @NonNull String extraName) {
2869 if (original != null && extras.getParcelable(extraName) != null) {
2870 extras.putParcelable(extraName, original);
2871 }
2872 }
2873
2874 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002875 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2876 * layout.
2877 *
2878 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2879 * in the view.</p>
2880 * @param context The context for your application / activity.
2881 * @param contentTitle The title that goes in the expanded entry.
2882 * @param contentText The text that goes in the expanded entry.
2883 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2884 * If this is an activity, it must include the
2885 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002886 * that you take care of task management as described in the
2887 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2888 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002889 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002890 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002891 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002892 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002893 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002894 public void setLatestEventInfo(Context context,
2895 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002896 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2897 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2898 new Throwable());
2899 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002900
Selim Cinek4ac6f602016-06-13 15:47:03 -07002901 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2902 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2903 }
2904
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002905 // ensure that any information already set directly is preserved
2906 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002907
2908 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002909 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002910 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002911 }
2912 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002913 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002914 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002915 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002916
2917 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002918 }
2919
Julia Reynoldsda303542015-11-23 14:00:20 -05002920 /**
2921 * @hide
2922 */
2923 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002924 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002925 }
2926
2927 /**
2928 * @hide
2929 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002930 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002931 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002932 }
2933
Yi Jin6b514142017-10-30 14:54:12 -07002934 /**
2935 * @hide
2936 */
2937 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2938 long token = proto.start(fieldId);
2939 proto.write(NotificationProto.CHANNEL_ID, getChannelId());
2940 proto.write(NotificationProto.HAS_TICKER_TEXT, this.tickerText != null);
2941 proto.write(NotificationProto.FLAGS, this.flags);
2942 proto.write(NotificationProto.COLOR, this.color);
2943 proto.write(NotificationProto.CATEGORY, this.category);
2944 proto.write(NotificationProto.GROUP_KEY, this.mGroupKey);
2945 proto.write(NotificationProto.SORT_KEY, this.mSortKey);
2946 if (this.actions != null) {
2947 proto.write(NotificationProto.ACTION_LENGTH, this.actions.length);
2948 }
2949 if (this.visibility >= VISIBILITY_SECRET && this.visibility <= VISIBILITY_PUBLIC) {
2950 proto.write(NotificationProto.VISIBILITY, this.visibility);
2951 }
2952 if (publicVersion != null) {
2953 publicVersion.writeToProto(proto, NotificationProto.PUBLIC_VERSION);
2954 }
2955 proto.end(token);
2956 }
2957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002958 @Override
2959 public String toString() {
2960 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002961 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04002962 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002963 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002964 sb.append(priority);
2965 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002966 if (contentView != null) {
2967 sb.append(contentView.getPackage());
2968 sb.append("/0x");
2969 sb.append(Integer.toHexString(contentView.getLayoutId()));
2970 } else {
2971 sb.append("null");
2972 }
2973 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002974 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2975 sb.append("default");
2976 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002977 int N = this.vibrate.length-1;
2978 sb.append("[");
2979 for (int i=0; i<N; i++) {
2980 sb.append(this.vibrate[i]);
2981 sb.append(',');
2982 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002983 if (N != -1) {
2984 sb.append(this.vibrate[N]);
2985 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002986 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002987 } else {
2988 sb.append("null");
2989 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002990 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002991 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002992 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002993 } else if (this.sound != null) {
2994 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002995 } else {
2996 sb.append("null");
2997 }
Chris Wren365b6d32015-07-16 10:39:26 -04002998 if (this.tickerText != null) {
2999 sb.append(" tick");
3000 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003001 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003002 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003003 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04003004 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04003005 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003006 if (this.category != null) {
3007 sb.append(" category=");
3008 sb.append(this.category);
3009 }
3010 if (this.mGroupKey != null) {
3011 sb.append(" groupKey=");
3012 sb.append(this.mGroupKey);
3013 }
3014 if (this.mSortKey != null) {
3015 sb.append(" sortKey=");
3016 sb.append(this.mSortKey);
3017 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003018 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04003019 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003020 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04003021 }
3022 sb.append(" vis=");
3023 sb.append(visibilityToString(this.visibility));
3024 if (this.publicVersion != null) {
3025 sb.append(" publicVersion=");
3026 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003027 }
3028 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003029 return sb.toString();
3030 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003031
Dan Sandler1b718782014-07-18 12:43:45 -04003032 /**
3033 * {@hide}
3034 */
3035 public static String visibilityToString(int vis) {
3036 switch (vis) {
3037 case VISIBILITY_PRIVATE:
3038 return "PRIVATE";
3039 case VISIBILITY_PUBLIC:
3040 return "PUBLIC";
3041 case VISIBILITY_SECRET:
3042 return "SECRET";
3043 default:
3044 return "UNKNOWN(" + String.valueOf(vis) + ")";
3045 }
3046 }
3047
Joe Onoratocb109a02011-01-18 17:57:41 -08003048 /**
John Spurlock1d881a12015-03-18 19:21:54 -04003049 * {@hide}
3050 */
3051 public static String priorityToString(@Priority int pri) {
3052 switch (pri) {
3053 case PRIORITY_MIN:
3054 return "MIN";
3055 case PRIORITY_LOW:
3056 return "LOW";
3057 case PRIORITY_DEFAULT:
3058 return "DEFAULT";
3059 case PRIORITY_HIGH:
3060 return "HIGH";
3061 case PRIORITY_MAX:
3062 return "MAX";
3063 default:
3064 return "UNKNOWN(" + String.valueOf(pri) + ")";
3065 }
3066 }
3067
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04003068 /**
3069 * @hide
3070 */
3071 public boolean hasCompletedProgress() {
3072 // not a progress notification; can't be complete
3073 if (!extras.containsKey(EXTRA_PROGRESS)
3074 || !extras.containsKey(EXTRA_PROGRESS_MAX)) {
3075 return false;
3076 }
3077 // many apps use max 0 for 'indeterminate'; not complete
3078 if (extras.getInt(EXTRA_PROGRESS_MAX) == 0) {
3079 return false;
3080 }
3081 return extras.getInt(EXTRA_PROGRESS) == extras.getInt(EXTRA_PROGRESS_MAX);
3082 }
3083
Jeff Sharkey000ce802017-04-29 13:13:27 -06003084 /** @removed */
3085 @Deprecated
Julia Reynolds37856052016-11-11 09:20:07 -05003086 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003087 return mChannelId;
3088 }
3089
3090 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003091 * Returns the id of the channel this notification posts to.
3092 */
3093 public String getChannelId() {
3094 return mChannelId;
3095 }
3096
Jeff Sharkey000ce802017-04-29 13:13:27 -06003097 /** @removed */
3098 @Deprecated
Julia Reynolds2a128742016-11-28 14:29:25 -05003099 public long getTimeout() {
3100 return mTimeout;
3101 }
3102
3103 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003104 * Returns the duration from posting after which this notification should be canceled by the
3105 * system, if it's not canceled already.
3106 */
3107 public long getTimeoutAfter() {
3108 return mTimeout;
3109 }
3110
3111 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003112 * Returns what icon should be shown for this notification if it is being displayed in a
3113 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
3114 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
3115 */
3116 public int getBadgeIconType() {
3117 return mBadgeIcon;
3118 }
3119
3120 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003121 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04003122 *
3123 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
3124 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003125 */
3126 public String getShortcutId() {
3127 return mShortcutId;
3128 }
3129
Julia Reynolds3aedded2017-03-31 14:42:09 -04003130
3131 /**
3132 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
3133 */
3134 public CharSequence getSettingsText() {
3135 return mSettingsText;
3136 }
3137
Julia Reynolds13d898c2017-02-02 12:22:05 -05003138 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003139 * Returns which type of notifications in a group are responsible for audibly alerting the
3140 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
3141 * {@link #GROUP_ALERT_SUMMARY}.
3142 */
3143 public @GroupAlertBehavior int getGroupAlertBehavior() {
3144 return mGroupAlertBehavior;
3145 }
3146
3147 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003148 * Returns the bubble metadata that will be used to display app content in a floating window
3149 * over the existing foreground activity.
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003150 */
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003151 public BubbleMetadata getBubbleMetadata() {
3152 return mBubbleMetadata;
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003153 }
3154
Gustav Sennton79ebf4b2018-12-19 10:31:51 +00003155 /**
3156 * Returns whether the platform is allowed (by the app developer) to generate contextual actions
3157 * for this notification.
3158 */
Gustav Sennton761884c2018-11-19 17:40:19 +00003159 public boolean getAllowSystemGeneratedContextualActions() {
3160 return mAllowSystemGeneratedContextualActions;
3161 }
3162
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003163 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003164 * The small icon representing this notification in the status bar and content view.
3165 *
3166 * @return the small icon representing this notification.
3167 *
3168 * @see Builder#getSmallIcon()
3169 * @see Builder#setSmallIcon(Icon)
3170 */
3171 public Icon getSmallIcon() {
3172 return mSmallIcon;
3173 }
3174
3175 /**
3176 * Used when notifying to clean up legacy small icons.
3177 * @hide
3178 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003179 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04003180 public void setSmallIcon(Icon icon) {
3181 mSmallIcon = icon;
3182 }
3183
3184 /**
3185 * The large icon shown in this notification's content view.
3186 * @see Builder#getLargeIcon()
3187 * @see Builder#setLargeIcon(Icon)
3188 */
3189 public Icon getLargeIcon() {
3190 return mLargeIcon;
3191 }
3192
3193 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02003194 * @hide
3195 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003196 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003197 public boolean isGroupSummary() {
3198 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
3199 }
3200
3201 /**
3202 * @hide
3203 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003204 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003205 public boolean isGroupChild() {
3206 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
3207 }
3208
3209 /**
Julia Reynolds30203152017-05-26 13:36:31 -04003210 * @hide
3211 */
3212 public boolean suppressAlertingDueToGrouping() {
3213 if (isGroupSummary()
3214 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
3215 return true;
3216 } else if (isGroupChild()
3217 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
3218 return true;
3219 }
3220 return false;
3221 }
3222
Tony Mak638430e2018-10-08 19:19:10 +01003223
3224 /**
3225 * Finds and returns a remote input and its corresponding action.
3226 *
3227 * @param requiresFreeform requires the remoteinput to allow freeform or not.
3228 * @return the result pair, {@code null} if no result is found.
3229 *
3230 * @hide
3231 */
3232 @Nullable
3233 public Pair<RemoteInput, Action> findRemoteInputActionPair(boolean requiresFreeform) {
3234 if (actions == null) {
3235 return null;
3236 }
3237 for (Notification.Action action : actions) {
3238 if (action.getRemoteInputs() == null) {
3239 continue;
3240 }
3241 RemoteInput resultRemoteInput = null;
3242 for (RemoteInput remoteInput : action.getRemoteInputs()) {
3243 if (remoteInput.getAllowFreeFormInput() || !requiresFreeform) {
3244 resultRemoteInput = remoteInput;
3245 }
3246 }
3247 if (resultRemoteInput != null) {
3248 return Pair.create(resultRemoteInput, action);
3249 }
3250 }
3251 return null;
3252 }
3253
Julia Reynolds30203152017-05-26 13:36:31 -04003254 /**
Gustav Sennton005d7a02019-01-04 13:41:32 +00003255 * Returns the actions that are contextual out of the actions in this notification.
Gustav Senntoneab53682018-11-01 16:30:23 +00003256 *
3257 * @hide
3258 */
3259 public List<Notification.Action> getContextualActions() {
3260 if (actions == null) return Collections.emptyList();
3261
3262 List<Notification.Action> contextualActions = new ArrayList<>();
3263 for (Notification.Action action : actions) {
Gustav Sennton005d7a02019-01-04 13:41:32 +00003264 if (action.isContextual()) {
Gustav Senntoneab53682018-11-01 16:30:23 +00003265 contextualActions.add(action);
3266 }
3267 }
3268 return contextualActions;
3269 }
3270
3271 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003272 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08003273 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003274 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07003275 * content views using the platform's notification layout template. If your app supports
3276 * versions of Android as old as API level 4, you can instead use
3277 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
3278 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
3279 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08003280 *
Scott Main183bf112012-08-13 19:12:13 -07003281 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08003282 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003283 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07003284 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003285 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3286 * .setContentText(subject)
3287 * .setSmallIcon(R.drawable.new_mail)
3288 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003289 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003290 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08003291 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003292 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003293 /**
3294 * @hide
3295 */
3296 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
3297 "android.rebuild.contentViewActionCount";
3298 /**
3299 * @hide
3300 */
3301 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
3302 = "android.rebuild.bigViewActionCount";
3303 /**
3304 * @hide
3305 */
3306 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
3307 = "android.rebuild.hudViewActionCount";
3308
Selim Cinek6743c0b2017-01-18 18:24:01 -08003309 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
3310 SystemProperties.getBoolean("notifications.only_title", true);
3311
Selim Cinek389edcd2017-05-11 19:16:44 -07003312 /**
3313 * The lightness difference that has to be added to the primary text color to obtain the
3314 * secondary text color when the background is light.
3315 */
3316 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
3317
3318 /**
3319 * The lightness difference that has to be added to the primary text color to obtain the
3320 * secondary text color when the background is dark.
3321 * A bit less then the above value, since it looks better on dark backgrounds.
3322 */
3323 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
3324
Joe Onorato46439ce2010-11-19 13:56:21 -08003325 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003326 private Notification mN;
3327 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003328 private Style mStyle;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003329 @UnsupportedAppUsage
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003330 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Selim Cineke7238dd2017-12-14 17:48:32 -08003331 private ArrayList<Person> mPersonList = new ArrayList<>();
Lucas Dupina291d192018-06-07 13:59:42 -07003332 private ContrastColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08003333 private boolean mIsLegacy;
3334 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02003335
3336 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08003337 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
3338 */
3339 private int mCachedContrastColor = COLOR_INVALID;
3340 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08003341 /**
Selim Cinek4717d862018-04-19 09:19:15 +08003342 * Caches a ambient version of {@link #mCachedAmbientColorIsFor}.
Adrian Roos487374f2017-01-11 15:48:14 -08003343 */
3344 private int mCachedAmbientColor = COLOR_INVALID;
3345 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Selim Cinek4717d862018-04-19 09:19:15 +08003346 /**
3347 * A neutral color color that can be used for icons.
3348 */
3349 private int mNeutralColor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08003350
3351 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08003352 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
3353 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
3354 */
3355 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08003356 private int mTextColorsAreForBackground = COLOR_INVALID;
3357 private int mPrimaryTextColor = COLOR_INVALID;
3358 private int mSecondaryTextColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07003359 private int mBackgroundColor = COLOR_INVALID;
3360 private int mForegroundColor = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07003361 /**
3362 * A temporary location where actions are stored. If != null the view originally has action
3363 * but doesn't have any for this inflation.
3364 */
3365 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07003366 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003367
Anthony Chenad4d1582017-04-10 16:07:58 -07003368 private boolean mTintActionButtons;
3369 private boolean mInNightMode;
3370
Adrian Roos70d7aa32017-01-11 15:39:06 -08003371 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003372 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08003373 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003374 * @param context
3375 * A {@link Context} that will be used by the Builder to construct the
3376 * RemoteViews. The Context will not be held past the lifetime of this Builder
3377 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003378 * @param channelId
3379 * The constructed Notification will be posted on this
3380 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
3381 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003382 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003383 public Builder(Context context, String channelId) {
3384 this(context, (Notification) null);
3385 mN.mChannelId = channelId;
3386 }
3387
3388 /**
3389 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
3390 * instead. All posted Notifications must specify a NotificationChannel Id.
3391 */
3392 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003393 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003394 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003395 }
3396
Joe Onoratocb109a02011-01-18 17:57:41 -08003397 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003398 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02003399 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003400 public Builder(Context context, Notification toAdopt) {
3401 mContext = context;
Anthony Chenad4d1582017-04-10 16:07:58 -07003402 Resources res = mContext.getResources();
3403 mTintActionButtons = res.getBoolean(R.bool.config_tintNotificationActionButtons);
3404
3405 if (res.getBoolean(R.bool.config_enableNightMode)) {
3406 Configuration currentConfig = res.getConfiguration();
3407 mInNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
3408 == Configuration.UI_MODE_NIGHT_YES;
3409 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003410
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003411 if (toAdopt == null) {
3412 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003413 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
3414 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
3415 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003416 mN.priority = PRIORITY_DEFAULT;
3417 mN.visibility = VISIBILITY_PRIVATE;
3418 } else {
3419 mN = toAdopt;
3420 if (mN.actions != null) {
3421 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003422 }
3423
Selim Cineke7238dd2017-12-14 17:48:32 -08003424 if (mN.extras.containsKey(EXTRA_PEOPLE_LIST)) {
3425 ArrayList<Person> people = mN.extras.getParcelableArrayList(EXTRA_PEOPLE_LIST);
3426 mPersonList.addAll(people);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003427 }
3428
Selim Cinek4ac6f602016-06-13 15:47:03 -07003429 if (mN.getSmallIcon() == null && mN.icon != 0) {
3430 setSmallIcon(mN.icon);
3431 }
3432
3433 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
3434 setLargeIcon(mN.largeIcon);
3435 }
3436
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003437 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
3438 if (!TextUtils.isEmpty(templateClass)) {
3439 final Class<? extends Style> styleClass
3440 = getNotificationStyleClass(templateClass);
3441 if (styleClass == null) {
3442 Log.d(TAG, "Unknown style class: " + templateClass);
3443 } else {
3444 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003445 final Constructor<? extends Style> ctor =
3446 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003447 ctor.setAccessible(true);
3448 final Style style = ctor.newInstance();
3449 style.restoreFromExtras(mN.extras);
3450
3451 if (style != null) {
3452 setStyle(style);
3453 }
3454 } catch (Throwable t) {
3455 Log.e(TAG, "Could not create Style", t);
3456 }
3457 }
3458 }
3459
3460 }
3461 }
3462
Lucas Dupina291d192018-06-07 13:59:42 -07003463 private ContrastColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08003464 if (mColorUtil == null) {
Lucas Dupina291d192018-06-07 13:59:42 -07003465 mColorUtil = ContrastColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003466 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003467 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003468 }
3469
3470 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003471 * If this notification is duplicative of a Launcher shortcut, sets the
3472 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
3473 * the shortcut.
3474 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04003475 * This field will be ignored by Launchers that don't support badging, don't show
3476 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003477 *
3478 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
3479 * supersedes
3480 */
3481 public Builder setShortcutId(String shortcutId) {
3482 mN.mShortcutId = shortcutId;
3483 return this;
3484 }
3485
3486 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003487 * Sets which icon to display as a badge for this notification.
3488 *
3489 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
3490 * {@link #BADGE_ICON_LARGE}.
3491 *
3492 * Note: This value might be ignored, for launchers that don't support badge icons.
3493 */
Julia Reynolds612beb22017-03-30 10:48:30 -04003494 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04003495 mN.mBadgeIcon = icon;
3496 return this;
3497 }
3498
3499 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003500 * Sets the group alert behavior for this notification. Use this method to mute this
3501 * notification if alerts for this notification's group should be handled by a different
3502 * notification. This is only applicable for notifications that belong to a
Julia Reynolds399d9bf2017-08-11 12:52:14 -04003503 * {@link #setGroup(String) group}. This must be called on all notifications you want to
3504 * mute. For example, if you want only the summary of your group to make noise, all
3505 * children in the group should have the group alert behavior {@link #GROUP_ALERT_SUMMARY}.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003506 *
3507 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
3508 */
3509 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
3510 mN.mGroupAlertBehavior = groupAlertBehavior;
3511 return this;
3512 }
3513
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003514 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003515 * Sets the {@link BubbleMetadata} that will be used to display app content in a floating
3516 * window over the existing foreground activity.
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003517 *
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003518 * <p>This data will be ignored unless the notification is posted to a channel that
3519 * allows {@link NotificationChannel#canBubble() bubbles}.</p>
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003520 *
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003521 * <b>Notifications with a valid and allowed bubble metadata will display in collapsed state
3522 * outside of the notification shade on unlocked devices. When a user interacts with the
3523 * collapsed state, the bubble intent will be invoked and displayed.</b>
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003524 */
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003525 public Builder setBubbleMetadata(BubbleMetadata data) {
3526 mN.mBubbleMetadata = data;
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003527 return this;
3528 }
3529
Jeff Sharkey000ce802017-04-29 13:13:27 -06003530 /** @removed */
3531 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003532 public Builder setChannel(String channelId) {
3533 mN.mChannelId = channelId;
3534 return this;
3535 }
3536
3537 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003538 * Specifies the channel the notification should be delivered on.
3539 */
3540 public Builder setChannelId(String channelId) {
3541 mN.mChannelId = channelId;
3542 return this;
3543 }
3544
Jeff Sharkey000ce802017-04-29 13:13:27 -06003545 /** @removed */
3546 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05003547 public Builder setTimeout(long durationMs) {
3548 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05003549 return this;
3550 }
3551
3552 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003553 * Specifies a duration in milliseconds after which this notification should be canceled,
3554 * if it is not already canceled.
3555 */
3556 public Builder setTimeoutAfter(long durationMs) {
3557 mN.mTimeout = durationMs;
3558 return this;
3559 }
3560
3561 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003562 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003563 *
3564 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
3565 * shown anymore by default and must be opted into by using
3566 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003567 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003568 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08003569 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003570 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003571 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08003572 return this;
3573 }
3574
Joe Onoratocb109a02011-01-18 17:57:41 -08003575 /**
Griff Hazen50c11652014-05-16 09:46:31 -07003576 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07003577 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003578 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
3579 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07003580 */
3581 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003582 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07003583 return this;
3584 }
3585
3586 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003587 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08003588 *
3589 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003590 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003591 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003592 * Useful when showing an elapsed time (like an ongoing phone call).
3593 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08003594 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07003595 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08003596 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003597 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003598 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07003599 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003600 */
3601 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003602 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003603 return this;
3604 }
3605
3606 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08003607 * Sets the Chronometer to count down instead of counting up.
3608 *
3609 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
3610 * If it isn't set the chronometer will count up.
3611 *
3612 * @see #setUsesChronometer(boolean)
3613 */
Adrian Roos96b7e202016-05-17 13:50:38 -07003614 public Builder setChronometerCountDown(boolean countDown) {
3615 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003616 return this;
3617 }
3618
3619 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003620 * Set the small icon resource, which will be used to represent the notification in the
3621 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003622 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003623
3624 * The platform template for the expanded view will draw this icon in the left, unless a
3625 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3626 * icon will be moved to the right-hand side.
3627 *
3628
3629 * @param icon
3630 * A resource ID in the application's package of the drawable to use.
3631 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003632 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003633 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003634 return setSmallIcon(icon != 0
3635 ? Icon.createWithResource(mContext, icon)
3636 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003637 }
3638
Joe Onoratocb109a02011-01-18 17:57:41 -08003639 /**
3640 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3641 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3642 * LevelListDrawable}.
3643 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003644 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003645 * @param level The level to use for the icon.
3646 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003647 * @see Notification#icon
3648 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003649 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003650 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003651 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003652 return setSmallIcon(icon);
3653 }
3654
3655 /**
3656 * Set the small icon, which will be used to represent the notification in the
koprivadebd4ee2018-09-13 10:59:46 -07003657 * status bar and content view (unless overridden there by a
Dan Sandlerd63f9322015-05-06 15:18:49 -04003658 * {@link #setLargeIcon(Bitmap) large icon}).
3659 *
3660 * @param icon An Icon object to use.
3661 * @see Notification#icon
3662 */
3663 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003664 mN.setSmallIcon(icon);
3665 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3666 mN.icon = icon.getResId();
3667 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003668 return this;
3669 }
3670
Joe Onoratocb109a02011-01-18 17:57:41 -08003671 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003672 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003673 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003674 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003675 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003676 return this;
3677 }
3678
Joe Onoratocb109a02011-01-18 17:57:41 -08003679 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003680 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003681 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003682 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003683 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003684 return this;
3685 }
3686
Joe Onoratocb109a02011-01-18 17:57:41 -08003687 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003688 * This provides some additional information that is displayed in the notification. No
3689 * guarantees are given where exactly it is displayed.
3690 *
3691 * <p>This information should only be provided if it provides an essential
3692 * benefit to the understanding of the notification. The more text you provide the
3693 * less readable it becomes. For example, an email client should only provide the account
3694 * name here if more than one email account has been added.</p>
3695 *
3696 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3697 * notification header area.
3698 *
3699 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3700 * this will be shown in the third line of text in the platform notification template.
3701 * You should not be using {@link #setProgress(int, int, boolean)} at the
3702 * same time on those versions; they occupy the same place.
3703 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003704 */
3705 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003706 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003707 return this;
3708 }
3709
3710 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003711 * Provides text that will appear as a link to your application's settings.
3712 *
3713 * <p>This text does not appear within notification {@link Style templates} but may
3714 * appear when the user uses an affordance to learn more about the notification.
3715 * Additionally, this text will not appear unless you provide a valid link target by
3716 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3717 *
3718 * <p>This text is meant to be concise description about what the user can customize
3719 * when they click on this link. The recommended maximum length is 40 characters.
3720 * @param text
3721 * @return
3722 */
3723 public Builder setSettingsText(CharSequence text) {
3724 mN.mSettingsText = safeCharSequence(text);
3725 return this;
3726 }
3727
3728 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003729 * Set the remote input history.
3730 *
3731 * This should be set to the most recent inputs that have been sent
3732 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3733 * longer relevant (e.g. for chat notifications once the other party has responded).
3734 *
3735 * The most recent input must be stored at the 0 index, the second most recent at the
3736 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3737 * and how much of each individual input is shown.
3738 *
3739 * <p>Note: The reply text will only be shown on notifications that have least one action
3740 * with a {@code RemoteInput}.</p>
3741 */
3742 public Builder setRemoteInputHistory(CharSequence[] text) {
3743 if (text == null) {
3744 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3745 } else {
3746 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3747 CharSequence[] safe = new CharSequence[N];
3748 for (int i = 0; i < N; i++) {
3749 safe[i] = safeCharSequence(text[i]);
3750 }
3751 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3752 }
3753 return this;
3754 }
3755
3756 /**
Kenny Guya0f6de82018-04-06 16:20:16 +01003757 * Sets whether remote history entries view should have a spinner.
3758 * @hide
3759 */
3760 public Builder setShowRemoteInputSpinner(boolean showSpinner) {
3761 mN.extras.putBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER, showSpinner);
3762 return this;
3763 }
3764
3765 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +01003766 * Sets whether smart reply buttons should be hidden.
3767 * @hide
3768 */
3769 public Builder setHideSmartReplies(boolean hideSmartReplies) {
3770 mN.extras.putBoolean(EXTRA_HIDE_SMART_REPLIES, hideSmartReplies);
3771 return this;
3772 }
3773
3774 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003775 * Sets the number of items this notification represents. May be displayed as a badge count
3776 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003777 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08003778 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003779 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003780 return this;
3781 }
3782
Joe Onoratocb109a02011-01-18 17:57:41 -08003783 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003784 * A small piece of additional information pertaining to this notification.
3785 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003786 * The platform template will draw this on the last line of the notification, at the far
3787 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003788 *
3789 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3790 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3791 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003792 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003793 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003794 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003795 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003796 return this;
3797 }
3798
Joe Onoratocb109a02011-01-18 17:57:41 -08003799 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003800 * Set the progress this notification represents.
3801 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003802 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003803 */
3804 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003805 mN.extras.putInt(EXTRA_PROGRESS, progress);
3806 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3807 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003808 return this;
3809 }
3810
3811 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003812 * Supply a custom RemoteViews to use instead of the platform template.
3813 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003814 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003815 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003816 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003817 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003818 return setCustomContentView(views);
3819 }
3820
3821 /**
3822 * Supply custom RemoteViews to use instead of the platform template.
3823 *
3824 * This will override the layout that would otherwise be constructed by this Builder
3825 * object.
3826 */
3827 public Builder setCustomContentView(RemoteViews contentView) {
3828 mN.contentView = contentView;
3829 return this;
3830 }
3831
3832 /**
3833 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3834 *
3835 * This will override the expanded layout that would otherwise be constructed by this
3836 * Builder object.
3837 */
3838 public Builder setCustomBigContentView(RemoteViews contentView) {
3839 mN.bigContentView = contentView;
3840 return this;
3841 }
3842
3843 /**
3844 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3845 *
3846 * This will override the heads-up layout that would otherwise be constructed by this
3847 * Builder object.
3848 */
3849 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3850 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003851 return this;
3852 }
3853
Joe Onoratocb109a02011-01-18 17:57:41 -08003854 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003855 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3856 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003857 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3858 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3859 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003860 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003861 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003862 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003863 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003864 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003865 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003866 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003867 return this;
3868 }
3869
Joe Onoratocb109a02011-01-18 17:57:41 -08003870 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003871 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3872 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003873 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003874 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003875 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003876 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003877 return this;
3878 }
3879
Joe Onoratocb109a02011-01-18 17:57:41 -08003880 /**
3881 * An intent to launch instead of posting the notification to the status bar.
3882 * Only for use with extremely high-priority notifications demanding the user's
3883 * <strong>immediate</strong> attention, such as an incoming phone call or
3884 * alarm clock that the user has explicitly set to a particular time.
3885 * If this facility is used for something else, please give the user an option
3886 * to turn it off and use a normal notification, as this can be extremely
3887 * disruptive.
3888 *
Chris Wren47c20a12014-06-18 17:27:29 -04003889 * <p>
3890 * The system UI may choose to display a heads-up notification, instead of
3891 * launching this intent, while the user is using the device.
3892 * </p>
Julia Reynolds47fd15f2018-11-28 10:16:00 -05003893 * <p>Apps targeting {@link Build.VERSION_CODES#Q} and above will have to request
3894 * a permission ({@link android.Manifest.permission#USE_FULL_SCREEN_INTENT}) in order to
3895 * use full screen intents.</p>
Chris Wren47c20a12014-06-18 17:27:29 -04003896 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003897 * @param intent The pending intent to launch.
3898 * @param highPriority Passing true will cause this notification to be sent
3899 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003900 *
3901 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003902 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003903 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003904 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003905 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3906 return this;
3907 }
3908
Joe Onoratocb109a02011-01-18 17:57:41 -08003909 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003910 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003911 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003912 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003913 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003914 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003915 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003916 return this;
3917 }
3918
Joe Onoratocb109a02011-01-18 17:57:41 -08003919 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003920 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003921 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003922 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003923 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003924 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003925 setTicker(tickerText);
3926 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003927 return this;
3928 }
3929
Joe Onoratocb109a02011-01-18 17:57:41 -08003930 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003931 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003932 *
3933 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04003934 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3935 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04003936 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003937 public Builder setLargeIcon(Bitmap b) {
3938 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3939 }
3940
3941 /**
3942 * Add a large icon to the notification content view.
3943 *
3944 * In the platform template, this image will be shown on the left of the notification view
3945 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3946 * badge atop the large icon).
3947 */
3948 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003949 mN.mLargeIcon = icon;
3950 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08003951 return this;
3952 }
3953
Joe Onoratocb109a02011-01-18 17:57:41 -08003954 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003955 * Set the sound to play.
3956 *
John Spurlockc0650f022014-07-19 13:22:39 -04003957 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
3958 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003959 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003960 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003961 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003962 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08003963 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003964 mN.sound = sound;
3965 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08003966 return this;
3967 }
3968
Joe Onoratocb109a02011-01-18 17:57:41 -08003969 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003970 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08003971 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003972 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
3973 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003974 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003975 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07003976 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003977 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08003978 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003979 mN.sound = sound;
3980 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08003981 return this;
3982 }
3983
Joe Onoratocb109a02011-01-18 17:57:41 -08003984 /**
John Spurlockc0650f022014-07-19 13:22:39 -04003985 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
3986 * use during playback.
3987 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003988 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04003989 * @see Notification#sound
3990 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003991 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04003992 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003993 mN.sound = sound;
3994 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04003995 return this;
3996 }
3997
3998 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003999 * Set the vibration pattern to use.
4000 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004001 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
4002 * <code>pattern</code> parameter.
4003 *
Chris Wren47c20a12014-06-18 17:27:29 -04004004 * <p>
4005 * A notification that vibrates is more likely to be presented as a heads-up notification.
4006 * </p>
4007 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004008 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004009 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08004010 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004011 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004012 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004013 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08004014 return this;
4015 }
4016
Joe Onoratocb109a02011-01-18 17:57:41 -08004017 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004018 * Set the desired color for the indicator LED on the device, as well as the
4019 * blink duty cycle (specified in milliseconds).
4020 *
4021
4022 * Not all devices will honor all (or even any) of these values.
4023 *
Julia Reynolds529e3322017-02-06 08:33:01 -05004024 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004025 * @see Notification#ledARGB
4026 * @see Notification#ledOnMS
4027 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08004028 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004029 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08004030 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004031 mN.ledARGB = argb;
4032 mN.ledOnMS = onMs;
4033 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004034 if (onMs != 0 || offMs != 0) {
4035 mN.flags |= FLAG_SHOW_LIGHTS;
4036 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004037 return this;
4038 }
4039
Joe Onoratocb109a02011-01-18 17:57:41 -08004040 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004041 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08004042 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004043
4044 * Ongoing notifications cannot be dismissed by the user, so your application or service
4045 * must take care of canceling them.
4046 *
4047
4048 * They are typically used to indicate a background task that the user is actively engaged
4049 * with (e.g., playing music) or is pending in some way and therefore occupying the device
4050 * (e.g., a file download, sync operation, active network connection).
4051 *
4052
4053 * @see Notification#FLAG_ONGOING_EVENT
Joe Onoratocb109a02011-01-18 17:57:41 -08004054 */
Joe Onorato46439ce2010-11-19 13:56:21 -08004055 public Builder setOngoing(boolean ongoing) {
4056 setFlag(FLAG_ONGOING_EVENT, ongoing);
4057 return this;
4058 }
4059
Joe Onoratocb109a02011-01-18 17:57:41 -08004060 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08004061 * Set whether this notification should be colorized. When set, the color set with
4062 * {@link #setColor(int)} will be used as the background color of this notification.
4063 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08004064 * This should only be used for high priority ongoing tasks like navigation, an ongoing
4065 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08004066 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07004067 * For most styles, the coloring will only be applied if the notification is for a
4068 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08004069 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07004070 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08004071 *
Aurimas Liutikas7f695332018-05-31 21:07:32 -07004072 * @see #setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08004073 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08004074 */
4075 public Builder setColorized(boolean colorize) {
4076 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
4077 return this;
4078 }
4079
4080 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08004081 * Set this flag if you would only like the sound, vibrate
4082 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004083 *
4084 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08004085 */
Joe Onorato46439ce2010-11-19 13:56:21 -08004086 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
4087 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
4088 return this;
4089 }
4090
Joe Onoratocb109a02011-01-18 17:57:41 -08004091 /**
Julia Reynolds04499532016-09-13 14:04:53 -04004092 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004093 *
4094 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08004095 */
Joe Onorato46439ce2010-11-19 13:56:21 -08004096 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08004097 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08004098 return this;
4099 }
4100
Joe Onoratocb109a02011-01-18 17:57:41 -08004101 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08004102 * Set whether or not this notification should not bridge to other devices.
4103 *
4104 * <p>Some notifications can be bridged to other devices for remote display.
4105 * This hint can be set to recommend this notification not be bridged.
4106 */
4107 public Builder setLocalOnly(boolean localOnly) {
4108 setFlag(FLAG_LOCAL_ONLY, localOnly);
4109 return this;
4110 }
4111
4112 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004113 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08004114 * <p>
4115 * The value should be one or more of the following fields combined with
4116 * bitwise-or:
4117 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
4118 * <p>
4119 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004120 *
4121 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05004122 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004123 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08004124 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004125 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004126 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004127 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08004128 return this;
4129 }
4130
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004131 /**
4132 * Set the priority of this notification.
4133 *
4134 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004135 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004136 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004137 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07004138 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004139 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004140 return this;
4141 }
Joe Malin8d40d042012-11-05 11:36:40 -08004142
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004143 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04004144 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08004145 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04004146 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004147 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04004148 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004149 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004150 return this;
4151 }
4152
4153 /**
Chris Wrendde75302014-03-26 17:24:15 -04004154 * Add a person that is relevant to this notification.
4155 *
Chris Wrene6c48932014-09-29 17:19:27 -04004156 * <P>
4157 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04004158 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4159 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4160 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04004161 * </P>
4162 *
4163 * <P>
4164 * The person should be specified by the {@code String} representation of a
4165 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
4166 * </P>
4167 *
4168 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
4169 * URIs. The path part of these URIs must exist in the contacts database, in the
4170 * appropriate column, or the reference will be discarded as invalid. Telephone schema
4171 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
Selim Cineke7238dd2017-12-14 17:48:32 -08004172 * It is also possible to provide a URI with the schema {@code name:} in order to uniquely
4173 * identify a person without an entry in the contacts database.
Chris Wrene6c48932014-09-29 17:19:27 -04004174 * </P>
4175 *
4176 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04004177 * @see Notification#EXTRA_PEOPLE
Selim Cineke7238dd2017-12-14 17:48:32 -08004178 * @deprecated use {@link #addPerson(Person)}
Chris Wrendde75302014-03-26 17:24:15 -04004179 */
Chris Wrene6c48932014-09-29 17:19:27 -04004180 public Builder addPerson(String uri) {
Selim Cinek9acd6732018-03-23 16:39:02 -07004181 addPerson(new Person.Builder().setUri(uri).build());
Selim Cineke7238dd2017-12-14 17:48:32 -08004182 return this;
4183 }
4184
4185 /**
4186 * Add a person that is relevant to this notification.
4187 *
4188 * <P>
4189 * Depending on user preferences, this annotation may allow the notification to pass
4190 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4191 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4192 * appear more prominently in the user interface.
4193 * </P>
4194 *
4195 * <P>
4196 * A person should usually contain a uri in order to benefit from the ranking boost.
4197 * However, even if no uri is provided, it's beneficial to provide other people in the
4198 * notification, such that listeners and voice only devices can announce and handle them
4199 * properly.
4200 * </P>
4201 *
4202 * @param person the person to add.
4203 * @see Notification#EXTRA_PEOPLE_LIST
4204 */
4205 public Builder addPerson(Person person) {
4206 mPersonList.add(person);
Chris Wrendde75302014-03-26 17:24:15 -04004207 return this;
4208 }
4209
4210 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004211 * Set this notification to be part of a group of notifications sharing the same key.
4212 * Grouped notifications may display in a cluster or stack on devices which
4213 * support such rendering.
4214 *
4215 * <p>To make this notification the summary for its group, also call
4216 * {@link #setGroupSummary}. A sort order can be specified for group members by using
4217 * {@link #setSortKey}.
4218 * @param groupKey The group key of the group.
4219 * @return this object for method chaining
4220 */
4221 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004222 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004223 return this;
4224 }
4225
4226 /**
4227 * Set this notification to be the group summary for a group of notifications.
4228 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04004229 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
4230 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004231 * @param isGroupSummary Whether this notification should be a group summary.
4232 * @return this object for method chaining
4233 */
4234 public Builder setGroupSummary(boolean isGroupSummary) {
4235 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
4236 return this;
4237 }
4238
4239 /**
4240 * Set a sort key that orders this notification among other notifications from the
4241 * same package. This can be useful if an external sort was already applied and an app
4242 * would like to preserve this. Notifications will be sorted lexicographically using this
4243 * value, although providing different priorities in addition to providing sort key may
4244 * cause this value to be ignored.
4245 *
4246 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07004247 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004248 *
4249 * @see String#compareTo(String)
4250 */
4251 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004252 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004253 return this;
4254 }
4255
4256 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004257 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004258 *
Griff Hazen720042b2014-02-24 15:46:56 -08004259 * <p>Values within the Bundle will replace existing extras values in this Builder.
4260 *
4261 * @see Notification#extras
4262 */
Griff Hazen959591e2014-05-15 22:26:18 -07004263 public Builder addExtras(Bundle extras) {
4264 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004265 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08004266 }
4267 return this;
4268 }
4269
4270 /**
4271 * Set metadata for this notification.
4272 *
4273 * <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 -04004274 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004275 * called.
4276 *
Griff Hazen720042b2014-02-24 15:46:56 -08004277 * <p>Replaces any existing extras values with those from the provided Bundle.
4278 * Use {@link #addExtras} to merge in metadata instead.
4279 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004280 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004281 */
Griff Hazen959591e2014-05-15 22:26:18 -07004282 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004283 if (extras != null) {
4284 mUserExtras = extras;
4285 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004286 return this;
4287 }
4288
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004289 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004290 * Get the current metadata Bundle used by this notification Builder.
4291 *
4292 * <p>The returned Bundle is shared with this Builder.
4293 *
4294 * <p>The current contents of this Bundle are copied into the Notification each time
4295 * {@link #build()} is called.
4296 *
4297 * @see Notification#extras
4298 */
4299 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004300 return mUserExtras;
4301 }
4302
4303 private Bundle getAllExtras() {
4304 final Bundle saveExtras = (Bundle) mUserExtras.clone();
4305 saveExtras.putAll(mN.extras);
4306 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08004307 }
4308
4309 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004310 * Add an action to this notification. Actions are typically displayed by
4311 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004312 * <p>
4313 * Every action must have an icon (32dp square and matching the
4314 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4315 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4316 * <p>
4317 * A notification in its expanded form can display up to 3 actions, from left to right in
4318 * the order they were added. Actions will not be displayed when the notification is
4319 * collapsed, however, so be sure that any essential functions may be accessed by the user
4320 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004321 *
4322 * @param icon Resource ID of a drawable that represents the action.
4323 * @param title Text describing the action.
4324 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04004325 *
4326 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004327 */
Dan Sandler86647982015-05-13 23:41:13 -04004328 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004329 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004330 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004331 return this;
4332 }
4333
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004334 /**
Griff Hazen959591e2014-05-15 22:26:18 -07004335 * Add an action to this notification. Actions are typically displayed by
4336 * the system as a button adjacent to the notification content.
4337 * <p>
4338 * Every action must have an icon (32dp square and matching the
4339 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4340 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4341 * <p>
4342 * A notification in its expanded form can display up to 3 actions, from left to right in
4343 * the order they were added. Actions will not be displayed when the notification is
4344 * collapsed, however, so be sure that any essential functions may be accessed by the user
4345 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
4346 *
4347 * @param action The action to add.
4348 */
4349 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08004350 if (action != null) {
4351 mActions.add(action);
4352 }
Griff Hazen959591e2014-05-15 22:26:18 -07004353 return this;
4354 }
4355
4356 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004357 * Alter the complete list of actions attached to this notification.
4358 * @see #addAction(Action).
4359 *
4360 * @param actions
4361 * @return
4362 */
4363 public Builder setActions(Action... actions) {
4364 mActions.clear();
4365 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08004366 if (actions[i] != null) {
4367 mActions.add(actions[i]);
4368 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004369 }
4370 return this;
4371 }
4372
4373 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004374 * Add a rich notification style to be applied at build time.
4375 *
4376 * @param style Object responsible for modifying the notification style.
4377 */
4378 public Builder setStyle(Style style) {
4379 if (mStyle != style) {
4380 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004381 if (mStyle != null) {
4382 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004383 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
4384 } else {
4385 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004386 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004387 }
4388 return this;
4389 }
4390
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004391 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05004392 * Returns the style set by {@link #setStyle(Style)}.
4393 */
4394 public Style getStyle() {
4395 return mStyle;
4396 }
4397
4398 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004399 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07004400 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004401 * @return The same Builder.
4402 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06004403 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004404 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004405 return this;
4406 }
4407
4408 /**
4409 * Supply a replacement Notification whose contents should be shown in insecure contexts
4410 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
4411 * @param n A replacement notification, presumably with some or all info redacted.
4412 * @return The same Builder.
4413 */
4414 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004415 if (n != null) {
4416 mN.publicVersion = new Notification();
4417 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
4418 } else {
4419 mN.publicVersion = null;
4420 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004421 return this;
4422 }
4423
Griff Hazenb720abe2014-05-20 13:15:30 -07004424 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004425 * Apply an extender to this notification builder. Extenders may be used to add
4426 * metadata or change options on this builder.
4427 */
Griff Hazen61a9e862014-05-22 16:05:19 -07004428 public Builder extend(Extender extender) {
4429 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004430 return this;
4431 }
4432
Dan Sandler4e787062015-06-17 15:09:48 -04004433 /**
4434 * @hide
4435 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04004436 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08004437 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004438 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004439 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004440 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004441 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04004442 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08004443 }
4444
Dan Sandler26e81cf2014-05-06 10:01:27 -04004445 /**
4446 * Sets {@link Notification#color}.
4447 *
4448 * @param argb The accent color to use
4449 *
4450 * @return The same Builder.
4451 */
Tor Norbye80756e32015-03-02 09:39:27 -08004452 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004453 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004454 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04004455 return this;
4456 }
4457
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004458 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04004459 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
4460 // This user can never be a badged profile,
4461 // and also includes USER_ALL system notifications.
4462 return null;
4463 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02004464 // Note: This assumes that the current user can read the profile badge of the
4465 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08004466 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05004467 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004468 }
4469
4470 private Bitmap getProfileBadge() {
4471 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01004472 if (badge == null) {
4473 return null;
4474 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004475 final int size = mContext.getResources().getDimensionPixelSize(
4476 R.dimen.notification_badge_size);
4477 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004478 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004479 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004480 badge.draw(canvas);
4481 return bitmap;
4482 }
4483
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004484 private void bindProfileBadge(RemoteViews contentView, StandardTemplateParams p) {
Kenny Guy98193ea2014-07-24 19:54:37 +01004485 Bitmap profileBadge = getProfileBadge();
4486
Kenny Guy98193ea2014-07-24 19:54:37 +01004487 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004488 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
4489 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004490 if (isColorized(p)) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004491 contentView.setDrawableTint(R.id.profile_badge, false,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004492 getPrimaryTextColor(p), PorterDuff.Mode.SRC_ATOP);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004493 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004494 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004495 }
4496
Gus Prevasbae5ba32018-11-28 15:45:13 -05004497 private void bindAlertedIcon(RemoteViews contentView, StandardTemplateParams p) {
4498 contentView.setDrawableTint(
4499 R.id.alerted_icon,
4500 false /* targetBackground */,
4501 getNeutralColor(p),
4502 PorterDuff.Mode.SRC_ATOP);
4503 }
4504
Julia Reynoldsfc640012018-02-21 12:25:27 -05004505 /**
4506 * @hide
4507 */
4508 public boolean usesStandardHeader() {
4509 if (mN.mUsesStandardHeader) {
4510 return true;
4511 }
4512 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.N) {
4513 if (mN.contentView == null && mN.bigContentView == null) {
4514 return true;
4515 }
4516 }
4517 boolean contentViewUsesHeader = mN.contentView == null
4518 || STANDARD_LAYOUTS.contains(mN.contentView.getLayoutId());
4519 boolean bigContentViewUsesHeader = mN.bigContentView == null
4520 || STANDARD_LAYOUTS.contains(mN.bigContentView.getLayoutId());
4521 return contentViewUsesHeader && bigContentViewUsesHeader;
4522 }
4523
Christoph Studerfe718432014-09-01 18:21:18 +02004524 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004525 resetNotificationHeader(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02004526 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08004527 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004528 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08004529 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004530 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08004531 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08004532 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004533 }
4534
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004535 /**
4536 * Resets the notification header to its original state
4537 */
4538 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07004539 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
4540 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08004541 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004542 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02004543 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004544 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07004545 contentView.setTextViewText(R.id.header_text, null);
Selim Cinekafeed292017-12-12 17:32:44 -08004546 contentView.setViewVisibility(R.id.header_text_secondary, View.GONE);
4547 contentView.setTextViewText(R.id.header_text_secondary, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004548 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinekafeed292017-12-12 17:32:44 -08004549 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004550 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004551 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004552 contentView.setImageViewIcon(R.id.profile_badge, null);
4553 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Gus Prevasa3226492018-10-23 11:10:09 -04004554 contentView.setViewVisibility(R.id.alerted_icon, View.GONE);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004555 mN.mUsesStandardHeader = false;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004556 }
4557
Selim Cinek384804b2018-04-18 14:31:07 +08004558 private RemoteViews applyStandardTemplate(int resId, TemplateBindResult result) {
4559 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this),
4560 result);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004561 }
4562
Selim Cinek384804b2018-04-18 14:31:07 +08004563 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p,
4564 TemplateBindResult result) {
Kenny Guy77320062014-08-27 21:37:15 +01004565 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04004566
Christoph Studerfe718432014-09-01 18:21:18 +02004567 resetStandardTemplate(contentView);
4568
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004569 final Bundle ex = mN.extras;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004570 updateBackgroundColor(contentView, p);
4571 bindNotificationHeader(contentView, p);
Selim Cinek384804b2018-04-18 14:31:07 +08004572 bindLargeIconAndReply(contentView, p, result);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004573 boolean showProgress = handleProgressBar(contentView, ex, p);
Adrian Roos70d7aa32017-01-11 15:39:06 -08004574 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08004575 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004576 contentView.setTextViewText(R.id.title, processTextSpans(p.title));
Lucas Dupin00be88f2019-01-03 17:50:52 -08004577 setTextViewColorPrimary(contentView, R.id.title, p);
Selim Cinek954cc232016-05-20 13:29:23 -07004578 contentView.setViewLayoutWidth(R.id.title, showProgress
4579 ? ViewGroup.LayoutParams.WRAP_CONTENT
4580 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08004581 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08004582 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08004583 int textId = showProgress ? com.android.internal.R.id.text_line_1
4584 : com.android.internal.R.id.text;
Selim Cinek48f66b72017-08-18 16:17:51 -07004585 contentView.setTextViewText(textId, processTextSpans(p.text));
Lucas Dupin00be88f2019-01-03 17:50:52 -08004586 setTextViewColorSecondary(contentView, textId, p);
Selim Cinek41598732016-01-11 16:58:37 -08004587 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08004588 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08004589
Selim Cinek279fa862016-06-14 10:57:25 -07004590 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004591
Selim Cinek29603462015-11-17 19:04:39 -08004592 return contentView;
4593 }
4594
Selim Cinek48f66b72017-08-18 16:17:51 -07004595 private CharSequence processTextSpans(CharSequence text) {
Lucas Dupind3c99322018-09-27 10:22:29 -07004596 if (hasForegroundColor() || mInNightMode) {
Lucas Dupina291d192018-06-07 13:59:42 -07004597 return ContrastColorUtil.clearColorSpans(text);
Selim Cinek48f66b72017-08-18 16:17:51 -07004598 }
4599 return text;
4600 }
4601
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004602 private void setTextViewColorPrimary(RemoteViews contentView, int id,
4603 StandardTemplateParams p) {
4604 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004605 contentView.setTextColor(id, mPrimaryTextColor);
4606 }
4607
Selim Cinek48f66b72017-08-18 16:17:51 -07004608 private boolean hasForegroundColor() {
4609 return mForegroundColor != COLOR_INVALID;
4610 }
4611
Selim Cinek389edcd2017-05-11 19:16:44 -07004612 /**
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004613 * Return the primary text color using the existing template params
Selim Cinek389edcd2017-05-11 19:16:44 -07004614 * @hide
4615 */
4616 @VisibleForTesting
4617 public int getPrimaryTextColor() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004618 return getPrimaryTextColor(mParams);
4619 }
4620
4621 /**
4622 * @param p the template params to inflate this with
4623 * @return the primary text color
4624 * @hide
4625 */
4626 @VisibleForTesting
4627 public int getPrimaryTextColor(StandardTemplateParams p) {
4628 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004629 return mPrimaryTextColor;
4630 }
4631
Selim Cinek389edcd2017-05-11 19:16:44 -07004632 /**
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004633 * Return the secondary text color using the existing template params
Selim Cinek389edcd2017-05-11 19:16:44 -07004634 * @hide
4635 */
4636 @VisibleForTesting
4637 public int getSecondaryTextColor() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004638 return getSecondaryTextColor(mParams);
4639 }
4640
4641 /**
4642 * @param p the template params to inflate this with
4643 * @return the secondary text color
4644 * @hide
4645 */
4646 @VisibleForTesting
4647 public int getSecondaryTextColor(StandardTemplateParams p) {
4648 ensureColors(p);
Selim Cinek389edcd2017-05-11 19:16:44 -07004649 return mSecondaryTextColor;
4650 }
4651
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004652 private void setTextViewColorSecondary(RemoteViews contentView, int id,
4653 StandardTemplateParams p) {
4654 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004655 contentView.setTextColor(id, mSecondaryTextColor);
4656 }
4657
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004658 private void ensureColors(StandardTemplateParams p) {
4659 int backgroundColor = getBackgroundColor(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004660 if (mPrimaryTextColor == COLOR_INVALID
4661 || mSecondaryTextColor == COLOR_INVALID
Selim Cinek7b9605b2017-01-19 17:36:00 -08004662 || mTextColorsAreForBackground != backgroundColor) {
4663 mTextColorsAreForBackground = backgroundColor;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004664 if (!hasForegroundColor() || !isColorized(p)) {
Lucas Dupina291d192018-06-07 13:59:42 -07004665 mPrimaryTextColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004666 backgroundColor, mInNightMode);
Lucas Dupina291d192018-06-07 13:59:42 -07004667 mSecondaryTextColor = ContrastColorUtil.resolveSecondaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004668 backgroundColor, mInNightMode);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004669 if (backgroundColor != COLOR_DEFAULT && isColorized(p)) {
Lucas Dupina291d192018-06-07 13:59:42 -07004670 mPrimaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004671 mPrimaryTextColor, backgroundColor, 4.5);
Lucas Dupina291d192018-06-07 13:59:42 -07004672 mSecondaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004673 mSecondaryTextColor, backgroundColor, 4.5);
4674 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07004675 } else {
Lucas Dupina291d192018-06-07 13:59:42 -07004676 double backLum = ContrastColorUtil.calculateLuminance(backgroundColor);
4677 double textLum = ContrastColorUtil.calculateLuminance(mForegroundColor);
4678 double contrast = ContrastColorUtil.calculateContrast(mForegroundColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004679 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07004680 // We only respect the given colors if worst case Black or White still has
4681 // contrast
4682 boolean backgroundLight = backLum > textLum
4683 && satisfiesTextContrast(backgroundColor, Color.BLACK)
4684 || backLum <= textLum
4685 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004686 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07004687 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004688 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004689 mForegroundColor,
4690 backgroundColor,
4691 true /* findFG */,
4692 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004693 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004694 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004695 } else {
4696 mSecondaryTextColor =
Lucas Dupina291d192018-06-07 13:59:42 -07004697 ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004698 mForegroundColor,
4699 backgroundColor,
4700 true /* findFG */,
4701 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004702 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004703 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004704 }
4705 } else {
4706 mPrimaryTextColor = mForegroundColor;
Lucas Dupina291d192018-06-07 13:59:42 -07004707 mSecondaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004708 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4709 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Lucas Dupina291d192018-06-07 13:59:42 -07004710 if (ContrastColorUtil.calculateContrast(mSecondaryTextColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004711 backgroundColor) < 4.5f) {
4712 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07004713 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004714 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004715 mSecondaryTextColor,
4716 backgroundColor,
4717 true /* findFG */,
4718 4.5f);
4719 } else {
4720 mSecondaryTextColor
Lucas Dupina291d192018-06-07 13:59:42 -07004721 = ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004722 mSecondaryTextColor,
4723 backgroundColor,
4724 true /* findFG */,
4725 4.5f);
4726 }
Lucas Dupina291d192018-06-07 13:59:42 -07004727 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004728 mSecondaryTextColor, backgroundLight
4729 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4730 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004731 }
4732 }
4733 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004734 }
4735 }
4736
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004737 private void updateBackgroundColor(RemoteViews contentView,
4738 StandardTemplateParams p) {
4739 if (isColorized(p)) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004740 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004741 getBackgroundColor(p));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004742 } else {
4743 // Clear it!
4744 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4745 0);
4746 }
4747 }
4748
Selim Cinek860b6da2015-12-16 19:02:19 -08004749 /**
4750 * @param remoteView the remote view to update the minheight in
4751 * @param hasMinHeight does it have a mimHeight
4752 * @hide
4753 */
4754 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4755 int minHeight = 0;
4756 if (hasMinHeight) {
4757 // we need to set the minHeight of the notification
4758 minHeight = mContext.getResources().getDimensionPixelSize(
4759 com.android.internal.R.dimen.notification_min_content_height);
4760 }
4761 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4762 }
4763
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004764 private boolean handleProgressBar(RemoteViews contentView, Bundle ex,
4765 StandardTemplateParams p) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004766 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4767 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4768 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004769 if (p.hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004770 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004771 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004772 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004773 contentView.setProgressBackgroundTintList(
4774 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4775 R.color.notification_progress_background_color)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004776 if (getRawColor(p) != COLOR_DEFAULT) {
4777 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor(p));
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004778 contentView.setProgressTintList(R.id.progress, colorStateList);
4779 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004780 }
Selim Cinek29603462015-11-17 19:04:39 -08004781 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004782 } else {
4783 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004784 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004785 }
Joe Onorato561d3852010-11-20 18:09:34 -08004786 }
4787
Selim Cinek384804b2018-04-18 14:31:07 +08004788 private void bindLargeIconAndReply(RemoteViews contentView, StandardTemplateParams p,
4789 TemplateBindResult result) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004790 boolean largeIconShown = bindLargeIcon(contentView, p);
4791 boolean replyIconShown = bindReplyIcon(contentView, p);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004792 boolean iconContainerVisible = largeIconShown || replyIconShown;
Selim Cinek384804b2018-04-18 14:31:07 +08004793 contentView.setViewVisibility(R.id.right_icon_container,
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004794 iconContainerVisible ? View.VISIBLE : View.GONE);
Selim Cinek1c72fa02018-04-23 18:00:54 +08004795 int marginEnd = calculateMarginEnd(largeIconShown, replyIconShown);
4796 contentView.setViewLayoutMarginEnd(R.id.line1, marginEnd);
4797 contentView.setViewLayoutMarginEnd(R.id.text, marginEnd);
4798 contentView.setViewLayoutMarginEnd(R.id.progress, marginEnd);
Selim Cinek384804b2018-04-18 14:31:07 +08004799 if (result != null) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08004800 result.setIconMarginEnd(marginEnd);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004801 result.setRightIconContainerVisible(iconContainerVisible);
Selim Cinek384804b2018-04-18 14:31:07 +08004802 }
4803 }
4804
Selim Cinek1c72fa02018-04-23 18:00:54 +08004805 private int calculateMarginEnd(boolean largeIconShown, boolean replyIconShown) {
4806 int marginEnd = 0;
4807 int contentMargin = mContext.getResources().getDimensionPixelSize(
4808 R.dimen.notification_content_margin_end);
4809 int iconSize = mContext.getResources().getDimensionPixelSize(
4810 R.dimen.notification_right_icon_size);
4811 if (replyIconShown) {
4812 // The size of the reply icon
4813 marginEnd += iconSize;
4814
4815 int replyInset = mContext.getResources().getDimensionPixelSize(
4816 R.dimen.notification_reply_inset);
4817 // We're subtracting the inset of the reply icon to make sure it's
4818 // aligned nicely on the right, and remove it from the following padding
4819 marginEnd -= replyInset * 2;
4820 }
4821 if (largeIconShown) {
4822 // adding size of the right icon
4823 marginEnd += iconSize;
4824
4825 if (replyIconShown) {
4826 // We also add some padding to the reply icon if it's around
4827 marginEnd += contentMargin;
4828 }
4829 }
4830 if (replyIconShown || largeIconShown) {
4831 // The padding to the content
4832 marginEnd += contentMargin;
4833 }
4834 return marginEnd;
4835 }
4836
Selim Cinek384804b2018-04-18 14:31:07 +08004837 /**
4838 * Bind the large icon.
4839 * @return if the largeIcon is visible
4840 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004841 private boolean bindLargeIcon(RemoteViews contentView, StandardTemplateParams p) {
Selim Cinek279fa862016-06-14 10:57:25 -07004842 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4843 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4844 }
Lucas Dupin00be88f2019-01-03 17:50:52 -08004845 boolean showLargeIcon = mN.mLargeIcon != null && !p.hideLargeIcon;
Selim Cinek88188f22017-09-19 16:46:56 -07004846 if (showLargeIcon) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004847 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4848 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004849 processLargeLegacyIcon(mN.mLargeIcon, contentView, p);
Selim Cinek88188f22017-09-19 16:46:56 -07004850 }
Selim Cinek384804b2018-04-18 14:31:07 +08004851 return showLargeIcon;
4852 }
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004853
Selim Cinek384804b2018-04-18 14:31:07 +08004854 /**
4855 * Bind the reply icon.
4856 * @return if the reply icon is visible
4857 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004858 private boolean bindReplyIcon(RemoteViews contentView, StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08004859 boolean actionVisible = !p.hideReplyIcon;
Selim Cinek384804b2018-04-18 14:31:07 +08004860 Action action = null;
Selim Cinek88188f22017-09-19 16:46:56 -07004861 if (actionVisible) {
Selim Cinek384804b2018-04-18 14:31:07 +08004862 action = findReplyAction();
4863 actionVisible = action != null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004864 }
Selim Cinek384804b2018-04-18 14:31:07 +08004865 if (actionVisible) {
4866 contentView.setViewVisibility(R.id.reply_icon_action, View.VISIBLE);
4867 contentView.setDrawableTint(R.id.reply_icon_action,
4868 false /* targetBackground */,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004869 getNeutralColor(p),
Selim Cinek384804b2018-04-18 14:31:07 +08004870 PorterDuff.Mode.SRC_ATOP);
4871 contentView.setOnClickPendingIntent(R.id.reply_icon_action, action.actionIntent);
4872 contentView.setRemoteInputs(R.id.reply_icon_action, action.mRemoteInputs);
4873 } else {
4874 contentView.setRemoteInputs(R.id.reply_icon_action, null);
4875 }
4876 contentView.setViewVisibility(R.id.reply_icon_action,
4877 actionVisible ? View.VISIBLE : View.GONE);
4878 return actionVisible;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004879 }
4880
4881 private Action findReplyAction() {
4882 ArrayList<Action> actions = mActions;
4883 if (mOriginalActions != null) {
4884 actions = mOriginalActions;
4885 }
4886 int numActions = actions.size();
4887 for (int i = 0; i < numActions; i++) {
4888 Action action = actions.get(i);
4889 if (hasValidRemoteInput(action)) {
4890 return action;
4891 }
4892 }
4893 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004894 }
4895
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004896 private void bindNotificationHeader(RemoteViews contentView, StandardTemplateParams p) {
4897 bindSmallIcon(contentView, p);
4898 bindHeaderAppName(contentView, p);
Lucas Dupin00be88f2019-01-03 17:50:52 -08004899 bindHeaderText(contentView, p);
4900 bindHeaderTextSecondary(contentView, p);
4901 bindHeaderChronometerAndTime(contentView, p);
4902 bindProfileBadge(contentView, p);
4903 bindAlertedIcon(contentView, p);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004904 bindActivePermissions(contentView, p);
4905 bindExpandButton(contentView, p);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004906 mN.mUsesStandardHeader = true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004907 }
4908
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004909 private void bindActivePermissions(RemoteViews contentView, StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08004910 int color = getNeutralColor(p);
Julia Reynoldsb887b232018-04-10 16:38:08 -04004911 contentView.setDrawableTint(R.id.camera, false, color, PorterDuff.Mode.SRC_ATOP);
4912 contentView.setDrawableTint(R.id.mic, false, color, PorterDuff.Mode.SRC_ATOP);
4913 contentView.setDrawableTint(R.id.overlay, false, color, PorterDuff.Mode.SRC_ATOP);
4914 }
4915
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004916 private void bindExpandButton(RemoteViews contentView, StandardTemplateParams p) {
4917 int color = isColorized(p) ? getPrimaryTextColor(p) : getSecondaryTextColor(p);
Sunny Goyal5b153922017-09-21 21:00:36 -07004918 contentView.setDrawableTint(R.id.expand_button, false, color,
4919 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08004920 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08004921 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004922 }
4923
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004924 private void bindHeaderChronometerAndTime(RemoteViews contentView,
4925 StandardTemplateParams p) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004926 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08004927 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004928 setTextViewColorSecondary(contentView, R.id.time_divider, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004929 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
4930 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
4931 contentView.setLong(R.id.chronometer, "setBase",
4932 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
4933 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07004934 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07004935 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004936 setTextViewColorSecondary(contentView, R.id.chronometer, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004937 } else {
4938 contentView.setViewVisibility(R.id.time, View.VISIBLE);
4939 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004940 setTextViewColorSecondary(contentView, R.id.time, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004941 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004942 } else {
4943 // We still want a time to be set but gone, such that we can show and hide it
4944 // on demand in case it's a child notification without anything in the header
4945 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004946 }
4947 }
4948
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004949 private void bindHeaderText(RemoteViews contentView, StandardTemplateParams p) {
4950 CharSequence summaryText = p.summaryText;
4951 if (summaryText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05004952 && mStyle.hasSummaryInHeader()) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004953 summaryText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05004954 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004955 if (summaryText == null
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004956 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4957 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004958 summaryText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004959 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004960 if (summaryText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004961 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek48f66b72017-08-18 16:17:51 -07004962 contentView.setTextViewText(R.id.header_text, processTextSpans(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004963 processLegacyText(summaryText)));
4964 setTextViewColorSecondary(contentView, R.id.header_text, p);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004965 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
4966 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004967 setTextViewColorSecondary(contentView, R.id.header_text_divider, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004968 }
4969 }
4970
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004971 private void bindHeaderTextSecondary(RemoteViews contentView, StandardTemplateParams p) {
4972 if (!TextUtils.isEmpty(p.headerTextSecondary)) {
Selim Cinekafeed292017-12-12 17:32:44 -08004973 contentView.setTextViewText(R.id.header_text_secondary, processTextSpans(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004974 processLegacyText(p.headerTextSecondary)));
4975 setTextViewColorSecondary(contentView, R.id.header_text_secondary, p);
Selim Cinekafeed292017-12-12 17:32:44 -08004976 contentView.setViewVisibility(R.id.header_text_secondary, View.VISIBLE);
4977 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004978 setTextViewColorSecondary(contentView, R.id.header_text_secondary_divider, p);
Selim Cinekafeed292017-12-12 17:32:44 -08004979 }
4980 }
4981
Adrian Rooseba05822016-04-22 17:09:27 -07004982 /**
4983 * @hide
4984 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01004985 @UnsupportedAppUsage
Adrian Rooseba05822016-04-22 17:09:27 -07004986 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004987 CharSequence name = null;
4988 final PackageManager pm = mContext.getPackageManager();
4989 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
4990 // only system packages which lump together a bunch of unrelated stuff
4991 // may substitute a different name to make the purpose of the
4992 // notification more clear. the correct package label should always
4993 // be accessible via SystemUI.
4994 final String pkg = mContext.getPackageName();
4995 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
4996 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
4997 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
4998 name = subName;
4999 } else {
5000 Log.w(TAG, "warning: pkg "
5001 + pkg + " attempting to substitute app name '" + subName
5002 + "' without holding perm "
5003 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
5004 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005005 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04005006 if (TextUtils.isEmpty(name)) {
5007 name = pm.getApplicationLabel(mContext.getApplicationInfo());
5008 }
5009 if (TextUtils.isEmpty(name)) {
5010 // still nothing?
5011 return null;
5012 }
5013
5014 return String.valueOf(name);
5015 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005016 private void bindHeaderAppName(RemoteViews contentView, StandardTemplateParams p) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04005017 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005018 if (isColorized(p)) {
5019 setTextViewColorPrimary(contentView, R.id.app_name_text, p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005020 } else {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005021 contentView.setTextColor(R.id.app_name_text, getSecondaryTextColor(p));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005022 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005023 }
5024
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005025 private boolean isColorized(StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005026 return p.allowColorization && mN.isColorized();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005027 }
5028
5029 private void bindSmallIcon(RemoteViews contentView, StandardTemplateParams p) {
Selim Cinek279fa862016-06-14 10:57:25 -07005030 if (mN.mSmallIcon == null && mN.icon != 0) {
5031 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
5032 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005033 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Sunny Goyal5b153922017-09-21 21:00:36 -07005034 contentView.setInt(R.id.icon, "setImageLevel", mN.iconLevel);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005035 processSmallIconColor(mN.mSmallIcon, contentView, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005036 }
5037
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005038 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005039 * @return true if the built notification will show the time or the chronometer; false
5040 * otherwise
5041 */
5042 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07005043 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005044 }
5045
Christoph Studerfe718432014-09-01 18:21:18 +02005046 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07005047 // actions_container is only reset when there are no actions to avoid focus issues with
5048 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02005049 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02005050 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08005051
5052 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
5053 big.setTextViewText(R.id.notification_material_reply_text_1, null);
Kenny Guya0f6de82018-04-06 16:20:16 +01005054 big.setViewVisibility(R.id.notification_material_reply_text_1_container, View.GONE);
5055 big.setViewVisibility(R.id.notification_material_reply_progress, View.GONE);
Adrian Roose458aa82015-12-08 16:17:19 -08005056
5057 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
5058 big.setTextViewText(R.id.notification_material_reply_text_2, null);
5059 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
5060 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07005061
Selim Cineked64a142018-02-06 18:06:01 -08005062 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
5063 R.dimen.notification_content_margin);
Christoph Studerfe718432014-09-01 18:21:18 +02005064 }
5065
Selim Cinek384804b2018-04-18 14:31:07 +08005066 private RemoteViews applyStandardTemplateWithActions(int layoutId,
5067 TemplateBindResult result) {
5068 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this),
5069 result);
Adrian Roos48d746a2016-04-12 14:57:28 -07005070 }
5071
Gustav Sennton1463d832018-11-06 16:12:48 +00005072 private static List<Notification.Action> filterOutContextualActions(
5073 List<Notification.Action> actions) {
5074 List<Notification.Action> nonContextualActions = new ArrayList<>();
5075 for (Notification.Action action : actions) {
Gustav Sennton005d7a02019-01-04 13:41:32 +00005076 if (!action.isContextual()) {
Gustav Sennton1463d832018-11-06 16:12:48 +00005077 nonContextualActions.add(action);
5078 }
5079 }
5080 return nonContextualActions;
5081 }
5082
Adrian Roos70d7aa32017-01-11 15:39:06 -08005083 private RemoteViews applyStandardTemplateWithActions(int layoutId,
Selim Cinek384804b2018-04-18 14:31:07 +08005084 StandardTemplateParams p, TemplateBindResult result) {
5085 RemoteViews big = applyStandardTemplate(layoutId, p, result);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005086
Christoph Studerfe718432014-09-01 18:21:18 +02005087 resetStandardTemplateWithActions(big);
5088
Adrian Roose458aa82015-12-08 16:17:19 -08005089 boolean validRemoteInput = false;
5090
Gustav Sennton1463d832018-11-06 16:12:48 +00005091 // In the UI contextual actions appear separately from the standard actions, so we
5092 // filter them out here.
5093 List<Notification.Action> nonContextualActions = filterOutContextualActions(mActions);
5094
5095 int N = nonContextualActions.size();
Lucas Dupin00be88f2019-01-03 17:50:52 -08005096 boolean emphazisedMode = mN.fullScreenIntent != null;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005097 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005098 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08005099 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005100 big.setViewVisibility(R.id.actions, View.VISIBLE);
Selim Cineked64a142018-02-06 18:06:01 -08005101 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Daniel Sandler8680bf82012-05-15 16:52:52 -04005102 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005103 for (int i=0; i<N; i++) {
Gustav Sennton1463d832018-11-06 16:12:48 +00005104 Action action = nonContextualActions.get(i);
5105
Selim Cinekffcc7cf2018-02-06 17:43:51 -08005106 boolean actionHasValidInput = hasValidRemoteInput(action);
5107 validRemoteInput |= actionHasValidInput;
Adrian Roose458aa82015-12-08 16:17:19 -08005108
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005109 final RemoteViews button = generateActionButton(action, emphazisedMode, p);
Selim Cinek396caca2018-04-10 17:46:46 -07005110 if (actionHasValidInput && !emphazisedMode) {
Selim Cinekffcc7cf2018-02-06 17:43:51 -08005111 // Clear the drawable
5112 button.setInt(R.id.action0, "setBackgroundResource", 0);
5113 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005114 big.addView(R.id.actions, button);
5115 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07005116 } else {
5117 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005118 }
Adrian Roose458aa82015-12-08 16:17:19 -08005119
5120 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Lucas Dupin00be88f2019-01-03 17:50:52 -08005121 if (validRemoteInput && replyText != null
Selim Cinekbee4e072018-05-21 22:06:43 -07005122 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])
5123 && p.maxRemoteInputHistory > 0) {
Kenny Guya0f6de82018-04-06 16:20:16 +01005124 boolean showSpinner = mN.extras.getBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER);
Adrian Roose458aa82015-12-08 16:17:19 -08005125 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Kenny Guya0f6de82018-04-06 16:20:16 +01005126 big.setViewVisibility(R.id.notification_material_reply_text_1_container,
5127 View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005128 big.setTextViewText(R.id.notification_material_reply_text_1,
5129 processTextSpans(replyText[0]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005130 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1, p);
Kenny Guya0f6de82018-04-06 16:20:16 +01005131 big.setViewVisibility(R.id.notification_material_reply_progress,
5132 showSpinner ? View.VISIBLE : View.GONE);
5133 big.setProgressIndeterminateTintList(
5134 R.id.notification_material_reply_progress,
5135 ColorStateList.valueOf(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005136 isColorized(p) ? getPrimaryTextColor(p) : resolveContrastColor(p)));
Adrian Roose458aa82015-12-08 16:17:19 -08005137
Selim Cinekbee4e072018-05-21 22:06:43 -07005138 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])
5139 && p.maxRemoteInputHistory > 1) {
Adrian Roose458aa82015-12-08 16:17:19 -08005140 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005141 big.setTextViewText(R.id.notification_material_reply_text_2,
5142 processTextSpans(replyText[1]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005143 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2, p);
Adrian Roose458aa82015-12-08 16:17:19 -08005144
Selim Cinekbee4e072018-05-21 22:06:43 -07005145 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])
5146 && p.maxRemoteInputHistory > 2) {
Adrian Roose458aa82015-12-08 16:17:19 -08005147 big.setViewVisibility(
5148 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005149 big.setTextViewText(R.id.notification_material_reply_text_3,
5150 processTextSpans(replyText[2]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005151 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3, p);
Adrian Roose458aa82015-12-08 16:17:19 -08005152 }
5153 }
5154 }
5155
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005156 return big;
5157 }
5158
Adrian Roose458aa82015-12-08 16:17:19 -08005159 private boolean hasValidRemoteInput(Action action) {
5160 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
5161 // Weird actions
5162 return false;
5163 }
5164
5165 RemoteInput[] remoteInputs = action.getRemoteInputs();
5166 if (remoteInputs == null) {
5167 return false;
5168 }
5169
5170 for (RemoteInput r : remoteInputs) {
5171 CharSequence[] choices = r.getChoices();
5172 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
5173 return true;
5174 }
5175 }
5176 return false;
5177 }
5178
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005179 /**
5180 * Construct a RemoteViews for the final 1U notification layout. In order:
5181 * 1. Custom contentView from the caller
5182 * 2. Style's proposed content view
5183 * 3. Standard template view
5184 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005185 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005186 return createContentView(false /* increasedheight */ );
5187 }
5188
5189 /**
5190 * Construct a RemoteViews for the smaller content view.
5191 *
5192 * @param increasedHeight true if this layout be created with an increased height. Some
5193 * styles may support showing more then just that basic 1U size
5194 * and the system may decide to render important notifications
5195 * slightly bigger even when collapsed.
5196 *
5197 * @hide
5198 */
5199 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005200 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005201 return mN.contentView;
5202 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005203 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005204 if (styleView != null) {
5205 return styleView;
5206 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005207 }
Selim Cinek384804b2018-04-18 14:31:07 +08005208 return applyStandardTemplate(getBaseLayoutResource(), null /* result */);
Joe Onorato46439ce2010-11-19 13:56:21 -08005209 }
5210
Selim Cineka7679b62017-05-10 16:33:25 -07005211 private boolean useExistingRemoteView() {
5212 return mStyle == null || (!mStyle.displayCustomViewInline()
5213 && !mRebuildStyledRemoteViews);
5214 }
5215
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005216 /**
5217 * Construct a RemoteViews for the final big notification layout.
5218 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005219 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05005220 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07005221 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005222 return mN.bigContentView;
5223 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05005224 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08005225 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005226 } else if (mActions.size() != 0) {
Selim Cinek384804b2018-04-18 14:31:07 +08005227 result = applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5228 null /* result */);
Selim Cinek850a8542015-11-11 11:48:36 -05005229 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08005230 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05005231 return result;
5232 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005233
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005234 /**
Selim Cinek414ad332017-02-24 19:06:12 -08005235 * Construct a RemoteViews for the final notification header only. This will not be
5236 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005237 *
5238 * @hide
5239 */
Lucas Dupin00be88f2019-01-03 17:50:52 -08005240 public RemoteViews makeNotificationHeader() {
5241 return makeNotificationHeader(mParams.reset().fillTextsFrom(this));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005242 }
5243
5244 /**
5245 * Construct a RemoteViews for the final notification header only. This will not be
5246 * colorized.
5247 *
5248 * @param p the template params to inflate this with
5249 */
5250 private RemoteViews makeNotificationHeader(StandardTemplateParams p) {
5251 // Headers on their own are never colorized
5252 p.disallowColorization();
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005253 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Lucas Dupin00be88f2019-01-03 17:50:52 -08005254 R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005255 resetNotificationHeader(header);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005256 bindNotificationHeader(header, p);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005257 return header;
5258 }
5259
Adrian Roos487374f2017-01-11 15:48:14 -08005260 /**
5261 * Construct a RemoteViews for the ambient version of the notification.
5262 *
5263 * @hide
5264 */
5265 public RemoteViews makeAmbientNotification() {
Selim Cinek0e069942019-01-24 16:09:41 -08005266 RemoteViews headsUpContentView = createHeadsUpContentView(false /* increasedHeight */);
5267 if (headsUpContentView != null) {
5268 return headsUpContentView;
5269 }
5270 return createContentView();
Adrian Roos487374f2017-01-11 15:48:14 -08005271 }
5272
Selim Cinek29603462015-11-17 19:04:39 -08005273 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005274 if (result != null) {
5275 result.setViewVisibility(R.id.text_line_1, View.GONE);
5276 }
Selim Cinek29603462015-11-17 19:04:39 -08005277 }
5278
Selim Cinek6743c0b2017-01-18 18:24:01 -08005279 /**
5280 * Adapt the Notification header if this view is used as an expanded view.
5281 *
5282 * @hide
5283 */
5284 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005285 if (result != null) {
5286 result.setBoolean(R.id.notification_header, "setExpanded", true);
5287 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005288 }
5289
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005290 /**
5291 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005292 *
5293 * @param increasedHeight true if this layout be created with an increased height. Some
5294 * styles may support showing more then just that basic 1U size
5295 * and the system may decide to render important notifications
5296 * slightly bigger even when collapsed.
5297 *
5298 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005299 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005300 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005301 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005302 return mN.headsUpContentView;
5303 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08005304 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
5305 if (styleView != null) {
5306 return styleView;
5307 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05005308 } else if (mActions.size() == 0) {
5309 return null;
5310 }
5311
Selim Cinekbee4e072018-05-21 22:06:43 -07005312 // We only want at most a single remote input history to be shown here, otherwise
5313 // the content would become squished.
5314 StandardTemplateParams p = mParams.reset().fillTextsFrom(this)
5315 .setMaxRemoteInputHistory(1);
5316 return applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5317 p,
5318 null /* result */);
Chris Wren8fd39ec2014-02-27 17:43:26 -05005319 }
5320
Selim Cinek624c02db2015-12-14 21:00:02 -08005321 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08005322 * Construct a RemoteViews for the final heads-up notification layout.
5323 */
5324 public RemoteViews createHeadsUpContentView() {
5325 return createHeadsUpContentView(false /* useIncreasedHeight */);
5326 }
5327
5328 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08005329 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5330 *
5331 * @hide
5332 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005333 @UnsupportedAppUsage
Selim Cinek624c02db2015-12-14 21:00:02 -08005334 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005335 return makePublicView(false /* ambient */);
5336 }
5337
5338 /**
5339 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5340 *
5341 * @hide
5342 */
5343 public RemoteViews makePublicAmbientNotification() {
5344 return makePublicView(true /* ambient */);
5345 }
5346
5347 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08005348 if (mN.publicVersion != null) {
5349 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005350 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08005351 }
5352 Bundle savedBundle = mN.extras;
5353 Style style = mStyle;
5354 mStyle = null;
5355 Icon largeIcon = mN.mLargeIcon;
5356 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07005357 Bitmap largeIconLegacy = mN.largeIcon;
5358 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005359 ArrayList<Action> actions = mActions;
5360 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08005361 Bundle publicExtras = new Bundle();
5362 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
5363 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
5364 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
5365 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07005366 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
5367 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cineked07b962018-04-30 14:39:35 -07005368 String appName = savedBundle.getString(EXTRA_SUBSTITUTE_APP_NAME);
5369 if (appName != null) {
5370 publicExtras.putString(EXTRA_SUBSTITUTE_APP_NAME, appName);
5371 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005372 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07005373 RemoteViews view;
Lucas Dupin00be88f2019-01-03 17:50:52 -08005374 view = makeNotificationHeader();
5375 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
Selim Cinek624c02db2015-12-14 21:00:02 -08005376 mN.extras = savedBundle;
5377 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07005378 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005379 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08005380 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005381 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08005382 }
5383
Selim Cinek6743c0b2017-01-18 18:24:01 -08005384 /**
5385 * Construct a content view for the display when low - priority
5386 *
5387 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
5388 * a new subtext is created consisting of the content of the
5389 * notification.
5390 * @hide
5391 */
5392 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005393 StandardTemplateParams p = mParams.reset()
5394 .forceDefaultColor()
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005395 .fillTextsFrom(this);
5396 if (!useRegularSubtext || TextUtils.isEmpty(mParams.summaryText)) {
5397 p.summaryText(createSummaryText());
Selim Cinek6743c0b2017-01-18 18:24:01 -08005398 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005399 RemoteViews header = makeNotificationHeader(p);
Selim Cinek1b554392017-02-28 17:22:49 -08005400 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08005401 return header;
5402 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005403
Selim Cinek6743c0b2017-01-18 18:24:01 -08005404 private CharSequence createSummaryText() {
5405 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
5406 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
5407 return titleText;
5408 }
5409 SpannableStringBuilder summary = new SpannableStringBuilder();
5410 if (titleText == null) {
5411 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
5412 }
5413 BidiFormatter bidi = BidiFormatter.getInstance();
5414 if (titleText != null) {
5415 summary.append(bidi.unicodeWrap(titleText));
5416 }
5417 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
5418 if (titleText != null && contentText != null) {
5419 summary.append(bidi.unicodeWrap(mContext.getText(
5420 R.string.notification_header_divider_symbol_with_spaces)));
5421 }
5422 if (contentText != null) {
5423 summary.append(bidi.unicodeWrap(contentText));
5424 }
5425 return summary;
5426 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05005427
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005428 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005429 StandardTemplateParams p) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04005430 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07005431 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005432 emphazisedMode ? getEmphasizedActionLayoutResource()
5433 : tombstone ? getActionTombstoneLayoutResource()
5434 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04005435 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04005436 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04005437 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005438 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005439 if (action.mRemoteInputs != null) {
5440 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
5441 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005442 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07005443 // change the background bgColor
Selim Cinek981962e2016-07-20 20:41:58 -07005444 CharSequence title = action.title;
5445 ColorStateList[] outResultColor = null;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005446 int background = resolveBackgroundColor(p);
Selim Cinek981962e2016-07-20 20:41:58 -07005447 if (isLegacy()) {
Lucas Dupina291d192018-06-07 13:59:42 -07005448 title = ContrastColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07005449 } else {
5450 outResultColor = new ColorStateList[1];
Selim Cinek396caca2018-04-10 17:46:46 -07005451 title = ensureColorSpanContrast(title, background, outResultColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005452 }
Selim Cinek48f66b72017-08-18 16:17:51 -07005453 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005454 setTextViewColorPrimary(button, R.id.action0, p);
Selim Cinek396caca2018-04-10 17:46:46 -07005455 int rippleColor;
5456 boolean hasColorOverride = outResultColor != null && outResultColor[0] != null;
5457 if (hasColorOverride) {
5458 // There's a span spanning the full text, let's take it and use it as the
5459 // background color
5460 background = outResultColor[0].getDefaultColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005461 int textColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07005462 background, mInNightMode);
Selim Cinek396caca2018-04-10 17:46:46 -07005463 button.setTextColor(R.id.action0, textColor);
5464 rippleColor = textColor;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005465 } else if (getRawColor(p) != COLOR_DEFAULT && !isColorized(p)
5466 && mTintActionButtons) {
5467 rippleColor = resolveContrastColor(p);
Selim Cinek396caca2018-04-10 17:46:46 -07005468 button.setTextColor(R.id.action0, rippleColor);
5469 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005470 rippleColor = getPrimaryTextColor(p);
Selim Cinek981962e2016-07-20 20:41:58 -07005471 }
Selim Cinek396caca2018-04-10 17:46:46 -07005472 // We only want about 20% alpha for the ripple
5473 rippleColor = (rippleColor & 0x00ffffff) | 0x33000000;
5474 button.setColorStateList(R.id.action0, "setRippleColor",
5475 ColorStateList.valueOf(rippleColor));
5476 button.setColorStateList(R.id.action0, "setButtonBackground",
5477 ColorStateList.valueOf(background));
5478 button.setBoolean(R.id.action0, "setHasStroke", !hasColorOverride);
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005479 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07005480 button.setTextViewText(R.id.action0, processTextSpans(
5481 processLegacyText(action.title)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005482 if (isColorized(p)) {
5483 setTextViewColorPrimary(button, R.id.action0, p);
5484 } else if (getRawColor(p) != COLOR_DEFAULT && mTintActionButtons) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005485 button.setTextColor(R.id.action0, resolveContrastColor(p));
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005486 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005487 }
Tony Mak7d4b3a52018-11-27 17:29:36 +00005488 button.setIntTag(R.id.action0, R.id.notification_action_index_tag,
5489 mActions.indexOf(action));
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005490 return button;
5491 }
5492
Joe Onoratocb109a02011-01-18 17:57:41 -08005493 /**
Selim Cinek981962e2016-07-20 20:41:58 -07005494 * Ensures contrast on color spans against a background color. also returns the color of the
5495 * text if a span was found that spans over the whole text.
5496 *
5497 * @param charSequence the charSequence on which the spans are
5498 * @param background the background color to ensure the contrast against
5499 * @param outResultColor an array in which a color will be returned as the first element if
5500 * there exists a full length color span.
5501 * @return the contrasted charSequence
5502 */
5503 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
5504 ColorStateList[] outResultColor) {
5505 if (charSequence instanceof Spanned) {
5506 Spanned ss = (Spanned) charSequence;
5507 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
5508 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
5509 for (Object span : spans) {
5510 Object resultSpan = span;
5511 int spanStart = ss.getSpanStart(span);
5512 int spanEnd = ss.getSpanEnd(span);
5513 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
5514 if (resultSpan instanceof CharacterStyle) {
5515 resultSpan = ((CharacterStyle) span).getUnderlying();
5516 }
5517 if (resultSpan instanceof TextAppearanceSpan) {
5518 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
5519 ColorStateList textColor = originalSpan.getTextColor();
5520 if (textColor != null) {
5521 int[] colors = textColor.getColors();
5522 int[] newColors = new int[colors.length];
5523 for (int i = 0; i < newColors.length; i++) {
Lucas Dupina291d192018-06-07 13:59:42 -07005524 newColors[i] = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005525 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005526 }
5527 textColor = new ColorStateList(textColor.getStates().clone(),
5528 newColors);
Selim Cinek396caca2018-04-10 17:46:46 -07005529 if (fullLength) {
5530 outResultColor[0] = textColor;
5531 // Let's drop the color from the span
5532 textColor = null;
5533 }
Selim Cinek981962e2016-07-20 20:41:58 -07005534 resultSpan = new TextAppearanceSpan(
5535 originalSpan.getFamily(),
5536 originalSpan.getTextStyle(),
5537 originalSpan.getTextSize(),
5538 textColor,
5539 originalSpan.getLinkTextColor());
Selim Cinek981962e2016-07-20 20:41:58 -07005540 }
5541 } else if (resultSpan instanceof ForegroundColorSpan) {
5542 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
5543 int foregroundColor = originalSpan.getForegroundColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005544 foregroundColor = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005545 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005546 if (fullLength) {
5547 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
Selim Cinek396caca2018-04-10 17:46:46 -07005548 resultSpan = null;
5549 } else {
5550 resultSpan = new ForegroundColorSpan(foregroundColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005551 }
5552 } else {
5553 resultSpan = span;
5554 }
Selim Cinek396caca2018-04-10 17:46:46 -07005555 if (resultSpan != null) {
5556 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
5557 }
Selim Cinek981962e2016-07-20 20:41:58 -07005558 }
5559 return builder;
5560 }
5561 return charSequence;
5562 }
5563
5564 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005565 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07005566 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005567 */
5568 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005569 if (!mIsLegacyInitialized) {
5570 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
5571 < Build.VERSION_CODES.LOLLIPOP;
5572 mIsLegacyInitialized = true;
5573 }
5574 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005575 }
5576
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005577 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08005578 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
Lucas Dupin00be88f2019-01-03 17:50:52 -08005579 if (isAlreadyLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005580 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005581 } else {
5582 return charSequence;
5583 }
5584 }
5585
Dan Sandler26e81cf2014-05-06 10:01:27 -04005586 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005587 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04005588 */
Adrian Roos487374f2017-01-11 15:48:14 -08005589 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005590 StandardTemplateParams p) {
Selim Cinekea4bef72015-12-02 15:51:10 -08005591 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005592 int color;
Lucas Dupin00be88f2019-01-03 17:50:52 -08005593 if (isColorized(p)) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005594 color = getPrimaryTextColor(p);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005595 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005596 color = resolveContrastColor(p);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005597 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005598 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07005599 contentView.setDrawableTint(R.id.icon, false, color,
5600 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005601
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005602 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005603 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08005604 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005605 }
5606
Dan Sandler26e81cf2014-05-06 10:01:27 -04005607 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005608 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04005609 * if it's grayscale).
5610 */
5611 // TODO: also check bounds, transparency, that sort of thing.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005612 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView,
5613 StandardTemplateParams p) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04005614 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005615 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005616 // resolve color will fall back to the default when legacy
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005617 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(p),
Sunny Goyal5b153922017-09-21 21:00:36 -07005618 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01005619 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005620 }
5621
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05005622 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005623 if (mN.color != COLOR_DEFAULT) {
5624 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02005625 }
Jorim Jaggi74419312014-06-10 20:57:21 +02005626 }
5627
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005628 int resolveContrastColor(StandardTemplateParams p) {
5629 int rawColor = getRawColor(p);
5630 if (mCachedContrastColorIsFor == rawColor && mCachedContrastColor != COLOR_INVALID) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08005631 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005632 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005633
Selim Cinekac5f0272017-05-02 16:05:41 -07005634 int color;
Selim Cinekc7f5a822018-03-20 19:32:06 -07005635 int background = mContext.getColor(
5636 com.android.internal.R.color.notification_material_background_color);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005637 if (rawColor == COLOR_DEFAULT) {
5638 ensureColors(p);
Lucas Dupinf03e7522018-06-25 16:21:13 -07005639 color = ContrastColorUtil.resolveDefaultColor(mContext, background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005640 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005641 color = ContrastColorUtil.resolveContrastColor(mContext, rawColor,
Anthony Chenad4d1582017-04-10 16:07:58 -07005642 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005643 }
5644 if (Color.alpha(color) < 255) {
5645 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005646 color = ContrastColorUtil.compositeColors(color, background);
Selim Cinekac5f0272017-05-02 16:05:41 -07005647 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005648 mCachedContrastColorIsFor = rawColor;
Selim Cinekac5f0272017-05-02 16:05:41 -07005649 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005650 }
5651
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005652 /**
5653 * Return the raw color of this Notification, which doesn't necessarily satisfy contrast.
5654 *
5655 * @see #resolveContrastColor(StandardTemplateParams) for the contrasted color
5656 * @param p the template params to inflate this with
5657 */
5658 private int getRawColor(StandardTemplateParams p) {
5659 if (p.forceDefaultColor) {
5660 return COLOR_DEFAULT;
5661 }
5662 return mN.color;
5663 }
5664
Selim Cinek4717d862018-04-19 09:19:15 +08005665 int resolveNeutralColor() {
5666 if (mNeutralColor != COLOR_INVALID) {
5667 return mNeutralColor;
5668 }
5669 int background = mContext.getColor(
5670 com.android.internal.R.color.notification_material_background_color);
Lucas Dupinf03e7522018-06-25 16:21:13 -07005671 mNeutralColor = ContrastColorUtil.resolveDefaultColor(mContext, background,
5672 mInNightMode);
Selim Cinek4717d862018-04-19 09:19:15 +08005673 if (Color.alpha(mNeutralColor) < 255) {
5674 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005675 mNeutralColor = ContrastColorUtil.compositeColors(mNeutralColor, background);
Selim Cinek4717d862018-04-19 09:19:15 +08005676 }
5677 return mNeutralColor;
5678 }
5679
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005680 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005681 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005682 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08005683 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005684 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005685 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005686 mN.actions = new Action[mActions.size()];
5687 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005688 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005689 if (!mPersonList.isEmpty()) {
Selim Cineke7238dd2017-12-14 17:48:32 -08005690 mN.extras.putParcelableArrayList(EXTRA_PEOPLE_LIST, mPersonList);
Dan Sandler0bf2ed82013-12-21 23:33:41 -06005691 }
Selim Cinek247fa012016-02-18 09:50:48 -08005692 if (mN.bigContentView != null || mN.contentView != null
5693 || mN.headsUpContentView != null) {
5694 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
5695 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005696 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08005697 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005698
Julia Reynolds3b848122016-02-26 10:45:32 -05005699 /**
5700 * Creates a Builder from an existing notification so further changes can be made.
5701 * @param context The context for your application / activity.
5702 * @param n The notification to create a Builder from.
5703 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005704 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005705 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005706 ApplicationInfo applicationInfo = n.extras.getParcelable(
5707 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005708 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05005709 if (applicationInfo != null) {
5710 try {
5711 builderContext = context.createApplicationContext(applicationInfo,
5712 Context.CONTEXT_RESTRICTED);
5713 } catch (NameNotFoundException e) {
5714 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
5715 builderContext = context; // try with our context
5716 }
5717 } else {
5718 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02005719 }
5720
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005721 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005722 }
5723
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005724 /**
Gustav Sennton761884c2018-11-19 17:40:19 +00005725 * Determines whether the platform can generate contextual actions for a notification.
5726 * By default this is true.
5727 */
5728 public Builder setAllowSystemGeneratedContextualActions(boolean allowed) {
5729 mN.mAllowSystemGeneratedContextualActions = allowed;
5730 return this;
5731 }
5732
5733 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005734 * @deprecated Use {@link #build()} instead.
5735 */
5736 @Deprecated
5737 public Notification getNotification() {
5738 return build();
5739 }
5740
5741 /**
5742 * Combine all of the options that have been set and return a new {@link Notification}
5743 * object.
5744 */
5745 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005746 // first, add any extras from the calling code
5747 if (mUserExtras != null) {
5748 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005749 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005750
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005751 mN.creationTime = System.currentTimeMillis();
5752
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005753 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05005754 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02005755
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005756 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005757
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005758 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02005759 mStyle.reduceImageSizes(mContext);
5760 mStyle.purgeResources();
Selim Cinek90343862018-02-01 11:07:11 -08005761 mStyle.validate(mContext);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005762 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005763 }
Selim Cinekd0426622017-07-11 13:19:59 +02005764 mN.reduceImageSizes(mContext);
5765
Adrian Roos5081c0d2016-02-26 16:04:19 -08005766 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07005767 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005768 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005769 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005770 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
5771 mN.contentView.getSequenceNumber());
5772 }
5773 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005774 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005775 if (mN.bigContentView != null) {
5776 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
5777 mN.bigContentView.getSequenceNumber());
5778 }
5779 }
5780 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005781 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005782 if (mN.headsUpContentView != null) {
5783 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
5784 mN.headsUpContentView.getSequenceNumber());
5785 }
5786 }
5787 }
5788
Julia Reynolds4c0c2022016-02-02 15:11:59 -05005789 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
5790 mN.flags |= FLAG_SHOW_LIGHTS;
5791 }
5792
Adrian Roosfb921842017-10-26 14:49:56 +02005793 mN.allPendingIntents = null;
5794
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005795 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005796 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005797
5798 /**
5799 * Apply this Builder to an existing {@link Notification} object.
5800 *
5801 * @hide
5802 */
5803 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005804 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005805 return n;
5806 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005807
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005808 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005809 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005810 * change. Also removes extenders on low ram devices, as
5811 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005812 *
5813 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5814 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005815 * @hide
5816 */
Kristian Monsen05f34792018-04-09 10:27:16 +02005817 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam,
5818 Context context) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005819 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005820
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005821 // Only strip views for known Styles because we won't know how to
5822 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005823 if (!isLowRam
5824 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005825 && getNotificationStyleClass(templateClass) == null) {
5826 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005827 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005828
5829 // Only strip unmodified BuilderRemoteViews.
5830 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005831 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005832 n.contentView.getSequenceNumber();
5833 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005834 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005835 n.bigContentView.getSequenceNumber();
5836 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005837 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005838 n.headsUpContentView.getSequenceNumber();
5839
5840 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005841 if (!isLowRam
5842 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005843 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005844 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005845
5846 Notification clone = n.clone();
5847 if (stripContentView) {
5848 clone.contentView = null;
5849 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5850 }
5851 if (stripBigContentView) {
5852 clone.bigContentView = null;
5853 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5854 }
5855 if (stripHeadsUpContentView) {
5856 clone.headsUpContentView = null;
5857 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5858 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005859 if (isLowRam) {
Kristian Monsen05f34792018-04-09 10:27:16 +02005860 String[] allowedServices = context.getResources().getStringArray(
5861 R.array.config_allowedManagedServicesOnLowRamDevices);
5862 if (allowedServices.length == 0) {
5863 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5864 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5865 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5866 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005867 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005868 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005869 }
5870
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005871 @UnsupportedAppUsage
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005872 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005873 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005874 }
5875
5876 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005877 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005878 }
5879
5880 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005881 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005882 }
5883
5884 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005885 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005886 }
5887
5888 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005889 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005890 }
5891
Adrian Roosc1a80b02016-04-05 14:54:55 -07005892 private int getMessagingLayoutResource() {
5893 return R.layout.notification_template_material_messaging;
5894 }
5895
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005896 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005897 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005898 }
5899
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005900 private int getEmphasizedActionLayoutResource() {
5901 return R.layout.notification_material_action_emphasized;
5902 }
5903
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005904 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005905 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005906 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005907
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005908 private int getBackgroundColor(StandardTemplateParams p) {
5909 if (isColorized(p)) {
5910 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : getRawColor(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005911 } else {
Selim Cinekc7f5a822018-03-20 19:32:06 -07005912 return COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005913 }
5914 }
5915
Selim Cinek396caca2018-04-10 17:46:46 -07005916 /**
Selim Cinek4717d862018-04-19 09:19:15 +08005917 * Gets a neutral color that can be used for icons or similar that should not stand out.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005918 * @param p the template params to inflate this with
Selim Cinek4717d862018-04-19 09:19:15 +08005919 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005920 private int getNeutralColor(StandardTemplateParams p) {
5921 if (isColorized(p)) {
5922 return getSecondaryTextColor(p);
Selim Cinek4717d862018-04-19 09:19:15 +08005923 } else {
5924 return resolveNeutralColor();
5925 }
5926 }
5927
5928 /**
Selim Cinek396caca2018-04-10 17:46:46 -07005929 * Same as getBackgroundColor but also resolved the default color to the background.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005930 * @param p the template params to inflate this with
Selim Cinek396caca2018-04-10 17:46:46 -07005931 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005932 private int resolveBackgroundColor(StandardTemplateParams p) {
5933 int backgroundColor = getBackgroundColor(p);
Selim Cinek396caca2018-04-10 17:46:46 -07005934 if (backgroundColor == COLOR_DEFAULT) {
5935 backgroundColor = mContext.getColor(
5936 com.android.internal.R.color.notification_material_background_color);
5937 }
5938 return backgroundColor;
5939 }
5940
Anthony Chenad4d1582017-04-10 16:07:58 -07005941 private boolean shouldTintActionButtons() {
5942 return mTintActionButtons;
5943 }
5944
Selim Cinek99104832017-01-25 14:47:33 -08005945 private boolean textColorsNeedInversion() {
5946 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
5947 return false;
5948 }
5949 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
5950 return targetSdkVersion > Build.VERSION_CODES.M
5951 && targetSdkVersion < Build.VERSION_CODES.O;
5952 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005953
5954 /**
5955 * Set a color palette to be used as the background and textColors
5956 *
5957 * @param backgroundColor the color to be used as the background
5958 * @param foregroundColor the color to be used as the foreground
5959 *
5960 * @hide
5961 */
5962 public void setColorPalette(int backgroundColor, int foregroundColor) {
5963 mBackgroundColor = backgroundColor;
5964 mForegroundColor = foregroundColor;
5965 mTextColorsAreForBackground = COLOR_INVALID;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005966 ensureColors(mParams.reset().fillTextsFrom(this));
Selim Cinek5fb73f82017-04-20 16:55:38 -07005967 }
Selim Cinekac5f0272017-05-02 16:05:41 -07005968
5969 /**
Selim Cineka7679b62017-05-10 16:33:25 -07005970 * Forces all styled remoteViews to be built from scratch and not use any cached
5971 * RemoteViews.
5972 * This is needed for legacy apps that are baking in their remoteviews into the
5973 * notification.
5974 *
5975 * @hide
5976 */
5977 public void setRebuildStyledRemoteViews(boolean rebuild) {
5978 mRebuildStyledRemoteViews = rebuild;
5979 }
Selim Cinekaa9db1f2018-02-27 17:35:47 -08005980
5981 /**
5982 * Get the text that should be displayed in the statusBar when heads upped. This is
5983 * usually just the app name, but may be different depending on the style.
5984 *
5985 * @param publicMode If true, return a text that is safe to display in public.
5986 *
5987 * @hide
5988 */
5989 public CharSequence getHeadsUpStatusBarText(boolean publicMode) {
5990 if (mStyle != null && !publicMode) {
5991 CharSequence text = mStyle.getHeadsUpStatusBarText();
5992 if (!TextUtils.isEmpty(text)) {
5993 return text;
5994 }
5995 }
5996 return loadHeaderAppName();
5997 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005998 }
5999
6000 /**
Selim Cinekd0426622017-07-11 13:19:59 +02006001 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
6002 * remote views.
6003 *
6004 * @hide
6005 */
6006 void reduceImageSizes(Context context) {
6007 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
6008 return;
6009 }
Selim Cineka8cb1262017-08-15 16:53:44 -07006010 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006011 if (mLargeIcon != null || largeIcon != null) {
6012 Resources resources = context.getResources();
6013 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07006014 int maxWidth = resources.getDimensionPixelSize(isLowRam
6015 ? R.dimen.notification_right_icon_size_low_ram
6016 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006017 int maxHeight = maxWidth;
6018 if (MediaStyle.class.equals(style)
6019 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006020 maxHeight = resources.getDimensionPixelSize(isLowRam
6021 ? R.dimen.notification_media_image_max_height_low_ram
6022 : R.dimen.notification_media_image_max_height);
6023 maxWidth = resources.getDimensionPixelSize(isLowRam
6024 ? R.dimen.notification_media_image_max_width_low_ram
6025 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006026 }
6027 if (mLargeIcon != null) {
6028 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
6029 }
6030 if (largeIcon != null) {
6031 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
6032 }
6033 }
Selim Cineka8cb1262017-08-15 16:53:44 -07006034 reduceImageSizesForRemoteView(contentView, context, isLowRam);
6035 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
6036 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02006037 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
6038 }
6039
Selim Cineka8cb1262017-08-15 16:53:44 -07006040 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
6041 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02006042 if (remoteView != null) {
6043 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006044 int maxWidth = resources.getDimensionPixelSize(isLowRam
6045 ? R.dimen.notification_custom_view_max_image_width_low_ram
6046 : R.dimen.notification_custom_view_max_image_width);
6047 int maxHeight = resources.getDimensionPixelSize(isLowRam
6048 ? R.dimen.notification_custom_view_max_image_height_low_ram
6049 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02006050 remoteView.reduceImageSizes(maxWidth, maxHeight);
6051 }
6052 }
6053
6054 /**
Selim Cinek22714f12017-04-13 16:23:53 -07006055 * @return whether this notification is a foreground service notification
Gus Prevasb0c1a462018-11-05 11:06:15 -05006056 * @hide
Selim Cinek7b9605b2017-01-19 17:36:00 -08006057 */
Gus Prevasb0c1a462018-11-05 11:06:15 -05006058 public boolean isForegroundService() {
Selim Cinek22714f12017-04-13 16:23:53 -07006059 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08006060 }
6061
6062 /**
Selim Cinek99104832017-01-25 14:47:33 -08006063 * @return whether this notification has a media session attached
6064 * @hide
6065 */
6066 public boolean hasMediaSession() {
6067 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
6068 }
6069
6070 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006071 * @return the style class of this notification
6072 * @hide
6073 */
6074 public Class<? extends Notification.Style> getNotificationStyle() {
6075 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
6076
6077 if (!TextUtils.isEmpty(templateClass)) {
6078 return Notification.getNotificationStyleClass(templateClass);
6079 }
6080 return null;
6081 }
6082
6083 /**
Selim Cinek22714f12017-04-13 16:23:53 -07006084 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08006085 *
6086 * @hide
6087 */
6088 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07006089 if (isColorizedMedia()) {
6090 return true;
6091 }
Julia Reynolds4db59552017-06-30 13:34:01 -04006092 return extras.getBoolean(EXTRA_COLORIZED)
6093 && (hasColorizedPermission() || isForegroundService());
6094 }
6095
6096 /**
6097 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
6098 * permission. The permission is checked when a notification is enqueued.
6099 */
6100 private boolean hasColorizedPermission() {
6101 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07006102 }
6103
6104 /**
6105 * @return true if this notification is colorized and it is a media notification
6106 *
6107 * @hide
6108 */
6109 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08006110 Class<? extends Style> style = getNotificationStyle();
6111 if (MediaStyle.class.equals(style)) {
6112 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
6113 if ((colorized == null || colorized) && hasMediaSession()) {
6114 return true;
6115 }
6116 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6117 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
6118 return true;
6119 }
6120 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07006121 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006122 }
6123
Selim Cinek0847acd2017-04-24 19:48:29 -07006124
6125 /**
6126 * @return true if this is a media notification
6127 *
6128 * @hide
6129 */
6130 public boolean isMediaNotification() {
6131 Class<? extends Style> style = getNotificationStyle();
6132 if (MediaStyle.class.equals(style)) {
6133 return true;
6134 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6135 return true;
6136 }
6137 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006138 }
6139
Selim Cinek279fa862016-06-14 10:57:25 -07006140 private boolean hasLargeIcon() {
6141 return mLargeIcon != null || largeIcon != null;
6142 }
6143
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006144 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006145 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006146 * @hide
6147 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07006148 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006149 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
6150 }
6151
6152 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006153 * @return true if the notification will show a chronometer; false otherwise
6154 * @hide
6155 */
6156 public boolean showsChronometer() {
6157 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
6158 }
6159
6160 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04006161 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05006162 */
6163 @SystemApi
6164 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
6165 Class<? extends Style>[] classes = new Class[] {
6166 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
6167 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
6168 MessagingStyle.class };
6169 for (Class<? extends Style> innerClass : classes) {
6170 if (templateClass.equals(innerClass.getName())) {
6171 return innerClass;
6172 }
6173 }
6174 return null;
6175 }
6176
6177 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006178 * An object that can apply a rich notification style to a {@link Notification.Builder}
6179 * object.
6180 */
Griff Hazendfcb0802014-02-11 12:00:00 -08006181 public static abstract class Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07006182
6183 /**
6184 * The number of items allowed simulatanously in the remote input history.
6185 * @hide
6186 */
6187 static final int MAX_REMOTE_INPUT_HISTORY_LINES = 3;
Chris Wrend6297db2012-05-03 16:20:13 -04006188 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006189
6190 /**
6191 * @hide
6192 */
6193 protected CharSequence mSummaryText = null;
6194
6195 /**
6196 * @hide
6197 */
6198 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04006199
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006200 protected Builder mBuilder;
6201
Chris Wrend6297db2012-05-03 16:20:13 -04006202 /**
6203 * Overrides ContentTitle in the big form of the template.
6204 * This defaults to the value passed to setContentTitle().
6205 */
6206 protected void internalSetBigContentTitle(CharSequence title) {
6207 mBigContentTitle = title;
6208 }
6209
6210 /**
6211 * Set the first line of text after the detail section in the big form of the template.
6212 */
6213 protected void internalSetSummaryText(CharSequence cs) {
6214 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04006215 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04006216 }
6217
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006218 public void setBuilder(Builder builder) {
6219 if (mBuilder != builder) {
6220 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07006221 if (mBuilder != null) {
6222 mBuilder.setStyle(this);
6223 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006224 }
6225 }
6226
Chris Wrend6297db2012-05-03 16:20:13 -04006227 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006228 if (mBuilder == null) {
6229 throw new IllegalArgumentException("Style requires a valid Builder object");
6230 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006231 }
Chris Wrend6297db2012-05-03 16:20:13 -04006232
6233 protected RemoteViews getStandardView(int layoutId) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006234 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder);
6235 return getStandardView(layoutId, p, null);
Selim Cinek384804b2018-04-18 14:31:07 +08006236 }
6237
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006238
Selim Cinek384804b2018-04-18 14:31:07 +08006239 /**
6240 * Get the standard view for this style.
6241 *
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006242 * @param layoutId The layout id to use.
6243 * @param p the params for this inflation.
Selim Cinek384804b2018-04-18 14:31:07 +08006244 * @param result The result where template bind information is saved.
6245 * @return A remoteView for this style.
6246 * @hide
6247 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006248 protected RemoteViews getStandardView(int layoutId, StandardTemplateParams p,
6249 TemplateBindResult result) {
Chris Wrend6297db2012-05-03 16:20:13 -04006250 checkBuilder();
6251
6252 if (mBigContentTitle != null) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006253 p.title = mBigContentTitle;
Chris Wrend6297db2012-05-03 16:20:13 -04006254 }
6255
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006256 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId, p,
6257 result);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006258
Chris Wrend6297db2012-05-03 16:20:13 -04006259 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
6260 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04006261 } else {
6262 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04006263 }
6264
Chris Wrend6297db2012-05-03 16:20:13 -04006265 return contentView;
6266 }
6267
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006268 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006269 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006270 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08006271 *
6272 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006273 * @hide
6274 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08006275 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006276 return null;
6277 }
6278
6279 /**
6280 * Construct a Style-specific RemoteViews for the final big notification layout.
6281 * @hide
6282 */
6283 public RemoteViews makeBigContentView() {
6284 return null;
6285 }
6286
6287 /**
6288 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08006289 *
6290 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006291 * @hide
6292 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006293 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006294 return null;
6295 }
6296
6297 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006298 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006299 * @hide
6300 */
6301 public void addExtras(Bundle extras) {
6302 if (mSummaryTextSet) {
6303 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
6304 }
6305 if (mBigContentTitle != null) {
6306 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
6307 }
Chris Wren91ad5632013-06-05 15:05:57 -04006308 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006309 }
6310
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006311 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006312 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006313 * @hide
6314 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02006315 protected void restoreFromExtras(Bundle extras) {
6316 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
6317 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
6318 mSummaryTextSet = true;
6319 }
6320 if (extras.containsKey(EXTRA_TITLE_BIG)) {
6321 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
6322 }
6323 }
6324
6325
6326 /**
6327 * @hide
6328 */
6329 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006330 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006331 return wip;
6332 }
6333
Daniel Sandler0ec46202015-06-24 01:27:05 -04006334 /**
6335 * @hide
6336 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006337 public void purgeResources() {}
6338
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006339 /**
6340 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
6341 * attached to.
6342 *
6343 * @return the fully constructed Notification.
6344 */
6345 public Notification build() {
6346 checkBuilder();
6347 return mBuilder.build();
6348 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006349
6350 /**
6351 * @hide
6352 * @return true if the style positions the progress bar on the second line; false if the
6353 * style hides the progress bar
6354 */
6355 protected boolean hasProgress() {
6356 return true;
6357 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006358
6359 /**
6360 * @hide
6361 * @return Whether we should put the summary be put into the notification header
6362 */
6363 public boolean hasSummaryInHeader() {
6364 return true;
6365 }
Selim Cinek593610c2016-02-16 18:42:57 -08006366
6367 /**
6368 * @hide
6369 * @return Whether custom content views are displayed inline in the style
6370 */
6371 public boolean displayCustomViewInline() {
6372 return false;
6373 }
Selim Cinekd0426622017-07-11 13:19:59 +02006374
6375 /**
6376 * Reduces the image sizes contained in this style.
6377 *
6378 * @hide
6379 */
6380 public void reduceImageSizes(Context context) {
6381 }
Selim Cinek90343862018-02-01 11:07:11 -08006382
6383 /**
6384 * Validate that this style was properly composed. This is called at build time.
6385 * @hide
6386 */
6387 public void validate(Context context) {
6388 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006389
6390 /**
6391 * @hide
6392 */
6393 public abstract boolean areNotificationsVisiblyDifferent(Style other);
Selim Cinekc7f5a822018-03-20 19:32:06 -07006394
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006395 /**
koprivaa1a78482018-10-09 10:09:23 -07006396 * @return the text that should be displayed in the statusBar when heads-upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006397 * If {@code null} is returned, the default implementation will be used.
6398 *
6399 * @hide
6400 */
6401 public CharSequence getHeadsUpStatusBarText() {
6402 return null;
6403 }
Joe Onorato46439ce2010-11-19 13:56:21 -08006404 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006405
6406 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006407 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08006408 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006409 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006410 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006411 * Notification notif = new Notification.Builder(mContext)
6412 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
6413 * .setContentText(subject)
6414 * .setSmallIcon(R.drawable.new_post)
6415 * .setLargeIcon(aBitmap)
6416 * .setStyle(new Notification.BigPictureStyle()
6417 * .bigPicture(aBigBitmap))
6418 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006419 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006420 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006421 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006422 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006423 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006424 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006425 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006426 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006427
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006428 public BigPictureStyle() {
6429 }
6430
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006431 /**
6432 * @deprecated use {@code BigPictureStyle()}.
6433 */
6434 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006435 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006436 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006437 }
6438
Chris Wrend6297db2012-05-03 16:20:13 -04006439 /**
6440 * Overrides ContentTitle in the big form of the template.
6441 * This defaults to the value passed to setContentTitle().
6442 */
6443 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006444 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006445 return this;
6446 }
6447
6448 /**
6449 * Set the first line of text after the detail section in the big form of the template.
6450 */
6451 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006452 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006453 return this;
6454 }
6455
Chris Wren0bd664d2012-08-01 13:56:56 -04006456 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05006457 * @hide
6458 */
6459 public Bitmap getBigPicture() {
6460 return mPicture;
6461 }
6462
6463 /**
Chris Wren0bd664d2012-08-01 13:56:56 -04006464 * Provide the bitmap to be used as the payload for the BigPicture notification.
6465 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006466 public BigPictureStyle bigPicture(Bitmap b) {
6467 mPicture = b;
6468 return this;
6469 }
6470
Chris Wren3745a3d2012-05-22 15:11:52 -04006471 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04006472 * Override the large icon when the big notification is shown.
6473 */
6474 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04006475 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
6476 }
6477
6478 /**
6479 * Override the large icon when the big notification is shown.
6480 */
6481 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04006482 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006483 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006484 return this;
6485 }
6486
Riley Andrews0394a0c2015-11-03 23:36:52 -08006487 /** @hide */
6488 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
6489
Daniel Sandler0ec46202015-06-24 01:27:05 -04006490 /**
6491 * @hide
6492 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006493 @Override
6494 public void purgeResources() {
6495 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08006496 if (mPicture != null &&
6497 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08006498 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006499 mPicture = mPicture.createAshmemBitmap();
6500 }
6501 if (mBigLargeIcon != null) {
6502 mBigLargeIcon.convertToAshmem();
6503 }
6504 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01006505
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006506 /**
6507 * @hide
6508 */
Selim Cinekd0426622017-07-11 13:19:59 +02006509 @Override
6510 public void reduceImageSizes(Context context) {
6511 super.reduceImageSizes(context);
6512 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006513 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006514 if (mPicture != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006515 int maxPictureWidth = resources.getDimensionPixelSize(isLowRam
6516 ? R.dimen.notification_big_picture_max_height_low_ram
6517 : R.dimen.notification_big_picture_max_height);
6518 int maxPictureHeight = resources.getDimensionPixelSize(isLowRam
6519 ? R.dimen.notification_big_picture_max_width_low_ram
6520 : R.dimen.notification_big_picture_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006521 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
6522 }
6523 if (mBigLargeIcon != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006524 int rightIconSize = resources.getDimensionPixelSize(isLowRam
6525 ? R.dimen.notification_right_icon_size_low_ram
6526 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006527 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
6528 }
6529 }
6530
6531 /**
6532 * @hide
6533 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006534 public RemoteViews makeBigContentView() {
6535 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01006536 // This covers the following cases:
6537 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006538 // mN.mLargeIcon
6539 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04006540 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07006541 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006542 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006543 oldLargeIcon = mBuilder.mN.mLargeIcon;
6544 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006545 // The legacy largeIcon might not allow us to clear the image, as it's taken in
6546 // replacement if the other one is null. Because we're restoring these legacy icons
6547 // for old listeners, this is in general non-null.
6548 largeIconLegacy = mBuilder.mN.largeIcon;
6549 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006550 }
6551
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006552 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder);
Selim Cinek384804b2018-04-18 14:31:07 +08006553 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource(),
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006554 p, null /* result */);
Selim Cinek03d0d652015-11-13 13:18:09 -05006555 if (mSummaryTextSet) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006556 contentView.setTextViewText(R.id.text, mBuilder.processTextSpans(
6557 mBuilder.processLegacyText(mSummaryText)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006558 mBuilder.setTextViewColorSecondary(contentView, R.id.text, p);
Selim Cinekc848c3a2016-01-13 15:27:30 -08006559 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05006560 }
Selim Cinek279fa862016-06-14 10:57:25 -07006561 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08006562
Christoph Studer5c510ee2014-12-15 16:32:27 +01006563 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006564 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006565 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006566 }
6567
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006568 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006569 return contentView;
6570 }
6571
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006572 /**
6573 * @hide
6574 */
6575 public void addExtras(Bundle extras) {
6576 super.addExtras(extras);
6577
6578 if (mBigLargeIconSet) {
6579 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
6580 }
6581 extras.putParcelable(EXTRA_PICTURE, mPicture);
6582 }
6583
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006584 /**
6585 * @hide
6586 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006587 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02006588 protected void restoreFromExtras(Bundle extras) {
6589 super.restoreFromExtras(extras);
6590
6591 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01006592 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02006593 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04006594 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006595 mPicture = extras.getParcelable(EXTRA_PICTURE);
6596 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006597
6598 /**
6599 * @hide
6600 */
6601 @Override
6602 public boolean hasSummaryInHeader() {
6603 return false;
6604 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006605
6606 /**
6607 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006608 * Note that we aren't actually comparing the contents of the bitmaps here, so this
6609 * is only doing a cursory inspection. Bitmaps of equal size will appear the same.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006610 */
6611 @Override
6612 public boolean areNotificationsVisiblyDifferent(Style other) {
6613 if (other == null || getClass() != other.getClass()) {
6614 return true;
6615 }
6616 BigPictureStyle otherS = (BigPictureStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006617 return areBitmapsObviouslyDifferent(getBigPicture(), otherS.getBigPicture());
6618 }
6619
6620 private static boolean areBitmapsObviouslyDifferent(Bitmap a, Bitmap b) {
6621 if (a == b) {
6622 return false;
6623 }
6624 if (a == null || b == null) {
6625 return true;
6626 }
6627 return a.getWidth() != b.getWidth()
6628 || a.getHeight() != b.getHeight()
6629 || a.getConfig() != b.getConfig()
6630 || a.getGenerationId() != b.getGenerationId();
Julia Reynolds7217dc92018-03-07 12:12:09 -05006631 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006632 }
6633
6634 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006635 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08006636 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006637 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006638 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006639 * Notification notif = new Notification.Builder(mContext)
6640 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
6641 * .setContentText(subject)
6642 * .setSmallIcon(R.drawable.new_mail)
6643 * .setLargeIcon(aBitmap)
6644 * .setStyle(new Notification.BigTextStyle()
6645 * .bigText(aVeryLongString))
6646 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006647 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006648 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006649 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006650 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006651 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006652
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006653 private CharSequence mBigText;
6654
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006655 public BigTextStyle() {
6656 }
6657
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006658 /**
6659 * @deprecated use {@code BigTextStyle()}.
6660 */
6661 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006662 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006663 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006664 }
6665
Chris Wrend6297db2012-05-03 16:20:13 -04006666 /**
6667 * Overrides ContentTitle in the big form of the template.
6668 * This defaults to the value passed to setContentTitle().
6669 */
6670 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006671 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006672 return this;
6673 }
6674
6675 /**
6676 * Set the first line of text after the detail section in the big form of the template.
6677 */
6678 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006679 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006680 return this;
6681 }
6682
Chris Wren0bd664d2012-08-01 13:56:56 -04006683 /**
6684 * Provide the longer text to be displayed in the big form of the
6685 * template in place of the content text.
6686 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006687 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006688 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006689 return this;
6690 }
6691
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006692 /**
6693 * @hide
6694 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05006695 public CharSequence getBigText() {
6696 return mBigText;
6697 }
6698
6699 /**
6700 * @hide
6701 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006702 public void addExtras(Bundle extras) {
6703 super.addExtras(extras);
6704
Christoph Studer4600f9b2014-07-22 22:44:43 +02006705 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
6706 }
6707
6708 /**
6709 * @hide
6710 */
6711 @Override
6712 protected void restoreFromExtras(Bundle extras) {
6713 super.restoreFromExtras(extras);
6714
6715 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006716 }
6717
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006718 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006719 * @param increasedHeight true if this layout be created with an increased height.
6720 *
6721 * @hide
6722 */
6723 @Override
6724 public RemoteViews makeContentView(boolean increasedHeight) {
6725 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006726 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006727 mBuilder.mActions = new ArrayList<>();
6728 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006729 mBuilder.mActions = mBuilder.mOriginalActions;
6730 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006731 return remoteViews;
6732 }
6733 return super.makeContentView(increasedHeight);
6734 }
6735
6736 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006737 * @hide
6738 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006739 @Override
6740 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
6741 if (increasedHeight && mBuilder.mActions.size() > 0) {
6742 return makeBigContentView();
6743 }
6744 return super.makeHeadsUpContentView(increasedHeight);
6745 }
6746
6747 /**
6748 * @hide
6749 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006750 public RemoteViews makeBigContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006751 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder).text(null);
Selim Cinek384804b2018-04-18 14:31:07 +08006752 TemplateBindResult result = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006753 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource(), p,
6754 result);
Selim Cinek1c72fa02018-04-23 18:00:54 +08006755 contentView.setInt(R.id.big_text, "setImageEndMargin", result.getIconMarginEnd());
Joe Malin8d40d042012-11-05 11:36:40 -08006756
Selim Cinek3a2c4b92015-12-17 17:01:17 -08006757 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07006758 if (TextUtils.isEmpty(bigTextText)) {
6759 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
6760 // experience
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006761 bigTextText = mBuilder.processLegacyText(
6762 mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT));
Selim Cinek75998782016-04-26 10:39:17 -07006763 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006764 contentView.setTextViewText(R.id.big_text, mBuilder.processTextSpans(bigTextText));
6765 mBuilder.setTextViewColorSecondary(contentView, R.id.big_text, p);
6766 contentView.setViewVisibility(R.id.big_text,
6767 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08006768 contentView.setBoolean(R.id.big_text, "setHasImage",
6769 result.isRightIconContainerVisible());
Selim Cinek4fb12d32015-11-19 18:10:48 -08006770
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006771 return contentView;
6772 }
6773
Julia Reynolds7217dc92018-03-07 12:12:09 -05006774 /**
6775 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006776 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006777 */
6778 @Override
6779 public boolean areNotificationsVisiblyDifferent(Style other) {
6780 if (other == null || getClass() != other.getClass()) {
6781 return true;
6782 }
6783 BigTextStyle newS = (BigTextStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006784 return !Objects.equals(String.valueOf(getBigText()), String.valueOf(newS.getBigText()));
Julia Reynolds7217dc92018-03-07 12:12:09 -05006785 }
6786
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006787 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006788
6789 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006790 * Helper class for generating large-format notifications that include multiple back-and-forth
6791 * messages of varying types between any number of people.
6792 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006793 * <p>
Alex Hillsfc737de2016-03-23 17:33:02 -04006794 * If the platform does not provide large-format notifications, this method has no effect. The
6795 * user will always see the normal notification view.
Selim Cinekce8794f2018-05-23 16:46:05 -07006796 *
6797 * <p>
6798 * If the app is targeting Android P and above, it is required to use the {@link Person}
6799 * class in order to get an optimal rendering of the notification and its avatars. For
6800 * conversations involving multiple people, the app should also make sure that it marks the
6801 * conversation as a group with {@link #setGroupConversation(boolean)}.
6802 *
6803 * <p>
6804 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior.
6805 * Here's an example of how this may be used:
Alex Hillsfc737de2016-03-23 17:33:02 -04006806 * <pre class="prettyprint">
6807 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006808 * Person user = new Person.Builder().setIcon(userIcon).setName(userName).build();
6809 * MessagingStyle style = new MessagingStyle(user)
6810 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getPerson())
6811 * .addMessage(messages[2].getText(), messages[2].getTime(), messages[2].getPerson())
6812 * .setGroupConversation(hasMultiplePeople());
6813 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006814 * Notification noti = new Notification.Builder()
Selim Cinekce8794f2018-05-23 16:46:05 -07006815 * .setContentTitle(&quot;2 new messages with &quot; + sender.toString())
Alex Hillsfc737de2016-03-23 17:33:02 -04006816 * .setContentText(subject)
6817 * .setSmallIcon(R.drawable.new_message)
6818 * .setLargeIcon(aBitmap)
Selim Cinekce8794f2018-05-23 16:46:05 -07006819 * .setStyle(style)
Alex Hillsfc737de2016-03-23 17:33:02 -04006820 * .build();
6821 * </pre>
6822 */
6823 public static class MessagingStyle extends Style {
6824
6825 /**
6826 * The maximum number of messages that will be retained in the Notification itself (the
6827 * number displayed is up to the platform).
6828 */
6829 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
6830
Selim Cinekcb8b9852017-12-15 18:01:52 -08006831 @NonNull Person mUser;
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006832 @Nullable CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04006833 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08006834 List<Message> mHistoricMessages = new ArrayList<>();
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006835 boolean mIsGroupConversation;
Alex Hillsfc737de2016-03-23 17:33:02 -04006836
6837 MessagingStyle() {
6838 }
6839
6840 /**
Alex Hillsfd590442016-10-07 09:52:44 -04006841 * @param userDisplayName Required - the name to be displayed for any replies sent by the
6842 * user before the posting app reposts the notification with those messages after they've
6843 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04006844 * {@link #addMessage(Notification.MessagingStyle.Message)}
Selim Cinekcb8b9852017-12-15 18:01:52 -08006845 *
6846 * @deprecated use {@code MessagingStyle(Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006847 */
Alex Hillsfd590442016-10-07 09:52:44 -04006848 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Selim Cinek9acd6732018-03-23 16:39:02 -07006849 this(new Person.Builder().setName(userDisplayName).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006850 }
6851
6852 /**
6853 * @param user Required - The person displayed for any messages that are sent by the
6854 * user. Any messages added with {@link #addMessage(Notification.MessagingStyle.Message)}
6855 * who don't have a Person associated with it will be displayed as if they were sent
Selim Cinek90343862018-02-01 11:07:11 -08006856 * by this user. The user also needs to have a valid name associated with it, which will
6857 * be enforced starting in Android P.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006858 */
6859 public MessagingStyle(@NonNull Person user) {
6860 mUser = user;
Selim Cinek90343862018-02-01 11:07:11 -08006861 }
6862
6863 /**
6864 * Validate that this style was properly composed. This is called at build time.
6865 * @hide
6866 */
6867 @Override
6868 public void validate(Context context) {
6869 super.validate(context);
6870 if (context.getApplicationInfo().targetSdkVersion
6871 >= Build.VERSION_CODES.P && (mUser == null || mUser.getName() == null)) {
6872 throw new RuntimeException("User must be valid and have a name.");
Selim Cinekcb8b9852017-12-15 18:01:52 -08006873 }
6874 }
6875
6876 /**
koprivaa1a78482018-10-09 10:09:23 -07006877 * @return the text that should be displayed in the statusBar when heads upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006878 * If {@code null} is returned, the default implementation will be used.
6879 *
6880 * @hide
6881 */
6882 @Override
6883 public CharSequence getHeadsUpStatusBarText() {
6884 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
6885 ? super.mBigContentTitle
6886 : mConversationTitle;
6887 if (!TextUtils.isEmpty(conversationTitle) && !hasOnlyWhiteSpaceSenders()) {
6888 return conversationTitle;
6889 }
6890 return null;
6891 }
6892
6893 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08006894 * @return the user to be displayed for any replies sent by the user
6895 */
Selim Cinekeb53c222018-04-06 12:56:09 -07006896 @NonNull
Selim Cinekcb8b9852017-12-15 18:01:52 -08006897 public Person getUser() {
6898 return mUser;
Alex Hillsfc737de2016-03-23 17:33:02 -04006899 }
6900
6901 /**
6902 * Returns the name to be displayed for any replies sent by the user
Selim Cinekcb8b9852017-12-15 18:01:52 -08006903 *
6904 * @deprecated use {@link #getUser()} instead
Alex Hillsfc737de2016-03-23 17:33:02 -04006905 */
6906 public CharSequence getUserDisplayName() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006907 return mUser.getName();
Alex Hillsfc737de2016-03-23 17:33:02 -04006908 }
6909
6910 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006911 * Sets the title to be displayed on this conversation. May be set to {@code null}.
6912 *
Kodlee Yin14656422017-12-22 17:00:46 -08006913 * <p>This API's behavior was changed in SDK version {@link Build.VERSION_CODES#P}. If your
6914 * application's target version is less than {@link Build.VERSION_CODES#P}, setting a
6915 * conversation title to a non-null value will make {@link #isGroupConversation()} return
6916 * {@code true} and passing {@code null} will make it return {@code false}. In
6917 * {@link Build.VERSION_CODES#P} and beyond, use {@link #setGroupConversation(boolean)}
6918 * to set group conversation status.
6919 *
6920 * @param conversationTitle Title displayed for this conversation
6921 * @return this object for method chaining
Alex Hillsfc737de2016-03-23 17:33:02 -04006922 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006923 public MessagingStyle setConversationTitle(@Nullable CharSequence conversationTitle) {
Alex Hillsfc737de2016-03-23 17:33:02 -04006924 mConversationTitle = conversationTitle;
6925 return this;
6926 }
6927
6928 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006929 * Return the title to be displayed on this conversation. May return {@code null}.
Alex Hillsfc737de2016-03-23 17:33:02 -04006930 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006931 @Nullable
Alex Hillsfc737de2016-03-23 17:33:02 -04006932 public CharSequence getConversationTitle() {
6933 return mConversationTitle;
6934 }
6935
6936 /**
6937 * Adds a message for display by this notification. Convenience call for a simple
6938 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6939 * @param text A {@link CharSequence} to be displayed as the message content
6940 * @param timestamp Time at which the message arrived
6941 * @param sender A {@link CharSequence} to be used for displaying the name of the
6942 * sender. Should be <code>null</code> for messages by the current user, in which case
6943 * the platform will insert {@link #getUserDisplayName()}.
6944 * Should be unique amongst all individuals in the conversation, and should be
6945 * consistent during re-posts of the notification.
6946 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07006947 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Alex Hillsfc737de2016-03-23 17:33:02 -04006948 *
6949 * @return this object for method chaining
Selim Cinekcb8b9852017-12-15 18:01:52 -08006950 *
6951 * @deprecated use {@link #addMessage(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006952 */
6953 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006954 return addMessage(text, timestamp,
Selim Cinek9acd6732018-03-23 16:39:02 -07006955 sender == null ? null : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006956 }
6957
6958 /**
6959 * Adds a message for display by this notification. Convenience call for a simple
6960 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6961 * @param text A {@link CharSequence} to be displayed as the message content
6962 * @param timestamp Time at which the message arrived
6963 * @param sender The {@link Person} who sent the message.
6964 * Should be <code>null</code> for messages by the current user, in which case
6965 * the platform will insert the user set in {@code MessagingStyle(Person)}.
6966 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07006967 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Selim Cinekcb8b9852017-12-15 18:01:52 -08006968 *
6969 * @return this object for method chaining
6970 */
Selim Cinekeb53c222018-04-06 12:56:09 -07006971 public MessagingStyle addMessage(@NonNull CharSequence text, long timestamp,
6972 @Nullable Person sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08006973 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04006974 }
6975
6976 /**
6977 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08006978 *
6979 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6980 * the newest last.
6981 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006982 * @param message The {@link Message} to be displayed
6983 * @return this object for method chaining
6984 */
6985 public MessagingStyle addMessage(Message message) {
6986 mMessages.add(message);
6987 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6988 mMessages.remove(0);
6989 }
6990 return this;
6991 }
6992
6993 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006994 * Adds a {@link Message} for historic context in this notification.
6995 *
6996 * <p>Messages should be added as historic if they are not the main subject of the
6997 * notification but may give context to a conversation. The system may choose to present
6998 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
6999 *
7000 * <p>The messages should be added in chronologic order, i.e. the oldest first,
7001 * the newest last.
7002 *
7003 * @param message The historic {@link Message} to be added
7004 * @return this object for method chaining
7005 */
7006 public MessagingStyle addHistoricMessage(Message message) {
7007 mHistoricMessages.add(message);
7008 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
7009 mHistoricMessages.remove(0);
7010 }
7011 return this;
7012 }
7013
7014 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007015 * Gets the list of {@code Message} objects that represent the notification
7016 */
7017 public List<Message> getMessages() {
7018 return mMessages;
7019 }
7020
7021 /**
Adrian Roos437cd562017-01-18 15:47:03 -08007022 * Gets the list of historic {@code Message}s in the notification.
7023 */
7024 public List<Message> getHistoricMessages() {
7025 return mHistoricMessages;
7026 }
7027
7028 /**
Selim Cinekce8794f2018-05-23 16:46:05 -07007029 * Sets whether this conversation notification represents a group. If the app is targeting
7030 * Android P, this is required if the app wants to display the largeIcon set with
7031 * {@link Notification.Builder#setLargeIcon(Bitmap)}, otherwise it will be hidden.
Kodlee Yin14656422017-12-22 17:00:46 -08007032 *
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007033 * @param isGroupConversation {@code true} if the conversation represents a group,
7034 * {@code false} otherwise.
7035 * @return this object for method chaining
7036 */
7037 public MessagingStyle setGroupConversation(boolean isGroupConversation) {
7038 mIsGroupConversation = isGroupConversation;
7039 return this;
7040 }
7041
7042 /**
Kodlee Yin14656422017-12-22 17:00:46 -08007043 * Returns {@code true} if this notification represents a group conversation, otherwise
7044 * {@code false}.
7045 *
7046 * <p> If the application that generated this {@link MessagingStyle} targets an SDK version
7047 * less than {@link Build.VERSION_CODES#P}, this method becomes dependent on whether or
7048 * not the conversation title is set; returning {@code true} if the conversation title is
7049 * a non-null value, or {@code false} otherwise. From {@link Build.VERSION_CODES#P} forward,
7050 * this method returns what's set by {@link #setGroupConversation(boolean)} allowing for
7051 * named, non-group conversations.
7052 *
7053 * @see #setConversationTitle(CharSequence)
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007054 */
7055 public boolean isGroupConversation() {
Kodlee Yin14656422017-12-22 17:00:46 -08007056 // When target SDK version is < P, a non-null conversation title dictates if this is
7057 // as group conversation.
7058 if (mBuilder != null
7059 && mBuilder.mContext.getApplicationInfo().targetSdkVersion
7060 < Build.VERSION_CODES.P) {
7061 return mConversationTitle != null;
7062 }
7063
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007064 return mIsGroupConversation;
7065 }
7066
7067 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007068 * @hide
7069 */
7070 @Override
7071 public void addExtras(Bundle extras) {
7072 super.addExtras(extras);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007073 if (mUser != null) {
7074 // For legacy usages
7075 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUser.getName());
7076 extras.putParcelable(EXTRA_MESSAGING_PERSON, mUser);
Alex Hillsfc737de2016-03-23 17:33:02 -04007077 }
7078 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007079 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04007080 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007081 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
7082 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04007083 }
Adrian Roos437cd562017-01-18 15:47:03 -08007084 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
7085 Message.getBundleArrayForMessages(mHistoricMessages));
7086 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007087
7088 fixTitleAndTextExtras(extras);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007089 extras.putBoolean(EXTRA_IS_GROUP_CONVERSATION, mIsGroupConversation);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007090 }
7091
7092 private void fixTitleAndTextExtras(Bundle extras) {
7093 Message m = findLatestIncomingMessage();
7094 CharSequence text = (m == null) ? null : m.mText;
7095 CharSequence sender = m == null ? null
Selim Cinekcb8b9852017-12-15 18:01:52 -08007096 : m.mSender == null || TextUtils.isEmpty(m.mSender.getName())
7097 ? mUser.getName() : m.mSender.getName();
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007098 CharSequence title;
7099 if (!TextUtils.isEmpty(mConversationTitle)) {
7100 if (!TextUtils.isEmpty(sender)) {
7101 BidiFormatter bidi = BidiFormatter.getInstance();
7102 title = mBuilder.mContext.getString(
7103 com.android.internal.R.string.notification_messaging_title_template,
Selim Cinekcb8b9852017-12-15 18:01:52 -08007104 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(sender));
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007105 } else {
7106 title = mConversationTitle;
7107 }
7108 } else {
7109 title = sender;
7110 }
7111
7112 if (title != null) {
7113 extras.putCharSequence(EXTRA_TITLE, title);
7114 }
7115 if (text != null) {
7116 extras.putCharSequence(EXTRA_TEXT, text);
7117 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007118 }
7119
7120 /**
7121 * @hide
7122 */
7123 @Override
7124 protected void restoreFromExtras(Bundle extras) {
7125 super.restoreFromExtras(extras);
7126
Selim Cinekcb8b9852017-12-15 18:01:52 -08007127 mUser = extras.getParcelable(EXTRA_MESSAGING_PERSON);
7128 if (mUser == null) {
7129 CharSequence displayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
Selim Cinek9acd6732018-03-23 16:39:02 -07007130 mUser = new Person.Builder().setName(displayName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007131 }
Adrian Roos96b7e202016-05-17 13:50:38 -07007132 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08007133 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007134 mMessages = Message.getMessagesFromBundleArray(messages);
Adrian Roos437cd562017-01-18 15:47:03 -08007135 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007136 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007137 mIsGroupConversation = extras.getBoolean(EXTRA_IS_GROUP_CONVERSATION);
Alex Hillsfc737de2016-03-23 17:33:02 -04007138 }
7139
7140 /**
7141 * @hide
7142 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07007143 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007144 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07007145 mBuilder.mOriginalActions = mBuilder.mActions;
7146 mBuilder.mActions = new ArrayList<>();
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007147 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007148 false /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007149 mBuilder.mActions = mBuilder.mOriginalActions;
7150 mBuilder.mOriginalActions = null;
7151 return remoteViews;
Adrian Roosc1a80b02016-04-05 14:54:55 -07007152 }
7153
Julia Reynolds7217dc92018-03-07 12:12:09 -05007154 /**
7155 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04007156 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05007157 */
7158 @Override
7159 public boolean areNotificationsVisiblyDifferent(Style other) {
7160 if (other == null || getClass() != other.getClass()) {
7161 return true;
7162 }
7163 MessagingStyle newS = (MessagingStyle) other;
7164 List<MessagingStyle.Message> oldMs = getMessages();
7165 List<MessagingStyle.Message> newMs = newS.getMessages();
7166
Dan Sandler7d67bd42018-05-15 14:06:38 -04007167 if (oldMs == null || newMs == null) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007168 newMs = new ArrayList<>();
7169 }
7170
7171 int n = oldMs.size();
7172 if (n != newMs.size()) {
7173 return true;
7174 }
7175 for (int i = 0; i < n; i++) {
7176 MessagingStyle.Message oldM = oldMs.get(i);
7177 MessagingStyle.Message newM = newMs.get(i);
Dan Sandler7d67bd42018-05-15 14:06:38 -04007178 if (!Objects.equals(
7179 String.valueOf(oldM.getText()),
7180 String.valueOf(newM.getText()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007181 return true;
7182 }
7183 if (!Objects.equals(oldM.getDataUri(), newM.getDataUri())) {
7184 return true;
7185 }
Dan Sandler7d67bd42018-05-15 14:06:38 -04007186 String oldSender = String.valueOf(oldM.getSenderPerson() == null
7187 ? oldM.getSender()
7188 : oldM.getSenderPerson().getName());
7189 String newSender = String.valueOf(newM.getSenderPerson() == null
7190 ? newM.getSender()
7191 : newM.getSenderPerson().getName());
Julia Reynolds7217dc92018-03-07 12:12:09 -05007192 if (!Objects.equals(oldSender, newSender)) {
7193 return true;
7194 }
7195
7196 String oldKey = oldM.getSenderPerson() == null
7197 ? null : oldM.getSenderPerson().getKey();
7198 String newKey = newM.getSenderPerson() == null
7199 ? null : newM.getSenderPerson().getKey();
7200 if (!Objects.equals(oldKey, newKey)) {
7201 return true;
7202 }
7203 // Other fields (like timestamp) intentionally excluded
7204 }
7205 return false;
7206 }
7207
Adrian Roosc1a80b02016-04-05 14:54:55 -07007208 private Message findLatestIncomingMessage() {
Selim Cinek88188f22017-09-19 16:46:56 -07007209 return findLatestIncomingMessage(mMessages);
7210 }
7211
7212 /**
7213 * @hide
7214 */
7215 @Nullable
7216 public static Message findLatestIncomingMessage(
7217 List<Message> messages) {
7218 for (int i = messages.size() - 1; i >= 0; i--) {
7219 Message m = messages.get(i);
Adrian Roosc1a80b02016-04-05 14:54:55 -07007220 // Incoming messages have a non-empty sender.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007221 if (m.mSender != null && !TextUtils.isEmpty(m.mSender.getName())) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07007222 return m;
7223 }
7224 }
Selim Cinek88188f22017-09-19 16:46:56 -07007225 if (!messages.isEmpty()) {
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007226 // No incoming messages, fall back to outgoing message
Selim Cinek88188f22017-09-19 16:46:56 -07007227 return messages.get(messages.size() - 1);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007228 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07007229 return null;
7230 }
7231
7232 /**
7233 * @hide
7234 */
7235 @Override
7236 public RemoteViews makeBigContentView() {
Selim Cinek384804b2018-04-18 14:31:07 +08007237 return makeMessagingView(false /* displayImagesAtEnd */, true /* hideLargeIcon */);
Selim Cinekafeed292017-12-12 17:32:44 -08007238 }
7239
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007240 /**
7241 * Create a messaging layout.
7242 *
7243 * @param displayImagesAtEnd should images be displayed at the end of the content instead
7244 * of inline.
Selim Cinek384804b2018-04-18 14:31:07 +08007245 * @param hideRightIcons Should the reply affordance be shown at the end of the notification
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007246 * @return the created remoteView.
7247 */
Selim Cinekafeed292017-12-12 17:32:44 -08007248 @NonNull
Selim Cinek384804b2018-04-18 14:31:07 +08007249 private RemoteViews makeMessagingView(boolean displayImagesAtEnd, boolean hideRightIcons) {
Selim Cinek88188f22017-09-19 16:46:56 -07007250 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
Adrian Roosc1a80b02016-04-05 14:54:55 -07007251 ? super.mBigContentTitle
7252 : mConversationTitle;
Selim Cinekce8794f2018-05-23 16:46:05 -07007253 boolean atLeastP = mBuilder.mContext.getApplicationInfo().targetSdkVersion
7254 >= Build.VERSION_CODES.P;
7255 boolean isOneToOne;
Selim Cinek2dd3e722018-01-19 11:06:06 -08007256 CharSequence nameReplacement = null;
Selim Cinekce8794f2018-05-23 16:46:05 -07007257 Icon avatarReplacement = null;
7258 if (!atLeastP) {
7259 isOneToOne = TextUtils.isEmpty(conversationTitle);
7260 avatarReplacement = mBuilder.mN.mLargeIcon;
7261 if (hasOnlyWhiteSpaceSenders()) {
7262 isOneToOne = true;
7263 nameReplacement = conversationTitle;
7264 conversationTitle = null;
7265 }
7266 } else {
7267 isOneToOne = !isGroupConversation();
Adrian Roosb1f427c2016-05-26 12:27:15 -07007268 }
Selim Cinek384804b2018-04-18 14:31:07 +08007269 TemplateBindResult bindResult = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007270 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).title(
7271 conversationTitle).text(null)
7272 .hideLargeIcon(hideRightIcons || isOneToOne)
7273 .hideReplyIcon(hideRightIcons)
7274 .headerTextSecondary(conversationTitle);
Adrian Roos48d746a2016-04-12 14:57:28 -07007275 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07007276 mBuilder.getMessagingLayoutResource(),
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007277 p,
Selim Cinek384804b2018-04-18 14:31:07 +08007278 bindResult);
Selim Cinek88188f22017-09-19 16:46:56 -07007279 addExtras(mBuilder.mN.extras);
Selim Cinekafeed292017-12-12 17:32:44 -08007280 // also update the end margin if there is an image
Selim Cinek1c72fa02018-04-23 18:00:54 +08007281 contentView.setViewLayoutMarginEnd(R.id.notification_messaging,
7282 bindResult.getIconMarginEnd());
Selim Cinek88188f22017-09-19 16:46:56 -07007283 contentView.setInt(R.id.status_bar_latest_event_content, "setLayoutColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007284 mBuilder.isColorized(p) ? mBuilder.getPrimaryTextColor(p)
7285 : mBuilder.resolveContrastColor(p));
Kenny Guy14d035c2018-05-02 19:10:36 +01007286 contentView.setInt(R.id.status_bar_latest_event_content, "setSenderTextColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007287 mBuilder.getPrimaryTextColor(p));
Kenny Guy14d035c2018-05-02 19:10:36 +01007288 contentView.setInt(R.id.status_bar_latest_event_content, "setMessageTextColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007289 mBuilder.getSecondaryTextColor(p));
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007290 contentView.setBoolean(R.id.status_bar_latest_event_content, "setDisplayImagesAtEnd",
7291 displayImagesAtEnd);
Selim Cinekce8794f2018-05-23 16:46:05 -07007292 contentView.setIcon(R.id.status_bar_latest_event_content, "setAvatarReplacement",
7293 avatarReplacement);
Selim Cinek2dd3e722018-01-19 11:06:06 -08007294 contentView.setCharSequence(R.id.status_bar_latest_event_content, "setNameReplacement",
7295 nameReplacement);
Selim Cinek88188f22017-09-19 16:46:56 -07007296 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsOneToOne",
7297 isOneToOne);
7298 contentView.setBundle(R.id.status_bar_latest_event_content, "setData",
7299 mBuilder.mN.extras);
Alex Hillsfc737de2016-03-23 17:33:02 -04007300 return contentView;
7301 }
7302
Selim Cinekf7409db2017-10-24 16:17:14 -07007303 private boolean hasOnlyWhiteSpaceSenders() {
7304 for (int i = 0; i < mMessages.size(); i++) {
7305 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007306 Person sender = m.getSenderPerson();
7307 if (sender != null && !isWhiteSpace(sender.getName())) {
Selim Cinekf7409db2017-10-24 16:17:14 -07007308 return false;
7309 }
7310 }
7311 return true;
7312 }
7313
7314 private boolean isWhiteSpace(CharSequence sender) {
7315 if (TextUtils.isEmpty(sender)) {
7316 return true;
7317 }
7318 if (sender.toString().matches("^\\s*$")) {
7319 return true;
7320 }
7321 // Let's check if we only have 0 whitespace chars. Some apps did this as a workaround
7322 // For the presentation that we had.
7323 for (int i = 0; i < sender.length(); i++) {
7324 char c = sender.charAt(i);
7325 if (c != '\u200B') {
7326 return false;
7327 }
7328 }
7329 return true;
7330 }
7331
Selim Cinek88188f22017-09-19 16:46:56 -07007332 private CharSequence createConversationTitleFromMessages() {
7333 ArraySet<CharSequence> names = new ArraySet<>();
7334 for (int i = 0; i < mMessages.size(); i++) {
7335 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007336 Person sender = m.getSenderPerson();
Selim Cinek88188f22017-09-19 16:46:56 -07007337 if (sender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007338 names.add(sender.getName());
Selim Cinek88188f22017-09-19 16:46:56 -07007339 }
7340 }
7341 SpannableStringBuilder title = new SpannableStringBuilder();
7342 int size = names.size();
7343 for (int i = 0; i < size; i++) {
7344 CharSequence name = names.valueAt(i);
7345 if (!TextUtils.isEmpty(title)) {
7346 title.append(", ");
7347 }
7348 title.append(BidiFormatter.getInstance().unicodeWrap(name));
7349 }
7350 return title;
7351 }
7352
Adrian Roosdedd1df2016-04-26 16:38:47 -07007353 /**
7354 * @hide
7355 */
7356 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007357 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007358 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007359 true /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007360 remoteViews.setInt(R.id.notification_messaging, "setMaxDisplayedLines", 1);
7361 return remoteViews;
Adrian Roosdedd1df2016-04-26 16:38:47 -07007362 }
7363
Adrian Roosc1a80b02016-04-05 14:54:55 -07007364 private static TextAppearanceSpan makeFontColorSpan(int color) {
7365 return new TextAppearanceSpan(null, 0, 0,
7366 ColorStateList.valueOf(color), null);
7367 }
7368
Alex Hillsd9b04d92016-04-11 16:38:16 -04007369 public static final class Message {
Tony Mak09db2ea2018-06-27 18:12:48 +01007370 /** @hide */
7371 public static final String KEY_TEXT = "text";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007372 static final String KEY_TIMESTAMP = "time";
7373 static final String KEY_SENDER = "sender";
Selim Cinekcb8b9852017-12-15 18:01:52 -08007374 static final String KEY_SENDER_PERSON = "sender_person";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007375 static final String KEY_DATA_MIME_TYPE = "type";
7376 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08007377 static final String KEY_EXTRAS_BUNDLE = "extras";
Kenny Guya0f6de82018-04-06 16:20:16 +01007378 static final String KEY_REMOTE_INPUT_HISTORY = "remote_input_history";
Alex Hillsfc737de2016-03-23 17:33:02 -04007379
7380 private final CharSequence mText;
7381 private final long mTimestamp;
Selim Cinekcb8b9852017-12-15 18:01:52 -08007382 @Nullable
7383 private final Person mSender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007384 /** True if this message was generated from the extra
7385 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}
7386 */
7387 private final boolean mRemoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007388
Shane Brennan5a871862017-03-11 13:14:17 -08007389 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007390 private String mDataMimeType;
7391 private Uri mDataUri;
7392
7393 /**
7394 * Constructor
7395 * @param text A {@link CharSequence} to be displayed as the message content
7396 * @param timestamp Time at which the message arrived
7397 * @param sender A {@link CharSequence} to be used for displaying the name of the
7398 * sender. Should be <code>null</code> for messages by the current user, in which case
7399 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
7400 * Should be unique amongst all individuals in the conversation, and should be
7401 * consistent during re-posts of the notification.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007402 *
7403 * @deprecated use {@code Message(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04007404 */
7405 public Message(CharSequence text, long timestamp, CharSequence sender){
Selim Cinek9acd6732018-03-23 16:39:02 -07007406 this(text, timestamp, sender == null ? null
7407 : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08007408 }
7409
7410 /**
7411 * Constructor
7412 * @param text A {@link CharSequence} to be displayed as the message content
7413 * @param timestamp Time at which the message arrived
7414 * @param sender The {@link Person} who sent the message.
7415 * Should be <code>null</code> for messages by the current user, in which case
7416 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7417 * <p>
Selim Cinek9acd6732018-03-23 16:39:02 -07007418 * The person provided should contain an Icon, set with
7419 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7420 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7421 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7422 * to differentiate between the different users.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007423 * </p>
7424 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007425 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender) {
Kenny Guya0f6de82018-04-06 16:20:16 +01007426 this(text, timestamp, sender, false /* remoteHistory */);
7427 }
7428
7429 /**
7430 * Constructor
7431 * @param text A {@link CharSequence} to be displayed as the message content
7432 * @param timestamp Time at which the message arrived
7433 * @param sender The {@link Person} who sent the message.
7434 * Should be <code>null</code> for messages by the current user, in which case
7435 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7436 * @param remoteInputHistory True if the messages was generated from the extra
7437 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7438 * <p>
7439 * The person provided should contain an Icon, set with
7440 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7441 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7442 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7443 * to differentiate between the different users.
7444 * </p>
7445 * @hide
7446 */
7447 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender,
7448 boolean remoteInputHistory) {
Alex Hillsfc737de2016-03-23 17:33:02 -04007449 mText = text;
7450 mTimestamp = timestamp;
7451 mSender = sender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007452 mRemoteInputHistory = remoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007453 }
7454
7455 /**
7456 * Sets a binary blob of data and an associated MIME type for a message. In the case
7457 * where the platform doesn't support the MIME type, the original text provided in the
7458 * constructor will be used.
7459 * @param dataMimeType The MIME type of the content. See
7460 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
7461 * types on Android and Android Wear.
7462 * @param dataUri The uri containing the content whose type is given by the MIME type.
7463 * <p class="note">
7464 * <ol>
7465 * <li>Notification Listeners including the System UI need permission to access the
7466 * data the Uri points to. The recommended ways to do this are:</li>
7467 * <li>Store the data in your own ContentProvider, making sure that other apps have
7468 * the correct permission to access your provider. The preferred mechanism for
7469 * providing access is to use per-URI permissions which are temporary and only
7470 * grant access to the receiving application. An easy way to create a
7471 * ContentProvider like this is to use the FileProvider helper class.</li>
7472 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
7473 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
7474 * also store non-media types (see MediaStore.Files for more info). Files can be
7475 * inserted into the MediaStore using scanFile() after which a content:// style
7476 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
7477 * Note that once added to the system MediaStore the content is accessible to any
7478 * app on the device.</li>
7479 * </ol>
7480 * @return this object for method chaining
7481 */
7482 public Message setData(String dataMimeType, Uri dataUri) {
7483 mDataMimeType = dataMimeType;
7484 mDataUri = dataUri;
7485 return this;
7486 }
7487
Alex Hillsfc737de2016-03-23 17:33:02 -04007488 /**
7489 * Get the text to be used for this message, or the fallback text if a type and content
7490 * Uri have been set
7491 */
7492 public CharSequence getText() {
7493 return mText;
7494 }
7495
7496 /**
7497 * Get the time at which this message arrived
7498 */
7499 public long getTimestamp() {
7500 return mTimestamp;
7501 }
7502
7503 /**
Shane Brennan5a871862017-03-11 13:14:17 -08007504 * Get the extras Bundle for this message.
7505 */
7506 public Bundle getExtras() {
7507 return mExtras;
7508 }
7509
7510 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007511 * Get the text used to display the contact's name in the messaging experience
Selim Cinekcb8b9852017-12-15 18:01:52 -08007512 *
7513 * @deprecated use {@link #getSenderPerson()}
Alex Hillsfc737de2016-03-23 17:33:02 -04007514 */
7515 public CharSequence getSender() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007516 return mSender == null ? null : mSender.getName();
7517 }
7518
7519 /**
7520 * Get the sender associated with this message.
7521 */
7522 @Nullable
7523 public Person getSenderPerson() {
Alex Hillsfc737de2016-03-23 17:33:02 -04007524 return mSender;
7525 }
7526
7527 /**
7528 * Get the MIME type of the data pointed to by the Uri
7529 */
7530 public String getDataMimeType() {
7531 return mDataMimeType;
7532 }
7533
7534 /**
koprivaa1a78482018-10-09 10:09:23 -07007535 * Get the Uri pointing to the content of the message. Can be null, in which case
Alex Hillsfc737de2016-03-23 17:33:02 -04007536 * {@see #getText()} is used.
7537 */
7538 public Uri getDataUri() {
7539 return mDataUri;
7540 }
7541
Kenny Guya0f6de82018-04-06 16:20:16 +01007542 /**
7543 * @return True if the message was generated from
7544 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7545 * @hide
7546 */
7547 public boolean isRemoteInputHistory() {
7548 return mRemoteInputHistory;
7549 }
7550
Beverlye98937a2018-11-15 10:18:57 -05007551 /**
7552 * @hide
7553 */
7554 @VisibleForTesting
7555 public Bundle toBundle() {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007556 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007557 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007558 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04007559 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007560 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04007561 if (mSender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007562 // Legacy listeners need this
7563 bundle.putCharSequence(KEY_SENDER, mSender.getName());
7564 bundle.putParcelable(KEY_SENDER_PERSON, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04007565 }
7566 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007567 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04007568 }
7569 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007570 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04007571 }
Shane Brennan5a871862017-03-11 13:14:17 -08007572 if (mExtras != null) {
7573 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
7574 }
Kenny Guya0f6de82018-04-06 16:20:16 +01007575 if (mRemoteInputHistory) {
7576 bundle.putBoolean(KEY_REMOTE_INPUT_HISTORY, mRemoteInputHistory);
7577 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007578 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04007579 }
7580
Alex Hillsd9b04d92016-04-11 16:38:16 -04007581 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
7582 Bundle[] bundles = new Bundle[messages.size()];
7583 final int N = messages.size();
7584 for (int i = 0; i < N; i++) {
7585 bundles[i] = messages.get(i).toBundle();
7586 }
7587 return bundles;
7588 }
7589
Selim Cinek88188f22017-09-19 16:46:56 -07007590 /**
7591 * @return A list of messages read from the bundles.
7592 *
7593 * @hide
7594 */
7595 public static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
7596 if (bundles == null) {
7597 return new ArrayList<>();
7598 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007599 List<Message> messages = new ArrayList<>(bundles.length);
7600 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07007601 if (bundles[i] instanceof Bundle) {
7602 Message message = getMessageFromBundle((Bundle)bundles[i]);
7603 if (message != null) {
7604 messages.add(message);
7605 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007606 }
7607 }
7608 return messages;
7609 }
7610
Selim Cinekb0dc61b2018-05-22 18:49:36 -07007611 /**
7612 * @return The message that is stored in the bundle or null if the message couldn't be
7613 * resolved.
7614 *
7615 * @hide
7616 */
7617 @Nullable
7618 public static Message getMessageFromBundle(Bundle bundle) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007619 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07007620 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007621 return null;
7622 } else {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007623
7624 Person senderPerson = bundle.getParcelable(KEY_SENDER_PERSON);
7625 if (senderPerson == null) {
7626 // Legacy apps that use compat don't actually provide the sender objects
7627 // We need to fix the compat version to provide people / use
7628 // the native api instead
7629 CharSequence senderName = bundle.getCharSequence(KEY_SENDER);
7630 if (senderName != null) {
Selim Cinek9acd6732018-03-23 16:39:02 -07007631 senderPerson = new Person.Builder().setName(senderName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007632 }
7633 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007634 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
Selim Cinekcb8b9852017-12-15 18:01:52 -08007635 bundle.getLong(KEY_TIMESTAMP),
Kenny Guya0f6de82018-04-06 16:20:16 +01007636 senderPerson,
7637 bundle.getBoolean(KEY_REMOTE_INPUT_HISTORY, false));
Alex Hillsd9b04d92016-04-11 16:38:16 -04007638 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
7639 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007640 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
7641 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04007642 }
Shane Brennan5a871862017-03-11 13:14:17 -08007643 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
7644 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
7645 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007646 return message;
7647 }
7648 } catch (ClassCastException e) {
7649 return null;
7650 }
7651 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007652 }
7653 }
7654
7655 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04007656 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08007657 *
Robert Ly91c5ce32014-06-08 15:37:00 -07007658 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04007659 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07007660 * Notification notif = new Notification.Builder(mContext)
7661 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
7662 * .setContentText(subject)
7663 * .setSmallIcon(R.drawable.new_mail)
7664 * .setLargeIcon(aBitmap)
7665 * .setStyle(new Notification.InboxStyle()
7666 * .addLine(str1)
7667 * .addLine(str2)
7668 * .setContentTitle(&quot;&quot;)
7669 * .setSummaryText(&quot;+3 more&quot;))
7670 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04007671 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08007672 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04007673 * @see Notification#bigContentView
7674 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007675 public static class InboxStyle extends Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07007676
7677 /**
7678 * The number of lines of remote input history allowed until we start reducing lines.
7679 */
7680 private static final int NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION = 1;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007681 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
7682
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007683 public InboxStyle() {
7684 }
7685
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007686 /**
7687 * @deprecated use {@code InboxStyle()}.
7688 */
7689 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04007690 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007691 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04007692 }
7693
Chris Wrend6297db2012-05-03 16:20:13 -04007694 /**
7695 * Overrides ContentTitle in the big form of the template.
7696 * This defaults to the value passed to setContentTitle().
7697 */
7698 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007699 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04007700 return this;
7701 }
7702
7703 /**
7704 * Set the first line of text after the detail section in the big form of the template.
7705 */
7706 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007707 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04007708 return this;
7709 }
7710
Chris Wren0bd664d2012-08-01 13:56:56 -04007711 /**
7712 * Append a line to the digest section of the Inbox notification.
7713 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04007714 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007715 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04007716 return this;
7717 }
7718
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007719 /**
7720 * @hide
7721 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05007722 public ArrayList<CharSequence> getLines() {
7723 return mTexts;
7724 }
7725
7726 /**
7727 * @hide
7728 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007729 public void addExtras(Bundle extras) {
7730 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007731
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007732 CharSequence[] a = new CharSequence[mTexts.size()];
7733 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
7734 }
7735
Christoph Studer4600f9b2014-07-22 22:44:43 +02007736 /**
7737 * @hide
7738 */
7739 @Override
7740 protected void restoreFromExtras(Bundle extras) {
7741 super.restoreFromExtras(extras);
7742
7743 mTexts.clear();
7744 if (extras.containsKey(EXTRA_TEXT_LINES)) {
7745 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
7746 }
7747 }
7748
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007749 /**
7750 * @hide
7751 */
7752 public RemoteViews makeBigContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007753 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder).text(null);
Selim Cinek384804b2018-04-18 14:31:07 +08007754 TemplateBindResult result = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007755 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource(), p, result);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007756
Chris Wrend6297db2012-05-03 16:20:13 -04007757 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 -04007758 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04007759
Chris Wren4ed80d52012-05-17 09:30:03 -04007760 // Make sure all rows are gone in case we reuse a view.
7761 for (int rowId : rowIds) {
7762 contentView.setViewVisibility(rowId, View.GONE);
7763 }
7764
Daniel Sandler879c5e02012-04-17 16:46:51 -04007765 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07007766 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7767 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08007768 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07007769 int onlyViewId = 0;
7770 int maxRows = rowIds.length;
7771 if (mBuilder.mActions.size() > 0) {
7772 maxRows--;
7773 }
Selim Cinekbee4e072018-05-21 22:06:43 -07007774 CharSequence[] remoteInputHistory = mBuilder.mN.extras.getCharSequenceArray(
7775 EXTRA_REMOTE_INPUT_HISTORY);
7776 if (remoteInputHistory != null
7777 && remoteInputHistory.length > NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION) {
7778 // Let's remove some messages to make room for the remote input history.
7779 // 1 is always able to fit, but let's remove them if they are 2 or 3
7780 int numRemoteInputs = Math.min(remoteInputHistory.length,
7781 MAX_REMOTE_INPUT_HISTORY_LINES);
7782 int totalNumRows = mTexts.size() + numRemoteInputs
7783 - NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION;
7784 if (totalNumRows > maxRows) {
7785 int overflow = totalNumRows - maxRows;
7786 if (mTexts.size() > maxRows) {
7787 // Heuristic: if the Texts don't fit anyway, we'll rather drop the last
7788 // few messages, even with the remote input
7789 maxRows -= overflow;
7790 } else {
7791 // otherwise we drop the first messages
7792 i = overflow;
7793 }
7794 }
7795 }
Selim Cinek07c80172016-04-21 16:40:47 -07007796 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007797 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07007798 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007799 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07007800 contentView.setTextViewText(rowIds[i],
7801 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007802 mBuilder.setTextViewColorSecondary(contentView, rowIds[i], p);
Selim Cinek07c80172016-04-21 16:40:47 -07007803 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek384804b2018-04-18 14:31:07 +08007804 handleInboxImageMargin(contentView, rowIds[i], first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007805 result.getIconMarginEnd());
Selim Cinek07c80172016-04-21 16:40:47 -07007806 if (first) {
7807 onlyViewId = rowIds[i];
7808 } else {
7809 onlyViewId = 0;
7810 }
Selim Cinek247fa012016-02-18 09:50:48 -08007811 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007812 }
7813 i++;
7814 }
Selim Cinek07c80172016-04-21 16:40:47 -07007815 if (onlyViewId != 0) {
7816 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
7817 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7818 R.dimen.notification_text_margin_top);
7819 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
7820 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007821
Daniel Sandler879c5e02012-04-17 16:46:51 -04007822 return contentView;
7823 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007824
Julia Reynolds7217dc92018-03-07 12:12:09 -05007825 /**
7826 * @hide
7827 */
7828 @Override
7829 public boolean areNotificationsVisiblyDifferent(Style other) {
7830 if (other == null || getClass() != other.getClass()) {
7831 return true;
7832 }
7833 InboxStyle newS = (InboxStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04007834
7835 final ArrayList<CharSequence> myLines = getLines();
7836 final ArrayList<CharSequence> newLines = newS.getLines();
7837 final int n = myLines.size();
7838 if (n != newLines.size()) {
7839 return true;
7840 }
7841
7842 for (int i = 0; i < n; i++) {
7843 if (!Objects.equals(
7844 String.valueOf(myLines.get(i)),
7845 String.valueOf(newLines.get(i)))) {
7846 return true;
7847 }
7848 }
7849 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05007850 }
7851
Selim Cinek384804b2018-04-18 14:31:07 +08007852 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007853 int marginEndValue) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08007854 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08007855 if (first) {
7856 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
7857 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
7858 boolean hasProgress = max != 0 || ind;
Selim Cinek384804b2018-04-18 14:31:07 +08007859 if (!hasProgress) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08007860 endMargin = marginEndValue;
Selim Cinek247fa012016-02-18 09:50:48 -08007861 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007862 }
Selim Cinek1c72fa02018-04-23 18:00:54 +08007863 contentView.setViewLayoutMarginEnd(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08007864 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04007865 }
Dan Sandler842dd772014-05-15 09:36:47 -04007866
7867 /**
7868 * Notification style for media playback notifications.
7869 *
7870 * In the expanded form, {@link Notification#bigContentView}, up to 5
7871 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04007872 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04007873 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
7874 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
7875 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08007876 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04007877 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
7878 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01007879 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04007880 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08007881 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007882 * Notifications created with MediaStyle will have their category set to
7883 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
7884 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08007885 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07007886 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
7887 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04007888 * the System UI can identify this as a notification representing an active media session
7889 * and respond accordingly (by showing album artwork in the lockscreen, for example).
7890 *
Selim Cinek99104832017-01-25 14:47:33 -08007891 * <p>
7892 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
7893 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
7894 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
7895 * <p>
7896 *
Dan Sandler842dd772014-05-15 09:36:47 -04007897 * To use this style with your Notification, feed it to
7898 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7899 * <pre class="prettyprint">
7900 * Notification noti = new Notification.Builder()
7901 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01007902 * .setContentTitle(&quot;Track title&quot;)
7903 * .setContentText(&quot;Artist - Album&quot;)
7904 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007905 * .setStyle(<b>new Notification.MediaStyle()</b>
7906 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04007907 * .build();
7908 * </pre>
7909 *
7910 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08007911 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04007912 */
7913 public static class MediaStyle extends Style {
Gus Prevas9cc96602018-10-11 11:24:23 -04007914 // Changing max media buttons requires also changing templates
7915 // (notification_template_material_media and notification_template_material_big_media).
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007916 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04007917 static final int MAX_MEDIA_BUTTONS = 5;
Gus Prevas9cc96602018-10-11 11:24:23 -04007918 @IdRes private static final int[] MEDIA_BUTTON_IDS = {
7919 R.id.action0,
7920 R.id.action1,
7921 R.id.action2,
7922 R.id.action3,
7923 R.id.action4,
7924 };
Dan Sandler842dd772014-05-15 09:36:47 -04007925
7926 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07007927 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04007928
7929 public MediaStyle() {
7930 }
7931
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007932 /**
7933 * @deprecated use {@code MediaStyle()}.
7934 */
7935 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04007936 public MediaStyle(Builder builder) {
7937 setBuilder(builder);
7938 }
7939
7940 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007941 * 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 -04007942 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007943 *
7944 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04007945 */
7946 public MediaStyle setShowActionsInCompactView(int...actions) {
7947 mActionsToShowInCompact = actions;
7948 return this;
7949 }
7950
7951 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07007952 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
7953 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04007954 */
Jeff Browndba34ba2014-06-24 20:46:03 -07007955 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04007956 mToken = token;
7957 return this;
7958 }
7959
Christoph Studer4600f9b2014-07-22 22:44:43 +02007960 /**
7961 * @hide
7962 */
Dan Sandler842dd772014-05-15 09:36:47 -04007963 @Override
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007964 @UnsupportedAppUsage
Dan Sandler842dd772014-05-15 09:36:47 -04007965 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02007966 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007967 if (wip.category == null) {
7968 wip.category = Notification.CATEGORY_TRANSPORT;
7969 }
Dan Sandler842dd772014-05-15 09:36:47 -04007970 return wip;
7971 }
7972
Christoph Studer4600f9b2014-07-22 22:44:43 +02007973 /**
7974 * @hide
7975 */
7976 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007977 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007978 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007979 }
7980
7981 /**
7982 * @hide
7983 */
7984 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007985 public RemoteViews makeBigContentView() {
7986 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007987 }
7988
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007989 /**
7990 * @hide
7991 */
7992 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007993 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007994 RemoteViews expanded = makeMediaBigContentView();
7995 return expanded != null ? expanded : makeMediaContentView();
7996 }
7997
Dan Sandler842dd772014-05-15 09:36:47 -04007998 /** @hide */
7999 @Override
8000 public void addExtras(Bundle extras) {
8001 super.addExtras(extras);
8002
8003 if (mToken != null) {
8004 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
8005 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01008006 if (mActionsToShowInCompact != null) {
8007 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
8008 }
Dan Sandler842dd772014-05-15 09:36:47 -04008009 }
8010
Christoph Studer4600f9b2014-07-22 22:44:43 +02008011 /**
8012 * @hide
8013 */
8014 @Override
8015 protected void restoreFromExtras(Bundle extras) {
8016 super.restoreFromExtras(extras);
8017
8018 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
8019 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
8020 }
8021 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
8022 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
8023 }
8024 }
8025
Julia Reynolds7217dc92018-03-07 12:12:09 -05008026 /**
8027 * @hide
8028 */
8029 @Override
8030 public boolean areNotificationsVisiblyDifferent(Style other) {
8031 if (other == null || getClass() != other.getClass()) {
8032 return true;
8033 }
8034 // All fields to compare are on the Notification object
8035 return false;
8036 }
8037
Gus Prevas9cc96602018-10-11 11:24:23 -04008038 private void bindMediaActionButton(RemoteViews container, @IdRes int buttonId,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008039 Action action, StandardTemplateParams p) {
Dan Sandler842dd772014-05-15 09:36:47 -04008040 final boolean tombstone = (action.actionIntent == null);
Gus Prevas9cc96602018-10-11 11:24:23 -04008041 container.setViewVisibility(buttonId, View.VISIBLE);
8042 container.setImageViewIcon(buttonId, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07008043
8044 // If the action buttons should not be tinted, then just use the default
8045 // notification color. Otherwise, just use the passed-in color.
Gus Prevas9cc96602018-10-11 11:24:23 -04008046 Resources resources = mBuilder.mContext.getResources();
8047 Configuration currentConfig = resources.getConfiguration();
Lucas Dupinf03e7522018-06-25 16:21:13 -07008048 boolean inNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
8049 == Configuration.UI_MODE_NIGHT_YES;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008050 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized(p)
8051 ? getActionColor(p)
Lucas Dupina291d192018-06-07 13:59:42 -07008052 : ContrastColorUtil.resolveColor(mBuilder.mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07008053 Notification.COLOR_DEFAULT, inNightMode);
Anthony Chenad4d1582017-04-10 16:07:58 -07008054
Gus Prevas9cc96602018-10-11 11:24:23 -04008055 container.setDrawableTint(buttonId, false, tintColor,
Sunny Goyal5b153922017-09-21 21:00:36 -07008056 PorterDuff.Mode.SRC_ATOP);
Gus Prevas9cc96602018-10-11 11:24:23 -04008057
8058 final TypedArray typedArray = mBuilder.mContext.obtainStyledAttributes(
8059 new int[]{ android.R.attr.colorControlHighlight });
8060 int rippleAlpha = Color.alpha(typedArray.getColor(0, 0));
8061 typedArray.recycle();
8062 int rippleColor = Color.argb(rippleAlpha, Color.red(tintColor), Color.green(tintColor),
8063 Color.blue(tintColor));
8064 container.setRippleDrawableColor(buttonId, ColorStateList.valueOf(rippleColor));
8065
Dan Sandler842dd772014-05-15 09:36:47 -04008066 if (!tombstone) {
Gus Prevas9cc96602018-10-11 11:24:23 -04008067 container.setOnClickPendingIntent(buttonId, action.actionIntent);
Dan Sandler842dd772014-05-15 09:36:47 -04008068 }
Gus Prevas9cc96602018-10-11 11:24:23 -04008069 container.setContentDescription(buttonId, action.title);
Dan Sandler842dd772014-05-15 09:36:47 -04008070 }
8071
8072 private RemoteViews makeMediaContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008073 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).fillTextsFrom(
8074 mBuilder);
Dan Sandler842dd772014-05-15 09:36:47 -04008075 RemoteViews view = mBuilder.applyStandardTemplate(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008076 R.layout.notification_template_material_media, p,
Selim Cinek384804b2018-04-18 14:31:07 +08008077 null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04008078
8079 final int numActions = mBuilder.mActions.size();
Gus Prevas9cc96602018-10-11 11:24:23 -04008080 final int numActionsToShow = mActionsToShowInCompact == null
Dan Sandler842dd772014-05-15 09:36:47 -04008081 ? 0
8082 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Gus Prevas9cc96602018-10-11 11:24:23 -04008083 if (numActionsToShow > numActions) {
8084 throw new IllegalArgumentException(String.format(
8085 "setShowActionsInCompactView: action %d out of bounds (max %d)",
8086 numActions, numActions - 1));
8087 }
8088 for (int i = 0; i < MAX_MEDIA_BUTTONS_IN_COMPACT; i++) {
8089 if (i < numActionsToShow) {
Dan Sandler842dd772014-05-15 09:36:47 -04008090 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008091 bindMediaActionButton(view, MEDIA_BUTTON_IDS[i], action, p);
Gus Prevas9cc96602018-10-11 11:24:23 -04008092 } else {
8093 view.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04008094 }
8095 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08008096 handleImage(view);
8097 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008098 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07008099 if (mBuilder.mN.hasLargeIcon()) {
Selim Cinek384804b2018-04-18 14:31:07 +08008100 endMargin = R.dimen.notification_media_image_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08008101 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008102 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04008103 return view;
8104 }
8105
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008106 private int getActionColor(StandardTemplateParams p) {
8107 return mBuilder.isColorized(p) ? mBuilder.getPrimaryTextColor(p)
8108 : mBuilder.resolveContrastColor(p);
Selim Cinek99104832017-01-25 14:47:33 -08008109 }
8110
Dan Sandler842dd772014-05-15 09:36:47 -04008111 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008112 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008113 // Dont add an expanded view if there is no more content to be revealed
8114 int actionsInCompact = mActionsToShowInCompact == null
8115 ? 0
8116 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07008117 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008118 return null;
8119 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008120 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).fillTextsFrom(
8121 mBuilder);
Selim Cinek5bf069a2015-11-10 19:14:27 -05008122 RemoteViews big = mBuilder.applyStandardTemplate(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008123 R.layout.notification_template_material_big_media, p , null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04008124
Gus Prevas9cc96602018-10-11 11:24:23 -04008125 for (int i = 0; i < MAX_MEDIA_BUTTONS; i++) {
8126 if (i < actionCount) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008127 bindMediaActionButton(big, MEDIA_BUTTON_IDS[i], mBuilder.mActions.get(i), p);
Gus Prevas9cc96602018-10-11 11:24:23 -04008128 } else {
8129 big.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04008130 }
8131 }
linanson2bcd4032019-01-14 15:22:04 +08008132 bindMediaActionButton(big, R.id.media_seamless, new Action(R.drawable.ic_media_seamless,
8133 mBuilder.mContext.getString(
8134 com.android.internal.R.string.ext_media_seamless_action), null), p);
8135 big.setViewVisibility(R.id.media_seamless, View.GONE);
Selim Cinek5bf069a2015-11-10 19:14:27 -05008136 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04008137 return big;
8138 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008139
Selim Cinek5bf069a2015-11-10 19:14:27 -05008140 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07008141 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008142 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
8143 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008144 }
8145 }
8146
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008147 /**
8148 * @hide
8149 */
8150 @Override
8151 protected boolean hasProgress() {
8152 return false;
8153 }
Dan Sandler842dd772014-05-15 09:36:47 -04008154 }
Griff Hazen61a9e862014-05-22 16:05:19 -07008155
Selim Cinek593610c2016-02-16 18:42:57 -08008156 /**
8157 * Notification style for custom views that are decorated by the system
8158 *
8159 * <p>Instead of providing a notification that is completely custom, a developer can set this
8160 * style and still obtain system decorations like the notification header with the expand
8161 * affordance and actions.
8162 *
8163 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8164 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8165 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8166 * corresponding custom views to display.
8167 *
8168 * To use this style with your Notification, feed it to
8169 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8170 * <pre class="prettyprint">
8171 * Notification noti = new Notification.Builder()
8172 * .setSmallIcon(R.drawable.ic_stat_player)
8173 * .setLargeIcon(albumArtBitmap))
8174 * .setCustomContentView(contentView);
8175 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
8176 * .build();
8177 * </pre>
8178 */
8179 public static class DecoratedCustomViewStyle extends Style {
8180
8181 public DecoratedCustomViewStyle() {
8182 }
8183
Selim Cinek593610c2016-02-16 18:42:57 -08008184 /**
8185 * @hide
8186 */
8187 public boolean displayCustomViewInline() {
8188 return true;
8189 }
8190
8191 /**
8192 * @hide
8193 */
8194 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008195 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008196 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
8197 }
8198
8199 /**
8200 * @hide
8201 */
8202 @Override
8203 public RemoteViews makeBigContentView() {
8204 return makeDecoratedBigContentView();
8205 }
8206
8207 /**
8208 * @hide
8209 */
8210 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008211 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008212 return makeDecoratedHeadsUpContentView();
8213 }
8214
Selim Cinek593610c2016-02-16 18:42:57 -08008215 private RemoteViews makeDecoratedHeadsUpContentView() {
8216 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
8217 ? mBuilder.mN.contentView
8218 : mBuilder.mN.headsUpContentView;
8219 if (mBuilder.mActions.size() == 0) {
8220 return makeStandardTemplateWithCustomContent(headsUpContentView);
8221 }
Selim Cinek384804b2018-04-18 14:31:07 +08008222 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008223 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008224 mBuilder.getBigBaseLayoutResource(), result);
8225 buildIntoRemoteViewContent(remoteViews, headsUpContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008226 return remoteViews;
8227 }
8228
Selim Cinek593610c2016-02-16 18:42:57 -08008229 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
Selim Cinek384804b2018-04-18 14:31:07 +08008230 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008231 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08008232 mBuilder.getBaseLayoutResource(), result);
8233 buildIntoRemoteViewContent(remoteViews, customContent, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008234 return remoteViews;
8235 }
8236
Selim Cinek593610c2016-02-16 18:42:57 -08008237 private RemoteViews makeDecoratedBigContentView() {
8238 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
8239 ? mBuilder.mN.contentView
8240 : mBuilder.mN.bigContentView;
8241 if (mBuilder.mActions.size() == 0) {
8242 return makeStandardTemplateWithCustomContent(bigContentView);
8243 }
Selim Cinek384804b2018-04-18 14:31:07 +08008244 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008245 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008246 mBuilder.getBigBaseLayoutResource(), result);
8247 buildIntoRemoteViewContent(remoteViews, bigContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008248 return remoteViews;
8249 }
Selim Cinek247fa012016-02-18 09:50:48 -08008250
8251 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
Selim Cinek384804b2018-04-18 14:31:07 +08008252 RemoteViews customContent, TemplateBindResult result) {
Selim Cinek6fe4a102019-01-25 14:38:24 -08008253 int childIndex = -1;
Adrian Roos5081c0d2016-02-26 16:04:19 -08008254 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008255 // Need to clone customContent before adding, because otherwise it can no longer be
8256 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008257 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07008258 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
8259 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Sunny Goyalc12d31c2018-11-12 16:29:18 -08008260 remoteViews.addFlags(RemoteViews.FLAG_REAPPLY_DISALLOWED);
Selim Cinek6fe4a102019-01-25 14:38:24 -08008261 childIndex = 0;
Adrian Roos5081c0d2016-02-26 16:04:19 -08008262 }
Selim Cinek6fe4a102019-01-25 14:38:24 -08008263 remoteViews.setIntTag(R.id.notification_main_column,
8264 com.android.internal.R.id.notification_custom_view_index_tag,
8265 childIndex);
Selim Cinek247fa012016-02-18 09:50:48 -08008266 // also update the end margin if there is an image
Selim Cinek384804b2018-04-18 14:31:07 +08008267 Resources resources = mBuilder.mContext.getResources();
8268 int endMargin = resources.getDimensionPixelSize(
Selim Cinek1c72fa02018-04-23 18:00:54 +08008269 R.dimen.notification_content_margin_end) + result.getIconMarginEnd();
Selim Cinek384804b2018-04-18 14:31:07 +08008270 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08008271 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05008272
8273 /**
8274 * @hide
8275 */
8276 @Override
8277 public boolean areNotificationsVisiblyDifferent(Style other) {
8278 if (other == null || getClass() != other.getClass()) {
8279 return true;
8280 }
8281 // Comparison done for all custom RemoteViews, independent of style
8282 return false;
8283 }
Selim Cinek593610c2016-02-16 18:42:57 -08008284 }
8285
Selim Cinek03eb3b72016-02-18 10:39:45 -08008286 /**
8287 * Notification style for media custom views that are decorated by the system
8288 *
8289 * <p>Instead of providing a media notification that is completely custom, a developer can set
8290 * this style and still obtain system decorations like the notification header with the expand
8291 * affordance and actions.
8292 *
8293 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8294 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8295 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8296 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08008297 * <p>
8298 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
8299 * notification by using {@link Notification.Builder#setColorized(boolean)}.
8300 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08008301 * To use this style with your Notification, feed it to
8302 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8303 * <pre class="prettyprint">
8304 * Notification noti = new Notification.Builder()
8305 * .setSmallIcon(R.drawable.ic_stat_player)
8306 * .setLargeIcon(albumArtBitmap))
8307 * .setCustomContentView(contentView);
8308 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
8309 * .setMediaSession(mySession))
8310 * .build();
8311 * </pre>
8312 *
8313 * @see android.app.Notification.DecoratedCustomViewStyle
8314 * @see android.app.Notification.MediaStyle
8315 */
8316 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
8317
8318 public DecoratedMediaCustomViewStyle() {
8319 }
8320
Selim Cinek03eb3b72016-02-18 10:39:45 -08008321 /**
8322 * @hide
8323 */
8324 public boolean displayCustomViewInline() {
8325 return true;
8326 }
8327
8328 /**
8329 * @hide
8330 */
8331 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008332 public RemoteViews makeContentView(boolean increasedHeight) {
8333 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008334 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8335 mBuilder.mN.contentView);
8336 }
8337
8338 /**
8339 * @hide
8340 */
8341 @Override
8342 public RemoteViews makeBigContentView() {
8343 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
8344 ? mBuilder.mN.bigContentView
8345 : mBuilder.mN.contentView;
8346 return makeBigContentViewWithCustomContent(customRemoteView);
8347 }
8348
8349 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
8350 RemoteViews remoteViews = super.makeBigContentView();
8351 if (remoteViews != null) {
8352 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
8353 customRemoteView);
8354 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08008355 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008356 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8357 customRemoteView);
8358 } else {
8359 return null;
8360 }
8361 }
8362
8363 /**
8364 * @hide
8365 */
8366 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008367 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08008368 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
8369 ? mBuilder.mN.headsUpContentView
8370 : mBuilder.mN.contentView;
8371 return makeBigContentViewWithCustomContent(customRemoteView);
8372 }
8373
Julia Reynolds7217dc92018-03-07 12:12:09 -05008374 /**
8375 * @hide
8376 */
8377 @Override
8378 public boolean areNotificationsVisiblyDifferent(Style other) {
8379 if (other == null || getClass() != other.getClass()) {
8380 return true;
8381 }
8382 // Comparison done for all custom RemoteViews, independent of style
8383 return false;
8384 }
8385
Selim Cinek03eb3b72016-02-18 10:39:45 -08008386 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
8387 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08008388 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008389 // Need to clone customContent before adding, because otherwise it can no longer be
8390 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008391 customContent = customContent.clone();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008392 customContent.overrideTextColors(mBuilder.getPrimaryTextColor(mBuilder.mParams));
Selim Cinekf91017e2016-03-14 12:25:09 -07008393 remoteViews.removeAllViews(id);
8394 remoteViews.addView(id, customContent);
Sunny Goyalc12d31c2018-11-12 16:29:18 -08008395 remoteViews.addFlags(RemoteViews.FLAG_REAPPLY_DISALLOWED);
Adrian Roos5081c0d2016-02-26 16:04:19 -08008396 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08008397 return remoteViews;
8398 }
8399 }
8400
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008401 /**
8402 * Encapsulates the information needed to display a notification as a bubble.
8403 *
8404 * <p>A bubble is used to display app content in a floating window over the existing
8405 * foreground activity. A bubble has a collapsed state represented by an icon,
8406 * {@link BubbleMetadata.Builder#setIcon(Icon)} and an expanded state which is populated
8407 * via {@link BubbleMetadata.Builder#setIntent(PendingIntent)}.</p>
8408 *
8409 * <b>Notifications with a valid and allowed bubble will display in collapsed state
8410 * outside of the notification shade on unlocked devices. When a user interacts with the
8411 * collapsed bubble, the bubble intent will be invoked and displayed.</b>
8412 *
8413 * @see Notification.Builder#setBubbleMetadata(BubbleMetadata)
8414 */
8415 public static final class BubbleMetadata implements Parcelable {
8416
8417 private PendingIntent mPendingIntent;
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008418 private PendingIntent mDeleteIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008419 private Icon mIcon;
8420 private int mDesiredHeight;
Mady Mellor0157ff22019-01-24 15:31:48 -08008421 private int mFlags;
8422
8423 /**
8424 * If set and the app creating the bubble is in the foreground, the bubble will be posted
8425 * in its expanded state, with the contents of {@link #getIntent()} in a floating window.
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_AUTO_EXPAND_BUBBLE = 0x00000001;
8433
8434 /**
8435 * If set and the app creating the bubble is in the foreground, the bubble will be posted
8436 * <b>without</b> the associated notification in the notification shade. Subsequent update
8437 * notifications to this bubble will post a notification in the shade.
8438 *
8439 * <p>If the app creating the bubble is not in the foreground this flag has no effect.</p>
8440 *
8441 * <p>Generally this flag should only be set if the user has performed an action to request
8442 * or create a bubble.</p>
8443 */
8444 private static final int FLAG_SUPPRESS_INITIAL_NOTIFICATION = 0x00000002;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008445
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008446 private BubbleMetadata(PendingIntent expandIntent, PendingIntent deleteIntent,
Mady Mellor138f4c92019-02-20 16:12:17 -08008447 Icon icon, int height) {
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008448 mPendingIntent = expandIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008449 mIcon = icon;
8450 mDesiredHeight = height;
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008451 mDeleteIntent = deleteIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008452 }
8453
8454 private BubbleMetadata(Parcel in) {
8455 mPendingIntent = PendingIntent.CREATOR.createFromParcel(in);
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008456 mIcon = Icon.CREATOR.createFromParcel(in);
8457 mDesiredHeight = in.readInt();
Mady Mellor0157ff22019-01-24 15:31:48 -08008458 mFlags = in.readInt();
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008459 if (in.readInt() != 0) {
8460 mDeleteIntent = PendingIntent.CREATOR.createFromParcel(in);
8461 }
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008462 }
8463
8464 /**
8465 * @return the pending intent used to populate the floating window for this bubble.
8466 */
8467 public PendingIntent getIntent() {
8468 return mPendingIntent;
8469 }
8470
8471 /**
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008472 * @return the pending intent to send when the bubble is dismissed by a user, if one exists.
8473 */
8474 public PendingIntent getDeleteIntent() {
8475 return mDeleteIntent;
8476 }
8477
8478 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008479 * @return the title that will appear along with the app content defined by
8480 * {@link #getIntent()} for this bubble.
Mady Mellor138f4c92019-02-20 16:12:17 -08008481 *
8482 * @deprecated titles are no longer required or shown.
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008483 */
Mady Mellor138f4c92019-02-20 16:12:17 -08008484 @Deprecated
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008485 public CharSequence getTitle() {
Mady Mellor138f4c92019-02-20 16:12:17 -08008486 return "";
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008487 }
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008488 /**
8489 * @return the icon that will be displayed for this bubble when it is collapsed.
8490 */
8491 public Icon getIcon() {
8492 return mIcon;
8493 }
8494
8495 /**
8496 * @return the ideal height for the floating window that app content defined by
8497 * {@link #getIntent()} for this bubble.
8498 */
8499 public int getDesiredHeight() {
8500 return mDesiredHeight;
8501 }
8502
Mady Mellor0157ff22019-01-24 15:31:48 -08008503 /**
8504 * @return whether this bubble should auto expand when it is posted.
8505 *
8506 * @see BubbleMetadata.Builder#setAutoExpandBubble(boolean)
8507 */
8508 public boolean getAutoExpandBubble() {
8509 return (mFlags & FLAG_AUTO_EXPAND_BUBBLE) != 0;
8510 }
8511
8512 /**
8513 * @return whether this bubble should suppress the initial notification when it is posted.
8514 *
8515 * @see BubbleMetadata.Builder#setSuppressInitialNotification(boolean)
8516 */
8517 public boolean getSuppressInitialNotification() {
8518 return (mFlags & FLAG_SUPPRESS_INITIAL_NOTIFICATION) != 0;
8519 }
8520
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008521 public static final Parcelable.Creator<BubbleMetadata> CREATOR =
8522 new Parcelable.Creator<BubbleMetadata>() {
8523
8524 @Override
8525 public BubbleMetadata createFromParcel(Parcel source) {
8526 return new BubbleMetadata(source);
8527 }
8528
8529 @Override
8530 public BubbleMetadata[] newArray(int size) {
8531 return new BubbleMetadata[size];
8532 }
8533 };
8534
8535 @Override
8536 public int describeContents() {
8537 return 0;
8538 }
8539
8540 @Override
8541 public void writeToParcel(Parcel out, int flags) {
8542 mPendingIntent.writeToParcel(out, 0);
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008543 mIcon.writeToParcel(out, 0);
8544 out.writeInt(mDesiredHeight);
Mady Mellor0157ff22019-01-24 15:31:48 -08008545 out.writeInt(mFlags);
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008546 out.writeInt(mDeleteIntent != null ? 1 : 0);
8547 if (mDeleteIntent != null) {
8548 mDeleteIntent.writeToParcel(out, 0);
8549 }
Mady Mellor0157ff22019-01-24 15:31:48 -08008550 }
8551
8552 private void setFlags(int flags) {
8553 mFlags = flags;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008554 }
8555
8556 /**
8557 * Builder to construct a {@link BubbleMetadata} object.
8558 */
8559 public static class Builder {
8560
8561 private PendingIntent mPendingIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008562 private Icon mIcon;
8563 private int mDesiredHeight;
Mady Mellor0157ff22019-01-24 15:31:48 -08008564 private int mFlags;
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008565 private PendingIntent mDeleteIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008566
8567 /**
8568 * Constructs a new builder object.
8569 */
8570 public Builder() {
8571 }
8572
8573 /**
8574 * Sets the intent that will be used when the bubble is expanded. This will display the
8575 * app content in a floating window over the existing foreground activity.
8576 */
8577 public BubbleMetadata.Builder setIntent(PendingIntent intent) {
8578 if (intent == null) {
8579 throw new IllegalArgumentException("Bubble requires non-null pending intent");
8580 }
8581 mPendingIntent = intent;
8582 return this;
8583 }
8584
8585 /**
8586 * Sets the title that will appear along with the app content for this bubble.
8587 *
8588 * <p>A title is required and should expect to fit on a single line and make sense when
8589 * shown with the content defined by {@link #setIntent(PendingIntent)}.</p>
Mady Mellor138f4c92019-02-20 16:12:17 -08008590 *
8591 * @deprecated titles are no longer required or shown.
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008592 */
Mady Mellor138f4c92019-02-20 16:12:17 -08008593 @Deprecated
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008594 public BubbleMetadata.Builder setTitle(CharSequence title) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008595 return this;
8596 }
8597
8598 /**
8599 * Sets the icon that will represent the bubble when it is collapsed.
8600 *
8601 * <p>An icon is required and should be representative of the content within the bubble.
8602 * If your app produces multiple bubbles, the image should be unique for each of them.
8603 * </p>
8604 */
8605 public BubbleMetadata.Builder setIcon(Icon icon) {
8606 if (icon == null) {
8607 throw new IllegalArgumentException("Bubbles require non-null icon");
8608 }
8609 mIcon = icon;
8610 return this;
8611 }
8612
8613 /**
8614 * Sets the desired height for the app content defined by
8615 * {@link #setIntent(PendingIntent)}, this height may not be respected if there is not
8616 * enough space on the screen or if the provided height is too small to be useful.
8617 */
8618 public BubbleMetadata.Builder setDesiredHeight(int height) {
8619 mDesiredHeight = Math.max(height, 0);
8620 return this;
8621 }
8622
8623 /**
Mady Mellor0157ff22019-01-24 15:31:48 -08008624 * If set and the app creating the bubble is in the foreground, the bubble will be
8625 * posted in its expanded state, with the contents of {@link #getIntent()} in a
8626 * floating window.
8627 *
8628 * <p>If the app creating the bubble is not in the foreground this flag has no effect.
8629 * </p>
8630 *
8631 * <p>Generally this flag should only be set if the user has performed an action to
8632 * request or create a bubble.</p>
8633 */
8634 public BubbleMetadata.Builder setAutoExpandBubble(boolean shouldExpand) {
8635 setFlag(FLAG_AUTO_EXPAND_BUBBLE, shouldExpand);
8636 return this;
8637 }
8638
8639 /**
8640 * If set and the app creating the bubble is in the foreground, the bubble will be
8641 * posted <b>without</b> the associated notification in the notification shade.
8642 * Subsequent update notifications to this bubble will post a notification in the shade.
8643 *
8644 * <p>If the app creating the bubble is not in the foreground this flag has no effect.
8645 * </p>
8646 *
8647 * <p>Generally this flag should only be set if the user has performed an action to
8648 * request or create a bubble.</p>
8649 */
8650 public BubbleMetadata.Builder setSuppressInitialNotification(
8651 boolean shouldSupressNotif) {
8652 setFlag(FLAG_SUPPRESS_INITIAL_NOTIFICATION, shouldSupressNotif);
8653 return this;
8654 }
8655
8656 /**
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008657 * Sets an optional intent to send when this bubble is explicitly removed by the user.
8658 */
8659 public BubbleMetadata.Builder setDeleteIntent(PendingIntent deleteIntent) {
8660 mDeleteIntent = deleteIntent;
8661 return this;
8662 }
8663
8664 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008665 * Creates the {@link BubbleMetadata} defined by this builder.
8666 * <p>Will throw {@link IllegalStateException} if required fields have not been set
8667 * on this builder.</p>
8668 */
8669 public BubbleMetadata build() {
8670 if (mPendingIntent == null) {
8671 throw new IllegalStateException("Must supply pending intent to bubble");
8672 }
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008673 if (mIcon == null) {
8674 throw new IllegalStateException("Must supply an icon for the bubble");
8675 }
Mady Mellor138f4c92019-02-20 16:12:17 -08008676 BubbleMetadata data = new BubbleMetadata(mPendingIntent, mDeleteIntent,
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008677 mIcon, mDesiredHeight);
Mady Mellor0157ff22019-01-24 15:31:48 -08008678 data.setFlags(mFlags);
8679 return data;
8680 }
8681
8682 /**
8683 * @hide
8684 */
8685 public BubbleMetadata.Builder setFlag(int mask, boolean value) {
8686 if (value) {
8687 mFlags |= mask;
8688 } else {
8689 mFlags &= ~mask;
8690 }
8691 return this;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008692 }
8693 }
8694 }
8695
8696
Christoph Studer4600f9b2014-07-22 22:44:43 +02008697 // When adding a new Style subclass here, don't forget to update
8698 // Builder.getNotificationStyleClass.
8699
Griff Hazen61a9e862014-05-22 16:05:19 -07008700 /**
8701 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
8702 * metadata or change options on a notification builder.
8703 */
8704 public interface Extender {
8705 /**
8706 * Apply this extender to a notification builder.
8707 * @param builder the builder to be modified.
8708 * @return the build object for chaining.
8709 */
8710 public Builder extend(Builder builder);
8711 }
8712
8713 /**
8714 * Helper class to add wearable extensions to notifications.
8715 * <p class="note"> See
8716 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
8717 * for Android Wear</a> for more information on how to use this class.
8718 * <p>
8719 * To create a notification with wearable extensions:
8720 * <ol>
8721 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
8722 * properties.
8723 * <li>Create a {@link android.app.Notification.WearableExtender}.
8724 * <li>Set wearable-specific properties using the
8725 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
8726 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
8727 * notification.
8728 * <li>Post the notification to the notification system with the
8729 * {@code NotificationManager.notify(...)} methods.
8730 * </ol>
8731 *
8732 * <pre class="prettyprint">
8733 * Notification notif = new Notification.Builder(mContext)
8734 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
8735 * .setContentText(subject)
8736 * .setSmallIcon(R.drawable.new_mail)
8737 * .extend(new Notification.WearableExtender()
8738 * .setContentIcon(R.drawable.new_mail))
8739 * .build();
8740 * NotificationManager notificationManger =
8741 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
8742 * notificationManger.notify(0, notif);</pre>
8743 *
8744 * <p>Wearable extensions can be accessed on an existing notification by using the
8745 * {@code WearableExtender(Notification)} constructor,
8746 * and then using the {@code get} methods to access values.
8747 *
8748 * <pre class="prettyprint">
8749 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
8750 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07008751 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008752 */
8753 public static final class WearableExtender implements Extender {
8754 /**
8755 * Sentinel value for an action index that is unset.
8756 */
8757 public static final int UNSET_ACTION_INDEX = -1;
8758
8759 /**
8760 * Size value for use with {@link #setCustomSizePreset} to show this notification with
8761 * default sizing.
8762 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07008763 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07008764 * on their content.
Gus Prevasd7363752018-09-18 14:35:15 -04008765 *
8766 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008767 */
Gus Prevasd7363752018-09-18 14:35:15 -04008768 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008769 public static final int SIZE_DEFAULT = 0;
8770
8771 /**
8772 * Size value for use with {@link #setCustomSizePreset} to show this notification
8773 * with an extra small size.
8774 * <p>This value is only applicable for custom display notifications created using
8775 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008776 *
8777 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008778 */
Gus Prevasd7363752018-09-18 14:35:15 -04008779 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008780 public static final int SIZE_XSMALL = 1;
8781
8782 /**
8783 * Size value for use with {@link #setCustomSizePreset} to show this notification
8784 * with a small size.
8785 * <p>This value is only applicable for custom display notifications created using
8786 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008787 *
8788 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008789 */
Gus Prevasd7363752018-09-18 14:35:15 -04008790 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008791 public static final int SIZE_SMALL = 2;
8792
8793 /**
8794 * Size value for use with {@link #setCustomSizePreset} to show this notification
8795 * with a medium size.
8796 * <p>This value is only applicable for custom display notifications created using
8797 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008798 *
8799 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008800 */
Gus Prevasd7363752018-09-18 14:35:15 -04008801 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008802 public static final int SIZE_MEDIUM = 3;
8803
8804 /**
8805 * Size value for use with {@link #setCustomSizePreset} to show this notification
8806 * with a large size.
8807 * <p>This value is only applicable for custom display notifications created using
8808 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008809 *
8810 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008811 */
Gus Prevasd7363752018-09-18 14:35:15 -04008812 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008813 public static final int SIZE_LARGE = 4;
8814
Griff Hazend5f11f92014-05-27 15:40:09 -07008815 /**
8816 * Size value for use with {@link #setCustomSizePreset} to show this notification
8817 * full screen.
8818 * <p>This value is only applicable for custom display notifications created using
8819 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008820 *
8821 * @deprecated Display intents are no longer supported.
Griff Hazend5f11f92014-05-27 15:40:09 -07008822 */
Gus Prevasd7363752018-09-18 14:35:15 -04008823 @Deprecated
Griff Hazend5f11f92014-05-27 15:40:09 -07008824 public static final int SIZE_FULL_SCREEN = 5;
8825
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008826 /**
8827 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
8828 * short amount of time when this notification is displayed on the screen. This
8829 * is the default value.
Gus Prevasd7363752018-09-18 14:35:15 -04008830 *
8831 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008832 */
Gus Prevasd7363752018-09-18 14:35:15 -04008833 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008834 public static final int SCREEN_TIMEOUT_SHORT = 0;
8835
8836 /**
8837 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
8838 * for a longer amount of time when this notification is displayed on the screen.
Gus Prevasd7363752018-09-18 14:35:15 -04008839 *
8840 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008841 */
Gus Prevasd7363752018-09-18 14:35:15 -04008842 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008843 public static final int SCREEN_TIMEOUT_LONG = -1;
8844
Griff Hazen61a9e862014-05-22 16:05:19 -07008845 /** Notification extra which contains wearable extensions */
8846 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
8847
Pete Gastaf6781d2014-10-07 15:17:05 -04008848 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07008849 private static final String KEY_ACTIONS = "actions";
8850 private static final String KEY_FLAGS = "flags";
8851 private static final String KEY_DISPLAY_INTENT = "displayIntent";
8852 private static final String KEY_PAGES = "pages";
8853 private static final String KEY_BACKGROUND = "background";
8854 private static final String KEY_CONTENT_ICON = "contentIcon";
8855 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
8856 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
8857 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
8858 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
8859 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008860 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04008861 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008862 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07008863
8864 // Flags bitwise-ored to mFlags
8865 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
8866 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
8867 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
8868 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008869 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04008870 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04008871 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07008872
8873 // Default value for flags integer
8874 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
8875
8876 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
8877 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
8878
8879 private ArrayList<Action> mActions = new ArrayList<Action>();
8880 private int mFlags = DEFAULT_FLAGS;
8881 private PendingIntent mDisplayIntent;
8882 private ArrayList<Notification> mPages = new ArrayList<Notification>();
8883 private Bitmap mBackground;
8884 private int mContentIcon;
8885 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
8886 private int mContentActionIndex = UNSET_ACTION_INDEX;
8887 private int mCustomSizePreset = SIZE_DEFAULT;
8888 private int mCustomContentHeight;
8889 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008890 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04008891 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008892 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07008893
8894 /**
8895 * Create a {@link android.app.Notification.WearableExtender} with default
8896 * options.
8897 */
8898 public WearableExtender() {
8899 }
8900
8901 public WearableExtender(Notification notif) {
8902 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
8903 if (wearableBundle != null) {
8904 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
8905 if (actions != null) {
8906 mActions.addAll(actions);
8907 }
8908
8909 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
8910 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
8911
8912 Notification[] pages = getNotificationArrayFromBundle(
8913 wearableBundle, KEY_PAGES);
8914 if (pages != null) {
8915 Collections.addAll(mPages, pages);
8916 }
8917
8918 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
8919 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
8920 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
8921 DEFAULT_CONTENT_ICON_GRAVITY);
8922 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
8923 UNSET_ACTION_INDEX);
8924 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
8925 SIZE_DEFAULT);
8926 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
8927 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008928 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04008929 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008930 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07008931 }
8932 }
8933
8934 /**
8935 * Apply wearable extensions to a notification that is being built. This is typically
8936 * called by the {@link android.app.Notification.Builder#extend} method of
8937 * {@link android.app.Notification.Builder}.
8938 */
8939 @Override
8940 public Notification.Builder extend(Notification.Builder builder) {
8941 Bundle wearableBundle = new Bundle();
8942
8943 if (!mActions.isEmpty()) {
8944 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
8945 }
8946 if (mFlags != DEFAULT_FLAGS) {
8947 wearableBundle.putInt(KEY_FLAGS, mFlags);
8948 }
8949 if (mDisplayIntent != null) {
8950 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
8951 }
8952 if (!mPages.isEmpty()) {
8953 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
8954 new Notification[mPages.size()]));
8955 }
8956 if (mBackground != null) {
8957 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
8958 }
8959 if (mContentIcon != 0) {
8960 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
8961 }
8962 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
8963 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
8964 }
8965 if (mContentActionIndex != UNSET_ACTION_INDEX) {
8966 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
8967 mContentActionIndex);
8968 }
8969 if (mCustomSizePreset != SIZE_DEFAULT) {
8970 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
8971 }
8972 if (mCustomContentHeight != 0) {
8973 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
8974 }
8975 if (mGravity != DEFAULT_GRAVITY) {
8976 wearableBundle.putInt(KEY_GRAVITY, mGravity);
8977 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008978 if (mHintScreenTimeout != 0) {
8979 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
8980 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04008981 if (mDismissalId != null) {
8982 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
8983 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008984 if (mBridgeTag != null) {
8985 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
8986 }
Griff Hazen61a9e862014-05-22 16:05:19 -07008987
8988 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
8989 return builder;
8990 }
8991
8992 @Override
8993 public WearableExtender clone() {
8994 WearableExtender that = new WearableExtender();
8995 that.mActions = new ArrayList<Action>(this.mActions);
8996 that.mFlags = this.mFlags;
8997 that.mDisplayIntent = this.mDisplayIntent;
8998 that.mPages = new ArrayList<Notification>(this.mPages);
8999 that.mBackground = this.mBackground;
9000 that.mContentIcon = this.mContentIcon;
9001 that.mContentIconGravity = this.mContentIconGravity;
9002 that.mContentActionIndex = this.mContentActionIndex;
9003 that.mCustomSizePreset = this.mCustomSizePreset;
9004 that.mCustomContentHeight = this.mCustomContentHeight;
9005 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009006 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04009007 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009008 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07009009 return that;
9010 }
9011
9012 /**
9013 * Add a wearable action to this notification.
9014 *
9015 * <p>When wearable actions are added using this method, the set of actions that
9016 * show on a wearable device splits from devices that only show actions added
9017 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
9018 * of which actions display on different devices.
9019 *
9020 * @param action the action to add to this notification
9021 * @return this object for method chaining
9022 * @see android.app.Notification.Action
9023 */
9024 public WearableExtender addAction(Action action) {
9025 mActions.add(action);
9026 return this;
9027 }
9028
9029 /**
9030 * Adds wearable actions to this notification.
9031 *
9032 * <p>When wearable actions are added using this method, the set of actions that
9033 * show on a wearable device splits from devices that only show actions added
9034 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
9035 * of which actions display on different devices.
9036 *
9037 * @param actions the actions to add to this notification
9038 * @return this object for method chaining
9039 * @see android.app.Notification.Action
9040 */
9041 public WearableExtender addActions(List<Action> actions) {
9042 mActions.addAll(actions);
9043 return this;
9044 }
9045
9046 /**
9047 * Clear all wearable actions present on this builder.
9048 * @return this object for method chaining.
9049 * @see #addAction
9050 */
9051 public WearableExtender clearActions() {
9052 mActions.clear();
9053 return this;
9054 }
9055
9056 /**
9057 * Get the wearable actions present on this notification.
9058 */
9059 public List<Action> getActions() {
9060 return mActions;
9061 }
9062
9063 /**
9064 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07009065 * this notification. The {@link PendingIntent} provided should be for an activity.
9066 *
9067 * <pre class="prettyprint">
9068 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
9069 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
9070 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
9071 * Notification notif = new Notification.Builder(context)
9072 * .extend(new Notification.WearableExtender()
9073 * .setDisplayIntent(displayPendingIntent)
9074 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
9075 * .build();</pre>
9076 *
9077 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07009078 * should have an empty task affinity. It is also recommended to use the device
9079 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07009080 *
9081 * <p>Example AndroidManifest.xml entry:
9082 * <pre class="prettyprint">
9083 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
9084 * android:exported=&quot;true&quot;
9085 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07009086 * android:taskAffinity=&quot;&quot;
9087 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07009088 *
9089 * @param intent the {@link PendingIntent} for an activity
9090 * @return this object for method chaining
9091 * @see android.app.Notification.WearableExtender#getDisplayIntent
Gus Prevasd7363752018-09-18 14:35:15 -04009092 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009093 */
Gus Prevasd7363752018-09-18 14:35:15 -04009094 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009095 public WearableExtender setDisplayIntent(PendingIntent intent) {
9096 mDisplayIntent = intent;
9097 return this;
9098 }
9099
9100 /**
9101 * Get the intent to launch inside of an activity view when displaying this
9102 * notification. This {@code PendingIntent} should be for an activity.
Gus Prevasd7363752018-09-18 14:35:15 -04009103 *
9104 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009105 */
Gus Prevasd7363752018-09-18 14:35:15 -04009106 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009107 public PendingIntent getDisplayIntent() {
9108 return mDisplayIntent;
9109 }
9110
9111 /**
9112 * Add an additional page of content to display with this notification. The current
9113 * notification forms the first page, and pages added using this function form
9114 * subsequent pages. This field can be used to separate a notification into multiple
9115 * sections.
9116 *
9117 * @param page the notification to add as another page
9118 * @return this object for method chaining
9119 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04009120 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009121 */
Gus Prevasd7363752018-09-18 14:35:15 -04009122 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009123 public WearableExtender addPage(Notification page) {
9124 mPages.add(page);
9125 return this;
9126 }
9127
9128 /**
9129 * Add additional pages of content to display with this notification. The current
9130 * notification forms the first page, and pages added using this function form
9131 * subsequent pages. This field can be used to separate a notification into multiple
9132 * sections.
9133 *
9134 * @param pages a list of notifications
9135 * @return this object for method chaining
9136 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04009137 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009138 */
Gus Prevasd7363752018-09-18 14:35:15 -04009139 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009140 public WearableExtender addPages(List<Notification> pages) {
9141 mPages.addAll(pages);
9142 return this;
9143 }
9144
9145 /**
9146 * Clear all additional pages present on this builder.
9147 * @return this object for method chaining.
9148 * @see #addPage
Gus Prevasd7363752018-09-18 14:35:15 -04009149 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009150 */
Gus Prevasd7363752018-09-18 14:35:15 -04009151 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009152 public WearableExtender clearPages() {
9153 mPages.clear();
9154 return this;
9155 }
9156
9157 /**
9158 * Get the array of additional pages of content for displaying this notification. The
9159 * current notification forms the first page, and elements within this array form
9160 * subsequent pages. This field can be used to separate a notification into multiple
9161 * sections.
9162 * @return the pages for this notification
Gus Prevasd7363752018-09-18 14:35:15 -04009163 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009164 */
Gus Prevasd7363752018-09-18 14:35:15 -04009165 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009166 public List<Notification> getPages() {
9167 return mPages;
9168 }
9169
9170 /**
9171 * Set a background image to be displayed behind the notification content.
9172 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
9173 * will work with any notification style.
9174 *
9175 * @param background the background bitmap
9176 * @return this object for method chaining
9177 * @see android.app.Notification.WearableExtender#getBackground
Gus Prevasd7363752018-09-18 14:35:15 -04009178 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009179 */
Gus Prevasd7363752018-09-18 14:35:15 -04009180 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009181 public WearableExtender setBackground(Bitmap background) {
9182 mBackground = background;
9183 return this;
9184 }
9185
9186 /**
9187 * Get a background image to be displayed behind the notification content.
9188 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
9189 * will work with any notification style.
9190 *
9191 * @return the background image
9192 * @see android.app.Notification.WearableExtender#setBackground
Gus Prevasd7363752018-09-18 14:35:15 -04009193 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009194 */
Gus Prevasd7363752018-09-18 14:35:15 -04009195 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009196 public Bitmap getBackground() {
9197 return mBackground;
9198 }
9199
9200 /**
9201 * Set an icon that goes with the content of this notification.
9202 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009203 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009204 public WearableExtender setContentIcon(int icon) {
9205 mContentIcon = icon;
9206 return this;
9207 }
9208
9209 /**
9210 * Get an icon that goes with the content of this notification.
9211 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009212 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009213 public int getContentIcon() {
9214 return mContentIcon;
9215 }
9216
9217 /**
9218 * Set the gravity that the content icon should have within the notification display.
9219 * Supported values include {@link android.view.Gravity#START} and
9220 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
9221 * @see #setContentIcon
9222 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009223 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009224 public WearableExtender setContentIconGravity(int contentIconGravity) {
9225 mContentIconGravity = contentIconGravity;
9226 return this;
9227 }
9228
9229 /**
9230 * Get the gravity that the content icon should have within the notification display.
9231 * Supported values include {@link android.view.Gravity#START} and
9232 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
9233 * @see #getContentIcon
9234 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009235 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009236 public int getContentIconGravity() {
9237 return mContentIconGravity;
9238 }
9239
9240 /**
Gus Prevasd7363752018-09-18 14:35:15 -04009241 * Set an action from this notification's actions as the primary action. If the action has a
9242 * {@link RemoteInput} associated with it, shortcuts to the options for that input are shown
9243 * directly on the notification.
Griff Hazen14f57992014-05-26 09:07:14 -07009244 *
Gus Prevasd7363752018-09-18 14:35:15 -04009245 * @param actionIndex The index of the primary action.
Griff Hazen14f57992014-05-26 09:07:14 -07009246 * If wearable actions were added to the main notification, this index
9247 * will apply to that list, otherwise it will apply to the regular
9248 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07009249 */
9250 public WearableExtender setContentAction(int actionIndex) {
9251 mContentActionIndex = actionIndex;
9252 return this;
9253 }
9254
9255 /**
Gus Prevasd7363752018-09-18 14:35:15 -04009256 * Get the index of the notification action, if any, that was specified as the primary
9257 * action.
Griff Hazen14f57992014-05-26 09:07:14 -07009258 *
9259 * <p>If wearable specific actions were added to the main notification, this index will
9260 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07009261 *
9262 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07009263 */
9264 public int getContentAction() {
9265 return mContentActionIndex;
9266 }
9267
9268 /**
9269 * Set the gravity that this notification should have within the available viewport space.
9270 * Supported values include {@link android.view.Gravity#TOP},
9271 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
9272 * The default value is {@link android.view.Gravity#BOTTOM}.
9273 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009274 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009275 public WearableExtender setGravity(int gravity) {
9276 mGravity = gravity;
9277 return this;
9278 }
9279
9280 /**
9281 * Get the gravity that this notification should have within the available viewport space.
9282 * Supported values include {@link android.view.Gravity#TOP},
9283 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
9284 * The default value is {@link android.view.Gravity#BOTTOM}.
9285 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009286 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009287 public int getGravity() {
9288 return mGravity;
9289 }
9290
9291 /**
9292 * Set the custom size preset for the display of this notification out of the available
9293 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
9294 * {@link #SIZE_LARGE}.
9295 * <p>Some custom size presets are only applicable for custom display notifications created
9296 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
9297 * documentation for the preset in question. See also
9298 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
9299 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009300 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009301 public WearableExtender setCustomSizePreset(int sizePreset) {
9302 mCustomSizePreset = sizePreset;
9303 return this;
9304 }
9305
9306 /**
9307 * Get the custom size preset for the display of this notification out of the available
9308 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
9309 * {@link #SIZE_LARGE}.
9310 * <p>Some custom size presets are only applicable for custom display notifications created
9311 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
9312 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
9313 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009314 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009315 public int getCustomSizePreset() {
9316 return mCustomSizePreset;
9317 }
9318
9319 /**
9320 * Set the custom height in pixels for the display of this notification's content.
9321 * <p>This option is only available for custom display notifications created
9322 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
9323 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
9324 * {@link #getCustomContentHeight}.
9325 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009326 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009327 public WearableExtender setCustomContentHeight(int height) {
9328 mCustomContentHeight = height;
9329 return this;
9330 }
9331
9332 /**
9333 * Get the custom height in pixels for the display of this notification's content.
9334 * <p>This option is only available for custom display notifications created
9335 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
9336 * {@link #setCustomContentHeight}.
9337 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009338 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009339 public int getCustomContentHeight() {
9340 return mCustomContentHeight;
9341 }
9342
9343 /**
9344 * Set whether the scrolling position for the contents of this notification should start
9345 * at the bottom of the contents instead of the top when the contents are too long to
9346 * display within the screen. Default is false (start scroll at the top).
9347 */
9348 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
9349 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
9350 return this;
9351 }
9352
9353 /**
9354 * Get whether the scrolling position for the contents of this notification should start
9355 * at the bottom of the contents instead of the top when the contents are too long to
9356 * display within the screen. Default is false (start scroll at the top).
9357 */
9358 public boolean getStartScrollBottom() {
9359 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
9360 }
9361
9362 /**
9363 * Set whether the content intent is available when the wearable device is not connected
9364 * to a companion device. The user can still trigger this intent when the wearable device
9365 * is offline, but a visual hint will indicate that the content intent may not be available.
9366 * Defaults to true.
9367 */
9368 public WearableExtender setContentIntentAvailableOffline(
9369 boolean contentIntentAvailableOffline) {
9370 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
9371 return this;
9372 }
9373
9374 /**
9375 * Get whether the content intent is available when the wearable device is not connected
9376 * to a companion device. The user can still trigger this intent when the wearable device
9377 * is offline, but a visual hint will indicate that the content intent may not be available.
9378 * Defaults to true.
9379 */
9380 public boolean getContentIntentAvailableOffline() {
9381 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
9382 }
9383
9384 /**
9385 * Set a hint that this notification's icon should not be displayed.
9386 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
9387 * @return this object for method chaining
9388 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009389 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009390 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
9391 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
9392 return this;
9393 }
9394
9395 /**
9396 * Get a hint that this notification's icon should not be displayed.
9397 * @return {@code true} if this icon should not be displayed, false otherwise.
9398 * The default value is {@code false} if this was never set.
9399 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009400 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009401 public boolean getHintHideIcon() {
9402 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
9403 }
9404
9405 /**
9406 * Set a visual hint that only the background image of this notification should be
9407 * displayed, and other semantic content should be hidden. This hint is only applicable
9408 * to sub-pages added using {@link #addPage}.
9409 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009410 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009411 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
9412 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
9413 return this;
9414 }
9415
9416 /**
9417 * Get a visual hint that only the background image of this notification should be
9418 * displayed, and other semantic content should be hidden. This hint is only applicable
9419 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
9420 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009421 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009422 public boolean getHintShowBackgroundOnly() {
9423 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
9424 }
9425
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009426 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009427 * Set a hint that this notification's background should not be clipped if possible,
9428 * and should instead be resized to fully display on the screen, retaining the aspect
9429 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009430 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
9431 * @return this object for method chaining
9432 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009433 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009434 public WearableExtender setHintAvoidBackgroundClipping(
9435 boolean hintAvoidBackgroundClipping) {
9436 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
9437 return this;
9438 }
9439
9440 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009441 * Get a hint that this notification's background should not be clipped if possible,
9442 * and should instead be resized to fully display on the screen, retaining the aspect
9443 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009444 * @return {@code true} if it's ok if the background is clipped on the screen, false
9445 * otherwise. The default value is {@code false} if this was never set.
9446 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009447 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009448 public boolean getHintAvoidBackgroundClipping() {
9449 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
9450 }
9451
9452 /**
9453 * Set a hint that the screen should remain on for at least this duration when
9454 * this notification is displayed on the screen.
9455 * @param timeout The requested screen timeout in milliseconds. Can also be either
9456 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9457 * @return this object for method chaining
9458 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009459 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009460 public WearableExtender setHintScreenTimeout(int timeout) {
9461 mHintScreenTimeout = timeout;
9462 return this;
9463 }
9464
9465 /**
9466 * Get the duration, in milliseconds, that the screen should remain on for
9467 * when this notification is displayed.
9468 * @return the duration in milliseconds if > 0, or either one of the sentinel values
9469 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9470 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009471 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009472 public int getHintScreenTimeout() {
9473 return mHintScreenTimeout;
9474 }
9475
Alex Hills9ab3a232016-04-05 14:54:56 -04009476 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04009477 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
9478 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9479 * qr codes, as well as other simple black-and-white tickets.
9480 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
9481 * @return this object for method chaining
Gus Prevasd7363752018-09-18 14:35:15 -04009482 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009483 */
Gus Prevasd7363752018-09-18 14:35:15 -04009484 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009485 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
9486 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
9487 return this;
9488 }
9489
9490 /**
9491 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
9492 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9493 * qr codes, as well as other simple black-and-white tickets.
9494 * @return {@code true} if it should be displayed in ambient, false otherwise
9495 * otherwise. The default value is {@code false} if this was never set.
Gus Prevasd7363752018-09-18 14:35:15 -04009496 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009497 */
Gus Prevasd7363752018-09-18 14:35:15 -04009498 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009499 public boolean getHintAmbientBigPicture() {
9500 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
9501 }
9502
9503 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04009504 * Set a hint that this notification's content intent will launch an {@link Activity}
9505 * directly, telling the platform that it can generate the appropriate transitions.
9506 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
9507 * an activity and transitions should be generated, false otherwise.
9508 * @return this object for method chaining
9509 */
9510 public WearableExtender setHintContentIntentLaunchesActivity(
9511 boolean hintContentIntentLaunchesActivity) {
9512 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
9513 return this;
9514 }
9515
9516 /**
9517 * Get a hint that this notification's content intent will launch an {@link Activity}
9518 * directly, telling the platform that it can generate the appropriate transitions
9519 * @return {@code true} if the content intent will launch an activity and transitions should
9520 * be generated, false otherwise. The default value is {@code false} if this was never set.
9521 */
9522 public boolean getHintContentIntentLaunchesActivity() {
9523 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
9524 }
9525
Nadia Benbernou948627e2016-04-14 14:41:08 -04009526 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009527 * Sets the dismissal id for this notification. If a notification is posted with a
9528 * dismissal id, then when that notification is canceled, notifications on other wearables
9529 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04009530 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009531 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04009532 * @param dismissalId the dismissal id of the notification.
9533 * @return this object for method chaining
9534 */
9535 public WearableExtender setDismissalId(String dismissalId) {
9536 mDismissalId = dismissalId;
9537 return this;
9538 }
9539
9540 /**
9541 * Returns the dismissal id of the notification.
9542 * @return the dismissal id of the notification or null if it has not been set.
9543 */
9544 public String getDismissalId() {
9545 return mDismissalId;
9546 }
9547
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009548 /**
9549 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
9550 * posted from a phone to provide finer-grained control on what notifications are bridged
9551 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
9552 * Features to Notifications</a> for more information.
9553 * @param bridgeTag the bridge tag of the notification.
9554 * @return this object for method chaining
9555 */
9556 public WearableExtender setBridgeTag(String bridgeTag) {
9557 mBridgeTag = bridgeTag;
9558 return this;
9559 }
9560
9561 /**
9562 * Returns the bridge tag of the notification.
9563 * @return the bridge tag or null if not present.
9564 */
9565 public String getBridgeTag() {
9566 return mBridgeTag;
9567 }
9568
Griff Hazen61a9e862014-05-22 16:05:19 -07009569 private void setFlag(int mask, boolean value) {
9570 if (value) {
9571 mFlags |= mask;
9572 } else {
9573 mFlags &= ~mask;
9574 }
9575 }
9576 }
9577
9578 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009579 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
9580 * with car extensions:
9581 *
9582 * <ol>
9583 * <li>Create an {@link Notification.Builder}, setting any desired
9584 * properties.
9585 * <li>Create a {@link CarExtender}.
9586 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
9587 * {@link CarExtender}.
9588 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9589 * to apply the extensions to a notification.
9590 * </ol>
9591 *
9592 * <pre class="prettyprint">
9593 * Notification notification = new Notification.Builder(context)
9594 * ...
9595 * .extend(new CarExtender()
9596 * .set*(...))
9597 * .build();
9598 * </pre>
9599 *
9600 * <p>Car extensions can be accessed on an existing notification by using the
9601 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
9602 * to access values.
9603 */
9604 public static final class CarExtender implements Extender {
9605 private static final String TAG = "CarExtender";
9606
9607 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
9608 private static final String EXTRA_LARGE_ICON = "large_icon";
9609 private static final String EXTRA_CONVERSATION = "car_conversation";
9610 private static final String EXTRA_COLOR = "app_color";
9611
9612 private Bitmap mLargeIcon;
9613 private UnreadConversation mUnreadConversation;
9614 private int mColor = Notification.COLOR_DEFAULT;
9615
9616 /**
9617 * Create a {@link CarExtender} with default options.
9618 */
9619 public CarExtender() {
9620 }
9621
9622 /**
9623 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
9624 *
9625 * @param notif The notification from which to copy options.
9626 */
9627 public CarExtender(Notification notif) {
9628 Bundle carBundle = notif.extras == null ?
9629 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
9630 if (carBundle != null) {
9631 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
9632 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
9633
9634 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
9635 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
9636 }
9637 }
9638
9639 /**
9640 * Apply car extensions to a notification that is being built. This is typically called by
9641 * the {@link Notification.Builder#extend(Notification.Extender)}
9642 * method of {@link Notification.Builder}.
9643 */
9644 @Override
9645 public Notification.Builder extend(Notification.Builder builder) {
9646 Bundle carExtensions = new Bundle();
9647
9648 if (mLargeIcon != null) {
9649 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
9650 }
9651 if (mColor != Notification.COLOR_DEFAULT) {
9652 carExtensions.putInt(EXTRA_COLOR, mColor);
9653 }
9654
9655 if (mUnreadConversation != null) {
9656 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
9657 carExtensions.putBundle(EXTRA_CONVERSATION, b);
9658 }
9659
9660 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
9661 return builder;
9662 }
9663
9664 /**
9665 * Sets the accent color to use when Android Auto presents the notification.
9666 *
9667 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
9668 * to accent the displayed notification. However, not all colors are acceptable in an
9669 * automotive setting. This method can be used to override the color provided in the
9670 * notification in such a situation.
9671 */
Tor Norbye80756e32015-03-02 09:39:27 -08009672 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009673 mColor = color;
9674 return this;
9675 }
9676
9677 /**
9678 * Gets the accent color.
9679 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04009680 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009681 */
Tor Norbye80756e32015-03-02 09:39:27 -08009682 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009683 public int getColor() {
9684 return mColor;
9685 }
9686
9687 /**
9688 * Sets the large icon of the car notification.
9689 *
9690 * If no large icon is set in the extender, Android Auto will display the icon
9691 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
9692 *
9693 * @param largeIcon The large icon to use in the car notification.
9694 * @return This object for method chaining.
9695 */
9696 public CarExtender setLargeIcon(Bitmap largeIcon) {
9697 mLargeIcon = largeIcon;
9698 return this;
9699 }
9700
9701 /**
9702 * Gets the large icon used in this car notification, or null if no icon has been set.
9703 *
9704 * @return The large icon for the car notification.
9705 * @see CarExtender#setLargeIcon
9706 */
9707 public Bitmap getLargeIcon() {
9708 return mLargeIcon;
9709 }
9710
9711 /**
9712 * Sets the unread conversation in a message notification.
9713 *
9714 * @param unreadConversation The unread part of the conversation this notification conveys.
9715 * @return This object for method chaining.
9716 */
9717 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
9718 mUnreadConversation = unreadConversation;
9719 return this;
9720 }
9721
9722 /**
9723 * Returns the unread conversation conveyed by this notification.
9724 * @see #setUnreadConversation(UnreadConversation)
9725 */
9726 public UnreadConversation getUnreadConversation() {
9727 return mUnreadConversation;
9728 }
9729
9730 /**
9731 * A class which holds the unread messages from a conversation.
9732 */
9733 public static class UnreadConversation {
9734 private static final String KEY_AUTHOR = "author";
9735 private static final String KEY_TEXT = "text";
9736 private static final String KEY_MESSAGES = "messages";
9737 private static final String KEY_REMOTE_INPUT = "remote_input";
9738 private static final String KEY_ON_REPLY = "on_reply";
9739 private static final String KEY_ON_READ = "on_read";
9740 private static final String KEY_PARTICIPANTS = "participants";
9741 private static final String KEY_TIMESTAMP = "timestamp";
9742
9743 private final String[] mMessages;
9744 private final RemoteInput mRemoteInput;
9745 private final PendingIntent mReplyPendingIntent;
9746 private final PendingIntent mReadPendingIntent;
9747 private final String[] mParticipants;
9748 private final long mLatestTimestamp;
9749
9750 UnreadConversation(String[] messages, RemoteInput remoteInput,
9751 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
9752 String[] participants, long latestTimestamp) {
9753 mMessages = messages;
9754 mRemoteInput = remoteInput;
9755 mReadPendingIntent = readPendingIntent;
9756 mReplyPendingIntent = replyPendingIntent;
9757 mParticipants = participants;
9758 mLatestTimestamp = latestTimestamp;
9759 }
9760
9761 /**
9762 * Gets the list of messages conveyed by this notification.
9763 */
9764 public String[] getMessages() {
9765 return mMessages;
9766 }
9767
9768 /**
9769 * Gets the remote input that will be used to convey the response to a message list, or
9770 * null if no such remote input exists.
9771 */
9772 public RemoteInput getRemoteInput() {
9773 return mRemoteInput;
9774 }
9775
9776 /**
9777 * Gets the pending intent that will be triggered when the user replies to this
9778 * notification.
9779 */
9780 public PendingIntent getReplyPendingIntent() {
9781 return mReplyPendingIntent;
9782 }
9783
9784 /**
9785 * Gets the pending intent that Android Auto will send after it reads aloud all messages
9786 * in this object's message list.
9787 */
9788 public PendingIntent getReadPendingIntent() {
9789 return mReadPendingIntent;
9790 }
9791
9792 /**
9793 * Gets the participants in the conversation.
9794 */
9795 public String[] getParticipants() {
9796 return mParticipants;
9797 }
9798
9799 /**
9800 * Gets the firs participant in the conversation.
9801 */
9802 public String getParticipant() {
9803 return mParticipants.length > 0 ? mParticipants[0] : null;
9804 }
9805
9806 /**
9807 * Gets the timestamp of the conversation.
9808 */
9809 public long getLatestTimestamp() {
9810 return mLatestTimestamp;
9811 }
9812
9813 Bundle getBundleForUnreadConversation() {
9814 Bundle b = new Bundle();
9815 String author = null;
9816 if (mParticipants != null && mParticipants.length > 1) {
9817 author = mParticipants[0];
9818 }
9819 Parcelable[] messages = new Parcelable[mMessages.length];
9820 for (int i = 0; i < messages.length; i++) {
9821 Bundle m = new Bundle();
9822 m.putString(KEY_TEXT, mMessages[i]);
9823 m.putString(KEY_AUTHOR, author);
9824 messages[i] = m;
9825 }
9826 b.putParcelableArray(KEY_MESSAGES, messages);
9827 if (mRemoteInput != null) {
9828 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
9829 }
9830 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
9831 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
9832 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
9833 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
9834 return b;
9835 }
9836
9837 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
9838 if (b == null) {
9839 return null;
9840 }
9841 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
9842 String[] messages = null;
9843 if (parcelableMessages != null) {
9844 String[] tmp = new String[parcelableMessages.length];
9845 boolean success = true;
9846 for (int i = 0; i < tmp.length; i++) {
9847 if (!(parcelableMessages[i] instanceof Bundle)) {
9848 success = false;
9849 break;
9850 }
9851 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
9852 if (tmp[i] == null) {
9853 success = false;
9854 break;
9855 }
9856 }
9857 if (success) {
9858 messages = tmp;
9859 } else {
9860 return null;
9861 }
9862 }
9863
9864 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
9865 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
9866
9867 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
9868
9869 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
9870 if (participants == null || participants.length != 1) {
9871 return null;
9872 }
9873
9874 return new UnreadConversation(messages,
9875 remoteInput,
9876 onReply,
9877 onRead,
9878 participants, b.getLong(KEY_TIMESTAMP));
9879 }
9880 };
9881
9882 /**
9883 * Builder class for {@link CarExtender.UnreadConversation} objects.
9884 */
9885 public static class Builder {
9886 private final List<String> mMessages = new ArrayList<String>();
9887 private final String mParticipant;
9888 private RemoteInput mRemoteInput;
9889 private PendingIntent mReadPendingIntent;
9890 private PendingIntent mReplyPendingIntent;
9891 private long mLatestTimestamp;
9892
9893 /**
9894 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
9895 *
9896 * @param name The name of the other participant in the conversation.
9897 */
9898 public Builder(String name) {
9899 mParticipant = name;
9900 }
9901
9902 /**
9903 * Appends a new unread message to the list of messages for this conversation.
9904 *
9905 * The messages should be added from oldest to newest.
9906 *
9907 * @param message The text of the new unread message.
9908 * @return This object for method chaining.
9909 */
9910 public Builder addMessage(String message) {
9911 mMessages.add(message);
9912 return this;
9913 }
9914
9915 /**
9916 * Sets the pending intent and remote input which will convey the reply to this
9917 * notification.
9918 *
9919 * @param pendingIntent The pending intent which will be triggered on a reply.
9920 * @param remoteInput The remote input parcelable which will carry the reply.
9921 * @return This object for method chaining.
9922 *
9923 * @see CarExtender.UnreadConversation#getRemoteInput
9924 * @see CarExtender.UnreadConversation#getReplyPendingIntent
9925 */
9926 public Builder setReplyAction(
9927 PendingIntent pendingIntent, RemoteInput remoteInput) {
9928 mRemoteInput = remoteInput;
9929 mReplyPendingIntent = pendingIntent;
9930
9931 return this;
9932 }
9933
9934 /**
9935 * Sets the pending intent that will be sent once the messages in this notification
9936 * are read.
9937 *
9938 * @param pendingIntent The pending intent to use.
9939 * @return This object for method chaining.
9940 */
9941 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
9942 mReadPendingIntent = pendingIntent;
9943 return this;
9944 }
9945
9946 /**
9947 * Sets the timestamp of the most recent message in an unread conversation.
9948 *
9949 * If a messaging notification has been posted by your application and has not
9950 * yet been cancelled, posting a later notification with the same id and tag
9951 * but without a newer timestamp may result in Android Auto not displaying a
9952 * heads up notification for the later notification.
9953 *
9954 * @param timestamp The timestamp of the most recent message in the conversation.
9955 * @return This object for method chaining.
9956 */
9957 public Builder setLatestTimestamp(long timestamp) {
9958 mLatestTimestamp = timestamp;
9959 return this;
9960 }
9961
9962 /**
9963 * Builds a new unread conversation object.
9964 *
9965 * @return The new unread conversation object.
9966 */
9967 public UnreadConversation build() {
9968 String[] messages = mMessages.toArray(new String[mMessages.size()]);
9969 String[] participants = { mParticipant };
9970 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
9971 mReadPendingIntent, participants, mLatestTimestamp);
9972 }
9973 }
9974 }
9975
9976 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009977 * <p>Helper class to add Android TV extensions to notifications. To create a notification
9978 * with a TV extension:
9979 *
9980 * <ol>
9981 * <li>Create an {@link Notification.Builder}, setting any desired properties.
9982 * <li>Create a {@link TvExtender}.
9983 * <li>Set TV-specific properties using the {@code set} methods of
9984 * {@link TvExtender}.
9985 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9986 * to apply the extension to a notification.
9987 * </ol>
9988 *
9989 * <pre class="prettyprint">
9990 * Notification notification = new Notification.Builder(context)
9991 * ...
9992 * .extend(new TvExtender()
9993 * .set*(...))
9994 * .build();
9995 * </pre>
9996 *
9997 * <p>TV extensions can be accessed on an existing notification by using the
9998 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
9999 * to access values.
10000 *
10001 * @hide
10002 */
10003 @SystemApi
10004 public static final class TvExtender implements Extender {
10005 private static final String TAG = "TvExtender";
10006
10007 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
10008 private static final String EXTRA_FLAGS = "flags";
10009 private static final String EXTRA_CONTENT_INTENT = "content_intent";
10010 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010011 private static final String EXTRA_CHANNEL_ID = "channel_id";
Rhiannon Malia1a083932018-01-24 15:02:30 -080010012 private static final String EXTRA_SUPPRESS_SHOW_OVER_APPS = "suppressShowOverApps";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010013
10014 // Flags bitwise-ored to mFlags
10015 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
10016
10017 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010018 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010019 private PendingIntent mContentIntent;
10020 private PendingIntent mDeleteIntent;
Rhiannon Malia1a083932018-01-24 15:02:30 -080010021 private boolean mSuppressShowOverApps;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010022
10023 /**
10024 * Create a {@link TvExtender} with default options.
10025 */
10026 public TvExtender() {
10027 mFlags = FLAG_AVAILABLE_ON_TV;
10028 }
10029
10030 /**
10031 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
10032 *
10033 * @param notif The notification from which to copy options.
10034 */
10035 public TvExtender(Notification notif) {
10036 Bundle bundle = notif.extras == null ?
10037 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
10038 if (bundle != null) {
10039 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010040 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Rhiannon Malia1a083932018-01-24 15:02:30 -080010041 mSuppressShowOverApps = bundle.getBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010042 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
10043 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
10044 }
10045 }
10046
10047 /**
10048 * Apply a TV extension to a notification that is being built. This is typically called by
10049 * the {@link Notification.Builder#extend(Notification.Extender)}
10050 * method of {@link Notification.Builder}.
10051 */
10052 @Override
10053 public Notification.Builder extend(Notification.Builder builder) {
10054 Bundle bundle = new Bundle();
10055
10056 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010057 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Rhiannon Malia1a083932018-01-24 15:02:30 -080010058 bundle.putBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS, mSuppressShowOverApps);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010059 if (mContentIntent != null) {
10060 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
10061 }
10062
10063 if (mDeleteIntent != null) {
10064 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
10065 }
10066
10067 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
10068 return builder;
10069 }
10070
10071 /**
10072 * Returns true if this notification should be shown on TV. This method return true
10073 * if the notification was extended with a TvExtender.
10074 */
10075 public boolean isAvailableOnTv() {
10076 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
10077 }
10078
10079 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010080 * Specifies the channel the notification should be delivered on when shown on TV.
10081 * It can be different from the channel that the notification is delivered to when
10082 * posting on a non-TV device.
10083 */
10084 public TvExtender setChannel(String channelId) {
10085 mChannelId = channelId;
10086 return this;
10087 }
10088
10089 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -040010090 * Specifies the channel the notification should be delivered on when shown on TV.
10091 * It can be different from the channel that the notification is delivered to when
10092 * posting on a non-TV device.
10093 */
10094 public TvExtender setChannelId(String channelId) {
10095 mChannelId = channelId;
10096 return this;
10097 }
10098
Jeff Sharkey000ce802017-04-29 13:13:27 -060010099 /** @removed */
10100 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010101 public String getChannel() {
10102 return mChannelId;
10103 }
10104
10105 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -040010106 * Returns the id of the channel this notification posts to on TV.
10107 */
10108 public String getChannelId() {
10109 return mChannelId;
10110 }
10111
10112 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010113 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
10114 * If provided, it is used instead of the content intent specified
10115 * at the level of Notification.
10116 */
10117 public TvExtender setContentIntent(PendingIntent intent) {
10118 mContentIntent = intent;
10119 return this;
10120 }
10121
10122 /**
10123 * Returns the TV-specific content intent. If this method returns null, the
10124 * main content intent on the notification should be used.
10125 *
10126 * @see {@link Notification#contentIntent}
10127 */
10128 public PendingIntent getContentIntent() {
10129 return mContentIntent;
10130 }
10131
10132 /**
10133 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
10134 * by the user on TV. If provided, it is used instead of the delete intent specified
10135 * at the level of Notification.
10136 */
10137 public TvExtender setDeleteIntent(PendingIntent intent) {
10138 mDeleteIntent = intent;
10139 return this;
10140 }
10141
10142 /**
10143 * Returns the TV-specific delete intent. If this method returns null, the
10144 * main delete intent on the notification should be used.
10145 *
10146 * @see {@link Notification#deleteIntent}
10147 */
10148 public PendingIntent getDeleteIntent() {
10149 return mDeleteIntent;
10150 }
Rhiannon Malia1a083932018-01-24 15:02:30 -080010151
10152 /**
10153 * Specifies whether this notification should suppress showing a message over top of apps
10154 * outside of the launcher.
10155 */
10156 public TvExtender setSuppressShowOverApps(boolean suppress) {
10157 mSuppressShowOverApps = suppress;
10158 return this;
10159 }
10160
10161 /**
10162 * Returns true if this notification should not show messages over top of apps
10163 * outside of the launcher.
10164 */
10165 public boolean getSuppressShowOverApps() {
10166 return mSuppressShowOverApps;
10167 }
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010168 }
10169
10170 /**
Griff Hazen61a9e862014-05-22 16:05:19 -070010171 * Get an array of Notification objects from a parcelable array bundle field.
10172 * Update the bundle to have a typed array so fetches in the future don't need
10173 * to do an array copy.
10174 */
10175 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
10176 Parcelable[] array = bundle.getParcelableArray(key);
10177 if (array instanceof Notification[] || array == null) {
10178 return (Notification[]) array;
10179 }
10180 Notification[] typedArray = Arrays.copyOf(array, array.length,
10181 Notification[].class);
10182 bundle.putParcelableArray(key, typedArray);
10183 return typedArray;
10184 }
Christoph Studer4600f9b2014-07-22 22:44:43 +020010185
10186 private static class BuilderRemoteViews extends RemoteViews {
10187 public BuilderRemoteViews(Parcel parcel) {
10188 super(parcel);
10189 }
10190
Kenny Guy77320062014-08-27 21:37:15 +010010191 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
10192 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +020010193 }
10194
10195 @Override
10196 public BuilderRemoteViews clone() {
10197 Parcel p = Parcel.obtain();
10198 writeToParcel(p, 0);
10199 p.setDataPosition(0);
10200 BuilderRemoteViews brv = new BuilderRemoteViews(p);
10201 p.recycle();
10202 return brv;
10203 }
10204 }
Adrian Roos70d7aa32017-01-11 15:39:06 -080010205
Selim Cinek384804b2018-04-18 14:31:07 +080010206 /**
10207 * A result object where information about the template that was created is saved.
10208 */
10209 private static class TemplateBindResult {
Selim Cinek1c72fa02018-04-23 18:00:54 +080010210 int mIconMarginEnd;
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010211 boolean mRightIconContainerVisible;
Selim Cinek384804b2018-04-18 14:31:07 +080010212
10213 /**
Selim Cinek1c72fa02018-04-23 18:00:54 +080010214 * Get the margin end that needs to be added to any fields that may overlap
Selim Cinek384804b2018-04-18 14:31:07 +080010215 * with the right actions.
10216 */
Selim Cinek1c72fa02018-04-23 18:00:54 +080010217 public int getIconMarginEnd() {
10218 return mIconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +080010219 }
10220
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010221 /**
10222 * Is the icon container visible on the right size because of the reply button or the
10223 * right icon.
10224 */
10225 public boolean isRightIconContainerVisible() {
10226 return mRightIconContainerVisible;
10227 }
10228
Selim Cinek1c72fa02018-04-23 18:00:54 +080010229 public void setIconMarginEnd(int iconMarginEnd) {
10230 this.mIconMarginEnd = iconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +080010231 }
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010232
10233 public void setRightIconContainerVisible(boolean iconContainerVisible) {
10234 mRightIconContainerVisible = iconContainerVisible;
10235 }
Selim Cinek384804b2018-04-18 14:31:07 +080010236 }
10237
Adrian Roos70d7aa32017-01-11 15:39:06 -080010238 private static class StandardTemplateParams {
10239 boolean hasProgress = true;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010240 CharSequence title;
10241 CharSequence text;
Selim Cinekafeed292017-12-12 17:32:44 -080010242 CharSequence headerTextSecondary;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010243 CharSequence summaryText;
Selim Cinekbee4e072018-05-21 22:06:43 -070010244 int maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek88188f22017-09-19 16:46:56 -070010245 boolean hideLargeIcon;
Selim Cinek384804b2018-04-18 14:31:07 +080010246 boolean hideReplyIcon;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010247 boolean allowColorization = true;
10248 boolean forceDefaultColor = false;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010249
10250 final StandardTemplateParams reset() {
10251 hasProgress = true;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010252 title = null;
10253 text = null;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010254 summaryText = null;
Selim Cinekafeed292017-12-12 17:32:44 -080010255 headerTextSecondary = null;
Selim Cinekbee4e072018-05-21 22:06:43 -070010256 maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010257 allowColorization = true;
10258 forceDefaultColor = false;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010259 return this;
10260 }
10261
10262 final StandardTemplateParams hasProgress(boolean hasProgress) {
10263 this.hasProgress = hasProgress;
10264 return this;
10265 }
10266
10267 final StandardTemplateParams title(CharSequence title) {
10268 this.title = title;
10269 return this;
10270 }
10271
10272 final StandardTemplateParams text(CharSequence text) {
10273 this.text = text;
10274 return this;
10275 }
10276
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010277 final StandardTemplateParams summaryText(CharSequence text) {
10278 this.summaryText = text;
10279 return this;
10280 }
10281
Selim Cinekafeed292017-12-12 17:32:44 -080010282 final StandardTemplateParams headerTextSecondary(CharSequence text) {
10283 this.headerTextSecondary = text;
10284 return this;
10285 }
10286
Selim Cinek384804b2018-04-18 14:31:07 +080010287 final StandardTemplateParams hideLargeIcon(boolean hideLargeIcon) {
10288 this.hideLargeIcon = hideLargeIcon;
Selim Cinek88188f22017-09-19 16:46:56 -070010289 return this;
10290 }
10291
Selim Cinek384804b2018-04-18 14:31:07 +080010292 final StandardTemplateParams hideReplyIcon(boolean hideReplyIcon) {
10293 this.hideReplyIcon = hideReplyIcon;
Selim Cinek88188f22017-09-19 16:46:56 -070010294 return this;
10295 }
10296
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010297 final StandardTemplateParams disallowColorization() {
10298 this.allowColorization = false;
10299 return this;
10300 }
10301
10302 final StandardTemplateParams forceDefaultColor() {
10303 this.forceDefaultColor = true;
10304 return this;
10305 }
10306
Adrian Roos70d7aa32017-01-11 15:39:06 -080010307 final StandardTemplateParams fillTextsFrom(Builder b) {
10308 Bundle extras = b.mN.extras;
Lucas Dupin00be88f2019-01-03 17:50:52 -080010309 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE));
Lucas Dupin06c5e642017-09-13 16:34:58 -070010310
Lucas Dupin06c5e642017-09-13 16:34:58 -070010311 CharSequence text = extras.getCharSequence(EXTRA_BIG_TEXT);
Lucas Dupin00be88f2019-01-03 17:50:52 -080010312 if (TextUtils.isEmpty(text)) {
Lucas Dupin06c5e642017-09-13 16:34:58 -070010313 text = extras.getCharSequence(EXTRA_TEXT);
10314 }
Lucas Dupin00be88f2019-01-03 17:50:52 -080010315 this.text = b.processLegacyText(text);
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010316 this.summaryText = extras.getCharSequence(EXTRA_SUB_TEXT);
Adrian Roos70d7aa32017-01-11 15:39:06 -080010317 return this;
10318 }
Selim Cinekbee4e072018-05-21 22:06:43 -070010319
10320 /**
10321 * Set the maximum lines of remote input history lines allowed.
10322 * @param maxRemoteInputHistory The number of lines.
10323 * @return The builder for method chaining.
10324 */
10325 public StandardTemplateParams setMaxRemoteInputHistory(int maxRemoteInputHistory) {
10326 this.maxRemoteInputHistory = maxRemoteInputHistory;
10327 return this;
10328 }
Adrian Roos70d7aa32017-01-11 15:39:06 -080010329 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010330}