blob: c9bd95da5c4271c53c6aa0d23e82a4efb5f62714 [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;
Mady Mellor7af771a2019-03-07 15:04:54 -080022import android.annotation.DimenRes;
Tor Norbye7b9c9122013-05-30 16:48:33 -070023import android.annotation.DrawableRes;
Gus Prevas9cc96602018-10-11 11:24:23 -040024import android.annotation.IdRes;
Tor Norbyed9273d62013-05-30 15:59:53 -070025import android.annotation.IntDef;
Alex Hillsfd590442016-10-07 09:52:44 -040026import android.annotation.NonNull;
Selim Cinek88188f22017-09-19 16:46:56 -070027import android.annotation.Nullable;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060028import android.annotation.RequiresPermission;
Daniel Sandler01df1c62014-06-09 10:54:01 -040029import android.annotation.SdkConstant;
30import android.annotation.SdkConstant.SdkConstantType;
Julia Reynoldse46bb372016-03-17 11:05:58 -040031import android.annotation.SystemApi;
Mathew Inwood61e8ae62018-08-14 14:17:44 +010032import android.annotation.UnsupportedAppUsage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.content.Context;
34import android.content.Intent;
Felipe Leme90205ef2019-03-05 09:59:52 -080035import android.content.LocusId;
Kenny Guy77320062014-08-27 21:37:15 +010036import android.content.pm.ApplicationInfo;
Dan Sandler732bd6c2016-04-12 14:20:32 -040037import android.content.pm.PackageManager;
Christoph Studer4600f9b2014-07-22 22:44:43 +020038import android.content.pm.PackageManager.NameNotFoundException;
Julia Reynolds13d898c2017-02-02 12:22:05 -050039import android.content.pm.ShortcutInfo;
Jorim Jaggief72a192014-08-26 21:57:46 +020040import android.content.res.ColorStateList;
Anthony Chenad4d1582017-04-10 16:07:58 -070041import android.content.res.Configuration;
42import android.content.res.Resources;
Gus Prevas9cc96602018-10-11 11:24:23 -040043import android.content.res.TypedArray;
Joe Onoratoef1e7762010-09-17 18:38:38 -040044import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010045import android.graphics.Canvas;
Adrian Roosc1a80b02016-04-05 14:54:55 -070046import android.graphics.Color;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010047import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010048import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040049import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040050import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070051import android.media.AudioManager;
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -080052import android.media.PlayerBase;
Jeff Browndba34ba2014-06-24 20:46:03 -070053import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040055import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020056import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050057import android.os.Bundle;
Dianne Hackborn98305522017-05-05 17:53:53 -070058import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.os.Parcel;
60import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040061import android.os.SystemClock;
Selim Cinek6743c0b2017-01-18 18:24:01 -080062import android.os.SystemProperties;
Jeff Sharkey6d515712012-09-20 16:06:08 -070063import android.os.UserHandle;
Adrian Roosc1a80b02016-04-05 14:54:55 -070064import android.text.BidiFormatter;
Selim Cinek60a54252016-02-26 17:03:25 -080065import android.text.SpannableStringBuilder;
66import android.text.Spanned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.text.TextUtils;
Selim Cinek60a54252016-02-26 17:03:25 -080068import android.text.style.AbsoluteSizeSpan;
Selim Cinek89991a22016-03-07 19:51:54 -080069import android.text.style.CharacterStyle;
Selim Cinek981962e2016-07-20 20:41:58 -070070import android.text.style.ForegroundColorSpan;
Selim Cinek60a54252016-02-26 17:03:25 -080071import android.text.style.RelativeSizeSpan;
72import android.text.style.TextAppearanceSpan;
Svet Ganovddb94882016-06-23 19:55:24 -070073import android.util.ArraySet;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040074import android.util.Log;
Tony Mak638430e2018-10-08 19:19:10 +010075import android.util.Pair;
Dan Sandler50128532015-12-08 15:42:41 -050076import android.util.SparseArray;
Yi Jin6b514142017-10-30 14:54:12 -070077import android.util.proto.ProtoOutputStream;
Griff Hazen61a9e862014-05-22 16:05:19 -070078import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080079import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080080import android.view.View;
Selim Cinek954cc232016-05-20 13:29:23 -070081import android.view.ViewGroup;
Felipe Leme90205ef2019-03-05 09:59:52 -080082import android.view.contentcapture.ContentCaptureContext;
Jeff Sharkey1c400132011-08-05 14:50:13 -070083import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import android.widget.RemoteViews;
85
Griff Hazen959591e2014-05-15 22:26:18 -070086import com.android.internal.R;
Selim Cinek389edcd2017-05-11 19:16:44 -070087import com.android.internal.annotations.VisibleForTesting;
Svet Ganovddb94882016-06-23 19:55:24 -070088import com.android.internal.util.ArrayUtils;
Lucas Dupina291d192018-06-07 13:59:42 -070089import com.android.internal.util.ContrastColorUtil;
Griff Hazen959591e2014-05-15 22:26:18 -070090
Tor Norbyed9273d62013-05-30 15:59:53 -070091import java.lang.annotation.Retention;
92import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020093import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050094import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070095import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070096import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070097import java.util.List;
Julia Reynolds7217dc92018-03-07 12:12:09 -050098import java.util.Objects;
Dan Sandler50128532015-12-08 15:42:41 -050099import java.util.Set;
Jeff Sharkey23b31182018-04-18 21:32:12 -0600100import java.util.function.Consumer;
Joe Onorato561d3852010-11-20 18:09:34 -0800101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102/**
103 * A class that represents how a persistent notification is to be presented to
104 * the user using the {@link android.app.NotificationManager}.
105 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800106 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
107 * easier to construct Notifications.</p>
108 *
Joe Fernandez558459f2011-10-13 16:47:36 -0700109 * <div class="special reference">
110 * <h3>Developer Guides</h3>
111 * <p>For a guide to creating notifications, read the
112 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
113 * developer guide.</p>
114 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 */
116public class Notification implements Parcelable
117{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -0400118 private static final String TAG = "Notification";
119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -0400121 * An activity that provides a user interface for adjusting notification preferences for its
Julia Reynolds3aedded2017-03-31 14:42:09 -0400122 * containing application.
Daniel Sandler01df1c62014-06-09 10:54:01 -0400123 */
124 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
125 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
126 = "android.intent.category.NOTIFICATION_PREFERENCES";
127
128 /**
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500129 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
130 * contain a {@link NotificationChannel#getId() channel id} that can be used to narrow down
Julia Reynolds3aedded2017-03-31 14:42:09 -0400131 * what settings should be shown in the target app.
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500132 */
133 public static final String EXTRA_CHANNEL_ID = "android.intent.extra.CHANNEL_ID";
134
135 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -0400136 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds005c8b92017-08-24 10:35:53 -0400137 * contain a {@link NotificationChannelGroup#getId() group id} that can be used to narrow down
138 * what settings should be shown in the target app.
139 */
140 public static final String EXTRA_CHANNEL_GROUP_ID = "android.intent.extra.CHANNEL_GROUP_ID";
141
142 /**
143 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds3aedded2017-03-31 14:42:09 -0400144 * contain the tag provided to {@link NotificationManager#notify(String, int, Notification)}
145 * that can be used to narrow down what settings should be shown in the target app.
146 */
147 public static final String EXTRA_NOTIFICATION_TAG = "android.intent.extra.NOTIFICATION_TAG";
148
149 /**
150 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
151 * contain the id provided to {@link NotificationManager#notify(String, int, Notification)}
152 * that can be used to narrow down what settings should be shown in the target app.
153 */
154 public static final String EXTRA_NOTIFICATION_ID = "android.intent.extra.NOTIFICATION_ID";
155
156 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 * Use all default values (where applicable).
158 */
159 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 /**
162 * Use the default notification sound. This will ignore any given
163 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500164 *
Chris Wren47c20a12014-06-18 17:27:29 -0400165 * <p>
166 * A notification that is noisy is more likely to be presented as a heads-up notification.
167 * </p>
168 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500170 */
171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 public static final int DEFAULT_SOUND = 1;
173
174 /**
175 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500176 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700177 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500178 *
Chris Wren47c20a12014-06-18 17:27:29 -0400179 * <p>
180 * A notification that vibrates is more likely to be presented as a heads-up notification.
181 * </p>
182 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500184 */
185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 /**
189 * Use the default notification lights. This will ignore the
190 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
191 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500192 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500194 */
195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200199 * Maximum length of CharSequences accepted by Builder and friends.
200 *
201 * <p>
202 * Avoids spamming the system with overly large strings such as full e-mails.
203 */
204 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
205
206 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800207 * Maximum entries of reply text that are accepted by Builder and friends.
208 */
209 private static final int MAX_REPLY_HISTORY = 5;
210
Tony Mak09db2ea2018-06-27 18:12:48 +0100211 /**
Gustav Sennton1463d832018-11-06 16:12:48 +0000212 * Maximum number of (generic) action buttons in a notification (contextual action buttons are
213 * handled separately).
Tony Mak09db2ea2018-06-27 18:12:48 +0100214 * @hide
215 */
216 public static final int MAX_ACTION_BUTTONS = 3;
Selim Cinekde4de0e2018-01-24 16:21:07 -0800217
218 /**
219 * If the notification contained an unsent draft for a RemoteInput when the user clicked on it,
220 * we're adding the draft as a String extra to the {@link #contentIntent} using this key.
221 *
222 * <p>Apps may use this extra to prepopulate text fields in the app, where the user usually
223 * sends messages.</p>
224 */
225 public static final String EXTRA_REMOTE_INPUT_DRAFT = "android.remoteInputDraft";
226
Adrian Roose458aa82015-12-08 16:17:19 -0800227 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500228 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800229 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500230 * Default value: {@link System#currentTimeMillis() Now}.
231 *
232 * Choose a timestamp that will be most relevant to the user. For most finite events, this
233 * corresponds to the time the event happened (or will happen, in the case of events that have
234 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800235 * timestamped according to when the activity began.
236 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500237 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800238 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500239 * <ul>
240 * <li>Notification of a new chat message should be stamped when the message was received.</li>
241 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
242 * <li>Notification of a completed file download should be stamped when the download finished.</li>
243 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
244 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
245 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800246 * </ul>
247 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700248 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
249 * anymore by default and must be opted into by using
250 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 */
252 public long when;
253
254 /**
Selim Cinekb85f36fd2016-04-20 18:46:36 -0700255 * The creation time of the notification
256 */
257 private long creationTime;
258
259 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400261 *
262 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 */
Dan Sandler86647982015-05-13 23:41:13 -0400264 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700265 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 public int icon;
267
268 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800269 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
270 * leave it at its default value of 0.
271 *
272 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700273 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800274 */
275 public int iconLevel;
276
277 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500278 * The number of events that this notification represents. For example, in a new mail
279 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800280 *
Julia Reynolds30331982017-04-27 10:12:50 -0400281 * The system may or may not use this field to modify the appearance of the notification.
Julia Reynolds13d898c2017-02-02 12:22:05 -0500282 * Starting with {@link android.os.Build.VERSION_CODES#O}, the number may be displayed as a
283 * badge icon in Launchers that support badging.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 */
Julia Reynolds30331982017-04-27 10:12:50 -0400285 public int number = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286
287 /**
288 * The intent to execute when the expanded status entry is clicked. If
289 * this is an activity, it must include the
290 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800291 * that you take care of task management as described in the
292 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800293 * Stack</a> document. In particular, make sure to read the notification section
294 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
295 * Notifications</a> for the correct ways to launch an application from a
296 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 */
298 public PendingIntent contentIntent;
299
300 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500301 * The intent to execute when the notification is explicitly dismissed by the user, either with
302 * the "Clear All" button or by swiping it away individually.
303 *
304 * This probably shouldn't be launching an activity since several of those will be sent
305 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 */
307 public PendingIntent deleteIntent;
308
309 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700310 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800311 *
Chris Wren47c20a12014-06-18 17:27:29 -0400312 * <p>
313 * The system UI may choose to display a heads-up notification, instead of
314 * launching this intent, while the user is using the device.
315 * </p>
316 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800317 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400318 */
319 public PendingIntent fullScreenIntent;
320
321 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400322 * Text that summarizes this notification for accessibility services.
323 *
324 * As of the L release, this text is no longer shown on screen, but it is still useful to
325 * accessibility services (where it serves as an audible announcement of the notification's
326 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400327 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800328 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 */
330 public CharSequence tickerText;
331
332 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400333 * Formerly, a view showing the {@link #tickerText}.
334 *
335 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400336 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400337 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800338 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400339
340 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400341 * The view that will represent this notification in the notification list (which is pulled
342 * down from the status bar).
343 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500344 * As of N, this field may be null. The notification view is determined by the inputs
345 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400346 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400348 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 public RemoteViews contentView;
350
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400351 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400352 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400353 * opportunity to show more detail. The system UI may choose to show this
354 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400355 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500356 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400357 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
358 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400359 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400360 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400361 public RemoteViews bigContentView;
362
Chris Wren8fd39ec2014-02-27 17:43:26 -0500363
364 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400365 * A medium-format version of {@link #contentView}, providing the Notification an
366 * opportunity to add action buttons to contentView. At its discretion, the system UI may
367 * choose to show this as a heads-up notification, which will pop up so the user can see
368 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400369 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500370 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400371 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
372 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500373 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400374 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500375 public RemoteViews headsUpContentView;
376
Julia Reynoldsfc640012018-02-21 12:25:27 -0500377 private boolean mUsesStandardHeader;
378
379 private static final ArraySet<Integer> STANDARD_LAYOUTS = new ArraySet<>();
380 static {
381 STANDARD_LAYOUTS.add(R.layout.notification_template_material_base);
382 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_base);
383 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_picture);
384 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_text);
385 STANDARD_LAYOUTS.add(R.layout.notification_template_material_inbox);
386 STANDARD_LAYOUTS.add(R.layout.notification_template_material_messaging);
387 STANDARD_LAYOUTS.add(R.layout.notification_template_material_media);
388 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_media);
Julia Reynoldsfc640012018-02-21 12:25:27 -0500389 STANDARD_LAYOUTS.add(R.layout.notification_template_header);
Julia Reynoldsfc640012018-02-21 12:25:27 -0500390 }
391
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400392 /**
Dan Sandler86647982015-05-13 23:41:13 -0400393 * A large bitmap to be shown in the notification content area.
394 *
395 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 */
Dan Sandler86647982015-05-13 23:41:13 -0400397 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800398 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399
400 /**
401 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500402 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400404 * A notification that is noisy is more likely to be presented as a heads-up notification.
405 * </p>
406 *
407 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500408 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500410 * @deprecated use {@link NotificationChannel#getSound()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500412 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 public Uri sound;
414
415 /**
416 * Use this constant as the value for audioStreamType to request that
417 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700418 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400419 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500420 * @deprecated Use {@link NotificationChannel#getAudioAttributes()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700422 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 public static final int STREAM_DEFAULT = -1;
424
425 /**
426 * The audio stream type to use when playing the sound.
427 * Should be one of the STREAM_ constants from
428 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400429 *
430 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700432 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 public int audioStreamType = STREAM_DEFAULT;
434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400436 * The default value of {@link #audioAttributes}.
437 */
438 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
439 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
440 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
441 .build();
442
443 /**
444 * The {@link AudioAttributes audio attributes} to use when playing the sound.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500445 *
446 * @deprecated use {@link NotificationChannel#getAudioAttributes()} instead.
John Spurlockc0650f022014-07-19 13:22:39 -0400447 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500448 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -0400449 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
450
451 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500452 * The pattern with which to vibrate.
453 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 * <p>
455 * To vibrate the default pattern, see {@link #defaults}.
456 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500457 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 * @see android.os.Vibrator#vibrate(long[],int)
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500459 * @deprecated use {@link NotificationChannel#getVibrationPattern()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500461 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 public long[] vibrate;
463
464 /**
465 * The color of the led. The hardware will do its best approximation.
466 *
467 * @see #FLAG_SHOW_LIGHTS
468 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500469 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 */
Tor Norbye80756e32015-03-02 09:39:27 -0800471 @ColorInt
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500472 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 public int ledARGB;
474
475 /**
476 * The number of milliseconds for the LED to be on while it's flashing.
477 * The hardware will do its best approximation.
478 *
479 * @see #FLAG_SHOW_LIGHTS
480 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500481 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500483 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 public int ledOnMS;
485
486 /**
487 * The number of milliseconds for the LED to be off while it's flashing.
488 * The hardware will do its best approximation.
489 *
490 * @see #FLAG_SHOW_LIGHTS
491 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500492 *
493 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500495 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 public int ledOffMS;
497
498 /**
499 * Specifies which values should be taken from the defaults.
500 * <p>
501 * To set, OR the desired from {@link #DEFAULT_SOUND},
502 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
503 * values, use {@link #DEFAULT_ALL}.
504 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500505 *
506 * @deprecated use {@link NotificationChannel#getSound()} and
507 * {@link NotificationChannel#shouldShowLights()} and
508 * {@link NotificationChannel#shouldVibrate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500510 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 public int defaults;
512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 /**
514 * Bit to be bitwise-ored into the {@link #flags} field that should be
515 * set if you want the LED on for this notification.
516 * <ul>
517 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
518 * or 0 for both ledOnMS and ledOffMS.</li>
519 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
520 * <li>To flash the LED, pass the number of milliseconds that it should
521 * be on and off to ledOnMS and ledOffMS.</li>
522 * </ul>
523 * <p>
524 * Since hardware varies, you are not guaranteed that any of the values
Ricardo Loo Forondaf8f6d0a2018-01-25 08:42:43 -0800525 * you pass are honored exactly. Use the system defaults if possible
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 * because they will be set to values that work on any given hardware.
527 * <p>
528 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500529 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500530 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500532 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
534
535 /**
536 * Bit to be bitwise-ored into the {@link #flags} field that should be
537 * set if this notification is in reference to something that is ongoing,
538 * like a phone call. It should not be set if this notification is in
539 * reference to something that happened at a particular point in time,
540 * like a missed phone call.
541 */
542 public static final int FLAG_ONGOING_EVENT = 0x00000002;
543
544 /**
545 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700546 * the audio will be repeated until the notification is
547 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 */
549 public static final int FLAG_INSISTENT = 0x00000004;
550
551 /**
552 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700553 * set if you would only like the sound, vibrate and ticker to be played
554 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 */
556 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
557
558 /**
559 * Bit to be bitwise-ored into the {@link #flags} field that should be
560 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500561 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 */
563 public static final int FLAG_AUTO_CANCEL = 0x00000010;
564
565 /**
566 * Bit to be bitwise-ored into the {@link #flags} field that should be
567 * set if the notification should not be canceled when the user clicks
568 * the Clear all button.
569 */
570 public static final int FLAG_NO_CLEAR = 0x00000020;
571
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700572 /**
573 * Bit to be bitwise-ored into the {@link #flags} field that should be
574 * set if this notification represents a currently running service. This
575 * will normally be set for you by {@link Service#startForeground}.
576 */
577 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
578
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400579 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500580 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800581 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500582 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400583 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700584 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500585 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400586
Griff Hazendfcb0802014-02-11 12:00:00 -0800587 /**
588 * Bit to be bitswise-ored into the {@link #flags} field that should be
589 * set if this notification is relevant to the current device only
590 * and it is not recommended that it bridge to other devices.
591 */
592 public static final int FLAG_LOCAL_ONLY = 0x00000100;
593
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700594 /**
595 * Bit to be bitswise-ored into the {@link #flags} field that should be
596 * set if this notification is the group summary for a group of notifications.
597 * Grouped notifications may display in a cluster or stack on devices which
598 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
599 */
600 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
601
Julia Reynoldse46bb372016-03-17 11:05:58 -0400602 /**
603 * Bit to be bitswise-ored into the {@link #flags} field that should be
604 * set if this notification is the group summary for an auto-group of notifications.
605 *
606 * @hide
607 */
608 @SystemApi
609 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
610
Julia Reynolds4db59552017-06-30 13:34:01 -0400611 /**
612 * @hide
613 */
614 public static final int FLAG_CAN_COLORIZE = 0x00000800;
615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 public int flags;
617
Tor Norbyed9273d62013-05-30 15:59:53 -0700618 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700619 @IntDef(prefix = { "PRIORITY_" }, value = {
620 PRIORITY_DEFAULT,
621 PRIORITY_LOW,
622 PRIORITY_MIN,
623 PRIORITY_HIGH,
624 PRIORITY_MAX
625 })
Tor Norbyed9273d62013-05-30 15:59:53 -0700626 @Retention(RetentionPolicy.SOURCE)
627 public @interface Priority {}
628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500630 * Default notification {@link #priority}. If your application does not prioritize its own
631 * notifications, use this value for all notifications.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500632 *
633 * @deprecated use {@link NotificationManager#IMPORTANCE_DEFAULT} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500634 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500635 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500636 public static final int PRIORITY_DEFAULT = 0;
637
638 /**
639 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
640 * items smaller, or at a different position in the list, compared with your app's
641 * {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500642 *
643 * @deprecated use {@link NotificationManager#IMPORTANCE_LOW} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500644 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500645 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500646 public static final int PRIORITY_LOW = -1;
647
648 /**
649 * Lowest {@link #priority}; these items might not be shown to the user except under special
650 * circumstances, such as detailed notification logs.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500651 *
652 * @deprecated use {@link NotificationManager#IMPORTANCE_MIN} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500653 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500654 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500655 public static final int PRIORITY_MIN = -2;
656
657 /**
658 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
659 * show these items larger, or at a different position in notification lists, compared with
660 * your app's {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500661 *
662 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500663 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500664 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500665 public static final int PRIORITY_HIGH = 1;
666
667 /**
668 * Highest {@link #priority}, for your application's most important items that require the
669 * user's prompt attention or input.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500670 *
671 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500672 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500673 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500674 public static final int PRIORITY_MAX = 2;
675
676 /**
677 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800678 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500679 * Priority is an indication of how much of the user's valuable attention should be consumed by
680 * this notification. Low-priority notifications may be hidden from the user in certain
681 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500682 * system will make a determination about how to interpret this priority when presenting
683 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400684 *
685 * <p>
686 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
687 * as a heads-up notification.
688 * </p>
689 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500690 * @deprecated use {@link NotificationChannel#getImportance()} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500691 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700692 @Priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500693 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500694 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800695
Dan Sandler26e81cf2014-05-06 10:01:27 -0400696 /**
697 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
698 * to be applied by the standard Style templates when presenting this notification.
699 *
700 * The current template design constructs a colorful header image by overlaying the
701 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
702 * ignored.
703 */
Tor Norbye80756e32015-03-02 09:39:27 -0800704 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400705 public int color = COLOR_DEFAULT;
706
707 /**
708 * Special value of {@link #color} telling the system not to decorate this notification with
709 * any special color but instead use default colors when presenting this notification.
710 */
Tor Norbye80756e32015-03-02 09:39:27 -0800711 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400712 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600713
714 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800715 * Special value of {@link #color} used as a place holder for an invalid color.
Selim Cinek99104832017-01-25 14:47:33 -0800716 * @hide
Adrian Roos4ff3b122016-02-01 12:26:13 -0800717 */
718 @ColorInt
Selim Cinek99104832017-01-25 14:47:33 -0800719 public static final int COLOR_INVALID = 1;
Adrian Roos4ff3b122016-02-01 12:26:13 -0800720
721 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700722 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
723 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600724 * lockscreen).
725 *
726 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
727 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
728 * shown in all situations, but the contents are only available if the device is unlocked for
729 * the appropriate user.
730 *
731 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
732 * can be read even in an "insecure" context (that is, above a secure lockscreen).
733 * To modify the public version of this notification—for example, to redact some portions—see
734 * {@link Builder#setPublicVersion(Notification)}.
735 *
736 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
737 * and ticker until the user has bypassed the lockscreen.
738 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600739 public @Visibility int visibility;
740
741 /** @hide */
742 @IntDef(prefix = { "VISIBILITY_" }, value = {
743 VISIBILITY_PUBLIC,
744 VISIBILITY_PRIVATE,
745 VISIBILITY_SECRET,
746 })
747 @Retention(RetentionPolicy.SOURCE)
748 public @interface Visibility {}
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600749
Griff Hazenfc3922d2014-08-20 11:56:44 -0700750 /**
751 * Notification visibility: Show this notification in its entirety on all lockscreens.
752 *
753 * {@see #visibility}
754 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600755 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700756
757 /**
758 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
759 * private information on secure lockscreens.
760 *
761 * {@see #visibility}
762 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600763 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700764
765 /**
766 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
767 *
768 * {@see #visibility}
769 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600770 public static final int VISIBILITY_SECRET = -1;
771
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500772 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400773 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500774 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400775 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500776
777 /**
Adora Zhangaa90e872018-03-12 14:07:50 -0700778 * Notification category: map turn-by-turn navigation.
779 */
780 public static final String CATEGORY_NAVIGATION = "navigation";
781
782 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400783 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500784 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400785 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500786
787 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400788 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500789 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400790 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500791
792 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400793 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500794 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400795 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500796
797 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400798 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500799 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400800 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500801
802 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400803 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500804 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400805 public static final String CATEGORY_ALARM = "alarm";
806
807 /**
808 * Notification category: progress of a long-running background operation.
809 */
810 public static final String CATEGORY_PROGRESS = "progress";
811
812 /**
813 * Notification category: social network or sharing update.
814 */
815 public static final String CATEGORY_SOCIAL = "social";
816
817 /**
818 * Notification category: error in background operation or authentication status.
819 */
820 public static final String CATEGORY_ERROR = "err";
821
822 /**
823 * Notification category: media transport control for playback.
824 */
825 public static final String CATEGORY_TRANSPORT = "transport";
826
827 /**
828 * Notification category: system or device status update. Reserved for system use.
829 */
830 public static final String CATEGORY_SYSTEM = "sys";
831
832 /**
833 * Notification category: indication of running background service.
834 */
835 public static final String CATEGORY_SERVICE = "service";
836
837 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400838 * Notification category: a specific, timely recommendation for a single thing.
839 * For example, a news app might want to recommend a news story it believes the user will
840 * want to read next.
841 */
842 public static final String CATEGORY_RECOMMENDATION = "recommendation";
843
844 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400845 * Notification category: ongoing information about device or contextual status.
846 */
847 public static final String CATEGORY_STATUS = "status";
848
849 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400850 * Notification category: user-scheduled reminder.
851 */
852 public static final String CATEGORY_REMINDER = "reminder";
853
854 /**
Adora Zhangaa90e872018-03-12 14:07:50 -0700855 * Notification category: extreme car emergencies.
856 * @hide
857 */
858 @SystemApi
859 public static final String CATEGORY_CAR_EMERGENCY = "car_emergency";
860
861 /**
862 * Notification category: car warnings.
863 * @hide
864 */
865 @SystemApi
866 public static final String CATEGORY_CAR_WARNING = "car_warning";
867
868 /**
869 * Notification category: general car system information.
870 * @hide
871 */
872 @SystemApi
873 public static final String CATEGORY_CAR_INFORMATION = "car_information";
874
875 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400876 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
877 * that best describes this Notification. May be used by the system for ranking and filtering.
878 */
879 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500880
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100881 @UnsupportedAppUsage
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700882 private String mGroupKey;
883
884 /**
885 * Get the key used to group this notification into a cluster or stack
886 * with other notifications on devices which support such rendering.
887 */
888 public String getGroup() {
889 return mGroupKey;
890 }
891
892 private String mSortKey;
893
894 /**
895 * Get a sort key that orders this notification among other notifications from the
896 * same package. This can be useful if an external sort was already applied and an app
897 * would like to preserve this. Notifications will be sorted lexicographically using this
898 * value, although providing different priorities in addition to providing sort key may
899 * cause this value to be ignored.
900 *
901 * <p>This sort key can also be used to order members of a notification group. See
902 * {@link Builder#setGroup}.
903 *
904 * @see String#compareTo(String)
905 */
906 public String getSortKey() {
907 return mSortKey;
908 }
909
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500910 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400911 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400912 * <p>
913 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
914 * APIs, and are intended to be used by
915 * {@link android.service.notification.NotificationListenerService} implementations to extract
916 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500917 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400918 public Bundle extras = new Bundle();
919
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400920 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700921 * All pending intents in the notification as the system needs to be able to access them but
922 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700923 * custom parcelable objects.
924 *
925 * @hide
926 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100927 @UnsupportedAppUsage
Felipe Lemedd85da62016-06-28 11:29:54 -0700928 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700929
930 /**
Dianne Hackborn98305522017-05-05 17:53:53 -0700931 * Token identifying the notification that is applying doze/bgcheck whitelisting to the
932 * pending intents inside of it, so only those will get the behavior.
933 *
934 * @hide
935 */
Adrian Roosfb921842017-10-26 14:49:56 +0200936 private IBinder mWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -0700937
938 /**
939 * Must be set by a process to start associating tokens with Notification objects
940 * coming in to it. This is set by NotificationManagerService.
941 *
942 * @hide
943 */
944 static public IBinder processWhitelistToken;
945
946 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400947 * {@link #extras} key: this is the title of the notification,
948 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
949 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500950 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400951
952 /**
953 * {@link #extras} key: this is the title of the notification when shown in expanded form,
954 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
955 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400956 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400957
958 /**
959 * {@link #extras} key: this is the main text payload, as supplied to
960 * {@link Builder#setContentText(CharSequence)}.
961 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500962 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400963
964 /**
965 * {@link #extras} key: this is a third line of text, as supplied to
966 * {@link Builder#setSubText(CharSequence)}.
967 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400968 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400969
970 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800971 * {@link #extras} key: this is the remote input history, as supplied to
972 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700973 *
974 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
975 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
976 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
977 * notifications once the other party has responded).
978 *
979 * The extra with this key is of type CharSequence[] and contains the most recent entry at
980 * the 0 index, the second most recent at the 1 index, etc.
981 *
982 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800983 */
984 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
985
986 /**
Kenny Guya0f6de82018-04-06 16:20:16 +0100987 * {@link #extras} key: boolean as supplied to
988 * {@link Builder#setShowRemoteInputSpinner(boolean)}.
989 *
990 * If set to true, then the view displaying the remote input history from
991 * {@link Builder#setRemoteInputHistory(CharSequence[])} will have a progress spinner.
992 *
993 * @see Builder#setShowRemoteInputSpinner(boolean)
994 * @hide
995 */
996 public static final String EXTRA_SHOW_REMOTE_INPUT_SPINNER = "android.remoteInputSpinner";
997
998 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +0100999 * {@link #extras} key: boolean as supplied to
1000 * {@link Builder#setHideSmartReplies(boolean)}.
1001 *
1002 * If set to true, then any smart reply buttons will be hidden.
1003 *
1004 * @see Builder#setHideSmartReplies(boolean)
1005 * @hide
1006 */
1007 public static final String EXTRA_HIDE_SMART_REPLIES = "android.hideSmartReplies";
1008
1009 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001010 * {@link #extras} key: this is a small piece of additional text as supplied to
1011 * {@link Builder#setContentInfo(CharSequence)}.
1012 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001013 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001014
1015 /**
1016 * {@link #extras} key: this is a line of summary information intended to be shown
1017 * alongside expanded notifications, as supplied to (e.g.)
1018 * {@link BigTextStyle#setSummaryText(CharSequence)}.
1019 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001020 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001021
1022 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02001023 * {@link #extras} key: this is the longer text shown in the big form of a
1024 * {@link BigTextStyle} notification, as supplied to
1025 * {@link BigTextStyle#bigText(CharSequence)}.
1026 */
1027 public static final String EXTRA_BIG_TEXT = "android.bigText";
1028
1029 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001030 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
1031 * supplied to {@link Builder#setSmallIcon(int)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -04001032 *
1033 * @deprecated Use {@link #getSmallIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001034 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04001035 @Deprecated
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001036 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001037
1038 /**
1039 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
1040 * notification payload, as
1041 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -04001042 *
1043 * @deprecated Use {@link #getLargeIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001044 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04001045 @Deprecated
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001046 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001047
1048 /**
1049 * {@link #extras} key: this is a bitmap to be used instead of the one from
1050 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
1051 * shown in its expanded form, as supplied to
1052 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
1053 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001054 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001055
1056 /**
1057 * {@link #extras} key: this is the progress value supplied to
1058 * {@link Builder#setProgress(int, int, boolean)}.
1059 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001060 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001061
1062 /**
1063 * {@link #extras} key: this is the maximum value supplied to
1064 * {@link Builder#setProgress(int, int, boolean)}.
1065 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001066 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001067
1068 /**
1069 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
1070 * {@link Builder#setProgress(int, int, boolean)}.
1071 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001072 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001073
1074 /**
1075 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
1076 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
1077 * {@link Builder#setUsesChronometer(boolean)}.
1078 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001079 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001080
1081 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08001082 * {@link #extras} key: whether the chronometer set on the notification should count down
1083 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -07001084 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -08001085 */
Adrian Roos96b7e202016-05-17 13:50:38 -07001086 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -08001087
1088 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001089 * {@link #extras} key: whether {@link #when} should be shown,
1090 * as supplied to {@link Builder#setShowWhen(boolean)}.
1091 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001092 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001093
1094 /**
1095 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
1096 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
1097 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001098 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001099
1100 /**
1101 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
1102 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
1103 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001104 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -04001105
1106 /**
1107 * {@link #extras} key: A string representing the name of the specific
1108 * {@link android.app.Notification.Style} used to create this notification.
1109 */
Chris Wren91ad5632013-06-05 15:05:57 -04001110 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001111
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001112 /**
Chris Wrene6c48932014-09-29 17:19:27 -04001113 * {@link #extras} key: A String array containing the people that this notification relates to,
1114 * each of which was supplied to {@link Builder#addPerson(String)}.
Selim Cineke7238dd2017-12-14 17:48:32 -08001115 *
1116 * @deprecated the actual objects are now in {@link #EXTRA_PEOPLE_LIST}
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001117 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001118 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001119
1120 /**
Selim Cineke7238dd2017-12-14 17:48:32 -08001121 * {@link #extras} key: An arrayList of {@link Person} objects containing the people that
1122 * this notification relates to.
1123 */
1124 public static final String EXTRA_PEOPLE_LIST = "android.people.list";
1125
1126 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04001127 * Allow certain system-generated notifications to appear before the device is provisioned.
1128 * Only available to notifications coming from the android package.
1129 * @hide
1130 */
Maurice Lam96c10032017-03-29 15:42:38 -07001131 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001132 @RequiresPermission(android.Manifest.permission.NOTIFICATION_DURING_SETUP)
John Spurlockfd7f1e02014-03-18 16:41:57 -04001133 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
1134
1135 /**
Robin Leed107af62018-04-27 13:55:56 +02001136 * {@link #extras} key:
1137 * flat {@link String} representation of a {@link android.content.ContentUris content URI}
1138 * pointing to an image that can be displayed in the background when the notification is
1139 * selected. Used on television platforms. The URI must point to an image stream suitable for
1140 * passing into {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001141 * BitmapFactory.decodeStream}; all other content types will be ignored.
Jose Limae9e3b3b2014-05-18 23:44:50 -07001142 */
1143 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
1144
1145 /**
Dan Sandler842dd772014-05-15 09:36:47 -04001146 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -07001147 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -04001148 * {@link android.app.Notification.MediaStyle} notification.
1149 */
1150 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
1151
1152 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +01001153 * {@link #extras} key: the indices of actions to be shown in the compact view,
1154 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
1155 */
1156 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
1157
Christoph Studer943aa672014-08-03 20:31:16 +02001158 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04001159 * {@link #extras} key: the username to be displayed for all messages sent by the user including
1160 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -07001161 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1162 * {@link CharSequence}
Selim Cinekcb8b9852017-12-15 18:01:52 -08001163 *
1164 * @deprecated use {@link #EXTRA_MESSAGING_PERSON}
Alex Hillsfc737de2016-03-23 17:33:02 -04001165 */
1166 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
1167
1168 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08001169 * {@link #extras} key: the person to be displayed for all messages sent by the user including
1170 * direct replies
1171 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1172 * {@link Person}
1173 */
1174 public static final String EXTRA_MESSAGING_PERSON = "android.messagingUser";
1175
1176 /**
Adrian Roos96b7e202016-05-17 13:50:38 -07001177 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -04001178 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -04001179 */
Alex Hillsd9b04d92016-04-11 16:38:16 -04001180 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -04001181
1182 /**
1183 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
1184 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -07001185 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1186 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -04001187 */
1188 public static final String EXTRA_MESSAGES = "android.messages";
1189
1190 /**
Adrian Roos437cd562017-01-18 15:47:03 -08001191 * {@link #extras} key: an array of
1192 * {@link android.app.Notification.MessagingStyle#addHistoricMessage historic}
1193 * {@link android.app.Notification.MessagingStyle.Message} bundles provided by a
1194 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1195 * array of bundles.
1196 */
1197 public static final String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
1198
1199 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08001200 * {@link #extras} key: whether the {@link android.app.Notification.MessagingStyle} notification
1201 * represents a group conversation.
1202 */
1203 public static final String EXTRA_IS_GROUP_CONVERSATION = "android.isGroupConversation";
1204
1205 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08001206 * {@link #extras} key: whether the notification should be colorized as
Gustav Senntondf4c2442019-03-18 11:41:11 +00001207 * supplied to {@link Builder#setColorized(boolean)}.
Selim Cinek7b9605b2017-01-19 17:36:00 -08001208 */
1209 public static final String EXTRA_COLORIZED = "android.colorized";
1210
1211 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001212 * @hide
1213 */
1214 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
1215
Selim Cinek247fa012016-02-18 09:50:48 -08001216 /**
1217 * @hide
1218 */
1219 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
1220
Shane Brennan472a3b32016-12-12 15:28:10 -08001221 /**
Selim Cinekd0426622017-07-11 13:19:59 +02001222 * @hide
1223 */
1224 public static final String EXTRA_REDUCED_IMAGES = "android.reduced.images";
1225
1226 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001227 * {@link #extras} key: the audio contents of this notification.
1228 *
1229 * This is for use when rendering the notification on an audio-focused interface;
1230 * the audio contents are a complete sound sample that contains the contents/body of the
1231 * notification. This may be used in substitute of a Text-to-Speech reading of the
1232 * notification. For example if the notification represents a voice message this should point
1233 * to the audio of that message.
1234 *
1235 * The data stored under this key should be a String representation of a Uri that contains the
1236 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1237 *
1238 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1239 * has a field for holding data URI. That field can be used for audio.
1240 * See {@code Message#setData}.
1241 *
1242 * Example usage:
1243 * <pre>
1244 * {@code
1245 * Notification.Builder myBuilder = (build your Notification as normal);
1246 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1247 * }
1248 * </pre>
1249 */
1250 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1251
Dan Sandler80eaa592016-04-14 23:34:54 -04001252 /** @hide */
1253 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001254 @RequiresPermission(android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME)
Dan Sandler732bd6c2016-04-12 14:20:32 -04001255 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1256
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001257 /**
Julia Reynolds3c7de112018-03-28 09:33:13 -04001258 * This is set on the notifications shown by system_server about apps running foreground
1259 * services. It indicates that the notification should be shown
1260 * only if any of the given apps do not already have a properly tagged
1261 * {@link #FLAG_FOREGROUND_SERVICE} notification currently visible to the user.
1262 * This is a string array of all package names of the apps.
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001263 * @hide
1264 */
1265 public static final String EXTRA_FOREGROUND_APPS = "android.foregroundApps";
1266
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001267 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04001268 private Icon mSmallIcon;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001269 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04001270 private Icon mLargeIcon;
1271
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001272 @UnsupportedAppUsage
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001273 private String mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001274 private long mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001275
Julia Reynolds13d898c2017-02-02 12:22:05 -05001276 private String mShortcutId;
Felipe Leme90205ef2019-03-05 09:59:52 -08001277 private LocusId mLocusId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04001278 private CharSequence mSettingsText;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001279
Mady Mellorc39b4ae2019-01-09 17:11:37 -08001280 private BubbleMetadata mBubbleMetadata;
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04001281
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001282 /** @hide */
1283 @IntDef(prefix = { "GROUP_ALERT_" }, value = {
1284 GROUP_ALERT_ALL, GROUP_ALERT_CHILDREN, GROUP_ALERT_SUMMARY
1285 })
1286 @Retention(RetentionPolicy.SOURCE)
1287 public @interface GroupAlertBehavior {}
1288
1289 /**
1290 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all notifications in a
1291 * group with sound or vibration ought to make sound or vibrate (respectively), so this
1292 * notification will not be muted when it is in a group.
1293 */
1294 public static final int GROUP_ALERT_ALL = 0;
1295
1296 /**
1297 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all children
1298 * notification in a group should be silenced (no sound or vibration) even if they are posted
1299 * to a {@link NotificationChannel} that has sound and/or vibration. Use this constant to
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001300 * mute this notification if this notification is a group child. This must be applied to all
1301 * children notifications you want to mute.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001302 *
1303 * <p> For example, you might want to use this constant if you post a number of children
1304 * notifications at once (say, after a periodic sync), and only need to notify the user
1305 * audibly once.
1306 */
1307 public static final int GROUP_ALERT_SUMMARY = 1;
1308
1309 /**
1310 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that the summary
1311 * notification in a group should be silenced (no sound or vibration) even if they are
1312 * posted to a {@link NotificationChannel} that has sound and/or vibration. Use this constant
1313 * to mute this notification if this notification is a group summary.
1314 *
1315 * <p>For example, you might want to use this constant if only the children notifications
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001316 * in your group have content and the summary is only used to visually group notifications
1317 * rather than to alert the user that new information is available.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001318 */
1319 public static final int GROUP_ALERT_CHILDREN = 2;
1320
Julia Reynolds2f431e22017-06-07 14:12:09 +00001321 private int mGroupAlertBehavior = GROUP_ALERT_ALL;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001322
Julia Reynolds13d898c2017-02-02 12:22:05 -05001323 /**
1324 * If this notification is being shown as a badge, always show as a number.
1325 */
1326 public static final int BADGE_ICON_NONE = 0;
1327
1328 /**
1329 * If this notification is being shown as a badge, use the {@link #getSmallIcon()} to
1330 * represent this notification.
1331 */
1332 public static final int BADGE_ICON_SMALL = 1;
1333
1334 /**
1335 * If this notification is being shown as a badge, use the {@link #getLargeIcon()} to
1336 * represent this notification.
1337 */
1338 public static final int BADGE_ICON_LARGE = 2;
Julia Reynolds7d5b4da2017-03-20 10:18:49 -04001339 private int mBadgeIcon = BADGE_ICON_NONE;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001340
Chris Wren51c75102013-07-16 20:49:17 -04001341 /**
Gustav Sennton761884c2018-11-19 17:40:19 +00001342 * Determines whether the platform can generate contextual actions for a notification.
1343 */
1344 private boolean mAllowSystemGeneratedContextualActions = true;
1345
1346 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001347 * Structure to encapsulate a named action that can be shown as part of this notification.
1348 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1349 * selected by the user.
1350 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001351 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1352 * or {@link Notification.Builder#addAction(Notification.Action)}
1353 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001354 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001355 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001356 /**
1357 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1358 * {@link RemoteInput}s.
1359 *
1360 * This is intended for {@link RemoteInput}s that only accept data, meaning
1361 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
Gustav Senntondf4c2442019-03-18 11:41:11 +00001362 * is null or empty, and {@link RemoteInput#getAllowedDataTypes} is non-null and not
Shane Brennan472a3b32016-12-12 15:28:10 -08001363 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1364 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1365 *
1366 * You can test if a RemoteInput matches these constraints using
1367 * {@link RemoteInput#isDataOnly}.
1368 */
1369 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1370
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001371 /**
1372 * {@link }: No semantic action defined.
1373 */
1374 public static final int SEMANTIC_ACTION_NONE = 0;
1375
1376 /**
1377 * {@code SemanticAction}: Reply to a conversation, chat, group, or wherever replies
1378 * may be appropriate.
1379 */
1380 public static final int SEMANTIC_ACTION_REPLY = 1;
1381
1382 /**
1383 * {@code SemanticAction}: Mark content as read.
1384 */
1385 public static final int SEMANTIC_ACTION_MARK_AS_READ = 2;
1386
1387 /**
1388 * {@code SemanticAction}: Mark content as unread.
1389 */
1390 public static final int SEMANTIC_ACTION_MARK_AS_UNREAD = 3;
1391
1392 /**
1393 * {@code SemanticAction}: Delete the content associated with the notification. This
1394 * could mean deleting an email, message, etc.
1395 */
1396 public static final int SEMANTIC_ACTION_DELETE = 4;
1397
1398 /**
1399 * {@code SemanticAction}: Archive the content associated with the notification. This
1400 * could mean archiving an email, message, etc.
1401 */
1402 public static final int SEMANTIC_ACTION_ARCHIVE = 5;
1403
1404 /**
1405 * {@code SemanticAction}: Mute the content associated with the notification. This could
1406 * mean silencing a conversation or currently playing media.
1407 */
1408 public static final int SEMANTIC_ACTION_MUTE = 6;
1409
1410 /**
1411 * {@code SemanticAction}: Unmute the content associated with the notification. This could
1412 * mean un-silencing a conversation or currently playing media.
1413 */
1414 public static final int SEMANTIC_ACTION_UNMUTE = 7;
1415
1416 /**
1417 * {@code SemanticAction}: Mark content with a thumbs up.
1418 */
1419 public static final int SEMANTIC_ACTION_THUMBS_UP = 8;
1420
1421 /**
1422 * {@code SemanticAction}: Mark content with a thumbs down.
1423 */
1424 public static final int SEMANTIC_ACTION_THUMBS_DOWN = 9;
1425
Kodlee Yincda5b092018-02-15 15:34:53 -08001426 /**
1427 * {@code SemanticAction}: Call a contact, group, etc.
1428 */
1429 public static final int SEMANTIC_ACTION_CALL = 10;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001430
Griff Hazen959591e2014-05-15 22:26:18 -07001431 private final Bundle mExtras;
Mathew Inwood8c854f82018-09-14 12:35:36 +01001432 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Dan Sandler86647982015-05-13 23:41:13 -04001433 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001434 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001435 private boolean mAllowGeneratedReplies = true;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001436 private final @SemanticAction int mSemanticAction;
Gustav Sennton005d7a02019-01-04 13:41:32 +00001437 private final boolean mIsContextual;
Griff Hazen959591e2014-05-15 22:26:18 -07001438
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001439 /**
1440 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001441 *
1442 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001443 */
Dan Sandler86647982015-05-13 23:41:13 -04001444 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001445 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001446
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001447 /**
1448 * Title of the action.
1449 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001450 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001451
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001452 /**
1453 * Intent to send when the user invokes this action. May be null, in which case the action
1454 * may be rendered in a disabled presentation by the system UI.
1455 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001456 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001457
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001458 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001459 if (in.readInt() != 0) {
1460 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001461 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1462 icon = mIcon.getResId();
1463 }
Dan Sandler86647982015-05-13 23:41:13 -04001464 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001465 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1466 if (in.readInt() == 1) {
1467 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1468 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001469 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001470 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001471 mAllowGeneratedReplies = in.readInt() == 1;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001472 mSemanticAction = in.readInt();
Gustav Sennton005d7a02019-01-04 13:41:32 +00001473 mIsContextual = in.readInt() == 1;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001474 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001475
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001476 /**
Dan Sandler86647982015-05-13 23:41:13 -04001477 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001478 */
Dan Sandler86647982015-05-13 23:41:13 -04001479 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001480 public Action(int icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001481 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true,
Gustav Sennton005d7a02019-01-04 13:41:32 +00001482 SEMANTIC_ACTION_NONE, false /* isContextual */);
Griff Hazen959591e2014-05-15 22:26:18 -07001483 }
1484
Adrian Roos7af53622016-10-12 13:44:05 -07001485 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001486 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001487 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
Gustav Sennton005d7a02019-01-04 13:41:32 +00001488 @SemanticAction int semanticAction, boolean isContextual) {
Dan Sandler86647982015-05-13 23:41:13 -04001489 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001490 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1491 this.icon = icon.getResId();
1492 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001493 this.title = title;
1494 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001495 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001496 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001497 this.mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001498 this.mSemanticAction = semanticAction;
Gustav Sennton005d7a02019-01-04 13:41:32 +00001499 this.mIsContextual = isContextual;
Griff Hazen959591e2014-05-15 22:26:18 -07001500 }
1501
1502 /**
Dan Sandler86647982015-05-13 23:41:13 -04001503 * Return an icon representing the action.
1504 */
1505 public Icon getIcon() {
1506 if (mIcon == null && icon != 0) {
1507 // you snuck an icon in here without using the builder; let's try to keep it
1508 mIcon = Icon.createWithResource("", icon);
1509 }
1510 return mIcon;
1511 }
1512
1513 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001514 * Get additional metadata carried around with this Action.
1515 */
1516 public Bundle getExtras() {
1517 return mExtras;
1518 }
1519
1520 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001521 * Return whether the platform should automatically generate possible replies for this
1522 * {@link Action}
1523 */
1524 public boolean getAllowGeneratedReplies() {
1525 return mAllowGeneratedReplies;
1526 }
1527
1528 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001529 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001530 * May return null if no remote inputs were added. Only returns inputs which accept
1531 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001532 */
1533 public RemoteInput[] getRemoteInputs() {
1534 return mRemoteInputs;
1535 }
1536
1537 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001538 * Returns the {@code SemanticAction} associated with this {@link Action}. A
1539 * {@code SemanticAction} denotes what an {@link Action}'s {@link PendingIntent} will do
1540 * (eg. reply, mark as read, delete, etc).
1541 */
1542 public @SemanticAction int getSemanticAction() {
1543 return mSemanticAction;
1544 }
1545
1546 /**
Gustav Sennton005d7a02019-01-04 13:41:32 +00001547 * Returns whether this is a contextual Action, i.e. whether the action is dependent on the
1548 * notification message body. An example of a contextual action could be an action opening a
1549 * map application with an address shown in the notification.
1550 */
1551 public boolean isContextual() {
1552 return mIsContextual;
1553 }
1554
1555 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001556 * Get the list of inputs to be collected from the user that ONLY accept data when this
1557 * action is sent. These remote inputs are guaranteed to return true on a call to
1558 * {@link RemoteInput#isDataOnly}.
1559 *
Shane Brennanf670d6c2017-04-25 13:05:45 -07001560 * Returns null if there are no data-only remote inputs.
Shane Brennan472a3b32016-12-12 15:28:10 -08001561 *
1562 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1563 * of non-textual RemoteInputs do not access these remote inputs.
1564 */
1565 public RemoteInput[] getDataOnlyRemoteInputs() {
1566 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1567 }
1568
1569 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001570 * Builder class for {@link Action} objects.
1571 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001572 public static final class Builder {
Gustav Senntone1fc87b2019-03-18 14:31:28 +00001573 @Nullable private final Icon mIcon;
1574 @Nullable private final CharSequence mTitle;
1575 @Nullable private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001576 private boolean mAllowGeneratedReplies = true;
Gustav Senntone1fc87b2019-03-18 14:31:28 +00001577 @NonNull private final Bundle mExtras;
1578 @Nullable private ArrayList<RemoteInput> mRemoteInputs;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001579 private @SemanticAction int mSemanticAction;
Gustav Sennton005d7a02019-01-04 13:41:32 +00001580 private boolean mIsContextual;
Griff Hazen959591e2014-05-15 22:26:18 -07001581
1582 /**
1583 * Construct a new builder for {@link Action} object.
1584 * @param icon icon to show for this action
1585 * @param title the title of the action
1586 * @param intent the {@link PendingIntent} to fire when users trigger this action
1587 */
Dan Sandler86647982015-05-13 23:41:13 -04001588 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001589 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001590 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001591 }
1592
1593 /**
1594 * Construct a new builder for {@link Action} object.
1595 * @param icon icon to show for this action
1596 * @param title the title of the action
1597 * @param intent the {@link PendingIntent} to fire when users trigger this action
1598 */
1599 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001600 this(icon, title, intent, new Bundle(), null, true, SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001601 }
1602
1603 /**
1604 * Construct a new builder for {@link Action} object using the fields from an
1605 * {@link Action}.
1606 * @param action the action to read fields from.
1607 */
1608 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001609 this(action.getIcon(), action.title, action.actionIntent,
1610 new Bundle(action.mExtras), action.getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001611 action.getAllowGeneratedReplies(), action.getSemanticAction());
Griff Hazen959591e2014-05-15 22:26:18 -07001612 }
1613
Gustav Senntone1fc87b2019-03-18 14:31:28 +00001614 private Builder(@Nullable Icon icon, @Nullable CharSequence title,
1615 @Nullable PendingIntent intent, @NonNull Bundle extras,
1616 @Nullable RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1617 @SemanticAction int semanticAction) {
Griff Hazen959591e2014-05-15 22:26:18 -07001618 mIcon = icon;
1619 mTitle = title;
1620 mIntent = intent;
1621 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001622 if (remoteInputs != null) {
1623 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1624 Collections.addAll(mRemoteInputs, remoteInputs);
1625 }
Adrian Roos7af53622016-10-12 13:44:05 -07001626 mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001627 mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001628 }
1629
1630 /**
1631 * Merge additional metadata into this builder.
1632 *
1633 * <p>Values within the Bundle will replace existing extras values in this Builder.
1634 *
1635 * @see Notification.Action#extras
1636 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001637 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07001638 public Builder addExtras(Bundle extras) {
1639 if (extras != null) {
1640 mExtras.putAll(extras);
1641 }
1642 return this;
1643 }
1644
1645 /**
1646 * Get the metadata Bundle used by this Builder.
1647 *
1648 * <p>The returned Bundle is shared with this Builder.
1649 */
Gustav Senntone1fc87b2019-03-18 14:31:28 +00001650 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07001651 public Bundle getExtras() {
1652 return mExtras;
1653 }
1654
1655 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001656 * Add an input to be collected from the user when this action is sent.
1657 * Response values can be retrieved from the fired intent by using the
1658 * {@link RemoteInput#getResultsFromIntent} function.
1659 * @param remoteInput a {@link RemoteInput} to add to the action
1660 * @return this object for method chaining
1661 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001662 @NonNull
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001663 public Builder addRemoteInput(RemoteInput remoteInput) {
1664 if (mRemoteInputs == null) {
1665 mRemoteInputs = new ArrayList<RemoteInput>();
1666 }
1667 mRemoteInputs.add(remoteInput);
1668 return this;
1669 }
1670
1671 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001672 * Set whether the platform should automatically generate possible replies to add to
1673 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1674 * {@link RemoteInput}, this has no effect.
1675 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1676 * otherwise
1677 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001678 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001679 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001680 @NonNull
Alex Hills42b0c4d2016-04-26 13:35:36 -04001681 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1682 mAllowGeneratedReplies = allowGeneratedReplies;
1683 return this;
1684 }
1685
1686 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001687 * Sets the {@code SemanticAction} for this {@link Action}. A
1688 * {@code SemanticAction} denotes what an {@link Action}'s
1689 * {@link PendingIntent} will do (eg. reply, mark as read, delete, etc).
1690 * @param semanticAction a SemanticAction defined within {@link Action} with
1691 * {@code SEMANTIC_ACTION_} prefixes
1692 * @return this object for method chaining
1693 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001694 @NonNull
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001695 public Builder setSemanticAction(@SemanticAction int semanticAction) {
1696 mSemanticAction = semanticAction;
1697 return this;
1698 }
1699
1700 /**
Gustav Sennton005d7a02019-01-04 13:41:32 +00001701 * Sets whether this {@link Action} is a contextual action, i.e. whether the action is
1702 * dependent on the notification message body. An example of a contextual action could
1703 * be an action opening a map application with an address shown in the notification.
1704 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001705 @NonNull
Gustav Sennton005d7a02019-01-04 13:41:32 +00001706 public Builder setContextual(boolean isContextual) {
1707 mIsContextual = isContextual;
1708 return this;
1709 }
1710
1711 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001712 * Apply an extender to this action builder. Extenders may be used to add
1713 * metadata or change options on this builder.
1714 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001715 @NonNull
Griff Hazen61a9e862014-05-22 16:05:19 -07001716 public Builder extend(Extender extender) {
1717 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001718 return this;
1719 }
1720
1721 /**
Gustav Senntonc98b1632018-11-23 12:26:15 +00001722 * Throws an NPE if we are building a contextual action missing one of the fields
1723 * necessary to display the action.
1724 */
1725 private void checkContextualActionNullFields() {
Gustav Sennton005d7a02019-01-04 13:41:32 +00001726 if (!mIsContextual) return;
Gustav Senntonc98b1632018-11-23 12:26:15 +00001727
1728 if (mIcon == null) {
1729 throw new NullPointerException("Contextual Actions must contain a valid icon");
1730 }
1731
1732 if (mIntent == null) {
1733 throw new NullPointerException(
1734 "Contextual Actions must contain a valid PendingIntent");
1735 }
1736 }
1737
1738 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001739 * Combine all of the options that have been set and return a new {@link Action}
1740 * object.
1741 * @return the built action
1742 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001743 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07001744 public Action build() {
Gustav Senntonc98b1632018-11-23 12:26:15 +00001745 checkContextualActionNullFields();
1746
Shane Brennan472a3b32016-12-12 15:28:10 -08001747 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1748 RemoteInput[] previousDataInputs =
1749 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001750 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001751 for (RemoteInput input : previousDataInputs) {
1752 dataOnlyInputs.add(input);
1753 }
1754 }
1755 List<RemoteInput> textInputs = new ArrayList<>();
1756 if (mRemoteInputs != null) {
1757 for (RemoteInput input : mRemoteInputs) {
1758 if (input.isDataOnly()) {
1759 dataOnlyInputs.add(input);
1760 } else {
1761 textInputs.add(input);
1762 }
1763 }
1764 }
1765 if (!dataOnlyInputs.isEmpty()) {
1766 RemoteInput[] dataInputsArr =
1767 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1768 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1769 }
1770 RemoteInput[] textInputsArr = textInputs.isEmpty()
1771 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1772 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Gustav Sennton005d7a02019-01-04 13:41:32 +00001773 mAllowGeneratedReplies, mSemanticAction, mIsContextual);
Griff Hazen959591e2014-05-15 22:26:18 -07001774 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001775 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001776
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001777 @Override
1778 public Action clone() {
1779 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001780 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001781 title,
1782 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001783 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001784 getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001785 getAllowGeneratedReplies(),
Gustav Sennton005d7a02019-01-04 13:41:32 +00001786 getSemanticAction(),
1787 isContextual());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001788 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001789
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001790 @Override
1791 public int describeContents() {
1792 return 0;
1793 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001794
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001795 @Override
1796 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001797 final Icon ic = getIcon();
1798 if (ic != null) {
1799 out.writeInt(1);
1800 ic.writeToParcel(out, 0);
1801 } else {
1802 out.writeInt(0);
1803 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001804 TextUtils.writeToParcel(title, out, flags);
1805 if (actionIntent != null) {
1806 out.writeInt(1);
1807 actionIntent.writeToParcel(out, flags);
1808 } else {
1809 out.writeInt(0);
1810 }
Griff Hazen959591e2014-05-15 22:26:18 -07001811 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001812 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001813 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001814 out.writeInt(mSemanticAction);
Gustav Sennton005d7a02019-01-04 13:41:32 +00001815 out.writeInt(mIsContextual ? 1 : 0);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001816 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001817
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07001818 public static final @android.annotation.NonNull Parcelable.Creator<Action> CREATOR =
Griff Hazen959591e2014-05-15 22:26:18 -07001819 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001820 public Action createFromParcel(Parcel in) {
1821 return new Action(in);
1822 }
1823 public Action[] newArray(int size) {
1824 return new Action[size];
1825 }
1826 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001827
1828 /**
1829 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1830 * metadata or change options on an action builder.
1831 */
1832 public interface Extender {
1833 /**
1834 * Apply this extender to a notification action builder.
1835 * @param builder the builder to be modified.
1836 * @return the build object for chaining.
1837 */
1838 public Builder extend(Builder builder);
1839 }
1840
1841 /**
1842 * Wearable extender for notification actions. To add extensions to an action,
1843 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1844 * the {@code WearableExtender()} constructor and apply it to a
1845 * {@link android.app.Notification.Action.Builder} using
1846 * {@link android.app.Notification.Action.Builder#extend}.
1847 *
1848 * <pre class="prettyprint">
1849 * Notification.Action action = new Notification.Action.Builder(
1850 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001851 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001852 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001853 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001854 */
1855 public static final class WearableExtender implements Extender {
1856 /** Notification action extra which contains wearable extensions */
1857 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1858
Pete Gastaf6781d2014-10-07 15:17:05 -04001859 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001860 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001861 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1862 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1863 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001864
1865 // Flags bitwise-ored to mFlags
1866 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001867 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001868 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001869
1870 // Default value for flags integer
1871 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1872
1873 private int mFlags = DEFAULT_FLAGS;
1874
Pete Gastaf6781d2014-10-07 15:17:05 -04001875 private CharSequence mInProgressLabel;
1876 private CharSequence mConfirmLabel;
1877 private CharSequence mCancelLabel;
1878
Griff Hazen61a9e862014-05-22 16:05:19 -07001879 /**
1880 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1881 * options.
1882 */
1883 public WearableExtender() {
1884 }
1885
1886 /**
1887 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1888 * wearable options present in an existing notification action.
1889 * @param action the notification action to inspect.
1890 */
1891 public WearableExtender(Action action) {
1892 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1893 if (wearableBundle != null) {
1894 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001895 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1896 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1897 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001898 }
1899 }
1900
1901 /**
1902 * Apply wearable extensions to a notification action that is being built. This is
1903 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1904 * method of {@link android.app.Notification.Action.Builder}.
1905 */
1906 @Override
1907 public Action.Builder extend(Action.Builder builder) {
1908 Bundle wearableBundle = new Bundle();
1909
1910 if (mFlags != DEFAULT_FLAGS) {
1911 wearableBundle.putInt(KEY_FLAGS, mFlags);
1912 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001913 if (mInProgressLabel != null) {
1914 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1915 }
1916 if (mConfirmLabel != null) {
1917 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1918 }
1919 if (mCancelLabel != null) {
1920 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1921 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001922
1923 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1924 return builder;
1925 }
1926
1927 @Override
1928 public WearableExtender clone() {
1929 WearableExtender that = new WearableExtender();
1930 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001931 that.mInProgressLabel = this.mInProgressLabel;
1932 that.mConfirmLabel = this.mConfirmLabel;
1933 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001934 return that;
1935 }
1936
1937 /**
1938 * Set whether this action is available when the wearable device is not connected to
1939 * a companion device. The user can still trigger this action when the wearable device is
1940 * offline, but a visual hint will indicate that the action may not be available.
1941 * Defaults to true.
1942 */
1943 public WearableExtender setAvailableOffline(boolean availableOffline) {
1944 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1945 return this;
1946 }
1947
1948 /**
1949 * Get whether this action is available when the wearable device is not connected to
1950 * a companion device. The user can still trigger this action when the wearable device is
1951 * offline, but a visual hint will indicate that the action may not be available.
1952 * Defaults to true.
1953 */
1954 public boolean isAvailableOffline() {
1955 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1956 }
1957
1958 private void setFlag(int mask, boolean value) {
1959 if (value) {
1960 mFlags |= mask;
1961 } else {
1962 mFlags &= ~mask;
1963 }
1964 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001965
1966 /**
1967 * Set a label to display while the wearable is preparing to automatically execute the
1968 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1969 *
1970 * @param label the label to display while the action is being prepared to execute
1971 * @return this object for method chaining
1972 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001973 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001974 public WearableExtender setInProgressLabel(CharSequence label) {
1975 mInProgressLabel = label;
1976 return this;
1977 }
1978
1979 /**
1980 * Get the label to display while the wearable is preparing to automatically execute
1981 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1982 *
1983 * @return the label to display while the action is being prepared to execute
1984 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001985 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001986 public CharSequence getInProgressLabel() {
1987 return mInProgressLabel;
1988 }
1989
1990 /**
1991 * Set a label to display to confirm that the action should be executed.
1992 * This is usually an imperative verb like "Send".
1993 *
1994 * @param label the label to confirm the action should be executed
1995 * @return this object for method chaining
1996 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001997 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001998 public WearableExtender setConfirmLabel(CharSequence label) {
1999 mConfirmLabel = label;
2000 return this;
2001 }
2002
2003 /**
2004 * Get the label to display to confirm that the action should be executed.
2005 * This is usually an imperative verb like "Send".
2006 *
2007 * @return the label to confirm the action should be executed
2008 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002009 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002010 public CharSequence getConfirmLabel() {
2011 return mConfirmLabel;
2012 }
2013
2014 /**
2015 * Set a label to display to cancel the action.
2016 * This is usually an imperative verb, like "Cancel".
2017 *
2018 * @param label the label to display to cancel the action
2019 * @return this object for method chaining
2020 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002021 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002022 public WearableExtender setCancelLabel(CharSequence label) {
2023 mCancelLabel = label;
2024 return this;
2025 }
2026
2027 /**
2028 * Get the label to display to cancel the action.
2029 * This is usually an imperative verb like "Cancel".
2030 *
2031 * @return the label to display to cancel the action
2032 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002033 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002034 public CharSequence getCancelLabel() {
2035 return mCancelLabel;
2036 }
Alex Hills9ab3a232016-04-05 14:54:56 -04002037
2038 /**
2039 * Set a hint that this Action will launch an {@link Activity} directly, telling the
2040 * platform that it can generate the appropriate transitions.
2041 * @param hintLaunchesActivity {@code true} if the content intent will launch
2042 * an activity and transitions should be generated, false otherwise.
2043 * @return this object for method chaining
2044 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04002045 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04002046 boolean hintLaunchesActivity) {
2047 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
2048 return this;
2049 }
2050
2051 /**
2052 * Get a hint that this Action will launch an {@link Activity} directly, telling the
2053 * platform that it can generate the appropriate transitions
2054 * @return {@code true} if the content intent will launch an activity and transitions
2055 * should be generated, false otherwise. The default value is {@code false} if this was
2056 * never set.
2057 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04002058 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04002059 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
2060 }
Alex Hills9f087612016-06-07 09:08:59 -04002061
2062 /**
2063 * Set a hint that this Action should be displayed inline.
2064 *
2065 * @param hintDisplayInline {@code true} if action should be displayed inline, false
2066 * otherwise
2067 * @return this object for method chaining
2068 */
2069 public WearableExtender setHintDisplayActionInline(
2070 boolean hintDisplayInline) {
2071 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
2072 return this;
2073 }
2074
2075 /**
2076 * Get a hint that this Action should be displayed inline.
2077 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08002078 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04002079 * otherwise. The default value is {@code false} if this was never set.
2080 */
2081 public boolean getHintDisplayActionInline() {
2082 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
2083 }
Griff Hazen61a9e862014-05-22 16:05:19 -07002084 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002085
2086 /**
2087 * Provides meaning to an {@link Action} that hints at what the associated
2088 * {@link PendingIntent} will do. For example, an {@link Action} with a
2089 * {@link PendingIntent} that replies to a text message notification may have the
2090 * {@link #SEMANTIC_ACTION_REPLY} {@code SemanticAction} set within it.
2091 *
2092 * @hide
2093 */
2094 @IntDef(prefix = { "SEMANTIC_ACTION_" }, value = {
2095 SEMANTIC_ACTION_NONE,
2096 SEMANTIC_ACTION_REPLY,
2097 SEMANTIC_ACTION_MARK_AS_READ,
2098 SEMANTIC_ACTION_MARK_AS_UNREAD,
2099 SEMANTIC_ACTION_DELETE,
2100 SEMANTIC_ACTION_ARCHIVE,
2101 SEMANTIC_ACTION_MUTE,
2102 SEMANTIC_ACTION_UNMUTE,
2103 SEMANTIC_ACTION_THUMBS_UP,
Kodlee Yincda5b092018-02-15 15:34:53 -08002104 SEMANTIC_ACTION_THUMBS_DOWN,
Gustav Sennton005d7a02019-01-04 13:41:32 +00002105 SEMANTIC_ACTION_CALL
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002106 })
2107 @Retention(RetentionPolicy.SOURCE)
2108 public @interface SemanticAction {}
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002109 }
2110
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002111 /**
2112 * Array of all {@link Action} structures attached to this notification by
2113 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
2114 * {@link android.service.notification.NotificationListenerService} that provide an alternative
2115 * interface for invoking actions.
2116 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05002117 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002118
2119 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002120 * Replacement version of this notification whose content will be shown
2121 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
2122 * and {@link #VISIBILITY_PUBLIC}.
2123 */
2124 public Notification publicVersion;
2125
2126 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002127 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08002128 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002129 */
2130 public Notification()
2131 {
2132 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002133 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002134 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 }
2136
2137 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002138 * @hide
2139 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002140 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002141 public Notification(Context context, int icon, CharSequence tickerText, long when,
2142 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
2143 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04002144 new Builder(context)
2145 .setWhen(when)
2146 .setSmallIcon(icon)
2147 .setTicker(tickerText)
2148 .setContentTitle(contentTitle)
2149 .setContentText(contentText)
2150 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
2151 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002152 }
2153
2154 /**
2155 * Constructs a Notification object with the information needed to
2156 * have a status bar icon without the standard expanded view.
2157 *
2158 * @param icon The resource id of the icon to put in the status bar.
2159 * @param tickerText The text that flows by in the status bar when the notification first
2160 * activates.
2161 * @param when The time to show in the time field. In the System.currentTimeMillis
2162 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08002163 *
2164 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002165 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002166 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002167 public Notification(int icon, CharSequence tickerText, long when)
2168 {
2169 this.icon = icon;
2170 this.tickerText = tickerText;
2171 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002172 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002173 }
2174
2175 /**
2176 * Unflatten the notification from a parcel.
2177 */
Svet Ganovddb94882016-06-23 19:55:24 -07002178 @SuppressWarnings("unchecked")
2179 public Notification(Parcel parcel) {
2180 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
2181 // intents in extras are always written as the last entry.
2182 readFromParcelImpl(parcel);
2183 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002184 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07002185 }
2186
2187 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002188 {
2189 int version = parcel.readInt();
2190
Adrian Roosfb921842017-10-26 14:49:56 +02002191 mWhitelistToken = parcel.readStrongBinder();
2192 if (mWhitelistToken == null) {
2193 mWhitelistToken = processWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -07002194 }
2195 // Propagate this token to all pending intents that are unmarshalled from the parcel.
Adrian Roosfb921842017-10-26 14:49:56 +02002196 parcel.setClassCookie(PendingIntent.class, mWhitelistToken);
Dianne Hackborn98305522017-05-05 17:53:53 -07002197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002198 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002199 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04002200 if (parcel.readInt() != 0) {
2201 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04002202 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
2203 icon = mSmallIcon.getResId();
2204 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002205 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002206 number = parcel.readInt();
2207 if (parcel.readInt() != 0) {
2208 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2209 }
2210 if (parcel.readInt() != 0) {
2211 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2212 }
2213 if (parcel.readInt() != 0) {
2214 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2215 }
2216 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002217 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002218 }
2219 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
2221 }
Joe Onorato561d3852010-11-20 18:09:34 -08002222 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002223 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08002224 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002225 defaults = parcel.readInt();
2226 flags = parcel.readInt();
2227 if (parcel.readInt() != 0) {
2228 sound = Uri.CREATOR.createFromParcel(parcel);
2229 }
2230
2231 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04002232 if (parcel.readInt() != 0) {
2233 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
2234 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002235 vibrate = parcel.createLongArray();
2236 ledARGB = parcel.readInt();
2237 ledOnMS = parcel.readInt();
2238 ledOffMS = parcel.readInt();
2239 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002240
2241 if (parcel.readInt() != 0) {
2242 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2243 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002244
2245 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002246
John Spurlockfd7f1e02014-03-18 16:41:57 -04002247 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002248
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002249 mGroupKey = parcel.readString();
2250
2251 mSortKey = parcel.readString();
2252
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06002253 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Robin Leead7e72a2017-12-04 15:45:46 +01002254 fixDuplicateExtras();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002255
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002256 actions = parcel.createTypedArray(Action.CREATOR); // may be null
2257
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002258 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002259 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2260 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002261
Chris Wren8fd39ec2014-02-27 17:43:26 -05002262 if (parcel.readInt() != 0) {
2263 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2264 }
2265
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002266 visibility = parcel.readInt();
2267
2268 if (parcel.readInt() != 0) {
2269 publicVersion = Notification.CREATOR.createFromParcel(parcel);
2270 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002271
2272 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002273
2274 if (parcel.readInt() != 0) {
2275 mChannelId = parcel.readString();
2276 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002277 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05002278
2279 if (parcel.readInt() != 0) {
2280 mShortcutId = parcel.readString();
2281 }
2282
Felipe Leme90205ef2019-03-05 09:59:52 -08002283 if (parcel.readInt() != 0) {
2284 mLocusId = LocusId.CREATOR.createFromParcel(parcel);
2285 }
2286
Julia Reynolds13d898c2017-02-02 12:22:05 -05002287 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04002288
2289 if (parcel.readInt() != 0) {
2290 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2291 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002292
2293 mGroupAlertBehavior = parcel.readInt();
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002294 if (parcel.readInt() != 0) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002295 mBubbleMetadata = BubbleMetadata.CREATOR.createFromParcel(parcel);
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002296 }
Gustav Sennton761884c2018-11-19 17:40:19 +00002297
2298 mAllowSystemGeneratedContextualActions = parcel.readBoolean();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002299 }
2300
Andy Stadler110988c2010-12-03 14:29:16 -08002301 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07002302 public Notification clone() {
2303 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04002304 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002305 return that;
2306 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002307
Daniel Sandler1a497d32013-04-18 14:52:45 -04002308 /**
2309 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
2310 * of this into that.
2311 * @hide
2312 */
2313 public void cloneInto(Notification that, boolean heavy) {
Adrian Roosfb921842017-10-26 14:49:56 +02002314 that.mWhitelistToken = this.mWhitelistToken;
Joe Onorato18e69df2010-05-17 22:26:12 -07002315 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002316 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002317 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07002318 that.number = this.number;
2319
2320 // PendingIntents are global, so there's no reason (or way) to clone them.
2321 that.contentIntent = this.contentIntent;
2322 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002323 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07002324
2325 if (this.tickerText != null) {
2326 that.tickerText = this.tickerText.toString();
2327 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002328 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002329 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04002330 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002331 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07002332 that.contentView = this.contentView.clone();
2333 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002334 if (heavy && this.mLargeIcon != null) {
2335 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08002336 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01002337 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07002338 that.sound = this.sound; // android.net.Uri is immutable
2339 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002340 if (this.audioAttributes != null) {
2341 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
2342 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002343
2344 final long[] vibrate = this.vibrate;
2345 if (vibrate != null) {
2346 final int N = vibrate.length;
2347 final long[] vib = that.vibrate = new long[N];
2348 System.arraycopy(vibrate, 0, vib, 0, N);
2349 }
2350
2351 that.ledARGB = this.ledARGB;
2352 that.ledOnMS = this.ledOnMS;
2353 that.ledOffMS = this.ledOffMS;
2354 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002355
Joe Onorato18e69df2010-05-17 22:26:12 -07002356 that.flags = this.flags;
2357
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002358 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08002359
John Spurlockfd7f1e02014-03-18 16:41:57 -04002360 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002361
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002362 that.mGroupKey = this.mGroupKey;
2363
2364 that.mSortKey = this.mSortKey;
2365
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002366 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002367 try {
2368 that.extras = new Bundle(this.extras);
2369 // will unparcel
2370 that.extras.size();
2371 } catch (BadParcelableException e) {
2372 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
2373 that.extras = null;
2374 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002375 }
2376
Felipe Lemedd85da62016-06-28 11:29:54 -07002377 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2378 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002379 }
2380
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002381 if (this.actions != null) {
2382 that.actions = new Action[this.actions.length];
2383 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002384 if ( this.actions[i] != null) {
2385 that.actions[i] = this.actions[i].clone();
2386 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002387 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002388 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002389
Daniel Sandler1a497d32013-04-18 14:52:45 -04002390 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002391 that.bigContentView = this.bigContentView.clone();
2392 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002393
Chris Wren8fd39ec2014-02-27 17:43:26 -05002394 if (heavy && this.headsUpContentView != null) {
2395 that.headsUpContentView = this.headsUpContentView.clone();
2396 }
2397
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002398 that.visibility = this.visibility;
2399
2400 if (this.publicVersion != null) {
2401 that.publicVersion = new Notification();
2402 this.publicVersion.cloneInto(that.publicVersion, heavy);
2403 }
2404
Dan Sandler26e81cf2014-05-06 10:01:27 -04002405 that.color = this.color;
2406
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002407 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002408 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002409 that.mShortcutId = this.mShortcutId;
Felipe Leme90205ef2019-03-05 09:59:52 -08002410 that.mLocusId = this.mLocusId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002411 that.mBadgeIcon = this.mBadgeIcon;
2412 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002413 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002414 that.mBubbleMetadata = this.mBubbleMetadata;
Gustav Sennton761884c2018-11-19 17:40:19 +00002415 that.mAllowSystemGeneratedContextualActions = this.mAllowSystemGeneratedContextualActions;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002416
Daniel Sandler1a497d32013-04-18 14:52:45 -04002417 if (!heavy) {
2418 that.lightenPayload(); // will clean out extras
2419 }
2420 }
2421
2422 /**
Jeff Sharkey23b31182018-04-18 21:32:12 -06002423 * Note all {@link Uri} that are referenced internally, with the expectation
2424 * that Uri permission grants will need to be issued to ensure the recipient
2425 * of this object is able to render its contents.
2426 *
2427 * @hide
2428 */
2429 public void visitUris(@NonNull Consumer<Uri> visitor) {
2430 visitor.accept(sound);
2431
2432 if (tickerView != null) tickerView.visitUris(visitor);
2433 if (contentView != null) contentView.visitUris(visitor);
2434 if (bigContentView != null) bigContentView.visitUris(visitor);
2435 if (headsUpContentView != null) headsUpContentView.visitUris(visitor);
2436
2437 if (extras != null) {
2438 visitor.accept(extras.getParcelable(EXTRA_AUDIO_CONTENTS_URI));
Robin Leed107af62018-04-27 13:55:56 +02002439 if (extras.containsKey(EXTRA_BACKGROUND_IMAGE_URI)) {
2440 visitor.accept(Uri.parse(extras.getString(EXTRA_BACKGROUND_IMAGE_URI)));
2441 }
Jeff Sharkey23b31182018-04-18 21:32:12 -06002442 }
2443
2444 if (MessagingStyle.class.equals(getNotificationStyle()) && extras != null) {
2445 final Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
2446 if (!ArrayUtils.isEmpty(messages)) {
2447 for (MessagingStyle.Message message : MessagingStyle.Message
2448 .getMessagesFromBundleArray(messages)) {
2449 visitor.accept(message.getDataUri());
2450 }
2451 }
2452
2453 final Parcelable[] historic = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
2454 if (!ArrayUtils.isEmpty(historic)) {
2455 for (MessagingStyle.Message message : MessagingStyle.Message
2456 .getMessagesFromBundleArray(historic)) {
2457 visitor.accept(message.getDataUri());
2458 }
2459 }
2460 }
2461 }
2462
2463 /**
Daniel Sandler1a497d32013-04-18 14:52:45 -04002464 * Removes heavyweight parts of the Notification object for archival or for sending to
2465 * listeners when the full contents are not necessary.
2466 * @hide
2467 */
2468 public final void lightenPayload() {
2469 tickerView = null;
2470 contentView = null;
2471 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002472 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002473 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002474 if (extras != null && !extras.isEmpty()) {
2475 final Set<String> keyset = extras.keySet();
2476 final int N = keyset.size();
2477 final String[] keys = keyset.toArray(new String[N]);
2478 for (int i=0; i<N; i++) {
2479 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002480 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2481 continue;
2482 }
Dan Sandler50128532015-12-08 15:42:41 -05002483 final Object obj = extras.get(key);
2484 if (obj != null &&
2485 ( obj instanceof Parcelable
2486 || obj instanceof Parcelable[]
2487 || obj instanceof SparseArray
2488 || obj instanceof ArrayList)) {
2489 extras.remove(key);
2490 }
2491 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002492 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002493 }
2494
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002495 /**
2496 * Make sure this CharSequence is safe to put into a bundle, which basically
2497 * means it had better not be some custom Parcelable implementation.
2498 * @hide
2499 */
2500 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002501 if (cs == null) return cs;
2502 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2503 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2504 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002505 if (cs instanceof Parcelable) {
2506 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2507 + " instance is a custom Parcelable and not allowed in Notification");
2508 return cs.toString();
2509 }
Selim Cinek60a54252016-02-26 17:03:25 -08002510 return removeTextSizeSpans(cs);
2511 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002512
Selim Cinek60a54252016-02-26 17:03:25 -08002513 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2514 if (charSequence instanceof Spanned) {
2515 Spanned ss = (Spanned) charSequence;
2516 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2517 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2518 for (Object span : spans) {
2519 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002520 if (resultSpan instanceof CharacterStyle) {
2521 resultSpan = ((CharacterStyle) span).getUnderlying();
2522 }
2523 if (resultSpan instanceof TextAppearanceSpan) {
2524 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002525 resultSpan = new TextAppearanceSpan(
2526 originalSpan.getFamily(),
2527 originalSpan.getTextStyle(),
2528 -1,
2529 originalSpan.getTextColor(),
2530 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002531 } else if (resultSpan instanceof RelativeSizeSpan
2532 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002533 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002534 } else {
2535 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002536 }
2537 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2538 ss.getSpanFlags(span));
2539 }
2540 return builder;
2541 }
2542 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002543 }
2544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002545 public int describeContents() {
2546 return 0;
2547 }
2548
2549 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002550 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002551 */
Svet Ganovddb94882016-06-23 19:55:24 -07002552 public void writeToParcel(Parcel parcel, int flags) {
2553 // We need to mark all pending intents getting into the notification
2554 // system as being put there to later allow the notification ranker
2555 // to launch them and by doing so add the app to the battery saver white
2556 // list for a short period of time. The problem is that the system
2557 // cannot look into the extras as there may be parcelables there that
2558 // the platform does not know how to handle. To go around that we have
2559 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002560 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002561 if (collectPendingIntents) {
2562 PendingIntent.setOnMarshaledListener(
2563 (PendingIntent intent, Parcel out, int outFlags) -> {
2564 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002565 if (allPendingIntents == null) {
2566 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002567 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002568 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002569 }
2570 });
2571 }
2572 try {
2573 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002574 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002575 writeToParcelImpl(parcel, flags);
2576 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002577 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002578 } finally {
2579 if (collectPendingIntents) {
2580 PendingIntent.setOnMarshaledListener(null);
2581 }
2582 }
2583 }
2584
2585 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002586 parcel.writeInt(1);
2587
Adrian Roosfb921842017-10-26 14:49:56 +02002588 parcel.writeStrongBinder(mWhitelistToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002589 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002590 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002591 if (mSmallIcon == null && icon != 0) {
2592 // you snuck an icon in here without using the builder; let's try to keep it
2593 mSmallIcon = Icon.createWithResource("", icon);
2594 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002595 if (mSmallIcon != null) {
2596 parcel.writeInt(1);
2597 mSmallIcon.writeToParcel(parcel, 0);
2598 } else {
2599 parcel.writeInt(0);
2600 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002601 parcel.writeInt(number);
2602 if (contentIntent != null) {
2603 parcel.writeInt(1);
2604 contentIntent.writeToParcel(parcel, 0);
2605 } else {
2606 parcel.writeInt(0);
2607 }
2608 if (deleteIntent != null) {
2609 parcel.writeInt(1);
2610 deleteIntent.writeToParcel(parcel, 0);
2611 } else {
2612 parcel.writeInt(0);
2613 }
2614 if (tickerText != null) {
2615 parcel.writeInt(1);
2616 TextUtils.writeToParcel(tickerText, parcel, flags);
2617 } else {
2618 parcel.writeInt(0);
2619 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002620 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002621 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002622 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002623 } else {
2624 parcel.writeInt(0);
2625 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002626 if (contentView != null) {
2627 parcel.writeInt(1);
2628 contentView.writeToParcel(parcel, 0);
2629 } else {
2630 parcel.writeInt(0);
2631 }
Selim Cinek279fa862016-06-14 10:57:25 -07002632 if (mLargeIcon == null && largeIcon != null) {
2633 // you snuck an icon in here without using the builder; let's try to keep it
2634 mLargeIcon = Icon.createWithBitmap(largeIcon);
2635 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002636 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002637 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002638 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002639 } else {
2640 parcel.writeInt(0);
2641 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002642
2643 parcel.writeInt(defaults);
2644 parcel.writeInt(this.flags);
2645
2646 if (sound != null) {
2647 parcel.writeInt(1);
2648 sound.writeToParcel(parcel, 0);
2649 } else {
2650 parcel.writeInt(0);
2651 }
2652 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002653
2654 if (audioAttributes != null) {
2655 parcel.writeInt(1);
2656 audioAttributes.writeToParcel(parcel, 0);
2657 } else {
2658 parcel.writeInt(0);
2659 }
2660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002661 parcel.writeLongArray(vibrate);
2662 parcel.writeInt(ledARGB);
2663 parcel.writeInt(ledOnMS);
2664 parcel.writeInt(ledOffMS);
2665 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002666
2667 if (fullScreenIntent != null) {
2668 parcel.writeInt(1);
2669 fullScreenIntent.writeToParcel(parcel, 0);
2670 } else {
2671 parcel.writeInt(0);
2672 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002673
2674 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002675
John Spurlockfd7f1e02014-03-18 16:41:57 -04002676 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002677
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002678 parcel.writeString(mGroupKey);
2679
2680 parcel.writeString(mSortKey);
2681
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002682 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002683
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002684 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002685
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002686 if (bigContentView != null) {
2687 parcel.writeInt(1);
2688 bigContentView.writeToParcel(parcel, 0);
2689 } else {
2690 parcel.writeInt(0);
2691 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002692
Chris Wren8fd39ec2014-02-27 17:43:26 -05002693 if (headsUpContentView != null) {
2694 parcel.writeInt(1);
2695 headsUpContentView.writeToParcel(parcel, 0);
2696 } else {
2697 parcel.writeInt(0);
2698 }
2699
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002700 parcel.writeInt(visibility);
2701
2702 if (publicVersion != null) {
2703 parcel.writeInt(1);
2704 publicVersion.writeToParcel(parcel, 0);
2705 } else {
2706 parcel.writeInt(0);
2707 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002708
2709 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002710
2711 if (mChannelId != null) {
2712 parcel.writeInt(1);
2713 parcel.writeString(mChannelId);
2714 } else {
2715 parcel.writeInt(0);
2716 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002717 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002718
2719 if (mShortcutId != null) {
2720 parcel.writeInt(1);
2721 parcel.writeString(mShortcutId);
2722 } else {
2723 parcel.writeInt(0);
2724 }
2725
Felipe Leme90205ef2019-03-05 09:59:52 -08002726 if (mLocusId != null) {
2727 parcel.writeInt(1);
2728 mLocusId.writeToParcel(parcel, 0);
2729 } else {
2730 parcel.writeInt(0);
2731 }
2732
Julia Reynolds13d898c2017-02-02 12:22:05 -05002733 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002734
2735 if (mSettingsText != null) {
2736 parcel.writeInt(1);
2737 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2738 } else {
2739 parcel.writeInt(0);
2740 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002741
2742 parcel.writeInt(mGroupAlertBehavior);
Julia Reynoldsfc640012018-02-21 12:25:27 -05002743
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002744 if (mBubbleMetadata != null) {
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002745 parcel.writeInt(1);
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002746 mBubbleMetadata.writeToParcel(parcel, 0);
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002747 } else {
2748 parcel.writeInt(0);
2749 }
2750
Gustav Sennton761884c2018-11-19 17:40:19 +00002751 parcel.writeBoolean(mAllowSystemGeneratedContextualActions);
2752
Julia Reynoldsfc640012018-02-21 12:25:27 -05002753 // mUsesStandardHeader is not written because it should be recomputed in listeners
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002754 }
2755
2756 /**
2757 * Parcelable.Creator that instantiates Notification objects
2758 */
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07002759 public static final @android.annotation.NonNull Parcelable.Creator<Notification> CREATOR
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002760 = new Parcelable.Creator<Notification>()
2761 {
2762 public Notification createFromParcel(Parcel parcel)
2763 {
2764 return new Notification(parcel);
2765 }
2766
2767 public Notification[] newArray(int size)
2768 {
2769 return new Notification[size];
2770 }
2771 };
2772
2773 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05002774 * @hide
2775 */
2776 public static boolean areActionsVisiblyDifferent(Notification first, Notification second) {
2777 Notification.Action[] firstAs = first.actions;
2778 Notification.Action[] secondAs = second.actions;
2779 if (firstAs == null && secondAs != null || firstAs != null && secondAs == null) {
2780 return true;
2781 }
2782 if (firstAs != null && secondAs != null) {
2783 if (firstAs.length != secondAs.length) {
2784 return true;
2785 }
2786 for (int i = 0; i < firstAs.length; i++) {
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -04002787 if (!Objects.equals(String.valueOf(firstAs[i].title),
2788 String.valueOf(secondAs[i].title))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05002789 return true;
2790 }
2791 RemoteInput[] firstRs = firstAs[i].getRemoteInputs();
2792 RemoteInput[] secondRs = secondAs[i].getRemoteInputs();
2793 if (firstRs == null) {
2794 firstRs = new RemoteInput[0];
2795 }
2796 if (secondRs == null) {
2797 secondRs = new RemoteInput[0];
2798 }
2799 if (firstRs.length != secondRs.length) {
2800 return true;
2801 }
2802 for (int j = 0; j < firstRs.length; j++) {
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -04002803 if (!Objects.equals(String.valueOf(firstRs[j].getLabel()),
2804 String.valueOf(secondRs[j].getLabel()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05002805 return true;
2806 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05002807 }
2808 }
2809 }
2810 return false;
2811 }
2812
2813 /**
2814 * @hide
2815 */
2816 public static boolean areStyledNotificationsVisiblyDifferent(Builder first, Builder second) {
2817 if (first.getStyle() == null) {
2818 return second.getStyle() != null;
2819 }
2820 if (second.getStyle() == null) {
2821 return true;
2822 }
2823 return first.getStyle().areNotificationsVisiblyDifferent(second.getStyle());
2824 }
2825
2826 /**
2827 * @hide
2828 */
2829 public static boolean areRemoteViewsChanged(Builder first, Builder second) {
Julia Reynoldse5c60452018-04-30 14:41:36 -04002830 if (!Objects.equals(first.usesStandardHeader(), second.usesStandardHeader())) {
2831 return true;
2832 }
2833
2834 if (areRemoteViewsChanged(first.mN.contentView, second.mN.contentView)) {
2835 return true;
2836 }
2837 if (areRemoteViewsChanged(first.mN.bigContentView, second.mN.bigContentView)) {
2838 return true;
2839 }
2840 if (areRemoteViewsChanged(first.mN.headsUpContentView, second.mN.headsUpContentView)) {
2841 return true;
2842 }
2843
2844 return false;
2845 }
2846
2847 private static boolean areRemoteViewsChanged(RemoteViews first, RemoteViews second) {
2848 if (first == null && second == null) {
2849 return false;
2850 }
2851 if (first == null && second != null || first != null && second == null) {
2852 return true;
2853 }
2854
2855 if (!Objects.equals(first.getLayoutId(), second.getLayoutId())) {
2856 return true;
2857 }
2858
2859 if (!Objects.equals(first.getSequenceNumber(), second.getSequenceNumber())) {
2860 return true;
2861 }
2862
2863 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05002864 }
2865
2866 /**
Robin Leead7e72a2017-12-04 15:45:46 +01002867 * Parcelling creates multiple copies of objects in {@code extras}. Fix them.
2868 * <p>
2869 * For backwards compatibility {@code extras} holds some references to "real" member data such
2870 * as {@link getLargeIcon()} which is mirrored by {@link #EXTRA_LARGE_ICON}. This is mostly
2871 * fine as long as the object stays in one process.
2872 * <p>
2873 * However, once the notification goes into a parcel each reference gets marshalled separately,
2874 * wasting memory. Especially with large images on Auto and TV, this is worth fixing.
2875 */
2876 private void fixDuplicateExtras() {
2877 if (extras != null) {
Robin Leead7e72a2017-12-04 15:45:46 +01002878 fixDuplicateExtra(mLargeIcon, EXTRA_LARGE_ICON);
2879 }
2880 }
2881
2882 /**
2883 * If we find an extra that's exactly the same as one of the "real" fields but refers to a
2884 * separate object, replace it with the field's version to avoid holding duplicate copies.
2885 */
2886 private void fixDuplicateExtra(@Nullable Parcelable original, @NonNull String extraName) {
2887 if (original != null && extras.getParcelable(extraName) != null) {
2888 extras.putParcelable(extraName, original);
2889 }
2890 }
2891
2892 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002893 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2894 * layout.
2895 *
2896 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2897 * in the view.</p>
2898 * @param context The context for your application / activity.
2899 * @param contentTitle The title that goes in the expanded entry.
2900 * @param contentText The text that goes in the expanded entry.
2901 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2902 * If this is an activity, it must include the
2903 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002904 * that you take care of task management as described in the
2905 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2906 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002907 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002908 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002909 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002910 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002911 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002912 public void setLatestEventInfo(Context context,
2913 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002914 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2915 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2916 new Throwable());
2917 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002918
Selim Cinek4ac6f602016-06-13 15:47:03 -07002919 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2920 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2921 }
2922
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002923 // ensure that any information already set directly is preserved
2924 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002925
2926 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002927 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002928 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002929 }
2930 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002931 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002932 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002933 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002934
2935 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002936 }
2937
Julia Reynoldsda303542015-11-23 14:00:20 -05002938 /**
2939 * @hide
2940 */
2941 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002942 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002943 }
2944
2945 /**
2946 * @hide
2947 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002948 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002949 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002950 }
2951
Yi Jin6b514142017-10-30 14:54:12 -07002952 /**
2953 * @hide
2954 */
2955 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2956 long token = proto.start(fieldId);
2957 proto.write(NotificationProto.CHANNEL_ID, getChannelId());
2958 proto.write(NotificationProto.HAS_TICKER_TEXT, this.tickerText != null);
2959 proto.write(NotificationProto.FLAGS, this.flags);
2960 proto.write(NotificationProto.COLOR, this.color);
2961 proto.write(NotificationProto.CATEGORY, this.category);
2962 proto.write(NotificationProto.GROUP_KEY, this.mGroupKey);
2963 proto.write(NotificationProto.SORT_KEY, this.mSortKey);
2964 if (this.actions != null) {
2965 proto.write(NotificationProto.ACTION_LENGTH, this.actions.length);
2966 }
2967 if (this.visibility >= VISIBILITY_SECRET && this.visibility <= VISIBILITY_PUBLIC) {
2968 proto.write(NotificationProto.VISIBILITY, this.visibility);
2969 }
2970 if (publicVersion != null) {
2971 publicVersion.writeToProto(proto, NotificationProto.PUBLIC_VERSION);
2972 }
2973 proto.end(token);
2974 }
2975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002976 @Override
2977 public String toString() {
2978 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002979 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04002980 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002981 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002982 sb.append(priority);
2983 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002984 if (contentView != null) {
2985 sb.append(contentView.getPackage());
2986 sb.append("/0x");
2987 sb.append(Integer.toHexString(contentView.getLayoutId()));
2988 } else {
2989 sb.append("null");
2990 }
2991 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002992 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2993 sb.append("default");
2994 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002995 int N = this.vibrate.length-1;
2996 sb.append("[");
2997 for (int i=0; i<N; i++) {
2998 sb.append(this.vibrate[i]);
2999 sb.append(',');
3000 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02003001 if (N != -1) {
3002 sb.append(this.vibrate[N]);
3003 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003004 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003005 } else {
3006 sb.append("null");
3007 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003008 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05003009 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003010 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05003011 } else if (this.sound != null) {
3012 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003013 } else {
3014 sb.append("null");
3015 }
Chris Wren365b6d32015-07-16 10:39:26 -04003016 if (this.tickerText != null) {
3017 sb.append(" tick");
3018 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003019 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003020 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003021 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04003022 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04003023 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003024 if (this.category != null) {
3025 sb.append(" category=");
3026 sb.append(this.category);
3027 }
3028 if (this.mGroupKey != null) {
3029 sb.append(" groupKey=");
3030 sb.append(this.mGroupKey);
3031 }
3032 if (this.mSortKey != null) {
3033 sb.append(" sortKey=");
3034 sb.append(this.mSortKey);
3035 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003036 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04003037 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003038 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04003039 }
3040 sb.append(" vis=");
3041 sb.append(visibilityToString(this.visibility));
3042 if (this.publicVersion != null) {
3043 sb.append(" publicVersion=");
3044 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003045 }
Felipe Leme90205ef2019-03-05 09:59:52 -08003046 if (this.mLocusId != null) {
3047 sb.append(" locusId=");
3048 sb.append(this.mLocusId); // LocusId.toString() is PII safe.
3049 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003050 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003051 return sb.toString();
3052 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003053
Dan Sandler1b718782014-07-18 12:43:45 -04003054 /**
3055 * {@hide}
3056 */
3057 public static String visibilityToString(int vis) {
3058 switch (vis) {
3059 case VISIBILITY_PRIVATE:
3060 return "PRIVATE";
3061 case VISIBILITY_PUBLIC:
3062 return "PUBLIC";
3063 case VISIBILITY_SECRET:
3064 return "SECRET";
3065 default:
3066 return "UNKNOWN(" + String.valueOf(vis) + ")";
3067 }
3068 }
3069
Joe Onoratocb109a02011-01-18 17:57:41 -08003070 /**
John Spurlock1d881a12015-03-18 19:21:54 -04003071 * {@hide}
3072 */
3073 public static String priorityToString(@Priority int pri) {
3074 switch (pri) {
3075 case PRIORITY_MIN:
3076 return "MIN";
3077 case PRIORITY_LOW:
3078 return "LOW";
3079 case PRIORITY_DEFAULT:
3080 return "DEFAULT";
3081 case PRIORITY_HIGH:
3082 return "HIGH";
3083 case PRIORITY_MAX:
3084 return "MAX";
3085 default:
3086 return "UNKNOWN(" + String.valueOf(pri) + ")";
3087 }
3088 }
3089
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04003090 /**
3091 * @hide
3092 */
3093 public boolean hasCompletedProgress() {
3094 // not a progress notification; can't be complete
3095 if (!extras.containsKey(EXTRA_PROGRESS)
3096 || !extras.containsKey(EXTRA_PROGRESS_MAX)) {
3097 return false;
3098 }
3099 // many apps use max 0 for 'indeterminate'; not complete
3100 if (extras.getInt(EXTRA_PROGRESS_MAX) == 0) {
3101 return false;
3102 }
3103 return extras.getInt(EXTRA_PROGRESS) == extras.getInt(EXTRA_PROGRESS_MAX);
3104 }
3105
Jeff Sharkey000ce802017-04-29 13:13:27 -06003106 /** @removed */
3107 @Deprecated
Julia Reynolds37856052016-11-11 09:20:07 -05003108 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003109 return mChannelId;
3110 }
3111
3112 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003113 * Returns the id of the channel this notification posts to.
3114 */
3115 public String getChannelId() {
3116 return mChannelId;
3117 }
3118
Jeff Sharkey000ce802017-04-29 13:13:27 -06003119 /** @removed */
3120 @Deprecated
Julia Reynolds2a128742016-11-28 14:29:25 -05003121 public long getTimeout() {
3122 return mTimeout;
3123 }
3124
3125 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003126 * Returns the duration from posting after which this notification should be canceled by the
3127 * system, if it's not canceled already.
3128 */
3129 public long getTimeoutAfter() {
3130 return mTimeout;
3131 }
3132
3133 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003134 * Returns what icon should be shown for this notification if it is being displayed in a
3135 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
3136 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
3137 */
3138 public int getBadgeIconType() {
3139 return mBadgeIcon;
3140 }
3141
3142 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003143 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04003144 *
3145 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
3146 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003147 */
3148 public String getShortcutId() {
3149 return mShortcutId;
3150 }
3151
Felipe Leme90205ef2019-03-05 09:59:52 -08003152 /**
3153 * Gets the {@link LocusId} associated with this notification.
3154 *
3155 * <p>Used by the device's intelligence services to correlate objects (such as
3156 * {@link ShortcutInfo} and {@link ContentCaptureContext}) that are correlated.
3157 */
3158 @Nullable
3159 public LocusId getLocusId() {
3160 return mLocusId;
3161 }
Julia Reynolds3aedded2017-03-31 14:42:09 -04003162
3163 /**
3164 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
3165 */
3166 public CharSequence getSettingsText() {
3167 return mSettingsText;
3168 }
3169
Julia Reynolds13d898c2017-02-02 12:22:05 -05003170 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003171 * Returns which type of notifications in a group are responsible for audibly alerting the
3172 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
3173 * {@link #GROUP_ALERT_SUMMARY}.
3174 */
3175 public @GroupAlertBehavior int getGroupAlertBehavior() {
3176 return mGroupAlertBehavior;
3177 }
3178
3179 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003180 * Returns the bubble metadata that will be used to display app content in a floating window
3181 * over the existing foreground activity.
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003182 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08003183 @Nullable
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003184 public BubbleMetadata getBubbleMetadata() {
3185 return mBubbleMetadata;
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003186 }
3187
Gustav Sennton79ebf4b2018-12-19 10:31:51 +00003188 /**
3189 * Returns whether the platform is allowed (by the app developer) to generate contextual actions
3190 * for this notification.
3191 */
Gustav Sennton761884c2018-11-19 17:40:19 +00003192 public boolean getAllowSystemGeneratedContextualActions() {
3193 return mAllowSystemGeneratedContextualActions;
3194 }
3195
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003196 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003197 * The small icon representing this notification in the status bar and content view.
3198 *
3199 * @return the small icon representing this notification.
3200 *
3201 * @see Builder#getSmallIcon()
3202 * @see Builder#setSmallIcon(Icon)
3203 */
3204 public Icon getSmallIcon() {
3205 return mSmallIcon;
3206 }
3207
3208 /**
3209 * Used when notifying to clean up legacy small icons.
3210 * @hide
3211 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003212 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04003213 public void setSmallIcon(Icon icon) {
3214 mSmallIcon = icon;
3215 }
3216
3217 /**
3218 * The large icon shown in this notification's content view.
3219 * @see Builder#getLargeIcon()
3220 * @see Builder#setLargeIcon(Icon)
3221 */
3222 public Icon getLargeIcon() {
3223 return mLargeIcon;
3224 }
3225
3226 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02003227 * @hide
3228 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003229 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003230 public boolean isGroupSummary() {
3231 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
3232 }
3233
3234 /**
3235 * @hide
3236 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003237 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003238 public boolean isGroupChild() {
3239 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
3240 }
3241
3242 /**
Julia Reynolds30203152017-05-26 13:36:31 -04003243 * @hide
3244 */
3245 public boolean suppressAlertingDueToGrouping() {
3246 if (isGroupSummary()
3247 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
3248 return true;
3249 } else if (isGroupChild()
3250 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
3251 return true;
3252 }
3253 return false;
3254 }
3255
Tony Mak638430e2018-10-08 19:19:10 +01003256
3257 /**
3258 * Finds and returns a remote input and its corresponding action.
3259 *
3260 * @param requiresFreeform requires the remoteinput to allow freeform or not.
3261 * @return the result pair, {@code null} if no result is found.
3262 *
3263 * @hide
3264 */
3265 @Nullable
3266 public Pair<RemoteInput, Action> findRemoteInputActionPair(boolean requiresFreeform) {
3267 if (actions == null) {
3268 return null;
3269 }
3270 for (Notification.Action action : actions) {
3271 if (action.getRemoteInputs() == null) {
3272 continue;
3273 }
3274 RemoteInput resultRemoteInput = null;
3275 for (RemoteInput remoteInput : action.getRemoteInputs()) {
3276 if (remoteInput.getAllowFreeFormInput() || !requiresFreeform) {
3277 resultRemoteInput = remoteInput;
3278 }
3279 }
3280 if (resultRemoteInput != null) {
3281 return Pair.create(resultRemoteInput, action);
3282 }
3283 }
3284 return null;
3285 }
3286
Julia Reynolds30203152017-05-26 13:36:31 -04003287 /**
Gustav Sennton005d7a02019-01-04 13:41:32 +00003288 * Returns the actions that are contextual out of the actions in this notification.
Gustav Senntoneab53682018-11-01 16:30:23 +00003289 *
3290 * @hide
3291 */
3292 public List<Notification.Action> getContextualActions() {
3293 if (actions == null) return Collections.emptyList();
3294
3295 List<Notification.Action> contextualActions = new ArrayList<>();
3296 for (Notification.Action action : actions) {
Gustav Sennton005d7a02019-01-04 13:41:32 +00003297 if (action.isContextual()) {
Gustav Senntoneab53682018-11-01 16:30:23 +00003298 contextualActions.add(action);
3299 }
3300 }
3301 return contextualActions;
3302 }
3303
3304 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003305 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08003306 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003307 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07003308 * content views using the platform's notification layout template. If your app supports
3309 * versions of Android as old as API level 4, you can instead use
3310 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
3311 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
3312 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08003313 *
Scott Main183bf112012-08-13 19:12:13 -07003314 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08003315 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003316 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07003317 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003318 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3319 * .setContentText(subject)
3320 * .setSmallIcon(R.drawable.new_mail)
3321 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003322 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003323 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08003324 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003325 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003326 /**
3327 * @hide
3328 */
3329 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
3330 "android.rebuild.contentViewActionCount";
3331 /**
3332 * @hide
3333 */
3334 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
3335 = "android.rebuild.bigViewActionCount";
3336 /**
3337 * @hide
3338 */
3339 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
3340 = "android.rebuild.hudViewActionCount";
3341
Selim Cinek6743c0b2017-01-18 18:24:01 -08003342 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
3343 SystemProperties.getBoolean("notifications.only_title", true);
3344
Selim Cinek389edcd2017-05-11 19:16:44 -07003345 /**
3346 * The lightness difference that has to be added to the primary text color to obtain the
3347 * secondary text color when the background is light.
3348 */
3349 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
3350
3351 /**
3352 * The lightness difference that has to be added to the primary text color to obtain the
3353 * secondary text color when the background is dark.
3354 * A bit less then the above value, since it looks better on dark backgrounds.
3355 */
3356 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
3357
Joe Onorato46439ce2010-11-19 13:56:21 -08003358 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003359 private Notification mN;
3360 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003361 private Style mStyle;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003362 @UnsupportedAppUsage
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003363 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Selim Cineke7238dd2017-12-14 17:48:32 -08003364 private ArrayList<Person> mPersonList = new ArrayList<>();
Lucas Dupina291d192018-06-07 13:59:42 -07003365 private ContrastColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08003366 private boolean mIsLegacy;
3367 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02003368
3369 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08003370 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
3371 */
3372 private int mCachedContrastColor = COLOR_INVALID;
3373 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08003374 /**
Selim Cinek4717d862018-04-19 09:19:15 +08003375 * Caches a ambient version of {@link #mCachedAmbientColorIsFor}.
Adrian Roos487374f2017-01-11 15:48:14 -08003376 */
3377 private int mCachedAmbientColor = COLOR_INVALID;
3378 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Selim Cinek4717d862018-04-19 09:19:15 +08003379 /**
3380 * A neutral color color that can be used for icons.
3381 */
3382 private int mNeutralColor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08003383
3384 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08003385 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
3386 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
3387 */
3388 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08003389 private int mTextColorsAreForBackground = COLOR_INVALID;
3390 private int mPrimaryTextColor = COLOR_INVALID;
3391 private int mSecondaryTextColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07003392 private int mBackgroundColor = COLOR_INVALID;
3393 private int mForegroundColor = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07003394 /**
3395 * A temporary location where actions are stored. If != null the view originally has action
3396 * but doesn't have any for this inflation.
3397 */
3398 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07003399 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003400
Anthony Chenad4d1582017-04-10 16:07:58 -07003401 private boolean mTintActionButtons;
3402 private boolean mInNightMode;
3403
Adrian Roos70d7aa32017-01-11 15:39:06 -08003404 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003405 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08003406 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003407 * @param context
3408 * A {@link Context} that will be used by the Builder to construct the
3409 * RemoteViews. The Context will not be held past the lifetime of this Builder
3410 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003411 * @param channelId
3412 * The constructed Notification will be posted on this
3413 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
3414 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003415 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003416 public Builder(Context context, String channelId) {
3417 this(context, (Notification) null);
3418 mN.mChannelId = channelId;
3419 }
3420
3421 /**
3422 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
3423 * instead. All posted Notifications must specify a NotificationChannel Id.
3424 */
3425 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003426 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003427 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003428 }
3429
Joe Onoratocb109a02011-01-18 17:57:41 -08003430 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003431 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02003432 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003433 public Builder(Context context, Notification toAdopt) {
3434 mContext = context;
Anthony Chenad4d1582017-04-10 16:07:58 -07003435 Resources res = mContext.getResources();
3436 mTintActionButtons = res.getBoolean(R.bool.config_tintNotificationActionButtons);
3437
3438 if (res.getBoolean(R.bool.config_enableNightMode)) {
3439 Configuration currentConfig = res.getConfiguration();
3440 mInNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
3441 == Configuration.UI_MODE_NIGHT_YES;
3442 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003443
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003444 if (toAdopt == null) {
3445 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003446 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
3447 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
3448 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003449 mN.priority = PRIORITY_DEFAULT;
3450 mN.visibility = VISIBILITY_PRIVATE;
3451 } else {
3452 mN = toAdopt;
3453 if (mN.actions != null) {
3454 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003455 }
3456
Selim Cineke7238dd2017-12-14 17:48:32 -08003457 if (mN.extras.containsKey(EXTRA_PEOPLE_LIST)) {
3458 ArrayList<Person> people = mN.extras.getParcelableArrayList(EXTRA_PEOPLE_LIST);
3459 mPersonList.addAll(people);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003460 }
3461
Selim Cinek4ac6f602016-06-13 15:47:03 -07003462 if (mN.getSmallIcon() == null && mN.icon != 0) {
3463 setSmallIcon(mN.icon);
3464 }
3465
3466 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
3467 setLargeIcon(mN.largeIcon);
3468 }
3469
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003470 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
3471 if (!TextUtils.isEmpty(templateClass)) {
3472 final Class<? extends Style> styleClass
3473 = getNotificationStyleClass(templateClass);
3474 if (styleClass == null) {
3475 Log.d(TAG, "Unknown style class: " + templateClass);
3476 } else {
3477 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003478 final Constructor<? extends Style> ctor =
3479 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003480 ctor.setAccessible(true);
3481 final Style style = ctor.newInstance();
3482 style.restoreFromExtras(mN.extras);
3483
3484 if (style != null) {
3485 setStyle(style);
3486 }
3487 } catch (Throwable t) {
3488 Log.e(TAG, "Could not create Style", t);
3489 }
3490 }
3491 }
3492
3493 }
3494 }
3495
Lucas Dupina291d192018-06-07 13:59:42 -07003496 private ContrastColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08003497 if (mColorUtil == null) {
Lucas Dupina291d192018-06-07 13:59:42 -07003498 mColorUtil = ContrastColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003499 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003500 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003501 }
3502
3503 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003504 * If this notification is duplicative of a Launcher shortcut, sets the
3505 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
3506 * the shortcut.
3507 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04003508 * This field will be ignored by Launchers that don't support badging, don't show
3509 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003510 *
3511 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
3512 * supersedes
3513 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003514 @NonNull
Julia Reynolds13d898c2017-02-02 12:22:05 -05003515 public Builder setShortcutId(String shortcutId) {
3516 mN.mShortcutId = shortcutId;
3517 return this;
3518 }
3519
3520 /**
Felipe Leme90205ef2019-03-05 09:59:52 -08003521 * Sets the {@link LocusId} associated with this notification.
3522 *
3523 * <p>This method should be called when the {@link LocusId} is used in other places (such
3524 * as {@link ShortcutInfo} and {@link ContentCaptureContext}) so the device's intelligence
3525 * services can correlate them.
3526 */
3527 @NonNull
3528 public Builder setLocusId(@Nullable LocusId locusId) {
3529 mN.mLocusId = locusId;
3530 return this;
3531 }
3532
3533 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003534 * Sets which icon to display as a badge for this notification.
3535 *
3536 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
3537 * {@link #BADGE_ICON_LARGE}.
3538 *
3539 * Note: This value might be ignored, for launchers that don't support badge icons.
3540 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003541 @NonNull
Julia Reynolds612beb22017-03-30 10:48:30 -04003542 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04003543 mN.mBadgeIcon = icon;
3544 return this;
3545 }
3546
3547 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003548 * Sets the group alert behavior for this notification. Use this method to mute this
3549 * notification if alerts for this notification's group should be handled by a different
3550 * notification. This is only applicable for notifications that belong to a
Julia Reynolds399d9bf2017-08-11 12:52:14 -04003551 * {@link #setGroup(String) group}. This must be called on all notifications you want to
3552 * mute. For example, if you want only the summary of your group to make noise, all
3553 * children in the group should have the group alert behavior {@link #GROUP_ALERT_SUMMARY}.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003554 *
3555 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
3556 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003557 @NonNull
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003558 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
3559 mN.mGroupAlertBehavior = groupAlertBehavior;
3560 return this;
3561 }
3562
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003563 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003564 * Sets the {@link BubbleMetadata} that will be used to display app content in a floating
3565 * window over the existing foreground activity.
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003566 *
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003567 * <p>This data will be ignored unless the notification is posted to a channel that
3568 * allows {@link NotificationChannel#canBubble() bubbles}.</p>
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003569 *
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003570 * <b>Notifications with a valid and allowed bubble metadata will display in collapsed state
3571 * outside of the notification shade on unlocked devices. When a user interacts with the
3572 * collapsed state, the bubble intent will be invoked and displayed.</b>
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003573 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003574 @NonNull
Mady Mellorcf8f1b22019-03-07 14:08:21 -08003575 public Builder setBubbleMetadata(@Nullable BubbleMetadata data) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003576 mN.mBubbleMetadata = data;
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003577 return this;
3578 }
3579
Jeff Sharkey000ce802017-04-29 13:13:27 -06003580 /** @removed */
3581 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003582 public Builder setChannel(String channelId) {
3583 mN.mChannelId = channelId;
3584 return this;
3585 }
3586
3587 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003588 * Specifies the channel the notification should be delivered on.
3589 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003590 @NonNull
Julia Reynoldsbad42972017-04-25 13:52:49 -04003591 public Builder setChannelId(String channelId) {
3592 mN.mChannelId = channelId;
3593 return this;
3594 }
3595
Jeff Sharkey000ce802017-04-29 13:13:27 -06003596 /** @removed */
3597 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05003598 public Builder setTimeout(long durationMs) {
3599 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05003600 return this;
3601 }
3602
3603 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003604 * Specifies a duration in milliseconds after which this notification should be canceled,
3605 * if it is not already canceled.
3606 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003607 @NonNull
Julia Reynoldsbad42972017-04-25 13:52:49 -04003608 public Builder setTimeoutAfter(long durationMs) {
3609 mN.mTimeout = durationMs;
3610 return this;
3611 }
3612
3613 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003614 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003615 *
3616 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
3617 * shown anymore by default and must be opted into by using
3618 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003619 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003620 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08003621 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003622 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003623 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003624 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08003625 return this;
3626 }
3627
Joe Onoratocb109a02011-01-18 17:57:41 -08003628 /**
Griff Hazen50c11652014-05-16 09:46:31 -07003629 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07003630 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003631 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
3632 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07003633 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003634 @NonNull
Daniel Sandler0c890492012-09-12 17:23:10 -07003635 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003636 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07003637 return this;
3638 }
3639
3640 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003641 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08003642 *
3643 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003644 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003645 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003646 * Useful when showing an elapsed time (like an ongoing phone call).
3647 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08003648 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07003649 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08003650 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003651 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003652 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07003653 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003654 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003655 @NonNull
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003656 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003657 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003658 return this;
3659 }
3660
3661 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08003662 * Sets the Chronometer to count down instead of counting up.
3663 *
3664 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
3665 * If it isn't set the chronometer will count up.
3666 *
3667 * @see #setUsesChronometer(boolean)
3668 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003669 @NonNull
Adrian Roos96b7e202016-05-17 13:50:38 -07003670 public Builder setChronometerCountDown(boolean countDown) {
3671 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003672 return this;
3673 }
3674
3675 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003676 * Set the small icon resource, which will be used to represent the notification in the
3677 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003678 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003679
3680 * The platform template for the expanded view will draw this icon in the left, unless a
3681 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3682 * icon will be moved to the right-hand side.
3683 *
3684
3685 * @param icon
3686 * A resource ID in the application's package of the drawable to use.
3687 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003688 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003689 @NonNull
Tor Norbye7b9c9122013-05-30 16:48:33 -07003690 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003691 return setSmallIcon(icon != 0
3692 ? Icon.createWithResource(mContext, icon)
3693 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003694 }
3695
Joe Onoratocb109a02011-01-18 17:57:41 -08003696 /**
3697 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3698 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3699 * LevelListDrawable}.
3700 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003701 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003702 * @param level The level to use for the icon.
3703 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003704 * @see Notification#icon
3705 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003706 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003707 @NonNull
Tor Norbye7b9c9122013-05-30 16:48:33 -07003708 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003709 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003710 return setSmallIcon(icon);
3711 }
3712
3713 /**
3714 * Set the small icon, which will be used to represent the notification in the
koprivadebd4ee2018-09-13 10:59:46 -07003715 * status bar and content view (unless overridden there by a
Dan Sandlerd63f9322015-05-06 15:18:49 -04003716 * {@link #setLargeIcon(Bitmap) large icon}).
3717 *
3718 * @param icon An Icon object to use.
3719 * @see Notification#icon
3720 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003721 @NonNull
Dan Sandlerd63f9322015-05-06 15:18:49 -04003722 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003723 mN.setSmallIcon(icon);
3724 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3725 mN.icon = icon.getResId();
3726 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003727 return this;
3728 }
3729
Joe Onoratocb109a02011-01-18 17:57:41 -08003730 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003731 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003732 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003733 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003734 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003735 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003736 return this;
3737 }
3738
Joe Onoratocb109a02011-01-18 17:57:41 -08003739 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003740 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003741 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003742 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003743 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003744 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003745 return this;
3746 }
3747
Joe Onoratocb109a02011-01-18 17:57:41 -08003748 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003749 * This provides some additional information that is displayed in the notification. No
3750 * guarantees are given where exactly it is displayed.
3751 *
3752 * <p>This information should only be provided if it provides an essential
3753 * benefit to the understanding of the notification. The more text you provide the
3754 * less readable it becomes. For example, an email client should only provide the account
3755 * name here if more than one email account has been added.</p>
3756 *
3757 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3758 * notification header area.
3759 *
3760 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3761 * this will be shown in the third line of text in the platform notification template.
3762 * You should not be using {@link #setProgress(int, int, boolean)} at the
3763 * same time on those versions; they occupy the same place.
3764 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003765 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003766 @NonNull
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003767 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003768 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003769 return this;
3770 }
3771
3772 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003773 * Provides text that will appear as a link to your application's settings.
3774 *
3775 * <p>This text does not appear within notification {@link Style templates} but may
3776 * appear when the user uses an affordance to learn more about the notification.
3777 * Additionally, this text will not appear unless you provide a valid link target by
3778 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3779 *
3780 * <p>This text is meant to be concise description about what the user can customize
3781 * when they click on this link. The recommended maximum length is 40 characters.
3782 * @param text
3783 * @return
3784 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003785 @NonNull
Julia Reynolds3aedded2017-03-31 14:42:09 -04003786 public Builder setSettingsText(CharSequence text) {
3787 mN.mSettingsText = safeCharSequence(text);
3788 return this;
3789 }
3790
3791 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003792 * Set the remote input history.
3793 *
3794 * This should be set to the most recent inputs that have been sent
3795 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3796 * longer relevant (e.g. for chat notifications once the other party has responded).
3797 *
3798 * The most recent input must be stored at the 0 index, the second most recent at the
3799 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3800 * and how much of each individual input is shown.
3801 *
3802 * <p>Note: The reply text will only be shown on notifications that have least one action
3803 * with a {@code RemoteInput}.</p>
3804 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003805 @NonNull
Adrian Roose458aa82015-12-08 16:17:19 -08003806 public Builder setRemoteInputHistory(CharSequence[] text) {
3807 if (text == null) {
3808 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3809 } else {
3810 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3811 CharSequence[] safe = new CharSequence[N];
3812 for (int i = 0; i < N; i++) {
3813 safe[i] = safeCharSequence(text[i]);
3814 }
3815 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3816 }
3817 return this;
3818 }
3819
3820 /**
Kenny Guya0f6de82018-04-06 16:20:16 +01003821 * Sets whether remote history entries view should have a spinner.
3822 * @hide
3823 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003824 @NonNull
Kenny Guya0f6de82018-04-06 16:20:16 +01003825 public Builder setShowRemoteInputSpinner(boolean showSpinner) {
3826 mN.extras.putBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER, showSpinner);
3827 return this;
3828 }
3829
3830 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +01003831 * Sets whether smart reply buttons should be hidden.
3832 * @hide
3833 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003834 @NonNull
Kenny Guy8cc15d22018-05-09 09:50:55 +01003835 public Builder setHideSmartReplies(boolean hideSmartReplies) {
3836 mN.extras.putBoolean(EXTRA_HIDE_SMART_REPLIES, hideSmartReplies);
3837 return this;
3838 }
3839
3840 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003841 * Sets the number of items this notification represents. May be displayed as a badge count
3842 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003843 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003844 @NonNull
Joe Onorato8595a3d2010-11-19 18:12:07 -08003845 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003846 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003847 return this;
3848 }
3849
Joe Onoratocb109a02011-01-18 17:57:41 -08003850 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003851 * A small piece of additional information pertaining to this notification.
3852 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003853 * The platform template will draw this on the last line of the notification, at the far
3854 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003855 *
3856 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3857 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3858 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003859 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003860 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003861 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003862 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003863 return this;
3864 }
3865
Joe Onoratocb109a02011-01-18 17:57:41 -08003866 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003867 * Set the progress this notification represents.
3868 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003869 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003870 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003871 @NonNull
Jeff Sharkey1c400132011-08-05 14:50:13 -07003872 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003873 mN.extras.putInt(EXTRA_PROGRESS, progress);
3874 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3875 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003876 return this;
3877 }
3878
3879 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003880 * Supply a custom RemoteViews to use instead of the platform template.
3881 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003882 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003883 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003884 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003885 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003886 return setCustomContentView(views);
3887 }
3888
3889 /**
3890 * Supply custom RemoteViews to use instead of the platform template.
3891 *
3892 * This will override the layout that would otherwise be constructed by this Builder
3893 * object.
3894 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003895 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003896 public Builder setCustomContentView(RemoteViews contentView) {
3897 mN.contentView = contentView;
3898 return this;
3899 }
3900
3901 /**
3902 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3903 *
3904 * This will override the expanded layout that would otherwise be constructed by this
3905 * Builder object.
3906 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003907 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003908 public Builder setCustomBigContentView(RemoteViews contentView) {
3909 mN.bigContentView = contentView;
3910 return this;
3911 }
3912
3913 /**
3914 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3915 *
3916 * This will override the heads-up layout that would otherwise be constructed by this
3917 * Builder object.
3918 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003919 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003920 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3921 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003922 return this;
3923 }
3924
Joe Onoratocb109a02011-01-18 17:57:41 -08003925 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003926 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3927 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003928 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3929 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3930 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003931 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003932 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003933 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003934 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003935 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003936 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003937 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003938 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003939 return this;
3940 }
3941
Joe Onoratocb109a02011-01-18 17:57:41 -08003942 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003943 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3944 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003945 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003946 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003947 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003948 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003949 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003950 return this;
3951 }
3952
Joe Onoratocb109a02011-01-18 17:57:41 -08003953 /**
3954 * An intent to launch instead of posting the notification to the status bar.
3955 * Only for use with extremely high-priority notifications demanding the user's
3956 * <strong>immediate</strong> attention, such as an incoming phone call or
3957 * alarm clock that the user has explicitly set to a particular time.
3958 * If this facility is used for something else, please give the user an option
3959 * to turn it off and use a normal notification, as this can be extremely
3960 * disruptive.
3961 *
Chris Wren47c20a12014-06-18 17:27:29 -04003962 * <p>
3963 * The system UI may choose to display a heads-up notification, instead of
3964 * launching this intent, while the user is using the device.
3965 * </p>
Julia Reynolds47fd15f2018-11-28 10:16:00 -05003966 * <p>Apps targeting {@link Build.VERSION_CODES#Q} and above will have to request
3967 * a permission ({@link android.Manifest.permission#USE_FULL_SCREEN_INTENT}) in order to
3968 * use full screen intents.</p>
Chris Wren47c20a12014-06-18 17:27:29 -04003969 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003970 * @param intent The pending intent to launch.
3971 * @param highPriority Passing true will cause this notification to be sent
3972 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003973 *
3974 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003975 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003976 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003977 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003978 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003979 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3980 return this;
3981 }
3982
Joe Onoratocb109a02011-01-18 17:57:41 -08003983 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003984 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003985 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003986 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003987 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003988 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003989 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003990 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003991 return this;
3992 }
3993
Joe Onoratocb109a02011-01-18 17:57:41 -08003994 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003995 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003996 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003997 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003998 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003999 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004000 setTicker(tickerText);
4001 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08004002 return this;
4003 }
4004
Joe Onoratocb109a02011-01-18 17:57:41 -08004005 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04004006 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004007 *
4008 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04004009 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
4010 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04004011 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004012 @NonNull
Dan Sandlerd63f9322015-05-06 15:18:49 -04004013 public Builder setLargeIcon(Bitmap b) {
4014 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
4015 }
4016
4017 /**
4018 * Add a large icon to the notification content view.
4019 *
4020 * In the platform template, this image will be shown on the left of the notification view
4021 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
4022 * badge atop the large icon).
4023 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004024 @NonNull
Dan Sandlerd63f9322015-05-06 15:18:49 -04004025 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004026 mN.mLargeIcon = icon;
4027 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08004028 return this;
4029 }
4030
Joe Onoratocb109a02011-01-18 17:57:41 -08004031 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004032 * Set the sound to play.
4033 *
John Spurlockc0650f022014-07-19 13:22:39 -04004034 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
4035 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004036 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004037 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08004038 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004039 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08004040 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004041 mN.sound = sound;
4042 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08004043 return this;
4044 }
4045
Joe Onoratocb109a02011-01-18 17:57:41 -08004046 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004047 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08004048 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004049 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
4050 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004051 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08004052 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07004053 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004054 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08004055 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004056 mN.sound = sound;
4057 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08004058 return this;
4059 }
4060
Joe Onoratocb109a02011-01-18 17:57:41 -08004061 /**
John Spurlockc0650f022014-07-19 13:22:39 -04004062 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
4063 * use during playback.
4064 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004065 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04004066 * @see Notification#sound
4067 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004068 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04004069 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004070 mN.sound = sound;
4071 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04004072 return this;
4073 }
4074
4075 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08004076 * Set the vibration pattern to use.
4077 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004078 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
4079 * <code>pattern</code> parameter.
4080 *
Chris Wren47c20a12014-06-18 17:27:29 -04004081 * <p>
4082 * A notification that vibrates is more likely to be presented as a heads-up notification.
4083 * </p>
4084 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004085 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004086 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08004087 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004088 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004089 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004090 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08004091 return this;
4092 }
4093
Joe Onoratocb109a02011-01-18 17:57:41 -08004094 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004095 * Set the desired color for the indicator LED on the device, as well as the
4096 * blink duty cycle (specified in milliseconds).
4097 *
4098
4099 * Not all devices will honor all (or even any) of these values.
4100 *
Julia Reynolds529e3322017-02-06 08:33:01 -05004101 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004102 * @see Notification#ledARGB
4103 * @see Notification#ledOnMS
4104 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08004105 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004106 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08004107 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004108 mN.ledARGB = argb;
4109 mN.ledOnMS = onMs;
4110 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004111 if (onMs != 0 || offMs != 0) {
4112 mN.flags |= FLAG_SHOW_LIGHTS;
4113 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004114 return this;
4115 }
4116
Joe Onoratocb109a02011-01-18 17:57:41 -08004117 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004118 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08004119 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004120
4121 * Ongoing notifications cannot be dismissed by the user, so your application or service
4122 * must take care of canceling them.
4123 *
4124
4125 * They are typically used to indicate a background task that the user is actively engaged
4126 * with (e.g., playing music) or is pending in some way and therefore occupying the device
4127 * (e.g., a file download, sync operation, active network connection).
4128 *
4129
4130 * @see Notification#FLAG_ONGOING_EVENT
Joe Onoratocb109a02011-01-18 17:57:41 -08004131 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004132 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08004133 public Builder setOngoing(boolean ongoing) {
4134 setFlag(FLAG_ONGOING_EVENT, ongoing);
4135 return this;
4136 }
4137
Joe Onoratocb109a02011-01-18 17:57:41 -08004138 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08004139 * Set whether this notification should be colorized. When set, the color set with
4140 * {@link #setColor(int)} will be used as the background color of this notification.
4141 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08004142 * This should only be used for high priority ongoing tasks like navigation, an ongoing
4143 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08004144 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07004145 * For most styles, the coloring will only be applied if the notification is for a
4146 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08004147 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07004148 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08004149 *
Aurimas Liutikas7f695332018-05-31 21:07:32 -07004150 * @see #setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08004151 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08004152 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004153 @NonNull
Selim Cinek7b9605b2017-01-19 17:36:00 -08004154 public Builder setColorized(boolean colorize) {
4155 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
4156 return this;
4157 }
4158
4159 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08004160 * Set this flag if you would only like the sound, vibrate
4161 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004162 *
4163 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08004164 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004165 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08004166 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
4167 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
4168 return this;
4169 }
4170
Joe Onoratocb109a02011-01-18 17:57:41 -08004171 /**
Julia Reynolds04499532016-09-13 14:04:53 -04004172 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004173 *
4174 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08004175 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004176 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08004177 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08004178 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08004179 return this;
4180 }
4181
Joe Onoratocb109a02011-01-18 17:57:41 -08004182 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08004183 * Set whether or not this notification should not bridge to other devices.
4184 *
4185 * <p>Some notifications can be bridged to other devices for remote display.
4186 * This hint can be set to recommend this notification not be bridged.
4187 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004188 @NonNull
Griff Hazendfcb0802014-02-11 12:00:00 -08004189 public Builder setLocalOnly(boolean localOnly) {
4190 setFlag(FLAG_LOCAL_ONLY, localOnly);
4191 return this;
4192 }
4193
4194 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004195 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08004196 * <p>
4197 * The value should be one or more of the following fields combined with
4198 * bitwise-or:
4199 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
4200 * <p>
4201 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004202 *
4203 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05004204 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004205 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08004206 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004207 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004208 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004209 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08004210 return this;
4211 }
4212
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004213 /**
4214 * Set the priority of this notification.
4215 *
4216 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004217 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004218 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004219 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07004220 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004221 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004222 return this;
4223 }
Joe Malin8d40d042012-11-05 11:36:40 -08004224
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004225 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04004226 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08004227 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04004228 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004229 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004230 @NonNull
John Spurlockfd7f1e02014-03-18 16:41:57 -04004231 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004232 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004233 return this;
4234 }
4235
4236 /**
Chris Wrendde75302014-03-26 17:24:15 -04004237 * Add a person that is relevant to this notification.
4238 *
Chris Wrene6c48932014-09-29 17:19:27 -04004239 * <P>
4240 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04004241 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4242 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4243 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04004244 * </P>
4245 *
4246 * <P>
4247 * The person should be specified by the {@code String} representation of a
4248 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
4249 * </P>
4250 *
4251 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
4252 * URIs. The path part of these URIs must exist in the contacts database, in the
4253 * appropriate column, or the reference will be discarded as invalid. Telephone schema
4254 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
Selim Cineke7238dd2017-12-14 17:48:32 -08004255 * It is also possible to provide a URI with the schema {@code name:} in order to uniquely
4256 * identify a person without an entry in the contacts database.
Chris Wrene6c48932014-09-29 17:19:27 -04004257 * </P>
4258 *
4259 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04004260 * @see Notification#EXTRA_PEOPLE
Selim Cineke7238dd2017-12-14 17:48:32 -08004261 * @deprecated use {@link #addPerson(Person)}
Chris Wrendde75302014-03-26 17:24:15 -04004262 */
Chris Wrene6c48932014-09-29 17:19:27 -04004263 public Builder addPerson(String uri) {
Selim Cinek9acd6732018-03-23 16:39:02 -07004264 addPerson(new Person.Builder().setUri(uri).build());
Selim Cineke7238dd2017-12-14 17:48:32 -08004265 return this;
4266 }
4267
4268 /**
4269 * Add a person that is relevant to this notification.
4270 *
4271 * <P>
4272 * Depending on user preferences, this annotation may allow the notification to pass
4273 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4274 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4275 * appear more prominently in the user interface.
4276 * </P>
4277 *
4278 * <P>
4279 * A person should usually contain a uri in order to benefit from the ranking boost.
4280 * However, even if no uri is provided, it's beneficial to provide other people in the
4281 * notification, such that listeners and voice only devices can announce and handle them
4282 * properly.
4283 * </P>
4284 *
4285 * @param person the person to add.
4286 * @see Notification#EXTRA_PEOPLE_LIST
4287 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004288 @NonNull
Selim Cineke7238dd2017-12-14 17:48:32 -08004289 public Builder addPerson(Person person) {
4290 mPersonList.add(person);
Chris Wrendde75302014-03-26 17:24:15 -04004291 return this;
4292 }
4293
4294 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004295 * Set this notification to be part of a group of notifications sharing the same key.
4296 * Grouped notifications may display in a cluster or stack on devices which
4297 * support such rendering.
4298 *
4299 * <p>To make this notification the summary for its group, also call
4300 * {@link #setGroupSummary}. A sort order can be specified for group members by using
4301 * {@link #setSortKey}.
4302 * @param groupKey The group key of the group.
4303 * @return this object for method chaining
4304 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004305 @NonNull
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004306 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004307 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004308 return this;
4309 }
4310
4311 /**
4312 * Set this notification to be the group summary for a group of notifications.
4313 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04004314 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
4315 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004316 * @param isGroupSummary Whether this notification should be a group summary.
4317 * @return this object for method chaining
4318 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004319 @NonNull
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004320 public Builder setGroupSummary(boolean isGroupSummary) {
4321 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
4322 return this;
4323 }
4324
4325 /**
4326 * Set a sort key that orders this notification among other notifications from the
4327 * same package. This can be useful if an external sort was already applied and an app
4328 * would like to preserve this. Notifications will be sorted lexicographically using this
4329 * value, although providing different priorities in addition to providing sort key may
4330 * cause this value to be ignored.
4331 *
4332 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07004333 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004334 *
4335 * @see String#compareTo(String)
4336 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004337 @NonNull
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004338 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004339 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004340 return this;
4341 }
4342
4343 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004344 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004345 *
Griff Hazen720042b2014-02-24 15:46:56 -08004346 * <p>Values within the Bundle will replace existing extras values in this Builder.
4347 *
4348 * @see Notification#extras
4349 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004350 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07004351 public Builder addExtras(Bundle extras) {
4352 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004353 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08004354 }
4355 return this;
4356 }
4357
4358 /**
4359 * Set metadata for this notification.
4360 *
4361 * <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 -04004362 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004363 * called.
4364 *
Griff Hazen720042b2014-02-24 15:46:56 -08004365 * <p>Replaces any existing extras values with those from the provided Bundle.
4366 * Use {@link #addExtras} to merge in metadata instead.
4367 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004368 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004369 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004370 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07004371 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004372 if (extras != null) {
4373 mUserExtras = extras;
4374 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004375 return this;
4376 }
4377
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004378 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004379 * Get the current metadata Bundle used by this notification Builder.
4380 *
4381 * <p>The returned Bundle is shared with this Builder.
4382 *
4383 * <p>The current contents of this Bundle are copied into the Notification each time
4384 * {@link #build()} is called.
4385 *
4386 * @see Notification#extras
4387 */
4388 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004389 return mUserExtras;
4390 }
4391
4392 private Bundle getAllExtras() {
4393 final Bundle saveExtras = (Bundle) mUserExtras.clone();
4394 saveExtras.putAll(mN.extras);
4395 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08004396 }
4397
4398 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004399 * Add an action to this notification. Actions are typically displayed by
4400 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004401 * <p>
4402 * Every action must have an icon (32dp square and matching the
4403 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4404 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4405 * <p>
4406 * A notification in its expanded form can display up to 3 actions, from left to right in
4407 * the order they were added. Actions will not be displayed when the notification is
4408 * collapsed, however, so be sure that any essential functions may be accessed by the user
4409 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004410 *
4411 * @param icon Resource ID of a drawable that represents the action.
4412 * @param title Text describing the action.
4413 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04004414 *
4415 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004416 */
Dan Sandler86647982015-05-13 23:41:13 -04004417 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004418 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004419 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004420 return this;
4421 }
4422
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004423 /**
Griff Hazen959591e2014-05-15 22:26:18 -07004424 * Add an action to this notification. Actions are typically displayed by
4425 * the system as a button adjacent to the notification content.
4426 * <p>
4427 * Every action must have an icon (32dp square and matching the
4428 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4429 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4430 * <p>
4431 * A notification in its expanded form can display up to 3 actions, from left to right in
4432 * the order they were added. Actions will not be displayed when the notification is
4433 * collapsed, however, so be sure that any essential functions may be accessed by the user
4434 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
4435 *
4436 * @param action The action to add.
4437 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004438 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07004439 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08004440 if (action != null) {
4441 mActions.add(action);
4442 }
Griff Hazen959591e2014-05-15 22:26:18 -07004443 return this;
4444 }
4445
4446 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004447 * Alter the complete list of actions attached to this notification.
4448 * @see #addAction(Action).
4449 *
4450 * @param actions
4451 * @return
4452 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004453 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004454 public Builder setActions(Action... actions) {
4455 mActions.clear();
4456 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08004457 if (actions[i] != null) {
4458 mActions.add(actions[i]);
4459 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004460 }
4461 return this;
4462 }
4463
4464 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004465 * Add a rich notification style to be applied at build time.
4466 *
4467 * @param style Object responsible for modifying the notification style.
4468 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004469 @NonNull
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004470 public Builder setStyle(Style style) {
4471 if (mStyle != style) {
4472 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004473 if (mStyle != null) {
4474 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004475 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
4476 } else {
4477 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004478 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004479 }
4480 return this;
4481 }
4482
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004483 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05004484 * Returns the style set by {@link #setStyle(Style)}.
4485 */
4486 public Style getStyle() {
4487 return mStyle;
4488 }
4489
4490 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004491 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07004492 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004493 * @return The same Builder.
4494 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004495 @NonNull
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06004496 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004497 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004498 return this;
4499 }
4500
4501 /**
4502 * Supply a replacement Notification whose contents should be shown in insecure contexts
4503 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
4504 * @param n A replacement notification, presumably with some or all info redacted.
4505 * @return The same Builder.
4506 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004507 @NonNull
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004508 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004509 if (n != null) {
4510 mN.publicVersion = new Notification();
4511 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
4512 } else {
4513 mN.publicVersion = null;
4514 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004515 return this;
4516 }
4517
Griff Hazenb720abe2014-05-20 13:15:30 -07004518 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004519 * Apply an extender to this notification builder. Extenders may be used to add
4520 * metadata or change options on this builder.
4521 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004522 @NonNull
Griff Hazen61a9e862014-05-22 16:05:19 -07004523 public Builder extend(Extender extender) {
4524 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004525 return this;
4526 }
4527
Dan Sandler4e787062015-06-17 15:09:48 -04004528 /**
4529 * @hide
4530 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004531 @NonNull
Julia Reynoldse46bb372016-03-17 11:05:58 -04004532 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08004533 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004534 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004535 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004536 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004537 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04004538 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08004539 }
4540
Dan Sandler26e81cf2014-05-06 10:01:27 -04004541 /**
4542 * Sets {@link Notification#color}.
4543 *
4544 * @param argb The accent color to use
4545 *
4546 * @return The same Builder.
4547 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004548 @NonNull
Tor Norbye80756e32015-03-02 09:39:27 -08004549 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004550 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004551 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04004552 return this;
4553 }
4554
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004555 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04004556 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
4557 // This user can never be a badged profile,
4558 // and also includes USER_ALL system notifications.
4559 return null;
4560 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02004561 // Note: This assumes that the current user can read the profile badge of the
4562 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08004563 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05004564 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004565 }
4566
4567 private Bitmap getProfileBadge() {
4568 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01004569 if (badge == null) {
4570 return null;
4571 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004572 final int size = mContext.getResources().getDimensionPixelSize(
4573 R.dimen.notification_badge_size);
4574 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004575 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004576 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004577 badge.draw(canvas);
4578 return bitmap;
4579 }
4580
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004581 private void bindProfileBadge(RemoteViews contentView, StandardTemplateParams p) {
Kenny Guy98193ea2014-07-24 19:54:37 +01004582 Bitmap profileBadge = getProfileBadge();
4583
Kenny Guy98193ea2014-07-24 19:54:37 +01004584 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004585 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
4586 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004587 if (isColorized(p)) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004588 contentView.setDrawableTint(R.id.profile_badge, false,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004589 getPrimaryTextColor(p), PorterDuff.Mode.SRC_ATOP);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004590 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004591 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004592 }
4593
Gus Prevasbae5ba32018-11-28 15:45:13 -05004594 private void bindAlertedIcon(RemoteViews contentView, StandardTemplateParams p) {
4595 contentView.setDrawableTint(
4596 R.id.alerted_icon,
4597 false /* targetBackground */,
4598 getNeutralColor(p),
4599 PorterDuff.Mode.SRC_ATOP);
4600 }
4601
Julia Reynoldsfc640012018-02-21 12:25:27 -05004602 /**
4603 * @hide
4604 */
4605 public boolean usesStandardHeader() {
4606 if (mN.mUsesStandardHeader) {
4607 return true;
4608 }
4609 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.N) {
4610 if (mN.contentView == null && mN.bigContentView == null) {
4611 return true;
4612 }
4613 }
4614 boolean contentViewUsesHeader = mN.contentView == null
4615 || STANDARD_LAYOUTS.contains(mN.contentView.getLayoutId());
4616 boolean bigContentViewUsesHeader = mN.bigContentView == null
4617 || STANDARD_LAYOUTS.contains(mN.bigContentView.getLayoutId());
4618 return contentViewUsesHeader && bigContentViewUsesHeader;
4619 }
4620
Christoph Studerfe718432014-09-01 18:21:18 +02004621 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004622 resetNotificationHeader(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02004623 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08004624 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004625 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08004626 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004627 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08004628 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08004629 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004630 }
4631
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004632 /**
4633 * Resets the notification header to its original state
4634 */
4635 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07004636 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
4637 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08004638 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004639 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02004640 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004641 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07004642 contentView.setTextViewText(R.id.header_text, null);
Selim Cinekafeed292017-12-12 17:32:44 -08004643 contentView.setViewVisibility(R.id.header_text_secondary, View.GONE);
4644 contentView.setTextViewText(R.id.header_text_secondary, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004645 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinekafeed292017-12-12 17:32:44 -08004646 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004647 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004648 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004649 contentView.setImageViewIcon(R.id.profile_badge, null);
4650 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Gus Prevasa3226492018-10-23 11:10:09 -04004651 contentView.setViewVisibility(R.id.alerted_icon, View.GONE);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004652 mN.mUsesStandardHeader = false;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004653 }
4654
Selim Cinek384804b2018-04-18 14:31:07 +08004655 private RemoteViews applyStandardTemplate(int resId, TemplateBindResult result) {
4656 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this),
4657 result);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004658 }
4659
Selim Cinek384804b2018-04-18 14:31:07 +08004660 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p,
4661 TemplateBindResult result) {
Kenny Guy77320062014-08-27 21:37:15 +01004662 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04004663
Christoph Studerfe718432014-09-01 18:21:18 +02004664 resetStandardTemplate(contentView);
4665
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004666 final Bundle ex = mN.extras;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004667 updateBackgroundColor(contentView, p);
4668 bindNotificationHeader(contentView, p);
Selim Cinek384804b2018-04-18 14:31:07 +08004669 bindLargeIconAndReply(contentView, p, result);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004670 boolean showProgress = handleProgressBar(contentView, ex, p);
Adrian Roos70d7aa32017-01-11 15:39:06 -08004671 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08004672 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004673 contentView.setTextViewText(R.id.title, processTextSpans(p.title));
Lucas Dupin00be88f2019-01-03 17:50:52 -08004674 setTextViewColorPrimary(contentView, R.id.title, p);
Selim Cinek954cc232016-05-20 13:29:23 -07004675 contentView.setViewLayoutWidth(R.id.title, showProgress
4676 ? ViewGroup.LayoutParams.WRAP_CONTENT
4677 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08004678 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08004679 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08004680 int textId = showProgress ? com.android.internal.R.id.text_line_1
4681 : com.android.internal.R.id.text;
Selim Cinek48f66b72017-08-18 16:17:51 -07004682 contentView.setTextViewText(textId, processTextSpans(p.text));
Lucas Dupin00be88f2019-01-03 17:50:52 -08004683 setTextViewColorSecondary(contentView, textId, p);
Selim Cinek41598732016-01-11 16:58:37 -08004684 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08004685 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08004686
Selim Cinek279fa862016-06-14 10:57:25 -07004687 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004688
Selim Cinek29603462015-11-17 19:04:39 -08004689 return contentView;
4690 }
4691
Selim Cinek48f66b72017-08-18 16:17:51 -07004692 private CharSequence processTextSpans(CharSequence text) {
Lucas Dupind3c99322018-09-27 10:22:29 -07004693 if (hasForegroundColor() || mInNightMode) {
Lucas Dupina291d192018-06-07 13:59:42 -07004694 return ContrastColorUtil.clearColorSpans(text);
Selim Cinek48f66b72017-08-18 16:17:51 -07004695 }
4696 return text;
4697 }
4698
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004699 private void setTextViewColorPrimary(RemoteViews contentView, int id,
4700 StandardTemplateParams p) {
4701 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004702 contentView.setTextColor(id, mPrimaryTextColor);
4703 }
4704
Selim Cinek48f66b72017-08-18 16:17:51 -07004705 private boolean hasForegroundColor() {
4706 return mForegroundColor != COLOR_INVALID;
4707 }
4708
Selim Cinek389edcd2017-05-11 19:16:44 -07004709 /**
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004710 * Return the primary text color using the existing template params
Selim Cinek389edcd2017-05-11 19:16:44 -07004711 * @hide
4712 */
4713 @VisibleForTesting
4714 public int getPrimaryTextColor() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004715 return getPrimaryTextColor(mParams);
4716 }
4717
4718 /**
4719 * @param p the template params to inflate this with
4720 * @return the primary text color
4721 * @hide
4722 */
4723 @VisibleForTesting
4724 public int getPrimaryTextColor(StandardTemplateParams p) {
4725 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004726 return mPrimaryTextColor;
4727 }
4728
Selim Cinek389edcd2017-05-11 19:16:44 -07004729 /**
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004730 * Return the secondary text color using the existing template params
Selim Cinek389edcd2017-05-11 19:16:44 -07004731 * @hide
4732 */
4733 @VisibleForTesting
4734 public int getSecondaryTextColor() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004735 return getSecondaryTextColor(mParams);
4736 }
4737
4738 /**
4739 * @param p the template params to inflate this with
4740 * @return the secondary text color
4741 * @hide
4742 */
4743 @VisibleForTesting
4744 public int getSecondaryTextColor(StandardTemplateParams p) {
4745 ensureColors(p);
Selim Cinek389edcd2017-05-11 19:16:44 -07004746 return mSecondaryTextColor;
4747 }
4748
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004749 private void setTextViewColorSecondary(RemoteViews contentView, int id,
4750 StandardTemplateParams p) {
4751 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004752 contentView.setTextColor(id, mSecondaryTextColor);
4753 }
4754
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004755 private void ensureColors(StandardTemplateParams p) {
4756 int backgroundColor = getBackgroundColor(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004757 if (mPrimaryTextColor == COLOR_INVALID
4758 || mSecondaryTextColor == COLOR_INVALID
Selim Cinek7b9605b2017-01-19 17:36:00 -08004759 || mTextColorsAreForBackground != backgroundColor) {
4760 mTextColorsAreForBackground = backgroundColor;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004761 if (!hasForegroundColor() || !isColorized(p)) {
Lucas Dupina291d192018-06-07 13:59:42 -07004762 mPrimaryTextColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004763 backgroundColor, mInNightMode);
Lucas Dupina291d192018-06-07 13:59:42 -07004764 mSecondaryTextColor = ContrastColorUtil.resolveSecondaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004765 backgroundColor, mInNightMode);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004766 if (backgroundColor != COLOR_DEFAULT && isColorized(p)) {
Lucas Dupina291d192018-06-07 13:59:42 -07004767 mPrimaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004768 mPrimaryTextColor, backgroundColor, 4.5);
Lucas Dupina291d192018-06-07 13:59:42 -07004769 mSecondaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004770 mSecondaryTextColor, backgroundColor, 4.5);
4771 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07004772 } else {
Lucas Dupina291d192018-06-07 13:59:42 -07004773 double backLum = ContrastColorUtil.calculateLuminance(backgroundColor);
4774 double textLum = ContrastColorUtil.calculateLuminance(mForegroundColor);
4775 double contrast = ContrastColorUtil.calculateContrast(mForegroundColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004776 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07004777 // We only respect the given colors if worst case Black or White still has
4778 // contrast
4779 boolean backgroundLight = backLum > textLum
4780 && satisfiesTextContrast(backgroundColor, Color.BLACK)
4781 || backLum <= textLum
4782 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004783 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07004784 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004785 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004786 mForegroundColor,
4787 backgroundColor,
4788 true /* findFG */,
4789 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004790 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004791 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004792 } else {
4793 mSecondaryTextColor =
Lucas Dupina291d192018-06-07 13:59:42 -07004794 ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004795 mForegroundColor,
4796 backgroundColor,
4797 true /* findFG */,
4798 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004799 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004800 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004801 }
4802 } else {
4803 mPrimaryTextColor = mForegroundColor;
Lucas Dupina291d192018-06-07 13:59:42 -07004804 mSecondaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004805 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4806 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Lucas Dupina291d192018-06-07 13:59:42 -07004807 if (ContrastColorUtil.calculateContrast(mSecondaryTextColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004808 backgroundColor) < 4.5f) {
4809 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07004810 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004811 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004812 mSecondaryTextColor,
4813 backgroundColor,
4814 true /* findFG */,
4815 4.5f);
4816 } else {
4817 mSecondaryTextColor
Lucas Dupina291d192018-06-07 13:59:42 -07004818 = ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004819 mSecondaryTextColor,
4820 backgroundColor,
4821 true /* findFG */,
4822 4.5f);
4823 }
Lucas Dupina291d192018-06-07 13:59:42 -07004824 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004825 mSecondaryTextColor, backgroundLight
4826 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4827 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004828 }
4829 }
4830 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004831 }
4832 }
4833
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004834 private void updateBackgroundColor(RemoteViews contentView,
4835 StandardTemplateParams p) {
4836 if (isColorized(p)) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004837 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004838 getBackgroundColor(p));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004839 } else {
4840 // Clear it!
4841 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4842 0);
4843 }
4844 }
4845
Selim Cinek860b6da2015-12-16 19:02:19 -08004846 /**
4847 * @param remoteView the remote view to update the minheight in
4848 * @param hasMinHeight does it have a mimHeight
4849 * @hide
4850 */
4851 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4852 int minHeight = 0;
4853 if (hasMinHeight) {
4854 // we need to set the minHeight of the notification
4855 minHeight = mContext.getResources().getDimensionPixelSize(
4856 com.android.internal.R.dimen.notification_min_content_height);
4857 }
4858 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4859 }
4860
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004861 private boolean handleProgressBar(RemoteViews contentView, Bundle ex,
4862 StandardTemplateParams p) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004863 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4864 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4865 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004866 if (p.hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004867 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004868 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004869 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004870 contentView.setProgressBackgroundTintList(
4871 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4872 R.color.notification_progress_background_color)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004873 if (getRawColor(p) != COLOR_DEFAULT) {
4874 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor(p));
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004875 contentView.setProgressTintList(R.id.progress, colorStateList);
4876 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004877 }
Selim Cinek29603462015-11-17 19:04:39 -08004878 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004879 } else {
4880 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004881 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004882 }
Joe Onorato561d3852010-11-20 18:09:34 -08004883 }
4884
Selim Cinek384804b2018-04-18 14:31:07 +08004885 private void bindLargeIconAndReply(RemoteViews contentView, StandardTemplateParams p,
4886 TemplateBindResult result) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004887 boolean largeIconShown = bindLargeIcon(contentView, p);
4888 boolean replyIconShown = bindReplyIcon(contentView, p);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004889 boolean iconContainerVisible = largeIconShown || replyIconShown;
Selim Cinek384804b2018-04-18 14:31:07 +08004890 contentView.setViewVisibility(R.id.right_icon_container,
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004891 iconContainerVisible ? View.VISIBLE : View.GONE);
Selim Cinek1c72fa02018-04-23 18:00:54 +08004892 int marginEnd = calculateMarginEnd(largeIconShown, replyIconShown);
4893 contentView.setViewLayoutMarginEnd(R.id.line1, marginEnd);
4894 contentView.setViewLayoutMarginEnd(R.id.text, marginEnd);
4895 contentView.setViewLayoutMarginEnd(R.id.progress, marginEnd);
Selim Cinek384804b2018-04-18 14:31:07 +08004896 if (result != null) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08004897 result.setIconMarginEnd(marginEnd);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004898 result.setRightIconContainerVisible(iconContainerVisible);
Selim Cinek384804b2018-04-18 14:31:07 +08004899 }
4900 }
4901
Selim Cinek1c72fa02018-04-23 18:00:54 +08004902 private int calculateMarginEnd(boolean largeIconShown, boolean replyIconShown) {
4903 int marginEnd = 0;
4904 int contentMargin = mContext.getResources().getDimensionPixelSize(
4905 R.dimen.notification_content_margin_end);
4906 int iconSize = mContext.getResources().getDimensionPixelSize(
4907 R.dimen.notification_right_icon_size);
4908 if (replyIconShown) {
4909 // The size of the reply icon
4910 marginEnd += iconSize;
4911
4912 int replyInset = mContext.getResources().getDimensionPixelSize(
4913 R.dimen.notification_reply_inset);
4914 // We're subtracting the inset of the reply icon to make sure it's
4915 // aligned nicely on the right, and remove it from the following padding
4916 marginEnd -= replyInset * 2;
4917 }
4918 if (largeIconShown) {
4919 // adding size of the right icon
4920 marginEnd += iconSize;
4921
4922 if (replyIconShown) {
4923 // We also add some padding to the reply icon if it's around
4924 marginEnd += contentMargin;
4925 }
4926 }
4927 if (replyIconShown || largeIconShown) {
4928 // The padding to the content
4929 marginEnd += contentMargin;
4930 }
4931 return marginEnd;
4932 }
4933
Selim Cinek384804b2018-04-18 14:31:07 +08004934 /**
4935 * Bind the large icon.
4936 * @return if the largeIcon is visible
4937 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004938 private boolean bindLargeIcon(RemoteViews contentView, StandardTemplateParams p) {
Selim Cinek279fa862016-06-14 10:57:25 -07004939 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4940 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4941 }
Lucas Dupin00be88f2019-01-03 17:50:52 -08004942 boolean showLargeIcon = mN.mLargeIcon != null && !p.hideLargeIcon;
Selim Cinek88188f22017-09-19 16:46:56 -07004943 if (showLargeIcon) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004944 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4945 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004946 processLargeLegacyIcon(mN.mLargeIcon, contentView, p);
Selim Cinek88188f22017-09-19 16:46:56 -07004947 }
Selim Cinek384804b2018-04-18 14:31:07 +08004948 return showLargeIcon;
4949 }
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004950
Selim Cinek384804b2018-04-18 14:31:07 +08004951 /**
4952 * Bind the reply icon.
4953 * @return if the reply icon is visible
4954 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004955 private boolean bindReplyIcon(RemoteViews contentView, StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08004956 boolean actionVisible = !p.hideReplyIcon;
Selim Cinek384804b2018-04-18 14:31:07 +08004957 Action action = null;
Selim Cinek88188f22017-09-19 16:46:56 -07004958 if (actionVisible) {
Selim Cinek384804b2018-04-18 14:31:07 +08004959 action = findReplyAction();
4960 actionVisible = action != null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004961 }
Selim Cinek384804b2018-04-18 14:31:07 +08004962 if (actionVisible) {
4963 contentView.setViewVisibility(R.id.reply_icon_action, View.VISIBLE);
4964 contentView.setDrawableTint(R.id.reply_icon_action,
4965 false /* targetBackground */,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004966 getNeutralColor(p),
Selim Cinek384804b2018-04-18 14:31:07 +08004967 PorterDuff.Mode.SRC_ATOP);
4968 contentView.setOnClickPendingIntent(R.id.reply_icon_action, action.actionIntent);
4969 contentView.setRemoteInputs(R.id.reply_icon_action, action.mRemoteInputs);
4970 } else {
4971 contentView.setRemoteInputs(R.id.reply_icon_action, null);
4972 }
4973 contentView.setViewVisibility(R.id.reply_icon_action,
4974 actionVisible ? View.VISIBLE : View.GONE);
4975 return actionVisible;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004976 }
4977
4978 private Action findReplyAction() {
4979 ArrayList<Action> actions = mActions;
4980 if (mOriginalActions != null) {
4981 actions = mOriginalActions;
4982 }
4983 int numActions = actions.size();
4984 for (int i = 0; i < numActions; i++) {
4985 Action action = actions.get(i);
4986 if (hasValidRemoteInput(action)) {
4987 return action;
4988 }
4989 }
4990 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004991 }
4992
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004993 private void bindNotificationHeader(RemoteViews contentView, StandardTemplateParams p) {
4994 bindSmallIcon(contentView, p);
4995 bindHeaderAppName(contentView, p);
Lucas Dupin00be88f2019-01-03 17:50:52 -08004996 bindHeaderText(contentView, p);
4997 bindHeaderTextSecondary(contentView, p);
4998 bindHeaderChronometerAndTime(contentView, p);
4999 bindProfileBadge(contentView, p);
5000 bindAlertedIcon(contentView, p);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005001 bindActivePermissions(contentView, p);
5002 bindExpandButton(contentView, p);
Julia Reynoldsfc640012018-02-21 12:25:27 -05005003 mN.mUsesStandardHeader = true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005004 }
5005
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005006 private void bindActivePermissions(RemoteViews contentView, StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005007 int color = getNeutralColor(p);
Julia Reynoldsb887b232018-04-10 16:38:08 -04005008 contentView.setDrawableTint(R.id.camera, false, color, PorterDuff.Mode.SRC_ATOP);
5009 contentView.setDrawableTint(R.id.mic, false, color, PorterDuff.Mode.SRC_ATOP);
5010 contentView.setDrawableTint(R.id.overlay, false, color, PorterDuff.Mode.SRC_ATOP);
5011 }
5012
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005013 private void bindExpandButton(RemoteViews contentView, StandardTemplateParams p) {
5014 int color = isColorized(p) ? getPrimaryTextColor(p) : getSecondaryTextColor(p);
Sunny Goyal5b153922017-09-21 21:00:36 -07005015 contentView.setDrawableTint(R.id.expand_button, false, color,
5016 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005017 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08005018 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005019 }
5020
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005021 private void bindHeaderChronometerAndTime(RemoteViews contentView,
5022 StandardTemplateParams p) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005023 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08005024 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005025 setTextViewColorSecondary(contentView, R.id.time_divider, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005026 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
5027 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
5028 contentView.setLong(R.id.chronometer, "setBase",
5029 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
5030 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07005031 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07005032 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005033 setTextViewColorSecondary(contentView, R.id.chronometer, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005034 } else {
5035 contentView.setViewVisibility(R.id.time, View.VISIBLE);
5036 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005037 setTextViewColorSecondary(contentView, R.id.time, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005038 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005039 } else {
5040 // We still want a time to be set but gone, such that we can show and hide it
5041 // on demand in case it's a child notification without anything in the header
5042 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005043 }
5044 }
5045
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005046 private void bindHeaderText(RemoteViews contentView, StandardTemplateParams p) {
5047 CharSequence summaryText = p.summaryText;
5048 if (summaryText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05005049 && mStyle.hasSummaryInHeader()) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005050 summaryText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05005051 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005052 if (summaryText == null
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07005053 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
5054 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005055 summaryText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07005056 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005057 if (summaryText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005058 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek48f66b72017-08-18 16:17:51 -07005059 contentView.setTextViewText(R.id.header_text, processTextSpans(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005060 processLegacyText(summaryText)));
5061 setTextViewColorSecondary(contentView, R.id.header_text, p);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07005062 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
5063 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005064 setTextViewColorSecondary(contentView, R.id.header_text_divider, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005065 }
5066 }
5067
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005068 private void bindHeaderTextSecondary(RemoteViews contentView, StandardTemplateParams p) {
5069 if (!TextUtils.isEmpty(p.headerTextSecondary)) {
Selim Cinekafeed292017-12-12 17:32:44 -08005070 contentView.setTextViewText(R.id.header_text_secondary, processTextSpans(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005071 processLegacyText(p.headerTextSecondary)));
5072 setTextViewColorSecondary(contentView, R.id.header_text_secondary, p);
Selim Cinekafeed292017-12-12 17:32:44 -08005073 contentView.setViewVisibility(R.id.header_text_secondary, View.VISIBLE);
5074 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005075 setTextViewColorSecondary(contentView, R.id.header_text_secondary_divider, p);
Selim Cinekafeed292017-12-12 17:32:44 -08005076 }
5077 }
5078
Adrian Rooseba05822016-04-22 17:09:27 -07005079 /**
5080 * @hide
5081 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005082 @UnsupportedAppUsage
Adrian Rooseba05822016-04-22 17:09:27 -07005083 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04005084 CharSequence name = null;
5085 final PackageManager pm = mContext.getPackageManager();
5086 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
5087 // only system packages which lump together a bunch of unrelated stuff
5088 // may substitute a different name to make the purpose of the
5089 // notification more clear. the correct package label should always
5090 // be accessible via SystemUI.
5091 final String pkg = mContext.getPackageName();
5092 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
5093 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
5094 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
5095 name = subName;
5096 } else {
5097 Log.w(TAG, "warning: pkg "
5098 + pkg + " attempting to substitute app name '" + subName
5099 + "' without holding perm "
5100 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
5101 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005102 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04005103 if (TextUtils.isEmpty(name)) {
5104 name = pm.getApplicationLabel(mContext.getApplicationInfo());
5105 }
5106 if (TextUtils.isEmpty(name)) {
5107 // still nothing?
5108 return null;
5109 }
5110
5111 return String.valueOf(name);
5112 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005113 private void bindHeaderAppName(RemoteViews contentView, StandardTemplateParams p) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04005114 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005115 if (isColorized(p)) {
5116 setTextViewColorPrimary(contentView, R.id.app_name_text, p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005117 } else {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005118 contentView.setTextColor(R.id.app_name_text, getSecondaryTextColor(p));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005119 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005120 }
5121
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005122 private boolean isColorized(StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005123 return p.allowColorization && mN.isColorized();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005124 }
5125
5126 private void bindSmallIcon(RemoteViews contentView, StandardTemplateParams p) {
Selim Cinek279fa862016-06-14 10:57:25 -07005127 if (mN.mSmallIcon == null && mN.icon != 0) {
5128 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
5129 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005130 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Sunny Goyal5b153922017-09-21 21:00:36 -07005131 contentView.setInt(R.id.icon, "setImageLevel", mN.iconLevel);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005132 processSmallIconColor(mN.mSmallIcon, contentView, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005133 }
5134
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005135 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005136 * @return true if the built notification will show the time or the chronometer; false
5137 * otherwise
5138 */
5139 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07005140 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005141 }
5142
Christoph Studerfe718432014-09-01 18:21:18 +02005143 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07005144 // actions_container is only reset when there are no actions to avoid focus issues with
5145 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02005146 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02005147 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08005148
5149 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
5150 big.setTextViewText(R.id.notification_material_reply_text_1, null);
Kenny Guya0f6de82018-04-06 16:20:16 +01005151 big.setViewVisibility(R.id.notification_material_reply_text_1_container, View.GONE);
5152 big.setViewVisibility(R.id.notification_material_reply_progress, View.GONE);
Adrian Roose458aa82015-12-08 16:17:19 -08005153
5154 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
5155 big.setTextViewText(R.id.notification_material_reply_text_2, null);
5156 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
5157 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07005158
Selim Cineked64a142018-02-06 18:06:01 -08005159 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
5160 R.dimen.notification_content_margin);
Christoph Studerfe718432014-09-01 18:21:18 +02005161 }
5162
Selim Cinek384804b2018-04-18 14:31:07 +08005163 private RemoteViews applyStandardTemplateWithActions(int layoutId,
5164 TemplateBindResult result) {
5165 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this),
5166 result);
Adrian Roos48d746a2016-04-12 14:57:28 -07005167 }
5168
Gustav Sennton1463d832018-11-06 16:12:48 +00005169 private static List<Notification.Action> filterOutContextualActions(
5170 List<Notification.Action> actions) {
5171 List<Notification.Action> nonContextualActions = new ArrayList<>();
5172 for (Notification.Action action : actions) {
Gustav Sennton005d7a02019-01-04 13:41:32 +00005173 if (!action.isContextual()) {
Gustav Sennton1463d832018-11-06 16:12:48 +00005174 nonContextualActions.add(action);
5175 }
5176 }
5177 return nonContextualActions;
5178 }
5179
Adrian Roos70d7aa32017-01-11 15:39:06 -08005180 private RemoteViews applyStandardTemplateWithActions(int layoutId,
Selim Cinek384804b2018-04-18 14:31:07 +08005181 StandardTemplateParams p, TemplateBindResult result) {
5182 RemoteViews big = applyStandardTemplate(layoutId, p, result);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005183
Christoph Studerfe718432014-09-01 18:21:18 +02005184 resetStandardTemplateWithActions(big);
5185
Adrian Roose458aa82015-12-08 16:17:19 -08005186 boolean validRemoteInput = false;
5187
Gustav Sennton1463d832018-11-06 16:12:48 +00005188 // In the UI contextual actions appear separately from the standard actions, so we
5189 // filter them out here.
5190 List<Notification.Action> nonContextualActions = filterOutContextualActions(mActions);
5191
5192 int N = nonContextualActions.size();
Lucas Dupin00be88f2019-01-03 17:50:52 -08005193 boolean emphazisedMode = mN.fullScreenIntent != null;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005194 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005195 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08005196 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005197 big.setViewVisibility(R.id.actions, View.VISIBLE);
Selim Cineked64a142018-02-06 18:06:01 -08005198 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Daniel Sandler8680bf82012-05-15 16:52:52 -04005199 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005200 for (int i=0; i<N; i++) {
Gustav Sennton1463d832018-11-06 16:12:48 +00005201 Action action = nonContextualActions.get(i);
5202
Selim Cinekffcc7cf2018-02-06 17:43:51 -08005203 boolean actionHasValidInput = hasValidRemoteInput(action);
5204 validRemoteInput |= actionHasValidInput;
Adrian Roose458aa82015-12-08 16:17:19 -08005205
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005206 final RemoteViews button = generateActionButton(action, emphazisedMode, p);
Selim Cinek396caca2018-04-10 17:46:46 -07005207 if (actionHasValidInput && !emphazisedMode) {
Selim Cinekffcc7cf2018-02-06 17:43:51 -08005208 // Clear the drawable
5209 button.setInt(R.id.action0, "setBackgroundResource", 0);
5210 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005211 big.addView(R.id.actions, button);
5212 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07005213 } else {
5214 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005215 }
Adrian Roose458aa82015-12-08 16:17:19 -08005216
5217 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Lucas Dupin00be88f2019-01-03 17:50:52 -08005218 if (validRemoteInput && replyText != null
Selim Cinekbee4e072018-05-21 22:06:43 -07005219 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])
5220 && p.maxRemoteInputHistory > 0) {
Kenny Guya0f6de82018-04-06 16:20:16 +01005221 boolean showSpinner = mN.extras.getBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER);
Adrian Roose458aa82015-12-08 16:17:19 -08005222 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Kenny Guya0f6de82018-04-06 16:20:16 +01005223 big.setViewVisibility(R.id.notification_material_reply_text_1_container,
5224 View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005225 big.setTextViewText(R.id.notification_material_reply_text_1,
5226 processTextSpans(replyText[0]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005227 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1, p);
Kenny Guya0f6de82018-04-06 16:20:16 +01005228 big.setViewVisibility(R.id.notification_material_reply_progress,
5229 showSpinner ? View.VISIBLE : View.GONE);
5230 big.setProgressIndeterminateTintList(
5231 R.id.notification_material_reply_progress,
5232 ColorStateList.valueOf(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005233 isColorized(p) ? getPrimaryTextColor(p) : resolveContrastColor(p)));
Adrian Roose458aa82015-12-08 16:17:19 -08005234
Selim Cinekbee4e072018-05-21 22:06:43 -07005235 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])
5236 && p.maxRemoteInputHistory > 1) {
Adrian Roose458aa82015-12-08 16:17:19 -08005237 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005238 big.setTextViewText(R.id.notification_material_reply_text_2,
5239 processTextSpans(replyText[1]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005240 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2, p);
Adrian Roose458aa82015-12-08 16:17:19 -08005241
Selim Cinekbee4e072018-05-21 22:06:43 -07005242 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])
5243 && p.maxRemoteInputHistory > 2) {
Adrian Roose458aa82015-12-08 16:17:19 -08005244 big.setViewVisibility(
5245 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005246 big.setTextViewText(R.id.notification_material_reply_text_3,
5247 processTextSpans(replyText[2]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005248 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3, p);
Adrian Roose458aa82015-12-08 16:17:19 -08005249 }
5250 }
5251 }
5252
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005253 return big;
5254 }
5255
Adrian Roose458aa82015-12-08 16:17:19 -08005256 private boolean hasValidRemoteInput(Action action) {
5257 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
5258 // Weird actions
5259 return false;
5260 }
5261
5262 RemoteInput[] remoteInputs = action.getRemoteInputs();
5263 if (remoteInputs == null) {
5264 return false;
5265 }
5266
5267 for (RemoteInput r : remoteInputs) {
5268 CharSequence[] choices = r.getChoices();
5269 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
5270 return true;
5271 }
5272 }
5273 return false;
5274 }
5275
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005276 /**
5277 * Construct a RemoteViews for the final 1U notification layout. In order:
5278 * 1. Custom contentView from the caller
5279 * 2. Style's proposed content view
5280 * 3. Standard template view
5281 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005282 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005283 return createContentView(false /* increasedheight */ );
5284 }
5285
5286 /**
5287 * Construct a RemoteViews for the smaller content view.
5288 *
5289 * @param increasedHeight true if this layout be created with an increased height. Some
5290 * styles may support showing more then just that basic 1U size
5291 * and the system may decide to render important notifications
5292 * slightly bigger even when collapsed.
5293 *
5294 * @hide
5295 */
5296 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005297 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005298 return mN.contentView;
5299 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005300 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005301 if (styleView != null) {
5302 return styleView;
5303 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005304 }
Selim Cinek384804b2018-04-18 14:31:07 +08005305 return applyStandardTemplate(getBaseLayoutResource(), null /* result */);
Joe Onorato46439ce2010-11-19 13:56:21 -08005306 }
5307
Selim Cineka7679b62017-05-10 16:33:25 -07005308 private boolean useExistingRemoteView() {
5309 return mStyle == null || (!mStyle.displayCustomViewInline()
5310 && !mRebuildStyledRemoteViews);
5311 }
5312
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005313 /**
5314 * Construct a RemoteViews for the final big notification layout.
5315 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005316 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05005317 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07005318 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005319 return mN.bigContentView;
5320 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05005321 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08005322 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005323 } else if (mActions.size() != 0) {
Selim Cinek384804b2018-04-18 14:31:07 +08005324 result = applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5325 null /* result */);
Selim Cinek850a8542015-11-11 11:48:36 -05005326 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08005327 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05005328 return result;
5329 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005330
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005331 /**
Selim Cinek414ad332017-02-24 19:06:12 -08005332 * Construct a RemoteViews for the final notification header only. This will not be
5333 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005334 *
5335 * @hide
5336 */
Lucas Dupin00be88f2019-01-03 17:50:52 -08005337 public RemoteViews makeNotificationHeader() {
5338 return makeNotificationHeader(mParams.reset().fillTextsFrom(this));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005339 }
5340
5341 /**
5342 * Construct a RemoteViews for the final notification header only. This will not be
5343 * colorized.
5344 *
5345 * @param p the template params to inflate this with
5346 */
5347 private RemoteViews makeNotificationHeader(StandardTemplateParams p) {
5348 // Headers on their own are never colorized
5349 p.disallowColorization();
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005350 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Lucas Dupin00be88f2019-01-03 17:50:52 -08005351 R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005352 resetNotificationHeader(header);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005353 bindNotificationHeader(header, p);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005354 return header;
5355 }
5356
Adrian Roos487374f2017-01-11 15:48:14 -08005357 /**
5358 * Construct a RemoteViews for the ambient version of the notification.
5359 *
5360 * @hide
5361 */
5362 public RemoteViews makeAmbientNotification() {
Selim Cinek0e069942019-01-24 16:09:41 -08005363 RemoteViews headsUpContentView = createHeadsUpContentView(false /* increasedHeight */);
5364 if (headsUpContentView != null) {
5365 return headsUpContentView;
5366 }
5367 return createContentView();
Adrian Roos487374f2017-01-11 15:48:14 -08005368 }
5369
Selim Cinek29603462015-11-17 19:04:39 -08005370 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005371 if (result != null) {
5372 result.setViewVisibility(R.id.text_line_1, View.GONE);
5373 }
Selim Cinek29603462015-11-17 19:04:39 -08005374 }
5375
Selim Cinek6743c0b2017-01-18 18:24:01 -08005376 /**
5377 * Adapt the Notification header if this view is used as an expanded view.
5378 *
5379 * @hide
5380 */
5381 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005382 if (result != null) {
5383 result.setBoolean(R.id.notification_header, "setExpanded", true);
5384 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005385 }
5386
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005387 /**
5388 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005389 *
5390 * @param increasedHeight true if this layout be created with an increased height. Some
5391 * styles may support showing more then just that basic 1U size
5392 * and the system may decide to render important notifications
5393 * slightly bigger even when collapsed.
5394 *
5395 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005396 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005397 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005398 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005399 return mN.headsUpContentView;
5400 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08005401 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
5402 if (styleView != null) {
5403 return styleView;
5404 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05005405 } else if (mActions.size() == 0) {
5406 return null;
5407 }
5408
Selim Cinekbee4e072018-05-21 22:06:43 -07005409 // We only want at most a single remote input history to be shown here, otherwise
5410 // the content would become squished.
5411 StandardTemplateParams p = mParams.reset().fillTextsFrom(this)
5412 .setMaxRemoteInputHistory(1);
5413 return applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5414 p,
5415 null /* result */);
Chris Wren8fd39ec2014-02-27 17:43:26 -05005416 }
5417
Selim Cinek624c02db2015-12-14 21:00:02 -08005418 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08005419 * Construct a RemoteViews for the final heads-up notification layout.
5420 */
5421 public RemoteViews createHeadsUpContentView() {
5422 return createHeadsUpContentView(false /* useIncreasedHeight */);
5423 }
5424
5425 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08005426 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5427 *
5428 * @hide
5429 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005430 @UnsupportedAppUsage
Selim Cinek624c02db2015-12-14 21:00:02 -08005431 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005432 return makePublicView(false /* ambient */);
5433 }
5434
5435 /**
5436 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5437 *
5438 * @hide
5439 */
5440 public RemoteViews makePublicAmbientNotification() {
5441 return makePublicView(true /* ambient */);
5442 }
5443
5444 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08005445 if (mN.publicVersion != null) {
5446 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005447 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08005448 }
5449 Bundle savedBundle = mN.extras;
5450 Style style = mStyle;
5451 mStyle = null;
5452 Icon largeIcon = mN.mLargeIcon;
5453 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07005454 Bitmap largeIconLegacy = mN.largeIcon;
5455 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005456 ArrayList<Action> actions = mActions;
5457 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08005458 Bundle publicExtras = new Bundle();
5459 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
5460 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
5461 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
5462 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07005463 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
5464 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cineked07b962018-04-30 14:39:35 -07005465 String appName = savedBundle.getString(EXTRA_SUBSTITUTE_APP_NAME);
5466 if (appName != null) {
5467 publicExtras.putString(EXTRA_SUBSTITUTE_APP_NAME, appName);
5468 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005469 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07005470 RemoteViews view;
Lucas Dupin00be88f2019-01-03 17:50:52 -08005471 view = makeNotificationHeader();
5472 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
Selim Cinek624c02db2015-12-14 21:00:02 -08005473 mN.extras = savedBundle;
5474 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07005475 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005476 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08005477 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005478 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08005479 }
5480
Selim Cinek6743c0b2017-01-18 18:24:01 -08005481 /**
5482 * Construct a content view for the display when low - priority
5483 *
5484 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
5485 * a new subtext is created consisting of the content of the
5486 * notification.
5487 * @hide
5488 */
5489 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005490 StandardTemplateParams p = mParams.reset()
5491 .forceDefaultColor()
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005492 .fillTextsFrom(this);
5493 if (!useRegularSubtext || TextUtils.isEmpty(mParams.summaryText)) {
5494 p.summaryText(createSummaryText());
Selim Cinek6743c0b2017-01-18 18:24:01 -08005495 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005496 RemoteViews header = makeNotificationHeader(p);
Selim Cinek1b554392017-02-28 17:22:49 -08005497 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08005498 return header;
5499 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005500
Selim Cinek6743c0b2017-01-18 18:24:01 -08005501 private CharSequence createSummaryText() {
5502 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
5503 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
5504 return titleText;
5505 }
5506 SpannableStringBuilder summary = new SpannableStringBuilder();
5507 if (titleText == null) {
5508 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
5509 }
5510 BidiFormatter bidi = BidiFormatter.getInstance();
5511 if (titleText != null) {
5512 summary.append(bidi.unicodeWrap(titleText));
5513 }
5514 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
5515 if (titleText != null && contentText != null) {
5516 summary.append(bidi.unicodeWrap(mContext.getText(
5517 R.string.notification_header_divider_symbol_with_spaces)));
5518 }
5519 if (contentText != null) {
5520 summary.append(bidi.unicodeWrap(contentText));
5521 }
5522 return summary;
5523 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05005524
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005525 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005526 StandardTemplateParams p) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04005527 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07005528 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005529 emphazisedMode ? getEmphasizedActionLayoutResource()
5530 : tombstone ? getActionTombstoneLayoutResource()
5531 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04005532 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04005533 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04005534 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005535 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005536 if (action.mRemoteInputs != null) {
5537 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
5538 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005539 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07005540 // change the background bgColor
Selim Cinek981962e2016-07-20 20:41:58 -07005541 CharSequence title = action.title;
5542 ColorStateList[] outResultColor = null;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005543 int background = resolveBackgroundColor(p);
Selim Cinek981962e2016-07-20 20:41:58 -07005544 if (isLegacy()) {
Lucas Dupina291d192018-06-07 13:59:42 -07005545 title = ContrastColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07005546 } else {
5547 outResultColor = new ColorStateList[1];
Selim Cinek396caca2018-04-10 17:46:46 -07005548 title = ensureColorSpanContrast(title, background, outResultColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005549 }
Selim Cinek48f66b72017-08-18 16:17:51 -07005550 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005551 setTextViewColorPrimary(button, R.id.action0, p);
Selim Cinek396caca2018-04-10 17:46:46 -07005552 int rippleColor;
5553 boolean hasColorOverride = outResultColor != null && outResultColor[0] != null;
5554 if (hasColorOverride) {
5555 // There's a span spanning the full text, let's take it and use it as the
5556 // background color
5557 background = outResultColor[0].getDefaultColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005558 int textColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07005559 background, mInNightMode);
Selim Cinek396caca2018-04-10 17:46:46 -07005560 button.setTextColor(R.id.action0, textColor);
5561 rippleColor = textColor;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005562 } else if (getRawColor(p) != COLOR_DEFAULT && !isColorized(p)
5563 && mTintActionButtons) {
5564 rippleColor = resolveContrastColor(p);
Selim Cinek396caca2018-04-10 17:46:46 -07005565 button.setTextColor(R.id.action0, rippleColor);
5566 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005567 rippleColor = getPrimaryTextColor(p);
Selim Cinek981962e2016-07-20 20:41:58 -07005568 }
Selim Cinek396caca2018-04-10 17:46:46 -07005569 // We only want about 20% alpha for the ripple
5570 rippleColor = (rippleColor & 0x00ffffff) | 0x33000000;
5571 button.setColorStateList(R.id.action0, "setRippleColor",
5572 ColorStateList.valueOf(rippleColor));
5573 button.setColorStateList(R.id.action0, "setButtonBackground",
5574 ColorStateList.valueOf(background));
5575 button.setBoolean(R.id.action0, "setHasStroke", !hasColorOverride);
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005576 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07005577 button.setTextViewText(R.id.action0, processTextSpans(
5578 processLegacyText(action.title)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005579 if (isColorized(p)) {
5580 setTextViewColorPrimary(button, R.id.action0, p);
5581 } else if (getRawColor(p) != COLOR_DEFAULT && mTintActionButtons) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005582 button.setTextColor(R.id.action0, resolveContrastColor(p));
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005583 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005584 }
Tony Mak7d4b3a52018-11-27 17:29:36 +00005585 button.setIntTag(R.id.action0, R.id.notification_action_index_tag,
5586 mActions.indexOf(action));
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005587 return button;
5588 }
5589
Joe Onoratocb109a02011-01-18 17:57:41 -08005590 /**
Selim Cinek981962e2016-07-20 20:41:58 -07005591 * Ensures contrast on color spans against a background color. also returns the color of the
5592 * text if a span was found that spans over the whole text.
5593 *
5594 * @param charSequence the charSequence on which the spans are
5595 * @param background the background color to ensure the contrast against
5596 * @param outResultColor an array in which a color will be returned as the first element if
5597 * there exists a full length color span.
5598 * @return the contrasted charSequence
5599 */
5600 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
5601 ColorStateList[] outResultColor) {
5602 if (charSequence instanceof Spanned) {
5603 Spanned ss = (Spanned) charSequence;
5604 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
5605 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
5606 for (Object span : spans) {
5607 Object resultSpan = span;
5608 int spanStart = ss.getSpanStart(span);
5609 int spanEnd = ss.getSpanEnd(span);
5610 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
5611 if (resultSpan instanceof CharacterStyle) {
5612 resultSpan = ((CharacterStyle) span).getUnderlying();
5613 }
5614 if (resultSpan instanceof TextAppearanceSpan) {
5615 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
5616 ColorStateList textColor = originalSpan.getTextColor();
5617 if (textColor != null) {
5618 int[] colors = textColor.getColors();
5619 int[] newColors = new int[colors.length];
5620 for (int i = 0; i < newColors.length; i++) {
Lucas Dupina291d192018-06-07 13:59:42 -07005621 newColors[i] = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005622 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005623 }
5624 textColor = new ColorStateList(textColor.getStates().clone(),
5625 newColors);
Selim Cinek396caca2018-04-10 17:46:46 -07005626 if (fullLength) {
5627 outResultColor[0] = textColor;
5628 // Let's drop the color from the span
5629 textColor = null;
5630 }
Selim Cinek981962e2016-07-20 20:41:58 -07005631 resultSpan = new TextAppearanceSpan(
5632 originalSpan.getFamily(),
5633 originalSpan.getTextStyle(),
5634 originalSpan.getTextSize(),
5635 textColor,
5636 originalSpan.getLinkTextColor());
Selim Cinek981962e2016-07-20 20:41:58 -07005637 }
5638 } else if (resultSpan instanceof ForegroundColorSpan) {
5639 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
5640 int foregroundColor = originalSpan.getForegroundColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005641 foregroundColor = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005642 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005643 if (fullLength) {
5644 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
Selim Cinek396caca2018-04-10 17:46:46 -07005645 resultSpan = null;
5646 } else {
5647 resultSpan = new ForegroundColorSpan(foregroundColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005648 }
5649 } else {
5650 resultSpan = span;
5651 }
Selim Cinek396caca2018-04-10 17:46:46 -07005652 if (resultSpan != null) {
5653 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
5654 }
Selim Cinek981962e2016-07-20 20:41:58 -07005655 }
5656 return builder;
5657 }
5658 return charSequence;
5659 }
5660
5661 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005662 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07005663 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005664 */
5665 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005666 if (!mIsLegacyInitialized) {
5667 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
5668 < Build.VERSION_CODES.LOLLIPOP;
5669 mIsLegacyInitialized = true;
5670 }
5671 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005672 }
5673
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005674 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08005675 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
Lucas Dupin00be88f2019-01-03 17:50:52 -08005676 if (isAlreadyLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005677 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005678 } else {
5679 return charSequence;
5680 }
5681 }
5682
Dan Sandler26e81cf2014-05-06 10:01:27 -04005683 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005684 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04005685 */
Adrian Roos487374f2017-01-11 15:48:14 -08005686 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005687 StandardTemplateParams p) {
Selim Cinekea4bef72015-12-02 15:51:10 -08005688 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005689 int color;
Lucas Dupin00be88f2019-01-03 17:50:52 -08005690 if (isColorized(p)) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005691 color = getPrimaryTextColor(p);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005692 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005693 color = resolveContrastColor(p);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005694 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005695 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07005696 contentView.setDrawableTint(R.id.icon, false, color,
5697 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005698
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005699 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005700 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08005701 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005702 }
5703
Dan Sandler26e81cf2014-05-06 10:01:27 -04005704 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005705 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04005706 * if it's grayscale).
5707 */
5708 // TODO: also check bounds, transparency, that sort of thing.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005709 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView,
5710 StandardTemplateParams p) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04005711 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005712 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005713 // resolve color will fall back to the default when legacy
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005714 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(p),
Sunny Goyal5b153922017-09-21 21:00:36 -07005715 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01005716 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005717 }
5718
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05005719 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005720 if (mN.color != COLOR_DEFAULT) {
5721 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02005722 }
Jorim Jaggi74419312014-06-10 20:57:21 +02005723 }
5724
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005725 int resolveContrastColor(StandardTemplateParams p) {
5726 int rawColor = getRawColor(p);
5727 if (mCachedContrastColorIsFor == rawColor && mCachedContrastColor != COLOR_INVALID) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08005728 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005729 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005730
Selim Cinekac5f0272017-05-02 16:05:41 -07005731 int color;
Selim Cinekc7f5a822018-03-20 19:32:06 -07005732 int background = mContext.getColor(
5733 com.android.internal.R.color.notification_material_background_color);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005734 if (rawColor == COLOR_DEFAULT) {
5735 ensureColors(p);
Lucas Dupinf03e7522018-06-25 16:21:13 -07005736 color = ContrastColorUtil.resolveDefaultColor(mContext, background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005737 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005738 color = ContrastColorUtil.resolveContrastColor(mContext, rawColor,
Anthony Chenad4d1582017-04-10 16:07:58 -07005739 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005740 }
5741 if (Color.alpha(color) < 255) {
5742 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005743 color = ContrastColorUtil.compositeColors(color, background);
Selim Cinekac5f0272017-05-02 16:05:41 -07005744 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005745 mCachedContrastColorIsFor = rawColor;
Selim Cinekac5f0272017-05-02 16:05:41 -07005746 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005747 }
5748
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005749 /**
5750 * Return the raw color of this Notification, which doesn't necessarily satisfy contrast.
5751 *
5752 * @see #resolveContrastColor(StandardTemplateParams) for the contrasted color
5753 * @param p the template params to inflate this with
5754 */
5755 private int getRawColor(StandardTemplateParams p) {
5756 if (p.forceDefaultColor) {
5757 return COLOR_DEFAULT;
5758 }
5759 return mN.color;
5760 }
5761
Selim Cinek4717d862018-04-19 09:19:15 +08005762 int resolveNeutralColor() {
5763 if (mNeutralColor != COLOR_INVALID) {
5764 return mNeutralColor;
5765 }
5766 int background = mContext.getColor(
5767 com.android.internal.R.color.notification_material_background_color);
Lucas Dupinf03e7522018-06-25 16:21:13 -07005768 mNeutralColor = ContrastColorUtil.resolveDefaultColor(mContext, background,
5769 mInNightMode);
Selim Cinek4717d862018-04-19 09:19:15 +08005770 if (Color.alpha(mNeutralColor) < 255) {
5771 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005772 mNeutralColor = ContrastColorUtil.compositeColors(mNeutralColor, background);
Selim Cinek4717d862018-04-19 09:19:15 +08005773 }
5774 return mNeutralColor;
5775 }
5776
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005777 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005778 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005779 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08005780 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005781 @NonNull
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005782 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005783 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005784 mN.actions = new Action[mActions.size()];
5785 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005786 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005787 if (!mPersonList.isEmpty()) {
Selim Cineke7238dd2017-12-14 17:48:32 -08005788 mN.extras.putParcelableArrayList(EXTRA_PEOPLE_LIST, mPersonList);
Dan Sandler0bf2ed82013-12-21 23:33:41 -06005789 }
Selim Cinek247fa012016-02-18 09:50:48 -08005790 if (mN.bigContentView != null || mN.contentView != null
5791 || mN.headsUpContentView != null) {
5792 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
5793 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005794 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08005795 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005796
Julia Reynolds3b848122016-02-26 10:45:32 -05005797 /**
5798 * Creates a Builder from an existing notification so further changes can be made.
5799 * @param context The context for your application / activity.
5800 * @param n The notification to create a Builder from.
5801 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005802 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005803 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005804 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005805 ApplicationInfo applicationInfo = n.extras.getParcelable(
5806 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005807 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05005808 if (applicationInfo != null) {
5809 try {
5810 builderContext = context.createApplicationContext(applicationInfo,
5811 Context.CONTEXT_RESTRICTED);
5812 } catch (NameNotFoundException e) {
5813 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
5814 builderContext = context; // try with our context
5815 }
5816 } else {
5817 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02005818 }
5819
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005820 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005821 }
5822
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005823 /**
Gustav Sennton761884c2018-11-19 17:40:19 +00005824 * Determines whether the platform can generate contextual actions for a notification.
5825 * By default this is true.
5826 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005827 @NonNull
Gustav Sennton761884c2018-11-19 17:40:19 +00005828 public Builder setAllowSystemGeneratedContextualActions(boolean allowed) {
5829 mN.mAllowSystemGeneratedContextualActions = allowed;
5830 return this;
5831 }
5832
5833 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005834 * @deprecated Use {@link #build()} instead.
5835 */
5836 @Deprecated
5837 public Notification getNotification() {
5838 return build();
5839 }
5840
5841 /**
5842 * Combine all of the options that have been set and return a new {@link Notification}
5843 * object.
5844 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005845 @NonNull
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005846 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005847 // first, add any extras from the calling code
5848 if (mUserExtras != null) {
5849 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005850 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005851
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005852 mN.creationTime = System.currentTimeMillis();
5853
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005854 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05005855 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02005856
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005857 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005858
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005859 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02005860 mStyle.reduceImageSizes(mContext);
5861 mStyle.purgeResources();
Selim Cinek90343862018-02-01 11:07:11 -08005862 mStyle.validate(mContext);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005863 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005864 }
Selim Cinekd0426622017-07-11 13:19:59 +02005865 mN.reduceImageSizes(mContext);
5866
Adrian Roos5081c0d2016-02-26 16:04:19 -08005867 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07005868 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005869 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005870 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005871 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
5872 mN.contentView.getSequenceNumber());
5873 }
5874 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005875 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005876 if (mN.bigContentView != null) {
5877 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
5878 mN.bigContentView.getSequenceNumber());
5879 }
5880 }
5881 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005882 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005883 if (mN.headsUpContentView != null) {
5884 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
5885 mN.headsUpContentView.getSequenceNumber());
5886 }
5887 }
5888 }
5889
Julia Reynolds4c0c2022016-02-02 15:11:59 -05005890 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
5891 mN.flags |= FLAG_SHOW_LIGHTS;
5892 }
5893
Adrian Roosfb921842017-10-26 14:49:56 +02005894 mN.allPendingIntents = null;
5895
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005896 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005897 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005898
5899 /**
5900 * Apply this Builder to an existing {@link Notification} object.
5901 *
5902 * @hide
5903 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005904 @NonNull
5905 public Notification buildInto(@NonNull Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005906 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005907 return n;
5908 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005909
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005910 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005911 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005912 * change. Also removes extenders on low ram devices, as
5913 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005914 *
5915 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5916 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005917 * @hide
5918 */
Kristian Monsen05f34792018-04-09 10:27:16 +02005919 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam,
5920 Context context) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005921 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005922
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005923 // Only strip views for known Styles because we won't know how to
5924 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005925 if (!isLowRam
5926 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005927 && getNotificationStyleClass(templateClass) == null) {
5928 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005929 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005930
5931 // Only strip unmodified BuilderRemoteViews.
5932 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005933 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005934 n.contentView.getSequenceNumber();
5935 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005936 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005937 n.bigContentView.getSequenceNumber();
5938 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005939 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005940 n.headsUpContentView.getSequenceNumber();
5941
5942 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005943 if (!isLowRam
5944 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005945 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005946 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005947
5948 Notification clone = n.clone();
5949 if (stripContentView) {
5950 clone.contentView = null;
5951 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5952 }
5953 if (stripBigContentView) {
5954 clone.bigContentView = null;
5955 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5956 }
5957 if (stripHeadsUpContentView) {
5958 clone.headsUpContentView = null;
5959 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5960 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005961 if (isLowRam) {
Kristian Monsen05f34792018-04-09 10:27:16 +02005962 String[] allowedServices = context.getResources().getStringArray(
5963 R.array.config_allowedManagedServicesOnLowRamDevices);
5964 if (allowedServices.length == 0) {
5965 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5966 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5967 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5968 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005969 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005970 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005971 }
5972
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005973 @UnsupportedAppUsage
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005974 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005975 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005976 }
5977
5978 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005979 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005980 }
5981
5982 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005983 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005984 }
5985
5986 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005987 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005988 }
5989
5990 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005991 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005992 }
5993
Adrian Roosc1a80b02016-04-05 14:54:55 -07005994 private int getMessagingLayoutResource() {
5995 return R.layout.notification_template_material_messaging;
5996 }
5997
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005998 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005999 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006000 }
6001
Selim Cinek06e9e1f2016-07-08 17:14:16 -07006002 private int getEmphasizedActionLayoutResource() {
6003 return R.layout.notification_material_action_emphasized;
6004 }
6005
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006006 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07006007 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006008 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08006009
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006010 private int getBackgroundColor(StandardTemplateParams p) {
6011 if (isColorized(p)) {
6012 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : getRawColor(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08006013 } else {
Selim Cinekc7f5a822018-03-20 19:32:06 -07006014 return COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08006015 }
6016 }
6017
Selim Cinek396caca2018-04-10 17:46:46 -07006018 /**
Selim Cinek4717d862018-04-19 09:19:15 +08006019 * Gets a neutral color that can be used for icons or similar that should not stand out.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006020 * @param p the template params to inflate this with
Selim Cinek4717d862018-04-19 09:19:15 +08006021 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006022 private int getNeutralColor(StandardTemplateParams p) {
6023 if (isColorized(p)) {
6024 return getSecondaryTextColor(p);
Selim Cinek4717d862018-04-19 09:19:15 +08006025 } else {
6026 return resolveNeutralColor();
6027 }
6028 }
6029
6030 /**
Selim Cinek396caca2018-04-10 17:46:46 -07006031 * Same as getBackgroundColor but also resolved the default color to the background.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006032 * @param p the template params to inflate this with
Selim Cinek396caca2018-04-10 17:46:46 -07006033 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006034 private int resolveBackgroundColor(StandardTemplateParams p) {
6035 int backgroundColor = getBackgroundColor(p);
Selim Cinek396caca2018-04-10 17:46:46 -07006036 if (backgroundColor == COLOR_DEFAULT) {
6037 backgroundColor = mContext.getColor(
6038 com.android.internal.R.color.notification_material_background_color);
6039 }
6040 return backgroundColor;
6041 }
6042
Anthony Chenad4d1582017-04-10 16:07:58 -07006043 private boolean shouldTintActionButtons() {
6044 return mTintActionButtons;
6045 }
6046
Selim Cinek99104832017-01-25 14:47:33 -08006047 private boolean textColorsNeedInversion() {
6048 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
6049 return false;
6050 }
6051 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
6052 return targetSdkVersion > Build.VERSION_CODES.M
6053 && targetSdkVersion < Build.VERSION_CODES.O;
6054 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07006055
6056 /**
6057 * Set a color palette to be used as the background and textColors
6058 *
6059 * @param backgroundColor the color to be used as the background
6060 * @param foregroundColor the color to be used as the foreground
6061 *
6062 * @hide
6063 */
6064 public void setColorPalette(int backgroundColor, int foregroundColor) {
6065 mBackgroundColor = backgroundColor;
6066 mForegroundColor = foregroundColor;
6067 mTextColorsAreForBackground = COLOR_INVALID;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006068 ensureColors(mParams.reset().fillTextsFrom(this));
Selim Cinek5fb73f82017-04-20 16:55:38 -07006069 }
Selim Cinekac5f0272017-05-02 16:05:41 -07006070
6071 /**
Selim Cineka7679b62017-05-10 16:33:25 -07006072 * Forces all styled remoteViews to be built from scratch and not use any cached
6073 * RemoteViews.
6074 * This is needed for legacy apps that are baking in their remoteviews into the
6075 * notification.
6076 *
6077 * @hide
6078 */
6079 public void setRebuildStyledRemoteViews(boolean rebuild) {
6080 mRebuildStyledRemoteViews = rebuild;
6081 }
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006082
6083 /**
6084 * Get the text that should be displayed in the statusBar when heads upped. This is
6085 * usually just the app name, but may be different depending on the style.
6086 *
6087 * @param publicMode If true, return a text that is safe to display in public.
6088 *
6089 * @hide
6090 */
6091 public CharSequence getHeadsUpStatusBarText(boolean publicMode) {
6092 if (mStyle != null && !publicMode) {
6093 CharSequence text = mStyle.getHeadsUpStatusBarText();
6094 if (!TextUtils.isEmpty(text)) {
6095 return text;
6096 }
6097 }
6098 return loadHeaderAppName();
6099 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08006100 }
6101
6102 /**
Selim Cinekd0426622017-07-11 13:19:59 +02006103 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
6104 * remote views.
6105 *
6106 * @hide
6107 */
6108 void reduceImageSizes(Context context) {
6109 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
6110 return;
6111 }
Selim Cineka8cb1262017-08-15 16:53:44 -07006112 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006113 if (mLargeIcon != null || largeIcon != null) {
6114 Resources resources = context.getResources();
6115 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07006116 int maxWidth = resources.getDimensionPixelSize(isLowRam
6117 ? R.dimen.notification_right_icon_size_low_ram
6118 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006119 int maxHeight = maxWidth;
6120 if (MediaStyle.class.equals(style)
6121 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006122 maxHeight = resources.getDimensionPixelSize(isLowRam
6123 ? R.dimen.notification_media_image_max_height_low_ram
6124 : R.dimen.notification_media_image_max_height);
6125 maxWidth = resources.getDimensionPixelSize(isLowRam
6126 ? R.dimen.notification_media_image_max_width_low_ram
6127 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006128 }
6129 if (mLargeIcon != null) {
6130 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
6131 }
6132 if (largeIcon != null) {
6133 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
6134 }
6135 }
Selim Cineka8cb1262017-08-15 16:53:44 -07006136 reduceImageSizesForRemoteView(contentView, context, isLowRam);
6137 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
6138 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02006139 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
6140 }
6141
Selim Cineka8cb1262017-08-15 16:53:44 -07006142 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
6143 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02006144 if (remoteView != null) {
6145 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006146 int maxWidth = resources.getDimensionPixelSize(isLowRam
6147 ? R.dimen.notification_custom_view_max_image_width_low_ram
6148 : R.dimen.notification_custom_view_max_image_width);
6149 int maxHeight = resources.getDimensionPixelSize(isLowRam
6150 ? R.dimen.notification_custom_view_max_image_height_low_ram
6151 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02006152 remoteView.reduceImageSizes(maxWidth, maxHeight);
6153 }
6154 }
6155
6156 /**
Selim Cinek22714f12017-04-13 16:23:53 -07006157 * @return whether this notification is a foreground service notification
Gus Prevasb0c1a462018-11-05 11:06:15 -05006158 * @hide
Selim Cinek7b9605b2017-01-19 17:36:00 -08006159 */
Gus Prevasb0c1a462018-11-05 11:06:15 -05006160 public boolean isForegroundService() {
Selim Cinek22714f12017-04-13 16:23:53 -07006161 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08006162 }
6163
6164 /**
Selim Cinek99104832017-01-25 14:47:33 -08006165 * @return whether this notification has a media session attached
6166 * @hide
6167 */
6168 public boolean hasMediaSession() {
6169 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
6170 }
6171
6172 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006173 * @return the style class of this notification
6174 * @hide
6175 */
6176 public Class<? extends Notification.Style> getNotificationStyle() {
6177 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
6178
6179 if (!TextUtils.isEmpty(templateClass)) {
6180 return Notification.getNotificationStyleClass(templateClass);
6181 }
6182 return null;
6183 }
6184
6185 /**
Selim Cinek22714f12017-04-13 16:23:53 -07006186 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08006187 *
6188 * @hide
6189 */
6190 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07006191 if (isColorizedMedia()) {
6192 return true;
6193 }
Julia Reynolds4db59552017-06-30 13:34:01 -04006194 return extras.getBoolean(EXTRA_COLORIZED)
6195 && (hasColorizedPermission() || isForegroundService());
6196 }
6197
6198 /**
6199 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
6200 * permission. The permission is checked when a notification is enqueued.
6201 */
6202 private boolean hasColorizedPermission() {
6203 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07006204 }
6205
6206 /**
6207 * @return true if this notification is colorized and it is a media notification
6208 *
6209 * @hide
6210 */
6211 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08006212 Class<? extends Style> style = getNotificationStyle();
6213 if (MediaStyle.class.equals(style)) {
6214 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
6215 if ((colorized == null || colorized) && hasMediaSession()) {
6216 return true;
6217 }
6218 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6219 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
6220 return true;
6221 }
6222 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07006223 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006224 }
6225
Selim Cinek0847acd2017-04-24 19:48:29 -07006226
6227 /**
6228 * @return true if this is a media notification
6229 *
6230 * @hide
6231 */
6232 public boolean isMediaNotification() {
6233 Class<? extends Style> style = getNotificationStyle();
6234 if (MediaStyle.class.equals(style)) {
6235 return true;
6236 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6237 return true;
6238 }
6239 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006240 }
6241
Selim Cinek279fa862016-06-14 10:57:25 -07006242 private boolean hasLargeIcon() {
6243 return mLargeIcon != null || largeIcon != null;
6244 }
6245
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006246 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006247 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006248 * @hide
6249 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07006250 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006251 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
6252 }
6253
6254 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006255 * @return true if the notification will show a chronometer; false otherwise
6256 * @hide
6257 */
6258 public boolean showsChronometer() {
6259 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
6260 }
6261
6262 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04006263 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05006264 */
6265 @SystemApi
6266 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
6267 Class<? extends Style>[] classes = new Class[] {
6268 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
6269 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
6270 MessagingStyle.class };
6271 for (Class<? extends Style> innerClass : classes) {
6272 if (templateClass.equals(innerClass.getName())) {
6273 return innerClass;
6274 }
6275 }
6276 return null;
6277 }
6278
6279 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006280 * An object that can apply a rich notification style to a {@link Notification.Builder}
6281 * object.
6282 */
Griff Hazendfcb0802014-02-11 12:00:00 -08006283 public static abstract class Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07006284
6285 /**
6286 * The number of items allowed simulatanously in the remote input history.
6287 * @hide
6288 */
6289 static final int MAX_REMOTE_INPUT_HISTORY_LINES = 3;
Chris Wrend6297db2012-05-03 16:20:13 -04006290 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006291
6292 /**
6293 * @hide
6294 */
6295 protected CharSequence mSummaryText = null;
6296
6297 /**
6298 * @hide
6299 */
6300 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04006301
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006302 protected Builder mBuilder;
6303
Chris Wrend6297db2012-05-03 16:20:13 -04006304 /**
6305 * Overrides ContentTitle in the big form of the template.
6306 * This defaults to the value passed to setContentTitle().
6307 */
6308 protected void internalSetBigContentTitle(CharSequence title) {
6309 mBigContentTitle = title;
6310 }
6311
6312 /**
6313 * Set the first line of text after the detail section in the big form of the template.
6314 */
6315 protected void internalSetSummaryText(CharSequence cs) {
6316 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04006317 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04006318 }
6319
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006320 public void setBuilder(Builder builder) {
6321 if (mBuilder != builder) {
6322 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07006323 if (mBuilder != null) {
6324 mBuilder.setStyle(this);
6325 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006326 }
6327 }
6328
Chris Wrend6297db2012-05-03 16:20:13 -04006329 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006330 if (mBuilder == null) {
6331 throw new IllegalArgumentException("Style requires a valid Builder object");
6332 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006333 }
Chris Wrend6297db2012-05-03 16:20:13 -04006334
6335 protected RemoteViews getStandardView(int layoutId) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006336 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder);
6337 return getStandardView(layoutId, p, null);
Selim Cinek384804b2018-04-18 14:31:07 +08006338 }
6339
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006340
Selim Cinek384804b2018-04-18 14:31:07 +08006341 /**
6342 * Get the standard view for this style.
6343 *
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006344 * @param layoutId The layout id to use.
6345 * @param p the params for this inflation.
Selim Cinek384804b2018-04-18 14:31:07 +08006346 * @param result The result where template bind information is saved.
6347 * @return A remoteView for this style.
6348 * @hide
6349 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006350 protected RemoteViews getStandardView(int layoutId, StandardTemplateParams p,
6351 TemplateBindResult result) {
Chris Wrend6297db2012-05-03 16:20:13 -04006352 checkBuilder();
6353
6354 if (mBigContentTitle != null) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006355 p.title = mBigContentTitle;
Chris Wrend6297db2012-05-03 16:20:13 -04006356 }
6357
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006358 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId, p,
6359 result);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006360
Chris Wrend6297db2012-05-03 16:20:13 -04006361 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
6362 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04006363 } else {
6364 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04006365 }
6366
Chris Wrend6297db2012-05-03 16:20:13 -04006367 return contentView;
6368 }
6369
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006370 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006371 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006372 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08006373 *
6374 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006375 * @hide
6376 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08006377 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006378 return null;
6379 }
6380
6381 /**
6382 * Construct a Style-specific RemoteViews for the final big notification layout.
6383 * @hide
6384 */
6385 public RemoteViews makeBigContentView() {
6386 return null;
6387 }
6388
6389 /**
6390 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08006391 *
6392 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006393 * @hide
6394 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006395 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006396 return null;
6397 }
6398
6399 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006400 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006401 * @hide
6402 */
6403 public void addExtras(Bundle extras) {
6404 if (mSummaryTextSet) {
6405 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
6406 }
6407 if (mBigContentTitle != null) {
6408 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
6409 }
Chris Wren91ad5632013-06-05 15:05:57 -04006410 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006411 }
6412
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006413 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006414 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006415 * @hide
6416 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02006417 protected void restoreFromExtras(Bundle extras) {
6418 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
6419 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
6420 mSummaryTextSet = true;
6421 }
6422 if (extras.containsKey(EXTRA_TITLE_BIG)) {
6423 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
6424 }
6425 }
6426
6427
6428 /**
6429 * @hide
6430 */
6431 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006432 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006433 return wip;
6434 }
6435
Daniel Sandler0ec46202015-06-24 01:27:05 -04006436 /**
6437 * @hide
6438 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006439 public void purgeResources() {}
6440
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006441 /**
6442 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
6443 * attached to.
6444 *
6445 * @return the fully constructed Notification.
6446 */
6447 public Notification build() {
6448 checkBuilder();
6449 return mBuilder.build();
6450 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006451
6452 /**
6453 * @hide
6454 * @return true if the style positions the progress bar on the second line; false if the
6455 * style hides the progress bar
6456 */
6457 protected boolean hasProgress() {
6458 return true;
6459 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006460
6461 /**
6462 * @hide
6463 * @return Whether we should put the summary be put into the notification header
6464 */
6465 public boolean hasSummaryInHeader() {
6466 return true;
6467 }
Selim Cinek593610c2016-02-16 18:42:57 -08006468
6469 /**
6470 * @hide
6471 * @return Whether custom content views are displayed inline in the style
6472 */
6473 public boolean displayCustomViewInline() {
6474 return false;
6475 }
Selim Cinekd0426622017-07-11 13:19:59 +02006476
6477 /**
6478 * Reduces the image sizes contained in this style.
6479 *
6480 * @hide
6481 */
6482 public void reduceImageSizes(Context context) {
6483 }
Selim Cinek90343862018-02-01 11:07:11 -08006484
6485 /**
6486 * Validate that this style was properly composed. This is called at build time.
6487 * @hide
6488 */
6489 public void validate(Context context) {
6490 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006491
6492 /**
6493 * @hide
6494 */
6495 public abstract boolean areNotificationsVisiblyDifferent(Style other);
Selim Cinekc7f5a822018-03-20 19:32:06 -07006496
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006497 /**
koprivaa1a78482018-10-09 10:09:23 -07006498 * @return the text that should be displayed in the statusBar when heads-upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006499 * If {@code null} is returned, the default implementation will be used.
6500 *
6501 * @hide
6502 */
6503 public CharSequence getHeadsUpStatusBarText() {
6504 return null;
6505 }
Joe Onorato46439ce2010-11-19 13:56:21 -08006506 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006507
6508 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006509 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08006510 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006511 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006512 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006513 * Notification notif = new Notification.Builder(mContext)
6514 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
6515 * .setContentText(subject)
6516 * .setSmallIcon(R.drawable.new_post)
6517 * .setLargeIcon(aBitmap)
6518 * .setStyle(new Notification.BigPictureStyle()
6519 * .bigPicture(aBigBitmap))
6520 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006521 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006522 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006523 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006524 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006525 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006526 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006527 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006528 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006529
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006530 public BigPictureStyle() {
6531 }
6532
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006533 /**
6534 * @deprecated use {@code BigPictureStyle()}.
6535 */
6536 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006537 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006538 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006539 }
6540
Chris Wrend6297db2012-05-03 16:20:13 -04006541 /**
6542 * Overrides ContentTitle in the big form of the template.
6543 * This defaults to the value passed to setContentTitle().
6544 */
6545 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006546 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006547 return this;
6548 }
6549
6550 /**
6551 * Set the first line of text after the detail section in the big form of the template.
6552 */
6553 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006554 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006555 return this;
6556 }
6557
Chris Wren0bd664d2012-08-01 13:56:56 -04006558 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05006559 * @hide
6560 */
6561 public Bitmap getBigPicture() {
6562 return mPicture;
6563 }
6564
6565 /**
Chris Wren0bd664d2012-08-01 13:56:56 -04006566 * Provide the bitmap to be used as the payload for the BigPicture notification.
6567 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006568 public BigPictureStyle bigPicture(Bitmap b) {
6569 mPicture = b;
6570 return this;
6571 }
6572
Chris Wren3745a3d2012-05-22 15:11:52 -04006573 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04006574 * Override the large icon when the big notification is shown.
6575 */
6576 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04006577 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
6578 }
6579
6580 /**
6581 * Override the large icon when the big notification is shown.
6582 */
6583 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04006584 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006585 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006586 return this;
6587 }
6588
Riley Andrews0394a0c2015-11-03 23:36:52 -08006589 /** @hide */
6590 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
6591
Daniel Sandler0ec46202015-06-24 01:27:05 -04006592 /**
6593 * @hide
6594 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006595 @Override
6596 public void purgeResources() {
6597 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08006598 if (mPicture != null &&
6599 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08006600 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006601 mPicture = mPicture.createAshmemBitmap();
6602 }
6603 if (mBigLargeIcon != null) {
6604 mBigLargeIcon.convertToAshmem();
6605 }
6606 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01006607
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006608 /**
6609 * @hide
6610 */
Selim Cinekd0426622017-07-11 13:19:59 +02006611 @Override
6612 public void reduceImageSizes(Context context) {
6613 super.reduceImageSizes(context);
6614 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006615 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006616 if (mPicture != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006617 int maxPictureWidth = resources.getDimensionPixelSize(isLowRam
6618 ? R.dimen.notification_big_picture_max_height_low_ram
6619 : R.dimen.notification_big_picture_max_height);
6620 int maxPictureHeight = resources.getDimensionPixelSize(isLowRam
6621 ? R.dimen.notification_big_picture_max_width_low_ram
6622 : R.dimen.notification_big_picture_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006623 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
6624 }
6625 if (mBigLargeIcon != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006626 int rightIconSize = resources.getDimensionPixelSize(isLowRam
6627 ? R.dimen.notification_right_icon_size_low_ram
6628 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006629 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
6630 }
6631 }
6632
6633 /**
6634 * @hide
6635 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006636 public RemoteViews makeBigContentView() {
6637 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01006638 // This covers the following cases:
6639 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006640 // mN.mLargeIcon
6641 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04006642 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07006643 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006644 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006645 oldLargeIcon = mBuilder.mN.mLargeIcon;
6646 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006647 // The legacy largeIcon might not allow us to clear the image, as it's taken in
6648 // replacement if the other one is null. Because we're restoring these legacy icons
6649 // for old listeners, this is in general non-null.
6650 largeIconLegacy = mBuilder.mN.largeIcon;
6651 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006652 }
6653
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006654 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder);
Selim Cinek384804b2018-04-18 14:31:07 +08006655 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource(),
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006656 p, null /* result */);
Selim Cinek03d0d652015-11-13 13:18:09 -05006657 if (mSummaryTextSet) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006658 contentView.setTextViewText(R.id.text, mBuilder.processTextSpans(
6659 mBuilder.processLegacyText(mSummaryText)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006660 mBuilder.setTextViewColorSecondary(contentView, R.id.text, p);
Selim Cinekc848c3a2016-01-13 15:27:30 -08006661 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05006662 }
Selim Cinek279fa862016-06-14 10:57:25 -07006663 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08006664
Christoph Studer5c510ee2014-12-15 16:32:27 +01006665 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006666 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006667 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006668 }
6669
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006670 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006671 return contentView;
6672 }
6673
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006674 /**
6675 * @hide
6676 */
6677 public void addExtras(Bundle extras) {
6678 super.addExtras(extras);
6679
6680 if (mBigLargeIconSet) {
6681 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
6682 }
6683 extras.putParcelable(EXTRA_PICTURE, mPicture);
6684 }
6685
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006686 /**
6687 * @hide
6688 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006689 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02006690 protected void restoreFromExtras(Bundle extras) {
6691 super.restoreFromExtras(extras);
6692
6693 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01006694 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02006695 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04006696 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006697 mPicture = extras.getParcelable(EXTRA_PICTURE);
6698 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006699
6700 /**
6701 * @hide
6702 */
6703 @Override
6704 public boolean hasSummaryInHeader() {
6705 return false;
6706 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006707
6708 /**
6709 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006710 * Note that we aren't actually comparing the contents of the bitmaps here, so this
6711 * is only doing a cursory inspection. Bitmaps of equal size will appear the same.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006712 */
6713 @Override
6714 public boolean areNotificationsVisiblyDifferent(Style other) {
6715 if (other == null || getClass() != other.getClass()) {
6716 return true;
6717 }
6718 BigPictureStyle otherS = (BigPictureStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006719 return areBitmapsObviouslyDifferent(getBigPicture(), otherS.getBigPicture());
6720 }
6721
6722 private static boolean areBitmapsObviouslyDifferent(Bitmap a, Bitmap b) {
6723 if (a == b) {
6724 return false;
6725 }
6726 if (a == null || b == null) {
6727 return true;
6728 }
6729 return a.getWidth() != b.getWidth()
6730 || a.getHeight() != b.getHeight()
6731 || a.getConfig() != b.getConfig()
6732 || a.getGenerationId() != b.getGenerationId();
Julia Reynolds7217dc92018-03-07 12:12:09 -05006733 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006734 }
6735
6736 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006737 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08006738 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006739 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006740 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006741 * Notification notif = new Notification.Builder(mContext)
6742 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
6743 * .setContentText(subject)
6744 * .setSmallIcon(R.drawable.new_mail)
6745 * .setLargeIcon(aBitmap)
6746 * .setStyle(new Notification.BigTextStyle()
6747 * .bigText(aVeryLongString))
6748 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006749 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006750 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006751 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006752 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006753 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006754
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006755 private CharSequence mBigText;
6756
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006757 public BigTextStyle() {
6758 }
6759
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006760 /**
6761 * @deprecated use {@code BigTextStyle()}.
6762 */
6763 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006764 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006765 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006766 }
6767
Chris Wrend6297db2012-05-03 16:20:13 -04006768 /**
6769 * Overrides ContentTitle in the big form of the template.
6770 * This defaults to the value passed to setContentTitle().
6771 */
6772 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006773 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006774 return this;
6775 }
6776
6777 /**
6778 * Set the first line of text after the detail section in the big form of the template.
6779 */
6780 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006781 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006782 return this;
6783 }
6784
Chris Wren0bd664d2012-08-01 13:56:56 -04006785 /**
6786 * Provide the longer text to be displayed in the big form of the
6787 * template in place of the content text.
6788 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006789 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006790 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006791 return this;
6792 }
6793
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006794 /**
6795 * @hide
6796 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05006797 public CharSequence getBigText() {
6798 return mBigText;
6799 }
6800
6801 /**
6802 * @hide
6803 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006804 public void addExtras(Bundle extras) {
6805 super.addExtras(extras);
6806
Christoph Studer4600f9b2014-07-22 22:44:43 +02006807 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
6808 }
6809
6810 /**
6811 * @hide
6812 */
6813 @Override
6814 protected void restoreFromExtras(Bundle extras) {
6815 super.restoreFromExtras(extras);
6816
6817 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006818 }
6819
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006820 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006821 * @param increasedHeight true if this layout be created with an increased height.
6822 *
6823 * @hide
6824 */
6825 @Override
6826 public RemoteViews makeContentView(boolean increasedHeight) {
6827 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006828 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006829 mBuilder.mActions = new ArrayList<>();
6830 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006831 mBuilder.mActions = mBuilder.mOriginalActions;
6832 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006833 return remoteViews;
6834 }
6835 return super.makeContentView(increasedHeight);
6836 }
6837
6838 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006839 * @hide
6840 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006841 @Override
6842 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
6843 if (increasedHeight && mBuilder.mActions.size() > 0) {
6844 return makeBigContentView();
6845 }
6846 return super.makeHeadsUpContentView(increasedHeight);
6847 }
6848
6849 /**
6850 * @hide
6851 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006852 public RemoteViews makeBigContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006853 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder).text(null);
Selim Cinek384804b2018-04-18 14:31:07 +08006854 TemplateBindResult result = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006855 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource(), p,
6856 result);
Selim Cinek1c72fa02018-04-23 18:00:54 +08006857 contentView.setInt(R.id.big_text, "setImageEndMargin", result.getIconMarginEnd());
Joe Malin8d40d042012-11-05 11:36:40 -08006858
Selim Cinek3a2c4b92015-12-17 17:01:17 -08006859 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07006860 if (TextUtils.isEmpty(bigTextText)) {
6861 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
6862 // experience
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006863 bigTextText = mBuilder.processLegacyText(
6864 mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT));
Selim Cinek75998782016-04-26 10:39:17 -07006865 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006866 contentView.setTextViewText(R.id.big_text, mBuilder.processTextSpans(bigTextText));
6867 mBuilder.setTextViewColorSecondary(contentView, R.id.big_text, p);
6868 contentView.setViewVisibility(R.id.big_text,
6869 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08006870 contentView.setBoolean(R.id.big_text, "setHasImage",
6871 result.isRightIconContainerVisible());
Selim Cinek4fb12d32015-11-19 18:10:48 -08006872
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006873 return contentView;
6874 }
6875
Julia Reynolds7217dc92018-03-07 12:12:09 -05006876 /**
6877 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006878 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006879 */
6880 @Override
6881 public boolean areNotificationsVisiblyDifferent(Style other) {
6882 if (other == null || getClass() != other.getClass()) {
6883 return true;
6884 }
6885 BigTextStyle newS = (BigTextStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006886 return !Objects.equals(String.valueOf(getBigText()), String.valueOf(newS.getBigText()));
Julia Reynolds7217dc92018-03-07 12:12:09 -05006887 }
6888
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006889 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006890
6891 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006892 * Helper class for generating large-format notifications that include multiple back-and-forth
6893 * messages of varying types between any number of people.
6894 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006895 * <p>
Alex Hillsfc737de2016-03-23 17:33:02 -04006896 * If the platform does not provide large-format notifications, this method has no effect. The
6897 * user will always see the normal notification view.
Selim Cinekce8794f2018-05-23 16:46:05 -07006898 *
6899 * <p>
6900 * If the app is targeting Android P and above, it is required to use the {@link Person}
6901 * class in order to get an optimal rendering of the notification and its avatars. For
6902 * conversations involving multiple people, the app should also make sure that it marks the
6903 * conversation as a group with {@link #setGroupConversation(boolean)}.
6904 *
6905 * <p>
6906 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior.
6907 * Here's an example of how this may be used:
Alex Hillsfc737de2016-03-23 17:33:02 -04006908 * <pre class="prettyprint">
6909 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006910 * Person user = new Person.Builder().setIcon(userIcon).setName(userName).build();
6911 * MessagingStyle style = new MessagingStyle(user)
6912 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getPerson())
6913 * .addMessage(messages[2].getText(), messages[2].getTime(), messages[2].getPerson())
6914 * .setGroupConversation(hasMultiplePeople());
6915 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006916 * Notification noti = new Notification.Builder()
Selim Cinekce8794f2018-05-23 16:46:05 -07006917 * .setContentTitle(&quot;2 new messages with &quot; + sender.toString())
Alex Hillsfc737de2016-03-23 17:33:02 -04006918 * .setContentText(subject)
6919 * .setSmallIcon(R.drawable.new_message)
6920 * .setLargeIcon(aBitmap)
Selim Cinekce8794f2018-05-23 16:46:05 -07006921 * .setStyle(style)
Alex Hillsfc737de2016-03-23 17:33:02 -04006922 * .build();
6923 * </pre>
6924 */
6925 public static class MessagingStyle extends Style {
6926
6927 /**
6928 * The maximum number of messages that will be retained in the Notification itself (the
6929 * number displayed is up to the platform).
6930 */
6931 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
6932
Selim Cinekcb8b9852017-12-15 18:01:52 -08006933 @NonNull Person mUser;
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006934 @Nullable CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04006935 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08006936 List<Message> mHistoricMessages = new ArrayList<>();
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006937 boolean mIsGroupConversation;
Alex Hillsfc737de2016-03-23 17:33:02 -04006938
6939 MessagingStyle() {
6940 }
6941
6942 /**
Alex Hillsfd590442016-10-07 09:52:44 -04006943 * @param userDisplayName Required - the name to be displayed for any replies sent by the
6944 * user before the posting app reposts the notification with those messages after they've
6945 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04006946 * {@link #addMessage(Notification.MessagingStyle.Message)}
Selim Cinekcb8b9852017-12-15 18:01:52 -08006947 *
6948 * @deprecated use {@code MessagingStyle(Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006949 */
Alex Hillsfd590442016-10-07 09:52:44 -04006950 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Selim Cinek9acd6732018-03-23 16:39:02 -07006951 this(new Person.Builder().setName(userDisplayName).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006952 }
6953
6954 /**
6955 * @param user Required - The person displayed for any messages that are sent by the
6956 * user. Any messages added with {@link #addMessage(Notification.MessagingStyle.Message)}
6957 * who don't have a Person associated with it will be displayed as if they were sent
Selim Cinek90343862018-02-01 11:07:11 -08006958 * by this user. The user also needs to have a valid name associated with it, which will
6959 * be enforced starting in Android P.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006960 */
6961 public MessagingStyle(@NonNull Person user) {
6962 mUser = user;
Selim Cinek90343862018-02-01 11:07:11 -08006963 }
6964
6965 /**
6966 * Validate that this style was properly composed. This is called at build time.
6967 * @hide
6968 */
6969 @Override
6970 public void validate(Context context) {
6971 super.validate(context);
6972 if (context.getApplicationInfo().targetSdkVersion
6973 >= Build.VERSION_CODES.P && (mUser == null || mUser.getName() == null)) {
6974 throw new RuntimeException("User must be valid and have a name.");
Selim Cinekcb8b9852017-12-15 18:01:52 -08006975 }
6976 }
6977
6978 /**
koprivaa1a78482018-10-09 10:09:23 -07006979 * @return the text that should be displayed in the statusBar when heads upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006980 * If {@code null} is returned, the default implementation will be used.
6981 *
6982 * @hide
6983 */
6984 @Override
6985 public CharSequence getHeadsUpStatusBarText() {
6986 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
6987 ? super.mBigContentTitle
6988 : mConversationTitle;
6989 if (!TextUtils.isEmpty(conversationTitle) && !hasOnlyWhiteSpaceSenders()) {
6990 return conversationTitle;
6991 }
6992 return null;
6993 }
6994
6995 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08006996 * @return the user to be displayed for any replies sent by the user
6997 */
Selim Cinekeb53c222018-04-06 12:56:09 -07006998 @NonNull
Selim Cinekcb8b9852017-12-15 18:01:52 -08006999 public Person getUser() {
7000 return mUser;
Alex Hillsfc737de2016-03-23 17:33:02 -04007001 }
7002
7003 /**
7004 * Returns the name to be displayed for any replies sent by the user
Selim Cinekcb8b9852017-12-15 18:01:52 -08007005 *
7006 * @deprecated use {@link #getUser()} instead
Alex Hillsfc737de2016-03-23 17:33:02 -04007007 */
7008 public CharSequence getUserDisplayName() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007009 return mUser.getName();
Alex Hillsfc737de2016-03-23 17:33:02 -04007010 }
7011
7012 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007013 * Sets the title to be displayed on this conversation. May be set to {@code null}.
7014 *
Kodlee Yin14656422017-12-22 17:00:46 -08007015 * <p>This API's behavior was changed in SDK version {@link Build.VERSION_CODES#P}. If your
7016 * application's target version is less than {@link Build.VERSION_CODES#P}, setting a
7017 * conversation title to a non-null value will make {@link #isGroupConversation()} return
7018 * {@code true} and passing {@code null} will make it return {@code false}. In
7019 * {@link Build.VERSION_CODES#P} and beyond, use {@link #setGroupConversation(boolean)}
7020 * to set group conversation status.
7021 *
7022 * @param conversationTitle Title displayed for this conversation
7023 * @return this object for method chaining
Alex Hillsfc737de2016-03-23 17:33:02 -04007024 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007025 public MessagingStyle setConversationTitle(@Nullable CharSequence conversationTitle) {
Alex Hillsfc737de2016-03-23 17:33:02 -04007026 mConversationTitle = conversationTitle;
7027 return this;
7028 }
7029
7030 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007031 * Return the title to be displayed on this conversation. May return {@code null}.
Alex Hillsfc737de2016-03-23 17:33:02 -04007032 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007033 @Nullable
Alex Hillsfc737de2016-03-23 17:33:02 -04007034 public CharSequence getConversationTitle() {
7035 return mConversationTitle;
7036 }
7037
7038 /**
7039 * Adds a message for display by this notification. Convenience call for a simple
7040 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
7041 * @param text A {@link CharSequence} to be displayed as the message content
7042 * @param timestamp Time at which the message arrived
7043 * @param sender A {@link CharSequence} to be used for displaying the name of the
7044 * sender. Should be <code>null</code> for messages by the current user, in which case
7045 * the platform will insert {@link #getUserDisplayName()}.
7046 * Should be unique amongst all individuals in the conversation, and should be
7047 * consistent during re-posts of the notification.
7048 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07007049 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Alex Hillsfc737de2016-03-23 17:33:02 -04007050 *
7051 * @return this object for method chaining
Selim Cinekcb8b9852017-12-15 18:01:52 -08007052 *
7053 * @deprecated use {@link #addMessage(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04007054 */
7055 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007056 return addMessage(text, timestamp,
Selim Cinek9acd6732018-03-23 16:39:02 -07007057 sender == null ? null : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08007058 }
7059
7060 /**
7061 * Adds a message for display by this notification. Convenience call for a simple
7062 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
7063 * @param text A {@link CharSequence} to be displayed as the message content
7064 * @param timestamp Time at which the message arrived
7065 * @param sender The {@link Person} who sent the message.
7066 * Should be <code>null</code> for messages by the current user, in which case
7067 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7068 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07007069 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Selim Cinekcb8b9852017-12-15 18:01:52 -08007070 *
7071 * @return this object for method chaining
7072 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007073 public MessagingStyle addMessage(@NonNull CharSequence text, long timestamp,
7074 @Nullable Person sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08007075 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04007076 }
7077
7078 /**
7079 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08007080 *
7081 * <p>The messages should be added in chronologic order, i.e. the oldest first,
7082 * the newest last.
7083 *
Alex Hillsfc737de2016-03-23 17:33:02 -04007084 * @param message The {@link Message} to be displayed
7085 * @return this object for method chaining
7086 */
7087 public MessagingStyle addMessage(Message message) {
7088 mMessages.add(message);
7089 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
7090 mMessages.remove(0);
7091 }
7092 return this;
7093 }
7094
7095 /**
Adrian Roos437cd562017-01-18 15:47:03 -08007096 * Adds a {@link Message} for historic context in this notification.
7097 *
7098 * <p>Messages should be added as historic if they are not the main subject of the
7099 * notification but may give context to a conversation. The system may choose to present
7100 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
7101 *
7102 * <p>The messages should be added in chronologic order, i.e. the oldest first,
7103 * the newest last.
7104 *
7105 * @param message The historic {@link Message} to be added
7106 * @return this object for method chaining
7107 */
7108 public MessagingStyle addHistoricMessage(Message message) {
7109 mHistoricMessages.add(message);
7110 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
7111 mHistoricMessages.remove(0);
7112 }
7113 return this;
7114 }
7115
7116 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007117 * Gets the list of {@code Message} objects that represent the notification
7118 */
7119 public List<Message> getMessages() {
7120 return mMessages;
7121 }
7122
7123 /**
Adrian Roos437cd562017-01-18 15:47:03 -08007124 * Gets the list of historic {@code Message}s in the notification.
7125 */
7126 public List<Message> getHistoricMessages() {
7127 return mHistoricMessages;
7128 }
7129
7130 /**
Selim Cinekce8794f2018-05-23 16:46:05 -07007131 * Sets whether this conversation notification represents a group. If the app is targeting
7132 * Android P, this is required if the app wants to display the largeIcon set with
7133 * {@link Notification.Builder#setLargeIcon(Bitmap)}, otherwise it will be hidden.
Kodlee Yin14656422017-12-22 17:00:46 -08007134 *
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007135 * @param isGroupConversation {@code true} if the conversation represents a group,
7136 * {@code false} otherwise.
7137 * @return this object for method chaining
7138 */
7139 public MessagingStyle setGroupConversation(boolean isGroupConversation) {
7140 mIsGroupConversation = isGroupConversation;
7141 return this;
7142 }
7143
7144 /**
Kodlee Yin14656422017-12-22 17:00:46 -08007145 * Returns {@code true} if this notification represents a group conversation, otherwise
7146 * {@code false}.
7147 *
7148 * <p> If the application that generated this {@link MessagingStyle} targets an SDK version
7149 * less than {@link Build.VERSION_CODES#P}, this method becomes dependent on whether or
7150 * not the conversation title is set; returning {@code true} if the conversation title is
7151 * a non-null value, or {@code false} otherwise. From {@link Build.VERSION_CODES#P} forward,
7152 * this method returns what's set by {@link #setGroupConversation(boolean)} allowing for
7153 * named, non-group conversations.
7154 *
7155 * @see #setConversationTitle(CharSequence)
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007156 */
7157 public boolean isGroupConversation() {
Kodlee Yin14656422017-12-22 17:00:46 -08007158 // When target SDK version is < P, a non-null conversation title dictates if this is
7159 // as group conversation.
7160 if (mBuilder != null
7161 && mBuilder.mContext.getApplicationInfo().targetSdkVersion
7162 < Build.VERSION_CODES.P) {
7163 return mConversationTitle != null;
7164 }
7165
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007166 return mIsGroupConversation;
7167 }
7168
7169 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007170 * @hide
7171 */
7172 @Override
7173 public void addExtras(Bundle extras) {
7174 super.addExtras(extras);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007175 if (mUser != null) {
7176 // For legacy usages
7177 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUser.getName());
7178 extras.putParcelable(EXTRA_MESSAGING_PERSON, mUser);
Alex Hillsfc737de2016-03-23 17:33:02 -04007179 }
7180 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007181 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04007182 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007183 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
7184 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04007185 }
Adrian Roos437cd562017-01-18 15:47:03 -08007186 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
7187 Message.getBundleArrayForMessages(mHistoricMessages));
7188 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007189
7190 fixTitleAndTextExtras(extras);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007191 extras.putBoolean(EXTRA_IS_GROUP_CONVERSATION, mIsGroupConversation);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007192 }
7193
7194 private void fixTitleAndTextExtras(Bundle extras) {
7195 Message m = findLatestIncomingMessage();
7196 CharSequence text = (m == null) ? null : m.mText;
7197 CharSequence sender = m == null ? null
Selim Cinekcb8b9852017-12-15 18:01:52 -08007198 : m.mSender == null || TextUtils.isEmpty(m.mSender.getName())
7199 ? mUser.getName() : m.mSender.getName();
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007200 CharSequence title;
7201 if (!TextUtils.isEmpty(mConversationTitle)) {
7202 if (!TextUtils.isEmpty(sender)) {
7203 BidiFormatter bidi = BidiFormatter.getInstance();
7204 title = mBuilder.mContext.getString(
7205 com.android.internal.R.string.notification_messaging_title_template,
Selim Cinekcb8b9852017-12-15 18:01:52 -08007206 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(sender));
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007207 } else {
7208 title = mConversationTitle;
7209 }
7210 } else {
7211 title = sender;
7212 }
7213
7214 if (title != null) {
7215 extras.putCharSequence(EXTRA_TITLE, title);
7216 }
7217 if (text != null) {
7218 extras.putCharSequence(EXTRA_TEXT, text);
7219 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007220 }
7221
7222 /**
7223 * @hide
7224 */
7225 @Override
7226 protected void restoreFromExtras(Bundle extras) {
7227 super.restoreFromExtras(extras);
7228
Selim Cinekcb8b9852017-12-15 18:01:52 -08007229 mUser = extras.getParcelable(EXTRA_MESSAGING_PERSON);
7230 if (mUser == null) {
7231 CharSequence displayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
Selim Cinek9acd6732018-03-23 16:39:02 -07007232 mUser = new Person.Builder().setName(displayName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007233 }
Adrian Roos96b7e202016-05-17 13:50:38 -07007234 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08007235 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007236 mMessages = Message.getMessagesFromBundleArray(messages);
Adrian Roos437cd562017-01-18 15:47:03 -08007237 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007238 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007239 mIsGroupConversation = extras.getBoolean(EXTRA_IS_GROUP_CONVERSATION);
Alex Hillsfc737de2016-03-23 17:33:02 -04007240 }
7241
7242 /**
7243 * @hide
7244 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07007245 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007246 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07007247 mBuilder.mOriginalActions = mBuilder.mActions;
7248 mBuilder.mActions = new ArrayList<>();
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007249 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007250 false /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007251 mBuilder.mActions = mBuilder.mOriginalActions;
7252 mBuilder.mOriginalActions = null;
7253 return remoteViews;
Adrian Roosc1a80b02016-04-05 14:54:55 -07007254 }
7255
Julia Reynolds7217dc92018-03-07 12:12:09 -05007256 /**
7257 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04007258 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05007259 */
7260 @Override
7261 public boolean areNotificationsVisiblyDifferent(Style other) {
7262 if (other == null || getClass() != other.getClass()) {
7263 return true;
7264 }
7265 MessagingStyle newS = (MessagingStyle) other;
7266 List<MessagingStyle.Message> oldMs = getMessages();
7267 List<MessagingStyle.Message> newMs = newS.getMessages();
7268
Dan Sandler7d67bd42018-05-15 14:06:38 -04007269 if (oldMs == null || newMs == null) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007270 newMs = new ArrayList<>();
7271 }
7272
7273 int n = oldMs.size();
7274 if (n != newMs.size()) {
7275 return true;
7276 }
7277 for (int i = 0; i < n; i++) {
7278 MessagingStyle.Message oldM = oldMs.get(i);
7279 MessagingStyle.Message newM = newMs.get(i);
Dan Sandler7d67bd42018-05-15 14:06:38 -04007280 if (!Objects.equals(
7281 String.valueOf(oldM.getText()),
7282 String.valueOf(newM.getText()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007283 return true;
7284 }
7285 if (!Objects.equals(oldM.getDataUri(), newM.getDataUri())) {
7286 return true;
7287 }
Dan Sandler7d67bd42018-05-15 14:06:38 -04007288 String oldSender = String.valueOf(oldM.getSenderPerson() == null
7289 ? oldM.getSender()
7290 : oldM.getSenderPerson().getName());
7291 String newSender = String.valueOf(newM.getSenderPerson() == null
7292 ? newM.getSender()
7293 : newM.getSenderPerson().getName());
Julia Reynolds7217dc92018-03-07 12:12:09 -05007294 if (!Objects.equals(oldSender, newSender)) {
7295 return true;
7296 }
7297
7298 String oldKey = oldM.getSenderPerson() == null
7299 ? null : oldM.getSenderPerson().getKey();
7300 String newKey = newM.getSenderPerson() == null
7301 ? null : newM.getSenderPerson().getKey();
7302 if (!Objects.equals(oldKey, newKey)) {
7303 return true;
7304 }
7305 // Other fields (like timestamp) intentionally excluded
7306 }
7307 return false;
7308 }
7309
Adrian Roosc1a80b02016-04-05 14:54:55 -07007310 private Message findLatestIncomingMessage() {
Selim Cinek88188f22017-09-19 16:46:56 -07007311 return findLatestIncomingMessage(mMessages);
7312 }
7313
7314 /**
7315 * @hide
7316 */
7317 @Nullable
7318 public static Message findLatestIncomingMessage(
7319 List<Message> messages) {
7320 for (int i = messages.size() - 1; i >= 0; i--) {
7321 Message m = messages.get(i);
Adrian Roosc1a80b02016-04-05 14:54:55 -07007322 // Incoming messages have a non-empty sender.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007323 if (m.mSender != null && !TextUtils.isEmpty(m.mSender.getName())) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07007324 return m;
7325 }
7326 }
Selim Cinek88188f22017-09-19 16:46:56 -07007327 if (!messages.isEmpty()) {
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007328 // No incoming messages, fall back to outgoing message
Selim Cinek88188f22017-09-19 16:46:56 -07007329 return messages.get(messages.size() - 1);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007330 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07007331 return null;
7332 }
7333
7334 /**
7335 * @hide
7336 */
7337 @Override
7338 public RemoteViews makeBigContentView() {
Selim Cinek384804b2018-04-18 14:31:07 +08007339 return makeMessagingView(false /* displayImagesAtEnd */, true /* hideLargeIcon */);
Selim Cinekafeed292017-12-12 17:32:44 -08007340 }
7341
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007342 /**
7343 * Create a messaging layout.
7344 *
7345 * @param displayImagesAtEnd should images be displayed at the end of the content instead
7346 * of inline.
Selim Cinek384804b2018-04-18 14:31:07 +08007347 * @param hideRightIcons Should the reply affordance be shown at the end of the notification
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007348 * @return the created remoteView.
7349 */
Selim Cinekafeed292017-12-12 17:32:44 -08007350 @NonNull
Selim Cinek384804b2018-04-18 14:31:07 +08007351 private RemoteViews makeMessagingView(boolean displayImagesAtEnd, boolean hideRightIcons) {
Selim Cinek88188f22017-09-19 16:46:56 -07007352 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
Adrian Roosc1a80b02016-04-05 14:54:55 -07007353 ? super.mBigContentTitle
7354 : mConversationTitle;
Selim Cinekce8794f2018-05-23 16:46:05 -07007355 boolean atLeastP = mBuilder.mContext.getApplicationInfo().targetSdkVersion
7356 >= Build.VERSION_CODES.P;
7357 boolean isOneToOne;
Selim Cinek2dd3e722018-01-19 11:06:06 -08007358 CharSequence nameReplacement = null;
Selim Cinekce8794f2018-05-23 16:46:05 -07007359 Icon avatarReplacement = null;
7360 if (!atLeastP) {
7361 isOneToOne = TextUtils.isEmpty(conversationTitle);
7362 avatarReplacement = mBuilder.mN.mLargeIcon;
7363 if (hasOnlyWhiteSpaceSenders()) {
7364 isOneToOne = true;
7365 nameReplacement = conversationTitle;
7366 conversationTitle = null;
7367 }
7368 } else {
7369 isOneToOne = !isGroupConversation();
Adrian Roosb1f427c2016-05-26 12:27:15 -07007370 }
Selim Cinek384804b2018-04-18 14:31:07 +08007371 TemplateBindResult bindResult = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007372 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).title(
7373 conversationTitle).text(null)
7374 .hideLargeIcon(hideRightIcons || isOneToOne)
7375 .hideReplyIcon(hideRightIcons)
7376 .headerTextSecondary(conversationTitle);
Adrian Roos48d746a2016-04-12 14:57:28 -07007377 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07007378 mBuilder.getMessagingLayoutResource(),
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007379 p,
Selim Cinek384804b2018-04-18 14:31:07 +08007380 bindResult);
Selim Cinek88188f22017-09-19 16:46:56 -07007381 addExtras(mBuilder.mN.extras);
Selim Cinekafeed292017-12-12 17:32:44 -08007382 // also update the end margin if there is an image
Selim Cinek1c72fa02018-04-23 18:00:54 +08007383 contentView.setViewLayoutMarginEnd(R.id.notification_messaging,
7384 bindResult.getIconMarginEnd());
Selim Cinek88188f22017-09-19 16:46:56 -07007385 contentView.setInt(R.id.status_bar_latest_event_content, "setLayoutColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007386 mBuilder.isColorized(p) ? mBuilder.getPrimaryTextColor(p)
7387 : mBuilder.resolveContrastColor(p));
Kenny Guy14d035c2018-05-02 19:10:36 +01007388 contentView.setInt(R.id.status_bar_latest_event_content, "setSenderTextColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007389 mBuilder.getPrimaryTextColor(p));
Kenny Guy14d035c2018-05-02 19:10:36 +01007390 contentView.setInt(R.id.status_bar_latest_event_content, "setMessageTextColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007391 mBuilder.getSecondaryTextColor(p));
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007392 contentView.setBoolean(R.id.status_bar_latest_event_content, "setDisplayImagesAtEnd",
7393 displayImagesAtEnd);
Selim Cinekce8794f2018-05-23 16:46:05 -07007394 contentView.setIcon(R.id.status_bar_latest_event_content, "setAvatarReplacement",
7395 avatarReplacement);
Selim Cinek2dd3e722018-01-19 11:06:06 -08007396 contentView.setCharSequence(R.id.status_bar_latest_event_content, "setNameReplacement",
7397 nameReplacement);
Selim Cinek88188f22017-09-19 16:46:56 -07007398 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsOneToOne",
7399 isOneToOne);
7400 contentView.setBundle(R.id.status_bar_latest_event_content, "setData",
7401 mBuilder.mN.extras);
Alex Hillsfc737de2016-03-23 17:33:02 -04007402 return contentView;
7403 }
7404
Selim Cinekf7409db2017-10-24 16:17:14 -07007405 private boolean hasOnlyWhiteSpaceSenders() {
7406 for (int i = 0; i < mMessages.size(); i++) {
7407 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007408 Person sender = m.getSenderPerson();
7409 if (sender != null && !isWhiteSpace(sender.getName())) {
Selim Cinekf7409db2017-10-24 16:17:14 -07007410 return false;
7411 }
7412 }
7413 return true;
7414 }
7415
7416 private boolean isWhiteSpace(CharSequence sender) {
7417 if (TextUtils.isEmpty(sender)) {
7418 return true;
7419 }
7420 if (sender.toString().matches("^\\s*$")) {
7421 return true;
7422 }
7423 // Let's check if we only have 0 whitespace chars. Some apps did this as a workaround
7424 // For the presentation that we had.
7425 for (int i = 0; i < sender.length(); i++) {
7426 char c = sender.charAt(i);
7427 if (c != '\u200B') {
7428 return false;
7429 }
7430 }
7431 return true;
7432 }
7433
Selim Cinek88188f22017-09-19 16:46:56 -07007434 private CharSequence createConversationTitleFromMessages() {
7435 ArraySet<CharSequence> names = new ArraySet<>();
7436 for (int i = 0; i < mMessages.size(); i++) {
7437 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007438 Person sender = m.getSenderPerson();
Selim Cinek88188f22017-09-19 16:46:56 -07007439 if (sender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007440 names.add(sender.getName());
Selim Cinek88188f22017-09-19 16:46:56 -07007441 }
7442 }
7443 SpannableStringBuilder title = new SpannableStringBuilder();
7444 int size = names.size();
7445 for (int i = 0; i < size; i++) {
7446 CharSequence name = names.valueAt(i);
7447 if (!TextUtils.isEmpty(title)) {
7448 title.append(", ");
7449 }
7450 title.append(BidiFormatter.getInstance().unicodeWrap(name));
7451 }
7452 return title;
7453 }
7454
Adrian Roosdedd1df2016-04-26 16:38:47 -07007455 /**
7456 * @hide
7457 */
7458 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007459 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007460 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007461 true /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007462 remoteViews.setInt(R.id.notification_messaging, "setMaxDisplayedLines", 1);
7463 return remoteViews;
Adrian Roosdedd1df2016-04-26 16:38:47 -07007464 }
7465
Adrian Roosc1a80b02016-04-05 14:54:55 -07007466 private static TextAppearanceSpan makeFontColorSpan(int color) {
7467 return new TextAppearanceSpan(null, 0, 0,
7468 ColorStateList.valueOf(color), null);
7469 }
7470
Alex Hillsd9b04d92016-04-11 16:38:16 -04007471 public static final class Message {
Tony Mak09db2ea2018-06-27 18:12:48 +01007472 /** @hide */
7473 public static final String KEY_TEXT = "text";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007474 static final String KEY_TIMESTAMP = "time";
7475 static final String KEY_SENDER = "sender";
Selim Cinekcb8b9852017-12-15 18:01:52 -08007476 static final String KEY_SENDER_PERSON = "sender_person";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007477 static final String KEY_DATA_MIME_TYPE = "type";
7478 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08007479 static final String KEY_EXTRAS_BUNDLE = "extras";
Kenny Guya0f6de82018-04-06 16:20:16 +01007480 static final String KEY_REMOTE_INPUT_HISTORY = "remote_input_history";
Alex Hillsfc737de2016-03-23 17:33:02 -04007481
7482 private final CharSequence mText;
7483 private final long mTimestamp;
Selim Cinekcb8b9852017-12-15 18:01:52 -08007484 @Nullable
7485 private final Person mSender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007486 /** True if this message was generated from the extra
7487 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}
7488 */
7489 private final boolean mRemoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007490
Shane Brennan5a871862017-03-11 13:14:17 -08007491 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007492 private String mDataMimeType;
7493 private Uri mDataUri;
7494
7495 /**
7496 * Constructor
7497 * @param text A {@link CharSequence} to be displayed as the message content
7498 * @param timestamp Time at which the message arrived
7499 * @param sender A {@link CharSequence} to be used for displaying the name of the
7500 * sender. Should be <code>null</code> for messages by the current user, in which case
7501 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
7502 * Should be unique amongst all individuals in the conversation, and should be
7503 * consistent during re-posts of the notification.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007504 *
7505 * @deprecated use {@code Message(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04007506 */
7507 public Message(CharSequence text, long timestamp, CharSequence sender){
Selim Cinek9acd6732018-03-23 16:39:02 -07007508 this(text, timestamp, sender == null ? null
7509 : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08007510 }
7511
7512 /**
7513 * Constructor
7514 * @param text A {@link CharSequence} to be displayed as the message content
7515 * @param timestamp Time at which the message arrived
7516 * @param sender The {@link Person} who sent the message.
7517 * Should be <code>null</code> for messages by the current user, in which case
7518 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7519 * <p>
Selim Cinek9acd6732018-03-23 16:39:02 -07007520 * The person provided should contain an Icon, set with
7521 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7522 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7523 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7524 * to differentiate between the different users.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007525 * </p>
7526 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007527 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender) {
Kenny Guya0f6de82018-04-06 16:20:16 +01007528 this(text, timestamp, sender, false /* remoteHistory */);
7529 }
7530
7531 /**
7532 * Constructor
7533 * @param text A {@link CharSequence} to be displayed as the message content
7534 * @param timestamp Time at which the message arrived
7535 * @param sender The {@link Person} who sent the message.
7536 * Should be <code>null</code> for messages by the current user, in which case
7537 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7538 * @param remoteInputHistory True if the messages was generated from the extra
7539 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7540 * <p>
7541 * The person provided should contain an Icon, set with
7542 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7543 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7544 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7545 * to differentiate between the different users.
7546 * </p>
7547 * @hide
7548 */
7549 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender,
7550 boolean remoteInputHistory) {
Alex Hillsfc737de2016-03-23 17:33:02 -04007551 mText = text;
7552 mTimestamp = timestamp;
7553 mSender = sender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007554 mRemoteInputHistory = remoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007555 }
7556
7557 /**
7558 * Sets a binary blob of data and an associated MIME type for a message. In the case
7559 * where the platform doesn't support the MIME type, the original text provided in the
7560 * constructor will be used.
7561 * @param dataMimeType The MIME type of the content. See
7562 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
7563 * types on Android and Android Wear.
7564 * @param dataUri The uri containing the content whose type is given by the MIME type.
7565 * <p class="note">
7566 * <ol>
7567 * <li>Notification Listeners including the System UI need permission to access the
7568 * data the Uri points to. The recommended ways to do this are:</li>
7569 * <li>Store the data in your own ContentProvider, making sure that other apps have
7570 * the correct permission to access your provider. The preferred mechanism for
7571 * providing access is to use per-URI permissions which are temporary and only
7572 * grant access to the receiving application. An easy way to create a
7573 * ContentProvider like this is to use the FileProvider helper class.</li>
7574 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
7575 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
7576 * also store non-media types (see MediaStore.Files for more info). Files can be
7577 * inserted into the MediaStore using scanFile() after which a content:// style
7578 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
7579 * Note that once added to the system MediaStore the content is accessible to any
7580 * app on the device.</li>
7581 * </ol>
7582 * @return this object for method chaining
7583 */
7584 public Message setData(String dataMimeType, Uri dataUri) {
7585 mDataMimeType = dataMimeType;
7586 mDataUri = dataUri;
7587 return this;
7588 }
7589
Alex Hillsfc737de2016-03-23 17:33:02 -04007590 /**
7591 * Get the text to be used for this message, or the fallback text if a type and content
7592 * Uri have been set
7593 */
7594 public CharSequence getText() {
7595 return mText;
7596 }
7597
7598 /**
7599 * Get the time at which this message arrived
7600 */
7601 public long getTimestamp() {
7602 return mTimestamp;
7603 }
7604
7605 /**
Shane Brennan5a871862017-03-11 13:14:17 -08007606 * Get the extras Bundle for this message.
7607 */
7608 public Bundle getExtras() {
7609 return mExtras;
7610 }
7611
7612 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007613 * Get the text used to display the contact's name in the messaging experience
Selim Cinekcb8b9852017-12-15 18:01:52 -08007614 *
7615 * @deprecated use {@link #getSenderPerson()}
Alex Hillsfc737de2016-03-23 17:33:02 -04007616 */
7617 public CharSequence getSender() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007618 return mSender == null ? null : mSender.getName();
7619 }
7620
7621 /**
7622 * Get the sender associated with this message.
7623 */
7624 @Nullable
7625 public Person getSenderPerson() {
Alex Hillsfc737de2016-03-23 17:33:02 -04007626 return mSender;
7627 }
7628
7629 /**
7630 * Get the MIME type of the data pointed to by the Uri
7631 */
7632 public String getDataMimeType() {
7633 return mDataMimeType;
7634 }
7635
7636 /**
koprivaa1a78482018-10-09 10:09:23 -07007637 * Get the Uri pointing to the content of the message. Can be null, in which case
Alex Hillsfc737de2016-03-23 17:33:02 -04007638 * {@see #getText()} is used.
7639 */
7640 public Uri getDataUri() {
7641 return mDataUri;
7642 }
7643
Kenny Guya0f6de82018-04-06 16:20:16 +01007644 /**
7645 * @return True if the message was generated from
7646 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7647 * @hide
7648 */
7649 public boolean isRemoteInputHistory() {
7650 return mRemoteInputHistory;
7651 }
7652
Beverlye98937a2018-11-15 10:18:57 -05007653 /**
7654 * @hide
7655 */
7656 @VisibleForTesting
7657 public Bundle toBundle() {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007658 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007659 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007660 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04007661 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007662 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04007663 if (mSender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007664 // Legacy listeners need this
7665 bundle.putCharSequence(KEY_SENDER, mSender.getName());
7666 bundle.putParcelable(KEY_SENDER_PERSON, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04007667 }
7668 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007669 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04007670 }
7671 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007672 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04007673 }
Shane Brennan5a871862017-03-11 13:14:17 -08007674 if (mExtras != null) {
7675 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
7676 }
Kenny Guya0f6de82018-04-06 16:20:16 +01007677 if (mRemoteInputHistory) {
7678 bundle.putBoolean(KEY_REMOTE_INPUT_HISTORY, mRemoteInputHistory);
7679 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007680 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04007681 }
7682
Alex Hillsd9b04d92016-04-11 16:38:16 -04007683 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
7684 Bundle[] bundles = new Bundle[messages.size()];
7685 final int N = messages.size();
7686 for (int i = 0; i < N; i++) {
7687 bundles[i] = messages.get(i).toBundle();
7688 }
7689 return bundles;
7690 }
7691
Selim Cinek88188f22017-09-19 16:46:56 -07007692 /**
7693 * @return A list of messages read from the bundles.
7694 *
7695 * @hide
7696 */
7697 public static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
7698 if (bundles == null) {
7699 return new ArrayList<>();
7700 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007701 List<Message> messages = new ArrayList<>(bundles.length);
7702 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07007703 if (bundles[i] instanceof Bundle) {
7704 Message message = getMessageFromBundle((Bundle)bundles[i]);
7705 if (message != null) {
7706 messages.add(message);
7707 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007708 }
7709 }
7710 return messages;
7711 }
7712
Selim Cinekb0dc61b2018-05-22 18:49:36 -07007713 /**
7714 * @return The message that is stored in the bundle or null if the message couldn't be
7715 * resolved.
7716 *
7717 * @hide
7718 */
7719 @Nullable
7720 public static Message getMessageFromBundle(Bundle bundle) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007721 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07007722 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007723 return null;
7724 } else {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007725
7726 Person senderPerson = bundle.getParcelable(KEY_SENDER_PERSON);
7727 if (senderPerson == null) {
7728 // Legacy apps that use compat don't actually provide the sender objects
7729 // We need to fix the compat version to provide people / use
7730 // the native api instead
7731 CharSequence senderName = bundle.getCharSequence(KEY_SENDER);
7732 if (senderName != null) {
Selim Cinek9acd6732018-03-23 16:39:02 -07007733 senderPerson = new Person.Builder().setName(senderName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007734 }
7735 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007736 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
Selim Cinekcb8b9852017-12-15 18:01:52 -08007737 bundle.getLong(KEY_TIMESTAMP),
Kenny Guya0f6de82018-04-06 16:20:16 +01007738 senderPerson,
7739 bundle.getBoolean(KEY_REMOTE_INPUT_HISTORY, false));
Alex Hillsd9b04d92016-04-11 16:38:16 -04007740 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
7741 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007742 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
7743 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04007744 }
Shane Brennan5a871862017-03-11 13:14:17 -08007745 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
7746 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
7747 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007748 return message;
7749 }
7750 } catch (ClassCastException e) {
7751 return null;
7752 }
7753 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007754 }
7755 }
7756
7757 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04007758 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08007759 *
Robert Ly91c5ce32014-06-08 15:37:00 -07007760 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04007761 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07007762 * Notification notif = new Notification.Builder(mContext)
7763 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
7764 * .setContentText(subject)
7765 * .setSmallIcon(R.drawable.new_mail)
7766 * .setLargeIcon(aBitmap)
7767 * .setStyle(new Notification.InboxStyle()
7768 * .addLine(str1)
7769 * .addLine(str2)
7770 * .setContentTitle(&quot;&quot;)
7771 * .setSummaryText(&quot;+3 more&quot;))
7772 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04007773 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08007774 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04007775 * @see Notification#bigContentView
7776 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007777 public static class InboxStyle extends Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07007778
7779 /**
7780 * The number of lines of remote input history allowed until we start reducing lines.
7781 */
7782 private static final int NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION = 1;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007783 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
7784
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007785 public InboxStyle() {
7786 }
7787
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007788 /**
7789 * @deprecated use {@code InboxStyle()}.
7790 */
7791 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04007792 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007793 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04007794 }
7795
Chris Wrend6297db2012-05-03 16:20:13 -04007796 /**
7797 * Overrides ContentTitle in the big form of the template.
7798 * This defaults to the value passed to setContentTitle().
7799 */
7800 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007801 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04007802 return this;
7803 }
7804
7805 /**
7806 * Set the first line of text after the detail section in the big form of the template.
7807 */
7808 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007809 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04007810 return this;
7811 }
7812
Chris Wren0bd664d2012-08-01 13:56:56 -04007813 /**
7814 * Append a line to the digest section of the Inbox notification.
7815 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04007816 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007817 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04007818 return this;
7819 }
7820
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007821 /**
7822 * @hide
7823 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05007824 public ArrayList<CharSequence> getLines() {
7825 return mTexts;
7826 }
7827
7828 /**
7829 * @hide
7830 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007831 public void addExtras(Bundle extras) {
7832 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007833
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007834 CharSequence[] a = new CharSequence[mTexts.size()];
7835 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
7836 }
7837
Christoph Studer4600f9b2014-07-22 22:44:43 +02007838 /**
7839 * @hide
7840 */
7841 @Override
7842 protected void restoreFromExtras(Bundle extras) {
7843 super.restoreFromExtras(extras);
7844
7845 mTexts.clear();
7846 if (extras.containsKey(EXTRA_TEXT_LINES)) {
7847 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
7848 }
7849 }
7850
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007851 /**
7852 * @hide
7853 */
7854 public RemoteViews makeBigContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007855 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder).text(null);
Selim Cinek384804b2018-04-18 14:31:07 +08007856 TemplateBindResult result = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007857 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource(), p, result);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007858
Chris Wrend6297db2012-05-03 16:20:13 -04007859 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 -04007860 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04007861
Chris Wren4ed80d52012-05-17 09:30:03 -04007862 // Make sure all rows are gone in case we reuse a view.
7863 for (int rowId : rowIds) {
7864 contentView.setViewVisibility(rowId, View.GONE);
7865 }
7866
Daniel Sandler879c5e02012-04-17 16:46:51 -04007867 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07007868 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7869 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08007870 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07007871 int onlyViewId = 0;
7872 int maxRows = rowIds.length;
7873 if (mBuilder.mActions.size() > 0) {
7874 maxRows--;
7875 }
Selim Cinekbee4e072018-05-21 22:06:43 -07007876 CharSequence[] remoteInputHistory = mBuilder.mN.extras.getCharSequenceArray(
7877 EXTRA_REMOTE_INPUT_HISTORY);
7878 if (remoteInputHistory != null
7879 && remoteInputHistory.length > NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION) {
7880 // Let's remove some messages to make room for the remote input history.
7881 // 1 is always able to fit, but let's remove them if they are 2 or 3
7882 int numRemoteInputs = Math.min(remoteInputHistory.length,
7883 MAX_REMOTE_INPUT_HISTORY_LINES);
7884 int totalNumRows = mTexts.size() + numRemoteInputs
7885 - NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION;
7886 if (totalNumRows > maxRows) {
7887 int overflow = totalNumRows - maxRows;
7888 if (mTexts.size() > maxRows) {
7889 // Heuristic: if the Texts don't fit anyway, we'll rather drop the last
7890 // few messages, even with the remote input
7891 maxRows -= overflow;
7892 } else {
7893 // otherwise we drop the first messages
7894 i = overflow;
7895 }
7896 }
7897 }
Selim Cinek07c80172016-04-21 16:40:47 -07007898 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007899 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07007900 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007901 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07007902 contentView.setTextViewText(rowIds[i],
7903 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007904 mBuilder.setTextViewColorSecondary(contentView, rowIds[i], p);
Selim Cinek07c80172016-04-21 16:40:47 -07007905 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek384804b2018-04-18 14:31:07 +08007906 handleInboxImageMargin(contentView, rowIds[i], first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007907 result.getIconMarginEnd());
Selim Cinek07c80172016-04-21 16:40:47 -07007908 if (first) {
7909 onlyViewId = rowIds[i];
7910 } else {
7911 onlyViewId = 0;
7912 }
Selim Cinek247fa012016-02-18 09:50:48 -08007913 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007914 }
7915 i++;
7916 }
Selim Cinek07c80172016-04-21 16:40:47 -07007917 if (onlyViewId != 0) {
7918 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
7919 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7920 R.dimen.notification_text_margin_top);
7921 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
7922 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007923
Daniel Sandler879c5e02012-04-17 16:46:51 -04007924 return contentView;
7925 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007926
Julia Reynolds7217dc92018-03-07 12:12:09 -05007927 /**
7928 * @hide
7929 */
7930 @Override
7931 public boolean areNotificationsVisiblyDifferent(Style other) {
7932 if (other == null || getClass() != other.getClass()) {
7933 return true;
7934 }
7935 InboxStyle newS = (InboxStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04007936
7937 final ArrayList<CharSequence> myLines = getLines();
7938 final ArrayList<CharSequence> newLines = newS.getLines();
7939 final int n = myLines.size();
7940 if (n != newLines.size()) {
7941 return true;
7942 }
7943
7944 for (int i = 0; i < n; i++) {
7945 if (!Objects.equals(
7946 String.valueOf(myLines.get(i)),
7947 String.valueOf(newLines.get(i)))) {
7948 return true;
7949 }
7950 }
7951 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05007952 }
7953
Selim Cinek384804b2018-04-18 14:31:07 +08007954 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007955 int marginEndValue) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08007956 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08007957 if (first) {
7958 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
7959 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
7960 boolean hasProgress = max != 0 || ind;
Selim Cinek384804b2018-04-18 14:31:07 +08007961 if (!hasProgress) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08007962 endMargin = marginEndValue;
Selim Cinek247fa012016-02-18 09:50:48 -08007963 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007964 }
Selim Cinek1c72fa02018-04-23 18:00:54 +08007965 contentView.setViewLayoutMarginEnd(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08007966 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04007967 }
Dan Sandler842dd772014-05-15 09:36:47 -04007968
7969 /**
7970 * Notification style for media playback notifications.
7971 *
7972 * In the expanded form, {@link Notification#bigContentView}, up to 5
7973 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04007974 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04007975 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
7976 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
7977 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08007978 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04007979 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
7980 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01007981 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04007982 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08007983 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007984 * Notifications created with MediaStyle will have their category set to
7985 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
7986 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08007987 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07007988 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
7989 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04007990 * the System UI can identify this as a notification representing an active media session
7991 * and respond accordingly (by showing album artwork in the lockscreen, for example).
7992 *
Selim Cinek99104832017-01-25 14:47:33 -08007993 * <p>
7994 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
7995 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
7996 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
7997 * <p>
7998 *
Dan Sandler842dd772014-05-15 09:36:47 -04007999 * To use this style with your Notification, feed it to
8000 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8001 * <pre class="prettyprint">
8002 * Notification noti = new Notification.Builder()
8003 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01008004 * .setContentTitle(&quot;Track title&quot;)
8005 * .setContentText(&quot;Artist - Album&quot;)
8006 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01008007 * .setStyle(<b>new Notification.MediaStyle()</b>
8008 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04008009 * .build();
8010 * </pre>
8011 *
8012 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08008013 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04008014 */
8015 public static class MediaStyle extends Style {
Gus Prevas9cc96602018-10-11 11:24:23 -04008016 // Changing max media buttons requires also changing templates
8017 // (notification_template_material_media and notification_template_material_big_media).
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008018 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04008019 static final int MAX_MEDIA_BUTTONS = 5;
Gus Prevas9cc96602018-10-11 11:24:23 -04008020 @IdRes private static final int[] MEDIA_BUTTON_IDS = {
8021 R.id.action0,
8022 R.id.action1,
8023 R.id.action2,
8024 R.id.action3,
8025 R.id.action4,
8026 };
Dan Sandler842dd772014-05-15 09:36:47 -04008027
8028 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07008029 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04008030
8031 public MediaStyle() {
8032 }
8033
Adrian Roosf5faf9d2016-05-23 13:56:15 -07008034 /**
8035 * @deprecated use {@code MediaStyle()}.
8036 */
8037 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04008038 public MediaStyle(Builder builder) {
8039 setBuilder(builder);
8040 }
8041
8042 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008043 * 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 -04008044 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008045 *
8046 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04008047 */
8048 public MediaStyle setShowActionsInCompactView(int...actions) {
8049 mActionsToShowInCompact = actions;
8050 return this;
8051 }
8052
8053 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07008054 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
8055 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04008056 */
Jeff Browndba34ba2014-06-24 20:46:03 -07008057 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04008058 mToken = token;
8059 return this;
8060 }
8061
Christoph Studer4600f9b2014-07-22 22:44:43 +02008062 /**
8063 * @hide
8064 */
Dan Sandler842dd772014-05-15 09:36:47 -04008065 @Override
Mathew Inwood61e8ae62018-08-14 14:17:44 +01008066 @UnsupportedAppUsage
Dan Sandler842dd772014-05-15 09:36:47 -04008067 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02008068 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01008069 if (wip.category == null) {
8070 wip.category = Notification.CATEGORY_TRANSPORT;
8071 }
Dan Sandler842dd772014-05-15 09:36:47 -04008072 return wip;
8073 }
8074
Christoph Studer4600f9b2014-07-22 22:44:43 +02008075 /**
8076 * @hide
8077 */
8078 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008079 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04008080 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02008081 }
8082
8083 /**
8084 * @hide
8085 */
8086 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04008087 public RemoteViews makeBigContentView() {
8088 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02008089 }
8090
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008091 /**
8092 * @hide
8093 */
8094 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008095 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008096 RemoteViews expanded = makeMediaBigContentView();
8097 return expanded != null ? expanded : makeMediaContentView();
8098 }
8099
Dan Sandler842dd772014-05-15 09:36:47 -04008100 /** @hide */
8101 @Override
8102 public void addExtras(Bundle extras) {
8103 super.addExtras(extras);
8104
8105 if (mToken != null) {
8106 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
8107 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01008108 if (mActionsToShowInCompact != null) {
8109 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
8110 }
Dan Sandler842dd772014-05-15 09:36:47 -04008111 }
8112
Christoph Studer4600f9b2014-07-22 22:44:43 +02008113 /**
8114 * @hide
8115 */
8116 @Override
8117 protected void restoreFromExtras(Bundle extras) {
8118 super.restoreFromExtras(extras);
8119
8120 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
8121 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
8122 }
8123 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
8124 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
8125 }
8126 }
8127
Julia Reynolds7217dc92018-03-07 12:12:09 -05008128 /**
8129 * @hide
8130 */
8131 @Override
8132 public boolean areNotificationsVisiblyDifferent(Style other) {
8133 if (other == null || getClass() != other.getClass()) {
8134 return true;
8135 }
8136 // All fields to compare are on the Notification object
8137 return false;
8138 }
8139
Gus Prevas9cc96602018-10-11 11:24:23 -04008140 private void bindMediaActionButton(RemoteViews container, @IdRes int buttonId,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008141 Action action, StandardTemplateParams p) {
Dan Sandler842dd772014-05-15 09:36:47 -04008142 final boolean tombstone = (action.actionIntent == null);
Gus Prevas9cc96602018-10-11 11:24:23 -04008143 container.setViewVisibility(buttonId, View.VISIBLE);
8144 container.setImageViewIcon(buttonId, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07008145
8146 // If the action buttons should not be tinted, then just use the default
8147 // notification color. Otherwise, just use the passed-in color.
Gus Prevas9cc96602018-10-11 11:24:23 -04008148 Resources resources = mBuilder.mContext.getResources();
8149 Configuration currentConfig = resources.getConfiguration();
Lucas Dupinf03e7522018-06-25 16:21:13 -07008150 boolean inNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
8151 == Configuration.UI_MODE_NIGHT_YES;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008152 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized(p)
8153 ? getActionColor(p)
Lucas Dupina291d192018-06-07 13:59:42 -07008154 : ContrastColorUtil.resolveColor(mBuilder.mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07008155 Notification.COLOR_DEFAULT, inNightMode);
Anthony Chenad4d1582017-04-10 16:07:58 -07008156
Gus Prevas9cc96602018-10-11 11:24:23 -04008157 container.setDrawableTint(buttonId, false, tintColor,
Sunny Goyal5b153922017-09-21 21:00:36 -07008158 PorterDuff.Mode.SRC_ATOP);
Gus Prevas9cc96602018-10-11 11:24:23 -04008159
8160 final TypedArray typedArray = mBuilder.mContext.obtainStyledAttributes(
8161 new int[]{ android.R.attr.colorControlHighlight });
8162 int rippleAlpha = Color.alpha(typedArray.getColor(0, 0));
8163 typedArray.recycle();
8164 int rippleColor = Color.argb(rippleAlpha, Color.red(tintColor), Color.green(tintColor),
8165 Color.blue(tintColor));
8166 container.setRippleDrawableColor(buttonId, ColorStateList.valueOf(rippleColor));
8167
Dan Sandler842dd772014-05-15 09:36:47 -04008168 if (!tombstone) {
Gus Prevas9cc96602018-10-11 11:24:23 -04008169 container.setOnClickPendingIntent(buttonId, action.actionIntent);
Dan Sandler842dd772014-05-15 09:36:47 -04008170 }
Gus Prevas9cc96602018-10-11 11:24:23 -04008171 container.setContentDescription(buttonId, action.title);
Dan Sandler842dd772014-05-15 09:36:47 -04008172 }
8173
8174 private RemoteViews makeMediaContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008175 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).fillTextsFrom(
8176 mBuilder);
Dan Sandler842dd772014-05-15 09:36:47 -04008177 RemoteViews view = mBuilder.applyStandardTemplate(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008178 R.layout.notification_template_material_media, p,
Selim Cinek384804b2018-04-18 14:31:07 +08008179 null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04008180
8181 final int numActions = mBuilder.mActions.size();
Gus Prevas9cc96602018-10-11 11:24:23 -04008182 final int numActionsToShow = mActionsToShowInCompact == null
Dan Sandler842dd772014-05-15 09:36:47 -04008183 ? 0
8184 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Gus Prevas9cc96602018-10-11 11:24:23 -04008185 if (numActionsToShow > numActions) {
8186 throw new IllegalArgumentException(String.format(
8187 "setShowActionsInCompactView: action %d out of bounds (max %d)",
8188 numActions, numActions - 1));
8189 }
8190 for (int i = 0; i < MAX_MEDIA_BUTTONS_IN_COMPACT; i++) {
8191 if (i < numActionsToShow) {
Dan Sandler842dd772014-05-15 09:36:47 -04008192 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008193 bindMediaActionButton(view, MEDIA_BUTTON_IDS[i], action, p);
Gus Prevas9cc96602018-10-11 11:24:23 -04008194 } else {
8195 view.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04008196 }
8197 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08008198 handleImage(view);
8199 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008200 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07008201 if (mBuilder.mN.hasLargeIcon()) {
Selim Cinek384804b2018-04-18 14:31:07 +08008202 endMargin = R.dimen.notification_media_image_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08008203 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008204 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04008205 return view;
8206 }
8207
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008208 private int getActionColor(StandardTemplateParams p) {
8209 return mBuilder.isColorized(p) ? mBuilder.getPrimaryTextColor(p)
8210 : mBuilder.resolveContrastColor(p);
Selim Cinek99104832017-01-25 14:47:33 -08008211 }
8212
Dan Sandler842dd772014-05-15 09:36:47 -04008213 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008214 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008215 // Dont add an expanded view if there is no more content to be revealed
8216 int actionsInCompact = mActionsToShowInCompact == null
8217 ? 0
8218 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07008219 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008220 return null;
8221 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008222 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).fillTextsFrom(
8223 mBuilder);
Selim Cinek5bf069a2015-11-10 19:14:27 -05008224 RemoteViews big = mBuilder.applyStandardTemplate(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008225 R.layout.notification_template_material_big_media, p , null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04008226
Gus Prevas9cc96602018-10-11 11:24:23 -04008227 for (int i = 0; i < MAX_MEDIA_BUTTONS; i++) {
8228 if (i < actionCount) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008229 bindMediaActionButton(big, MEDIA_BUTTON_IDS[i], mBuilder.mActions.get(i), p);
Gus Prevas9cc96602018-10-11 11:24:23 -04008230 } else {
8231 big.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04008232 }
8233 }
linanson2bcd4032019-01-14 15:22:04 +08008234 bindMediaActionButton(big, R.id.media_seamless, new Action(R.drawable.ic_media_seamless,
8235 mBuilder.mContext.getString(
8236 com.android.internal.R.string.ext_media_seamless_action), null), p);
8237 big.setViewVisibility(R.id.media_seamless, View.GONE);
Selim Cinek5bf069a2015-11-10 19:14:27 -05008238 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04008239 return big;
8240 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008241
Selim Cinek5bf069a2015-11-10 19:14:27 -05008242 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07008243 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008244 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
8245 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008246 }
8247 }
8248
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008249 /**
8250 * @hide
8251 */
8252 @Override
8253 protected boolean hasProgress() {
8254 return false;
8255 }
Dan Sandler842dd772014-05-15 09:36:47 -04008256 }
Griff Hazen61a9e862014-05-22 16:05:19 -07008257
Selim Cinek593610c2016-02-16 18:42:57 -08008258 /**
8259 * Notification style for custom views that are decorated by the system
8260 *
8261 * <p>Instead of providing a notification that is completely custom, a developer can set this
8262 * style and still obtain system decorations like the notification header with the expand
8263 * affordance and actions.
8264 *
8265 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8266 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8267 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8268 * corresponding custom views to display.
8269 *
8270 * To use this style with your Notification, feed it to
8271 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8272 * <pre class="prettyprint">
8273 * Notification noti = new Notification.Builder()
8274 * .setSmallIcon(R.drawable.ic_stat_player)
8275 * .setLargeIcon(albumArtBitmap))
8276 * .setCustomContentView(contentView);
8277 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
8278 * .build();
8279 * </pre>
8280 */
8281 public static class DecoratedCustomViewStyle extends Style {
8282
8283 public DecoratedCustomViewStyle() {
8284 }
8285
Selim Cinek593610c2016-02-16 18:42:57 -08008286 /**
8287 * @hide
8288 */
8289 public boolean displayCustomViewInline() {
8290 return true;
8291 }
8292
8293 /**
8294 * @hide
8295 */
8296 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008297 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008298 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
8299 }
8300
8301 /**
8302 * @hide
8303 */
8304 @Override
8305 public RemoteViews makeBigContentView() {
8306 return makeDecoratedBigContentView();
8307 }
8308
8309 /**
8310 * @hide
8311 */
8312 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008313 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008314 return makeDecoratedHeadsUpContentView();
8315 }
8316
Selim Cinek593610c2016-02-16 18:42:57 -08008317 private RemoteViews makeDecoratedHeadsUpContentView() {
8318 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
8319 ? mBuilder.mN.contentView
8320 : mBuilder.mN.headsUpContentView;
8321 if (mBuilder.mActions.size() == 0) {
8322 return makeStandardTemplateWithCustomContent(headsUpContentView);
8323 }
Selim Cinek384804b2018-04-18 14:31:07 +08008324 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008325 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008326 mBuilder.getBigBaseLayoutResource(), result);
8327 buildIntoRemoteViewContent(remoteViews, headsUpContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008328 return remoteViews;
8329 }
8330
Selim Cinek593610c2016-02-16 18:42:57 -08008331 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
Selim Cinek384804b2018-04-18 14:31:07 +08008332 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008333 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08008334 mBuilder.getBaseLayoutResource(), result);
8335 buildIntoRemoteViewContent(remoteViews, customContent, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008336 return remoteViews;
8337 }
8338
Selim Cinek593610c2016-02-16 18:42:57 -08008339 private RemoteViews makeDecoratedBigContentView() {
8340 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
8341 ? mBuilder.mN.contentView
8342 : mBuilder.mN.bigContentView;
8343 if (mBuilder.mActions.size() == 0) {
8344 return makeStandardTemplateWithCustomContent(bigContentView);
8345 }
Selim Cinek384804b2018-04-18 14:31:07 +08008346 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008347 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008348 mBuilder.getBigBaseLayoutResource(), result);
8349 buildIntoRemoteViewContent(remoteViews, bigContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008350 return remoteViews;
8351 }
Selim Cinek247fa012016-02-18 09:50:48 -08008352
8353 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
Selim Cinek384804b2018-04-18 14:31:07 +08008354 RemoteViews customContent, TemplateBindResult result) {
Selim Cinek6fe4a102019-01-25 14:38:24 -08008355 int childIndex = -1;
Adrian Roos5081c0d2016-02-26 16:04:19 -08008356 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008357 // Need to clone customContent before adding, because otherwise it can no longer be
8358 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008359 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07008360 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
8361 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Sunny Goyalc12d31c2018-11-12 16:29:18 -08008362 remoteViews.addFlags(RemoteViews.FLAG_REAPPLY_DISALLOWED);
Selim Cinek6fe4a102019-01-25 14:38:24 -08008363 childIndex = 0;
Adrian Roos5081c0d2016-02-26 16:04:19 -08008364 }
Selim Cinek6fe4a102019-01-25 14:38:24 -08008365 remoteViews.setIntTag(R.id.notification_main_column,
8366 com.android.internal.R.id.notification_custom_view_index_tag,
8367 childIndex);
Selim Cinek247fa012016-02-18 09:50:48 -08008368 // also update the end margin if there is an image
Selim Cinek384804b2018-04-18 14:31:07 +08008369 Resources resources = mBuilder.mContext.getResources();
8370 int endMargin = resources.getDimensionPixelSize(
Selim Cinek1c72fa02018-04-23 18:00:54 +08008371 R.dimen.notification_content_margin_end) + result.getIconMarginEnd();
Selim Cinek384804b2018-04-18 14:31:07 +08008372 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08008373 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05008374
8375 /**
8376 * @hide
8377 */
8378 @Override
8379 public boolean areNotificationsVisiblyDifferent(Style other) {
8380 if (other == null || getClass() != other.getClass()) {
8381 return true;
8382 }
8383 // Comparison done for all custom RemoteViews, independent of style
8384 return false;
8385 }
Selim Cinek593610c2016-02-16 18:42:57 -08008386 }
8387
Selim Cinek03eb3b72016-02-18 10:39:45 -08008388 /**
8389 * Notification style for media custom views that are decorated by the system
8390 *
8391 * <p>Instead of providing a media notification that is completely custom, a developer can set
8392 * this style and still obtain system decorations like the notification header with the expand
8393 * affordance and actions.
8394 *
8395 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8396 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8397 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8398 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08008399 * <p>
8400 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
8401 * notification by using {@link Notification.Builder#setColorized(boolean)}.
8402 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08008403 * To use this style with your Notification, feed it to
8404 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8405 * <pre class="prettyprint">
8406 * Notification noti = new Notification.Builder()
8407 * .setSmallIcon(R.drawable.ic_stat_player)
8408 * .setLargeIcon(albumArtBitmap))
8409 * .setCustomContentView(contentView);
8410 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
8411 * .setMediaSession(mySession))
8412 * .build();
8413 * </pre>
8414 *
8415 * @see android.app.Notification.DecoratedCustomViewStyle
8416 * @see android.app.Notification.MediaStyle
8417 */
8418 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
8419
8420 public DecoratedMediaCustomViewStyle() {
8421 }
8422
Selim Cinek03eb3b72016-02-18 10:39:45 -08008423 /**
8424 * @hide
8425 */
8426 public boolean displayCustomViewInline() {
8427 return true;
8428 }
8429
8430 /**
8431 * @hide
8432 */
8433 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008434 public RemoteViews makeContentView(boolean increasedHeight) {
8435 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008436 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8437 mBuilder.mN.contentView);
8438 }
8439
8440 /**
8441 * @hide
8442 */
8443 @Override
8444 public RemoteViews makeBigContentView() {
8445 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
8446 ? mBuilder.mN.bigContentView
8447 : mBuilder.mN.contentView;
8448 return makeBigContentViewWithCustomContent(customRemoteView);
8449 }
8450
8451 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
8452 RemoteViews remoteViews = super.makeBigContentView();
8453 if (remoteViews != null) {
8454 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
8455 customRemoteView);
8456 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08008457 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008458 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8459 customRemoteView);
8460 } else {
8461 return null;
8462 }
8463 }
8464
8465 /**
8466 * @hide
8467 */
8468 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008469 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08008470 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
8471 ? mBuilder.mN.headsUpContentView
8472 : mBuilder.mN.contentView;
8473 return makeBigContentViewWithCustomContent(customRemoteView);
8474 }
8475
Julia Reynolds7217dc92018-03-07 12:12:09 -05008476 /**
8477 * @hide
8478 */
8479 @Override
8480 public boolean areNotificationsVisiblyDifferent(Style other) {
8481 if (other == null || getClass() != other.getClass()) {
8482 return true;
8483 }
8484 // Comparison done for all custom RemoteViews, independent of style
8485 return false;
8486 }
8487
Selim Cinek03eb3b72016-02-18 10:39:45 -08008488 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
8489 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08008490 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008491 // Need to clone customContent before adding, because otherwise it can no longer be
8492 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008493 customContent = customContent.clone();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008494 customContent.overrideTextColors(mBuilder.getPrimaryTextColor(mBuilder.mParams));
Selim Cinekf91017e2016-03-14 12:25:09 -07008495 remoteViews.removeAllViews(id);
8496 remoteViews.addView(id, customContent);
Sunny Goyalc12d31c2018-11-12 16:29:18 -08008497 remoteViews.addFlags(RemoteViews.FLAG_REAPPLY_DISALLOWED);
Adrian Roos5081c0d2016-02-26 16:04:19 -08008498 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08008499 return remoteViews;
8500 }
8501 }
8502
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008503 /**
8504 * Encapsulates the information needed to display a notification as a bubble.
8505 *
8506 * <p>A bubble is used to display app content in a floating window over the existing
8507 * foreground activity. A bubble has a collapsed state represented by an icon,
8508 * {@link BubbleMetadata.Builder#setIcon(Icon)} and an expanded state which is populated
8509 * via {@link BubbleMetadata.Builder#setIntent(PendingIntent)}.</p>
8510 *
8511 * <b>Notifications with a valid and allowed bubble will display in collapsed state
8512 * outside of the notification shade on unlocked devices. When a user interacts with the
8513 * collapsed bubble, the bubble intent will be invoked and displayed.</b>
8514 *
8515 * @see Notification.Builder#setBubbleMetadata(BubbleMetadata)
8516 */
8517 public static final class BubbleMetadata implements Parcelable {
8518
8519 private PendingIntent mPendingIntent;
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008520 private PendingIntent mDeleteIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008521 private Icon mIcon;
8522 private int mDesiredHeight;
Mady Mellor7af771a2019-03-07 15:04:54 -08008523 @DimenRes private int mDesiredHeightResId;
Mady Mellor0157ff22019-01-24 15:31:48 -08008524 private int mFlags;
8525
8526 /**
8527 * If set and the app creating the bubble is in the foreground, the bubble will be posted
8528 * in its expanded state, with the contents of {@link #getIntent()} in a floating window.
8529 *
8530 * <p>If the app creating the bubble is not in the foreground this flag has no effect.</p>
8531 *
8532 * <p>Generally this flag should only be set if the user has performed an action to request
8533 * or create a bubble.</p>
8534 */
8535 private static final int FLAG_AUTO_EXPAND_BUBBLE = 0x00000001;
8536
8537 /**
8538 * If set and the app creating the bubble is in the foreground, the bubble will be posted
8539 * <b>without</b> the associated notification in the notification shade. Subsequent update
8540 * notifications to this bubble will post a notification in the shade.
8541 *
8542 * <p>If the app creating the bubble is not in the foreground this flag has no effect.</p>
8543 *
8544 * <p>Generally this flag should only be set if the user has performed an action to request
8545 * or create a bubble.</p>
8546 */
8547 private static final int FLAG_SUPPRESS_INITIAL_NOTIFICATION = 0x00000002;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008548
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008549 private BubbleMetadata(PendingIntent expandIntent, PendingIntent deleteIntent,
Mady Mellor7af771a2019-03-07 15:04:54 -08008550 Icon icon, int height, @DimenRes int heightResId) {
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008551 mPendingIntent = expandIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008552 mIcon = icon;
8553 mDesiredHeight = height;
Mady Mellor7af771a2019-03-07 15:04:54 -08008554 mDesiredHeightResId = heightResId;
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008555 mDeleteIntent = deleteIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008556 }
8557
8558 private BubbleMetadata(Parcel in) {
8559 mPendingIntent = PendingIntent.CREATOR.createFromParcel(in);
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008560 mIcon = Icon.CREATOR.createFromParcel(in);
8561 mDesiredHeight = in.readInt();
Mady Mellor0157ff22019-01-24 15:31:48 -08008562 mFlags = in.readInt();
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008563 if (in.readInt() != 0) {
8564 mDeleteIntent = PendingIntent.CREATOR.createFromParcel(in);
8565 }
Mady Mellor7af771a2019-03-07 15:04:54 -08008566 mDesiredHeightResId = in.readInt();
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008567 }
8568
8569 /**
8570 * @return the pending intent used to populate the floating window for this bubble.
8571 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008572 @NonNull
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008573 public PendingIntent getIntent() {
8574 return mPendingIntent;
8575 }
8576
8577 /**
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008578 * @return the pending intent to send when the bubble is dismissed by a user, if one exists.
8579 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008580 @Nullable
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008581 public PendingIntent getDeleteIntent() {
8582 return mDeleteIntent;
8583 }
8584
8585 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008586 * @return the title that will appear along with the app content defined by
8587 * {@link #getIntent()} for this bubble.
Mady Mellor138f4c92019-02-20 16:12:17 -08008588 *
8589 * @deprecated titles are no longer required or shown.
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008590 */
Mady Mellor138f4c92019-02-20 16:12:17 -08008591 @Deprecated
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008592 public CharSequence getTitle() {
Mady Mellor138f4c92019-02-20 16:12:17 -08008593 return "";
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008594 }
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008595
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008596 /**
8597 * @return the icon that will be displayed for this bubble when it is collapsed.
8598 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008599 @NonNull
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008600 public Icon getIcon() {
8601 return mIcon;
8602 }
8603
8604 /**
Mady Mellor7af771a2019-03-07 15:04:54 -08008605 * @return the ideal height, in DPs, for the floating window that app content defined by
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008606 * {@link #getIntent()} for this bubble.
8607 */
8608 public int getDesiredHeight() {
8609 return mDesiredHeight;
8610 }
8611
Mady Mellor0157ff22019-01-24 15:31:48 -08008612 /**
Mady Mellor7af771a2019-03-07 15:04:54 -08008613 * @return the resId of ideal height for the floating window that app content defined by
8614 * {@link #getIntent()} for this bubble.
8615 */
8616 @DimenRes
8617 public int getDesiredHeightResId() {
8618 return mDesiredHeightResId;
8619 }
8620
8621 /**
Mady Mellor0157ff22019-01-24 15:31:48 -08008622 * @return whether this bubble should auto expand when it is posted.
8623 *
8624 * @see BubbleMetadata.Builder#setAutoExpandBubble(boolean)
8625 */
8626 public boolean getAutoExpandBubble() {
8627 return (mFlags & FLAG_AUTO_EXPAND_BUBBLE) != 0;
8628 }
8629
8630 /**
8631 * @return whether this bubble should suppress the initial notification when it is posted.
8632 *
8633 * @see BubbleMetadata.Builder#setSuppressInitialNotification(boolean)
8634 */
8635 public boolean getSuppressInitialNotification() {
8636 return (mFlags & FLAG_SUPPRESS_INITIAL_NOTIFICATION) != 0;
8637 }
8638
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07008639 public static final @android.annotation.NonNull Parcelable.Creator<BubbleMetadata> CREATOR =
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008640 new Parcelable.Creator<BubbleMetadata>() {
8641
8642 @Override
8643 public BubbleMetadata createFromParcel(Parcel source) {
8644 return new BubbleMetadata(source);
8645 }
8646
8647 @Override
8648 public BubbleMetadata[] newArray(int size) {
8649 return new BubbleMetadata[size];
8650 }
8651 };
8652
8653 @Override
8654 public int describeContents() {
8655 return 0;
8656 }
8657
8658 @Override
8659 public void writeToParcel(Parcel out, int flags) {
8660 mPendingIntent.writeToParcel(out, 0);
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008661 mIcon.writeToParcel(out, 0);
8662 out.writeInt(mDesiredHeight);
Mady Mellor0157ff22019-01-24 15:31:48 -08008663 out.writeInt(mFlags);
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008664 out.writeInt(mDeleteIntent != null ? 1 : 0);
8665 if (mDeleteIntent != null) {
8666 mDeleteIntent.writeToParcel(out, 0);
8667 }
Mady Mellor7af771a2019-03-07 15:04:54 -08008668 out.writeInt(mDesiredHeightResId);
Mady Mellor0157ff22019-01-24 15:31:48 -08008669 }
8670
8671 private void setFlags(int flags) {
8672 mFlags = flags;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008673 }
8674
8675 /**
8676 * Builder to construct a {@link BubbleMetadata} object.
8677 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008678 public static final class Builder {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008679
8680 private PendingIntent mPendingIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008681 private Icon mIcon;
8682 private int mDesiredHeight;
Mady Mellor7af771a2019-03-07 15:04:54 -08008683 @DimenRes private int mDesiredHeightResId;
Mady Mellor0157ff22019-01-24 15:31:48 -08008684 private int mFlags;
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008685 private PendingIntent mDeleteIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008686
8687 /**
8688 * Constructs a new builder object.
8689 */
8690 public Builder() {
8691 }
8692
8693 /**
8694 * Sets the intent that will be used when the bubble is expanded. This will display the
8695 * app content in a floating window over the existing foreground activity.
8696 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008697 @NonNull
8698 public BubbleMetadata.Builder setIntent(@NonNull PendingIntent intent) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008699 if (intent == null) {
8700 throw new IllegalArgumentException("Bubble requires non-null pending intent");
8701 }
8702 mPendingIntent = intent;
8703 return this;
8704 }
8705
8706 /**
8707 * Sets the title that will appear along with the app content for this bubble.
8708 *
8709 * <p>A title is required and should expect to fit on a single line and make sense when
8710 * shown with the content defined by {@link #setIntent(PendingIntent)}.</p>
Mady Mellor138f4c92019-02-20 16:12:17 -08008711 *
8712 * @deprecated titles are no longer required or shown.
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008713 */
Mady Mellor138f4c92019-02-20 16:12:17 -08008714 @Deprecated
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008715 public BubbleMetadata.Builder setTitle(CharSequence title) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008716 return this;
8717 }
8718
8719 /**
8720 * Sets the icon that will represent the bubble when it is collapsed.
8721 *
8722 * <p>An icon is required and should be representative of the content within the bubble.
8723 * If your app produces multiple bubbles, the image should be unique for each of them.
8724 * </p>
8725 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008726 @NonNull
8727 public BubbleMetadata.Builder setIcon(@NonNull Icon icon) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008728 if (icon == null) {
8729 throw new IllegalArgumentException("Bubbles require non-null icon");
8730 }
8731 mIcon = icon;
8732 return this;
8733 }
8734
8735 /**
Mady Mellor7af771a2019-03-07 15:04:54 -08008736 * Sets the desired height in DPs for the app content defined by
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008737 * {@link #setIntent(PendingIntent)}, this height may not be respected if there is not
8738 * enough space on the screen or if the provided height is too small to be useful.
Mady Mellor7af771a2019-03-07 15:04:54 -08008739 * <p>
8740 * If {@link #setDesiredHeightResId(int)} was previously called on this builder, the
8741 * previous value set will be cleared after calling this method, and this value will
8742 * be used instead.
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008743 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008744 @NonNull
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008745 public BubbleMetadata.Builder setDesiredHeight(int height) {
8746 mDesiredHeight = Math.max(height, 0);
Mady Mellor7af771a2019-03-07 15:04:54 -08008747 mDesiredHeightResId = 0;
8748 return this;
8749 }
8750
8751
8752 /**
8753 * Sets the desired height via resId for the app content defined by
8754 * {@link #setIntent(PendingIntent)}, this height may not be respected if there is not
8755 * enough space on the screen or if the provided height is too small to be useful.
8756 * <p>
8757 * If {@link #setDesiredHeight(int)} was previously called on this builder, the
8758 * previous value set will be cleared after calling this method, and this value will
8759 * be used instead.
8760 */
8761 @NonNull
8762 public BubbleMetadata.Builder setDesiredHeightResId(@DimenRes int heightResId) {
8763 mDesiredHeightResId = heightResId;
8764 mDesiredHeight = 0;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008765 return this;
8766 }
8767
8768 /**
Mady Mellor0157ff22019-01-24 15:31:48 -08008769 * If set and the app creating the bubble is in the foreground, the bubble will be
8770 * posted in its expanded state, with the contents of {@link #getIntent()} in a
8771 * floating window.
8772 *
8773 * <p>If the app creating the bubble is not in the foreground this flag has no effect.
8774 * </p>
8775 *
8776 * <p>Generally this flag should only be set if the user has performed an action to
8777 * request or create a bubble.</p>
8778 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008779 @NonNull
Mady Mellor0157ff22019-01-24 15:31:48 -08008780 public BubbleMetadata.Builder setAutoExpandBubble(boolean shouldExpand) {
8781 setFlag(FLAG_AUTO_EXPAND_BUBBLE, shouldExpand);
8782 return this;
8783 }
8784
8785 /**
8786 * If set and the app creating the bubble is in the foreground, the bubble will be
8787 * posted <b>without</b> the associated notification in the notification shade.
8788 * Subsequent update notifications to this bubble will post a notification in the shade.
8789 *
8790 * <p>If the app creating the bubble is not in the foreground this flag has no effect.
8791 * </p>
8792 *
8793 * <p>Generally this flag should only be set if the user has performed an action to
8794 * request or create a bubble.</p>
8795 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008796 @NonNull
Mady Mellor0157ff22019-01-24 15:31:48 -08008797 public BubbleMetadata.Builder setSuppressInitialNotification(
8798 boolean shouldSupressNotif) {
8799 setFlag(FLAG_SUPPRESS_INITIAL_NOTIFICATION, shouldSupressNotif);
8800 return this;
8801 }
8802
8803 /**
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008804 * Sets an optional intent to send when this bubble is explicitly removed by the user.
8805 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008806 @NonNull
8807 public BubbleMetadata.Builder setDeleteIntent(@Nullable PendingIntent deleteIntent) {
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008808 mDeleteIntent = deleteIntent;
8809 return this;
8810 }
8811
8812 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008813 * Creates the {@link BubbleMetadata} defined by this builder.
8814 * <p>Will throw {@link IllegalStateException} if required fields have not been set
8815 * on this builder.</p>
8816 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008817 @NonNull
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008818 public BubbleMetadata build() {
8819 if (mPendingIntent == null) {
8820 throw new IllegalStateException("Must supply pending intent to bubble");
8821 }
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008822 if (mIcon == null) {
8823 throw new IllegalStateException("Must supply an icon for the bubble");
8824 }
Mady Mellor138f4c92019-02-20 16:12:17 -08008825 BubbleMetadata data = new BubbleMetadata(mPendingIntent, mDeleteIntent,
Mady Mellor7af771a2019-03-07 15:04:54 -08008826 mIcon, mDesiredHeight, mDesiredHeightResId);
Mady Mellor0157ff22019-01-24 15:31:48 -08008827 data.setFlags(mFlags);
8828 return data;
8829 }
8830
8831 /**
8832 * @hide
8833 */
8834 public BubbleMetadata.Builder setFlag(int mask, boolean value) {
8835 if (value) {
8836 mFlags |= mask;
8837 } else {
8838 mFlags &= ~mask;
8839 }
8840 return this;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008841 }
8842 }
8843 }
8844
8845
Christoph Studer4600f9b2014-07-22 22:44:43 +02008846 // When adding a new Style subclass here, don't forget to update
8847 // Builder.getNotificationStyleClass.
8848
Griff Hazen61a9e862014-05-22 16:05:19 -07008849 /**
8850 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
8851 * metadata or change options on a notification builder.
8852 */
8853 public interface Extender {
8854 /**
8855 * Apply this extender to a notification builder.
8856 * @param builder the builder to be modified.
8857 * @return the build object for chaining.
8858 */
8859 public Builder extend(Builder builder);
8860 }
8861
8862 /**
8863 * Helper class to add wearable extensions to notifications.
8864 * <p class="note"> See
8865 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
8866 * for Android Wear</a> for more information on how to use this class.
8867 * <p>
8868 * To create a notification with wearable extensions:
8869 * <ol>
8870 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
8871 * properties.
8872 * <li>Create a {@link android.app.Notification.WearableExtender}.
8873 * <li>Set wearable-specific properties using the
8874 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
8875 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
8876 * notification.
8877 * <li>Post the notification to the notification system with the
8878 * {@code NotificationManager.notify(...)} methods.
8879 * </ol>
8880 *
8881 * <pre class="prettyprint">
8882 * Notification notif = new Notification.Builder(mContext)
8883 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
8884 * .setContentText(subject)
8885 * .setSmallIcon(R.drawable.new_mail)
8886 * .extend(new Notification.WearableExtender()
8887 * .setContentIcon(R.drawable.new_mail))
8888 * .build();
8889 * NotificationManager notificationManger =
8890 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
8891 * notificationManger.notify(0, notif);</pre>
8892 *
8893 * <p>Wearable extensions can be accessed on an existing notification by using the
8894 * {@code WearableExtender(Notification)} constructor,
8895 * and then using the {@code get} methods to access values.
8896 *
8897 * <pre class="prettyprint">
8898 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
8899 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07008900 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008901 */
8902 public static final class WearableExtender implements Extender {
8903 /**
8904 * Sentinel value for an action index that is unset.
8905 */
8906 public static final int UNSET_ACTION_INDEX = -1;
8907
8908 /**
8909 * Size value for use with {@link #setCustomSizePreset} to show this notification with
8910 * default sizing.
8911 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07008912 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07008913 * on their content.
Gus Prevasd7363752018-09-18 14:35:15 -04008914 *
8915 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008916 */
Gus Prevasd7363752018-09-18 14:35:15 -04008917 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008918 public static final int SIZE_DEFAULT = 0;
8919
8920 /**
8921 * Size value for use with {@link #setCustomSizePreset} to show this notification
8922 * with an extra small size.
8923 * <p>This value is only applicable for custom display notifications created using
8924 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008925 *
8926 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008927 */
Gus Prevasd7363752018-09-18 14:35:15 -04008928 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008929 public static final int SIZE_XSMALL = 1;
8930
8931 /**
8932 * Size value for use with {@link #setCustomSizePreset} to show this notification
8933 * with a small size.
8934 * <p>This value is only applicable for custom display notifications created using
8935 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008936 *
8937 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008938 */
Gus Prevasd7363752018-09-18 14:35:15 -04008939 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008940 public static final int SIZE_SMALL = 2;
8941
8942 /**
8943 * Size value for use with {@link #setCustomSizePreset} to show this notification
8944 * with a medium size.
8945 * <p>This value is only applicable for custom display notifications created using
8946 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008947 *
8948 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008949 */
Gus Prevasd7363752018-09-18 14:35:15 -04008950 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008951 public static final int SIZE_MEDIUM = 3;
8952
8953 /**
8954 * Size value for use with {@link #setCustomSizePreset} to show this notification
8955 * with a large size.
8956 * <p>This value is only applicable for custom display notifications created using
8957 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008958 *
8959 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008960 */
Gus Prevasd7363752018-09-18 14:35:15 -04008961 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008962 public static final int SIZE_LARGE = 4;
8963
Griff Hazend5f11f92014-05-27 15:40:09 -07008964 /**
8965 * Size value for use with {@link #setCustomSizePreset} to show this notification
8966 * full screen.
8967 * <p>This value is only applicable for custom display notifications created using
8968 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008969 *
8970 * @deprecated Display intents are no longer supported.
Griff Hazend5f11f92014-05-27 15:40:09 -07008971 */
Gus Prevasd7363752018-09-18 14:35:15 -04008972 @Deprecated
Griff Hazend5f11f92014-05-27 15:40:09 -07008973 public static final int SIZE_FULL_SCREEN = 5;
8974
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008975 /**
8976 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
8977 * short amount of time when this notification is displayed on the screen. This
8978 * is the default value.
Gus Prevasd7363752018-09-18 14:35:15 -04008979 *
8980 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008981 */
Gus Prevasd7363752018-09-18 14:35:15 -04008982 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008983 public static final int SCREEN_TIMEOUT_SHORT = 0;
8984
8985 /**
8986 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
8987 * for a longer amount of time when this notification is displayed on the screen.
Gus Prevasd7363752018-09-18 14:35:15 -04008988 *
8989 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008990 */
Gus Prevasd7363752018-09-18 14:35:15 -04008991 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008992 public static final int SCREEN_TIMEOUT_LONG = -1;
8993
Griff Hazen61a9e862014-05-22 16:05:19 -07008994 /** Notification extra which contains wearable extensions */
8995 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
8996
Pete Gastaf6781d2014-10-07 15:17:05 -04008997 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07008998 private static final String KEY_ACTIONS = "actions";
8999 private static final String KEY_FLAGS = "flags";
9000 private static final String KEY_DISPLAY_INTENT = "displayIntent";
9001 private static final String KEY_PAGES = "pages";
9002 private static final String KEY_BACKGROUND = "background";
9003 private static final String KEY_CONTENT_ICON = "contentIcon";
9004 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
9005 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
9006 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
9007 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
9008 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009009 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04009010 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009011 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07009012
9013 // Flags bitwise-ored to mFlags
9014 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
9015 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
9016 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
9017 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009018 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04009019 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04009020 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07009021
9022 // Default value for flags integer
9023 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
9024
9025 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
9026 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
9027
9028 private ArrayList<Action> mActions = new ArrayList<Action>();
9029 private int mFlags = DEFAULT_FLAGS;
9030 private PendingIntent mDisplayIntent;
9031 private ArrayList<Notification> mPages = new ArrayList<Notification>();
9032 private Bitmap mBackground;
9033 private int mContentIcon;
9034 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
9035 private int mContentActionIndex = UNSET_ACTION_INDEX;
9036 private int mCustomSizePreset = SIZE_DEFAULT;
9037 private int mCustomContentHeight;
9038 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009039 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04009040 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009041 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07009042
9043 /**
9044 * Create a {@link android.app.Notification.WearableExtender} with default
9045 * options.
9046 */
9047 public WearableExtender() {
9048 }
9049
9050 public WearableExtender(Notification notif) {
9051 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
9052 if (wearableBundle != null) {
9053 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
9054 if (actions != null) {
9055 mActions.addAll(actions);
9056 }
9057
9058 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
9059 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
9060
9061 Notification[] pages = getNotificationArrayFromBundle(
9062 wearableBundle, KEY_PAGES);
9063 if (pages != null) {
9064 Collections.addAll(mPages, pages);
9065 }
9066
9067 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
9068 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
9069 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
9070 DEFAULT_CONTENT_ICON_GRAVITY);
9071 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
9072 UNSET_ACTION_INDEX);
9073 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
9074 SIZE_DEFAULT);
9075 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
9076 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009077 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04009078 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009079 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07009080 }
9081 }
9082
9083 /**
9084 * Apply wearable extensions to a notification that is being built. This is typically
9085 * called by the {@link android.app.Notification.Builder#extend} method of
9086 * {@link android.app.Notification.Builder}.
9087 */
9088 @Override
9089 public Notification.Builder extend(Notification.Builder builder) {
9090 Bundle wearableBundle = new Bundle();
9091
9092 if (!mActions.isEmpty()) {
9093 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
9094 }
9095 if (mFlags != DEFAULT_FLAGS) {
9096 wearableBundle.putInt(KEY_FLAGS, mFlags);
9097 }
9098 if (mDisplayIntent != null) {
9099 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
9100 }
9101 if (!mPages.isEmpty()) {
9102 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
9103 new Notification[mPages.size()]));
9104 }
9105 if (mBackground != null) {
9106 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
9107 }
9108 if (mContentIcon != 0) {
9109 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
9110 }
9111 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
9112 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
9113 }
9114 if (mContentActionIndex != UNSET_ACTION_INDEX) {
9115 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
9116 mContentActionIndex);
9117 }
9118 if (mCustomSizePreset != SIZE_DEFAULT) {
9119 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
9120 }
9121 if (mCustomContentHeight != 0) {
9122 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
9123 }
9124 if (mGravity != DEFAULT_GRAVITY) {
9125 wearableBundle.putInt(KEY_GRAVITY, mGravity);
9126 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009127 if (mHintScreenTimeout != 0) {
9128 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
9129 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04009130 if (mDismissalId != null) {
9131 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
9132 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009133 if (mBridgeTag != null) {
9134 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
9135 }
Griff Hazen61a9e862014-05-22 16:05:19 -07009136
9137 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
9138 return builder;
9139 }
9140
9141 @Override
9142 public WearableExtender clone() {
9143 WearableExtender that = new WearableExtender();
9144 that.mActions = new ArrayList<Action>(this.mActions);
9145 that.mFlags = this.mFlags;
9146 that.mDisplayIntent = this.mDisplayIntent;
9147 that.mPages = new ArrayList<Notification>(this.mPages);
9148 that.mBackground = this.mBackground;
9149 that.mContentIcon = this.mContentIcon;
9150 that.mContentIconGravity = this.mContentIconGravity;
9151 that.mContentActionIndex = this.mContentActionIndex;
9152 that.mCustomSizePreset = this.mCustomSizePreset;
9153 that.mCustomContentHeight = this.mCustomContentHeight;
9154 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009155 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04009156 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009157 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07009158 return that;
9159 }
9160
9161 /**
9162 * Add a wearable action to this notification.
9163 *
9164 * <p>When wearable actions are added using this method, the set of actions that
9165 * show on a wearable device splits from devices that only show actions added
9166 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
9167 * of which actions display on different devices.
9168 *
9169 * @param action the action to add to this notification
9170 * @return this object for method chaining
9171 * @see android.app.Notification.Action
9172 */
9173 public WearableExtender addAction(Action action) {
9174 mActions.add(action);
9175 return this;
9176 }
9177
9178 /**
9179 * Adds wearable actions to this notification.
9180 *
9181 * <p>When wearable actions are added using this method, the set of actions that
9182 * show on a wearable device splits from devices that only show actions added
9183 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
9184 * of which actions display on different devices.
9185 *
9186 * @param actions the actions to add to this notification
9187 * @return this object for method chaining
9188 * @see android.app.Notification.Action
9189 */
9190 public WearableExtender addActions(List<Action> actions) {
9191 mActions.addAll(actions);
9192 return this;
9193 }
9194
9195 /**
9196 * Clear all wearable actions present on this builder.
9197 * @return this object for method chaining.
9198 * @see #addAction
9199 */
9200 public WearableExtender clearActions() {
9201 mActions.clear();
9202 return this;
9203 }
9204
9205 /**
9206 * Get the wearable actions present on this notification.
9207 */
9208 public List<Action> getActions() {
9209 return mActions;
9210 }
9211
9212 /**
9213 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07009214 * this notification. The {@link PendingIntent} provided should be for an activity.
9215 *
9216 * <pre class="prettyprint">
9217 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
9218 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
9219 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
9220 * Notification notif = new Notification.Builder(context)
9221 * .extend(new Notification.WearableExtender()
9222 * .setDisplayIntent(displayPendingIntent)
9223 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
9224 * .build();</pre>
9225 *
9226 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07009227 * should have an empty task affinity. It is also recommended to use the device
9228 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07009229 *
9230 * <p>Example AndroidManifest.xml entry:
9231 * <pre class="prettyprint">
9232 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
9233 * android:exported=&quot;true&quot;
9234 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07009235 * android:taskAffinity=&quot;&quot;
9236 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07009237 *
9238 * @param intent the {@link PendingIntent} for an activity
9239 * @return this object for method chaining
9240 * @see android.app.Notification.WearableExtender#getDisplayIntent
Gus Prevasd7363752018-09-18 14:35:15 -04009241 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009242 */
Gus Prevasd7363752018-09-18 14:35:15 -04009243 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009244 public WearableExtender setDisplayIntent(PendingIntent intent) {
9245 mDisplayIntent = intent;
9246 return this;
9247 }
9248
9249 /**
9250 * Get the intent to launch inside of an activity view when displaying this
9251 * notification. This {@code PendingIntent} should be for an activity.
Gus Prevasd7363752018-09-18 14:35:15 -04009252 *
9253 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009254 */
Gus Prevasd7363752018-09-18 14:35:15 -04009255 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009256 public PendingIntent getDisplayIntent() {
9257 return mDisplayIntent;
9258 }
9259
9260 /**
9261 * Add an additional page of content to display with this notification. The current
9262 * notification forms the first page, and pages added using this function form
9263 * subsequent pages. This field can be used to separate a notification into multiple
9264 * sections.
9265 *
9266 * @param page the notification to add as another page
9267 * @return this object for method chaining
9268 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04009269 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009270 */
Gus Prevasd7363752018-09-18 14:35:15 -04009271 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009272 public WearableExtender addPage(Notification page) {
9273 mPages.add(page);
9274 return this;
9275 }
9276
9277 /**
9278 * Add additional pages of content to display with this notification. The current
9279 * notification forms the first page, and pages added using this function form
9280 * subsequent pages. This field can be used to separate a notification into multiple
9281 * sections.
9282 *
9283 * @param pages a list of notifications
9284 * @return this object for method chaining
9285 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04009286 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009287 */
Gus Prevasd7363752018-09-18 14:35:15 -04009288 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009289 public WearableExtender addPages(List<Notification> pages) {
9290 mPages.addAll(pages);
9291 return this;
9292 }
9293
9294 /**
9295 * Clear all additional pages present on this builder.
9296 * @return this object for method chaining.
9297 * @see #addPage
Gus Prevasd7363752018-09-18 14:35:15 -04009298 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009299 */
Gus Prevasd7363752018-09-18 14:35:15 -04009300 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009301 public WearableExtender clearPages() {
9302 mPages.clear();
9303 return this;
9304 }
9305
9306 /**
9307 * Get the array of additional pages of content for displaying this notification. The
9308 * current notification forms the first page, and elements within this array form
9309 * subsequent pages. This field can be used to separate a notification into multiple
9310 * sections.
9311 * @return the pages for this notification
Gus Prevasd7363752018-09-18 14:35:15 -04009312 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009313 */
Gus Prevasd7363752018-09-18 14:35:15 -04009314 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009315 public List<Notification> getPages() {
9316 return mPages;
9317 }
9318
9319 /**
9320 * Set a background image to be displayed behind the notification content.
9321 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
9322 * will work with any notification style.
9323 *
9324 * @param background the background bitmap
9325 * @return this object for method chaining
9326 * @see android.app.Notification.WearableExtender#getBackground
Gus Prevasd7363752018-09-18 14:35:15 -04009327 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009328 */
Gus Prevasd7363752018-09-18 14:35:15 -04009329 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009330 public WearableExtender setBackground(Bitmap background) {
9331 mBackground = background;
9332 return this;
9333 }
9334
9335 /**
9336 * Get a background image to be displayed behind the notification content.
9337 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
9338 * will work with any notification style.
9339 *
9340 * @return the background image
9341 * @see android.app.Notification.WearableExtender#setBackground
Gus Prevasd7363752018-09-18 14:35:15 -04009342 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009343 */
Gus Prevasd7363752018-09-18 14:35:15 -04009344 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009345 public Bitmap getBackground() {
9346 return mBackground;
9347 }
9348
9349 /**
9350 * Set an icon that goes with the content of this notification.
9351 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009352 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009353 public WearableExtender setContentIcon(int icon) {
9354 mContentIcon = icon;
9355 return this;
9356 }
9357
9358 /**
9359 * Get an icon that goes with the content of this notification.
9360 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009361 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009362 public int getContentIcon() {
9363 return mContentIcon;
9364 }
9365
9366 /**
9367 * Set the gravity that the content icon should have within the notification display.
9368 * Supported values include {@link android.view.Gravity#START} and
9369 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
9370 * @see #setContentIcon
9371 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009372 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009373 public WearableExtender setContentIconGravity(int contentIconGravity) {
9374 mContentIconGravity = contentIconGravity;
9375 return this;
9376 }
9377
9378 /**
9379 * Get the gravity that the content icon should have within the notification display.
9380 * Supported values include {@link android.view.Gravity#START} and
9381 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
9382 * @see #getContentIcon
9383 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009384 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009385 public int getContentIconGravity() {
9386 return mContentIconGravity;
9387 }
9388
9389 /**
Gus Prevasd7363752018-09-18 14:35:15 -04009390 * Set an action from this notification's actions as the primary action. If the action has a
9391 * {@link RemoteInput} associated with it, shortcuts to the options for that input are shown
9392 * directly on the notification.
Griff Hazen14f57992014-05-26 09:07:14 -07009393 *
Gus Prevasd7363752018-09-18 14:35:15 -04009394 * @param actionIndex The index of the primary action.
Griff Hazen14f57992014-05-26 09:07:14 -07009395 * If wearable actions were added to the main notification, this index
9396 * will apply to that list, otherwise it will apply to the regular
9397 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07009398 */
9399 public WearableExtender setContentAction(int actionIndex) {
9400 mContentActionIndex = actionIndex;
9401 return this;
9402 }
9403
9404 /**
Gus Prevasd7363752018-09-18 14:35:15 -04009405 * Get the index of the notification action, if any, that was specified as the primary
9406 * action.
Griff Hazen14f57992014-05-26 09:07:14 -07009407 *
9408 * <p>If wearable specific actions were added to the main notification, this index will
9409 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07009410 *
9411 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07009412 */
9413 public int getContentAction() {
9414 return mContentActionIndex;
9415 }
9416
9417 /**
9418 * Set the gravity that this notification should have within the available viewport space.
9419 * Supported values include {@link android.view.Gravity#TOP},
9420 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
9421 * The default value is {@link android.view.Gravity#BOTTOM}.
9422 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009423 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009424 public WearableExtender setGravity(int gravity) {
9425 mGravity = gravity;
9426 return this;
9427 }
9428
9429 /**
9430 * Get the gravity that this notification should have within the available viewport space.
9431 * Supported values include {@link android.view.Gravity#TOP},
9432 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
9433 * The default value is {@link android.view.Gravity#BOTTOM}.
9434 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009435 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009436 public int getGravity() {
9437 return mGravity;
9438 }
9439
9440 /**
9441 * Set the custom size preset for the display of this notification out of the available
9442 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
9443 * {@link #SIZE_LARGE}.
9444 * <p>Some custom size presets are only applicable for custom display notifications created
9445 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
9446 * documentation for the preset in question. See also
9447 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
9448 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009449 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009450 public WearableExtender setCustomSizePreset(int sizePreset) {
9451 mCustomSizePreset = sizePreset;
9452 return this;
9453 }
9454
9455 /**
9456 * Get the custom size preset for the display of this notification out of the available
9457 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
9458 * {@link #SIZE_LARGE}.
9459 * <p>Some custom size presets are only applicable for custom display notifications created
9460 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
9461 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
9462 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009463 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009464 public int getCustomSizePreset() {
9465 return mCustomSizePreset;
9466 }
9467
9468 /**
9469 * Set the custom height in pixels for the display of this notification's content.
9470 * <p>This option is only available for custom display notifications created
9471 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
9472 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
9473 * {@link #getCustomContentHeight}.
9474 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009475 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009476 public WearableExtender setCustomContentHeight(int height) {
9477 mCustomContentHeight = height;
9478 return this;
9479 }
9480
9481 /**
9482 * Get the custom height in pixels for the display of this notification's content.
9483 * <p>This option is only available for custom display notifications created
9484 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
9485 * {@link #setCustomContentHeight}.
9486 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009487 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009488 public int getCustomContentHeight() {
9489 return mCustomContentHeight;
9490 }
9491
9492 /**
9493 * Set whether the scrolling position for the contents of this notification should start
9494 * at the bottom of the contents instead of the top when the contents are too long to
9495 * display within the screen. Default is false (start scroll at the top).
9496 */
9497 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
9498 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
9499 return this;
9500 }
9501
9502 /**
9503 * Get whether the scrolling position for the contents of this notification should start
9504 * at the bottom of the contents instead of the top when the contents are too long to
9505 * display within the screen. Default is false (start scroll at the top).
9506 */
9507 public boolean getStartScrollBottom() {
9508 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
9509 }
9510
9511 /**
9512 * Set whether the content intent is available when the wearable device is not connected
9513 * to a companion device. The user can still trigger this intent when the wearable device
9514 * is offline, but a visual hint will indicate that the content intent may not be available.
9515 * Defaults to true.
9516 */
9517 public WearableExtender setContentIntentAvailableOffline(
9518 boolean contentIntentAvailableOffline) {
9519 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
9520 return this;
9521 }
9522
9523 /**
9524 * Get whether the content intent is available when the wearable device is not connected
9525 * to a companion device. The user can still trigger this intent when the wearable device
9526 * is offline, but a visual hint will indicate that the content intent may not be available.
9527 * Defaults to true.
9528 */
9529 public boolean getContentIntentAvailableOffline() {
9530 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
9531 }
9532
9533 /**
9534 * Set a hint that this notification's icon should not be displayed.
9535 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
9536 * @return this object for method chaining
9537 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009538 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009539 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
9540 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
9541 return this;
9542 }
9543
9544 /**
9545 * Get a hint that this notification's icon should not be displayed.
9546 * @return {@code true} if this icon should not be displayed, false otherwise.
9547 * The default value is {@code false} if this was never set.
9548 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009549 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009550 public boolean getHintHideIcon() {
9551 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
9552 }
9553
9554 /**
9555 * Set a visual hint that only the background image of this notification should be
9556 * displayed, and other semantic content should be hidden. This hint is only applicable
9557 * to sub-pages added using {@link #addPage}.
9558 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009559 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009560 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
9561 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
9562 return this;
9563 }
9564
9565 /**
9566 * Get a visual hint that only the background image of this notification should be
9567 * displayed, and other semantic content should be hidden. This hint is only applicable
9568 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
9569 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009570 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009571 public boolean getHintShowBackgroundOnly() {
9572 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
9573 }
9574
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009575 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009576 * Set a hint that this notification's background should not be clipped if possible,
9577 * and should instead be resized to fully display on the screen, retaining the aspect
9578 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009579 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
9580 * @return this object for method chaining
9581 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009582 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009583 public WearableExtender setHintAvoidBackgroundClipping(
9584 boolean hintAvoidBackgroundClipping) {
9585 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
9586 return this;
9587 }
9588
9589 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009590 * Get a hint that this notification's background should not be clipped if possible,
9591 * and should instead be resized to fully display on the screen, retaining the aspect
9592 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009593 * @return {@code true} if it's ok if the background is clipped on the screen, false
9594 * otherwise. The default value is {@code false} if this was never set.
9595 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009596 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009597 public boolean getHintAvoidBackgroundClipping() {
9598 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
9599 }
9600
9601 /**
9602 * Set a hint that the screen should remain on for at least this duration when
9603 * this notification is displayed on the screen.
9604 * @param timeout The requested screen timeout in milliseconds. Can also be either
9605 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9606 * @return this object for method chaining
9607 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009608 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009609 public WearableExtender setHintScreenTimeout(int timeout) {
9610 mHintScreenTimeout = timeout;
9611 return this;
9612 }
9613
9614 /**
9615 * Get the duration, in milliseconds, that the screen should remain on for
9616 * when this notification is displayed.
9617 * @return the duration in milliseconds if > 0, or either one of the sentinel values
9618 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9619 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009620 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009621 public int getHintScreenTimeout() {
9622 return mHintScreenTimeout;
9623 }
9624
Alex Hills9ab3a232016-04-05 14:54:56 -04009625 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04009626 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
9627 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9628 * qr codes, as well as other simple black-and-white tickets.
9629 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
9630 * @return this object for method chaining
Gus Prevasd7363752018-09-18 14:35:15 -04009631 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009632 */
Gus Prevasd7363752018-09-18 14:35:15 -04009633 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009634 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
9635 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
9636 return this;
9637 }
9638
9639 /**
9640 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
9641 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9642 * qr codes, as well as other simple black-and-white tickets.
9643 * @return {@code true} if it should be displayed in ambient, false otherwise
9644 * otherwise. The default value is {@code false} if this was never set.
Gus Prevasd7363752018-09-18 14:35:15 -04009645 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009646 */
Gus Prevasd7363752018-09-18 14:35:15 -04009647 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009648 public boolean getHintAmbientBigPicture() {
9649 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
9650 }
9651
9652 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04009653 * Set a hint that this notification's content intent will launch an {@link Activity}
9654 * directly, telling the platform that it can generate the appropriate transitions.
9655 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
9656 * an activity and transitions should be generated, false otherwise.
9657 * @return this object for method chaining
9658 */
9659 public WearableExtender setHintContentIntentLaunchesActivity(
9660 boolean hintContentIntentLaunchesActivity) {
9661 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
9662 return this;
9663 }
9664
9665 /**
9666 * Get a hint that this notification's content intent will launch an {@link Activity}
9667 * directly, telling the platform that it can generate the appropriate transitions
9668 * @return {@code true} if the content intent will launch an activity and transitions should
9669 * be generated, false otherwise. The default value is {@code false} if this was never set.
9670 */
9671 public boolean getHintContentIntentLaunchesActivity() {
9672 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
9673 }
9674
Nadia Benbernou948627e2016-04-14 14:41:08 -04009675 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009676 * Sets the dismissal id for this notification. If a notification is posted with a
9677 * dismissal id, then when that notification is canceled, notifications on other wearables
9678 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04009679 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009680 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04009681 * @param dismissalId the dismissal id of the notification.
9682 * @return this object for method chaining
9683 */
9684 public WearableExtender setDismissalId(String dismissalId) {
9685 mDismissalId = dismissalId;
9686 return this;
9687 }
9688
9689 /**
9690 * Returns the dismissal id of the notification.
9691 * @return the dismissal id of the notification or null if it has not been set.
9692 */
9693 public String getDismissalId() {
9694 return mDismissalId;
9695 }
9696
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009697 /**
9698 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
9699 * posted from a phone to provide finer-grained control on what notifications are bridged
9700 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
9701 * Features to Notifications</a> for more information.
9702 * @param bridgeTag the bridge tag of the notification.
9703 * @return this object for method chaining
9704 */
9705 public WearableExtender setBridgeTag(String bridgeTag) {
9706 mBridgeTag = bridgeTag;
9707 return this;
9708 }
9709
9710 /**
9711 * Returns the bridge tag of the notification.
9712 * @return the bridge tag or null if not present.
9713 */
9714 public String getBridgeTag() {
9715 return mBridgeTag;
9716 }
9717
Griff Hazen61a9e862014-05-22 16:05:19 -07009718 private void setFlag(int mask, boolean value) {
9719 if (value) {
9720 mFlags |= mask;
9721 } else {
9722 mFlags &= ~mask;
9723 }
9724 }
9725 }
9726
9727 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009728 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
9729 * with car extensions:
9730 *
9731 * <ol>
9732 * <li>Create an {@link Notification.Builder}, setting any desired
9733 * properties.
9734 * <li>Create a {@link CarExtender}.
9735 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
9736 * {@link CarExtender}.
9737 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9738 * to apply the extensions to a notification.
9739 * </ol>
9740 *
9741 * <pre class="prettyprint">
9742 * Notification notification = new Notification.Builder(context)
9743 * ...
9744 * .extend(new CarExtender()
9745 * .set*(...))
9746 * .build();
9747 * </pre>
9748 *
9749 * <p>Car extensions can be accessed on an existing notification by using the
9750 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
9751 * to access values.
9752 */
9753 public static final class CarExtender implements Extender {
9754 private static final String TAG = "CarExtender";
9755
9756 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
9757 private static final String EXTRA_LARGE_ICON = "large_icon";
9758 private static final String EXTRA_CONVERSATION = "car_conversation";
9759 private static final String EXTRA_COLOR = "app_color";
9760
9761 private Bitmap mLargeIcon;
9762 private UnreadConversation mUnreadConversation;
9763 private int mColor = Notification.COLOR_DEFAULT;
9764
9765 /**
9766 * Create a {@link CarExtender} with default options.
9767 */
9768 public CarExtender() {
9769 }
9770
9771 /**
9772 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
9773 *
9774 * @param notif The notification from which to copy options.
9775 */
9776 public CarExtender(Notification notif) {
9777 Bundle carBundle = notif.extras == null ?
9778 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
9779 if (carBundle != null) {
9780 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
9781 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
9782
9783 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
9784 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
9785 }
9786 }
9787
9788 /**
9789 * Apply car extensions to a notification that is being built. This is typically called by
9790 * the {@link Notification.Builder#extend(Notification.Extender)}
9791 * method of {@link Notification.Builder}.
9792 */
9793 @Override
9794 public Notification.Builder extend(Notification.Builder builder) {
9795 Bundle carExtensions = new Bundle();
9796
9797 if (mLargeIcon != null) {
9798 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
9799 }
9800 if (mColor != Notification.COLOR_DEFAULT) {
9801 carExtensions.putInt(EXTRA_COLOR, mColor);
9802 }
9803
9804 if (mUnreadConversation != null) {
9805 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
9806 carExtensions.putBundle(EXTRA_CONVERSATION, b);
9807 }
9808
9809 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
9810 return builder;
9811 }
9812
9813 /**
9814 * Sets the accent color to use when Android Auto presents the notification.
9815 *
9816 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
9817 * to accent the displayed notification. However, not all colors are acceptable in an
9818 * automotive setting. This method can be used to override the color provided in the
9819 * notification in such a situation.
9820 */
Tor Norbye80756e32015-03-02 09:39:27 -08009821 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009822 mColor = color;
9823 return this;
9824 }
9825
9826 /**
9827 * Gets the accent color.
9828 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04009829 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009830 */
Tor Norbye80756e32015-03-02 09:39:27 -08009831 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009832 public int getColor() {
9833 return mColor;
9834 }
9835
9836 /**
9837 * Sets the large icon of the car notification.
9838 *
9839 * If no large icon is set in the extender, Android Auto will display the icon
9840 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
9841 *
9842 * @param largeIcon The large icon to use in the car notification.
9843 * @return This object for method chaining.
9844 */
9845 public CarExtender setLargeIcon(Bitmap largeIcon) {
9846 mLargeIcon = largeIcon;
9847 return this;
9848 }
9849
9850 /**
9851 * Gets the large icon used in this car notification, or null if no icon has been set.
9852 *
9853 * @return The large icon for the car notification.
9854 * @see CarExtender#setLargeIcon
9855 */
9856 public Bitmap getLargeIcon() {
9857 return mLargeIcon;
9858 }
9859
9860 /**
9861 * Sets the unread conversation in a message notification.
9862 *
9863 * @param unreadConversation The unread part of the conversation this notification conveys.
9864 * @return This object for method chaining.
9865 */
9866 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
9867 mUnreadConversation = unreadConversation;
9868 return this;
9869 }
9870
9871 /**
9872 * Returns the unread conversation conveyed by this notification.
9873 * @see #setUnreadConversation(UnreadConversation)
9874 */
9875 public UnreadConversation getUnreadConversation() {
9876 return mUnreadConversation;
9877 }
9878
9879 /**
9880 * A class which holds the unread messages from a conversation.
9881 */
9882 public static class UnreadConversation {
9883 private static final String KEY_AUTHOR = "author";
9884 private static final String KEY_TEXT = "text";
9885 private static final String KEY_MESSAGES = "messages";
9886 private static final String KEY_REMOTE_INPUT = "remote_input";
9887 private static final String KEY_ON_REPLY = "on_reply";
9888 private static final String KEY_ON_READ = "on_read";
9889 private static final String KEY_PARTICIPANTS = "participants";
9890 private static final String KEY_TIMESTAMP = "timestamp";
9891
9892 private final String[] mMessages;
9893 private final RemoteInput mRemoteInput;
9894 private final PendingIntent mReplyPendingIntent;
9895 private final PendingIntent mReadPendingIntent;
9896 private final String[] mParticipants;
9897 private final long mLatestTimestamp;
9898
9899 UnreadConversation(String[] messages, RemoteInput remoteInput,
9900 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
9901 String[] participants, long latestTimestamp) {
9902 mMessages = messages;
9903 mRemoteInput = remoteInput;
9904 mReadPendingIntent = readPendingIntent;
9905 mReplyPendingIntent = replyPendingIntent;
9906 mParticipants = participants;
9907 mLatestTimestamp = latestTimestamp;
9908 }
9909
9910 /**
9911 * Gets the list of messages conveyed by this notification.
9912 */
9913 public String[] getMessages() {
9914 return mMessages;
9915 }
9916
9917 /**
9918 * Gets the remote input that will be used to convey the response to a message list, or
9919 * null if no such remote input exists.
9920 */
9921 public RemoteInput getRemoteInput() {
9922 return mRemoteInput;
9923 }
9924
9925 /**
9926 * Gets the pending intent that will be triggered when the user replies to this
9927 * notification.
9928 */
9929 public PendingIntent getReplyPendingIntent() {
9930 return mReplyPendingIntent;
9931 }
9932
9933 /**
9934 * Gets the pending intent that Android Auto will send after it reads aloud all messages
9935 * in this object's message list.
9936 */
9937 public PendingIntent getReadPendingIntent() {
9938 return mReadPendingIntent;
9939 }
9940
9941 /**
9942 * Gets the participants in the conversation.
9943 */
9944 public String[] getParticipants() {
9945 return mParticipants;
9946 }
9947
9948 /**
9949 * Gets the firs participant in the conversation.
9950 */
9951 public String getParticipant() {
9952 return mParticipants.length > 0 ? mParticipants[0] : null;
9953 }
9954
9955 /**
9956 * Gets the timestamp of the conversation.
9957 */
9958 public long getLatestTimestamp() {
9959 return mLatestTimestamp;
9960 }
9961
9962 Bundle getBundleForUnreadConversation() {
9963 Bundle b = new Bundle();
9964 String author = null;
9965 if (mParticipants != null && mParticipants.length > 1) {
9966 author = mParticipants[0];
9967 }
9968 Parcelable[] messages = new Parcelable[mMessages.length];
9969 for (int i = 0; i < messages.length; i++) {
9970 Bundle m = new Bundle();
9971 m.putString(KEY_TEXT, mMessages[i]);
9972 m.putString(KEY_AUTHOR, author);
9973 messages[i] = m;
9974 }
9975 b.putParcelableArray(KEY_MESSAGES, messages);
9976 if (mRemoteInput != null) {
9977 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
9978 }
9979 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
9980 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
9981 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
9982 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
9983 return b;
9984 }
9985
9986 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
9987 if (b == null) {
9988 return null;
9989 }
9990 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
9991 String[] messages = null;
9992 if (parcelableMessages != null) {
9993 String[] tmp = new String[parcelableMessages.length];
9994 boolean success = true;
9995 for (int i = 0; i < tmp.length; i++) {
9996 if (!(parcelableMessages[i] instanceof Bundle)) {
9997 success = false;
9998 break;
9999 }
10000 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
10001 if (tmp[i] == null) {
10002 success = false;
10003 break;
10004 }
10005 }
10006 if (success) {
10007 messages = tmp;
10008 } else {
10009 return null;
10010 }
10011 }
10012
10013 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
10014 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
10015
10016 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
10017
10018 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
10019 if (participants == null || participants.length != 1) {
10020 return null;
10021 }
10022
10023 return new UnreadConversation(messages,
10024 remoteInput,
10025 onReply,
10026 onRead,
10027 participants, b.getLong(KEY_TIMESTAMP));
10028 }
10029 };
10030
10031 /**
10032 * Builder class for {@link CarExtender.UnreadConversation} objects.
10033 */
10034 public static class Builder {
10035 private final List<String> mMessages = new ArrayList<String>();
10036 private final String mParticipant;
10037 private RemoteInput mRemoteInput;
10038 private PendingIntent mReadPendingIntent;
10039 private PendingIntent mReplyPendingIntent;
10040 private long mLatestTimestamp;
10041
10042 /**
10043 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
10044 *
10045 * @param name The name of the other participant in the conversation.
10046 */
10047 public Builder(String name) {
10048 mParticipant = name;
10049 }
10050
10051 /**
10052 * Appends a new unread message to the list of messages for this conversation.
10053 *
10054 * The messages should be added from oldest to newest.
10055 *
10056 * @param message The text of the new unread message.
10057 * @return This object for method chaining.
10058 */
10059 public Builder addMessage(String message) {
10060 mMessages.add(message);
10061 return this;
10062 }
10063
10064 /**
10065 * Sets the pending intent and remote input which will convey the reply to this
10066 * notification.
10067 *
10068 * @param pendingIntent The pending intent which will be triggered on a reply.
10069 * @param remoteInput The remote input parcelable which will carry the reply.
10070 * @return This object for method chaining.
10071 *
10072 * @see CarExtender.UnreadConversation#getRemoteInput
10073 * @see CarExtender.UnreadConversation#getReplyPendingIntent
10074 */
10075 public Builder setReplyAction(
10076 PendingIntent pendingIntent, RemoteInput remoteInput) {
10077 mRemoteInput = remoteInput;
10078 mReplyPendingIntent = pendingIntent;
10079
10080 return this;
10081 }
10082
10083 /**
10084 * Sets the pending intent that will be sent once the messages in this notification
10085 * are read.
10086 *
10087 * @param pendingIntent The pending intent to use.
10088 * @return This object for method chaining.
10089 */
10090 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
10091 mReadPendingIntent = pendingIntent;
10092 return this;
10093 }
10094
10095 /**
10096 * Sets the timestamp of the most recent message in an unread conversation.
10097 *
10098 * If a messaging notification has been posted by your application and has not
10099 * yet been cancelled, posting a later notification with the same id and tag
10100 * but without a newer timestamp may result in Android Auto not displaying a
10101 * heads up notification for the later notification.
10102 *
10103 * @param timestamp The timestamp of the most recent message in the conversation.
10104 * @return This object for method chaining.
10105 */
10106 public Builder setLatestTimestamp(long timestamp) {
10107 mLatestTimestamp = timestamp;
10108 return this;
10109 }
10110
10111 /**
10112 * Builds a new unread conversation object.
10113 *
10114 * @return The new unread conversation object.
10115 */
10116 public UnreadConversation build() {
10117 String[] messages = mMessages.toArray(new String[mMessages.size()]);
10118 String[] participants = { mParticipant };
10119 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
10120 mReadPendingIntent, participants, mLatestTimestamp);
10121 }
10122 }
10123 }
10124
10125 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010126 * <p>Helper class to add Android TV extensions to notifications. To create a notification
10127 * with a TV extension:
10128 *
10129 * <ol>
10130 * <li>Create an {@link Notification.Builder}, setting any desired properties.
10131 * <li>Create a {@link TvExtender}.
10132 * <li>Set TV-specific properties using the {@code set} methods of
10133 * {@link TvExtender}.
10134 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
10135 * to apply the extension to a notification.
10136 * </ol>
10137 *
10138 * <pre class="prettyprint">
10139 * Notification notification = new Notification.Builder(context)
10140 * ...
10141 * .extend(new TvExtender()
10142 * .set*(...))
10143 * .build();
10144 * </pre>
10145 *
10146 * <p>TV extensions can be accessed on an existing notification by using the
10147 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
10148 * to access values.
10149 *
10150 * @hide
10151 */
10152 @SystemApi
10153 public static final class TvExtender implements Extender {
10154 private static final String TAG = "TvExtender";
10155
10156 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
10157 private static final String EXTRA_FLAGS = "flags";
10158 private static final String EXTRA_CONTENT_INTENT = "content_intent";
10159 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010160 private static final String EXTRA_CHANNEL_ID = "channel_id";
Rhiannon Malia1a083932018-01-24 15:02:30 -080010161 private static final String EXTRA_SUPPRESS_SHOW_OVER_APPS = "suppressShowOverApps";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010162
10163 // Flags bitwise-ored to mFlags
10164 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
10165
10166 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010167 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010168 private PendingIntent mContentIntent;
10169 private PendingIntent mDeleteIntent;
Rhiannon Malia1a083932018-01-24 15:02:30 -080010170 private boolean mSuppressShowOverApps;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010171
10172 /**
10173 * Create a {@link TvExtender} with default options.
10174 */
10175 public TvExtender() {
10176 mFlags = FLAG_AVAILABLE_ON_TV;
10177 }
10178
10179 /**
10180 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
10181 *
10182 * @param notif The notification from which to copy options.
10183 */
10184 public TvExtender(Notification notif) {
10185 Bundle bundle = notif.extras == null ?
10186 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
10187 if (bundle != null) {
10188 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010189 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Rhiannon Malia1a083932018-01-24 15:02:30 -080010190 mSuppressShowOverApps = bundle.getBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010191 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
10192 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
10193 }
10194 }
10195
10196 /**
10197 * Apply a TV extension to a notification that is being built. This is typically called by
10198 * the {@link Notification.Builder#extend(Notification.Extender)}
10199 * method of {@link Notification.Builder}.
10200 */
10201 @Override
10202 public Notification.Builder extend(Notification.Builder builder) {
10203 Bundle bundle = new Bundle();
10204
10205 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010206 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Rhiannon Malia1a083932018-01-24 15:02:30 -080010207 bundle.putBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS, mSuppressShowOverApps);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010208 if (mContentIntent != null) {
10209 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
10210 }
10211
10212 if (mDeleteIntent != null) {
10213 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
10214 }
10215
10216 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
10217 return builder;
10218 }
10219
10220 /**
10221 * Returns true if this notification should be shown on TV. This method return true
10222 * if the notification was extended with a TvExtender.
10223 */
10224 public boolean isAvailableOnTv() {
10225 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
10226 }
10227
10228 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010229 * Specifies the channel the notification should be delivered on when shown on TV.
10230 * It can be different from the channel that the notification is delivered to when
10231 * posting on a non-TV device.
10232 */
10233 public TvExtender setChannel(String channelId) {
10234 mChannelId = channelId;
10235 return this;
10236 }
10237
10238 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -040010239 * Specifies the channel the notification should be delivered on when shown on TV.
10240 * It can be different from the channel that the notification is delivered to when
10241 * posting on a non-TV device.
10242 */
10243 public TvExtender setChannelId(String channelId) {
10244 mChannelId = channelId;
10245 return this;
10246 }
10247
Jeff Sharkey000ce802017-04-29 13:13:27 -060010248 /** @removed */
10249 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010250 public String getChannel() {
10251 return mChannelId;
10252 }
10253
10254 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -040010255 * Returns the id of the channel this notification posts to on TV.
10256 */
10257 public String getChannelId() {
10258 return mChannelId;
10259 }
10260
10261 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010262 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
10263 * If provided, it is used instead of the content intent specified
10264 * at the level of Notification.
10265 */
10266 public TvExtender setContentIntent(PendingIntent intent) {
10267 mContentIntent = intent;
10268 return this;
10269 }
10270
10271 /**
10272 * Returns the TV-specific content intent. If this method returns null, the
10273 * main content intent on the notification should be used.
10274 *
10275 * @see {@link Notification#contentIntent}
10276 */
10277 public PendingIntent getContentIntent() {
10278 return mContentIntent;
10279 }
10280
10281 /**
10282 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
10283 * by the user on TV. If provided, it is used instead of the delete intent specified
10284 * at the level of Notification.
10285 */
10286 public TvExtender setDeleteIntent(PendingIntent intent) {
10287 mDeleteIntent = intent;
10288 return this;
10289 }
10290
10291 /**
10292 * Returns the TV-specific delete intent. If this method returns null, the
10293 * main delete intent on the notification should be used.
10294 *
10295 * @see {@link Notification#deleteIntent}
10296 */
10297 public PendingIntent getDeleteIntent() {
10298 return mDeleteIntent;
10299 }
Rhiannon Malia1a083932018-01-24 15:02:30 -080010300
10301 /**
10302 * Specifies whether this notification should suppress showing a message over top of apps
10303 * outside of the launcher.
10304 */
10305 public TvExtender setSuppressShowOverApps(boolean suppress) {
10306 mSuppressShowOverApps = suppress;
10307 return this;
10308 }
10309
10310 /**
10311 * Returns true if this notification should not show messages over top of apps
10312 * outside of the launcher.
10313 */
10314 public boolean getSuppressShowOverApps() {
10315 return mSuppressShowOverApps;
10316 }
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010317 }
10318
10319 /**
Griff Hazen61a9e862014-05-22 16:05:19 -070010320 * Get an array of Notification objects from a parcelable array bundle field.
10321 * Update the bundle to have a typed array so fetches in the future don't need
10322 * to do an array copy.
10323 */
10324 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
10325 Parcelable[] array = bundle.getParcelableArray(key);
10326 if (array instanceof Notification[] || array == null) {
10327 return (Notification[]) array;
10328 }
10329 Notification[] typedArray = Arrays.copyOf(array, array.length,
10330 Notification[].class);
10331 bundle.putParcelableArray(key, typedArray);
10332 return typedArray;
10333 }
Christoph Studer4600f9b2014-07-22 22:44:43 +020010334
10335 private static class BuilderRemoteViews extends RemoteViews {
10336 public BuilderRemoteViews(Parcel parcel) {
10337 super(parcel);
10338 }
10339
Kenny Guy77320062014-08-27 21:37:15 +010010340 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
10341 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +020010342 }
10343
10344 @Override
10345 public BuilderRemoteViews clone() {
10346 Parcel p = Parcel.obtain();
10347 writeToParcel(p, 0);
10348 p.setDataPosition(0);
10349 BuilderRemoteViews brv = new BuilderRemoteViews(p);
10350 p.recycle();
10351 return brv;
10352 }
10353 }
Adrian Roos70d7aa32017-01-11 15:39:06 -080010354
Selim Cinek384804b2018-04-18 14:31:07 +080010355 /**
10356 * A result object where information about the template that was created is saved.
10357 */
10358 private static class TemplateBindResult {
Selim Cinek1c72fa02018-04-23 18:00:54 +080010359 int mIconMarginEnd;
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010360 boolean mRightIconContainerVisible;
Selim Cinek384804b2018-04-18 14:31:07 +080010361
10362 /**
Selim Cinek1c72fa02018-04-23 18:00:54 +080010363 * Get the margin end that needs to be added to any fields that may overlap
Selim Cinek384804b2018-04-18 14:31:07 +080010364 * with the right actions.
10365 */
Selim Cinek1c72fa02018-04-23 18:00:54 +080010366 public int getIconMarginEnd() {
10367 return mIconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +080010368 }
10369
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010370 /**
10371 * Is the icon container visible on the right size because of the reply button or the
10372 * right icon.
10373 */
10374 public boolean isRightIconContainerVisible() {
10375 return mRightIconContainerVisible;
10376 }
10377
Selim Cinek1c72fa02018-04-23 18:00:54 +080010378 public void setIconMarginEnd(int iconMarginEnd) {
10379 this.mIconMarginEnd = iconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +080010380 }
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010381
10382 public void setRightIconContainerVisible(boolean iconContainerVisible) {
10383 mRightIconContainerVisible = iconContainerVisible;
10384 }
Selim Cinek384804b2018-04-18 14:31:07 +080010385 }
10386
Adrian Roos70d7aa32017-01-11 15:39:06 -080010387 private static class StandardTemplateParams {
10388 boolean hasProgress = true;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010389 CharSequence title;
10390 CharSequence text;
Selim Cinekafeed292017-12-12 17:32:44 -080010391 CharSequence headerTextSecondary;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010392 CharSequence summaryText;
Selim Cinekbee4e072018-05-21 22:06:43 -070010393 int maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek88188f22017-09-19 16:46:56 -070010394 boolean hideLargeIcon;
Selim Cinek384804b2018-04-18 14:31:07 +080010395 boolean hideReplyIcon;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010396 boolean allowColorization = true;
10397 boolean forceDefaultColor = false;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010398
10399 final StandardTemplateParams reset() {
10400 hasProgress = true;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010401 title = null;
10402 text = null;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010403 summaryText = null;
Selim Cinekafeed292017-12-12 17:32:44 -080010404 headerTextSecondary = null;
Selim Cinekbee4e072018-05-21 22:06:43 -070010405 maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010406 allowColorization = true;
10407 forceDefaultColor = false;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010408 return this;
10409 }
10410
10411 final StandardTemplateParams hasProgress(boolean hasProgress) {
10412 this.hasProgress = hasProgress;
10413 return this;
10414 }
10415
10416 final StandardTemplateParams title(CharSequence title) {
10417 this.title = title;
10418 return this;
10419 }
10420
10421 final StandardTemplateParams text(CharSequence text) {
10422 this.text = text;
10423 return this;
10424 }
10425
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010426 final StandardTemplateParams summaryText(CharSequence text) {
10427 this.summaryText = text;
10428 return this;
10429 }
10430
Selim Cinekafeed292017-12-12 17:32:44 -080010431 final StandardTemplateParams headerTextSecondary(CharSequence text) {
10432 this.headerTextSecondary = text;
10433 return this;
10434 }
10435
Selim Cinek384804b2018-04-18 14:31:07 +080010436 final StandardTemplateParams hideLargeIcon(boolean hideLargeIcon) {
10437 this.hideLargeIcon = hideLargeIcon;
Selim Cinek88188f22017-09-19 16:46:56 -070010438 return this;
10439 }
10440
Selim Cinek384804b2018-04-18 14:31:07 +080010441 final StandardTemplateParams hideReplyIcon(boolean hideReplyIcon) {
10442 this.hideReplyIcon = hideReplyIcon;
Selim Cinek88188f22017-09-19 16:46:56 -070010443 return this;
10444 }
10445
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010446 final StandardTemplateParams disallowColorization() {
10447 this.allowColorization = false;
10448 return this;
10449 }
10450
10451 final StandardTemplateParams forceDefaultColor() {
10452 this.forceDefaultColor = true;
10453 return this;
10454 }
10455
Adrian Roos70d7aa32017-01-11 15:39:06 -080010456 final StandardTemplateParams fillTextsFrom(Builder b) {
10457 Bundle extras = b.mN.extras;
Lucas Dupin00be88f2019-01-03 17:50:52 -080010458 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE));
Lucas Dupin06c5e642017-09-13 16:34:58 -070010459
Lucas Dupin06c5e642017-09-13 16:34:58 -070010460 CharSequence text = extras.getCharSequence(EXTRA_BIG_TEXT);
Lucas Dupin00be88f2019-01-03 17:50:52 -080010461 if (TextUtils.isEmpty(text)) {
Lucas Dupin06c5e642017-09-13 16:34:58 -070010462 text = extras.getCharSequence(EXTRA_TEXT);
10463 }
Lucas Dupin00be88f2019-01-03 17:50:52 -080010464 this.text = b.processLegacyText(text);
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010465 this.summaryText = extras.getCharSequence(EXTRA_SUB_TEXT);
Adrian Roos70d7aa32017-01-11 15:39:06 -080010466 return this;
10467 }
Selim Cinekbee4e072018-05-21 22:06:43 -070010468
10469 /**
10470 * Set the maximum lines of remote input history lines allowed.
10471 * @param maxRemoteInputHistory The number of lines.
10472 * @return The builder for method chaining.
10473 */
10474 public StandardTemplateParams setMaxRemoteInputHistory(int maxRemoteInputHistory) {
10475 this.maxRemoteInputHistory = maxRemoteInputHistory;
10476 return this;
10477 }
Adrian Roos70d7aa32017-01-11 15:39:06 -080010478 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010479}