blob: 4f41da6e52fbcb6209e1a7f7c0b5c0cb9aafd2f2 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Lucas Dupina291d192018-06-07 13:59:42 -070019import static com.android.internal.util.ContrastColorUtil.satisfiesTextContrast;
Selim Cinek389edcd2017-05-11 19:16:44 -070020
Tor Norbye80756e32015-03-02 09:39:27 -080021import android.annotation.ColorInt;
Tor Norbye7b9c9122013-05-30 16:48:33 -070022import android.annotation.DrawableRes;
Gus Prevas9cc96602018-10-11 11:24:23 -040023import android.annotation.IdRes;
Tor Norbyed9273d62013-05-30 15:59:53 -070024import android.annotation.IntDef;
Alex Hillsfd590442016-10-07 09:52:44 -040025import android.annotation.NonNull;
Selim Cinek88188f22017-09-19 16:46:56 -070026import android.annotation.Nullable;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060027import android.annotation.RequiresPermission;
Daniel Sandler01df1c62014-06-09 10:54:01 -040028import android.annotation.SdkConstant;
29import android.annotation.SdkConstant.SdkConstantType;
Julia Reynoldse46bb372016-03-17 11:05:58 -040030import android.annotation.SystemApi;
Mathew Inwood61e8ae62018-08-14 14:17:44 +010031import android.annotation.UnsupportedAppUsage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.content.Context;
33import android.content.Intent;
Kenny Guy77320062014-08-27 21:37:15 +010034import android.content.pm.ApplicationInfo;
Dan Sandler732bd6c2016-04-12 14:20:32 -040035import android.content.pm.PackageManager;
Christoph Studer4600f9b2014-07-22 22:44:43 +020036import android.content.pm.PackageManager.NameNotFoundException;
Julia Reynolds13d898c2017-02-02 12:22:05 -050037import android.content.pm.ShortcutInfo;
Jorim Jaggief72a192014-08-26 21:57:46 +020038import android.content.res.ColorStateList;
Anthony Chenad4d1582017-04-10 16:07:58 -070039import android.content.res.Configuration;
40import android.content.res.Resources;
Gus Prevas9cc96602018-10-11 11:24:23 -040041import android.content.res.TypedArray;
Joe Onoratoef1e7762010-09-17 18:38:38 -040042import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010043import android.graphics.Canvas;
Adrian Roosc1a80b02016-04-05 14:54:55 -070044import android.graphics.Color;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010045import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010046import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040047import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040048import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070049import android.media.AudioManager;
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -080050import android.media.PlayerBase;
Jeff Browndba34ba2014-06-24 20:46:03 -070051import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040053import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020054import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050055import android.os.Bundle;
Dianne Hackborn98305522017-05-05 17:53:53 -070056import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.os.Parcel;
58import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040059import android.os.SystemClock;
Selim Cinek6743c0b2017-01-18 18:24:01 -080060import android.os.SystemProperties;
Jeff Sharkey6d515712012-09-20 16:06:08 -070061import android.os.UserHandle;
Adrian Roosc1a80b02016-04-05 14:54:55 -070062import android.text.BidiFormatter;
Selim Cinek60a54252016-02-26 17:03:25 -080063import android.text.SpannableStringBuilder;
64import android.text.Spanned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.text.TextUtils;
Selim Cinek60a54252016-02-26 17:03:25 -080066import android.text.style.AbsoluteSizeSpan;
Selim Cinek89991a22016-03-07 19:51:54 -080067import android.text.style.CharacterStyle;
Selim Cinek981962e2016-07-20 20:41:58 -070068import android.text.style.ForegroundColorSpan;
Selim Cinek60a54252016-02-26 17:03:25 -080069import android.text.style.RelativeSizeSpan;
70import android.text.style.TextAppearanceSpan;
Svet Ganovddb94882016-06-23 19:55:24 -070071import android.util.ArraySet;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040072import android.util.Log;
Tony Mak638430e2018-10-08 19:19:10 +010073import android.util.Pair;
Dan Sandler50128532015-12-08 15:42:41 -050074import android.util.SparseArray;
Yi Jin6b514142017-10-30 14:54:12 -070075import android.util.proto.ProtoOutputStream;
Griff Hazen61a9e862014-05-22 16:05:19 -070076import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080077import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080078import android.view.View;
Selim Cinek954cc232016-05-20 13:29:23 -070079import android.view.ViewGroup;
Jeff Sharkey1c400132011-08-05 14:50:13 -070080import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import android.widget.RemoteViews;
82
Griff Hazen959591e2014-05-15 22:26:18 -070083import com.android.internal.R;
Selim Cinek389edcd2017-05-11 19:16:44 -070084import com.android.internal.annotations.VisibleForTesting;
Svet Ganovddb94882016-06-23 19:55:24 -070085import com.android.internal.util.ArrayUtils;
Lucas Dupina291d192018-06-07 13:59:42 -070086import com.android.internal.util.ContrastColorUtil;
Adrian Roos0bc3f6a2017-03-06 11:54:05 -080087import com.android.internal.util.Preconditions;
Griff Hazen959591e2014-05-15 22:26:18 -070088
Tor Norbyed9273d62013-05-30 15:59:53 -070089import java.lang.annotation.Retention;
90import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020091import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050092import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070093import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070094import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070095import java.util.List;
Julia Reynolds7217dc92018-03-07 12:12:09 -050096import java.util.Objects;
Dan Sandler50128532015-12-08 15:42:41 -050097import java.util.Set;
Jeff Sharkey23b31182018-04-18 21:32:12 -060098import java.util.function.Consumer;
Joe Onorato561d3852010-11-20 18:09:34 -080099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100/**
101 * A class that represents how a persistent notification is to be presented to
102 * the user using the {@link android.app.NotificationManager}.
103 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800104 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
105 * easier to construct Notifications.</p>
106 *
Joe Fernandez558459f2011-10-13 16:47:36 -0700107 * <div class="special reference">
108 * <h3>Developer Guides</h3>
109 * <p>For a guide to creating notifications, read the
110 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
111 * developer guide.</p>
112 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 */
114public class Notification implements Parcelable
115{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -0400116 private static final String TAG = "Notification";
117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -0400119 * An activity that provides a user interface for adjusting notification preferences for its
Julia Reynolds3aedded2017-03-31 14:42:09 -0400120 * containing application.
Daniel Sandler01df1c62014-06-09 10:54:01 -0400121 */
122 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
123 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
124 = "android.intent.category.NOTIFICATION_PREFERENCES";
125
126 /**
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500127 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
128 * contain a {@link NotificationChannel#getId() channel id} that can be used to narrow down
Julia Reynolds3aedded2017-03-31 14:42:09 -0400129 * what settings should be shown in the target app.
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500130 */
131 public static final String EXTRA_CHANNEL_ID = "android.intent.extra.CHANNEL_ID";
132
133 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -0400134 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds005c8b92017-08-24 10:35:53 -0400135 * contain a {@link NotificationChannelGroup#getId() group id} that can be used to narrow down
136 * what settings should be shown in the target app.
137 */
138 public static final String EXTRA_CHANNEL_GROUP_ID = "android.intent.extra.CHANNEL_GROUP_ID";
139
140 /**
141 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds3aedded2017-03-31 14:42:09 -0400142 * contain the tag provided to {@link NotificationManager#notify(String, int, Notification)}
143 * that can be used to narrow down what settings should be shown in the target app.
144 */
145 public static final String EXTRA_NOTIFICATION_TAG = "android.intent.extra.NOTIFICATION_TAG";
146
147 /**
148 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
149 * contain the id provided to {@link NotificationManager#notify(String, int, Notification)}
150 * that can be used to narrow down what settings should be shown in the target app.
151 */
152 public static final String EXTRA_NOTIFICATION_ID = "android.intent.extra.NOTIFICATION_ID";
153
154 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 * Use all default values (where applicable).
156 */
157 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 /**
160 * Use the default notification sound. This will ignore any given
161 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500162 *
Chris Wren47c20a12014-06-18 17:27:29 -0400163 * <p>
164 * A notification that is noisy is more likely to be presented as a heads-up notification.
165 * </p>
166 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500168 */
169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 public static final int DEFAULT_SOUND = 1;
171
172 /**
173 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500174 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700175 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500176 *
Chris Wren47c20a12014-06-18 17:27:29 -0400177 * <p>
178 * A notification that vibrates is more likely to be presented as a heads-up notification.
179 * </p>
180 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500182 */
183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 /**
187 * Use the default notification lights. This will ignore the
188 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
189 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500190 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500192 */
193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200197 * Maximum length of CharSequences accepted by Builder and friends.
198 *
199 * <p>
200 * Avoids spamming the system with overly large strings such as full e-mails.
201 */
202 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
203
204 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800205 * Maximum entries of reply text that are accepted by Builder and friends.
206 */
207 private static final int MAX_REPLY_HISTORY = 5;
208
Tony Mak09db2ea2018-06-27 18:12:48 +0100209 /**
210 * Maximum numbers of action buttons in a notification.
211 * @hide
212 */
213 public static final int MAX_ACTION_BUTTONS = 3;
Selim Cinekde4de0e2018-01-24 16:21:07 -0800214
215 /**
216 * If the notification contained an unsent draft for a RemoteInput when the user clicked on it,
217 * we're adding the draft as a String extra to the {@link #contentIntent} using this key.
218 *
219 * <p>Apps may use this extra to prepopulate text fields in the app, where the user usually
220 * sends messages.</p>
221 */
222 public static final String EXTRA_REMOTE_INPUT_DRAFT = "android.remoteInputDraft";
223
Adrian Roose458aa82015-12-08 16:17:19 -0800224 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500225 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800226 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500227 * Default value: {@link System#currentTimeMillis() Now}.
228 *
229 * Choose a timestamp that will be most relevant to the user. For most finite events, this
230 * corresponds to the time the event happened (or will happen, in the case of events that have
231 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800232 * timestamped according to when the activity began.
233 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500234 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800235 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500236 * <ul>
237 * <li>Notification of a new chat message should be stamped when the message was received.</li>
238 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
239 * <li>Notification of a completed file download should be stamped when the download finished.</li>
240 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
241 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
242 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800243 * </ul>
244 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700245 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
246 * anymore by default and must be opted into by using
247 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 */
249 public long when;
250
251 /**
Selim Cinekb85f36fd2016-04-20 18:46:36 -0700252 * The creation time of the notification
253 */
254 private long creationTime;
255
256 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400258 *
259 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 */
Dan Sandler86647982015-05-13 23:41:13 -0400261 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700262 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 public int icon;
264
265 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800266 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
267 * leave it at its default value of 0.
268 *
269 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700270 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800271 */
272 public int iconLevel;
273
274 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500275 * The number of events that this notification represents. For example, in a new mail
276 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800277 *
Julia Reynolds30331982017-04-27 10:12:50 -0400278 * The system may or may not use this field to modify the appearance of the notification.
Julia Reynolds13d898c2017-02-02 12:22:05 -0500279 * Starting with {@link android.os.Build.VERSION_CODES#O}, the number may be displayed as a
280 * badge icon in Launchers that support badging.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 */
Julia Reynolds30331982017-04-27 10:12:50 -0400282 public int number = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283
284 /**
285 * The intent to execute when the expanded status entry is clicked. If
286 * this is an activity, it must include the
287 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800288 * that you take care of task management as described in the
289 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800290 * Stack</a> document. In particular, make sure to read the notification section
291 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
292 * Notifications</a> for the correct ways to launch an application from a
293 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 */
295 public PendingIntent contentIntent;
296
297 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500298 * The intent to execute when the notification is explicitly dismissed by the user, either with
299 * the "Clear All" button or by swiping it away individually.
300 *
301 * This probably shouldn't be launching an activity since several of those will be sent
302 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 */
304 public PendingIntent deleteIntent;
305
306 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700307 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800308 *
Chris Wren47c20a12014-06-18 17:27:29 -0400309 * <p>
310 * The system UI may choose to display a heads-up notification, instead of
311 * launching this intent, while the user is using the device.
312 * </p>
313 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800314 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400315 */
316 public PendingIntent fullScreenIntent;
317
318 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400319 * Text that summarizes this notification for accessibility services.
320 *
321 * As of the L release, this text is no longer shown on screen, but it is still useful to
322 * accessibility services (where it serves as an audible announcement of the notification's
323 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400324 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800325 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 */
327 public CharSequence tickerText;
328
329 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400330 * Formerly, a view showing the {@link #tickerText}.
331 *
332 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400333 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400334 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800335 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400336
337 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400338 * The view that will represent this notification in the notification list (which is pulled
339 * down from the status bar).
340 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500341 * As of N, this field may be null. The notification view is determined by the inputs
342 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400343 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400345 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 public RemoteViews contentView;
347
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400348 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400349 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400350 * opportunity to show more detail. The system UI may choose to show this
351 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400352 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500353 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400354 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
355 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400356 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400357 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400358 public RemoteViews bigContentView;
359
Chris Wren8fd39ec2014-02-27 17:43:26 -0500360
361 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400362 * A medium-format version of {@link #contentView}, providing the Notification an
363 * opportunity to add action buttons to contentView. At its discretion, the system UI may
364 * choose to show this as a heads-up notification, which will pop up so the user can see
365 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400366 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500367 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400368 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
369 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500370 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400371 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500372 public RemoteViews headsUpContentView;
373
Julia Reynoldsfc640012018-02-21 12:25:27 -0500374 private boolean mUsesStandardHeader;
375
376 private static final ArraySet<Integer> STANDARD_LAYOUTS = new ArraySet<>();
377 static {
378 STANDARD_LAYOUTS.add(R.layout.notification_template_material_base);
379 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_base);
380 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_picture);
381 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_text);
382 STANDARD_LAYOUTS.add(R.layout.notification_template_material_inbox);
383 STANDARD_LAYOUTS.add(R.layout.notification_template_material_messaging);
384 STANDARD_LAYOUTS.add(R.layout.notification_template_material_media);
385 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_media);
386 STANDARD_LAYOUTS.add(R.layout.notification_template_ambient_header);
387 STANDARD_LAYOUTS.add(R.layout.notification_template_header);
388 STANDARD_LAYOUTS.add(R.layout.notification_template_material_ambient);
389 }
390
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400391 /**
Dan Sandler86647982015-05-13 23:41:13 -0400392 * A large bitmap to be shown in the notification content area.
393 *
394 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 */
Dan Sandler86647982015-05-13 23:41:13 -0400396 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800397 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398
399 /**
400 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500401 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400403 * A notification that is noisy is more likely to be presented as a heads-up notification.
404 * </p>
405 *
406 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500407 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500409 * @deprecated use {@link NotificationChannel#getSound()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500411 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 public Uri sound;
413
414 /**
415 * Use this constant as the value for audioStreamType to request that
416 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700417 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400418 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500419 * @deprecated Use {@link NotificationChannel#getAudioAttributes()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700421 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 public static final int STREAM_DEFAULT = -1;
423
424 /**
425 * The audio stream type to use when playing the sound.
426 * Should be one of the STREAM_ constants from
427 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400428 *
429 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700431 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 public int audioStreamType = STREAM_DEFAULT;
433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400435 * The default value of {@link #audioAttributes}.
436 */
437 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
438 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
439 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
440 .build();
441
442 /**
443 * The {@link AudioAttributes audio attributes} to use when playing the sound.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500444 *
445 * @deprecated use {@link NotificationChannel#getAudioAttributes()} instead.
John Spurlockc0650f022014-07-19 13:22:39 -0400446 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500447 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -0400448 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
449
450 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500451 * The pattern with which to vibrate.
452 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 * <p>
454 * To vibrate the default pattern, see {@link #defaults}.
455 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500456 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 * @see android.os.Vibrator#vibrate(long[],int)
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500458 * @deprecated use {@link NotificationChannel#getVibrationPattern()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500460 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 public long[] vibrate;
462
463 /**
464 * The color of the led. The hardware will do its best approximation.
465 *
466 * @see #FLAG_SHOW_LIGHTS
467 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500468 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 */
Tor Norbye80756e32015-03-02 09:39:27 -0800470 @ColorInt
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500471 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 public int ledARGB;
473
474 /**
475 * The number of milliseconds for the LED to be on while it's flashing.
476 * The hardware will do its best approximation.
477 *
478 * @see #FLAG_SHOW_LIGHTS
479 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500480 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500482 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 public int ledOnMS;
484
485 /**
486 * The number of milliseconds for the LED to be off while it's flashing.
487 * The hardware will do its best approximation.
488 *
489 * @see #FLAG_SHOW_LIGHTS
490 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500491 *
492 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500494 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 public int ledOffMS;
496
497 /**
498 * Specifies which values should be taken from the defaults.
499 * <p>
500 * To set, OR the desired from {@link #DEFAULT_SOUND},
501 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
502 * values, use {@link #DEFAULT_ALL}.
503 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500504 *
505 * @deprecated use {@link NotificationChannel#getSound()} and
506 * {@link NotificationChannel#shouldShowLights()} and
507 * {@link NotificationChannel#shouldVibrate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500509 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 public int defaults;
511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 /**
513 * Bit to be bitwise-ored into the {@link #flags} field that should be
514 * set if you want the LED on for this notification.
515 * <ul>
516 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
517 * or 0 for both ledOnMS and ledOffMS.</li>
518 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
519 * <li>To flash the LED, pass the number of milliseconds that it should
520 * be on and off to ledOnMS and ledOffMS.</li>
521 * </ul>
522 * <p>
523 * Since hardware varies, you are not guaranteed that any of the values
Ricardo Loo Forondaf8f6d0a2018-01-25 08:42:43 -0800524 * you pass are honored exactly. Use the system defaults if possible
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 * because they will be set to values that work on any given hardware.
526 * <p>
527 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500528 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500529 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500531 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
533
534 /**
535 * Bit to be bitwise-ored into the {@link #flags} field that should be
536 * set if this notification is in reference to something that is ongoing,
537 * like a phone call. It should not be set if this notification is in
538 * reference to something that happened at a particular point in time,
539 * like a missed phone call.
540 */
541 public static final int FLAG_ONGOING_EVENT = 0x00000002;
542
543 /**
544 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700545 * the audio will be repeated until the notification is
546 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 */
548 public static final int FLAG_INSISTENT = 0x00000004;
549
550 /**
551 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700552 * set if you would only like the sound, vibrate and ticker to be played
553 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 */
555 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
556
557 /**
558 * Bit to be bitwise-ored into the {@link #flags} field that should be
559 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500560 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 */
562 public static final int FLAG_AUTO_CANCEL = 0x00000010;
563
564 /**
565 * Bit to be bitwise-ored into the {@link #flags} field that should be
566 * set if the notification should not be canceled when the user clicks
567 * the Clear all button.
568 */
569 public static final int FLAG_NO_CLEAR = 0x00000020;
570
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700571 /**
572 * Bit to be bitwise-ored into the {@link #flags} field that should be
573 * set if this notification represents a currently running service. This
574 * will normally be set for you by {@link Service#startForeground}.
575 */
576 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
577
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400578 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500579 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800580 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500581 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400582 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700583 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500584 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400585
Griff Hazendfcb0802014-02-11 12:00:00 -0800586 /**
587 * Bit to be bitswise-ored into the {@link #flags} field that should be
588 * set if this notification is relevant to the current device only
589 * and it is not recommended that it bridge to other devices.
590 */
591 public static final int FLAG_LOCAL_ONLY = 0x00000100;
592
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700593 /**
594 * Bit to be bitswise-ored into the {@link #flags} field that should be
595 * set if this notification is the group summary for a group of notifications.
596 * Grouped notifications may display in a cluster or stack on devices which
597 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
598 */
599 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
600
Julia Reynoldse46bb372016-03-17 11:05:58 -0400601 /**
602 * Bit to be bitswise-ored into the {@link #flags} field that should be
603 * set if this notification is the group summary for an auto-group of notifications.
604 *
605 * @hide
606 */
607 @SystemApi
608 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
609
Julia Reynolds4db59552017-06-30 13:34:01 -0400610 /**
611 * @hide
612 */
613 public static final int FLAG_CAN_COLORIZE = 0x00000800;
614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 public int flags;
616
Tor Norbyed9273d62013-05-30 15:59:53 -0700617 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700618 @IntDef(prefix = { "PRIORITY_" }, value = {
619 PRIORITY_DEFAULT,
620 PRIORITY_LOW,
621 PRIORITY_MIN,
622 PRIORITY_HIGH,
623 PRIORITY_MAX
624 })
Tor Norbyed9273d62013-05-30 15:59:53 -0700625 @Retention(RetentionPolicy.SOURCE)
626 public @interface Priority {}
627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500629 * Default notification {@link #priority}. If your application does not prioritize its own
630 * notifications, use this value for all notifications.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500631 *
632 * @deprecated use {@link NotificationManager#IMPORTANCE_DEFAULT} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500633 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500634 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500635 public static final int PRIORITY_DEFAULT = 0;
636
637 /**
638 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
639 * items smaller, or at a different position in the list, compared with your app's
640 * {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500641 *
642 * @deprecated use {@link NotificationManager#IMPORTANCE_LOW} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500643 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500644 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500645 public static final int PRIORITY_LOW = -1;
646
647 /**
648 * Lowest {@link #priority}; these items might not be shown to the user except under special
649 * circumstances, such as detailed notification logs.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500650 *
651 * @deprecated use {@link NotificationManager#IMPORTANCE_MIN} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500652 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500653 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500654 public static final int PRIORITY_MIN = -2;
655
656 /**
657 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
658 * show these items larger, or at a different position in notification lists, compared with
659 * your app's {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500660 *
661 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500662 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500663 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500664 public static final int PRIORITY_HIGH = 1;
665
666 /**
667 * Highest {@link #priority}, for your application's most important items that require the
668 * user's prompt attention or input.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500669 *
670 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500671 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500672 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500673 public static final int PRIORITY_MAX = 2;
674
675 /**
676 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800677 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500678 * Priority is an indication of how much of the user's valuable attention should be consumed by
679 * this notification. Low-priority notifications may be hidden from the user in certain
680 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500681 * system will make a determination about how to interpret this priority when presenting
682 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400683 *
684 * <p>
685 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
686 * as a heads-up notification.
687 * </p>
688 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500689 * @deprecated use {@link NotificationChannel#getImportance()} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500690 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700691 @Priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500692 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500693 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800694
Dan Sandler26e81cf2014-05-06 10:01:27 -0400695 /**
696 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
697 * to be applied by the standard Style templates when presenting this notification.
698 *
699 * The current template design constructs a colorful header image by overlaying the
700 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
701 * ignored.
702 */
Tor Norbye80756e32015-03-02 09:39:27 -0800703 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400704 public int color = COLOR_DEFAULT;
705
706 /**
707 * Special value of {@link #color} telling the system not to decorate this notification with
708 * any special color but instead use default colors when presenting this notification.
709 */
Tor Norbye80756e32015-03-02 09:39:27 -0800710 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400711 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600712
713 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800714 * Special value of {@link #color} used as a place holder for an invalid color.
Selim Cinek99104832017-01-25 14:47:33 -0800715 * @hide
Adrian Roos4ff3b122016-02-01 12:26:13 -0800716 */
717 @ColorInt
Selim Cinek99104832017-01-25 14:47:33 -0800718 public static final int COLOR_INVALID = 1;
Adrian Roos4ff3b122016-02-01 12:26:13 -0800719
720 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700721 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
722 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600723 * lockscreen).
724 *
725 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
726 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
727 * shown in all situations, but the contents are only available if the device is unlocked for
728 * the appropriate user.
729 *
730 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
731 * can be read even in an "insecure" context (that is, above a secure lockscreen).
732 * To modify the public version of this notification—for example, to redact some portions—see
733 * {@link Builder#setPublicVersion(Notification)}.
734 *
735 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
736 * and ticker until the user has bypassed the lockscreen.
737 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600738 public @Visibility int visibility;
739
740 /** @hide */
741 @IntDef(prefix = { "VISIBILITY_" }, value = {
742 VISIBILITY_PUBLIC,
743 VISIBILITY_PRIVATE,
744 VISIBILITY_SECRET,
745 })
746 @Retention(RetentionPolicy.SOURCE)
747 public @interface Visibility {}
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600748
Griff Hazenfc3922d2014-08-20 11:56:44 -0700749 /**
750 * Notification visibility: Show this notification in its entirety on all lockscreens.
751 *
752 * {@see #visibility}
753 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600754 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700755
756 /**
757 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
758 * private information on secure lockscreens.
759 *
760 * {@see #visibility}
761 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600762 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700763
764 /**
765 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
766 *
767 * {@see #visibility}
768 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600769 public static final int VISIBILITY_SECRET = -1;
770
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500771 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400772 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500773 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400774 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500775
776 /**
Adora Zhangaa90e872018-03-12 14:07:50 -0700777 * Notification category: map turn-by-turn navigation.
778 */
779 public static final String CATEGORY_NAVIGATION = "navigation";
780
781 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400782 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500783 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400784 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500785
786 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400787 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500788 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400789 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500790
791 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400792 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500793 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400794 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500795
796 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400797 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500798 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400799 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500800
801 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400802 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500803 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400804 public static final String CATEGORY_ALARM = "alarm";
805
806 /**
807 * Notification category: progress of a long-running background operation.
808 */
809 public static final String CATEGORY_PROGRESS = "progress";
810
811 /**
812 * Notification category: social network or sharing update.
813 */
814 public static final String CATEGORY_SOCIAL = "social";
815
816 /**
817 * Notification category: error in background operation or authentication status.
818 */
819 public static final String CATEGORY_ERROR = "err";
820
821 /**
822 * Notification category: media transport control for playback.
823 */
824 public static final String CATEGORY_TRANSPORT = "transport";
825
826 /**
827 * Notification category: system or device status update. Reserved for system use.
828 */
829 public static final String CATEGORY_SYSTEM = "sys";
830
831 /**
832 * Notification category: indication of running background service.
833 */
834 public static final String CATEGORY_SERVICE = "service";
835
836 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400837 * Notification category: a specific, timely recommendation for a single thing.
838 * For example, a news app might want to recommend a news story it believes the user will
839 * want to read next.
840 */
841 public static final String CATEGORY_RECOMMENDATION = "recommendation";
842
843 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400844 * Notification category: ongoing information about device or contextual status.
845 */
846 public static final String CATEGORY_STATUS = "status";
847
848 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400849 * Notification category: user-scheduled reminder.
850 */
851 public static final String CATEGORY_REMINDER = "reminder";
852
853 /**
Adora Zhangaa90e872018-03-12 14:07:50 -0700854 * Notification category: extreme car emergencies.
855 * @hide
856 */
857 @SystemApi
858 public static final String CATEGORY_CAR_EMERGENCY = "car_emergency";
859
860 /**
861 * Notification category: car warnings.
862 * @hide
863 */
864 @SystemApi
865 public static final String CATEGORY_CAR_WARNING = "car_warning";
866
867 /**
868 * Notification category: general car system information.
869 * @hide
870 */
871 @SystemApi
872 public static final String CATEGORY_CAR_INFORMATION = "car_information";
873
874 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400875 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
876 * that best describes this Notification. May be used by the system for ranking and filtering.
877 */
878 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500879
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100880 @UnsupportedAppUsage
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700881 private String mGroupKey;
882
883 /**
884 * Get the key used to group this notification into a cluster or stack
885 * with other notifications on devices which support such rendering.
886 */
887 public String getGroup() {
888 return mGroupKey;
889 }
890
891 private String mSortKey;
892
893 /**
894 * Get a sort key that orders this notification among other notifications from the
895 * same package. This can be useful if an external sort was already applied and an app
896 * would like to preserve this. Notifications will be sorted lexicographically using this
897 * value, although providing different priorities in addition to providing sort key may
898 * cause this value to be ignored.
899 *
900 * <p>This sort key can also be used to order members of a notification group. See
901 * {@link Builder#setGroup}.
902 *
903 * @see String#compareTo(String)
904 */
905 public String getSortKey() {
906 return mSortKey;
907 }
908
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500909 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400910 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400911 * <p>
912 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
913 * APIs, and are intended to be used by
914 * {@link android.service.notification.NotificationListenerService} implementations to extract
915 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500916 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400917 public Bundle extras = new Bundle();
918
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400919 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700920 * All pending intents in the notification as the system needs to be able to access them but
921 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700922 * custom parcelable objects.
923 *
924 * @hide
925 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100926 @UnsupportedAppUsage
Felipe Lemedd85da62016-06-28 11:29:54 -0700927 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700928
929 /**
Dianne Hackborn98305522017-05-05 17:53:53 -0700930 * Token identifying the notification that is applying doze/bgcheck whitelisting to the
931 * pending intents inside of it, so only those will get the behavior.
932 *
933 * @hide
934 */
Adrian Roosfb921842017-10-26 14:49:56 +0200935 private IBinder mWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -0700936
937 /**
938 * Must be set by a process to start associating tokens with Notification objects
939 * coming in to it. This is set by NotificationManagerService.
940 *
941 * @hide
942 */
943 static public IBinder processWhitelistToken;
944
945 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400946 * {@link #extras} key: this is the title of the notification,
947 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
948 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500949 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400950
951 /**
952 * {@link #extras} key: this is the title of the notification when shown in expanded form,
953 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
954 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400955 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400956
957 /**
958 * {@link #extras} key: this is the main text payload, as supplied to
959 * {@link Builder#setContentText(CharSequence)}.
960 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500961 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400962
963 /**
964 * {@link #extras} key: this is a third line of text, as supplied to
965 * {@link Builder#setSubText(CharSequence)}.
966 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400967 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400968
969 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800970 * {@link #extras} key: this is the remote input history, as supplied to
971 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700972 *
973 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
974 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
975 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
976 * notifications once the other party has responded).
977 *
978 * The extra with this key is of type CharSequence[] and contains the most recent entry at
979 * the 0 index, the second most recent at the 1 index, etc.
980 *
981 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800982 */
983 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
984
985 /**
Kenny Guya0f6de82018-04-06 16:20:16 +0100986 * {@link #extras} key: boolean as supplied to
987 * {@link Builder#setShowRemoteInputSpinner(boolean)}.
988 *
989 * If set to true, then the view displaying the remote input history from
990 * {@link Builder#setRemoteInputHistory(CharSequence[])} will have a progress spinner.
991 *
992 * @see Builder#setShowRemoteInputSpinner(boolean)
993 * @hide
994 */
995 public static final String EXTRA_SHOW_REMOTE_INPUT_SPINNER = "android.remoteInputSpinner";
996
997 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +0100998 * {@link #extras} key: boolean as supplied to
999 * {@link Builder#setHideSmartReplies(boolean)}.
1000 *
1001 * If set to true, then any smart reply buttons will be hidden.
1002 *
1003 * @see Builder#setHideSmartReplies(boolean)
1004 * @hide
1005 */
1006 public static final String EXTRA_HIDE_SMART_REPLIES = "android.hideSmartReplies";
1007
1008 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001009 * {@link #extras} key: this is a small piece of additional text as supplied to
1010 * {@link Builder#setContentInfo(CharSequence)}.
1011 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001012 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001013
1014 /**
1015 * {@link #extras} key: this is a line of summary information intended to be shown
1016 * alongside expanded notifications, as supplied to (e.g.)
1017 * {@link BigTextStyle#setSummaryText(CharSequence)}.
1018 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001019 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001020
1021 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02001022 * {@link #extras} key: this is the longer text shown in the big form of a
1023 * {@link BigTextStyle} notification, as supplied to
1024 * {@link BigTextStyle#bigText(CharSequence)}.
1025 */
1026 public static final String EXTRA_BIG_TEXT = "android.bigText";
1027
1028 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001029 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
1030 * supplied to {@link Builder#setSmallIcon(int)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -04001031 *
1032 * @deprecated Use {@link #getSmallIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001033 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04001034 @Deprecated
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001035 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001036
1037 /**
1038 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
1039 * notification payload, as
1040 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -04001041 *
1042 * @deprecated Use {@link #getLargeIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001043 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04001044 @Deprecated
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001045 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001046
1047 /**
1048 * {@link #extras} key: this is a bitmap to be used instead of the one from
1049 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
1050 * shown in its expanded form, as supplied to
1051 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
1052 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001053 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001054
1055 /**
1056 * {@link #extras} key: this is the progress value supplied to
1057 * {@link Builder#setProgress(int, int, boolean)}.
1058 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001059 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001060
1061 /**
1062 * {@link #extras} key: this is the maximum value supplied to
1063 * {@link Builder#setProgress(int, int, boolean)}.
1064 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001065 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001066
1067 /**
1068 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
1069 * {@link Builder#setProgress(int, int, boolean)}.
1070 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001071 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001072
1073 /**
1074 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
1075 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
1076 * {@link Builder#setUsesChronometer(boolean)}.
1077 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001078 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001079
1080 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08001081 * {@link #extras} key: whether the chronometer set on the notification should count down
1082 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -07001083 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -08001084 */
Adrian Roos96b7e202016-05-17 13:50:38 -07001085 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -08001086
1087 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001088 * {@link #extras} key: whether {@link #when} should be shown,
1089 * as supplied to {@link Builder#setShowWhen(boolean)}.
1090 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001091 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001092
1093 /**
1094 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
1095 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
1096 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001097 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001098
1099 /**
1100 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
1101 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
1102 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001103 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -04001104
1105 /**
1106 * {@link #extras} key: A string representing the name of the specific
1107 * {@link android.app.Notification.Style} used to create this notification.
1108 */
Chris Wren91ad5632013-06-05 15:05:57 -04001109 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001110
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001111 /**
Chris Wrene6c48932014-09-29 17:19:27 -04001112 * {@link #extras} key: A String array containing the people that this notification relates to,
1113 * each of which was supplied to {@link Builder#addPerson(String)}.
Selim Cineke7238dd2017-12-14 17:48:32 -08001114 *
1115 * @deprecated the actual objects are now in {@link #EXTRA_PEOPLE_LIST}
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001116 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001117 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001118
1119 /**
Selim Cineke7238dd2017-12-14 17:48:32 -08001120 * {@link #extras} key: An arrayList of {@link Person} objects containing the people that
1121 * this notification relates to.
1122 */
1123 public static final String EXTRA_PEOPLE_LIST = "android.people.list";
1124
1125 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04001126 * Allow certain system-generated notifications to appear before the device is provisioned.
1127 * Only available to notifications coming from the android package.
1128 * @hide
1129 */
Maurice Lam96c10032017-03-29 15:42:38 -07001130 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001131 @RequiresPermission(android.Manifest.permission.NOTIFICATION_DURING_SETUP)
John Spurlockfd7f1e02014-03-18 16:41:57 -04001132 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
1133
1134 /**
Robin Leed107af62018-04-27 13:55:56 +02001135 * {@link #extras} key:
1136 * flat {@link String} representation of a {@link android.content.ContentUris content URI}
1137 * pointing to an image that can be displayed in the background when the notification is
1138 * selected. Used on television platforms. The URI must point to an image stream suitable for
1139 * passing into {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001140 * BitmapFactory.decodeStream}; all other content types will be ignored.
Jose Limae9e3b3b2014-05-18 23:44:50 -07001141 */
1142 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
1143
1144 /**
Dan Sandler842dd772014-05-15 09:36:47 -04001145 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -07001146 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -04001147 * {@link android.app.Notification.MediaStyle} notification.
1148 */
1149 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
1150
1151 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +01001152 * {@link #extras} key: the indices of actions to be shown in the compact view,
1153 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
1154 */
1155 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
1156
Christoph Studer943aa672014-08-03 20:31:16 +02001157 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04001158 * {@link #extras} key: the username to be displayed for all messages sent by the user including
1159 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -07001160 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1161 * {@link CharSequence}
Selim Cinekcb8b9852017-12-15 18:01:52 -08001162 *
1163 * @deprecated use {@link #EXTRA_MESSAGING_PERSON}
Alex Hillsfc737de2016-03-23 17:33:02 -04001164 */
1165 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
1166
1167 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08001168 * {@link #extras} key: the person to be displayed for all messages sent by the user including
1169 * direct replies
1170 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1171 * {@link Person}
1172 */
1173 public static final String EXTRA_MESSAGING_PERSON = "android.messagingUser";
1174
1175 /**
Adrian Roos96b7e202016-05-17 13:50:38 -07001176 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -04001177 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -04001178 */
Alex Hillsd9b04d92016-04-11 16:38:16 -04001179 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -04001180
1181 /**
1182 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
1183 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -07001184 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1185 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -04001186 */
1187 public static final String EXTRA_MESSAGES = "android.messages";
1188
1189 /**
Adrian Roos437cd562017-01-18 15:47:03 -08001190 * {@link #extras} key: an array of
1191 * {@link android.app.Notification.MessagingStyle#addHistoricMessage historic}
1192 * {@link android.app.Notification.MessagingStyle.Message} bundles provided by a
1193 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1194 * array of bundles.
1195 */
1196 public static final String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
1197
1198 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08001199 * {@link #extras} key: whether the {@link android.app.Notification.MessagingStyle} notification
1200 * represents a group conversation.
1201 */
1202 public static final String EXTRA_IS_GROUP_CONVERSATION = "android.isGroupConversation";
1203
1204 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08001205 * {@link #extras} key: whether the notification should be colorized as
1206 * supplied to {@link Builder#setColorized(boolean)}}.
1207 */
1208 public static final String EXTRA_COLORIZED = "android.colorized";
1209
1210 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001211 * @hide
1212 */
1213 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
1214
Selim Cinek247fa012016-02-18 09:50:48 -08001215 /**
1216 * @hide
1217 */
1218 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
1219
Shane Brennan472a3b32016-12-12 15:28:10 -08001220 /**
Selim Cinekd0426622017-07-11 13:19:59 +02001221 * @hide
1222 */
1223 public static final String EXTRA_REDUCED_IMAGES = "android.reduced.images";
1224
1225 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001226 * {@link #extras} key: the audio contents of this notification.
1227 *
1228 * This is for use when rendering the notification on an audio-focused interface;
1229 * the audio contents are a complete sound sample that contains the contents/body of the
1230 * notification. This may be used in substitute of a Text-to-Speech reading of the
1231 * notification. For example if the notification represents a voice message this should point
1232 * to the audio of that message.
1233 *
1234 * The data stored under this key should be a String representation of a Uri that contains the
1235 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1236 *
1237 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1238 * has a field for holding data URI. That field can be used for audio.
1239 * See {@code Message#setData}.
1240 *
1241 * Example usage:
1242 * <pre>
1243 * {@code
1244 * Notification.Builder myBuilder = (build your Notification as normal);
1245 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1246 * }
1247 * </pre>
1248 */
1249 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1250
Dan Sandler80eaa592016-04-14 23:34:54 -04001251 /** @hide */
1252 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001253 @RequiresPermission(android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME)
Dan Sandler732bd6c2016-04-12 14:20:32 -04001254 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1255
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001256 /**
Julia Reynolds3c7de112018-03-28 09:33:13 -04001257 * This is set on the notifications shown by system_server about apps running foreground
1258 * services. It indicates that the notification should be shown
1259 * only if any of the given apps do not already have a properly tagged
1260 * {@link #FLAG_FOREGROUND_SERVICE} notification currently visible to the user.
1261 * This is a string array of all package names of the apps.
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001262 * @hide
1263 */
1264 public static final String EXTRA_FOREGROUND_APPS = "android.foregroundApps";
1265
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001266 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04001267 private Icon mSmallIcon;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001268 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04001269 private Icon mLargeIcon;
1270
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001271 @UnsupportedAppUsage
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001272 private String mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001273 private long mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001274
Julia Reynolds13d898c2017-02-02 12:22:05 -05001275 private String mShortcutId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04001276 private CharSequence mSettingsText;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001277
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001278 /** @hide */
1279 @IntDef(prefix = { "GROUP_ALERT_" }, value = {
1280 GROUP_ALERT_ALL, GROUP_ALERT_CHILDREN, GROUP_ALERT_SUMMARY
1281 })
1282 @Retention(RetentionPolicy.SOURCE)
1283 public @interface GroupAlertBehavior {}
1284
1285 /**
1286 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all notifications in a
1287 * group with sound or vibration ought to make sound or vibrate (respectively), so this
1288 * notification will not be muted when it is in a group.
1289 */
1290 public static final int GROUP_ALERT_ALL = 0;
1291
1292 /**
1293 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all children
1294 * notification in a group should be silenced (no sound or vibration) even if they are posted
1295 * to a {@link NotificationChannel} that has sound and/or vibration. Use this constant to
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001296 * mute this notification if this notification is a group child. This must be applied to all
1297 * children notifications you want to mute.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001298 *
1299 * <p> For example, you might want to use this constant if you post a number of children
1300 * notifications at once (say, after a periodic sync), and only need to notify the user
1301 * audibly once.
1302 */
1303 public static final int GROUP_ALERT_SUMMARY = 1;
1304
1305 /**
1306 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that the summary
1307 * notification in a group should be silenced (no sound or vibration) even if they are
1308 * posted to a {@link NotificationChannel} that has sound and/or vibration. Use this constant
1309 * to mute this notification if this notification is a group summary.
1310 *
1311 * <p>For example, you might want to use this constant if only the children notifications
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001312 * in your group have content and the summary is only used to visually group notifications
1313 * rather than to alert the user that new information is available.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001314 */
1315 public static final int GROUP_ALERT_CHILDREN = 2;
1316
Julia Reynolds2f431e22017-06-07 14:12:09 +00001317 private int mGroupAlertBehavior = GROUP_ALERT_ALL;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001318
Julia Reynolds13d898c2017-02-02 12:22:05 -05001319 /**
1320 * If this notification is being shown as a badge, always show as a number.
1321 */
1322 public static final int BADGE_ICON_NONE = 0;
1323
1324 /**
1325 * If this notification is being shown as a badge, use the {@link #getSmallIcon()} to
1326 * represent this notification.
1327 */
1328 public static final int BADGE_ICON_SMALL = 1;
1329
1330 /**
1331 * If this notification is being shown as a badge, use the {@link #getLargeIcon()} to
1332 * represent this notification.
1333 */
1334 public static final int BADGE_ICON_LARGE = 2;
Julia Reynolds7d5b4da2017-03-20 10:18:49 -04001335 private int mBadgeIcon = BADGE_ICON_NONE;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001336
Chris Wren51c75102013-07-16 20:49:17 -04001337 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001338 * Structure to encapsulate a named action that can be shown as part of this notification.
1339 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1340 * selected by the user.
1341 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001342 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1343 * or {@link Notification.Builder#addAction(Notification.Action)}
1344 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001345 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001346 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001347 /**
1348 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1349 * {@link RemoteInput}s.
1350 *
1351 * This is intended for {@link RemoteInput}s that only accept data, meaning
1352 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
1353 * is null or empty, and {@link RemoteInput#getAllowedDataTypes is non-null and not
1354 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1355 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1356 *
1357 * You can test if a RemoteInput matches these constraints using
1358 * {@link RemoteInput#isDataOnly}.
1359 */
1360 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1361
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001362 /**
1363 * {@link }: No semantic action defined.
1364 */
1365 public static final int SEMANTIC_ACTION_NONE = 0;
1366
1367 /**
1368 * {@code SemanticAction}: Reply to a conversation, chat, group, or wherever replies
1369 * may be appropriate.
1370 */
1371 public static final int SEMANTIC_ACTION_REPLY = 1;
1372
1373 /**
1374 * {@code SemanticAction}: Mark content as read.
1375 */
1376 public static final int SEMANTIC_ACTION_MARK_AS_READ = 2;
1377
1378 /**
1379 * {@code SemanticAction}: Mark content as unread.
1380 */
1381 public static final int SEMANTIC_ACTION_MARK_AS_UNREAD = 3;
1382
1383 /**
1384 * {@code SemanticAction}: Delete the content associated with the notification. This
1385 * could mean deleting an email, message, etc.
1386 */
1387 public static final int SEMANTIC_ACTION_DELETE = 4;
1388
1389 /**
1390 * {@code SemanticAction}: Archive the content associated with the notification. This
1391 * could mean archiving an email, message, etc.
1392 */
1393 public static final int SEMANTIC_ACTION_ARCHIVE = 5;
1394
1395 /**
1396 * {@code SemanticAction}: Mute the content associated with the notification. This could
1397 * mean silencing a conversation or currently playing media.
1398 */
1399 public static final int SEMANTIC_ACTION_MUTE = 6;
1400
1401 /**
1402 * {@code SemanticAction}: Unmute the content associated with the notification. This could
1403 * mean un-silencing a conversation or currently playing media.
1404 */
1405 public static final int SEMANTIC_ACTION_UNMUTE = 7;
1406
1407 /**
1408 * {@code SemanticAction}: Mark content with a thumbs up.
1409 */
1410 public static final int SEMANTIC_ACTION_THUMBS_UP = 8;
1411
1412 /**
1413 * {@code SemanticAction}: Mark content with a thumbs down.
1414 */
1415 public static final int SEMANTIC_ACTION_THUMBS_DOWN = 9;
1416
Kodlee Yincda5b092018-02-15 15:34:53 -08001417 /**
1418 * {@code SemanticAction}: Call a contact, group, etc.
1419 */
1420 public static final int SEMANTIC_ACTION_CALL = 10;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001421
Griff Hazen959591e2014-05-15 22:26:18 -07001422 private final Bundle mExtras;
Mathew Inwood8c854f82018-09-14 12:35:36 +01001423 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Dan Sandler86647982015-05-13 23:41:13 -04001424 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001425 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001426 private boolean mAllowGeneratedReplies = true;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001427 private final @SemanticAction int mSemanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001428
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001429 /**
1430 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001431 *
1432 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001433 */
Dan Sandler86647982015-05-13 23:41:13 -04001434 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001435 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001436
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001437 /**
1438 * Title of the action.
1439 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001440 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001441
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001442 /**
1443 * Intent to send when the user invokes this action. May be null, in which case the action
1444 * may be rendered in a disabled presentation by the system UI.
1445 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001446 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001447
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001448 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001449 if (in.readInt() != 0) {
1450 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001451 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1452 icon = mIcon.getResId();
1453 }
Dan Sandler86647982015-05-13 23:41:13 -04001454 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001455 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1456 if (in.readInt() == 1) {
1457 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1458 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001459 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001460 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001461 mAllowGeneratedReplies = in.readInt() == 1;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001462 mSemanticAction = in.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001463 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001464
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001465 /**
Dan Sandler86647982015-05-13 23:41:13 -04001466 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001467 */
Dan Sandler86647982015-05-13 23:41:13 -04001468 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001469 public Action(int icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001470 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true,
1471 SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001472 }
1473
Adrian Roos7af53622016-10-12 13:44:05 -07001474 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001475 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001476 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1477 @SemanticAction int semanticAction) {
Dan Sandler86647982015-05-13 23:41:13 -04001478 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001479 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1480 this.icon = icon.getResId();
1481 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001482 this.title = title;
1483 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001484 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001485 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001486 this.mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001487 this.mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001488 }
1489
1490 /**
Dan Sandler86647982015-05-13 23:41:13 -04001491 * Return an icon representing the action.
1492 */
1493 public Icon getIcon() {
1494 if (mIcon == null && icon != 0) {
1495 // you snuck an icon in here without using the builder; let's try to keep it
1496 mIcon = Icon.createWithResource("", icon);
1497 }
1498 return mIcon;
1499 }
1500
1501 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001502 * Get additional metadata carried around with this Action.
1503 */
1504 public Bundle getExtras() {
1505 return mExtras;
1506 }
1507
1508 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001509 * Return whether the platform should automatically generate possible replies for this
1510 * {@link Action}
1511 */
1512 public boolean getAllowGeneratedReplies() {
1513 return mAllowGeneratedReplies;
1514 }
1515
1516 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001517 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001518 * May return null if no remote inputs were added. Only returns inputs which accept
1519 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001520 */
1521 public RemoteInput[] getRemoteInputs() {
1522 return mRemoteInputs;
1523 }
1524
1525 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001526 * Returns the {@code SemanticAction} associated with this {@link Action}. A
1527 * {@code SemanticAction} denotes what an {@link Action}'s {@link PendingIntent} will do
1528 * (eg. reply, mark as read, delete, etc).
1529 */
1530 public @SemanticAction int getSemanticAction() {
1531 return mSemanticAction;
1532 }
1533
1534 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001535 * Get the list of inputs to be collected from the user that ONLY accept data when this
1536 * action is sent. These remote inputs are guaranteed to return true on a call to
1537 * {@link RemoteInput#isDataOnly}.
1538 *
Shane Brennanf670d6c2017-04-25 13:05:45 -07001539 * Returns null if there are no data-only remote inputs.
Shane Brennan472a3b32016-12-12 15:28:10 -08001540 *
1541 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1542 * of non-textual RemoteInputs do not access these remote inputs.
1543 */
1544 public RemoteInput[] getDataOnlyRemoteInputs() {
1545 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1546 }
1547
1548 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001549 * Builder class for {@link Action} objects.
1550 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001551 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001552 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001553 private final CharSequence mTitle;
1554 private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001555 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001556 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001557 private ArrayList<RemoteInput> mRemoteInputs;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001558 private @SemanticAction int mSemanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001559
1560 /**
1561 * Construct a new builder for {@link Action} object.
1562 * @param icon icon to show for this action
1563 * @param title the title of the action
1564 * @param intent the {@link PendingIntent} to fire when users trigger this action
1565 */
Dan Sandler86647982015-05-13 23:41:13 -04001566 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001567 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001568 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001569 }
1570
1571 /**
1572 * Construct a new builder for {@link Action} object.
1573 * @param icon icon to show for this action
1574 * @param title the title of the action
1575 * @param intent the {@link PendingIntent} to fire when users trigger this action
1576 */
1577 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001578 this(icon, title, intent, new Bundle(), null, true, SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001579 }
1580
1581 /**
1582 * Construct a new builder for {@link Action} object using the fields from an
1583 * {@link Action}.
1584 * @param action the action to read fields from.
1585 */
1586 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001587 this(action.getIcon(), action.title, action.actionIntent,
1588 new Bundle(action.mExtras), action.getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001589 action.getAllowGeneratedReplies(), action.getSemanticAction());
Griff Hazen959591e2014-05-15 22:26:18 -07001590 }
1591
Dan Sandler86647982015-05-13 23:41:13 -04001592 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001593 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1594 @SemanticAction int semanticAction) {
Griff Hazen959591e2014-05-15 22:26:18 -07001595 mIcon = icon;
1596 mTitle = title;
1597 mIntent = intent;
1598 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001599 if (remoteInputs != null) {
1600 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1601 Collections.addAll(mRemoteInputs, remoteInputs);
1602 }
Adrian Roos7af53622016-10-12 13:44:05 -07001603 mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001604 mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001605 }
1606
1607 /**
1608 * Merge additional metadata into this builder.
1609 *
1610 * <p>Values within the Bundle will replace existing extras values in this Builder.
1611 *
1612 * @see Notification.Action#extras
1613 */
1614 public Builder addExtras(Bundle extras) {
1615 if (extras != null) {
1616 mExtras.putAll(extras);
1617 }
1618 return this;
1619 }
1620
1621 /**
1622 * Get the metadata Bundle used by this Builder.
1623 *
1624 * <p>The returned Bundle is shared with this Builder.
1625 */
1626 public Bundle getExtras() {
1627 return mExtras;
1628 }
1629
1630 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001631 * Add an input to be collected from the user when this action is sent.
1632 * Response values can be retrieved from the fired intent by using the
1633 * {@link RemoteInput#getResultsFromIntent} function.
1634 * @param remoteInput a {@link RemoteInput} to add to the action
1635 * @return this object for method chaining
1636 */
1637 public Builder addRemoteInput(RemoteInput remoteInput) {
1638 if (mRemoteInputs == null) {
1639 mRemoteInputs = new ArrayList<RemoteInput>();
1640 }
1641 mRemoteInputs.add(remoteInput);
1642 return this;
1643 }
1644
1645 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001646 * Set whether the platform should automatically generate possible replies to add to
1647 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1648 * {@link RemoteInput}, this has no effect.
1649 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1650 * otherwise
1651 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001652 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001653 */
1654 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1655 mAllowGeneratedReplies = allowGeneratedReplies;
1656 return this;
1657 }
1658
1659 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001660 * Sets the {@code SemanticAction} for this {@link Action}. A
1661 * {@code SemanticAction} denotes what an {@link Action}'s
1662 * {@link PendingIntent} will do (eg. reply, mark as read, delete, etc).
1663 * @param semanticAction a SemanticAction defined within {@link Action} with
1664 * {@code SEMANTIC_ACTION_} prefixes
1665 * @return this object for method chaining
1666 */
1667 public Builder setSemanticAction(@SemanticAction int semanticAction) {
1668 mSemanticAction = semanticAction;
1669 return this;
1670 }
1671
1672 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001673 * Apply an extender to this action builder. Extenders may be used to add
1674 * metadata or change options on this builder.
1675 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001676 public Builder extend(Extender extender) {
1677 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001678 return this;
1679 }
1680
1681 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001682 * Combine all of the options that have been set and return a new {@link Action}
1683 * object.
1684 * @return the built action
1685 */
1686 public Action build() {
Shane Brennan472a3b32016-12-12 15:28:10 -08001687 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1688 RemoteInput[] previousDataInputs =
1689 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001690 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001691 for (RemoteInput input : previousDataInputs) {
1692 dataOnlyInputs.add(input);
1693 }
1694 }
1695 List<RemoteInput> textInputs = new ArrayList<>();
1696 if (mRemoteInputs != null) {
1697 for (RemoteInput input : mRemoteInputs) {
1698 if (input.isDataOnly()) {
1699 dataOnlyInputs.add(input);
1700 } else {
1701 textInputs.add(input);
1702 }
1703 }
1704 }
1705 if (!dataOnlyInputs.isEmpty()) {
1706 RemoteInput[] dataInputsArr =
1707 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1708 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1709 }
1710 RemoteInput[] textInputsArr = textInputs.isEmpty()
1711 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1712 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001713 mAllowGeneratedReplies, mSemanticAction);
Griff Hazen959591e2014-05-15 22:26:18 -07001714 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001715 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001716
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001717 @Override
1718 public Action clone() {
1719 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001720 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001721 title,
1722 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001723 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001724 getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001725 getAllowGeneratedReplies(),
1726 getSemanticAction());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001727 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001728
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001729 @Override
1730 public int describeContents() {
1731 return 0;
1732 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001733
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001734 @Override
1735 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001736 final Icon ic = getIcon();
1737 if (ic != null) {
1738 out.writeInt(1);
1739 ic.writeToParcel(out, 0);
1740 } else {
1741 out.writeInt(0);
1742 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001743 TextUtils.writeToParcel(title, out, flags);
1744 if (actionIntent != null) {
1745 out.writeInt(1);
1746 actionIntent.writeToParcel(out, flags);
1747 } else {
1748 out.writeInt(0);
1749 }
Griff Hazen959591e2014-05-15 22:26:18 -07001750 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001751 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001752 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001753 out.writeInt(mSemanticAction);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001754 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001755
Griff Hazen959591e2014-05-15 22:26:18 -07001756 public static final Parcelable.Creator<Action> CREATOR =
1757 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001758 public Action createFromParcel(Parcel in) {
1759 return new Action(in);
1760 }
1761 public Action[] newArray(int size) {
1762 return new Action[size];
1763 }
1764 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001765
1766 /**
1767 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1768 * metadata or change options on an action builder.
1769 */
1770 public interface Extender {
1771 /**
1772 * Apply this extender to a notification action builder.
1773 * @param builder the builder to be modified.
1774 * @return the build object for chaining.
1775 */
1776 public Builder extend(Builder builder);
1777 }
1778
1779 /**
1780 * Wearable extender for notification actions. To add extensions to an action,
1781 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1782 * the {@code WearableExtender()} constructor and apply it to a
1783 * {@link android.app.Notification.Action.Builder} using
1784 * {@link android.app.Notification.Action.Builder#extend}.
1785 *
1786 * <pre class="prettyprint">
1787 * Notification.Action action = new Notification.Action.Builder(
1788 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001789 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001790 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001791 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001792 */
1793 public static final class WearableExtender implements Extender {
1794 /** Notification action extra which contains wearable extensions */
1795 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1796
Pete Gastaf6781d2014-10-07 15:17:05 -04001797 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001798 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001799 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1800 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1801 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001802
1803 // Flags bitwise-ored to mFlags
1804 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001805 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001806 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001807
1808 // Default value for flags integer
1809 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1810
1811 private int mFlags = DEFAULT_FLAGS;
1812
Pete Gastaf6781d2014-10-07 15:17:05 -04001813 private CharSequence mInProgressLabel;
1814 private CharSequence mConfirmLabel;
1815 private CharSequence mCancelLabel;
1816
Griff Hazen61a9e862014-05-22 16:05:19 -07001817 /**
1818 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1819 * options.
1820 */
1821 public WearableExtender() {
1822 }
1823
1824 /**
1825 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1826 * wearable options present in an existing notification action.
1827 * @param action the notification action to inspect.
1828 */
1829 public WearableExtender(Action action) {
1830 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1831 if (wearableBundle != null) {
1832 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001833 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1834 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1835 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001836 }
1837 }
1838
1839 /**
1840 * Apply wearable extensions to a notification action that is being built. This is
1841 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1842 * method of {@link android.app.Notification.Action.Builder}.
1843 */
1844 @Override
1845 public Action.Builder extend(Action.Builder builder) {
1846 Bundle wearableBundle = new Bundle();
1847
1848 if (mFlags != DEFAULT_FLAGS) {
1849 wearableBundle.putInt(KEY_FLAGS, mFlags);
1850 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001851 if (mInProgressLabel != null) {
1852 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1853 }
1854 if (mConfirmLabel != null) {
1855 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1856 }
1857 if (mCancelLabel != null) {
1858 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1859 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001860
1861 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1862 return builder;
1863 }
1864
1865 @Override
1866 public WearableExtender clone() {
1867 WearableExtender that = new WearableExtender();
1868 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001869 that.mInProgressLabel = this.mInProgressLabel;
1870 that.mConfirmLabel = this.mConfirmLabel;
1871 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001872 return that;
1873 }
1874
1875 /**
1876 * Set whether this action is available when the wearable device is not connected to
1877 * a companion device. The user can still trigger this action when the wearable device is
1878 * offline, but a visual hint will indicate that the action may not be available.
1879 * Defaults to true.
1880 */
1881 public WearableExtender setAvailableOffline(boolean availableOffline) {
1882 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1883 return this;
1884 }
1885
1886 /**
1887 * Get whether this action is available when the wearable device is not connected to
1888 * a companion device. The user can still trigger this action when the wearable device is
1889 * offline, but a visual hint will indicate that the action may not be available.
1890 * Defaults to true.
1891 */
1892 public boolean isAvailableOffline() {
1893 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1894 }
1895
1896 private void setFlag(int mask, boolean value) {
1897 if (value) {
1898 mFlags |= mask;
1899 } else {
1900 mFlags &= ~mask;
1901 }
1902 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001903
1904 /**
1905 * Set a label to display while the wearable is preparing to automatically execute the
1906 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1907 *
1908 * @param label the label to display while the action is being prepared to execute
1909 * @return this object for method chaining
1910 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001911 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001912 public WearableExtender setInProgressLabel(CharSequence label) {
1913 mInProgressLabel = label;
1914 return this;
1915 }
1916
1917 /**
1918 * Get the label to display while the wearable is preparing to automatically execute
1919 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1920 *
1921 * @return the label to display while the action is being prepared to execute
1922 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001923 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001924 public CharSequence getInProgressLabel() {
1925 return mInProgressLabel;
1926 }
1927
1928 /**
1929 * Set a label to display to confirm that the action should be executed.
1930 * This is usually an imperative verb like "Send".
1931 *
1932 * @param label the label to confirm the action should be executed
1933 * @return this object for method chaining
1934 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001935 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001936 public WearableExtender setConfirmLabel(CharSequence label) {
1937 mConfirmLabel = label;
1938 return this;
1939 }
1940
1941 /**
1942 * Get the label to display to confirm that the action should be executed.
1943 * This is usually an imperative verb like "Send".
1944 *
1945 * @return the label to confirm the action should be executed
1946 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001947 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001948 public CharSequence getConfirmLabel() {
1949 return mConfirmLabel;
1950 }
1951
1952 /**
1953 * Set a label to display to cancel the action.
1954 * This is usually an imperative verb, like "Cancel".
1955 *
1956 * @param label the label to display to cancel the action
1957 * @return this object for method chaining
1958 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001959 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001960 public WearableExtender setCancelLabel(CharSequence label) {
1961 mCancelLabel = label;
1962 return this;
1963 }
1964
1965 /**
1966 * Get the label to display to cancel the action.
1967 * This is usually an imperative verb like "Cancel".
1968 *
1969 * @return the label to display to cancel the action
1970 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001971 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001972 public CharSequence getCancelLabel() {
1973 return mCancelLabel;
1974 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001975
1976 /**
1977 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1978 * platform that it can generate the appropriate transitions.
1979 * @param hintLaunchesActivity {@code true} if the content intent will launch
1980 * an activity and transitions should be generated, false otherwise.
1981 * @return this object for method chaining
1982 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001983 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001984 boolean hintLaunchesActivity) {
1985 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1986 return this;
1987 }
1988
1989 /**
1990 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1991 * platform that it can generate the appropriate transitions
1992 * @return {@code true} if the content intent will launch an activity and transitions
1993 * should be generated, false otherwise. The default value is {@code false} if this was
1994 * never set.
1995 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001996 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001997 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1998 }
Alex Hills9f087612016-06-07 09:08:59 -04001999
2000 /**
2001 * Set a hint that this Action should be displayed inline.
2002 *
2003 * @param hintDisplayInline {@code true} if action should be displayed inline, false
2004 * otherwise
2005 * @return this object for method chaining
2006 */
2007 public WearableExtender setHintDisplayActionInline(
2008 boolean hintDisplayInline) {
2009 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
2010 return this;
2011 }
2012
2013 /**
2014 * Get a hint that this Action should be displayed inline.
2015 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08002016 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04002017 * otherwise. The default value is {@code false} if this was never set.
2018 */
2019 public boolean getHintDisplayActionInline() {
2020 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
2021 }
Griff Hazen61a9e862014-05-22 16:05:19 -07002022 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002023
2024 /**
2025 * Provides meaning to an {@link Action} that hints at what the associated
2026 * {@link PendingIntent} will do. For example, an {@link Action} with a
2027 * {@link PendingIntent} that replies to a text message notification may have the
2028 * {@link #SEMANTIC_ACTION_REPLY} {@code SemanticAction} set within it.
2029 *
2030 * @hide
2031 */
2032 @IntDef(prefix = { "SEMANTIC_ACTION_" }, value = {
2033 SEMANTIC_ACTION_NONE,
2034 SEMANTIC_ACTION_REPLY,
2035 SEMANTIC_ACTION_MARK_AS_READ,
2036 SEMANTIC_ACTION_MARK_AS_UNREAD,
2037 SEMANTIC_ACTION_DELETE,
2038 SEMANTIC_ACTION_ARCHIVE,
2039 SEMANTIC_ACTION_MUTE,
2040 SEMANTIC_ACTION_UNMUTE,
2041 SEMANTIC_ACTION_THUMBS_UP,
Kodlee Yincda5b092018-02-15 15:34:53 -08002042 SEMANTIC_ACTION_THUMBS_DOWN,
2043 SEMANTIC_ACTION_CALL
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002044 })
2045 @Retention(RetentionPolicy.SOURCE)
2046 public @interface SemanticAction {}
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002047 }
2048
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002049 /**
2050 * Array of all {@link Action} structures attached to this notification by
2051 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
2052 * {@link android.service.notification.NotificationListenerService} that provide an alternative
2053 * interface for invoking actions.
2054 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05002055 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002056
2057 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002058 * Replacement version of this notification whose content will be shown
2059 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
2060 * and {@link #VISIBILITY_PUBLIC}.
2061 */
2062 public Notification publicVersion;
2063
2064 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002065 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08002066 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067 */
2068 public Notification()
2069 {
2070 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002071 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002072 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073 }
2074
2075 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002076 * @hide
2077 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002078 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002079 public Notification(Context context, int icon, CharSequence tickerText, long when,
2080 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
2081 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04002082 new Builder(context)
2083 .setWhen(when)
2084 .setSmallIcon(icon)
2085 .setTicker(tickerText)
2086 .setContentTitle(contentTitle)
2087 .setContentText(contentText)
2088 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
2089 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 }
2091
2092 /**
2093 * Constructs a Notification object with the information needed to
2094 * have a status bar icon without the standard expanded view.
2095 *
2096 * @param icon The resource id of the icon to put in the status bar.
2097 * @param tickerText The text that flows by in the status bar when the notification first
2098 * activates.
2099 * @param when The time to show in the time field. In the System.currentTimeMillis
2100 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08002101 *
2102 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002103 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002104 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002105 public Notification(int icon, CharSequence tickerText, long when)
2106 {
2107 this.icon = icon;
2108 this.tickerText = tickerText;
2109 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002110 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002111 }
2112
2113 /**
2114 * Unflatten the notification from a parcel.
2115 */
Svet Ganovddb94882016-06-23 19:55:24 -07002116 @SuppressWarnings("unchecked")
2117 public Notification(Parcel parcel) {
2118 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
2119 // intents in extras are always written as the last entry.
2120 readFromParcelImpl(parcel);
2121 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002122 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07002123 }
2124
2125 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002126 {
2127 int version = parcel.readInt();
2128
Adrian Roosfb921842017-10-26 14:49:56 +02002129 mWhitelistToken = parcel.readStrongBinder();
2130 if (mWhitelistToken == null) {
2131 mWhitelistToken = processWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -07002132 }
2133 // Propagate this token to all pending intents that are unmarshalled from the parcel.
Adrian Roosfb921842017-10-26 14:49:56 +02002134 parcel.setClassCookie(PendingIntent.class, mWhitelistToken);
Dianne Hackborn98305522017-05-05 17:53:53 -07002135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002137 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04002138 if (parcel.readInt() != 0) {
2139 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04002140 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
2141 icon = mSmallIcon.getResId();
2142 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002143 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002144 number = parcel.readInt();
2145 if (parcel.readInt() != 0) {
2146 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2147 }
2148 if (parcel.readInt() != 0) {
2149 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2150 }
2151 if (parcel.readInt() != 0) {
2152 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2153 }
2154 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002155 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002156 }
2157 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002158 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
2159 }
Joe Onorato561d3852010-11-20 18:09:34 -08002160 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002161 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08002162 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002163 defaults = parcel.readInt();
2164 flags = parcel.readInt();
2165 if (parcel.readInt() != 0) {
2166 sound = Uri.CREATOR.createFromParcel(parcel);
2167 }
2168
2169 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04002170 if (parcel.readInt() != 0) {
2171 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
2172 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002173 vibrate = parcel.createLongArray();
2174 ledARGB = parcel.readInt();
2175 ledOnMS = parcel.readInt();
2176 ledOffMS = parcel.readInt();
2177 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002178
2179 if (parcel.readInt() != 0) {
2180 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2181 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002182
2183 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002184
John Spurlockfd7f1e02014-03-18 16:41:57 -04002185 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002186
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002187 mGroupKey = parcel.readString();
2188
2189 mSortKey = parcel.readString();
2190
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06002191 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Robin Leead7e72a2017-12-04 15:45:46 +01002192 fixDuplicateExtras();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002193
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002194 actions = parcel.createTypedArray(Action.CREATOR); // may be null
2195
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002196 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002197 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2198 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002199
Chris Wren8fd39ec2014-02-27 17:43:26 -05002200 if (parcel.readInt() != 0) {
2201 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2202 }
2203
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002204 visibility = parcel.readInt();
2205
2206 if (parcel.readInt() != 0) {
2207 publicVersion = Notification.CREATOR.createFromParcel(parcel);
2208 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002209
2210 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002211
2212 if (parcel.readInt() != 0) {
2213 mChannelId = parcel.readString();
2214 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002215 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05002216
2217 if (parcel.readInt() != 0) {
2218 mShortcutId = parcel.readString();
2219 }
2220
2221 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04002222
2223 if (parcel.readInt() != 0) {
2224 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2225 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002226
2227 mGroupAlertBehavior = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002228 }
2229
Andy Stadler110988c2010-12-03 14:29:16 -08002230 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07002231 public Notification clone() {
2232 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04002233 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002234 return that;
2235 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002236
Daniel Sandler1a497d32013-04-18 14:52:45 -04002237 /**
2238 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
2239 * of this into that.
2240 * @hide
2241 */
2242 public void cloneInto(Notification that, boolean heavy) {
Adrian Roosfb921842017-10-26 14:49:56 +02002243 that.mWhitelistToken = this.mWhitelistToken;
Joe Onorato18e69df2010-05-17 22:26:12 -07002244 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002245 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002246 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07002247 that.number = this.number;
2248
2249 // PendingIntents are global, so there's no reason (or way) to clone them.
2250 that.contentIntent = this.contentIntent;
2251 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002252 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07002253
2254 if (this.tickerText != null) {
2255 that.tickerText = this.tickerText.toString();
2256 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002257 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002258 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04002259 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002260 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07002261 that.contentView = this.contentView.clone();
2262 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002263 if (heavy && this.mLargeIcon != null) {
2264 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08002265 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01002266 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07002267 that.sound = this.sound; // android.net.Uri is immutable
2268 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002269 if (this.audioAttributes != null) {
2270 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
2271 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002272
2273 final long[] vibrate = this.vibrate;
2274 if (vibrate != null) {
2275 final int N = vibrate.length;
2276 final long[] vib = that.vibrate = new long[N];
2277 System.arraycopy(vibrate, 0, vib, 0, N);
2278 }
2279
2280 that.ledARGB = this.ledARGB;
2281 that.ledOnMS = this.ledOnMS;
2282 that.ledOffMS = this.ledOffMS;
2283 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002284
Joe Onorato18e69df2010-05-17 22:26:12 -07002285 that.flags = this.flags;
2286
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002287 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08002288
John Spurlockfd7f1e02014-03-18 16:41:57 -04002289 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002290
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002291 that.mGroupKey = this.mGroupKey;
2292
2293 that.mSortKey = this.mSortKey;
2294
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002295 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002296 try {
2297 that.extras = new Bundle(this.extras);
2298 // will unparcel
2299 that.extras.size();
2300 } catch (BadParcelableException e) {
2301 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
2302 that.extras = null;
2303 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002304 }
2305
Felipe Lemedd85da62016-06-28 11:29:54 -07002306 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2307 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002308 }
2309
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002310 if (this.actions != null) {
2311 that.actions = new Action[this.actions.length];
2312 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002313 if ( this.actions[i] != null) {
2314 that.actions[i] = this.actions[i].clone();
2315 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002316 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002317 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002318
Daniel Sandler1a497d32013-04-18 14:52:45 -04002319 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002320 that.bigContentView = this.bigContentView.clone();
2321 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002322
Chris Wren8fd39ec2014-02-27 17:43:26 -05002323 if (heavy && this.headsUpContentView != null) {
2324 that.headsUpContentView = this.headsUpContentView.clone();
2325 }
2326
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002327 that.visibility = this.visibility;
2328
2329 if (this.publicVersion != null) {
2330 that.publicVersion = new Notification();
2331 this.publicVersion.cloneInto(that.publicVersion, heavy);
2332 }
2333
Dan Sandler26e81cf2014-05-06 10:01:27 -04002334 that.color = this.color;
2335
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002336 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002337 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002338 that.mShortcutId = this.mShortcutId;
2339 that.mBadgeIcon = this.mBadgeIcon;
2340 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002341 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002342
Daniel Sandler1a497d32013-04-18 14:52:45 -04002343 if (!heavy) {
2344 that.lightenPayload(); // will clean out extras
2345 }
2346 }
2347
2348 /**
Jeff Sharkey23b31182018-04-18 21:32:12 -06002349 * Note all {@link Uri} that are referenced internally, with the expectation
2350 * that Uri permission grants will need to be issued to ensure the recipient
2351 * of this object is able to render its contents.
2352 *
2353 * @hide
2354 */
2355 public void visitUris(@NonNull Consumer<Uri> visitor) {
2356 visitor.accept(sound);
2357
2358 if (tickerView != null) tickerView.visitUris(visitor);
2359 if (contentView != null) contentView.visitUris(visitor);
2360 if (bigContentView != null) bigContentView.visitUris(visitor);
2361 if (headsUpContentView != null) headsUpContentView.visitUris(visitor);
2362
2363 if (extras != null) {
2364 visitor.accept(extras.getParcelable(EXTRA_AUDIO_CONTENTS_URI));
Robin Leed107af62018-04-27 13:55:56 +02002365 if (extras.containsKey(EXTRA_BACKGROUND_IMAGE_URI)) {
2366 visitor.accept(Uri.parse(extras.getString(EXTRA_BACKGROUND_IMAGE_URI)));
2367 }
Jeff Sharkey23b31182018-04-18 21:32:12 -06002368 }
2369
2370 if (MessagingStyle.class.equals(getNotificationStyle()) && extras != null) {
2371 final Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
2372 if (!ArrayUtils.isEmpty(messages)) {
2373 for (MessagingStyle.Message message : MessagingStyle.Message
2374 .getMessagesFromBundleArray(messages)) {
2375 visitor.accept(message.getDataUri());
2376 }
2377 }
2378
2379 final Parcelable[] historic = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
2380 if (!ArrayUtils.isEmpty(historic)) {
2381 for (MessagingStyle.Message message : MessagingStyle.Message
2382 .getMessagesFromBundleArray(historic)) {
2383 visitor.accept(message.getDataUri());
2384 }
2385 }
2386 }
2387 }
2388
2389 /**
Daniel Sandler1a497d32013-04-18 14:52:45 -04002390 * Removes heavyweight parts of the Notification object for archival or for sending to
2391 * listeners when the full contents are not necessary.
2392 * @hide
2393 */
2394 public final void lightenPayload() {
2395 tickerView = null;
2396 contentView = null;
2397 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002398 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002399 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002400 if (extras != null && !extras.isEmpty()) {
2401 final Set<String> keyset = extras.keySet();
2402 final int N = keyset.size();
2403 final String[] keys = keyset.toArray(new String[N]);
2404 for (int i=0; i<N; i++) {
2405 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002406 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2407 continue;
2408 }
Dan Sandler50128532015-12-08 15:42:41 -05002409 final Object obj = extras.get(key);
2410 if (obj != null &&
2411 ( obj instanceof Parcelable
2412 || obj instanceof Parcelable[]
2413 || obj instanceof SparseArray
2414 || obj instanceof ArrayList)) {
2415 extras.remove(key);
2416 }
2417 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002418 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002419 }
2420
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002421 /**
2422 * Make sure this CharSequence is safe to put into a bundle, which basically
2423 * means it had better not be some custom Parcelable implementation.
2424 * @hide
2425 */
2426 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002427 if (cs == null) return cs;
2428 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2429 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2430 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002431 if (cs instanceof Parcelable) {
2432 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2433 + " instance is a custom Parcelable and not allowed in Notification");
2434 return cs.toString();
2435 }
Selim Cinek60a54252016-02-26 17:03:25 -08002436 return removeTextSizeSpans(cs);
2437 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002438
Selim Cinek60a54252016-02-26 17:03:25 -08002439 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2440 if (charSequence instanceof Spanned) {
2441 Spanned ss = (Spanned) charSequence;
2442 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2443 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2444 for (Object span : spans) {
2445 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002446 if (resultSpan instanceof CharacterStyle) {
2447 resultSpan = ((CharacterStyle) span).getUnderlying();
2448 }
2449 if (resultSpan instanceof TextAppearanceSpan) {
2450 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002451 resultSpan = new TextAppearanceSpan(
2452 originalSpan.getFamily(),
2453 originalSpan.getTextStyle(),
2454 -1,
2455 originalSpan.getTextColor(),
2456 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002457 } else if (resultSpan instanceof RelativeSizeSpan
2458 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002459 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002460 } else {
2461 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002462 }
2463 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2464 ss.getSpanFlags(span));
2465 }
2466 return builder;
2467 }
2468 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002469 }
2470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002471 public int describeContents() {
2472 return 0;
2473 }
2474
2475 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002476 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002477 */
Svet Ganovddb94882016-06-23 19:55:24 -07002478 public void writeToParcel(Parcel parcel, int flags) {
2479 // We need to mark all pending intents getting into the notification
2480 // system as being put there to later allow the notification ranker
2481 // to launch them and by doing so add the app to the battery saver white
2482 // list for a short period of time. The problem is that the system
2483 // cannot look into the extras as there may be parcelables there that
2484 // the platform does not know how to handle. To go around that we have
2485 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002486 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002487 if (collectPendingIntents) {
2488 PendingIntent.setOnMarshaledListener(
2489 (PendingIntent intent, Parcel out, int outFlags) -> {
2490 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002491 if (allPendingIntents == null) {
2492 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002493 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002494 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002495 }
2496 });
2497 }
2498 try {
2499 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002500 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002501 writeToParcelImpl(parcel, flags);
2502 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002503 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002504 } finally {
2505 if (collectPendingIntents) {
2506 PendingIntent.setOnMarshaledListener(null);
2507 }
2508 }
2509 }
2510
2511 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002512 parcel.writeInt(1);
2513
Adrian Roosfb921842017-10-26 14:49:56 +02002514 parcel.writeStrongBinder(mWhitelistToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002515 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002516 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002517 if (mSmallIcon == null && icon != 0) {
2518 // you snuck an icon in here without using the builder; let's try to keep it
2519 mSmallIcon = Icon.createWithResource("", icon);
2520 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002521 if (mSmallIcon != null) {
2522 parcel.writeInt(1);
2523 mSmallIcon.writeToParcel(parcel, 0);
2524 } else {
2525 parcel.writeInt(0);
2526 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002527 parcel.writeInt(number);
2528 if (contentIntent != null) {
2529 parcel.writeInt(1);
2530 contentIntent.writeToParcel(parcel, 0);
2531 } else {
2532 parcel.writeInt(0);
2533 }
2534 if (deleteIntent != null) {
2535 parcel.writeInt(1);
2536 deleteIntent.writeToParcel(parcel, 0);
2537 } else {
2538 parcel.writeInt(0);
2539 }
2540 if (tickerText != null) {
2541 parcel.writeInt(1);
2542 TextUtils.writeToParcel(tickerText, parcel, flags);
2543 } else {
2544 parcel.writeInt(0);
2545 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002546 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002547 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002548 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002549 } else {
2550 parcel.writeInt(0);
2551 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002552 if (contentView != null) {
2553 parcel.writeInt(1);
2554 contentView.writeToParcel(parcel, 0);
2555 } else {
2556 parcel.writeInt(0);
2557 }
Selim Cinek279fa862016-06-14 10:57:25 -07002558 if (mLargeIcon == null && largeIcon != null) {
2559 // you snuck an icon in here without using the builder; let's try to keep it
2560 mLargeIcon = Icon.createWithBitmap(largeIcon);
2561 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002562 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002563 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002564 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002565 } else {
2566 parcel.writeInt(0);
2567 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002568
2569 parcel.writeInt(defaults);
2570 parcel.writeInt(this.flags);
2571
2572 if (sound != null) {
2573 parcel.writeInt(1);
2574 sound.writeToParcel(parcel, 0);
2575 } else {
2576 parcel.writeInt(0);
2577 }
2578 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002579
2580 if (audioAttributes != null) {
2581 parcel.writeInt(1);
2582 audioAttributes.writeToParcel(parcel, 0);
2583 } else {
2584 parcel.writeInt(0);
2585 }
2586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002587 parcel.writeLongArray(vibrate);
2588 parcel.writeInt(ledARGB);
2589 parcel.writeInt(ledOnMS);
2590 parcel.writeInt(ledOffMS);
2591 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002592
2593 if (fullScreenIntent != null) {
2594 parcel.writeInt(1);
2595 fullScreenIntent.writeToParcel(parcel, 0);
2596 } else {
2597 parcel.writeInt(0);
2598 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002599
2600 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002601
John Spurlockfd7f1e02014-03-18 16:41:57 -04002602 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002603
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002604 parcel.writeString(mGroupKey);
2605
2606 parcel.writeString(mSortKey);
2607
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002608 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002609
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002610 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002611
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002612 if (bigContentView != null) {
2613 parcel.writeInt(1);
2614 bigContentView.writeToParcel(parcel, 0);
2615 } else {
2616 parcel.writeInt(0);
2617 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002618
Chris Wren8fd39ec2014-02-27 17:43:26 -05002619 if (headsUpContentView != null) {
2620 parcel.writeInt(1);
2621 headsUpContentView.writeToParcel(parcel, 0);
2622 } else {
2623 parcel.writeInt(0);
2624 }
2625
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002626 parcel.writeInt(visibility);
2627
2628 if (publicVersion != null) {
2629 parcel.writeInt(1);
2630 publicVersion.writeToParcel(parcel, 0);
2631 } else {
2632 parcel.writeInt(0);
2633 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002634
2635 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002636
2637 if (mChannelId != null) {
2638 parcel.writeInt(1);
2639 parcel.writeString(mChannelId);
2640 } else {
2641 parcel.writeInt(0);
2642 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002643 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002644
2645 if (mShortcutId != null) {
2646 parcel.writeInt(1);
2647 parcel.writeString(mShortcutId);
2648 } else {
2649 parcel.writeInt(0);
2650 }
2651
2652 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002653
2654 if (mSettingsText != null) {
2655 parcel.writeInt(1);
2656 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2657 } else {
2658 parcel.writeInt(0);
2659 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002660
2661 parcel.writeInt(mGroupAlertBehavior);
Julia Reynoldsfc640012018-02-21 12:25:27 -05002662
2663 // mUsesStandardHeader is not written because it should be recomputed in listeners
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002664 }
2665
2666 /**
2667 * Parcelable.Creator that instantiates Notification objects
2668 */
2669 public static final Parcelable.Creator<Notification> CREATOR
2670 = new Parcelable.Creator<Notification>()
2671 {
2672 public Notification createFromParcel(Parcel parcel)
2673 {
2674 return new Notification(parcel);
2675 }
2676
2677 public Notification[] newArray(int size)
2678 {
2679 return new Notification[size];
2680 }
2681 };
2682
2683 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05002684 * @hide
2685 */
2686 public static boolean areActionsVisiblyDifferent(Notification first, Notification second) {
2687 Notification.Action[] firstAs = first.actions;
2688 Notification.Action[] secondAs = second.actions;
2689 if (firstAs == null && secondAs != null || firstAs != null && secondAs == null) {
2690 return true;
2691 }
2692 if (firstAs != null && secondAs != null) {
2693 if (firstAs.length != secondAs.length) {
2694 return true;
2695 }
2696 for (int i = 0; i < firstAs.length; i++) {
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -04002697 if (!Objects.equals(String.valueOf(firstAs[i].title),
2698 String.valueOf(secondAs[i].title))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05002699 return true;
2700 }
2701 RemoteInput[] firstRs = firstAs[i].getRemoteInputs();
2702 RemoteInput[] secondRs = secondAs[i].getRemoteInputs();
2703 if (firstRs == null) {
2704 firstRs = new RemoteInput[0];
2705 }
2706 if (secondRs == null) {
2707 secondRs = new RemoteInput[0];
2708 }
2709 if (firstRs.length != secondRs.length) {
2710 return true;
2711 }
2712 for (int j = 0; j < firstRs.length; j++) {
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -04002713 if (!Objects.equals(String.valueOf(firstRs[j].getLabel()),
2714 String.valueOf(secondRs[j].getLabel()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05002715 return true;
2716 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05002717 }
2718 }
2719 }
2720 return false;
2721 }
2722
2723 /**
2724 * @hide
2725 */
2726 public static boolean areStyledNotificationsVisiblyDifferent(Builder first, Builder second) {
2727 if (first.getStyle() == null) {
2728 return second.getStyle() != null;
2729 }
2730 if (second.getStyle() == null) {
2731 return true;
2732 }
2733 return first.getStyle().areNotificationsVisiblyDifferent(second.getStyle());
2734 }
2735
2736 /**
2737 * @hide
2738 */
2739 public static boolean areRemoteViewsChanged(Builder first, Builder second) {
Julia Reynoldse5c60452018-04-30 14:41:36 -04002740 if (!Objects.equals(first.usesStandardHeader(), second.usesStandardHeader())) {
2741 return true;
2742 }
2743
2744 if (areRemoteViewsChanged(first.mN.contentView, second.mN.contentView)) {
2745 return true;
2746 }
2747 if (areRemoteViewsChanged(first.mN.bigContentView, second.mN.bigContentView)) {
2748 return true;
2749 }
2750 if (areRemoteViewsChanged(first.mN.headsUpContentView, second.mN.headsUpContentView)) {
2751 return true;
2752 }
2753
2754 return false;
2755 }
2756
2757 private static boolean areRemoteViewsChanged(RemoteViews first, RemoteViews second) {
2758 if (first == null && second == null) {
2759 return false;
2760 }
2761 if (first == null && second != null || first != null && second == null) {
2762 return true;
2763 }
2764
2765 if (!Objects.equals(first.getLayoutId(), second.getLayoutId())) {
2766 return true;
2767 }
2768
2769 if (!Objects.equals(first.getSequenceNumber(), second.getSequenceNumber())) {
2770 return true;
2771 }
2772
2773 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05002774 }
2775
2776 /**
Robin Leead7e72a2017-12-04 15:45:46 +01002777 * Parcelling creates multiple copies of objects in {@code extras}. Fix them.
2778 * <p>
2779 * For backwards compatibility {@code extras} holds some references to "real" member data such
2780 * as {@link getLargeIcon()} which is mirrored by {@link #EXTRA_LARGE_ICON}. This is mostly
2781 * fine as long as the object stays in one process.
2782 * <p>
2783 * However, once the notification goes into a parcel each reference gets marshalled separately,
2784 * wasting memory. Especially with large images on Auto and TV, this is worth fixing.
2785 */
2786 private void fixDuplicateExtras() {
2787 if (extras != null) {
Robin Leead7e72a2017-12-04 15:45:46 +01002788 fixDuplicateExtra(mLargeIcon, EXTRA_LARGE_ICON);
2789 }
2790 }
2791
2792 /**
2793 * If we find an extra that's exactly the same as one of the "real" fields but refers to a
2794 * separate object, replace it with the field's version to avoid holding duplicate copies.
2795 */
2796 private void fixDuplicateExtra(@Nullable Parcelable original, @NonNull String extraName) {
2797 if (original != null && extras.getParcelable(extraName) != null) {
2798 extras.putParcelable(extraName, original);
2799 }
2800 }
2801
2802 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002803 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2804 * layout.
2805 *
2806 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2807 * in the view.</p>
2808 * @param context The context for your application / activity.
2809 * @param contentTitle The title that goes in the expanded entry.
2810 * @param contentText The text that goes in the expanded entry.
2811 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2812 * If this is an activity, it must include the
2813 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002814 * that you take care of task management as described in the
2815 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2816 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002817 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002818 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002819 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002820 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002821 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002822 public void setLatestEventInfo(Context context,
2823 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002824 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2825 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2826 new Throwable());
2827 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002828
Selim Cinek4ac6f602016-06-13 15:47:03 -07002829 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2830 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2831 }
2832
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002833 // ensure that any information already set directly is preserved
2834 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002835
2836 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002837 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002838 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002839 }
2840 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002841 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002843 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002844
2845 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002846 }
2847
Julia Reynoldsda303542015-11-23 14:00:20 -05002848 /**
2849 * @hide
2850 */
2851 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002852 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002853 }
2854
2855 /**
2856 * @hide
2857 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002858 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002859 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002860 }
2861
Yi Jin6b514142017-10-30 14:54:12 -07002862 /**
2863 * @hide
2864 */
2865 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2866 long token = proto.start(fieldId);
2867 proto.write(NotificationProto.CHANNEL_ID, getChannelId());
2868 proto.write(NotificationProto.HAS_TICKER_TEXT, this.tickerText != null);
2869 proto.write(NotificationProto.FLAGS, this.flags);
2870 proto.write(NotificationProto.COLOR, this.color);
2871 proto.write(NotificationProto.CATEGORY, this.category);
2872 proto.write(NotificationProto.GROUP_KEY, this.mGroupKey);
2873 proto.write(NotificationProto.SORT_KEY, this.mSortKey);
2874 if (this.actions != null) {
2875 proto.write(NotificationProto.ACTION_LENGTH, this.actions.length);
2876 }
2877 if (this.visibility >= VISIBILITY_SECRET && this.visibility <= VISIBILITY_PUBLIC) {
2878 proto.write(NotificationProto.VISIBILITY, this.visibility);
2879 }
2880 if (publicVersion != null) {
2881 publicVersion.writeToProto(proto, NotificationProto.PUBLIC_VERSION);
2882 }
2883 proto.end(token);
2884 }
2885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002886 @Override
2887 public String toString() {
2888 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002889 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04002890 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002891 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002892 sb.append(priority);
2893 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002894 if (contentView != null) {
2895 sb.append(contentView.getPackage());
2896 sb.append("/0x");
2897 sb.append(Integer.toHexString(contentView.getLayoutId()));
2898 } else {
2899 sb.append("null");
2900 }
2901 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002902 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2903 sb.append("default");
2904 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002905 int N = this.vibrate.length-1;
2906 sb.append("[");
2907 for (int i=0; i<N; i++) {
2908 sb.append(this.vibrate[i]);
2909 sb.append(',');
2910 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002911 if (N != -1) {
2912 sb.append(this.vibrate[N]);
2913 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002914 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002915 } else {
2916 sb.append("null");
2917 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002918 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002919 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002920 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002921 } else if (this.sound != null) {
2922 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002923 } else {
2924 sb.append("null");
2925 }
Chris Wren365b6d32015-07-16 10:39:26 -04002926 if (this.tickerText != null) {
2927 sb.append(" tick");
2928 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002929 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002930 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002931 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002932 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002933 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002934 if (this.category != null) {
2935 sb.append(" category=");
2936 sb.append(this.category);
2937 }
2938 if (this.mGroupKey != null) {
2939 sb.append(" groupKey=");
2940 sb.append(this.mGroupKey);
2941 }
2942 if (this.mSortKey != null) {
2943 sb.append(" sortKey=");
2944 sb.append(this.mSortKey);
2945 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002946 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002947 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002948 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002949 }
2950 sb.append(" vis=");
2951 sb.append(visibilityToString(this.visibility));
2952 if (this.publicVersion != null) {
2953 sb.append(" publicVersion=");
2954 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002955 }
2956 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002957 return sb.toString();
2958 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002959
Dan Sandler1b718782014-07-18 12:43:45 -04002960 /**
2961 * {@hide}
2962 */
2963 public static String visibilityToString(int vis) {
2964 switch (vis) {
2965 case VISIBILITY_PRIVATE:
2966 return "PRIVATE";
2967 case VISIBILITY_PUBLIC:
2968 return "PUBLIC";
2969 case VISIBILITY_SECRET:
2970 return "SECRET";
2971 default:
2972 return "UNKNOWN(" + String.valueOf(vis) + ")";
2973 }
2974 }
2975
Joe Onoratocb109a02011-01-18 17:57:41 -08002976 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002977 * {@hide}
2978 */
2979 public static String priorityToString(@Priority int pri) {
2980 switch (pri) {
2981 case PRIORITY_MIN:
2982 return "MIN";
2983 case PRIORITY_LOW:
2984 return "LOW";
2985 case PRIORITY_DEFAULT:
2986 return "DEFAULT";
2987 case PRIORITY_HIGH:
2988 return "HIGH";
2989 case PRIORITY_MAX:
2990 return "MAX";
2991 default:
2992 return "UNKNOWN(" + String.valueOf(pri) + ")";
2993 }
2994 }
2995
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04002996 /**
2997 * @hide
2998 */
2999 public boolean hasCompletedProgress() {
3000 // not a progress notification; can't be complete
3001 if (!extras.containsKey(EXTRA_PROGRESS)
3002 || !extras.containsKey(EXTRA_PROGRESS_MAX)) {
3003 return false;
3004 }
3005 // many apps use max 0 for 'indeterminate'; not complete
3006 if (extras.getInt(EXTRA_PROGRESS_MAX) == 0) {
3007 return false;
3008 }
3009 return extras.getInt(EXTRA_PROGRESS) == extras.getInt(EXTRA_PROGRESS_MAX);
3010 }
3011
Jeff Sharkey000ce802017-04-29 13:13:27 -06003012 /** @removed */
3013 @Deprecated
Julia Reynolds37856052016-11-11 09:20:07 -05003014 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003015 return mChannelId;
3016 }
3017
3018 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003019 * Returns the id of the channel this notification posts to.
3020 */
3021 public String getChannelId() {
3022 return mChannelId;
3023 }
3024
Jeff Sharkey000ce802017-04-29 13:13:27 -06003025 /** @removed */
3026 @Deprecated
Julia Reynolds2a128742016-11-28 14:29:25 -05003027 public long getTimeout() {
3028 return mTimeout;
3029 }
3030
3031 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003032 * Returns the duration from posting after which this notification should be canceled by the
3033 * system, if it's not canceled already.
3034 */
3035 public long getTimeoutAfter() {
3036 return mTimeout;
3037 }
3038
3039 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003040 * Returns what icon should be shown for this notification if it is being displayed in a
3041 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
3042 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
3043 */
3044 public int getBadgeIconType() {
3045 return mBadgeIcon;
3046 }
3047
3048 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003049 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04003050 *
3051 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
3052 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003053 */
3054 public String getShortcutId() {
3055 return mShortcutId;
3056 }
3057
Julia Reynolds3aedded2017-03-31 14:42:09 -04003058
3059 /**
3060 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
3061 */
3062 public CharSequence getSettingsText() {
3063 return mSettingsText;
3064 }
3065
Julia Reynolds13d898c2017-02-02 12:22:05 -05003066 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003067 * Returns which type of notifications in a group are responsible for audibly alerting the
3068 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
3069 * {@link #GROUP_ALERT_SUMMARY}.
3070 */
3071 public @GroupAlertBehavior int getGroupAlertBehavior() {
3072 return mGroupAlertBehavior;
3073 }
3074
3075 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003076 * The small icon representing this notification in the status bar and content view.
3077 *
3078 * @return the small icon representing this notification.
3079 *
3080 * @see Builder#getSmallIcon()
3081 * @see Builder#setSmallIcon(Icon)
3082 */
3083 public Icon getSmallIcon() {
3084 return mSmallIcon;
3085 }
3086
3087 /**
3088 * Used when notifying to clean up legacy small icons.
3089 * @hide
3090 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003091 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04003092 public void setSmallIcon(Icon icon) {
3093 mSmallIcon = icon;
3094 }
3095
3096 /**
3097 * The large icon shown in this notification's content view.
3098 * @see Builder#getLargeIcon()
3099 * @see Builder#setLargeIcon(Icon)
3100 */
3101 public Icon getLargeIcon() {
3102 return mLargeIcon;
3103 }
3104
3105 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02003106 * @hide
3107 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003108 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003109 public boolean isGroupSummary() {
3110 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
3111 }
3112
3113 /**
3114 * @hide
3115 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003116 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003117 public boolean isGroupChild() {
3118 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
3119 }
3120
3121 /**
Julia Reynolds30203152017-05-26 13:36:31 -04003122 * @hide
3123 */
3124 public boolean suppressAlertingDueToGrouping() {
3125 if (isGroupSummary()
3126 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
3127 return true;
3128 } else if (isGroupChild()
3129 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
3130 return true;
3131 }
3132 return false;
3133 }
3134
Tony Mak638430e2018-10-08 19:19:10 +01003135
3136 /**
3137 * Finds and returns a remote input and its corresponding action.
3138 *
3139 * @param requiresFreeform requires the remoteinput to allow freeform or not.
3140 * @return the result pair, {@code null} if no result is found.
3141 *
3142 * @hide
3143 */
3144 @Nullable
3145 public Pair<RemoteInput, Action> findRemoteInputActionPair(boolean requiresFreeform) {
3146 if (actions == null) {
3147 return null;
3148 }
3149 for (Notification.Action action : actions) {
3150 if (action.getRemoteInputs() == null) {
3151 continue;
3152 }
3153 RemoteInput resultRemoteInput = null;
3154 for (RemoteInput remoteInput : action.getRemoteInputs()) {
3155 if (remoteInput.getAllowFreeFormInput() || !requiresFreeform) {
3156 resultRemoteInput = remoteInput;
3157 }
3158 }
3159 if (resultRemoteInput != null) {
3160 return Pair.create(resultRemoteInput, action);
3161 }
3162 }
3163 return null;
3164 }
3165
Julia Reynolds30203152017-05-26 13:36:31 -04003166 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003167 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08003168 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003169 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07003170 * content views using the platform's notification layout template. If your app supports
3171 * versions of Android as old as API level 4, you can instead use
3172 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
3173 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
3174 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08003175 *
Scott Main183bf112012-08-13 19:12:13 -07003176 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08003177 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003178 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07003179 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003180 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3181 * .setContentText(subject)
3182 * .setSmallIcon(R.drawable.new_mail)
3183 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003184 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003185 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08003186 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003187 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003188 /**
3189 * @hide
3190 */
3191 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
3192 "android.rebuild.contentViewActionCount";
3193 /**
3194 * @hide
3195 */
3196 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
3197 = "android.rebuild.bigViewActionCount";
3198 /**
3199 * @hide
3200 */
3201 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
3202 = "android.rebuild.hudViewActionCount";
3203
Selim Cinek6743c0b2017-01-18 18:24:01 -08003204 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
3205 SystemProperties.getBoolean("notifications.only_title", true);
3206
Selim Cinek389edcd2017-05-11 19:16:44 -07003207 /**
3208 * The lightness difference that has to be added to the primary text color to obtain the
3209 * secondary text color when the background is light.
3210 */
3211 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
3212
3213 /**
3214 * The lightness difference that has to be added to the primary text color to obtain the
3215 * secondary text color when the background is dark.
3216 * A bit less then the above value, since it looks better on dark backgrounds.
3217 */
3218 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
3219
Joe Onorato46439ce2010-11-19 13:56:21 -08003220 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003221 private Notification mN;
3222 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003223 private Style mStyle;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003224 @UnsupportedAppUsage
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003225 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Selim Cineke7238dd2017-12-14 17:48:32 -08003226 private ArrayList<Person> mPersonList = new ArrayList<>();
Lucas Dupina291d192018-06-07 13:59:42 -07003227 private ContrastColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08003228 private boolean mIsLegacy;
3229 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02003230
3231 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08003232 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
3233 */
3234 private int mCachedContrastColor = COLOR_INVALID;
3235 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08003236 /**
Selim Cinek4717d862018-04-19 09:19:15 +08003237 * Caches a ambient version of {@link #mCachedAmbientColorIsFor}.
Adrian Roos487374f2017-01-11 15:48:14 -08003238 */
3239 private int mCachedAmbientColor = COLOR_INVALID;
3240 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Selim Cinek4717d862018-04-19 09:19:15 +08003241 /**
3242 * A neutral color color that can be used for icons.
3243 */
3244 private int mNeutralColor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08003245
3246 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08003247 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
3248 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
3249 */
3250 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08003251 private int mTextColorsAreForBackground = COLOR_INVALID;
3252 private int mPrimaryTextColor = COLOR_INVALID;
3253 private int mSecondaryTextColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07003254 private int mBackgroundColor = COLOR_INVALID;
3255 private int mForegroundColor = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07003256 /**
3257 * A temporary location where actions are stored. If != null the view originally has action
3258 * but doesn't have any for this inflation.
3259 */
3260 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07003261 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003262
Anthony Chenad4d1582017-04-10 16:07:58 -07003263 private boolean mTintActionButtons;
3264 private boolean mInNightMode;
3265
Adrian Roos70d7aa32017-01-11 15:39:06 -08003266 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003267 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08003268 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003269 * @param context
3270 * A {@link Context} that will be used by the Builder to construct the
3271 * RemoteViews. The Context will not be held past the lifetime of this Builder
3272 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003273 * @param channelId
3274 * The constructed Notification will be posted on this
3275 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
3276 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003277 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003278 public Builder(Context context, String channelId) {
3279 this(context, (Notification) null);
3280 mN.mChannelId = channelId;
3281 }
3282
3283 /**
3284 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
3285 * instead. All posted Notifications must specify a NotificationChannel Id.
3286 */
3287 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003288 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003289 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003290 }
3291
Joe Onoratocb109a02011-01-18 17:57:41 -08003292 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003293 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02003294 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003295 public Builder(Context context, Notification toAdopt) {
3296 mContext = context;
Anthony Chenad4d1582017-04-10 16:07:58 -07003297 Resources res = mContext.getResources();
3298 mTintActionButtons = res.getBoolean(R.bool.config_tintNotificationActionButtons);
3299
3300 if (res.getBoolean(R.bool.config_enableNightMode)) {
3301 Configuration currentConfig = res.getConfiguration();
3302 mInNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
3303 == Configuration.UI_MODE_NIGHT_YES;
3304 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003305
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003306 if (toAdopt == null) {
3307 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003308 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
3309 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
3310 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003311 mN.priority = PRIORITY_DEFAULT;
3312 mN.visibility = VISIBILITY_PRIVATE;
3313 } else {
3314 mN = toAdopt;
3315 if (mN.actions != null) {
3316 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003317 }
3318
Selim Cineke7238dd2017-12-14 17:48:32 -08003319 if (mN.extras.containsKey(EXTRA_PEOPLE_LIST)) {
3320 ArrayList<Person> people = mN.extras.getParcelableArrayList(EXTRA_PEOPLE_LIST);
3321 mPersonList.addAll(people);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003322 }
3323
Selim Cinek4ac6f602016-06-13 15:47:03 -07003324 if (mN.getSmallIcon() == null && mN.icon != 0) {
3325 setSmallIcon(mN.icon);
3326 }
3327
3328 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
3329 setLargeIcon(mN.largeIcon);
3330 }
3331
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003332 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
3333 if (!TextUtils.isEmpty(templateClass)) {
3334 final Class<? extends Style> styleClass
3335 = getNotificationStyleClass(templateClass);
3336 if (styleClass == null) {
3337 Log.d(TAG, "Unknown style class: " + templateClass);
3338 } else {
3339 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003340 final Constructor<? extends Style> ctor =
3341 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003342 ctor.setAccessible(true);
3343 final Style style = ctor.newInstance();
3344 style.restoreFromExtras(mN.extras);
3345
3346 if (style != null) {
3347 setStyle(style);
3348 }
3349 } catch (Throwable t) {
3350 Log.e(TAG, "Could not create Style", t);
3351 }
3352 }
3353 }
3354
3355 }
3356 }
3357
Lucas Dupina291d192018-06-07 13:59:42 -07003358 private ContrastColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08003359 if (mColorUtil == null) {
Lucas Dupina291d192018-06-07 13:59:42 -07003360 mColorUtil = ContrastColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003361 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003362 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003363 }
3364
3365 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003366 * If this notification is duplicative of a Launcher shortcut, sets the
3367 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
3368 * the shortcut.
3369 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04003370 * This field will be ignored by Launchers that don't support badging, don't show
3371 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003372 *
3373 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
3374 * supersedes
3375 */
3376 public Builder setShortcutId(String shortcutId) {
3377 mN.mShortcutId = shortcutId;
3378 return this;
3379 }
3380
3381 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003382 * Sets which icon to display as a badge for this notification.
3383 *
3384 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
3385 * {@link #BADGE_ICON_LARGE}.
3386 *
3387 * Note: This value might be ignored, for launchers that don't support badge icons.
3388 */
Julia Reynolds612beb22017-03-30 10:48:30 -04003389 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04003390 mN.mBadgeIcon = icon;
3391 return this;
3392 }
3393
3394 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003395 * Sets the group alert behavior for this notification. Use this method to mute this
3396 * notification if alerts for this notification's group should be handled by a different
3397 * notification. This is only applicable for notifications that belong to a
Julia Reynolds399d9bf2017-08-11 12:52:14 -04003398 * {@link #setGroup(String) group}. This must be called on all notifications you want to
3399 * mute. For example, if you want only the summary of your group to make noise, all
3400 * children in the group should have the group alert behavior {@link #GROUP_ALERT_SUMMARY}.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003401 *
3402 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
3403 */
3404 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
3405 mN.mGroupAlertBehavior = groupAlertBehavior;
3406 return this;
3407 }
3408
Jeff Sharkey000ce802017-04-29 13:13:27 -06003409 /** @removed */
3410 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003411 public Builder setChannel(String channelId) {
3412 mN.mChannelId = channelId;
3413 return this;
3414 }
3415
3416 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003417 * Specifies the channel the notification should be delivered on.
3418 */
3419 public Builder setChannelId(String channelId) {
3420 mN.mChannelId = channelId;
3421 return this;
3422 }
3423
Jeff Sharkey000ce802017-04-29 13:13:27 -06003424 /** @removed */
3425 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05003426 public Builder setTimeout(long durationMs) {
3427 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05003428 return this;
3429 }
3430
3431 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003432 * Specifies a duration in milliseconds after which this notification should be canceled,
3433 * if it is not already canceled.
3434 */
3435 public Builder setTimeoutAfter(long durationMs) {
3436 mN.mTimeout = durationMs;
3437 return this;
3438 }
3439
3440 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003441 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003442 *
3443 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
3444 * shown anymore by default and must be opted into by using
3445 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003446 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003447 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08003448 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003449 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003450 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08003451 return this;
3452 }
3453
Joe Onoratocb109a02011-01-18 17:57:41 -08003454 /**
Griff Hazen50c11652014-05-16 09:46:31 -07003455 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07003456 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003457 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
3458 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07003459 */
3460 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003461 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07003462 return this;
3463 }
3464
3465 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003466 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08003467 *
3468 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003469 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003470 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003471 * Useful when showing an elapsed time (like an ongoing phone call).
3472 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08003473 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07003474 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08003475 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003476 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003477 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07003478 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003479 */
3480 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003481 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003482 return this;
3483 }
3484
3485 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08003486 * Sets the Chronometer to count down instead of counting up.
3487 *
3488 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
3489 * If it isn't set the chronometer will count up.
3490 *
3491 * @see #setUsesChronometer(boolean)
3492 */
Adrian Roos96b7e202016-05-17 13:50:38 -07003493 public Builder setChronometerCountDown(boolean countDown) {
3494 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003495 return this;
3496 }
3497
3498 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003499 * Set the small icon resource, which will be used to represent the notification in the
3500 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003501 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003502
3503 * The platform template for the expanded view will draw this icon in the left, unless a
3504 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3505 * icon will be moved to the right-hand side.
3506 *
3507
3508 * @param icon
3509 * A resource ID in the application's package of the drawable to use.
3510 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003511 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003512 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003513 return setSmallIcon(icon != 0
3514 ? Icon.createWithResource(mContext, icon)
3515 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003516 }
3517
Joe Onoratocb109a02011-01-18 17:57:41 -08003518 /**
3519 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3520 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3521 * LevelListDrawable}.
3522 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003523 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003524 * @param level The level to use for the icon.
3525 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003526 * @see Notification#icon
3527 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003528 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003529 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003530 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003531 return setSmallIcon(icon);
3532 }
3533
3534 /**
3535 * Set the small icon, which will be used to represent the notification in the
koprivadebd4ee2018-09-13 10:59:46 -07003536 * status bar and content view (unless overridden there by a
Dan Sandlerd63f9322015-05-06 15:18:49 -04003537 * {@link #setLargeIcon(Bitmap) large icon}).
3538 *
3539 * @param icon An Icon object to use.
3540 * @see Notification#icon
3541 */
3542 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003543 mN.setSmallIcon(icon);
3544 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3545 mN.icon = icon.getResId();
3546 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003547 return this;
3548 }
3549
Joe Onoratocb109a02011-01-18 17:57:41 -08003550 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003551 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003552 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003553 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003554 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003555 return this;
3556 }
3557
Joe Onoratocb109a02011-01-18 17:57:41 -08003558 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003559 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003560 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003561 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003562 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003563 return this;
3564 }
3565
Joe Onoratocb109a02011-01-18 17:57:41 -08003566 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003567 * This provides some additional information that is displayed in the notification. No
3568 * guarantees are given where exactly it is displayed.
3569 *
3570 * <p>This information should only be provided if it provides an essential
3571 * benefit to the understanding of the notification. The more text you provide the
3572 * less readable it becomes. For example, an email client should only provide the account
3573 * name here if more than one email account has been added.</p>
3574 *
3575 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3576 * notification header area.
3577 *
3578 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3579 * this will be shown in the third line of text in the platform notification template.
3580 * You should not be using {@link #setProgress(int, int, boolean)} at the
3581 * same time on those versions; they occupy the same place.
3582 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003583 */
3584 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003585 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003586 return this;
3587 }
3588
3589 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003590 * Provides text that will appear as a link to your application's settings.
3591 *
3592 * <p>This text does not appear within notification {@link Style templates} but may
3593 * appear when the user uses an affordance to learn more about the notification.
3594 * Additionally, this text will not appear unless you provide a valid link target by
3595 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3596 *
3597 * <p>This text is meant to be concise description about what the user can customize
3598 * when they click on this link. The recommended maximum length is 40 characters.
3599 * @param text
3600 * @return
3601 */
3602 public Builder setSettingsText(CharSequence text) {
3603 mN.mSettingsText = safeCharSequence(text);
3604 return this;
3605 }
3606
3607 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003608 * Set the remote input history.
3609 *
3610 * This should be set to the most recent inputs that have been sent
3611 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3612 * longer relevant (e.g. for chat notifications once the other party has responded).
3613 *
3614 * The most recent input must be stored at the 0 index, the second most recent at the
3615 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3616 * and how much of each individual input is shown.
3617 *
3618 * <p>Note: The reply text will only be shown on notifications that have least one action
3619 * with a {@code RemoteInput}.</p>
3620 */
3621 public Builder setRemoteInputHistory(CharSequence[] text) {
3622 if (text == null) {
3623 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3624 } else {
3625 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3626 CharSequence[] safe = new CharSequence[N];
3627 for (int i = 0; i < N; i++) {
3628 safe[i] = safeCharSequence(text[i]);
3629 }
3630 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3631 }
3632 return this;
3633 }
3634
3635 /**
Kenny Guya0f6de82018-04-06 16:20:16 +01003636 * Sets whether remote history entries view should have a spinner.
3637 * @hide
3638 */
3639 public Builder setShowRemoteInputSpinner(boolean showSpinner) {
3640 mN.extras.putBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER, showSpinner);
3641 return this;
3642 }
3643
3644 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +01003645 * Sets whether smart reply buttons should be hidden.
3646 * @hide
3647 */
3648 public Builder setHideSmartReplies(boolean hideSmartReplies) {
3649 mN.extras.putBoolean(EXTRA_HIDE_SMART_REPLIES, hideSmartReplies);
3650 return this;
3651 }
3652
3653 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003654 * Sets the number of items this notification represents. May be displayed as a badge count
3655 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003656 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08003657 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003658 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003659 return this;
3660 }
3661
Joe Onoratocb109a02011-01-18 17:57:41 -08003662 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003663 * A small piece of additional information pertaining to this notification.
3664 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003665 * The platform template will draw this on the last line of the notification, at the far
3666 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003667 *
3668 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3669 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3670 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003671 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003672 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003673 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003674 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003675 return this;
3676 }
3677
Joe Onoratocb109a02011-01-18 17:57:41 -08003678 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003679 * Set the progress this notification represents.
3680 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003681 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003682 */
3683 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003684 mN.extras.putInt(EXTRA_PROGRESS, progress);
3685 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3686 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003687 return this;
3688 }
3689
3690 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003691 * Supply a custom RemoteViews to use instead of the platform template.
3692 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003693 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003694 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003695 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003696 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003697 return setCustomContentView(views);
3698 }
3699
3700 /**
3701 * Supply custom RemoteViews to use instead of the platform template.
3702 *
3703 * This will override the layout that would otherwise be constructed by this Builder
3704 * object.
3705 */
3706 public Builder setCustomContentView(RemoteViews contentView) {
3707 mN.contentView = contentView;
3708 return this;
3709 }
3710
3711 /**
3712 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3713 *
3714 * This will override the expanded layout that would otherwise be constructed by this
3715 * Builder object.
3716 */
3717 public Builder setCustomBigContentView(RemoteViews contentView) {
3718 mN.bigContentView = contentView;
3719 return this;
3720 }
3721
3722 /**
3723 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3724 *
3725 * This will override the heads-up layout that would otherwise be constructed by this
3726 * Builder object.
3727 */
3728 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3729 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003730 return this;
3731 }
3732
Joe Onoratocb109a02011-01-18 17:57:41 -08003733 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003734 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3735 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003736 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3737 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3738 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003739 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003740 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003741 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003742 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003743 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003744 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003745 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003746 return this;
3747 }
3748
Joe Onoratocb109a02011-01-18 17:57:41 -08003749 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003750 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3751 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003752 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003753 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003754 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003755 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003756 return this;
3757 }
3758
Joe Onoratocb109a02011-01-18 17:57:41 -08003759 /**
3760 * An intent to launch instead of posting the notification to the status bar.
3761 * Only for use with extremely high-priority notifications demanding the user's
3762 * <strong>immediate</strong> attention, such as an incoming phone call or
3763 * alarm clock that the user has explicitly set to a particular time.
3764 * If this facility is used for something else, please give the user an option
3765 * to turn it off and use a normal notification, as this can be extremely
3766 * disruptive.
3767 *
Chris Wren47c20a12014-06-18 17:27:29 -04003768 * <p>
3769 * The system UI may choose to display a heads-up notification, instead of
3770 * launching this intent, while the user is using the device.
3771 * </p>
3772 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003773 * @param intent The pending intent to launch.
3774 * @param highPriority Passing true will cause this notification to be sent
3775 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003776 *
3777 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003778 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003779 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003780 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003781 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3782 return this;
3783 }
3784
Joe Onoratocb109a02011-01-18 17:57:41 -08003785 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003786 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003787 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003788 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003789 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003790 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003791 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003792 return this;
3793 }
3794
Joe Onoratocb109a02011-01-18 17:57:41 -08003795 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003796 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003797 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003798 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003799 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003800 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003801 setTicker(tickerText);
3802 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003803 return this;
3804 }
3805
Joe Onoratocb109a02011-01-18 17:57:41 -08003806 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003807 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003808 *
3809 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04003810 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3811 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04003812 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003813 public Builder setLargeIcon(Bitmap b) {
3814 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3815 }
3816
3817 /**
3818 * Add a large icon to the notification content view.
3819 *
3820 * In the platform template, this image will be shown on the left of the notification view
3821 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3822 * badge atop the large icon).
3823 */
3824 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003825 mN.mLargeIcon = icon;
3826 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08003827 return this;
3828 }
3829
Joe Onoratocb109a02011-01-18 17:57:41 -08003830 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003831 * Set the sound to play.
3832 *
John Spurlockc0650f022014-07-19 13:22:39 -04003833 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
3834 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003835 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003836 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003837 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003838 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08003839 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003840 mN.sound = sound;
3841 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08003842 return this;
3843 }
3844
Joe Onoratocb109a02011-01-18 17:57:41 -08003845 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003846 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08003847 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003848 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
3849 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003850 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003851 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07003852 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003853 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08003854 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003855 mN.sound = sound;
3856 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08003857 return this;
3858 }
3859
Joe Onoratocb109a02011-01-18 17:57:41 -08003860 /**
John Spurlockc0650f022014-07-19 13:22:39 -04003861 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
3862 * use during playback.
3863 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003864 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04003865 * @see Notification#sound
3866 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003867 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04003868 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003869 mN.sound = sound;
3870 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04003871 return this;
3872 }
3873
3874 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003875 * Set the vibration pattern to use.
3876 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003877 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
3878 * <code>pattern</code> parameter.
3879 *
Chris Wren47c20a12014-06-18 17:27:29 -04003880 * <p>
3881 * A notification that vibrates is more likely to be presented as a heads-up notification.
3882 * </p>
3883 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003884 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003885 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08003886 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003887 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003888 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003889 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08003890 return this;
3891 }
3892
Joe Onoratocb109a02011-01-18 17:57:41 -08003893 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003894 * Set the desired color for the indicator LED on the device, as well as the
3895 * blink duty cycle (specified in milliseconds).
3896 *
3897
3898 * Not all devices will honor all (or even any) of these values.
3899 *
Julia Reynolds529e3322017-02-06 08:33:01 -05003900 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003901 * @see Notification#ledARGB
3902 * @see Notification#ledOnMS
3903 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08003904 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003905 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08003906 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003907 mN.ledARGB = argb;
3908 mN.ledOnMS = onMs;
3909 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003910 if (onMs != 0 || offMs != 0) {
3911 mN.flags |= FLAG_SHOW_LIGHTS;
3912 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003913 return this;
3914 }
3915
Joe Onoratocb109a02011-01-18 17:57:41 -08003916 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003917 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08003918 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003919
3920 * Ongoing notifications cannot be dismissed by the user, so your application or service
3921 * must take care of canceling them.
3922 *
3923
3924 * They are typically used to indicate a background task that the user is actively engaged
3925 * with (e.g., playing music) or is pending in some way and therefore occupying the device
3926 * (e.g., a file download, sync operation, active network connection).
3927 *
3928
3929 * @see Notification#FLAG_ONGOING_EVENT
Joe Onoratocb109a02011-01-18 17:57:41 -08003930 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003931 public Builder setOngoing(boolean ongoing) {
3932 setFlag(FLAG_ONGOING_EVENT, ongoing);
3933 return this;
3934 }
3935
Joe Onoratocb109a02011-01-18 17:57:41 -08003936 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08003937 * Set whether this notification should be colorized. When set, the color set with
3938 * {@link #setColor(int)} will be used as the background color of this notification.
3939 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08003940 * This should only be used for high priority ongoing tasks like navigation, an ongoing
3941 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08003942 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07003943 * For most styles, the coloring will only be applied if the notification is for a
3944 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08003945 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07003946 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08003947 *
Aurimas Liutikas7f695332018-05-31 21:07:32 -07003948 * @see #setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08003949 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08003950 */
3951 public Builder setColorized(boolean colorize) {
3952 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
3953 return this;
3954 }
3955
3956 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003957 * Set this flag if you would only like the sound, vibrate
3958 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003959 *
3960 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08003961 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003962 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
3963 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
3964 return this;
3965 }
3966
Joe Onoratocb109a02011-01-18 17:57:41 -08003967 /**
Julia Reynolds04499532016-09-13 14:04:53 -04003968 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003969 *
3970 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08003971 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003972 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08003973 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08003974 return this;
3975 }
3976
Joe Onoratocb109a02011-01-18 17:57:41 -08003977 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08003978 * Set whether or not this notification should not bridge to other devices.
3979 *
3980 * <p>Some notifications can be bridged to other devices for remote display.
3981 * This hint can be set to recommend this notification not be bridged.
3982 */
3983 public Builder setLocalOnly(boolean localOnly) {
3984 setFlag(FLAG_LOCAL_ONLY, localOnly);
3985 return this;
3986 }
3987
3988 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003989 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08003990 * <p>
3991 * The value should be one or more of the following fields combined with
3992 * bitwise-or:
3993 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
3994 * <p>
3995 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003996 *
3997 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05003998 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003999 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08004000 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004001 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004002 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004003 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08004004 return this;
4005 }
4006
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004007 /**
4008 * Set the priority of this notification.
4009 *
4010 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004011 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004012 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004013 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07004014 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004015 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004016 return this;
4017 }
Joe Malin8d40d042012-11-05 11:36:40 -08004018
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004019 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04004020 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08004021 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04004022 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004023 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04004024 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004025 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004026 return this;
4027 }
4028
4029 /**
Chris Wrendde75302014-03-26 17:24:15 -04004030 * Add a person that is relevant to this notification.
4031 *
Chris Wrene6c48932014-09-29 17:19:27 -04004032 * <P>
4033 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04004034 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4035 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4036 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04004037 * </P>
4038 *
4039 * <P>
4040 * The person should be specified by the {@code String} representation of a
4041 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
4042 * </P>
4043 *
4044 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
4045 * URIs. The path part of these URIs must exist in the contacts database, in the
4046 * appropriate column, or the reference will be discarded as invalid. Telephone schema
4047 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
Selim Cineke7238dd2017-12-14 17:48:32 -08004048 * It is also possible to provide a URI with the schema {@code name:} in order to uniquely
4049 * identify a person without an entry in the contacts database.
Chris Wrene6c48932014-09-29 17:19:27 -04004050 * </P>
4051 *
4052 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04004053 * @see Notification#EXTRA_PEOPLE
Selim Cineke7238dd2017-12-14 17:48:32 -08004054 * @deprecated use {@link #addPerson(Person)}
Chris Wrendde75302014-03-26 17:24:15 -04004055 */
Chris Wrene6c48932014-09-29 17:19:27 -04004056 public Builder addPerson(String uri) {
Selim Cinek9acd6732018-03-23 16:39:02 -07004057 addPerson(new Person.Builder().setUri(uri).build());
Selim Cineke7238dd2017-12-14 17:48:32 -08004058 return this;
4059 }
4060
4061 /**
4062 * Add a person that is relevant to this notification.
4063 *
4064 * <P>
4065 * Depending on user preferences, this annotation may allow the notification to pass
4066 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4067 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4068 * appear more prominently in the user interface.
4069 * </P>
4070 *
4071 * <P>
4072 * A person should usually contain a uri in order to benefit from the ranking boost.
4073 * However, even if no uri is provided, it's beneficial to provide other people in the
4074 * notification, such that listeners and voice only devices can announce and handle them
4075 * properly.
4076 * </P>
4077 *
4078 * @param person the person to add.
4079 * @see Notification#EXTRA_PEOPLE_LIST
4080 */
4081 public Builder addPerson(Person person) {
4082 mPersonList.add(person);
Chris Wrendde75302014-03-26 17:24:15 -04004083 return this;
4084 }
4085
4086 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004087 * Set this notification to be part of a group of notifications sharing the same key.
4088 * Grouped notifications may display in a cluster or stack on devices which
4089 * support such rendering.
4090 *
4091 * <p>To make this notification the summary for its group, also call
4092 * {@link #setGroupSummary}. A sort order can be specified for group members by using
4093 * {@link #setSortKey}.
4094 * @param groupKey The group key of the group.
4095 * @return this object for method chaining
4096 */
4097 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004098 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004099 return this;
4100 }
4101
4102 /**
4103 * Set this notification to be the group summary for a group of notifications.
4104 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04004105 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
4106 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004107 * @param isGroupSummary Whether this notification should be a group summary.
4108 * @return this object for method chaining
4109 */
4110 public Builder setGroupSummary(boolean isGroupSummary) {
4111 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
4112 return this;
4113 }
4114
4115 /**
4116 * Set a sort key that orders this notification among other notifications from the
4117 * same package. This can be useful if an external sort was already applied and an app
4118 * would like to preserve this. Notifications will be sorted lexicographically using this
4119 * value, although providing different priorities in addition to providing sort key may
4120 * cause this value to be ignored.
4121 *
4122 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07004123 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004124 *
4125 * @see String#compareTo(String)
4126 */
4127 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004128 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004129 return this;
4130 }
4131
4132 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004133 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004134 *
Griff Hazen720042b2014-02-24 15:46:56 -08004135 * <p>Values within the Bundle will replace existing extras values in this Builder.
4136 *
4137 * @see Notification#extras
4138 */
Griff Hazen959591e2014-05-15 22:26:18 -07004139 public Builder addExtras(Bundle extras) {
4140 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004141 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08004142 }
4143 return this;
4144 }
4145
4146 /**
4147 * Set metadata for this notification.
4148 *
4149 * <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 -04004150 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004151 * called.
4152 *
Griff Hazen720042b2014-02-24 15:46:56 -08004153 * <p>Replaces any existing extras values with those from the provided Bundle.
4154 * Use {@link #addExtras} to merge in metadata instead.
4155 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004156 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004157 */
Griff Hazen959591e2014-05-15 22:26:18 -07004158 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004159 if (extras != null) {
4160 mUserExtras = extras;
4161 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004162 return this;
4163 }
4164
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004165 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004166 * Get the current metadata Bundle used by this notification Builder.
4167 *
4168 * <p>The returned Bundle is shared with this Builder.
4169 *
4170 * <p>The current contents of this Bundle are copied into the Notification each time
4171 * {@link #build()} is called.
4172 *
4173 * @see Notification#extras
4174 */
4175 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004176 return mUserExtras;
4177 }
4178
4179 private Bundle getAllExtras() {
4180 final Bundle saveExtras = (Bundle) mUserExtras.clone();
4181 saveExtras.putAll(mN.extras);
4182 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08004183 }
4184
4185 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004186 * Add an action to this notification. Actions are typically displayed by
4187 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004188 * <p>
4189 * Every action must have an icon (32dp square and matching the
4190 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4191 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4192 * <p>
4193 * A notification in its expanded form can display up to 3 actions, from left to right in
4194 * the order they were added. Actions will not be displayed when the notification is
4195 * collapsed, however, so be sure that any essential functions may be accessed by the user
4196 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004197 *
4198 * @param icon Resource ID of a drawable that represents the action.
4199 * @param title Text describing the action.
4200 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04004201 *
4202 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004203 */
Dan Sandler86647982015-05-13 23:41:13 -04004204 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004205 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004206 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004207 return this;
4208 }
4209
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004210 /**
Griff Hazen959591e2014-05-15 22:26:18 -07004211 * Add an action to this notification. Actions are typically displayed by
4212 * the system as a button adjacent to the notification content.
4213 * <p>
4214 * Every action must have an icon (32dp square and matching the
4215 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4216 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4217 * <p>
4218 * A notification in its expanded form can display up to 3 actions, from left to right in
4219 * the order they were added. Actions will not be displayed when the notification is
4220 * collapsed, however, so be sure that any essential functions may be accessed by the user
4221 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
4222 *
4223 * @param action The action to add.
4224 */
4225 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08004226 if (action != null) {
4227 mActions.add(action);
4228 }
Griff Hazen959591e2014-05-15 22:26:18 -07004229 return this;
4230 }
4231
4232 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004233 * Alter the complete list of actions attached to this notification.
4234 * @see #addAction(Action).
4235 *
4236 * @param actions
4237 * @return
4238 */
4239 public Builder setActions(Action... actions) {
4240 mActions.clear();
4241 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08004242 if (actions[i] != null) {
4243 mActions.add(actions[i]);
4244 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004245 }
4246 return this;
4247 }
4248
4249 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004250 * Add a rich notification style to be applied at build time.
4251 *
4252 * @param style Object responsible for modifying the notification style.
4253 */
4254 public Builder setStyle(Style style) {
4255 if (mStyle != style) {
4256 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004257 if (mStyle != null) {
4258 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004259 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
4260 } else {
4261 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004262 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004263 }
4264 return this;
4265 }
4266
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004267 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05004268 * Returns the style set by {@link #setStyle(Style)}.
4269 */
4270 public Style getStyle() {
4271 return mStyle;
4272 }
4273
4274 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004275 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07004276 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004277 * @return The same Builder.
4278 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06004279 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004280 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004281 return this;
4282 }
4283
4284 /**
4285 * Supply a replacement Notification whose contents should be shown in insecure contexts
4286 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
4287 * @param n A replacement notification, presumably with some or all info redacted.
4288 * @return The same Builder.
4289 */
4290 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004291 if (n != null) {
4292 mN.publicVersion = new Notification();
4293 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
4294 } else {
4295 mN.publicVersion = null;
4296 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004297 return this;
4298 }
4299
Griff Hazenb720abe2014-05-20 13:15:30 -07004300 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004301 * Apply an extender to this notification builder. Extenders may be used to add
4302 * metadata or change options on this builder.
4303 */
Griff Hazen61a9e862014-05-22 16:05:19 -07004304 public Builder extend(Extender extender) {
4305 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004306 return this;
4307 }
4308
Dan Sandler4e787062015-06-17 15:09:48 -04004309 /**
4310 * @hide
4311 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04004312 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08004313 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004314 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004315 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004316 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004317 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04004318 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08004319 }
4320
Dan Sandler26e81cf2014-05-06 10:01:27 -04004321 /**
4322 * Sets {@link Notification#color}.
4323 *
4324 * @param argb The accent color to use
4325 *
4326 * @return The same Builder.
4327 */
Tor Norbye80756e32015-03-02 09:39:27 -08004328 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004329 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004330 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04004331 return this;
4332 }
4333
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004334 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04004335 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
4336 // This user can never be a badged profile,
4337 // and also includes USER_ALL system notifications.
4338 return null;
4339 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02004340 // Note: This assumes that the current user can read the profile badge of the
4341 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08004342 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05004343 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004344 }
4345
4346 private Bitmap getProfileBadge() {
4347 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01004348 if (badge == null) {
4349 return null;
4350 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004351 final int size = mContext.getResources().getDimensionPixelSize(
4352 R.dimen.notification_badge_size);
4353 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004354 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004355 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004356 badge.draw(canvas);
4357 return bitmap;
4358 }
4359
Selim Cinekc848c3a2016-01-13 15:27:30 -08004360 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01004361 Bitmap profileBadge = getProfileBadge();
4362
Kenny Guy98193ea2014-07-24 19:54:37 +01004363 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004364 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
4365 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004366 if (isColorized()) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004367 contentView.setDrawableTint(R.id.profile_badge, false,
4368 getPrimaryTextColor(), PorterDuff.Mode.SRC_ATOP);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004369 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004370 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004371 }
4372
Julia Reynoldsfc640012018-02-21 12:25:27 -05004373 /**
4374 * @hide
4375 */
4376 public boolean usesStandardHeader() {
4377 if (mN.mUsesStandardHeader) {
4378 return true;
4379 }
4380 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.N) {
4381 if (mN.contentView == null && mN.bigContentView == null) {
4382 return true;
4383 }
4384 }
4385 boolean contentViewUsesHeader = mN.contentView == null
4386 || STANDARD_LAYOUTS.contains(mN.contentView.getLayoutId());
4387 boolean bigContentViewUsesHeader = mN.bigContentView == null
4388 || STANDARD_LAYOUTS.contains(mN.bigContentView.getLayoutId());
4389 return contentViewUsesHeader && bigContentViewUsesHeader;
4390 }
4391
Christoph Studerfe718432014-09-01 18:21:18 +02004392 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004393 resetNotificationHeader(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02004394 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08004395 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004396 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08004397 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004398 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08004399 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08004400 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004401 }
4402
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004403 /**
4404 * Resets the notification header to its original state
4405 */
4406 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07004407 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
4408 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08004409 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004410 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02004411 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004412 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07004413 contentView.setTextViewText(R.id.header_text, null);
Selim Cinekafeed292017-12-12 17:32:44 -08004414 contentView.setViewVisibility(R.id.header_text_secondary, View.GONE);
4415 contentView.setTextViewText(R.id.header_text_secondary, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004416 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinekafeed292017-12-12 17:32:44 -08004417 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004418 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004419 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004420 contentView.setImageViewIcon(R.id.profile_badge, null);
4421 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004422 mN.mUsesStandardHeader = false;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004423 }
4424
Selim Cinek384804b2018-04-18 14:31:07 +08004425 private RemoteViews applyStandardTemplate(int resId, TemplateBindResult result) {
4426 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this),
4427 result);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004428 }
4429
4430 /**
4431 * @param hasProgress whether the progress bar should be shown and set
Selim Cinek384804b2018-04-18 14:31:07 +08004432 * @param result
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004433 */
Selim Cinek384804b2018-04-18 14:31:07 +08004434 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress,
4435 TemplateBindResult result) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004436 return applyStandardTemplate(resId, mParams.reset().hasProgress(hasProgress)
Selim Cinek384804b2018-04-18 14:31:07 +08004437 .fillTextsFrom(this), result);
Adrian Roosc1a80b02016-04-05 14:54:55 -07004438 }
4439
Selim Cinek384804b2018-04-18 14:31:07 +08004440 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p,
4441 TemplateBindResult result) {
Kenny Guy77320062014-08-27 21:37:15 +01004442 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04004443
Christoph Studerfe718432014-09-01 18:21:18 +02004444 resetStandardTemplate(contentView);
4445
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004446 final Bundle ex = mN.extras;
Selim Cinek7b9605b2017-01-19 17:36:00 -08004447 updateBackgroundColor(contentView);
Selim Cinekafeed292017-12-12 17:32:44 -08004448 bindNotificationHeader(contentView, p.ambient, p.headerTextSecondary);
Selim Cinek384804b2018-04-18 14:31:07 +08004449 bindLargeIconAndReply(contentView, p, result);
Adrian Roos70d7aa32017-01-11 15:39:06 -08004450 boolean showProgress = handleProgressBar(p.hasProgress, contentView, ex);
4451 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08004452 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004453 contentView.setTextViewText(R.id.title, processTextSpans(p.title));
Adrian Roos72171622017-01-27 10:32:06 -08004454 if (!p.ambient) {
4455 setTextViewColorPrimary(contentView, R.id.title);
4456 }
Selim Cinek954cc232016-05-20 13:29:23 -07004457 contentView.setViewLayoutWidth(R.id.title, showProgress
4458 ? ViewGroup.LayoutParams.WRAP_CONTENT
4459 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08004460 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08004461 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08004462 int textId = showProgress ? com.android.internal.R.id.text_line_1
4463 : com.android.internal.R.id.text;
Selim Cinek48f66b72017-08-18 16:17:51 -07004464 contentView.setTextViewText(textId, processTextSpans(p.text));
Adrian Roos72171622017-01-27 10:32:06 -08004465 if (!p.ambient) {
4466 setTextViewColorSecondary(contentView, textId);
4467 }
Selim Cinek41598732016-01-11 16:58:37 -08004468 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08004469 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08004470
Selim Cinek279fa862016-06-14 10:57:25 -07004471 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004472
Selim Cinek29603462015-11-17 19:04:39 -08004473 return contentView;
4474 }
4475
Selim Cinek48f66b72017-08-18 16:17:51 -07004476 private CharSequence processTextSpans(CharSequence text) {
Lucas Dupind3c99322018-09-27 10:22:29 -07004477 if (hasForegroundColor() || mInNightMode) {
Lucas Dupina291d192018-06-07 13:59:42 -07004478 return ContrastColorUtil.clearColorSpans(text);
Selim Cinek48f66b72017-08-18 16:17:51 -07004479 }
4480 return text;
4481 }
4482
Selim Cinek7b9605b2017-01-19 17:36:00 -08004483 private void setTextViewColorPrimary(RemoteViews contentView, int id) {
4484 ensureColors();
4485 contentView.setTextColor(id, mPrimaryTextColor);
4486 }
4487
Selim Cinek48f66b72017-08-18 16:17:51 -07004488 private boolean hasForegroundColor() {
4489 return mForegroundColor != COLOR_INVALID;
4490 }
4491
Selim Cinek389edcd2017-05-11 19:16:44 -07004492 /**
4493 * @return the primary text color
4494 * @hide
4495 */
4496 @VisibleForTesting
4497 public int getPrimaryTextColor() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004498 ensureColors();
4499 return mPrimaryTextColor;
4500 }
4501
Selim Cinek389edcd2017-05-11 19:16:44 -07004502 /**
4503 * @return the secondary text color
4504 * @hide
4505 */
4506 @VisibleForTesting
4507 public int getSecondaryTextColor() {
4508 ensureColors();
4509 return mSecondaryTextColor;
4510 }
4511
Selim Cinek7b9605b2017-01-19 17:36:00 -08004512 private void setTextViewColorSecondary(RemoteViews contentView, int id) {
4513 ensureColors();
4514 contentView.setTextColor(id, mSecondaryTextColor);
4515 }
4516
4517 private void ensureColors() {
4518 int backgroundColor = getBackgroundColor();
4519 if (mPrimaryTextColor == COLOR_INVALID
4520 || mSecondaryTextColor == COLOR_INVALID
Selim Cinek7b9605b2017-01-19 17:36:00 -08004521 || mTextColorsAreForBackground != backgroundColor) {
4522 mTextColorsAreForBackground = backgroundColor;
Selim Cinek48f66b72017-08-18 16:17:51 -07004523 if (!hasForegroundColor() || !isColorized()) {
Lucas Dupina291d192018-06-07 13:59:42 -07004524 mPrimaryTextColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004525 backgroundColor, mInNightMode);
Lucas Dupina291d192018-06-07 13:59:42 -07004526 mSecondaryTextColor = ContrastColorUtil.resolveSecondaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004527 backgroundColor, mInNightMode);
Selim Cinekc7f5a822018-03-20 19:32:06 -07004528 if (backgroundColor != COLOR_DEFAULT && isColorized()) {
Lucas Dupina291d192018-06-07 13:59:42 -07004529 mPrimaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004530 mPrimaryTextColor, backgroundColor, 4.5);
Lucas Dupina291d192018-06-07 13:59:42 -07004531 mSecondaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004532 mSecondaryTextColor, backgroundColor, 4.5);
4533 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07004534 } else {
Lucas Dupina291d192018-06-07 13:59:42 -07004535 double backLum = ContrastColorUtil.calculateLuminance(backgroundColor);
4536 double textLum = ContrastColorUtil.calculateLuminance(mForegroundColor);
4537 double contrast = ContrastColorUtil.calculateContrast(mForegroundColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004538 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07004539 // We only respect the given colors if worst case Black or White still has
4540 // contrast
4541 boolean backgroundLight = backLum > textLum
4542 && satisfiesTextContrast(backgroundColor, Color.BLACK)
4543 || backLum <= textLum
4544 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004545 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07004546 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004547 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004548 mForegroundColor,
4549 backgroundColor,
4550 true /* findFG */,
4551 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004552 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004553 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004554 } else {
4555 mSecondaryTextColor =
Lucas Dupina291d192018-06-07 13:59:42 -07004556 ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004557 mForegroundColor,
4558 backgroundColor,
4559 true /* findFG */,
4560 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004561 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004562 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004563 }
4564 } else {
4565 mPrimaryTextColor = mForegroundColor;
Lucas Dupina291d192018-06-07 13:59:42 -07004566 mSecondaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004567 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4568 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Lucas Dupina291d192018-06-07 13:59:42 -07004569 if (ContrastColorUtil.calculateContrast(mSecondaryTextColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004570 backgroundColor) < 4.5f) {
4571 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07004572 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004573 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004574 mSecondaryTextColor,
4575 backgroundColor,
4576 true /* findFG */,
4577 4.5f);
4578 } else {
4579 mSecondaryTextColor
Lucas Dupina291d192018-06-07 13:59:42 -07004580 = ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004581 mSecondaryTextColor,
4582 backgroundColor,
4583 true /* findFG */,
4584 4.5f);
4585 }
Lucas Dupina291d192018-06-07 13:59:42 -07004586 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004587 mSecondaryTextColor, backgroundLight
4588 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4589 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004590 }
4591 }
4592 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004593 }
4594 }
4595
4596 private void updateBackgroundColor(RemoteViews contentView) {
4597 if (isColorized()) {
4598 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
4599 getBackgroundColor());
4600 } else {
4601 // Clear it!
4602 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4603 0);
4604 }
4605 }
4606
Selim Cinek860b6da2015-12-16 19:02:19 -08004607 /**
4608 * @param remoteView the remote view to update the minheight in
4609 * @param hasMinHeight does it have a mimHeight
4610 * @hide
4611 */
4612 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4613 int minHeight = 0;
4614 if (hasMinHeight) {
4615 // we need to set the minHeight of the notification
4616 minHeight = mContext.getResources().getDimensionPixelSize(
4617 com.android.internal.R.dimen.notification_min_content_height);
4618 }
4619 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4620 }
4621
Selim Cinek29603462015-11-17 19:04:39 -08004622 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004623 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4624 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4625 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4626 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004627 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004628 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004629 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004630 contentView.setProgressBackgroundTintList(
4631 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4632 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08004633 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08004634 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004635 contentView.setProgressTintList(R.id.progress, colorStateList);
4636 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004637 }
Selim Cinek29603462015-11-17 19:04:39 -08004638 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004639 } else {
4640 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004641 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004642 }
Joe Onorato561d3852010-11-20 18:09:34 -08004643 }
4644
Selim Cinek384804b2018-04-18 14:31:07 +08004645 private void bindLargeIconAndReply(RemoteViews contentView, StandardTemplateParams p,
4646 TemplateBindResult result) {
4647 boolean largeIconShown = bindLargeIcon(contentView, p.hideLargeIcon || p.ambient);
Selim Cinek1c72fa02018-04-23 18:00:54 +08004648 boolean replyIconShown = bindReplyIcon(contentView, p.hideReplyIcon || p.ambient);
Selim Cinek384804b2018-04-18 14:31:07 +08004649 contentView.setViewVisibility(R.id.right_icon_container,
Selim Cinek1c72fa02018-04-23 18:00:54 +08004650 largeIconShown || replyIconShown ? View.VISIBLE : View.GONE);
4651 int marginEnd = calculateMarginEnd(largeIconShown, replyIconShown);
4652 contentView.setViewLayoutMarginEnd(R.id.line1, marginEnd);
4653 contentView.setViewLayoutMarginEnd(R.id.text, marginEnd);
4654 contentView.setViewLayoutMarginEnd(R.id.progress, marginEnd);
Selim Cinek384804b2018-04-18 14:31:07 +08004655 if (result != null) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08004656 result.setIconMarginEnd(marginEnd);
Selim Cinek384804b2018-04-18 14:31:07 +08004657 }
4658 }
4659
Selim Cinek1c72fa02018-04-23 18:00:54 +08004660 private int calculateMarginEnd(boolean largeIconShown, boolean replyIconShown) {
4661 int marginEnd = 0;
4662 int contentMargin = mContext.getResources().getDimensionPixelSize(
4663 R.dimen.notification_content_margin_end);
4664 int iconSize = mContext.getResources().getDimensionPixelSize(
4665 R.dimen.notification_right_icon_size);
4666 if (replyIconShown) {
4667 // The size of the reply icon
4668 marginEnd += iconSize;
4669
4670 int replyInset = mContext.getResources().getDimensionPixelSize(
4671 R.dimen.notification_reply_inset);
4672 // We're subtracting the inset of the reply icon to make sure it's
4673 // aligned nicely on the right, and remove it from the following padding
4674 marginEnd -= replyInset * 2;
4675 }
4676 if (largeIconShown) {
4677 // adding size of the right icon
4678 marginEnd += iconSize;
4679
4680 if (replyIconShown) {
4681 // We also add some padding to the reply icon if it's around
4682 marginEnd += contentMargin;
4683 }
4684 }
4685 if (replyIconShown || largeIconShown) {
4686 // The padding to the content
4687 marginEnd += contentMargin;
4688 }
4689 return marginEnd;
4690 }
4691
Selim Cinek384804b2018-04-18 14:31:07 +08004692 /**
4693 * Bind the large icon.
4694 * @return if the largeIcon is visible
4695 */
4696 private boolean bindLargeIcon(RemoteViews contentView, boolean hideLargeIcon) {
Selim Cinek279fa862016-06-14 10:57:25 -07004697 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4698 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4699 }
Selim Cinek88188f22017-09-19 16:46:56 -07004700 boolean showLargeIcon = mN.mLargeIcon != null && !hideLargeIcon;
4701 if (showLargeIcon) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004702 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4703 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
4704 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Selim Cinek88188f22017-09-19 16:46:56 -07004705 }
Selim Cinek384804b2018-04-18 14:31:07 +08004706 return showLargeIcon;
4707 }
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004708
Selim Cinek384804b2018-04-18 14:31:07 +08004709 /**
4710 * Bind the reply icon.
4711 * @return if the reply icon is visible
4712 */
4713 private boolean bindReplyIcon(RemoteViews contentView, boolean hideReplyIcon) {
4714 boolean actionVisible = !hideReplyIcon;
4715 Action action = null;
Selim Cinek88188f22017-09-19 16:46:56 -07004716 if (actionVisible) {
Selim Cinek384804b2018-04-18 14:31:07 +08004717 action = findReplyAction();
4718 actionVisible = action != null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004719 }
Selim Cinek384804b2018-04-18 14:31:07 +08004720 if (actionVisible) {
4721 contentView.setViewVisibility(R.id.reply_icon_action, View.VISIBLE);
4722 contentView.setDrawableTint(R.id.reply_icon_action,
4723 false /* targetBackground */,
Selim Cinek4717d862018-04-19 09:19:15 +08004724 getNeutralColor(),
Selim Cinek384804b2018-04-18 14:31:07 +08004725 PorterDuff.Mode.SRC_ATOP);
4726 contentView.setOnClickPendingIntent(R.id.reply_icon_action, action.actionIntent);
4727 contentView.setRemoteInputs(R.id.reply_icon_action, action.mRemoteInputs);
4728 } else {
4729 contentView.setRemoteInputs(R.id.reply_icon_action, null);
4730 }
4731 contentView.setViewVisibility(R.id.reply_icon_action,
4732 actionVisible ? View.VISIBLE : View.GONE);
4733 return actionVisible;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004734 }
4735
4736 private Action findReplyAction() {
4737 ArrayList<Action> actions = mActions;
4738 if (mOriginalActions != null) {
4739 actions = mOriginalActions;
4740 }
4741 int numActions = actions.size();
4742 for (int i = 0; i < numActions; i++) {
4743 Action action = actions.get(i);
4744 if (hasValidRemoteInput(action)) {
4745 return action;
4746 }
4747 }
4748 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004749 }
4750
Selim Cinekafeed292017-12-12 17:32:44 -08004751 private void bindNotificationHeader(RemoteViews contentView, boolean ambient,
4752 CharSequence secondaryHeaderText) {
Adrian Roos487374f2017-01-11 15:48:14 -08004753 bindSmallIcon(contentView, ambient);
4754 bindHeaderAppName(contentView, ambient);
4755 if (!ambient) {
4756 // Ambient view does not have these
4757 bindHeaderText(contentView);
Selim Cinekafeed292017-12-12 17:32:44 -08004758 bindHeaderTextSecondary(contentView, secondaryHeaderText);
Adrian Roos487374f2017-01-11 15:48:14 -08004759 bindHeaderChronometerAndTime(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08004760 bindProfileBadge(contentView);
4761 }
Julia Reynolds6013e5c2018-04-18 08:43:34 -04004762 bindActivePermissions(contentView, ambient);
Adrian Roosd83e9992017-03-16 15:17:57 -07004763 bindExpandButton(contentView);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004764 mN.mUsesStandardHeader = true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004765 }
4766
Julia Reynolds6013e5c2018-04-18 08:43:34 -04004767 private void bindActivePermissions(RemoteViews contentView, boolean ambient) {
Selim Cinek4717d862018-04-19 09:19:15 +08004768 int color = ambient ? resolveAmbientColor() : getNeutralColor();
Julia Reynoldsb887b232018-04-10 16:38:08 -04004769 contentView.setDrawableTint(R.id.camera, false, color, PorterDuff.Mode.SRC_ATOP);
4770 contentView.setDrawableTint(R.id.mic, false, color, PorterDuff.Mode.SRC_ATOP);
4771 contentView.setDrawableTint(R.id.overlay, false, color, PorterDuff.Mode.SRC_ATOP);
4772 }
4773
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004774 private void bindExpandButton(RemoteViews contentView) {
Selim Cinekc7f5a822018-03-20 19:32:06 -07004775 int color = isColorized() ? getPrimaryTextColor() : getSecondaryTextColor();
Sunny Goyal5b153922017-09-21 21:00:36 -07004776 contentView.setDrawableTint(R.id.expand_button, false, color,
4777 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08004778 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08004779 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004780 }
4781
4782 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
4783 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08004784 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004785 setTextViewColorSecondary(contentView, R.id.time_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004786 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
4787 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
4788 contentView.setLong(R.id.chronometer, "setBase",
4789 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
4790 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07004791 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07004792 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004793 setTextViewColorSecondary(contentView, R.id.chronometer);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004794 } else {
4795 contentView.setViewVisibility(R.id.time, View.VISIBLE);
4796 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004797 setTextViewColorSecondary(contentView, R.id.time);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004798 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004799 } else {
4800 // We still want a time to be set but gone, such that we can show and hide it
4801 // on demand in case it's a child notification without anything in the header
4802 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004803 }
4804 }
4805
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004806 private void bindHeaderText(RemoteViews contentView) {
4807 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4808 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05004809 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004810 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05004811 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004812 if (headerText == null
4813 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4814 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
4815 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
4816 }
4817 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004818 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek48f66b72017-08-18 16:17:51 -07004819 contentView.setTextViewText(R.id.header_text, processTextSpans(
4820 processLegacyText(headerText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004821 setTextViewColorSecondary(contentView, R.id.header_text);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004822 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
4823 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004824 setTextViewColorSecondary(contentView, R.id.header_text_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004825 }
4826 }
4827
Selim Cinekafeed292017-12-12 17:32:44 -08004828 private void bindHeaderTextSecondary(RemoteViews contentView, CharSequence secondaryText) {
4829 if (!TextUtils.isEmpty(secondaryText)) {
4830 contentView.setTextViewText(R.id.header_text_secondary, processTextSpans(
4831 processLegacyText(secondaryText)));
4832 setTextViewColorSecondary(contentView, R.id.header_text_secondary);
4833 contentView.setViewVisibility(R.id.header_text_secondary, View.VISIBLE);
4834 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.VISIBLE);
4835 setTextViewColorSecondary(contentView, R.id.header_text_secondary_divider);
4836 }
4837 }
4838
Adrian Rooseba05822016-04-22 17:09:27 -07004839 /**
4840 * @hide
4841 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01004842 @UnsupportedAppUsage
Adrian Rooseba05822016-04-22 17:09:27 -07004843 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004844 CharSequence name = null;
4845 final PackageManager pm = mContext.getPackageManager();
4846 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
4847 // only system packages which lump together a bunch of unrelated stuff
4848 // may substitute a different name to make the purpose of the
4849 // notification more clear. the correct package label should always
4850 // be accessible via SystemUI.
4851 final String pkg = mContext.getPackageName();
4852 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
4853 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
4854 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
4855 name = subName;
4856 } else {
4857 Log.w(TAG, "warning: pkg "
4858 + pkg + " attempting to substitute app name '" + subName
4859 + "' without holding perm "
4860 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
4861 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004862 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04004863 if (TextUtils.isEmpty(name)) {
4864 name = pm.getApplicationLabel(mContext.getApplicationInfo());
4865 }
4866 if (TextUtils.isEmpty(name)) {
4867 // still nothing?
4868 return null;
4869 }
4870
4871 return String.valueOf(name);
4872 }
Adrian Roos487374f2017-01-11 15:48:14 -08004873 private void bindHeaderAppName(RemoteViews contentView, boolean ambient) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004874 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos72171622017-01-27 10:32:06 -08004875 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004876 setTextViewColorPrimary(contentView, R.id.app_name_text);
4877 } else {
4878 contentView.setTextColor(R.id.app_name_text,
Selim Cinekc7f5a822018-03-20 19:32:06 -07004879 ambient ? resolveAmbientColor() : getSecondaryTextColor());
Selim Cinek7b9605b2017-01-19 17:36:00 -08004880 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004881 }
4882
Adrian Roos487374f2017-01-11 15:48:14 -08004883 private void bindSmallIcon(RemoteViews contentView, boolean ambient) {
Selim Cinek279fa862016-06-14 10:57:25 -07004884 if (mN.mSmallIcon == null && mN.icon != 0) {
4885 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
4886 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004887 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Sunny Goyal5b153922017-09-21 21:00:36 -07004888 contentView.setInt(R.id.icon, "setImageLevel", mN.iconLevel);
Adrian Roos487374f2017-01-11 15:48:14 -08004889 processSmallIconColor(mN.mSmallIcon, contentView, ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004890 }
4891
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004892 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004893 * @return true if the built notification will show the time or the chronometer; false
4894 * otherwise
4895 */
4896 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07004897 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004898 }
4899
Christoph Studerfe718432014-09-01 18:21:18 +02004900 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004901 // actions_container is only reset when there are no actions to avoid focus issues with
4902 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02004903 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004904 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08004905
4906 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
4907 big.setTextViewText(R.id.notification_material_reply_text_1, null);
Kenny Guya0f6de82018-04-06 16:20:16 +01004908 big.setViewVisibility(R.id.notification_material_reply_text_1_container, View.GONE);
4909 big.setViewVisibility(R.id.notification_material_reply_progress, View.GONE);
Adrian Roose458aa82015-12-08 16:17:19 -08004910
4911 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
4912 big.setTextViewText(R.id.notification_material_reply_text_2, null);
4913 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
4914 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07004915
Selim Cineked64a142018-02-06 18:06:01 -08004916 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
4917 R.dimen.notification_content_margin);
Christoph Studerfe718432014-09-01 18:21:18 +02004918 }
4919
Selim Cinek384804b2018-04-18 14:31:07 +08004920 private RemoteViews applyStandardTemplateWithActions(int layoutId,
4921 TemplateBindResult result) {
4922 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this),
4923 result);
Adrian Roos48d746a2016-04-12 14:57:28 -07004924 }
4925
Adrian Roos70d7aa32017-01-11 15:39:06 -08004926 private RemoteViews applyStandardTemplateWithActions(int layoutId,
Selim Cinek384804b2018-04-18 14:31:07 +08004927 StandardTemplateParams p, TemplateBindResult result) {
4928 RemoteViews big = applyStandardTemplate(layoutId, p, result);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004929
Christoph Studerfe718432014-09-01 18:21:18 +02004930 resetStandardTemplateWithActions(big);
4931
Adrian Roose458aa82015-12-08 16:17:19 -08004932 boolean validRemoteInput = false;
4933
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004934 int N = mActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08004935 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004936 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004937 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08004938 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004939 big.setViewVisibility(R.id.actions, View.VISIBLE);
Selim Cineked64a142018-02-06 18:06:01 -08004940 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Daniel Sandler8680bf82012-05-15 16:52:52 -04004941 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004942 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08004943 Action action = mActions.get(i);
Selim Cinekffcc7cf2018-02-06 17:43:51 -08004944 boolean actionHasValidInput = hasValidRemoteInput(action);
4945 validRemoteInput |= actionHasValidInput;
Adrian Roose458aa82015-12-08 16:17:19 -08004946
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004947 final RemoteViews button = generateActionButton(action, emphazisedMode,
Selim Cinek396caca2018-04-10 17:46:46 -07004948 p.ambient);
4949 if (actionHasValidInput && !emphazisedMode) {
Selim Cinekffcc7cf2018-02-06 17:43:51 -08004950 // Clear the drawable
4951 button.setInt(R.id.action0, "setBackgroundResource", 0);
4952 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004953 big.addView(R.id.actions, button);
4954 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004955 } else {
4956 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004957 }
Adrian Roose458aa82015-12-08 16:17:19 -08004958
4959 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08004960 if (!p.ambient && validRemoteInput && replyText != null
Selim Cinekbee4e072018-05-21 22:06:43 -07004961 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])
4962 && p.maxRemoteInputHistory > 0) {
Kenny Guya0f6de82018-04-06 16:20:16 +01004963 boolean showSpinner = mN.extras.getBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER);
Adrian Roose458aa82015-12-08 16:17:19 -08004964 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Kenny Guya0f6de82018-04-06 16:20:16 +01004965 big.setViewVisibility(R.id.notification_material_reply_text_1_container,
4966 View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004967 big.setTextViewText(R.id.notification_material_reply_text_1,
4968 processTextSpans(replyText[0]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004969 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1);
Kenny Guya0f6de82018-04-06 16:20:16 +01004970 big.setViewVisibility(R.id.notification_material_reply_progress,
4971 showSpinner ? View.VISIBLE : View.GONE);
4972 big.setProgressIndeterminateTintList(
4973 R.id.notification_material_reply_progress,
4974 ColorStateList.valueOf(
4975 isColorized() ? getPrimaryTextColor() : resolveContrastColor()));
Adrian Roose458aa82015-12-08 16:17:19 -08004976
Selim Cinekbee4e072018-05-21 22:06:43 -07004977 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])
4978 && p.maxRemoteInputHistory > 1) {
Adrian Roose458aa82015-12-08 16:17:19 -08004979 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004980 big.setTextViewText(R.id.notification_material_reply_text_2,
4981 processTextSpans(replyText[1]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004982 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2);
Adrian Roose458aa82015-12-08 16:17:19 -08004983
Selim Cinekbee4e072018-05-21 22:06:43 -07004984 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])
4985 && p.maxRemoteInputHistory > 2) {
Adrian Roose458aa82015-12-08 16:17:19 -08004986 big.setViewVisibility(
4987 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004988 big.setTextViewText(R.id.notification_material_reply_text_3,
4989 processTextSpans(replyText[2]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004990 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3);
Adrian Roose458aa82015-12-08 16:17:19 -08004991 }
4992 }
4993 }
4994
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004995 return big;
4996 }
4997
Adrian Roose458aa82015-12-08 16:17:19 -08004998 private boolean hasValidRemoteInput(Action action) {
4999 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
5000 // Weird actions
5001 return false;
5002 }
5003
5004 RemoteInput[] remoteInputs = action.getRemoteInputs();
5005 if (remoteInputs == null) {
5006 return false;
5007 }
5008
5009 for (RemoteInput r : remoteInputs) {
5010 CharSequence[] choices = r.getChoices();
5011 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
5012 return true;
5013 }
5014 }
5015 return false;
5016 }
5017
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005018 /**
5019 * Construct a RemoteViews for the final 1U notification layout. In order:
5020 * 1. Custom contentView from the caller
5021 * 2. Style's proposed content view
5022 * 3. Standard template view
5023 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005024 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005025 return createContentView(false /* increasedheight */ );
5026 }
5027
5028 /**
5029 * Construct a RemoteViews for the smaller content view.
5030 *
5031 * @param increasedHeight true if this layout be created with an increased height. Some
5032 * styles may support showing more then just that basic 1U size
5033 * and the system may decide to render important notifications
5034 * slightly bigger even when collapsed.
5035 *
5036 * @hide
5037 */
5038 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005039 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005040 return mN.contentView;
5041 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005042 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005043 if (styleView != null) {
5044 return styleView;
5045 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005046 }
Selim Cinek384804b2018-04-18 14:31:07 +08005047 return applyStandardTemplate(getBaseLayoutResource(), null /* result */);
Joe Onorato46439ce2010-11-19 13:56:21 -08005048 }
5049
Selim Cineka7679b62017-05-10 16:33:25 -07005050 private boolean useExistingRemoteView() {
5051 return mStyle == null || (!mStyle.displayCustomViewInline()
5052 && !mRebuildStyledRemoteViews);
5053 }
5054
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005055 /**
5056 * Construct a RemoteViews for the final big notification layout.
5057 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005058 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05005059 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07005060 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005061 return mN.bigContentView;
5062 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05005063 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08005064 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005065 } else if (mActions.size() != 0) {
Selim Cinek384804b2018-04-18 14:31:07 +08005066 result = applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5067 null /* result */);
Selim Cinek850a8542015-11-11 11:48:36 -05005068 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08005069 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05005070 return result;
5071 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005072
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005073 /**
Selim Cinek414ad332017-02-24 19:06:12 -08005074 * Construct a RemoteViews for the final notification header only. This will not be
5075 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005076 *
Adrian Roos6f6e1592017-05-02 16:22:53 -07005077 * @param ambient if true, generate the header for the ambient display layout.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005078 * @hide
5079 */
Adrian Roos6f6e1592017-05-02 16:22:53 -07005080 public RemoteViews makeNotificationHeader(boolean ambient) {
Selim Cinek414ad332017-02-24 19:06:12 -08005081 Boolean colorized = (Boolean) mN.extras.get(EXTRA_COLORIZED);
5082 mN.extras.putBoolean(EXTRA_COLORIZED, false);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005083 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Adrian Roos6f6e1592017-05-02 16:22:53 -07005084 ambient ? R.layout.notification_template_ambient_header
5085 : R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005086 resetNotificationHeader(header);
Selim Cinekafeed292017-12-12 17:32:44 -08005087 bindNotificationHeader(header, ambient, null);
Selim Cinek414ad332017-02-24 19:06:12 -08005088 if (colorized != null) {
5089 mN.extras.putBoolean(EXTRA_COLORIZED, colorized);
5090 } else {
5091 mN.extras.remove(EXTRA_COLORIZED);
5092 }
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005093 return header;
5094 }
5095
Adrian Roos487374f2017-01-11 15:48:14 -08005096 /**
5097 * Construct a RemoteViews for the ambient version of the notification.
5098 *
5099 * @hide
5100 */
5101 public RemoteViews makeAmbientNotification() {
5102 RemoteViews ambient = applyStandardTemplateWithActions(
5103 R.layout.notification_template_material_ambient,
Selim Cinek384804b2018-04-18 14:31:07 +08005104 mParams.reset().ambient(true).fillTextsFrom(this).hasProgress(false),
5105 null /* result */);
Adrian Roos487374f2017-01-11 15:48:14 -08005106 return ambient;
5107 }
5108
Selim Cinek29603462015-11-17 19:04:39 -08005109 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005110 if (result != null) {
5111 result.setViewVisibility(R.id.text_line_1, View.GONE);
5112 }
Selim Cinek29603462015-11-17 19:04:39 -08005113 }
5114
Selim Cinek6743c0b2017-01-18 18:24:01 -08005115 /**
5116 * Adapt the Notification header if this view is used as an expanded view.
5117 *
5118 * @hide
5119 */
5120 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005121 if (result != null) {
5122 result.setBoolean(R.id.notification_header, "setExpanded", true);
5123 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005124 }
5125
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005126 /**
5127 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005128 *
5129 * @param increasedHeight true if this layout be created with an increased height. Some
5130 * styles may support showing more then just that basic 1U size
5131 * and the system may decide to render important notifications
5132 * slightly bigger even when collapsed.
5133 *
5134 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005135 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005136 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005137 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005138 return mN.headsUpContentView;
5139 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08005140 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
5141 if (styleView != null) {
5142 return styleView;
5143 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05005144 } else if (mActions.size() == 0) {
5145 return null;
5146 }
5147
Selim Cinekbee4e072018-05-21 22:06:43 -07005148 // We only want at most a single remote input history to be shown here, otherwise
5149 // the content would become squished.
5150 StandardTemplateParams p = mParams.reset().fillTextsFrom(this)
5151 .setMaxRemoteInputHistory(1);
5152 return applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5153 p,
5154 null /* result */);
Chris Wren8fd39ec2014-02-27 17:43:26 -05005155 }
5156
Selim Cinek624c02db2015-12-14 21:00:02 -08005157 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08005158 * Construct a RemoteViews for the final heads-up notification layout.
5159 */
5160 public RemoteViews createHeadsUpContentView() {
5161 return createHeadsUpContentView(false /* useIncreasedHeight */);
5162 }
5163
5164 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08005165 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5166 *
5167 * @hide
5168 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005169 @UnsupportedAppUsage
Selim Cinek624c02db2015-12-14 21:00:02 -08005170 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005171 return makePublicView(false /* ambient */);
5172 }
5173
5174 /**
5175 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5176 *
5177 * @hide
5178 */
5179 public RemoteViews makePublicAmbientNotification() {
5180 return makePublicView(true /* ambient */);
5181 }
5182
5183 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08005184 if (mN.publicVersion != null) {
5185 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005186 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08005187 }
5188 Bundle savedBundle = mN.extras;
5189 Style style = mStyle;
5190 mStyle = null;
5191 Icon largeIcon = mN.mLargeIcon;
5192 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07005193 Bitmap largeIconLegacy = mN.largeIcon;
5194 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005195 ArrayList<Action> actions = mActions;
5196 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08005197 Bundle publicExtras = new Bundle();
5198 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
5199 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
5200 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
5201 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07005202 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
5203 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cineked07b962018-04-30 14:39:35 -07005204 String appName = savedBundle.getString(EXTRA_SUBSTITUTE_APP_NAME);
5205 if (appName != null) {
5206 publicExtras.putString(EXTRA_SUBSTITUTE_APP_NAME, appName);
5207 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005208 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07005209 RemoteViews view;
5210 if (ambient) {
5211 publicExtras.putCharSequence(EXTRA_TITLE,
5212 mContext.getString(com.android.internal.R.string.notification_hidden_text));
5213 view = makeAmbientNotification();
5214 } else{
5215 view = makeNotificationHeader(false /* ambient */);
5216 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
5217 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005218 mN.extras = savedBundle;
5219 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07005220 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005221 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08005222 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005223 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08005224 }
5225
Selim Cinek6743c0b2017-01-18 18:24:01 -08005226 /**
5227 * Construct a content view for the display when low - priority
5228 *
5229 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
5230 * a new subtext is created consisting of the content of the
5231 * notification.
5232 * @hide
5233 */
5234 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
5235 int color = mN.color;
5236 mN.color = COLOR_DEFAULT;
5237 CharSequence summary = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
5238 if (!useRegularSubtext || TextUtils.isEmpty(summary)) {
5239 CharSequence newSummary = createSummaryText();
5240 if (!TextUtils.isEmpty(newSummary)) {
5241 mN.extras.putCharSequence(EXTRA_SUB_TEXT, newSummary);
5242 }
5243 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08005244
Adrian Roos6f6e1592017-05-02 16:22:53 -07005245 RemoteViews header = makeNotificationHeader(false /* ambient */);
Selim Cinek1b554392017-02-28 17:22:49 -08005246 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08005247 if (summary != null) {
5248 mN.extras.putCharSequence(EXTRA_SUB_TEXT, summary);
5249 } else {
5250 mN.extras.remove(EXTRA_SUB_TEXT);
5251 }
5252 mN.color = color;
5253 return header;
5254 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005255
Selim Cinek6743c0b2017-01-18 18:24:01 -08005256 private CharSequence createSummaryText() {
5257 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
5258 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
5259 return titleText;
5260 }
5261 SpannableStringBuilder summary = new SpannableStringBuilder();
5262 if (titleText == null) {
5263 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
5264 }
5265 BidiFormatter bidi = BidiFormatter.getInstance();
5266 if (titleText != null) {
5267 summary.append(bidi.unicodeWrap(titleText));
5268 }
5269 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
5270 if (titleText != null && contentText != null) {
5271 summary.append(bidi.unicodeWrap(mContext.getText(
5272 R.string.notification_header_divider_symbol_with_spaces)));
5273 }
5274 if (contentText != null) {
5275 summary.append(bidi.unicodeWrap(contentText));
5276 }
5277 return summary;
5278 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05005279
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005280 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Selim Cinek396caca2018-04-10 17:46:46 -07005281 boolean ambient) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04005282 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07005283 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005284 emphazisedMode ? getEmphasizedActionLayoutResource()
5285 : tombstone ? getActionTombstoneLayoutResource()
5286 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04005287 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04005288 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04005289 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005290 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005291 if (action.mRemoteInputs != null) {
5292 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
5293 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005294 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07005295 // change the background bgColor
Selim Cinek981962e2016-07-20 20:41:58 -07005296 CharSequence title = action.title;
5297 ColorStateList[] outResultColor = null;
Selim Cinek396caca2018-04-10 17:46:46 -07005298 int background = resolveBackgroundColor();
Selim Cinek981962e2016-07-20 20:41:58 -07005299 if (isLegacy()) {
Lucas Dupina291d192018-06-07 13:59:42 -07005300 title = ContrastColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07005301 } else {
5302 outResultColor = new ColorStateList[1];
Selim Cinek396caca2018-04-10 17:46:46 -07005303 title = ensureColorSpanContrast(title, background, outResultColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005304 }
Selim Cinek48f66b72017-08-18 16:17:51 -07005305 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005306 setTextViewColorPrimary(button, R.id.action0);
Selim Cinek396caca2018-04-10 17:46:46 -07005307 int rippleColor;
5308 boolean hasColorOverride = outResultColor != null && outResultColor[0] != null;
5309 if (hasColorOverride) {
5310 // There's a span spanning the full text, let's take it and use it as the
5311 // background color
5312 background = outResultColor[0].getDefaultColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005313 int textColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07005314 background, mInNightMode);
Selim Cinek396caca2018-04-10 17:46:46 -07005315 button.setTextColor(R.id.action0, textColor);
5316 rippleColor = textColor;
Anthony Chenad4d1582017-04-10 16:07:58 -07005317 } else if (mN.color != COLOR_DEFAULT && !isColorized() && mTintActionButtons) {
Selim Cinek396caca2018-04-10 17:46:46 -07005318 rippleColor = resolveContrastColor();
5319 button.setTextColor(R.id.action0, rippleColor);
5320 } else {
5321 rippleColor = getPrimaryTextColor();
Selim Cinek981962e2016-07-20 20:41:58 -07005322 }
Selim Cinek396caca2018-04-10 17:46:46 -07005323 // We only want about 20% alpha for the ripple
5324 rippleColor = (rippleColor & 0x00ffffff) | 0x33000000;
5325 button.setColorStateList(R.id.action0, "setRippleColor",
5326 ColorStateList.valueOf(rippleColor));
5327 button.setColorStateList(R.id.action0, "setButtonBackground",
5328 ColorStateList.valueOf(background));
5329 button.setBoolean(R.id.action0, "setHasStroke", !hasColorOverride);
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005330 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07005331 button.setTextViewText(R.id.action0, processTextSpans(
5332 processLegacyText(action.title)));
Adrian Roos72171622017-01-27 10:32:06 -08005333 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005334 setTextViewColorPrimary(button, R.id.action0);
Anthony Chenad4d1582017-04-10 16:07:58 -07005335 } else if (mN.color != COLOR_DEFAULT && mTintActionButtons) {
Adrian Roos487374f2017-01-11 15:48:14 -08005336 button.setTextColor(R.id.action0,
5337 ambient ? resolveAmbientColor() : resolveContrastColor());
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005338 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005339 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005340 return button;
5341 }
5342
Joe Onoratocb109a02011-01-18 17:57:41 -08005343 /**
Selim Cinek981962e2016-07-20 20:41:58 -07005344 * Ensures contrast on color spans against a background color. also returns the color of the
5345 * text if a span was found that spans over the whole text.
5346 *
5347 * @param charSequence the charSequence on which the spans are
5348 * @param background the background color to ensure the contrast against
5349 * @param outResultColor an array in which a color will be returned as the first element if
5350 * there exists a full length color span.
5351 * @return the contrasted charSequence
5352 */
5353 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
5354 ColorStateList[] outResultColor) {
5355 if (charSequence instanceof Spanned) {
5356 Spanned ss = (Spanned) charSequence;
5357 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
5358 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
5359 for (Object span : spans) {
5360 Object resultSpan = span;
5361 int spanStart = ss.getSpanStart(span);
5362 int spanEnd = ss.getSpanEnd(span);
5363 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
5364 if (resultSpan instanceof CharacterStyle) {
5365 resultSpan = ((CharacterStyle) span).getUnderlying();
5366 }
5367 if (resultSpan instanceof TextAppearanceSpan) {
5368 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
5369 ColorStateList textColor = originalSpan.getTextColor();
5370 if (textColor != null) {
5371 int[] colors = textColor.getColors();
5372 int[] newColors = new int[colors.length];
5373 for (int i = 0; i < newColors.length; i++) {
Lucas Dupina291d192018-06-07 13:59:42 -07005374 newColors[i] = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005375 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005376 }
5377 textColor = new ColorStateList(textColor.getStates().clone(),
5378 newColors);
Selim Cinek396caca2018-04-10 17:46:46 -07005379 if (fullLength) {
5380 outResultColor[0] = textColor;
5381 // Let's drop the color from the span
5382 textColor = null;
5383 }
Selim Cinek981962e2016-07-20 20:41:58 -07005384 resultSpan = new TextAppearanceSpan(
5385 originalSpan.getFamily(),
5386 originalSpan.getTextStyle(),
5387 originalSpan.getTextSize(),
5388 textColor,
5389 originalSpan.getLinkTextColor());
Selim Cinek981962e2016-07-20 20:41:58 -07005390 }
5391 } else if (resultSpan instanceof ForegroundColorSpan) {
5392 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
5393 int foregroundColor = originalSpan.getForegroundColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005394 foregroundColor = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005395 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005396 if (fullLength) {
5397 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
Selim Cinek396caca2018-04-10 17:46:46 -07005398 resultSpan = null;
5399 } else {
5400 resultSpan = new ForegroundColorSpan(foregroundColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005401 }
5402 } else {
5403 resultSpan = span;
5404 }
Selim Cinek396caca2018-04-10 17:46:46 -07005405 if (resultSpan != null) {
5406 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
5407 }
Selim Cinek981962e2016-07-20 20:41:58 -07005408 }
5409 return builder;
5410 }
5411 return charSequence;
5412 }
5413
5414 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005415 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07005416 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005417 */
5418 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005419 if (!mIsLegacyInitialized) {
5420 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
5421 < Build.VERSION_CODES.LOLLIPOP;
5422 mIsLegacyInitialized = true;
5423 }
5424 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005425 }
5426
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005427 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08005428 return processLegacyText(charSequence, false /* ambient */);
5429 }
5430
5431 private CharSequence processLegacyText(CharSequence charSequence, boolean ambient) {
5432 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
5433 boolean wantLightText = ambient;
5434 if (isAlreadyLightText != wantLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005435 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005436 } else {
5437 return charSequence;
5438 }
5439 }
5440
Dan Sandler26e81cf2014-05-06 10:01:27 -04005441 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005442 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04005443 */
Adrian Roos487374f2017-01-11 15:48:14 -08005444 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
5445 boolean ambient) {
Selim Cinekea4bef72015-12-02 15:51:10 -08005446 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005447 int color;
5448 if (ambient) {
5449 color = resolveAmbientColor();
5450 } else if (isColorized()) {
5451 color = getPrimaryTextColor();
5452 } else {
5453 color = resolveContrastColor();
5454 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005455 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07005456 contentView.setDrawableTint(R.id.icon, false, color,
5457 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005458
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005459 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005460 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08005461 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005462 }
5463
Dan Sandler26e81cf2014-05-06 10:01:27 -04005464 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005465 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04005466 * if it's grayscale).
5467 */
5468 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04005469 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
5470 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005471 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005472 // resolve color will fall back to the default when legacy
Sunny Goyal5b153922017-09-21 21:00:36 -07005473 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(),
5474 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01005475 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005476 }
5477
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05005478 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005479 if (mN.color != COLOR_DEFAULT) {
5480 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02005481 }
Jorim Jaggi74419312014-06-10 20:57:21 +02005482 }
5483
Adrian Roos4ff3b122016-02-01 12:26:13 -08005484 int resolveContrastColor() {
5485 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
5486 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005487 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005488
Selim Cinekac5f0272017-05-02 16:05:41 -07005489 int color;
Selim Cinekc7f5a822018-03-20 19:32:06 -07005490 int background = mContext.getColor(
5491 com.android.internal.R.color.notification_material_background_color);
Selim Cinekac5f0272017-05-02 16:05:41 -07005492 if (mN.color == COLOR_DEFAULT) {
5493 ensureColors();
Lucas Dupinf03e7522018-06-25 16:21:13 -07005494 color = ContrastColorUtil.resolveDefaultColor(mContext, background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005495 } else {
Lucas Dupina291d192018-06-07 13:59:42 -07005496 color = ContrastColorUtil.resolveContrastColor(mContext, mN.color,
Anthony Chenad4d1582017-04-10 16:07:58 -07005497 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005498 }
5499 if (Color.alpha(color) < 255) {
5500 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005501 color = ContrastColorUtil.compositeColors(color, background);
Selim Cinekac5f0272017-05-02 16:05:41 -07005502 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005503 mCachedContrastColorIsFor = mN.color;
Selim Cinekac5f0272017-05-02 16:05:41 -07005504 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005505 }
5506
Selim Cinek4717d862018-04-19 09:19:15 +08005507 int resolveNeutralColor() {
5508 if (mNeutralColor != COLOR_INVALID) {
5509 return mNeutralColor;
5510 }
5511 int background = mContext.getColor(
5512 com.android.internal.R.color.notification_material_background_color);
Lucas Dupinf03e7522018-06-25 16:21:13 -07005513 mNeutralColor = ContrastColorUtil.resolveDefaultColor(mContext, background,
5514 mInNightMode);
Selim Cinek4717d862018-04-19 09:19:15 +08005515 if (Color.alpha(mNeutralColor) < 255) {
5516 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005517 mNeutralColor = ContrastColorUtil.compositeColors(mNeutralColor, background);
Selim Cinek4717d862018-04-19 09:19:15 +08005518 }
5519 return mNeutralColor;
5520 }
5521
Adrian Roos487374f2017-01-11 15:48:14 -08005522 int resolveAmbientColor() {
5523 if (mCachedAmbientColorIsFor == mN.color && mCachedAmbientColorIsFor != COLOR_INVALID) {
5524 return mCachedAmbientColor;
5525 }
Lucas Dupina291d192018-06-07 13:59:42 -07005526 final int contrasted = ContrastColorUtil.resolveAmbientColor(mContext, mN.color);
Adrian Roos487374f2017-01-11 15:48:14 -08005527
5528 mCachedAmbientColorIsFor = mN.color;
5529 return mCachedAmbientColor = contrasted;
5530 }
5531
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005532 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005533 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005534 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08005535 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005536 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005537 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005538 mN.actions = new Action[mActions.size()];
5539 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005540 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005541 if (!mPersonList.isEmpty()) {
Selim Cineke7238dd2017-12-14 17:48:32 -08005542 mN.extras.putParcelableArrayList(EXTRA_PEOPLE_LIST, mPersonList);
Dan Sandler0bf2ed82013-12-21 23:33:41 -06005543 }
Selim Cinek247fa012016-02-18 09:50:48 -08005544 if (mN.bigContentView != null || mN.contentView != null
5545 || mN.headsUpContentView != null) {
5546 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
5547 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005548 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08005549 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005550
Julia Reynolds3b848122016-02-26 10:45:32 -05005551 /**
5552 * Creates a Builder from an existing notification so further changes can be made.
5553 * @param context The context for your application / activity.
5554 * @param n The notification to create a Builder from.
5555 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005556 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005557 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005558 ApplicationInfo applicationInfo = n.extras.getParcelable(
5559 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005560 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05005561 if (applicationInfo != null) {
5562 try {
5563 builderContext = context.createApplicationContext(applicationInfo,
5564 Context.CONTEXT_RESTRICTED);
5565 } catch (NameNotFoundException e) {
5566 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
5567 builderContext = context; // try with our context
5568 }
5569 } else {
5570 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02005571 }
5572
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005573 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005574 }
5575
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005576 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005577 * @deprecated Use {@link #build()} instead.
5578 */
5579 @Deprecated
5580 public Notification getNotification() {
5581 return build();
5582 }
5583
5584 /**
5585 * Combine all of the options that have been set and return a new {@link Notification}
5586 * object.
5587 */
5588 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005589 // first, add any extras from the calling code
5590 if (mUserExtras != null) {
5591 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005592 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005593
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005594 mN.creationTime = System.currentTimeMillis();
5595
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005596 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05005597 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02005598
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005599 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005600
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005601 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02005602 mStyle.reduceImageSizes(mContext);
5603 mStyle.purgeResources();
Selim Cinek90343862018-02-01 11:07:11 -08005604 mStyle.validate(mContext);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005605 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005606 }
Selim Cinekd0426622017-07-11 13:19:59 +02005607 mN.reduceImageSizes(mContext);
5608
Adrian Roos5081c0d2016-02-26 16:04:19 -08005609 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07005610 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005611 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005612 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005613 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
5614 mN.contentView.getSequenceNumber());
5615 }
5616 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005617 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005618 if (mN.bigContentView != null) {
5619 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
5620 mN.bigContentView.getSequenceNumber());
5621 }
5622 }
5623 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005624 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005625 if (mN.headsUpContentView != null) {
5626 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
5627 mN.headsUpContentView.getSequenceNumber());
5628 }
5629 }
5630 }
5631
Julia Reynolds4c0c2022016-02-02 15:11:59 -05005632 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
5633 mN.flags |= FLAG_SHOW_LIGHTS;
5634 }
5635
Adrian Roosfb921842017-10-26 14:49:56 +02005636 mN.allPendingIntents = null;
5637
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005638 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005639 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005640
5641 /**
5642 * Apply this Builder to an existing {@link Notification} object.
5643 *
5644 * @hide
5645 */
5646 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005647 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005648 return n;
5649 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005650
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005651 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005652 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005653 * change. Also removes extenders on low ram devices, as
5654 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005655 *
5656 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5657 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005658 * @hide
5659 */
Kristian Monsen05f34792018-04-09 10:27:16 +02005660 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam,
5661 Context context) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005662 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005663
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005664 // Only strip views for known Styles because we won't know how to
5665 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005666 if (!isLowRam
5667 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005668 && getNotificationStyleClass(templateClass) == null) {
5669 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005670 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005671
5672 // Only strip unmodified BuilderRemoteViews.
5673 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005674 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005675 n.contentView.getSequenceNumber();
5676 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005677 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005678 n.bigContentView.getSequenceNumber();
5679 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005680 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005681 n.headsUpContentView.getSequenceNumber();
5682
5683 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005684 if (!isLowRam
5685 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005686 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005687 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005688
5689 Notification clone = n.clone();
5690 if (stripContentView) {
5691 clone.contentView = null;
5692 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5693 }
5694 if (stripBigContentView) {
5695 clone.bigContentView = null;
5696 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5697 }
5698 if (stripHeadsUpContentView) {
5699 clone.headsUpContentView = null;
5700 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5701 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005702 if (isLowRam) {
Kristian Monsen05f34792018-04-09 10:27:16 +02005703 String[] allowedServices = context.getResources().getStringArray(
5704 R.array.config_allowedManagedServicesOnLowRamDevices);
5705 if (allowedServices.length == 0) {
5706 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5707 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5708 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5709 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005710 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005711 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005712 }
5713
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005714 @UnsupportedAppUsage
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005715 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005716 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005717 }
5718
5719 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005720 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005721 }
5722
5723 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005724 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005725 }
5726
5727 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005728 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005729 }
5730
5731 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005732 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005733 }
5734
Adrian Roosc1a80b02016-04-05 14:54:55 -07005735 private int getMessagingLayoutResource() {
5736 return R.layout.notification_template_material_messaging;
5737 }
5738
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005739 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005740 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005741 }
5742
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005743 private int getEmphasizedActionLayoutResource() {
5744 return R.layout.notification_material_action_emphasized;
5745 }
5746
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005747 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005748 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005749 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005750
5751 private int getBackgroundColor() {
5752 if (isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005753 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : mN.color;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005754 } else {
Selim Cinekc7f5a822018-03-20 19:32:06 -07005755 return COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005756 }
5757 }
5758
Selim Cinek396caca2018-04-10 17:46:46 -07005759 /**
Selim Cinek4717d862018-04-19 09:19:15 +08005760 * Gets a neutral color that can be used for icons or similar that should not stand out.
5761 */
5762 private int getNeutralColor() {
5763 if (isColorized()) {
5764 return getSecondaryTextColor();
5765 } else {
5766 return resolveNeutralColor();
5767 }
5768 }
5769
5770 /**
Selim Cinek396caca2018-04-10 17:46:46 -07005771 * Same as getBackgroundColor but also resolved the default color to the background.
5772 */
5773 private int resolveBackgroundColor() {
5774 int backgroundColor = getBackgroundColor();
5775 if (backgroundColor == COLOR_DEFAULT) {
5776 backgroundColor = mContext.getColor(
5777 com.android.internal.R.color.notification_material_background_color);
5778 }
5779 return backgroundColor;
5780 }
5781
Selim Cinek7b9605b2017-01-19 17:36:00 -08005782 private boolean isColorized() {
5783 return mN.isColorized();
5784 }
Selim Cinek99104832017-01-25 14:47:33 -08005785
Anthony Chenad4d1582017-04-10 16:07:58 -07005786 private boolean shouldTintActionButtons() {
5787 return mTintActionButtons;
5788 }
5789
Selim Cinek99104832017-01-25 14:47:33 -08005790 private boolean textColorsNeedInversion() {
5791 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
5792 return false;
5793 }
5794 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
5795 return targetSdkVersion > Build.VERSION_CODES.M
5796 && targetSdkVersion < Build.VERSION_CODES.O;
5797 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005798
5799 /**
5800 * Set a color palette to be used as the background and textColors
5801 *
5802 * @param backgroundColor the color to be used as the background
5803 * @param foregroundColor the color to be used as the foreground
5804 *
5805 * @hide
5806 */
5807 public void setColorPalette(int backgroundColor, int foregroundColor) {
5808 mBackgroundColor = backgroundColor;
5809 mForegroundColor = foregroundColor;
5810 mTextColorsAreForBackground = COLOR_INVALID;
5811 ensureColors();
5812 }
Selim Cinekac5f0272017-05-02 16:05:41 -07005813
5814 /**
Selim Cineka7679b62017-05-10 16:33:25 -07005815 * Forces all styled remoteViews to be built from scratch and not use any cached
5816 * RemoteViews.
5817 * This is needed for legacy apps that are baking in their remoteviews into the
5818 * notification.
5819 *
5820 * @hide
5821 */
5822 public void setRebuildStyledRemoteViews(boolean rebuild) {
5823 mRebuildStyledRemoteViews = rebuild;
5824 }
Selim Cinekaa9db1f2018-02-27 17:35:47 -08005825
5826 /**
5827 * Get the text that should be displayed in the statusBar when heads upped. This is
5828 * usually just the app name, but may be different depending on the style.
5829 *
5830 * @param publicMode If true, return a text that is safe to display in public.
5831 *
5832 * @hide
5833 */
5834 public CharSequence getHeadsUpStatusBarText(boolean publicMode) {
5835 if (mStyle != null && !publicMode) {
5836 CharSequence text = mStyle.getHeadsUpStatusBarText();
5837 if (!TextUtils.isEmpty(text)) {
5838 return text;
5839 }
5840 }
5841 return loadHeaderAppName();
5842 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005843 }
5844
5845 /**
Selim Cinekd0426622017-07-11 13:19:59 +02005846 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
5847 * remote views.
5848 *
5849 * @hide
5850 */
5851 void reduceImageSizes(Context context) {
5852 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
5853 return;
5854 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005855 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02005856 if (mLargeIcon != null || largeIcon != null) {
5857 Resources resources = context.getResources();
5858 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07005859 int maxWidth = resources.getDimensionPixelSize(isLowRam
5860 ? R.dimen.notification_right_icon_size_low_ram
5861 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02005862 int maxHeight = maxWidth;
5863 if (MediaStyle.class.equals(style)
5864 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005865 maxHeight = resources.getDimensionPixelSize(isLowRam
5866 ? R.dimen.notification_media_image_max_height_low_ram
5867 : R.dimen.notification_media_image_max_height);
5868 maxWidth = resources.getDimensionPixelSize(isLowRam
5869 ? R.dimen.notification_media_image_max_width_low_ram
5870 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02005871 }
5872 if (mLargeIcon != null) {
5873 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
5874 }
5875 if (largeIcon != null) {
5876 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
5877 }
5878 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005879 reduceImageSizesForRemoteView(contentView, context, isLowRam);
5880 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
5881 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02005882 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
5883 }
5884
Selim Cineka8cb1262017-08-15 16:53:44 -07005885 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
5886 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02005887 if (remoteView != null) {
5888 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07005889 int maxWidth = resources.getDimensionPixelSize(isLowRam
5890 ? R.dimen.notification_custom_view_max_image_width_low_ram
5891 : R.dimen.notification_custom_view_max_image_width);
5892 int maxHeight = resources.getDimensionPixelSize(isLowRam
5893 ? R.dimen.notification_custom_view_max_image_height_low_ram
5894 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02005895 remoteView.reduceImageSizes(maxWidth, maxHeight);
5896 }
5897 }
5898
5899 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005900 * @return whether this notification is a foreground service notification
Selim Cinek7b9605b2017-01-19 17:36:00 -08005901 */
Selim Cinek22714f12017-04-13 16:23:53 -07005902 private boolean isForegroundService() {
5903 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005904 }
5905
5906 /**
Selim Cinek99104832017-01-25 14:47:33 -08005907 * @return whether this notification has a media session attached
5908 * @hide
5909 */
5910 public boolean hasMediaSession() {
5911 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
5912 }
5913
5914 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005915 * @return the style class of this notification
5916 * @hide
5917 */
5918 public Class<? extends Notification.Style> getNotificationStyle() {
5919 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
5920
5921 if (!TextUtils.isEmpty(templateClass)) {
5922 return Notification.getNotificationStyleClass(templateClass);
5923 }
5924 return null;
5925 }
5926
5927 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005928 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08005929 *
5930 * @hide
5931 */
5932 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005933 if (isColorizedMedia()) {
5934 return true;
5935 }
Julia Reynolds4db59552017-06-30 13:34:01 -04005936 return extras.getBoolean(EXTRA_COLORIZED)
5937 && (hasColorizedPermission() || isForegroundService());
5938 }
5939
5940 /**
5941 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
5942 * permission. The permission is checked when a notification is enqueued.
5943 */
5944 private boolean hasColorizedPermission() {
5945 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07005946 }
5947
5948 /**
5949 * @return true if this notification is colorized and it is a media notification
5950 *
5951 * @hide
5952 */
5953 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08005954 Class<? extends Style> style = getNotificationStyle();
5955 if (MediaStyle.class.equals(style)) {
5956 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
5957 if ((colorized == null || colorized) && hasMediaSession()) {
5958 return true;
5959 }
5960 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5961 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
5962 return true;
5963 }
5964 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005965 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005966 }
5967
Selim Cinek0847acd2017-04-24 19:48:29 -07005968
5969 /**
5970 * @return true if this is a media notification
5971 *
5972 * @hide
5973 */
5974 public boolean isMediaNotification() {
5975 Class<? extends Style> style = getNotificationStyle();
5976 if (MediaStyle.class.equals(style)) {
5977 return true;
5978 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5979 return true;
5980 }
5981 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005982 }
5983
Selim Cinek279fa862016-06-14 10:57:25 -07005984 private boolean hasLargeIcon() {
5985 return mLargeIcon != null || largeIcon != null;
5986 }
5987
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005988 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005989 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005990 * @hide
5991 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07005992 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005993 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
5994 }
5995
5996 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005997 * @return true if the notification will show a chronometer; false otherwise
5998 * @hide
5999 */
6000 public boolean showsChronometer() {
6001 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
6002 }
6003
6004 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04006005 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05006006 */
6007 @SystemApi
6008 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
6009 Class<? extends Style>[] classes = new Class[] {
6010 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
6011 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
6012 MessagingStyle.class };
6013 for (Class<? extends Style> innerClass : classes) {
6014 if (templateClass.equals(innerClass.getName())) {
6015 return innerClass;
6016 }
6017 }
6018 return null;
6019 }
6020
6021 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006022 * An object that can apply a rich notification style to a {@link Notification.Builder}
6023 * object.
6024 */
Griff Hazendfcb0802014-02-11 12:00:00 -08006025 public static abstract class Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07006026
6027 /**
6028 * The number of items allowed simulatanously in the remote input history.
6029 * @hide
6030 */
6031 static final int MAX_REMOTE_INPUT_HISTORY_LINES = 3;
Chris Wrend6297db2012-05-03 16:20:13 -04006032 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006033
6034 /**
6035 * @hide
6036 */
6037 protected CharSequence mSummaryText = null;
6038
6039 /**
6040 * @hide
6041 */
6042 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04006043
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006044 protected Builder mBuilder;
6045
Chris Wrend6297db2012-05-03 16:20:13 -04006046 /**
6047 * Overrides ContentTitle in the big form of the template.
6048 * This defaults to the value passed to setContentTitle().
6049 */
6050 protected void internalSetBigContentTitle(CharSequence title) {
6051 mBigContentTitle = title;
6052 }
6053
6054 /**
6055 * Set the first line of text after the detail section in the big form of the template.
6056 */
6057 protected void internalSetSummaryText(CharSequence cs) {
6058 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04006059 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04006060 }
6061
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006062 public void setBuilder(Builder builder) {
6063 if (mBuilder != builder) {
6064 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07006065 if (mBuilder != null) {
6066 mBuilder.setStyle(this);
6067 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006068 }
6069 }
6070
Chris Wrend6297db2012-05-03 16:20:13 -04006071 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006072 if (mBuilder == null) {
6073 throw new IllegalArgumentException("Style requires a valid Builder object");
6074 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006075 }
Chris Wrend6297db2012-05-03 16:20:13 -04006076
6077 protected RemoteViews getStandardView(int layoutId) {
Selim Cinek384804b2018-04-18 14:31:07 +08006078 return getStandardView(layoutId, null);
6079 }
6080
6081 /**
6082 * Get the standard view for this style.
6083 *
6084 * @param layoutId The layout id to use
6085 * @param result The result where template bind information is saved.
6086 * @return A remoteView for this style.
6087 * @hide
6088 */
6089 protected RemoteViews getStandardView(int layoutId, TemplateBindResult result) {
Chris Wrend6297db2012-05-03 16:20:13 -04006090 checkBuilder();
6091
Christoph Studer4600f9b2014-07-22 22:44:43 +02006092 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006093 CharSequence oldBuilderContentTitle =
6094 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04006095 if (mBigContentTitle != null) {
6096 mBuilder.setContentTitle(mBigContentTitle);
6097 }
6098
Selim Cinek384804b2018-04-18 14:31:07 +08006099 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId, result);
Chris Wrend6297db2012-05-03 16:20:13 -04006100
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006101 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006102
Chris Wrend6297db2012-05-03 16:20:13 -04006103 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
6104 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04006105 } else {
6106 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04006107 }
6108
Chris Wrend6297db2012-05-03 16:20:13 -04006109 return contentView;
6110 }
6111
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006112 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006113 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006114 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08006115 *
6116 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006117 * @hide
6118 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08006119 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006120 return null;
6121 }
6122
6123 /**
6124 * Construct a Style-specific RemoteViews for the final big notification layout.
6125 * @hide
6126 */
6127 public RemoteViews makeBigContentView() {
6128 return null;
6129 }
6130
6131 /**
6132 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08006133 *
6134 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006135 * @hide
6136 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006137 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006138 return null;
6139 }
6140
6141 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006142 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006143 * @hide
6144 */
6145 public void addExtras(Bundle extras) {
6146 if (mSummaryTextSet) {
6147 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
6148 }
6149 if (mBigContentTitle != null) {
6150 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
6151 }
Chris Wren91ad5632013-06-05 15:05:57 -04006152 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006153 }
6154
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006155 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006156 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006157 * @hide
6158 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02006159 protected void restoreFromExtras(Bundle extras) {
6160 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
6161 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
6162 mSummaryTextSet = true;
6163 }
6164 if (extras.containsKey(EXTRA_TITLE_BIG)) {
6165 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
6166 }
6167 }
6168
6169
6170 /**
6171 * @hide
6172 */
6173 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006174 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006175 return wip;
6176 }
6177
Daniel Sandler0ec46202015-06-24 01:27:05 -04006178 /**
6179 * @hide
6180 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006181 public void purgeResources() {}
6182
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006183 /**
6184 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
6185 * attached to.
6186 *
6187 * @return the fully constructed Notification.
6188 */
6189 public Notification build() {
6190 checkBuilder();
6191 return mBuilder.build();
6192 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006193
6194 /**
6195 * @hide
6196 * @return true if the style positions the progress bar on the second line; false if the
6197 * style hides the progress bar
6198 */
6199 protected boolean hasProgress() {
6200 return true;
6201 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006202
6203 /**
6204 * @hide
6205 * @return Whether we should put the summary be put into the notification header
6206 */
6207 public boolean hasSummaryInHeader() {
6208 return true;
6209 }
Selim Cinek593610c2016-02-16 18:42:57 -08006210
6211 /**
6212 * @hide
6213 * @return Whether custom content views are displayed inline in the style
6214 */
6215 public boolean displayCustomViewInline() {
6216 return false;
6217 }
Selim Cinekd0426622017-07-11 13:19:59 +02006218
6219 /**
6220 * Reduces the image sizes contained in this style.
6221 *
6222 * @hide
6223 */
6224 public void reduceImageSizes(Context context) {
6225 }
Selim Cinek90343862018-02-01 11:07:11 -08006226
6227 /**
6228 * Validate that this style was properly composed. This is called at build time.
6229 * @hide
6230 */
6231 public void validate(Context context) {
6232 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006233
6234 /**
6235 * @hide
6236 */
6237 public abstract boolean areNotificationsVisiblyDifferent(Style other);
Selim Cinekc7f5a822018-03-20 19:32:06 -07006238
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006239 /**
koprivaa1a78482018-10-09 10:09:23 -07006240 * @return the text that should be displayed in the statusBar when heads-upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006241 * If {@code null} is returned, the default implementation will be used.
6242 *
6243 * @hide
6244 */
6245 public CharSequence getHeadsUpStatusBarText() {
6246 return null;
6247 }
Joe Onorato46439ce2010-11-19 13:56:21 -08006248 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006249
6250 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006251 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08006252 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006253 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006254 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006255 * Notification notif = new Notification.Builder(mContext)
6256 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
6257 * .setContentText(subject)
6258 * .setSmallIcon(R.drawable.new_post)
6259 * .setLargeIcon(aBitmap)
6260 * .setStyle(new Notification.BigPictureStyle()
6261 * .bigPicture(aBigBitmap))
6262 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006263 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006264 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006265 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006266 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006267 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006268 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006269 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006270 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006271
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006272 public BigPictureStyle() {
6273 }
6274
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006275 /**
6276 * @deprecated use {@code BigPictureStyle()}.
6277 */
6278 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006279 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006280 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006281 }
6282
Chris Wrend6297db2012-05-03 16:20:13 -04006283 /**
6284 * Overrides ContentTitle in the big form of the template.
6285 * This defaults to the value passed to setContentTitle().
6286 */
6287 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006288 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006289 return this;
6290 }
6291
6292 /**
6293 * Set the first line of text after the detail section in the big form of the template.
6294 */
6295 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006296 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006297 return this;
6298 }
6299
Chris Wren0bd664d2012-08-01 13:56:56 -04006300 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05006301 * @hide
6302 */
6303 public Bitmap getBigPicture() {
6304 return mPicture;
6305 }
6306
6307 /**
Chris Wren0bd664d2012-08-01 13:56:56 -04006308 * Provide the bitmap to be used as the payload for the BigPicture notification.
6309 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006310 public BigPictureStyle bigPicture(Bitmap b) {
6311 mPicture = b;
6312 return this;
6313 }
6314
Chris Wren3745a3d2012-05-22 15:11:52 -04006315 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04006316 * Override the large icon when the big notification is shown.
6317 */
6318 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04006319 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
6320 }
6321
6322 /**
6323 * Override the large icon when the big notification is shown.
6324 */
6325 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04006326 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006327 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006328 return this;
6329 }
6330
Riley Andrews0394a0c2015-11-03 23:36:52 -08006331 /** @hide */
6332 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
6333
Daniel Sandler0ec46202015-06-24 01:27:05 -04006334 /**
6335 * @hide
6336 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006337 @Override
6338 public void purgeResources() {
6339 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08006340 if (mPicture != null &&
6341 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08006342 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006343 mPicture = mPicture.createAshmemBitmap();
6344 }
6345 if (mBigLargeIcon != null) {
6346 mBigLargeIcon.convertToAshmem();
6347 }
6348 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01006349
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006350 /**
6351 * @hide
6352 */
Selim Cinekd0426622017-07-11 13:19:59 +02006353 @Override
6354 public void reduceImageSizes(Context context) {
6355 super.reduceImageSizes(context);
6356 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006357 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006358 if (mPicture != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006359 int maxPictureWidth = resources.getDimensionPixelSize(isLowRam
6360 ? R.dimen.notification_big_picture_max_height_low_ram
6361 : R.dimen.notification_big_picture_max_height);
6362 int maxPictureHeight = resources.getDimensionPixelSize(isLowRam
6363 ? R.dimen.notification_big_picture_max_width_low_ram
6364 : R.dimen.notification_big_picture_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006365 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
6366 }
6367 if (mBigLargeIcon != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006368 int rightIconSize = resources.getDimensionPixelSize(isLowRam
6369 ? R.dimen.notification_right_icon_size_low_ram
6370 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006371 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
6372 }
6373 }
6374
6375 /**
6376 * @hide
6377 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006378 public RemoteViews makeBigContentView() {
6379 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01006380 // This covers the following cases:
6381 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006382 // mN.mLargeIcon
6383 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04006384 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07006385 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006386 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006387 oldLargeIcon = mBuilder.mN.mLargeIcon;
6388 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006389 // The legacy largeIcon might not allow us to clear the image, as it's taken in
6390 // replacement if the other one is null. Because we're restoring these legacy icons
6391 // for old listeners, this is in general non-null.
6392 largeIconLegacy = mBuilder.mN.largeIcon;
6393 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006394 }
6395
Selim Cinek384804b2018-04-18 14:31:07 +08006396 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource(),
6397 null /* result */);
Selim Cinek03d0d652015-11-13 13:18:09 -05006398 if (mSummaryTextSet) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006399 contentView.setTextViewText(R.id.text, mBuilder.processTextSpans(
6400 mBuilder.processLegacyText(mSummaryText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006401 mBuilder.setTextViewColorSecondary(contentView, R.id.text);
Selim Cinekc848c3a2016-01-13 15:27:30 -08006402 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05006403 }
Selim Cinek279fa862016-06-14 10:57:25 -07006404 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08006405
Christoph Studer5c510ee2014-12-15 16:32:27 +01006406 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006407 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006408 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006409 }
6410
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006411 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006412 return contentView;
6413 }
6414
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006415 /**
6416 * @hide
6417 */
6418 public void addExtras(Bundle extras) {
6419 super.addExtras(extras);
6420
6421 if (mBigLargeIconSet) {
6422 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
6423 }
6424 extras.putParcelable(EXTRA_PICTURE, mPicture);
6425 }
6426
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006427 /**
6428 * @hide
6429 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006430 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02006431 protected void restoreFromExtras(Bundle extras) {
6432 super.restoreFromExtras(extras);
6433
6434 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01006435 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02006436 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04006437 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006438 mPicture = extras.getParcelable(EXTRA_PICTURE);
6439 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006440
6441 /**
6442 * @hide
6443 */
6444 @Override
6445 public boolean hasSummaryInHeader() {
6446 return false;
6447 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006448
6449 /**
6450 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006451 * Note that we aren't actually comparing the contents of the bitmaps here, so this
6452 * is only doing a cursory inspection. Bitmaps of equal size will appear the same.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006453 */
6454 @Override
6455 public boolean areNotificationsVisiblyDifferent(Style other) {
6456 if (other == null || getClass() != other.getClass()) {
6457 return true;
6458 }
6459 BigPictureStyle otherS = (BigPictureStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006460 return areBitmapsObviouslyDifferent(getBigPicture(), otherS.getBigPicture());
6461 }
6462
6463 private static boolean areBitmapsObviouslyDifferent(Bitmap a, Bitmap b) {
6464 if (a == b) {
6465 return false;
6466 }
6467 if (a == null || b == null) {
6468 return true;
6469 }
6470 return a.getWidth() != b.getWidth()
6471 || a.getHeight() != b.getHeight()
6472 || a.getConfig() != b.getConfig()
6473 || a.getGenerationId() != b.getGenerationId();
Julia Reynolds7217dc92018-03-07 12:12:09 -05006474 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006475 }
6476
6477 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006478 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08006479 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006480 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006481 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006482 * Notification notif = new Notification.Builder(mContext)
6483 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
6484 * .setContentText(subject)
6485 * .setSmallIcon(R.drawable.new_mail)
6486 * .setLargeIcon(aBitmap)
6487 * .setStyle(new Notification.BigTextStyle()
6488 * .bigText(aVeryLongString))
6489 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006490 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006491 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006492 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006493 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006494 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006495
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006496 private CharSequence mBigText;
6497
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006498 public BigTextStyle() {
6499 }
6500
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006501 /**
6502 * @deprecated use {@code BigTextStyle()}.
6503 */
6504 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006505 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006506 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006507 }
6508
Chris Wrend6297db2012-05-03 16:20:13 -04006509 /**
6510 * Overrides ContentTitle in the big form of the template.
6511 * This defaults to the value passed to setContentTitle().
6512 */
6513 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006514 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006515 return this;
6516 }
6517
6518 /**
6519 * Set the first line of text after the detail section in the big form of the template.
6520 */
6521 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006522 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006523 return this;
6524 }
6525
Chris Wren0bd664d2012-08-01 13:56:56 -04006526 /**
6527 * Provide the longer text to be displayed in the big form of the
6528 * template in place of the content text.
6529 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006530 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006531 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006532 return this;
6533 }
6534
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006535 /**
6536 * @hide
6537 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05006538 public CharSequence getBigText() {
6539 return mBigText;
6540 }
6541
6542 /**
6543 * @hide
6544 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006545 public void addExtras(Bundle extras) {
6546 super.addExtras(extras);
6547
Christoph Studer4600f9b2014-07-22 22:44:43 +02006548 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
6549 }
6550
6551 /**
6552 * @hide
6553 */
6554 @Override
6555 protected void restoreFromExtras(Bundle extras) {
6556 super.restoreFromExtras(extras);
6557
6558 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006559 }
6560
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006561 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006562 * @param increasedHeight true if this layout be created with an increased height.
6563 *
6564 * @hide
6565 */
6566 @Override
6567 public RemoteViews makeContentView(boolean increasedHeight) {
6568 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006569 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006570 mBuilder.mActions = new ArrayList<>();
6571 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006572 mBuilder.mActions = mBuilder.mOriginalActions;
6573 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006574 return remoteViews;
6575 }
6576 return super.makeContentView(increasedHeight);
6577 }
6578
6579 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006580 * @hide
6581 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006582 @Override
6583 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
6584 if (increasedHeight && mBuilder.mActions.size() > 0) {
6585 return makeBigContentView();
6586 }
6587 return super.makeHeadsUpContentView(increasedHeight);
6588 }
6589
6590 /**
6591 * @hide
6592 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006593 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02006594
6595 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07006596 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006597 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04006598
Selim Cinek384804b2018-04-18 14:31:07 +08006599 TemplateBindResult result = new TemplateBindResult();
6600 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource(), result);
Selim Cinek1c72fa02018-04-23 18:00:54 +08006601 contentView.setInt(R.id.big_text, "setImageEndMargin", result.getIconMarginEnd());
Joe Malin8d40d042012-11-05 11:36:40 -08006602
Selim Cinek75998782016-04-26 10:39:17 -07006603 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006604
Selim Cinek3a2c4b92015-12-17 17:01:17 -08006605 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07006606 if (TextUtils.isEmpty(bigTextText)) {
6607 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
6608 // experience
6609 bigTextText = mBuilder.processLegacyText(text);
6610 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07006611 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08006612
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006613 return contentView;
6614 }
6615
Julia Reynolds7217dc92018-03-07 12:12:09 -05006616 /**
6617 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006618 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006619 */
6620 @Override
6621 public boolean areNotificationsVisiblyDifferent(Style other) {
6622 if (other == null || getClass() != other.getClass()) {
6623 return true;
6624 }
6625 BigTextStyle newS = (BigTextStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006626 return !Objects.equals(String.valueOf(getBigText()), String.valueOf(newS.getBigText()));
Julia Reynolds7217dc92018-03-07 12:12:09 -05006627 }
6628
Adrian Roosb1f427c2016-05-26 12:27:15 -07006629 static void applyBigTextContentView(Builder builder,
6630 RemoteViews contentView, CharSequence bigTextText) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006631 contentView.setTextViewText(R.id.big_text, builder.processTextSpans(bigTextText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006632 builder.setTextViewColorSecondary(contentView, R.id.big_text);
Adrian Roosb1f427c2016-05-26 12:27:15 -07006633 contentView.setViewVisibility(R.id.big_text,
6634 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek279fa862016-06-14 10:57:25 -07006635 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07006636 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006637 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006638
6639 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006640 * Helper class for generating large-format notifications that include multiple back-and-forth
6641 * messages of varying types between any number of people.
6642 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006643 * <p>
Alex Hillsfc737de2016-03-23 17:33:02 -04006644 * If the platform does not provide large-format notifications, this method has no effect. The
6645 * user will always see the normal notification view.
Selim Cinekce8794f2018-05-23 16:46:05 -07006646 *
6647 * <p>
6648 * If the app is targeting Android P and above, it is required to use the {@link Person}
6649 * class in order to get an optimal rendering of the notification and its avatars. For
6650 * conversations involving multiple people, the app should also make sure that it marks the
6651 * conversation as a group with {@link #setGroupConversation(boolean)}.
6652 *
6653 * <p>
6654 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior.
6655 * Here's an example of how this may be used:
Alex Hillsfc737de2016-03-23 17:33:02 -04006656 * <pre class="prettyprint">
6657 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006658 * Person user = new Person.Builder().setIcon(userIcon).setName(userName).build();
6659 * MessagingStyle style = new MessagingStyle(user)
6660 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getPerson())
6661 * .addMessage(messages[2].getText(), messages[2].getTime(), messages[2].getPerson())
6662 * .setGroupConversation(hasMultiplePeople());
6663 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006664 * Notification noti = new Notification.Builder()
Selim Cinekce8794f2018-05-23 16:46:05 -07006665 * .setContentTitle(&quot;2 new messages with &quot; + sender.toString())
Alex Hillsfc737de2016-03-23 17:33:02 -04006666 * .setContentText(subject)
6667 * .setSmallIcon(R.drawable.new_message)
6668 * .setLargeIcon(aBitmap)
Selim Cinekce8794f2018-05-23 16:46:05 -07006669 * .setStyle(style)
Alex Hillsfc737de2016-03-23 17:33:02 -04006670 * .build();
6671 * </pre>
6672 */
6673 public static class MessagingStyle extends Style {
6674
6675 /**
6676 * The maximum number of messages that will be retained in the Notification itself (the
6677 * number displayed is up to the platform).
6678 */
6679 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
6680
Selim Cinekcb8b9852017-12-15 18:01:52 -08006681 @NonNull Person mUser;
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006682 @Nullable CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04006683 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08006684 List<Message> mHistoricMessages = new ArrayList<>();
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006685 boolean mIsGroupConversation;
Alex Hillsfc737de2016-03-23 17:33:02 -04006686
6687 MessagingStyle() {
6688 }
6689
6690 /**
Alex Hillsfd590442016-10-07 09:52:44 -04006691 * @param userDisplayName Required - the name to be displayed for any replies sent by the
6692 * user before the posting app reposts the notification with those messages after they've
6693 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04006694 * {@link #addMessage(Notification.MessagingStyle.Message)}
Selim Cinekcb8b9852017-12-15 18:01:52 -08006695 *
6696 * @deprecated use {@code MessagingStyle(Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006697 */
Alex Hillsfd590442016-10-07 09:52:44 -04006698 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Selim Cinek9acd6732018-03-23 16:39:02 -07006699 this(new Person.Builder().setName(userDisplayName).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006700 }
6701
6702 /**
6703 * @param user Required - The person displayed for any messages that are sent by the
6704 * user. Any messages added with {@link #addMessage(Notification.MessagingStyle.Message)}
6705 * who don't have a Person associated with it will be displayed as if they were sent
Selim Cinek90343862018-02-01 11:07:11 -08006706 * by this user. The user also needs to have a valid name associated with it, which will
6707 * be enforced starting in Android P.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006708 */
6709 public MessagingStyle(@NonNull Person user) {
6710 mUser = user;
Selim Cinek90343862018-02-01 11:07:11 -08006711 }
6712
6713 /**
6714 * Validate that this style was properly composed. This is called at build time.
6715 * @hide
6716 */
6717 @Override
6718 public void validate(Context context) {
6719 super.validate(context);
6720 if (context.getApplicationInfo().targetSdkVersion
6721 >= Build.VERSION_CODES.P && (mUser == null || mUser.getName() == null)) {
6722 throw new RuntimeException("User must be valid and have a name.");
Selim Cinekcb8b9852017-12-15 18:01:52 -08006723 }
6724 }
6725
6726 /**
koprivaa1a78482018-10-09 10:09:23 -07006727 * @return the text that should be displayed in the statusBar when heads upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006728 * If {@code null} is returned, the default implementation will be used.
6729 *
6730 * @hide
6731 */
6732 @Override
6733 public CharSequence getHeadsUpStatusBarText() {
6734 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
6735 ? super.mBigContentTitle
6736 : mConversationTitle;
6737 if (!TextUtils.isEmpty(conversationTitle) && !hasOnlyWhiteSpaceSenders()) {
6738 return conversationTitle;
6739 }
6740 return null;
6741 }
6742
6743 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08006744 * @return the user to be displayed for any replies sent by the user
6745 */
Selim Cinekeb53c222018-04-06 12:56:09 -07006746 @NonNull
Selim Cinekcb8b9852017-12-15 18:01:52 -08006747 public Person getUser() {
6748 return mUser;
Alex Hillsfc737de2016-03-23 17:33:02 -04006749 }
6750
6751 /**
6752 * Returns the name to be displayed for any replies sent by the user
Selim Cinekcb8b9852017-12-15 18:01:52 -08006753 *
6754 * @deprecated use {@link #getUser()} instead
Alex Hillsfc737de2016-03-23 17:33:02 -04006755 */
6756 public CharSequence getUserDisplayName() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006757 return mUser.getName();
Alex Hillsfc737de2016-03-23 17:33:02 -04006758 }
6759
6760 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006761 * Sets the title to be displayed on this conversation. May be set to {@code null}.
6762 *
Kodlee Yin14656422017-12-22 17:00:46 -08006763 * <p>This API's behavior was changed in SDK version {@link Build.VERSION_CODES#P}. If your
6764 * application's target version is less than {@link Build.VERSION_CODES#P}, setting a
6765 * conversation title to a non-null value will make {@link #isGroupConversation()} return
6766 * {@code true} and passing {@code null} will make it return {@code false}. In
6767 * {@link Build.VERSION_CODES#P} and beyond, use {@link #setGroupConversation(boolean)}
6768 * to set group conversation status.
6769 *
6770 * @param conversationTitle Title displayed for this conversation
6771 * @return this object for method chaining
Alex Hillsfc737de2016-03-23 17:33:02 -04006772 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006773 public MessagingStyle setConversationTitle(@Nullable CharSequence conversationTitle) {
Alex Hillsfc737de2016-03-23 17:33:02 -04006774 mConversationTitle = conversationTitle;
6775 return this;
6776 }
6777
6778 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006779 * Return the title to be displayed on this conversation. May return {@code null}.
Alex Hillsfc737de2016-03-23 17:33:02 -04006780 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006781 @Nullable
Alex Hillsfc737de2016-03-23 17:33:02 -04006782 public CharSequence getConversationTitle() {
6783 return mConversationTitle;
6784 }
6785
6786 /**
6787 * Adds a message for display by this notification. Convenience call for a simple
6788 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6789 * @param text A {@link CharSequence} to be displayed as the message content
6790 * @param timestamp Time at which the message arrived
6791 * @param sender A {@link CharSequence} to be used for displaying the name of the
6792 * sender. Should be <code>null</code> for messages by the current user, in which case
6793 * the platform will insert {@link #getUserDisplayName()}.
6794 * Should be unique amongst all individuals in the conversation, and should be
6795 * consistent during re-posts of the notification.
6796 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07006797 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Alex Hillsfc737de2016-03-23 17:33:02 -04006798 *
6799 * @return this object for method chaining
Selim Cinekcb8b9852017-12-15 18:01:52 -08006800 *
6801 * @deprecated use {@link #addMessage(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006802 */
6803 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006804 return addMessage(text, timestamp,
Selim Cinek9acd6732018-03-23 16:39:02 -07006805 sender == null ? null : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006806 }
6807
6808 /**
6809 * Adds a message for display by this notification. Convenience call for a simple
6810 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6811 * @param text A {@link CharSequence} to be displayed as the message content
6812 * @param timestamp Time at which the message arrived
6813 * @param sender The {@link Person} who sent the message.
6814 * Should be <code>null</code> for messages by the current user, in which case
6815 * the platform will insert the user set in {@code MessagingStyle(Person)}.
6816 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07006817 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Selim Cinekcb8b9852017-12-15 18:01:52 -08006818 *
6819 * @return this object for method chaining
6820 */
Selim Cinekeb53c222018-04-06 12:56:09 -07006821 public MessagingStyle addMessage(@NonNull CharSequence text, long timestamp,
6822 @Nullable Person sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08006823 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04006824 }
6825
6826 /**
6827 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08006828 *
6829 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6830 * the newest last.
6831 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006832 * @param message The {@link Message} to be displayed
6833 * @return this object for method chaining
6834 */
6835 public MessagingStyle addMessage(Message message) {
6836 mMessages.add(message);
6837 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6838 mMessages.remove(0);
6839 }
6840 return this;
6841 }
6842
6843 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006844 * Adds a {@link Message} for historic context in this notification.
6845 *
6846 * <p>Messages should be added as historic if they are not the main subject of the
6847 * notification but may give context to a conversation. The system may choose to present
6848 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
6849 *
6850 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6851 * the newest last.
6852 *
6853 * @param message The historic {@link Message} to be added
6854 * @return this object for method chaining
6855 */
6856 public MessagingStyle addHistoricMessage(Message message) {
6857 mHistoricMessages.add(message);
6858 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6859 mHistoricMessages.remove(0);
6860 }
6861 return this;
6862 }
6863
6864 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006865 * Gets the list of {@code Message} objects that represent the notification
6866 */
6867 public List<Message> getMessages() {
6868 return mMessages;
6869 }
6870
6871 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006872 * Gets the list of historic {@code Message}s in the notification.
6873 */
6874 public List<Message> getHistoricMessages() {
6875 return mHistoricMessages;
6876 }
6877
6878 /**
Selim Cinekce8794f2018-05-23 16:46:05 -07006879 * Sets whether this conversation notification represents a group. If the app is targeting
6880 * Android P, this is required if the app wants to display the largeIcon set with
6881 * {@link Notification.Builder#setLargeIcon(Bitmap)}, otherwise it will be hidden.
Kodlee Yin14656422017-12-22 17:00:46 -08006882 *
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006883 * @param isGroupConversation {@code true} if the conversation represents a group,
6884 * {@code false} otherwise.
6885 * @return this object for method chaining
6886 */
6887 public MessagingStyle setGroupConversation(boolean isGroupConversation) {
6888 mIsGroupConversation = isGroupConversation;
6889 return this;
6890 }
6891
6892 /**
Kodlee Yin14656422017-12-22 17:00:46 -08006893 * Returns {@code true} if this notification represents a group conversation, otherwise
6894 * {@code false}.
6895 *
6896 * <p> If the application that generated this {@link MessagingStyle} targets an SDK version
6897 * less than {@link Build.VERSION_CODES#P}, this method becomes dependent on whether or
6898 * not the conversation title is set; returning {@code true} if the conversation title is
6899 * a non-null value, or {@code false} otherwise. From {@link Build.VERSION_CODES#P} forward,
6900 * this method returns what's set by {@link #setGroupConversation(boolean)} allowing for
6901 * named, non-group conversations.
6902 *
6903 * @see #setConversationTitle(CharSequence)
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006904 */
6905 public boolean isGroupConversation() {
Kodlee Yin14656422017-12-22 17:00:46 -08006906 // When target SDK version is < P, a non-null conversation title dictates if this is
6907 // as group conversation.
6908 if (mBuilder != null
6909 && mBuilder.mContext.getApplicationInfo().targetSdkVersion
6910 < Build.VERSION_CODES.P) {
6911 return mConversationTitle != null;
6912 }
6913
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006914 return mIsGroupConversation;
6915 }
6916
6917 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006918 * @hide
6919 */
6920 @Override
6921 public void addExtras(Bundle extras) {
6922 super.addExtras(extras);
Selim Cinekcb8b9852017-12-15 18:01:52 -08006923 if (mUser != null) {
6924 // For legacy usages
6925 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUser.getName());
6926 extras.putParcelable(EXTRA_MESSAGING_PERSON, mUser);
Alex Hillsfc737de2016-03-23 17:33:02 -04006927 }
6928 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006929 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04006930 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006931 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
6932 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04006933 }
Adrian Roos437cd562017-01-18 15:47:03 -08006934 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
6935 Message.getBundleArrayForMessages(mHistoricMessages));
6936 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006937
6938 fixTitleAndTextExtras(extras);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006939 extras.putBoolean(EXTRA_IS_GROUP_CONVERSATION, mIsGroupConversation);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006940 }
6941
6942 private void fixTitleAndTextExtras(Bundle extras) {
6943 Message m = findLatestIncomingMessage();
6944 CharSequence text = (m == null) ? null : m.mText;
6945 CharSequence sender = m == null ? null
Selim Cinekcb8b9852017-12-15 18:01:52 -08006946 : m.mSender == null || TextUtils.isEmpty(m.mSender.getName())
6947 ? mUser.getName() : m.mSender.getName();
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006948 CharSequence title;
6949 if (!TextUtils.isEmpty(mConversationTitle)) {
6950 if (!TextUtils.isEmpty(sender)) {
6951 BidiFormatter bidi = BidiFormatter.getInstance();
6952 title = mBuilder.mContext.getString(
6953 com.android.internal.R.string.notification_messaging_title_template,
Selim Cinekcb8b9852017-12-15 18:01:52 -08006954 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(sender));
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006955 } else {
6956 title = mConversationTitle;
6957 }
6958 } else {
6959 title = sender;
6960 }
6961
6962 if (title != null) {
6963 extras.putCharSequence(EXTRA_TITLE, title);
6964 }
6965 if (text != null) {
6966 extras.putCharSequence(EXTRA_TEXT, text);
6967 }
Alex Hillsfc737de2016-03-23 17:33:02 -04006968 }
6969
6970 /**
6971 * @hide
6972 */
6973 @Override
6974 protected void restoreFromExtras(Bundle extras) {
6975 super.restoreFromExtras(extras);
6976
Selim Cinekcb8b9852017-12-15 18:01:52 -08006977 mUser = extras.getParcelable(EXTRA_MESSAGING_PERSON);
6978 if (mUser == null) {
6979 CharSequence displayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
Selim Cinek9acd6732018-03-23 16:39:02 -07006980 mUser = new Person.Builder().setName(displayName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08006981 }
Adrian Roos96b7e202016-05-17 13:50:38 -07006982 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08006983 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07006984 mMessages = Message.getMessagesFromBundleArray(messages);
Adrian Roos437cd562017-01-18 15:47:03 -08006985 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07006986 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006987 mIsGroupConversation = extras.getBoolean(EXTRA_IS_GROUP_CONVERSATION);
Alex Hillsfc737de2016-03-23 17:33:02 -04006988 }
6989
6990 /**
6991 * @hide
6992 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07006993 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006994 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07006995 mBuilder.mOriginalActions = mBuilder.mActions;
6996 mBuilder.mActions = new ArrayList<>();
Selim Cinek85d0e6e2018-03-23 18:08:32 -07006997 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08006998 false /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07006999 mBuilder.mActions = mBuilder.mOriginalActions;
7000 mBuilder.mOriginalActions = null;
7001 return remoteViews;
Adrian Roosc1a80b02016-04-05 14:54:55 -07007002 }
7003
Julia Reynolds7217dc92018-03-07 12:12:09 -05007004 /**
7005 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04007006 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05007007 */
7008 @Override
7009 public boolean areNotificationsVisiblyDifferent(Style other) {
7010 if (other == null || getClass() != other.getClass()) {
7011 return true;
7012 }
7013 MessagingStyle newS = (MessagingStyle) other;
7014 List<MessagingStyle.Message> oldMs = getMessages();
7015 List<MessagingStyle.Message> newMs = newS.getMessages();
7016
Dan Sandler7d67bd42018-05-15 14:06:38 -04007017 if (oldMs == null || newMs == null) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007018 newMs = new ArrayList<>();
7019 }
7020
7021 int n = oldMs.size();
7022 if (n != newMs.size()) {
7023 return true;
7024 }
7025 for (int i = 0; i < n; i++) {
7026 MessagingStyle.Message oldM = oldMs.get(i);
7027 MessagingStyle.Message newM = newMs.get(i);
Dan Sandler7d67bd42018-05-15 14:06:38 -04007028 if (!Objects.equals(
7029 String.valueOf(oldM.getText()),
7030 String.valueOf(newM.getText()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007031 return true;
7032 }
7033 if (!Objects.equals(oldM.getDataUri(), newM.getDataUri())) {
7034 return true;
7035 }
Dan Sandler7d67bd42018-05-15 14:06:38 -04007036 String oldSender = String.valueOf(oldM.getSenderPerson() == null
7037 ? oldM.getSender()
7038 : oldM.getSenderPerson().getName());
7039 String newSender = String.valueOf(newM.getSenderPerson() == null
7040 ? newM.getSender()
7041 : newM.getSenderPerson().getName());
Julia Reynolds7217dc92018-03-07 12:12:09 -05007042 if (!Objects.equals(oldSender, newSender)) {
7043 return true;
7044 }
7045
7046 String oldKey = oldM.getSenderPerson() == null
7047 ? null : oldM.getSenderPerson().getKey();
7048 String newKey = newM.getSenderPerson() == null
7049 ? null : newM.getSenderPerson().getKey();
7050 if (!Objects.equals(oldKey, newKey)) {
7051 return true;
7052 }
7053 // Other fields (like timestamp) intentionally excluded
7054 }
7055 return false;
7056 }
7057
Adrian Roosc1a80b02016-04-05 14:54:55 -07007058 private Message findLatestIncomingMessage() {
Selim Cinek88188f22017-09-19 16:46:56 -07007059 return findLatestIncomingMessage(mMessages);
7060 }
7061
7062 /**
7063 * @hide
7064 */
7065 @Nullable
7066 public static Message findLatestIncomingMessage(
7067 List<Message> messages) {
7068 for (int i = messages.size() - 1; i >= 0; i--) {
7069 Message m = messages.get(i);
Adrian Roosc1a80b02016-04-05 14:54:55 -07007070 // Incoming messages have a non-empty sender.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007071 if (m.mSender != null && !TextUtils.isEmpty(m.mSender.getName())) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07007072 return m;
7073 }
7074 }
Selim Cinek88188f22017-09-19 16:46:56 -07007075 if (!messages.isEmpty()) {
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007076 // No incoming messages, fall back to outgoing message
Selim Cinek88188f22017-09-19 16:46:56 -07007077 return messages.get(messages.size() - 1);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007078 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07007079 return null;
7080 }
7081
7082 /**
7083 * @hide
7084 */
7085 @Override
7086 public RemoteViews makeBigContentView() {
Selim Cinek384804b2018-04-18 14:31:07 +08007087 return makeMessagingView(false /* displayImagesAtEnd */, true /* hideLargeIcon */);
Selim Cinekafeed292017-12-12 17:32:44 -08007088 }
7089
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007090 /**
7091 * Create a messaging layout.
7092 *
7093 * @param displayImagesAtEnd should images be displayed at the end of the content instead
7094 * of inline.
Selim Cinek384804b2018-04-18 14:31:07 +08007095 * @param hideRightIcons Should the reply affordance be shown at the end of the notification
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007096 * @return the created remoteView.
7097 */
Selim Cinekafeed292017-12-12 17:32:44 -08007098 @NonNull
Selim Cinek384804b2018-04-18 14:31:07 +08007099 private RemoteViews makeMessagingView(boolean displayImagesAtEnd, boolean hideRightIcons) {
Selim Cinek88188f22017-09-19 16:46:56 -07007100 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
Adrian Roosc1a80b02016-04-05 14:54:55 -07007101 ? super.mBigContentTitle
7102 : mConversationTitle;
Selim Cinekce8794f2018-05-23 16:46:05 -07007103 boolean atLeastP = mBuilder.mContext.getApplicationInfo().targetSdkVersion
7104 >= Build.VERSION_CODES.P;
7105 boolean isOneToOne;
Selim Cinek2dd3e722018-01-19 11:06:06 -08007106 CharSequence nameReplacement = null;
Selim Cinekce8794f2018-05-23 16:46:05 -07007107 Icon avatarReplacement = null;
7108 if (!atLeastP) {
7109 isOneToOne = TextUtils.isEmpty(conversationTitle);
7110 avatarReplacement = mBuilder.mN.mLargeIcon;
7111 if (hasOnlyWhiteSpaceSenders()) {
7112 isOneToOne = true;
7113 nameReplacement = conversationTitle;
7114 conversationTitle = null;
7115 }
7116 } else {
7117 isOneToOne = !isGroupConversation();
Adrian Roosb1f427c2016-05-26 12:27:15 -07007118 }
Selim Cinek384804b2018-04-18 14:31:07 +08007119 TemplateBindResult bindResult = new TemplateBindResult();
Adrian Roos48d746a2016-04-12 14:57:28 -07007120 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07007121 mBuilder.getMessagingLayoutResource(),
Selim Cinek88188f22017-09-19 16:46:56 -07007122 mBuilder.mParams.reset().hasProgress(false).title(conversationTitle).text(null)
Selim Cinek384804b2018-04-18 14:31:07 +08007123 .hideLargeIcon(hideRightIcons || isOneToOne)
7124 .hideReplyIcon(hideRightIcons)
7125 .headerTextSecondary(conversationTitle),
7126 bindResult);
Selim Cinek88188f22017-09-19 16:46:56 -07007127 addExtras(mBuilder.mN.extras);
Selim Cinekafeed292017-12-12 17:32:44 -08007128 // also update the end margin if there is an image
Selim Cinek1c72fa02018-04-23 18:00:54 +08007129 contentView.setViewLayoutMarginEnd(R.id.notification_messaging,
7130 bindResult.getIconMarginEnd());
Selim Cinek88188f22017-09-19 16:46:56 -07007131 contentView.setInt(R.id.status_bar_latest_event_content, "setLayoutColor",
Kenny Guy14d035c2018-05-02 19:10:36 +01007132 mBuilder.isColorized() ? mBuilder.getPrimaryTextColor()
7133 : mBuilder.resolveContrastColor());
7134 contentView.setInt(R.id.status_bar_latest_event_content, "setSenderTextColor",
7135 mBuilder.getPrimaryTextColor());
7136 contentView.setInt(R.id.status_bar_latest_event_content, "setMessageTextColor",
7137 mBuilder.getSecondaryTextColor());
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007138 contentView.setBoolean(R.id.status_bar_latest_event_content, "setDisplayImagesAtEnd",
7139 displayImagesAtEnd);
Selim Cinekce8794f2018-05-23 16:46:05 -07007140 contentView.setIcon(R.id.status_bar_latest_event_content, "setAvatarReplacement",
7141 avatarReplacement);
Selim Cinek2dd3e722018-01-19 11:06:06 -08007142 contentView.setCharSequence(R.id.status_bar_latest_event_content, "setNameReplacement",
7143 nameReplacement);
Selim Cinek88188f22017-09-19 16:46:56 -07007144 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsOneToOne",
7145 isOneToOne);
7146 contentView.setBundle(R.id.status_bar_latest_event_content, "setData",
7147 mBuilder.mN.extras);
Alex Hillsfc737de2016-03-23 17:33:02 -04007148 return contentView;
7149 }
7150
Selim Cinekf7409db2017-10-24 16:17:14 -07007151 private boolean hasOnlyWhiteSpaceSenders() {
7152 for (int i = 0; i < mMessages.size(); i++) {
7153 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007154 Person sender = m.getSenderPerson();
7155 if (sender != null && !isWhiteSpace(sender.getName())) {
Selim Cinekf7409db2017-10-24 16:17:14 -07007156 return false;
7157 }
7158 }
7159 return true;
7160 }
7161
7162 private boolean isWhiteSpace(CharSequence sender) {
7163 if (TextUtils.isEmpty(sender)) {
7164 return true;
7165 }
7166 if (sender.toString().matches("^\\s*$")) {
7167 return true;
7168 }
7169 // Let's check if we only have 0 whitespace chars. Some apps did this as a workaround
7170 // For the presentation that we had.
7171 for (int i = 0; i < sender.length(); i++) {
7172 char c = sender.charAt(i);
7173 if (c != '\u200B') {
7174 return false;
7175 }
7176 }
7177 return true;
7178 }
7179
Selim Cinek88188f22017-09-19 16:46:56 -07007180 private CharSequence createConversationTitleFromMessages() {
7181 ArraySet<CharSequence> names = new ArraySet<>();
7182 for (int i = 0; i < mMessages.size(); i++) {
7183 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007184 Person sender = m.getSenderPerson();
Selim Cinek88188f22017-09-19 16:46:56 -07007185 if (sender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007186 names.add(sender.getName());
Selim Cinek88188f22017-09-19 16:46:56 -07007187 }
7188 }
7189 SpannableStringBuilder title = new SpannableStringBuilder();
7190 int size = names.size();
7191 for (int i = 0; i < size; i++) {
7192 CharSequence name = names.valueAt(i);
7193 if (!TextUtils.isEmpty(title)) {
7194 title.append(", ");
7195 }
7196 title.append(BidiFormatter.getInstance().unicodeWrap(name));
7197 }
7198 return title;
7199 }
7200
Adrian Roosdedd1df2016-04-26 16:38:47 -07007201 /**
7202 * @hide
7203 */
7204 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007205 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007206 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007207 true /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007208 remoteViews.setInt(R.id.notification_messaging, "setMaxDisplayedLines", 1);
7209 return remoteViews;
Adrian Roosdedd1df2016-04-26 16:38:47 -07007210 }
7211
Adrian Roosc1a80b02016-04-05 14:54:55 -07007212 private static TextAppearanceSpan makeFontColorSpan(int color) {
7213 return new TextAppearanceSpan(null, 0, 0,
7214 ColorStateList.valueOf(color), null);
7215 }
7216
Alex Hillsd9b04d92016-04-11 16:38:16 -04007217 public static final class Message {
Tony Mak09db2ea2018-06-27 18:12:48 +01007218 /** @hide */
7219 public static final String KEY_TEXT = "text";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007220 static final String KEY_TIMESTAMP = "time";
7221 static final String KEY_SENDER = "sender";
Selim Cinekcb8b9852017-12-15 18:01:52 -08007222 static final String KEY_SENDER_PERSON = "sender_person";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007223 static final String KEY_DATA_MIME_TYPE = "type";
7224 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08007225 static final String KEY_EXTRAS_BUNDLE = "extras";
Kenny Guya0f6de82018-04-06 16:20:16 +01007226 static final String KEY_REMOTE_INPUT_HISTORY = "remote_input_history";
Alex Hillsfc737de2016-03-23 17:33:02 -04007227
7228 private final CharSequence mText;
7229 private final long mTimestamp;
Selim Cinekcb8b9852017-12-15 18:01:52 -08007230 @Nullable
7231 private final Person mSender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007232 /** True if this message was generated from the extra
7233 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}
7234 */
7235 private final boolean mRemoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007236
Shane Brennan5a871862017-03-11 13:14:17 -08007237 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007238 private String mDataMimeType;
7239 private Uri mDataUri;
7240
7241 /**
7242 * Constructor
7243 * @param text A {@link CharSequence} to be displayed as the message content
7244 * @param timestamp Time at which the message arrived
7245 * @param sender A {@link CharSequence} to be used for displaying the name of the
7246 * sender. Should be <code>null</code> for messages by the current user, in which case
7247 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
7248 * Should be unique amongst all individuals in the conversation, and should be
7249 * consistent during re-posts of the notification.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007250 *
7251 * @deprecated use {@code Message(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04007252 */
7253 public Message(CharSequence text, long timestamp, CharSequence sender){
Selim Cinek9acd6732018-03-23 16:39:02 -07007254 this(text, timestamp, sender == null ? null
7255 : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08007256 }
7257
7258 /**
7259 * Constructor
7260 * @param text A {@link CharSequence} to be displayed as the message content
7261 * @param timestamp Time at which the message arrived
7262 * @param sender The {@link Person} who sent the message.
7263 * Should be <code>null</code> for messages by the current user, in which case
7264 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7265 * <p>
Selim Cinek9acd6732018-03-23 16:39:02 -07007266 * The person provided should contain an Icon, set with
7267 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7268 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7269 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7270 * to differentiate between the different users.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007271 * </p>
7272 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007273 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender) {
Kenny Guya0f6de82018-04-06 16:20:16 +01007274 this(text, timestamp, sender, false /* remoteHistory */);
7275 }
7276
7277 /**
7278 * Constructor
7279 * @param text A {@link CharSequence} to be displayed as the message content
7280 * @param timestamp Time at which the message arrived
7281 * @param sender The {@link Person} who sent the message.
7282 * Should be <code>null</code> for messages by the current user, in which case
7283 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7284 * @param remoteInputHistory True if the messages was generated from the extra
7285 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7286 * <p>
7287 * The person provided should contain an Icon, set with
7288 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7289 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7290 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7291 * to differentiate between the different users.
7292 * </p>
7293 * @hide
7294 */
7295 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender,
7296 boolean remoteInputHistory) {
Alex Hillsfc737de2016-03-23 17:33:02 -04007297 mText = text;
7298 mTimestamp = timestamp;
7299 mSender = sender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007300 mRemoteInputHistory = remoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007301 }
7302
7303 /**
7304 * Sets a binary blob of data and an associated MIME type for a message. In the case
7305 * where the platform doesn't support the MIME type, the original text provided in the
7306 * constructor will be used.
7307 * @param dataMimeType The MIME type of the content. See
7308 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
7309 * types on Android and Android Wear.
7310 * @param dataUri The uri containing the content whose type is given by the MIME type.
7311 * <p class="note">
7312 * <ol>
7313 * <li>Notification Listeners including the System UI need permission to access the
7314 * data the Uri points to. The recommended ways to do this are:</li>
7315 * <li>Store the data in your own ContentProvider, making sure that other apps have
7316 * the correct permission to access your provider. The preferred mechanism for
7317 * providing access is to use per-URI permissions which are temporary and only
7318 * grant access to the receiving application. An easy way to create a
7319 * ContentProvider like this is to use the FileProvider helper class.</li>
7320 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
7321 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
7322 * also store non-media types (see MediaStore.Files for more info). Files can be
7323 * inserted into the MediaStore using scanFile() after which a content:// style
7324 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
7325 * Note that once added to the system MediaStore the content is accessible to any
7326 * app on the device.</li>
7327 * </ol>
7328 * @return this object for method chaining
7329 */
7330 public Message setData(String dataMimeType, Uri dataUri) {
7331 mDataMimeType = dataMimeType;
7332 mDataUri = dataUri;
7333 return this;
7334 }
7335
Alex Hillsfc737de2016-03-23 17:33:02 -04007336 /**
7337 * Get the text to be used for this message, or the fallback text if a type and content
7338 * Uri have been set
7339 */
7340 public CharSequence getText() {
7341 return mText;
7342 }
7343
7344 /**
7345 * Get the time at which this message arrived
7346 */
7347 public long getTimestamp() {
7348 return mTimestamp;
7349 }
7350
7351 /**
Shane Brennan5a871862017-03-11 13:14:17 -08007352 * Get the extras Bundle for this message.
7353 */
7354 public Bundle getExtras() {
7355 return mExtras;
7356 }
7357
7358 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007359 * Get the text used to display the contact's name in the messaging experience
Selim Cinekcb8b9852017-12-15 18:01:52 -08007360 *
7361 * @deprecated use {@link #getSenderPerson()}
Alex Hillsfc737de2016-03-23 17:33:02 -04007362 */
7363 public CharSequence getSender() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007364 return mSender == null ? null : mSender.getName();
7365 }
7366
7367 /**
7368 * Get the sender associated with this message.
7369 */
7370 @Nullable
7371 public Person getSenderPerson() {
Alex Hillsfc737de2016-03-23 17:33:02 -04007372 return mSender;
7373 }
7374
7375 /**
7376 * Get the MIME type of the data pointed to by the Uri
7377 */
7378 public String getDataMimeType() {
7379 return mDataMimeType;
7380 }
7381
7382 /**
koprivaa1a78482018-10-09 10:09:23 -07007383 * Get the Uri pointing to the content of the message. Can be null, in which case
Alex Hillsfc737de2016-03-23 17:33:02 -04007384 * {@see #getText()} is used.
7385 */
7386 public Uri getDataUri() {
7387 return mDataUri;
7388 }
7389
Kenny Guya0f6de82018-04-06 16:20:16 +01007390 /**
7391 * @return True if the message was generated from
7392 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7393 * @hide
7394 */
7395 public boolean isRemoteInputHistory() {
7396 return mRemoteInputHistory;
7397 }
7398
Alex Hillsd9b04d92016-04-11 16:38:16 -04007399 private Bundle toBundle() {
7400 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007401 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007402 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04007403 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007404 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04007405 if (mSender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007406 // Legacy listeners need this
7407 bundle.putCharSequence(KEY_SENDER, mSender.getName());
7408 bundle.putParcelable(KEY_SENDER_PERSON, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04007409 }
7410 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007411 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04007412 }
7413 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007414 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04007415 }
Shane Brennan5a871862017-03-11 13:14:17 -08007416 if (mExtras != null) {
7417 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
7418 }
Kenny Guya0f6de82018-04-06 16:20:16 +01007419 if (mRemoteInputHistory) {
7420 bundle.putBoolean(KEY_REMOTE_INPUT_HISTORY, mRemoteInputHistory);
7421 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007422 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04007423 }
7424
Alex Hillsd9b04d92016-04-11 16:38:16 -04007425 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
7426 Bundle[] bundles = new Bundle[messages.size()];
7427 final int N = messages.size();
7428 for (int i = 0; i < N; i++) {
7429 bundles[i] = messages.get(i).toBundle();
7430 }
7431 return bundles;
7432 }
7433
Selim Cinek88188f22017-09-19 16:46:56 -07007434 /**
7435 * @return A list of messages read from the bundles.
7436 *
7437 * @hide
7438 */
7439 public static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
7440 if (bundles == null) {
7441 return new ArrayList<>();
7442 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007443 List<Message> messages = new ArrayList<>(bundles.length);
7444 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07007445 if (bundles[i] instanceof Bundle) {
7446 Message message = getMessageFromBundle((Bundle)bundles[i]);
7447 if (message != null) {
7448 messages.add(message);
7449 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007450 }
7451 }
7452 return messages;
7453 }
7454
Selim Cinekb0dc61b2018-05-22 18:49:36 -07007455 /**
7456 * @return The message that is stored in the bundle or null if the message couldn't be
7457 * resolved.
7458 *
7459 * @hide
7460 */
7461 @Nullable
7462 public static Message getMessageFromBundle(Bundle bundle) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007463 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07007464 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007465 return null;
7466 } else {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007467
7468 Person senderPerson = bundle.getParcelable(KEY_SENDER_PERSON);
7469 if (senderPerson == null) {
7470 // Legacy apps that use compat don't actually provide the sender objects
7471 // We need to fix the compat version to provide people / use
7472 // the native api instead
7473 CharSequence senderName = bundle.getCharSequence(KEY_SENDER);
7474 if (senderName != null) {
Selim Cinek9acd6732018-03-23 16:39:02 -07007475 senderPerson = new Person.Builder().setName(senderName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007476 }
7477 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007478 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
Selim Cinekcb8b9852017-12-15 18:01:52 -08007479 bundle.getLong(KEY_TIMESTAMP),
Kenny Guya0f6de82018-04-06 16:20:16 +01007480 senderPerson,
7481 bundle.getBoolean(KEY_REMOTE_INPUT_HISTORY, false));
Alex Hillsd9b04d92016-04-11 16:38:16 -04007482 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
7483 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007484 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
7485 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04007486 }
Shane Brennan5a871862017-03-11 13:14:17 -08007487 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
7488 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
7489 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007490 return message;
7491 }
7492 } catch (ClassCastException e) {
7493 return null;
7494 }
7495 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007496 }
7497 }
7498
7499 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04007500 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08007501 *
Robert Ly91c5ce32014-06-08 15:37:00 -07007502 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04007503 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07007504 * Notification notif = new Notification.Builder(mContext)
7505 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
7506 * .setContentText(subject)
7507 * .setSmallIcon(R.drawable.new_mail)
7508 * .setLargeIcon(aBitmap)
7509 * .setStyle(new Notification.InboxStyle()
7510 * .addLine(str1)
7511 * .addLine(str2)
7512 * .setContentTitle(&quot;&quot;)
7513 * .setSummaryText(&quot;+3 more&quot;))
7514 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04007515 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08007516 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04007517 * @see Notification#bigContentView
7518 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007519 public static class InboxStyle extends Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07007520
7521 /**
7522 * The number of lines of remote input history allowed until we start reducing lines.
7523 */
7524 private static final int NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION = 1;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007525 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
7526
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007527 public InboxStyle() {
7528 }
7529
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007530 /**
7531 * @deprecated use {@code InboxStyle()}.
7532 */
7533 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04007534 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007535 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04007536 }
7537
Chris Wrend6297db2012-05-03 16:20:13 -04007538 /**
7539 * Overrides ContentTitle in the big form of the template.
7540 * This defaults to the value passed to setContentTitle().
7541 */
7542 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007543 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04007544 return this;
7545 }
7546
7547 /**
7548 * Set the first line of text after the detail section in the big form of the template.
7549 */
7550 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007551 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04007552 return this;
7553 }
7554
Chris Wren0bd664d2012-08-01 13:56:56 -04007555 /**
7556 * Append a line to the digest section of the Inbox notification.
7557 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04007558 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007559 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04007560 return this;
7561 }
7562
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007563 /**
7564 * @hide
7565 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05007566 public ArrayList<CharSequence> getLines() {
7567 return mTexts;
7568 }
7569
7570 /**
7571 * @hide
7572 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007573 public void addExtras(Bundle extras) {
7574 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007575
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007576 CharSequence[] a = new CharSequence[mTexts.size()];
7577 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
7578 }
7579
Christoph Studer4600f9b2014-07-22 22:44:43 +02007580 /**
7581 * @hide
7582 */
7583 @Override
7584 protected void restoreFromExtras(Bundle extras) {
7585 super.restoreFromExtras(extras);
7586
7587 mTexts.clear();
7588 if (extras.containsKey(EXTRA_TEXT_LINES)) {
7589 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
7590 }
7591 }
7592
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007593 /**
7594 * @hide
7595 */
7596 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08007597 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02007598 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007599 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
7600 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007601
Selim Cinek384804b2018-04-18 14:31:07 +08007602 TemplateBindResult result = new TemplateBindResult();
7603 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource(), result);
Daniel Sandler619738c2012-06-07 16:33:08 -04007604
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007605 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007606
Chris Wrend6297db2012-05-03 16:20:13 -04007607 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 -04007608 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04007609
Chris Wren4ed80d52012-05-17 09:30:03 -04007610 // Make sure all rows are gone in case we reuse a view.
7611 for (int rowId : rowIds) {
7612 contentView.setViewVisibility(rowId, View.GONE);
7613 }
7614
Daniel Sandler879c5e02012-04-17 16:46:51 -04007615 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07007616 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7617 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08007618 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07007619 int onlyViewId = 0;
7620 int maxRows = rowIds.length;
7621 if (mBuilder.mActions.size() > 0) {
7622 maxRows--;
7623 }
Selim Cinekbee4e072018-05-21 22:06:43 -07007624 CharSequence[] remoteInputHistory = mBuilder.mN.extras.getCharSequenceArray(
7625 EXTRA_REMOTE_INPUT_HISTORY);
7626 if (remoteInputHistory != null
7627 && remoteInputHistory.length > NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION) {
7628 // Let's remove some messages to make room for the remote input history.
7629 // 1 is always able to fit, but let's remove them if they are 2 or 3
7630 int numRemoteInputs = Math.min(remoteInputHistory.length,
7631 MAX_REMOTE_INPUT_HISTORY_LINES);
7632 int totalNumRows = mTexts.size() + numRemoteInputs
7633 - NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION;
7634 if (totalNumRows > maxRows) {
7635 int overflow = totalNumRows - maxRows;
7636 if (mTexts.size() > maxRows) {
7637 // Heuristic: if the Texts don't fit anyway, we'll rather drop the last
7638 // few messages, even with the remote input
7639 maxRows -= overflow;
7640 } else {
7641 // otherwise we drop the first messages
7642 i = overflow;
7643 }
7644 }
7645 }
Selim Cinek07c80172016-04-21 16:40:47 -07007646 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007647 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07007648 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007649 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07007650 contentView.setTextViewText(rowIds[i],
7651 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08007652 mBuilder.setTextViewColorSecondary(contentView, rowIds[i]);
Selim Cinek07c80172016-04-21 16:40:47 -07007653 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek384804b2018-04-18 14:31:07 +08007654 handleInboxImageMargin(contentView, rowIds[i], first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007655 result.getIconMarginEnd());
Selim Cinek07c80172016-04-21 16:40:47 -07007656 if (first) {
7657 onlyViewId = rowIds[i];
7658 } else {
7659 onlyViewId = 0;
7660 }
Selim Cinek247fa012016-02-18 09:50:48 -08007661 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007662 }
7663 i++;
7664 }
Selim Cinek07c80172016-04-21 16:40:47 -07007665 if (onlyViewId != 0) {
7666 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
7667 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7668 R.dimen.notification_text_margin_top);
7669 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
7670 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007671
Daniel Sandler879c5e02012-04-17 16:46:51 -04007672 return contentView;
7673 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007674
Julia Reynolds7217dc92018-03-07 12:12:09 -05007675 /**
7676 * @hide
7677 */
7678 @Override
7679 public boolean areNotificationsVisiblyDifferent(Style other) {
7680 if (other == null || getClass() != other.getClass()) {
7681 return true;
7682 }
7683 InboxStyle newS = (InboxStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04007684
7685 final ArrayList<CharSequence> myLines = getLines();
7686 final ArrayList<CharSequence> newLines = newS.getLines();
7687 final int n = myLines.size();
7688 if (n != newLines.size()) {
7689 return true;
7690 }
7691
7692 for (int i = 0; i < n; i++) {
7693 if (!Objects.equals(
7694 String.valueOf(myLines.get(i)),
7695 String.valueOf(newLines.get(i)))) {
7696 return true;
7697 }
7698 }
7699 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05007700 }
7701
Selim Cinek384804b2018-04-18 14:31:07 +08007702 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007703 int marginEndValue) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08007704 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08007705 if (first) {
7706 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
7707 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
7708 boolean hasProgress = max != 0 || ind;
Selim Cinek384804b2018-04-18 14:31:07 +08007709 if (!hasProgress) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08007710 endMargin = marginEndValue;
Selim Cinek247fa012016-02-18 09:50:48 -08007711 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007712 }
Selim Cinek1c72fa02018-04-23 18:00:54 +08007713 contentView.setViewLayoutMarginEnd(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08007714 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04007715 }
Dan Sandler842dd772014-05-15 09:36:47 -04007716
7717 /**
7718 * Notification style for media playback notifications.
7719 *
7720 * In the expanded form, {@link Notification#bigContentView}, up to 5
7721 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04007722 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04007723 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
7724 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
7725 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08007726 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04007727 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
7728 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01007729 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04007730 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08007731 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007732 * Notifications created with MediaStyle will have their category set to
7733 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
7734 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08007735 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07007736 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
7737 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04007738 * the System UI can identify this as a notification representing an active media session
7739 * and respond accordingly (by showing album artwork in the lockscreen, for example).
7740 *
Selim Cinek99104832017-01-25 14:47:33 -08007741 * <p>
7742 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
7743 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
7744 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
7745 * <p>
7746 *
Dan Sandler842dd772014-05-15 09:36:47 -04007747 * To use this style with your Notification, feed it to
7748 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7749 * <pre class="prettyprint">
7750 * Notification noti = new Notification.Builder()
7751 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01007752 * .setContentTitle(&quot;Track title&quot;)
7753 * .setContentText(&quot;Artist - Album&quot;)
7754 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007755 * .setStyle(<b>new Notification.MediaStyle()</b>
7756 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04007757 * .build();
7758 * </pre>
7759 *
7760 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08007761 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04007762 */
7763 public static class MediaStyle extends Style {
Gus Prevas9cc96602018-10-11 11:24:23 -04007764 // Changing max media buttons requires also changing templates
7765 // (notification_template_material_media and notification_template_material_big_media).
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007766 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04007767 static final int MAX_MEDIA_BUTTONS = 5;
Gus Prevas9cc96602018-10-11 11:24:23 -04007768 @IdRes private static final int[] MEDIA_BUTTON_IDS = {
7769 R.id.action0,
7770 R.id.action1,
7771 R.id.action2,
7772 R.id.action3,
7773 R.id.action4,
7774 };
Dan Sandler842dd772014-05-15 09:36:47 -04007775
7776 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07007777 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04007778
7779 public MediaStyle() {
7780 }
7781
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007782 /**
7783 * @deprecated use {@code MediaStyle()}.
7784 */
7785 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04007786 public MediaStyle(Builder builder) {
7787 setBuilder(builder);
7788 }
7789
7790 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007791 * 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 -04007792 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007793 *
7794 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04007795 */
7796 public MediaStyle setShowActionsInCompactView(int...actions) {
7797 mActionsToShowInCompact = actions;
7798 return this;
7799 }
7800
7801 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07007802 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
7803 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04007804 */
Jeff Browndba34ba2014-06-24 20:46:03 -07007805 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04007806 mToken = token;
7807 return this;
7808 }
7809
Christoph Studer4600f9b2014-07-22 22:44:43 +02007810 /**
7811 * @hide
7812 */
Dan Sandler842dd772014-05-15 09:36:47 -04007813 @Override
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007814 @UnsupportedAppUsage
Dan Sandler842dd772014-05-15 09:36:47 -04007815 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02007816 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007817 if (wip.category == null) {
7818 wip.category = Notification.CATEGORY_TRANSPORT;
7819 }
Dan Sandler842dd772014-05-15 09:36:47 -04007820 return wip;
7821 }
7822
Christoph Studer4600f9b2014-07-22 22:44:43 +02007823 /**
7824 * @hide
7825 */
7826 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007827 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007828 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007829 }
7830
7831 /**
7832 * @hide
7833 */
7834 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007835 public RemoteViews makeBigContentView() {
7836 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007837 }
7838
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007839 /**
7840 * @hide
7841 */
7842 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007843 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007844 RemoteViews expanded = makeMediaBigContentView();
7845 return expanded != null ? expanded : makeMediaContentView();
7846 }
7847
Dan Sandler842dd772014-05-15 09:36:47 -04007848 /** @hide */
7849 @Override
7850 public void addExtras(Bundle extras) {
7851 super.addExtras(extras);
7852
7853 if (mToken != null) {
7854 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
7855 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01007856 if (mActionsToShowInCompact != null) {
7857 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
7858 }
Dan Sandler842dd772014-05-15 09:36:47 -04007859 }
7860
Christoph Studer4600f9b2014-07-22 22:44:43 +02007861 /**
7862 * @hide
7863 */
7864 @Override
7865 protected void restoreFromExtras(Bundle extras) {
7866 super.restoreFromExtras(extras);
7867
7868 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
7869 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
7870 }
7871 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
7872 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
7873 }
7874 }
7875
Julia Reynolds7217dc92018-03-07 12:12:09 -05007876 /**
7877 * @hide
7878 */
7879 @Override
7880 public boolean areNotificationsVisiblyDifferent(Style other) {
7881 if (other == null || getClass() != other.getClass()) {
7882 return true;
7883 }
7884 // All fields to compare are on the Notification object
7885 return false;
7886 }
7887
Gus Prevas9cc96602018-10-11 11:24:23 -04007888 private void bindMediaActionButton(RemoteViews container, @IdRes int buttonId,
7889 Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04007890 final boolean tombstone = (action.actionIntent == null);
Gus Prevas9cc96602018-10-11 11:24:23 -04007891 container.setViewVisibility(buttonId, View.VISIBLE);
7892 container.setImageViewIcon(buttonId, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07007893
7894 // If the action buttons should not be tinted, then just use the default
7895 // notification color. Otherwise, just use the passed-in color.
Gus Prevas9cc96602018-10-11 11:24:23 -04007896 Resources resources = mBuilder.mContext.getResources();
7897 Configuration currentConfig = resources.getConfiguration();
Lucas Dupinf03e7522018-06-25 16:21:13 -07007898 boolean inNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
7899 == Configuration.UI_MODE_NIGHT_YES;
Anthony Chenad4d1582017-04-10 16:07:58 -07007900 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized()
7901 ? color
Lucas Dupina291d192018-06-07 13:59:42 -07007902 : ContrastColorUtil.resolveColor(mBuilder.mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07007903 Notification.COLOR_DEFAULT, inNightMode);
Anthony Chenad4d1582017-04-10 16:07:58 -07007904
Gus Prevas9cc96602018-10-11 11:24:23 -04007905 container.setDrawableTint(buttonId, false, tintColor,
Sunny Goyal5b153922017-09-21 21:00:36 -07007906 PorterDuff.Mode.SRC_ATOP);
Gus Prevas9cc96602018-10-11 11:24:23 -04007907
7908 final TypedArray typedArray = mBuilder.mContext.obtainStyledAttributes(
7909 new int[]{ android.R.attr.colorControlHighlight });
7910 int rippleAlpha = Color.alpha(typedArray.getColor(0, 0));
7911 typedArray.recycle();
7912 int rippleColor = Color.argb(rippleAlpha, Color.red(tintColor), Color.green(tintColor),
7913 Color.blue(tintColor));
7914 container.setRippleDrawableColor(buttonId, ColorStateList.valueOf(rippleColor));
7915
Dan Sandler842dd772014-05-15 09:36:47 -04007916 if (!tombstone) {
Gus Prevas9cc96602018-10-11 11:24:23 -04007917 container.setOnClickPendingIntent(buttonId, action.actionIntent);
Dan Sandler842dd772014-05-15 09:36:47 -04007918 }
Gus Prevas9cc96602018-10-11 11:24:23 -04007919 container.setContentDescription(buttonId, action.title);
Dan Sandler842dd772014-05-15 09:36:47 -04007920 }
7921
7922 private RemoteViews makeMediaContentView() {
7923 RemoteViews view = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08007924 R.layout.notification_template_material_media, false, /* hasProgress */
7925 null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04007926
7927 final int numActions = mBuilder.mActions.size();
Gus Prevas9cc96602018-10-11 11:24:23 -04007928 final int numActionsToShow = mActionsToShowInCompact == null
Dan Sandler842dd772014-05-15 09:36:47 -04007929 ? 0
7930 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Gus Prevas9cc96602018-10-11 11:24:23 -04007931 if (numActionsToShow > numActions) {
7932 throw new IllegalArgumentException(String.format(
7933 "setShowActionsInCompactView: action %d out of bounds (max %d)",
7934 numActions, numActions - 1));
7935 }
7936 for (int i = 0; i < MAX_MEDIA_BUTTONS_IN_COMPACT; i++) {
7937 if (i < numActionsToShow) {
Dan Sandler842dd772014-05-15 09:36:47 -04007938 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Gus Prevas9cc96602018-10-11 11:24:23 -04007939 bindMediaActionButton(view, MEDIA_BUTTON_IDS[i], action, getActionColor());
7940 } else {
7941 view.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04007942 }
7943 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08007944 handleImage(view);
7945 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007946 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07007947 if (mBuilder.mN.hasLargeIcon()) {
Selim Cinek384804b2018-04-18 14:31:07 +08007948 endMargin = R.dimen.notification_media_image_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08007949 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007950 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04007951 return view;
7952 }
7953
Selim Cinekc7f5a822018-03-20 19:32:06 -07007954 private int getActionColor() {
7955 return mBuilder.isColorized() ? mBuilder.getPrimaryTextColor()
7956 : mBuilder.resolveContrastColor();
Selim Cinek99104832017-01-25 14:47:33 -08007957 }
7958
Dan Sandler842dd772014-05-15 09:36:47 -04007959 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007960 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007961 // Dont add an expanded view if there is no more content to be revealed
7962 int actionsInCompact = mActionsToShowInCompact == null
7963 ? 0
7964 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07007965 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007966 return null;
7967 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05007968 RemoteViews big = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08007969 R.layout.notification_template_material_big_media, false, null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04007970
Gus Prevas9cc96602018-10-11 11:24:23 -04007971 for (int i = 0; i < MAX_MEDIA_BUTTONS; i++) {
7972 if (i < actionCount) {
7973 bindMediaActionButton(big, MEDIA_BUTTON_IDS[i], mBuilder.mActions.get(i),
Selim Cinekc7f5a822018-03-20 19:32:06 -07007974 getActionColor());
Gus Prevas9cc96602018-10-11 11:24:23 -04007975 } else {
7976 big.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04007977 }
7978 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05007979 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04007980 return big;
7981 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007982
Selim Cinek5bf069a2015-11-10 19:14:27 -05007983 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07007984 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007985 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
7986 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007987 }
7988 }
7989
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007990 /**
7991 * @hide
7992 */
7993 @Override
7994 protected boolean hasProgress() {
7995 return false;
7996 }
Dan Sandler842dd772014-05-15 09:36:47 -04007997 }
Griff Hazen61a9e862014-05-22 16:05:19 -07007998
Selim Cinek593610c2016-02-16 18:42:57 -08007999 /**
8000 * Notification style for custom views that are decorated by the system
8001 *
8002 * <p>Instead of providing a notification that is completely custom, a developer can set this
8003 * style and still obtain system decorations like the notification header with the expand
8004 * affordance and actions.
8005 *
8006 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8007 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8008 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8009 * corresponding custom views to display.
8010 *
8011 * To use this style with your Notification, feed it to
8012 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8013 * <pre class="prettyprint">
8014 * Notification noti = new Notification.Builder()
8015 * .setSmallIcon(R.drawable.ic_stat_player)
8016 * .setLargeIcon(albumArtBitmap))
8017 * .setCustomContentView(contentView);
8018 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
8019 * .build();
8020 * </pre>
8021 */
8022 public static class DecoratedCustomViewStyle extends Style {
8023
8024 public DecoratedCustomViewStyle() {
8025 }
8026
Selim Cinek593610c2016-02-16 18:42:57 -08008027 /**
8028 * @hide
8029 */
8030 public boolean displayCustomViewInline() {
8031 return true;
8032 }
8033
8034 /**
8035 * @hide
8036 */
8037 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008038 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008039 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
8040 }
8041
8042 /**
8043 * @hide
8044 */
8045 @Override
8046 public RemoteViews makeBigContentView() {
8047 return makeDecoratedBigContentView();
8048 }
8049
8050 /**
8051 * @hide
8052 */
8053 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008054 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008055 return makeDecoratedHeadsUpContentView();
8056 }
8057
Selim Cinek593610c2016-02-16 18:42:57 -08008058 private RemoteViews makeDecoratedHeadsUpContentView() {
8059 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
8060 ? mBuilder.mN.contentView
8061 : mBuilder.mN.headsUpContentView;
8062 if (mBuilder.mActions.size() == 0) {
8063 return makeStandardTemplateWithCustomContent(headsUpContentView);
8064 }
Selim Cinek384804b2018-04-18 14:31:07 +08008065 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008066 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008067 mBuilder.getBigBaseLayoutResource(), result);
8068 buildIntoRemoteViewContent(remoteViews, headsUpContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008069 return remoteViews;
8070 }
8071
Selim Cinek593610c2016-02-16 18:42:57 -08008072 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
Selim Cinek384804b2018-04-18 14:31:07 +08008073 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008074 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08008075 mBuilder.getBaseLayoutResource(), result);
8076 buildIntoRemoteViewContent(remoteViews, customContent, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008077 return remoteViews;
8078 }
8079
Selim Cinek593610c2016-02-16 18:42:57 -08008080 private RemoteViews makeDecoratedBigContentView() {
8081 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
8082 ? mBuilder.mN.contentView
8083 : mBuilder.mN.bigContentView;
8084 if (mBuilder.mActions.size() == 0) {
8085 return makeStandardTemplateWithCustomContent(bigContentView);
8086 }
Selim Cinek384804b2018-04-18 14:31:07 +08008087 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008088 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008089 mBuilder.getBigBaseLayoutResource(), result);
8090 buildIntoRemoteViewContent(remoteViews, bigContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008091 return remoteViews;
8092 }
Selim Cinek247fa012016-02-18 09:50:48 -08008093
8094 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
Selim Cinek384804b2018-04-18 14:31:07 +08008095 RemoteViews customContent, TemplateBindResult result) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08008096 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008097 // Need to clone customContent before adding, because otherwise it can no longer be
8098 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008099 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07008100 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
8101 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Selim Cinekfc8073c2017-08-16 17:50:20 -07008102 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08008103 }
Selim Cinek247fa012016-02-18 09:50:48 -08008104 // also update the end margin if there is an image
Selim Cinek384804b2018-04-18 14:31:07 +08008105 Resources resources = mBuilder.mContext.getResources();
8106 int endMargin = resources.getDimensionPixelSize(
Selim Cinek1c72fa02018-04-23 18:00:54 +08008107 R.dimen.notification_content_margin_end) + result.getIconMarginEnd();
Selim Cinek384804b2018-04-18 14:31:07 +08008108 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08008109 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05008110
8111 /**
8112 * @hide
8113 */
8114 @Override
8115 public boolean areNotificationsVisiblyDifferent(Style other) {
8116 if (other == null || getClass() != other.getClass()) {
8117 return true;
8118 }
8119 // Comparison done for all custom RemoteViews, independent of style
8120 return false;
8121 }
Selim Cinek593610c2016-02-16 18:42:57 -08008122 }
8123
Selim Cinek03eb3b72016-02-18 10:39:45 -08008124 /**
8125 * Notification style for media custom views that are decorated by the system
8126 *
8127 * <p>Instead of providing a media notification that is completely custom, a developer can set
8128 * this style and still obtain system decorations like the notification header with the expand
8129 * affordance and actions.
8130 *
8131 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8132 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8133 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8134 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08008135 * <p>
8136 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
8137 * notification by using {@link Notification.Builder#setColorized(boolean)}.
8138 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08008139 * To use this style with your Notification, feed it to
8140 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8141 * <pre class="prettyprint">
8142 * Notification noti = new Notification.Builder()
8143 * .setSmallIcon(R.drawable.ic_stat_player)
8144 * .setLargeIcon(albumArtBitmap))
8145 * .setCustomContentView(contentView);
8146 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
8147 * .setMediaSession(mySession))
8148 * .build();
8149 * </pre>
8150 *
8151 * @see android.app.Notification.DecoratedCustomViewStyle
8152 * @see android.app.Notification.MediaStyle
8153 */
8154 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
8155
8156 public DecoratedMediaCustomViewStyle() {
8157 }
8158
Selim Cinek03eb3b72016-02-18 10:39:45 -08008159 /**
8160 * @hide
8161 */
8162 public boolean displayCustomViewInline() {
8163 return true;
8164 }
8165
8166 /**
8167 * @hide
8168 */
8169 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008170 public RemoteViews makeContentView(boolean increasedHeight) {
8171 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008172 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8173 mBuilder.mN.contentView);
8174 }
8175
8176 /**
8177 * @hide
8178 */
8179 @Override
8180 public RemoteViews makeBigContentView() {
8181 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
8182 ? mBuilder.mN.bigContentView
8183 : mBuilder.mN.contentView;
8184 return makeBigContentViewWithCustomContent(customRemoteView);
8185 }
8186
8187 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
8188 RemoteViews remoteViews = super.makeBigContentView();
8189 if (remoteViews != null) {
8190 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
8191 customRemoteView);
8192 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08008193 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008194 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8195 customRemoteView);
8196 } else {
8197 return null;
8198 }
8199 }
8200
8201 /**
8202 * @hide
8203 */
8204 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008205 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08008206 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
8207 ? mBuilder.mN.headsUpContentView
8208 : mBuilder.mN.contentView;
8209 return makeBigContentViewWithCustomContent(customRemoteView);
8210 }
8211
Julia Reynolds7217dc92018-03-07 12:12:09 -05008212 /**
8213 * @hide
8214 */
8215 @Override
8216 public boolean areNotificationsVisiblyDifferent(Style other) {
8217 if (other == null || getClass() != other.getClass()) {
8218 return true;
8219 }
8220 // Comparison done for all custom RemoteViews, independent of style
8221 return false;
8222 }
8223
Selim Cinek03eb3b72016-02-18 10:39:45 -08008224 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
8225 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08008226 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008227 // Need to clone customContent before adding, because otherwise it can no longer be
8228 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008229 customContent = customContent.clone();
Selim Cinek87c31532017-08-18 18:53:44 -07008230 customContent.overrideTextColors(mBuilder.getPrimaryTextColor());
Selim Cinekf91017e2016-03-14 12:25:09 -07008231 remoteViews.removeAllViews(id);
8232 remoteViews.addView(id, customContent);
Selim Cinekfc8073c2017-08-16 17:50:20 -07008233 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08008234 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08008235 return remoteViews;
8236 }
8237 }
8238
Christoph Studer4600f9b2014-07-22 22:44:43 +02008239 // When adding a new Style subclass here, don't forget to update
8240 // Builder.getNotificationStyleClass.
8241
Griff Hazen61a9e862014-05-22 16:05:19 -07008242 /**
8243 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
8244 * metadata or change options on a notification builder.
8245 */
8246 public interface Extender {
8247 /**
8248 * Apply this extender to a notification builder.
8249 * @param builder the builder to be modified.
8250 * @return the build object for chaining.
8251 */
8252 public Builder extend(Builder builder);
8253 }
8254
8255 /**
8256 * Helper class to add wearable extensions to notifications.
8257 * <p class="note"> See
8258 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
8259 * for Android Wear</a> for more information on how to use this class.
8260 * <p>
8261 * To create a notification with wearable extensions:
8262 * <ol>
8263 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
8264 * properties.
8265 * <li>Create a {@link android.app.Notification.WearableExtender}.
8266 * <li>Set wearable-specific properties using the
8267 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
8268 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
8269 * notification.
8270 * <li>Post the notification to the notification system with the
8271 * {@code NotificationManager.notify(...)} methods.
8272 * </ol>
8273 *
8274 * <pre class="prettyprint">
8275 * Notification notif = new Notification.Builder(mContext)
8276 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
8277 * .setContentText(subject)
8278 * .setSmallIcon(R.drawable.new_mail)
8279 * .extend(new Notification.WearableExtender()
8280 * .setContentIcon(R.drawable.new_mail))
8281 * .build();
8282 * NotificationManager notificationManger =
8283 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
8284 * notificationManger.notify(0, notif);</pre>
8285 *
8286 * <p>Wearable extensions can be accessed on an existing notification by using the
8287 * {@code WearableExtender(Notification)} constructor,
8288 * and then using the {@code get} methods to access values.
8289 *
8290 * <pre class="prettyprint">
8291 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
8292 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07008293 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008294 */
8295 public static final class WearableExtender implements Extender {
8296 /**
8297 * Sentinel value for an action index that is unset.
8298 */
8299 public static final int UNSET_ACTION_INDEX = -1;
8300
8301 /**
8302 * Size value for use with {@link #setCustomSizePreset} to show this notification with
8303 * default sizing.
8304 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07008305 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07008306 * on their content.
Gus Prevasd7363752018-09-18 14:35:15 -04008307 *
8308 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008309 */
Gus Prevasd7363752018-09-18 14:35:15 -04008310 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008311 public static final int SIZE_DEFAULT = 0;
8312
8313 /**
8314 * Size value for use with {@link #setCustomSizePreset} to show this notification
8315 * with an extra small size.
8316 * <p>This value is only applicable for custom display notifications created using
8317 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008318 *
8319 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008320 */
Gus Prevasd7363752018-09-18 14:35:15 -04008321 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008322 public static final int SIZE_XSMALL = 1;
8323
8324 /**
8325 * Size value for use with {@link #setCustomSizePreset} to show this notification
8326 * with a small size.
8327 * <p>This value is only applicable for custom display notifications created using
8328 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008329 *
8330 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008331 */
Gus Prevasd7363752018-09-18 14:35:15 -04008332 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008333 public static final int SIZE_SMALL = 2;
8334
8335 /**
8336 * Size value for use with {@link #setCustomSizePreset} to show this notification
8337 * with a medium size.
8338 * <p>This value is only applicable for custom display notifications created using
8339 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008340 *
8341 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008342 */
Gus Prevasd7363752018-09-18 14:35:15 -04008343 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008344 public static final int SIZE_MEDIUM = 3;
8345
8346 /**
8347 * Size value for use with {@link #setCustomSizePreset} to show this notification
8348 * with a large size.
8349 * <p>This value is only applicable for custom display notifications created using
8350 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008351 *
8352 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008353 */
Gus Prevasd7363752018-09-18 14:35:15 -04008354 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008355 public static final int SIZE_LARGE = 4;
8356
Griff Hazend5f11f92014-05-27 15:40:09 -07008357 /**
8358 * Size value for use with {@link #setCustomSizePreset} to show this notification
8359 * full screen.
8360 * <p>This value is only applicable for custom display notifications created using
8361 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008362 *
8363 * @deprecated Display intents are no longer supported.
Griff Hazend5f11f92014-05-27 15:40:09 -07008364 */
Gus Prevasd7363752018-09-18 14:35:15 -04008365 @Deprecated
Griff Hazend5f11f92014-05-27 15:40:09 -07008366 public static final int SIZE_FULL_SCREEN = 5;
8367
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008368 /**
8369 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
8370 * short amount of time when this notification is displayed on the screen. This
8371 * is the default value.
Gus Prevasd7363752018-09-18 14:35:15 -04008372 *
8373 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008374 */
Gus Prevasd7363752018-09-18 14:35:15 -04008375 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008376 public static final int SCREEN_TIMEOUT_SHORT = 0;
8377
8378 /**
8379 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
8380 * for a longer amount of time when this notification is displayed on the screen.
Gus Prevasd7363752018-09-18 14:35:15 -04008381 *
8382 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008383 */
Gus Prevasd7363752018-09-18 14:35:15 -04008384 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008385 public static final int SCREEN_TIMEOUT_LONG = -1;
8386
Griff Hazen61a9e862014-05-22 16:05:19 -07008387 /** Notification extra which contains wearable extensions */
8388 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
8389
Pete Gastaf6781d2014-10-07 15:17:05 -04008390 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07008391 private static final String KEY_ACTIONS = "actions";
8392 private static final String KEY_FLAGS = "flags";
8393 private static final String KEY_DISPLAY_INTENT = "displayIntent";
8394 private static final String KEY_PAGES = "pages";
8395 private static final String KEY_BACKGROUND = "background";
8396 private static final String KEY_CONTENT_ICON = "contentIcon";
8397 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
8398 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
8399 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
8400 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
8401 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008402 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04008403 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008404 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07008405
8406 // Flags bitwise-ored to mFlags
8407 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
8408 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
8409 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
8410 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008411 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04008412 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04008413 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07008414
8415 // Default value for flags integer
8416 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
8417
8418 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
8419 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
8420
8421 private ArrayList<Action> mActions = new ArrayList<Action>();
8422 private int mFlags = DEFAULT_FLAGS;
8423 private PendingIntent mDisplayIntent;
8424 private ArrayList<Notification> mPages = new ArrayList<Notification>();
8425 private Bitmap mBackground;
8426 private int mContentIcon;
8427 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
8428 private int mContentActionIndex = UNSET_ACTION_INDEX;
8429 private int mCustomSizePreset = SIZE_DEFAULT;
8430 private int mCustomContentHeight;
8431 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008432 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04008433 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008434 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07008435
8436 /**
8437 * Create a {@link android.app.Notification.WearableExtender} with default
8438 * options.
8439 */
8440 public WearableExtender() {
8441 }
8442
8443 public WearableExtender(Notification notif) {
8444 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
8445 if (wearableBundle != null) {
8446 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
8447 if (actions != null) {
8448 mActions.addAll(actions);
8449 }
8450
8451 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
8452 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
8453
8454 Notification[] pages = getNotificationArrayFromBundle(
8455 wearableBundle, KEY_PAGES);
8456 if (pages != null) {
8457 Collections.addAll(mPages, pages);
8458 }
8459
8460 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
8461 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
8462 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
8463 DEFAULT_CONTENT_ICON_GRAVITY);
8464 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
8465 UNSET_ACTION_INDEX);
8466 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
8467 SIZE_DEFAULT);
8468 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
8469 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008470 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04008471 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008472 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07008473 }
8474 }
8475
8476 /**
8477 * Apply wearable extensions to a notification that is being built. This is typically
8478 * called by the {@link android.app.Notification.Builder#extend} method of
8479 * {@link android.app.Notification.Builder}.
8480 */
8481 @Override
8482 public Notification.Builder extend(Notification.Builder builder) {
8483 Bundle wearableBundle = new Bundle();
8484
8485 if (!mActions.isEmpty()) {
8486 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
8487 }
8488 if (mFlags != DEFAULT_FLAGS) {
8489 wearableBundle.putInt(KEY_FLAGS, mFlags);
8490 }
8491 if (mDisplayIntent != null) {
8492 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
8493 }
8494 if (!mPages.isEmpty()) {
8495 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
8496 new Notification[mPages.size()]));
8497 }
8498 if (mBackground != null) {
8499 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
8500 }
8501 if (mContentIcon != 0) {
8502 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
8503 }
8504 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
8505 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
8506 }
8507 if (mContentActionIndex != UNSET_ACTION_INDEX) {
8508 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
8509 mContentActionIndex);
8510 }
8511 if (mCustomSizePreset != SIZE_DEFAULT) {
8512 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
8513 }
8514 if (mCustomContentHeight != 0) {
8515 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
8516 }
8517 if (mGravity != DEFAULT_GRAVITY) {
8518 wearableBundle.putInt(KEY_GRAVITY, mGravity);
8519 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008520 if (mHintScreenTimeout != 0) {
8521 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
8522 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04008523 if (mDismissalId != null) {
8524 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
8525 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008526 if (mBridgeTag != null) {
8527 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
8528 }
Griff Hazen61a9e862014-05-22 16:05:19 -07008529
8530 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
8531 return builder;
8532 }
8533
8534 @Override
8535 public WearableExtender clone() {
8536 WearableExtender that = new WearableExtender();
8537 that.mActions = new ArrayList<Action>(this.mActions);
8538 that.mFlags = this.mFlags;
8539 that.mDisplayIntent = this.mDisplayIntent;
8540 that.mPages = new ArrayList<Notification>(this.mPages);
8541 that.mBackground = this.mBackground;
8542 that.mContentIcon = this.mContentIcon;
8543 that.mContentIconGravity = this.mContentIconGravity;
8544 that.mContentActionIndex = this.mContentActionIndex;
8545 that.mCustomSizePreset = this.mCustomSizePreset;
8546 that.mCustomContentHeight = this.mCustomContentHeight;
8547 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008548 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04008549 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008550 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07008551 return that;
8552 }
8553
8554 /**
8555 * Add a wearable action to this notification.
8556 *
8557 * <p>When wearable actions are added using this method, the set of actions that
8558 * show on a wearable device splits from devices that only show actions added
8559 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
8560 * of which actions display on different devices.
8561 *
8562 * @param action the action to add to this notification
8563 * @return this object for method chaining
8564 * @see android.app.Notification.Action
8565 */
8566 public WearableExtender addAction(Action action) {
8567 mActions.add(action);
8568 return this;
8569 }
8570
8571 /**
8572 * Adds wearable actions to this notification.
8573 *
8574 * <p>When wearable actions are added using this method, the set of actions that
8575 * show on a wearable device splits from devices that only show actions added
8576 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
8577 * of which actions display on different devices.
8578 *
8579 * @param actions the actions to add to this notification
8580 * @return this object for method chaining
8581 * @see android.app.Notification.Action
8582 */
8583 public WearableExtender addActions(List<Action> actions) {
8584 mActions.addAll(actions);
8585 return this;
8586 }
8587
8588 /**
8589 * Clear all wearable actions present on this builder.
8590 * @return this object for method chaining.
8591 * @see #addAction
8592 */
8593 public WearableExtender clearActions() {
8594 mActions.clear();
8595 return this;
8596 }
8597
8598 /**
8599 * Get the wearable actions present on this notification.
8600 */
8601 public List<Action> getActions() {
8602 return mActions;
8603 }
8604
8605 /**
8606 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07008607 * this notification. The {@link PendingIntent} provided should be for an activity.
8608 *
8609 * <pre class="prettyprint">
8610 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
8611 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
8612 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
8613 * Notification notif = new Notification.Builder(context)
8614 * .extend(new Notification.WearableExtender()
8615 * .setDisplayIntent(displayPendingIntent)
8616 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
8617 * .build();</pre>
8618 *
8619 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07008620 * should have an empty task affinity. It is also recommended to use the device
8621 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07008622 *
8623 * <p>Example AndroidManifest.xml entry:
8624 * <pre class="prettyprint">
8625 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
8626 * android:exported=&quot;true&quot;
8627 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07008628 * android:taskAffinity=&quot;&quot;
8629 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008630 *
8631 * @param intent the {@link PendingIntent} for an activity
8632 * @return this object for method chaining
8633 * @see android.app.Notification.WearableExtender#getDisplayIntent
Gus Prevasd7363752018-09-18 14:35:15 -04008634 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008635 */
Gus Prevasd7363752018-09-18 14:35:15 -04008636 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008637 public WearableExtender setDisplayIntent(PendingIntent intent) {
8638 mDisplayIntent = intent;
8639 return this;
8640 }
8641
8642 /**
8643 * Get the intent to launch inside of an activity view when displaying this
8644 * notification. This {@code PendingIntent} should be for an activity.
Gus Prevasd7363752018-09-18 14:35:15 -04008645 *
8646 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008647 */
Gus Prevasd7363752018-09-18 14:35:15 -04008648 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008649 public PendingIntent getDisplayIntent() {
8650 return mDisplayIntent;
8651 }
8652
8653 /**
8654 * Add an additional page of content to display with this notification. The current
8655 * notification forms the first page, and pages added using this function form
8656 * subsequent pages. This field can be used to separate a notification into multiple
8657 * sections.
8658 *
8659 * @param page the notification to add as another page
8660 * @return this object for method chaining
8661 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04008662 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008663 */
Gus Prevasd7363752018-09-18 14:35:15 -04008664 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008665 public WearableExtender addPage(Notification page) {
8666 mPages.add(page);
8667 return this;
8668 }
8669
8670 /**
8671 * Add additional pages of content to display with this notification. The current
8672 * notification forms the first page, and pages added using this function form
8673 * subsequent pages. This field can be used to separate a notification into multiple
8674 * sections.
8675 *
8676 * @param pages a list of notifications
8677 * @return this object for method chaining
8678 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04008679 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008680 */
Gus Prevasd7363752018-09-18 14:35:15 -04008681 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008682 public WearableExtender addPages(List<Notification> pages) {
8683 mPages.addAll(pages);
8684 return this;
8685 }
8686
8687 /**
8688 * Clear all additional pages present on this builder.
8689 * @return this object for method chaining.
8690 * @see #addPage
Gus Prevasd7363752018-09-18 14:35:15 -04008691 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008692 */
Gus Prevasd7363752018-09-18 14:35:15 -04008693 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008694 public WearableExtender clearPages() {
8695 mPages.clear();
8696 return this;
8697 }
8698
8699 /**
8700 * Get the array of additional pages of content for displaying this notification. The
8701 * current notification forms the first page, and elements within this array form
8702 * subsequent pages. This field can be used to separate a notification into multiple
8703 * sections.
8704 * @return the pages for this notification
Gus Prevasd7363752018-09-18 14:35:15 -04008705 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008706 */
Gus Prevasd7363752018-09-18 14:35:15 -04008707 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008708 public List<Notification> getPages() {
8709 return mPages;
8710 }
8711
8712 /**
8713 * Set a background image to be displayed behind the notification content.
8714 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8715 * will work with any notification style.
8716 *
8717 * @param background the background bitmap
8718 * @return this object for method chaining
8719 * @see android.app.Notification.WearableExtender#getBackground
Gus Prevasd7363752018-09-18 14:35:15 -04008720 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008721 */
Gus Prevasd7363752018-09-18 14:35:15 -04008722 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008723 public WearableExtender setBackground(Bitmap background) {
8724 mBackground = background;
8725 return this;
8726 }
8727
8728 /**
8729 * Get a background image to be displayed behind the notification content.
8730 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8731 * will work with any notification style.
8732 *
8733 * @return the background image
8734 * @see android.app.Notification.WearableExtender#setBackground
Gus Prevasd7363752018-09-18 14:35:15 -04008735 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008736 */
Gus Prevasd7363752018-09-18 14:35:15 -04008737 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008738 public Bitmap getBackground() {
8739 return mBackground;
8740 }
8741
8742 /**
8743 * Set an icon that goes with the content of this notification.
8744 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008745 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008746 public WearableExtender setContentIcon(int icon) {
8747 mContentIcon = icon;
8748 return this;
8749 }
8750
8751 /**
8752 * Get an icon that goes with the content of this notification.
8753 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008754 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008755 public int getContentIcon() {
8756 return mContentIcon;
8757 }
8758
8759 /**
8760 * Set the gravity that the content icon should have within the notification display.
8761 * Supported values include {@link android.view.Gravity#START} and
8762 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8763 * @see #setContentIcon
8764 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008765 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008766 public WearableExtender setContentIconGravity(int contentIconGravity) {
8767 mContentIconGravity = contentIconGravity;
8768 return this;
8769 }
8770
8771 /**
8772 * Get the gravity that the content icon should have within the notification display.
8773 * Supported values include {@link android.view.Gravity#START} and
8774 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8775 * @see #getContentIcon
8776 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008777 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008778 public int getContentIconGravity() {
8779 return mContentIconGravity;
8780 }
8781
8782 /**
Gus Prevasd7363752018-09-18 14:35:15 -04008783 * Set an action from this notification's actions as the primary action. If the action has a
8784 * {@link RemoteInput} associated with it, shortcuts to the options for that input are shown
8785 * directly on the notification.
Griff Hazen14f57992014-05-26 09:07:14 -07008786 *
Gus Prevasd7363752018-09-18 14:35:15 -04008787 * @param actionIndex The index of the primary action.
Griff Hazen14f57992014-05-26 09:07:14 -07008788 * If wearable actions were added to the main notification, this index
8789 * will apply to that list, otherwise it will apply to the regular
8790 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07008791 */
8792 public WearableExtender setContentAction(int actionIndex) {
8793 mContentActionIndex = actionIndex;
8794 return this;
8795 }
8796
8797 /**
Gus Prevasd7363752018-09-18 14:35:15 -04008798 * Get the index of the notification action, if any, that was specified as the primary
8799 * action.
Griff Hazen14f57992014-05-26 09:07:14 -07008800 *
8801 * <p>If wearable specific actions were added to the main notification, this index will
8802 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07008803 *
8804 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07008805 */
8806 public int getContentAction() {
8807 return mContentActionIndex;
8808 }
8809
8810 /**
8811 * Set the gravity that this notification should have within the available viewport space.
8812 * Supported values include {@link android.view.Gravity#TOP},
8813 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8814 * The default value is {@link android.view.Gravity#BOTTOM}.
8815 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008816 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008817 public WearableExtender setGravity(int gravity) {
8818 mGravity = gravity;
8819 return this;
8820 }
8821
8822 /**
8823 * Get the gravity that this notification should have within the available viewport space.
8824 * Supported values include {@link android.view.Gravity#TOP},
8825 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8826 * The default value is {@link android.view.Gravity#BOTTOM}.
8827 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008828 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008829 public int getGravity() {
8830 return mGravity;
8831 }
8832
8833 /**
8834 * Set the custom size preset for the display of this notification out of the available
8835 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
8836 * {@link #SIZE_LARGE}.
8837 * <p>Some custom size presets are only applicable for custom display notifications created
8838 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
8839 * documentation for the preset in question. See also
8840 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
8841 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008842 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008843 public WearableExtender setCustomSizePreset(int sizePreset) {
8844 mCustomSizePreset = sizePreset;
8845 return this;
8846 }
8847
8848 /**
8849 * Get the custom size preset for the display of this notification out of the available
8850 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
8851 * {@link #SIZE_LARGE}.
8852 * <p>Some custom size presets are only applicable for custom display notifications created
8853 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
8854 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
8855 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008856 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008857 public int getCustomSizePreset() {
8858 return mCustomSizePreset;
8859 }
8860
8861 /**
8862 * Set the custom height in pixels for the display of this notification's content.
8863 * <p>This option is only available for custom display notifications created
8864 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
8865 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
8866 * {@link #getCustomContentHeight}.
8867 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008868 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008869 public WearableExtender setCustomContentHeight(int height) {
8870 mCustomContentHeight = height;
8871 return this;
8872 }
8873
8874 /**
8875 * Get the custom height in pixels for the display of this notification's content.
8876 * <p>This option is only available for custom display notifications created
8877 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
8878 * {@link #setCustomContentHeight}.
8879 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008880 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008881 public int getCustomContentHeight() {
8882 return mCustomContentHeight;
8883 }
8884
8885 /**
8886 * Set whether the scrolling position for the contents of this notification should start
8887 * at the bottom of the contents instead of the top when the contents are too long to
8888 * display within the screen. Default is false (start scroll at the top).
8889 */
8890 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
8891 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
8892 return this;
8893 }
8894
8895 /**
8896 * Get whether the scrolling position for the contents of this notification should start
8897 * at the bottom of the contents instead of the top when the contents are too long to
8898 * display within the screen. Default is false (start scroll at the top).
8899 */
8900 public boolean getStartScrollBottom() {
8901 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
8902 }
8903
8904 /**
8905 * Set whether the content intent is available when the wearable device is not connected
8906 * to a companion device. The user can still trigger this intent when the wearable device
8907 * is offline, but a visual hint will indicate that the content intent may not be available.
8908 * Defaults to true.
8909 */
8910 public WearableExtender setContentIntentAvailableOffline(
8911 boolean contentIntentAvailableOffline) {
8912 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
8913 return this;
8914 }
8915
8916 /**
8917 * Get whether the content intent is available when the wearable device is not connected
8918 * to a companion device. The user can still trigger this intent when the wearable device
8919 * is offline, but a visual hint will indicate that the content intent may not be available.
8920 * Defaults to true.
8921 */
8922 public boolean getContentIntentAvailableOffline() {
8923 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
8924 }
8925
8926 /**
8927 * Set a hint that this notification's icon should not be displayed.
8928 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
8929 * @return this object for method chaining
8930 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008931 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008932 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
8933 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
8934 return this;
8935 }
8936
8937 /**
8938 * Get a hint that this notification's icon should not be displayed.
8939 * @return {@code true} if this icon should not be displayed, false otherwise.
8940 * The default value is {@code false} if this was never set.
8941 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008942 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008943 public boolean getHintHideIcon() {
8944 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
8945 }
8946
8947 /**
8948 * Set a visual hint that only the background image of this notification should be
8949 * displayed, and other semantic content should be hidden. This hint is only applicable
8950 * to sub-pages added using {@link #addPage}.
8951 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008952 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008953 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
8954 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
8955 return this;
8956 }
8957
8958 /**
8959 * Get a visual hint that only the background image of this notification should be
8960 * displayed, and other semantic content should be hidden. This hint is only applicable
8961 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
8962 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008963 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008964 public boolean getHintShowBackgroundOnly() {
8965 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
8966 }
8967
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008968 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08008969 * Set a hint that this notification's background should not be clipped if possible,
8970 * and should instead be resized to fully display on the screen, retaining the aspect
8971 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008972 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
8973 * @return this object for method chaining
8974 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008975 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008976 public WearableExtender setHintAvoidBackgroundClipping(
8977 boolean hintAvoidBackgroundClipping) {
8978 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
8979 return this;
8980 }
8981
8982 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08008983 * Get a hint that this notification's background should not be clipped if possible,
8984 * and should instead be resized to fully display on the screen, retaining the aspect
8985 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008986 * @return {@code true} if it's ok if the background is clipped on the screen, false
8987 * otherwise. The default value is {@code false} if this was never set.
8988 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008989 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008990 public boolean getHintAvoidBackgroundClipping() {
8991 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
8992 }
8993
8994 /**
8995 * Set a hint that the screen should remain on for at least this duration when
8996 * this notification is displayed on the screen.
8997 * @param timeout The requested screen timeout in milliseconds. Can also be either
8998 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
8999 * @return this object for method chaining
9000 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009001 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009002 public WearableExtender setHintScreenTimeout(int timeout) {
9003 mHintScreenTimeout = timeout;
9004 return this;
9005 }
9006
9007 /**
9008 * Get the duration, in milliseconds, that the screen should remain on for
9009 * when this notification is displayed.
9010 * @return the duration in milliseconds if > 0, or either one of the sentinel values
9011 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9012 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009013 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009014 public int getHintScreenTimeout() {
9015 return mHintScreenTimeout;
9016 }
9017
Alex Hills9ab3a232016-04-05 14:54:56 -04009018 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04009019 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
9020 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9021 * qr codes, as well as other simple black-and-white tickets.
9022 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
9023 * @return this object for method chaining
Gus Prevasd7363752018-09-18 14:35:15 -04009024 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009025 */
Gus Prevasd7363752018-09-18 14:35:15 -04009026 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009027 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
9028 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
9029 return this;
9030 }
9031
9032 /**
9033 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
9034 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9035 * qr codes, as well as other simple black-and-white tickets.
9036 * @return {@code true} if it should be displayed in ambient, false otherwise
9037 * otherwise. The default value is {@code false} if this was never set.
Gus Prevasd7363752018-09-18 14:35:15 -04009038 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009039 */
Gus Prevasd7363752018-09-18 14:35:15 -04009040 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009041 public boolean getHintAmbientBigPicture() {
9042 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
9043 }
9044
9045 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04009046 * Set a hint that this notification's content intent will launch an {@link Activity}
9047 * directly, telling the platform that it can generate the appropriate transitions.
9048 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
9049 * an activity and transitions should be generated, false otherwise.
9050 * @return this object for method chaining
9051 */
9052 public WearableExtender setHintContentIntentLaunchesActivity(
9053 boolean hintContentIntentLaunchesActivity) {
9054 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
9055 return this;
9056 }
9057
9058 /**
9059 * Get a hint that this notification's content intent will launch an {@link Activity}
9060 * directly, telling the platform that it can generate the appropriate transitions
9061 * @return {@code true} if the content intent will launch an activity and transitions should
9062 * be generated, false otherwise. The default value is {@code false} if this was never set.
9063 */
9064 public boolean getHintContentIntentLaunchesActivity() {
9065 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
9066 }
9067
Nadia Benbernou948627e2016-04-14 14:41:08 -04009068 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009069 * Sets the dismissal id for this notification. If a notification is posted with a
9070 * dismissal id, then when that notification is canceled, notifications on other wearables
9071 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04009072 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009073 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04009074 * @param dismissalId the dismissal id of the notification.
9075 * @return this object for method chaining
9076 */
9077 public WearableExtender setDismissalId(String dismissalId) {
9078 mDismissalId = dismissalId;
9079 return this;
9080 }
9081
9082 /**
9083 * Returns the dismissal id of the notification.
9084 * @return the dismissal id of the notification or null if it has not been set.
9085 */
9086 public String getDismissalId() {
9087 return mDismissalId;
9088 }
9089
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009090 /**
9091 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
9092 * posted from a phone to provide finer-grained control on what notifications are bridged
9093 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
9094 * Features to Notifications</a> for more information.
9095 * @param bridgeTag the bridge tag of the notification.
9096 * @return this object for method chaining
9097 */
9098 public WearableExtender setBridgeTag(String bridgeTag) {
9099 mBridgeTag = bridgeTag;
9100 return this;
9101 }
9102
9103 /**
9104 * Returns the bridge tag of the notification.
9105 * @return the bridge tag or null if not present.
9106 */
9107 public String getBridgeTag() {
9108 return mBridgeTag;
9109 }
9110
Griff Hazen61a9e862014-05-22 16:05:19 -07009111 private void setFlag(int mask, boolean value) {
9112 if (value) {
9113 mFlags |= mask;
9114 } else {
9115 mFlags &= ~mask;
9116 }
9117 }
9118 }
9119
9120 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009121 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
9122 * with car extensions:
9123 *
9124 * <ol>
9125 * <li>Create an {@link Notification.Builder}, setting any desired
9126 * properties.
9127 * <li>Create a {@link CarExtender}.
9128 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
9129 * {@link CarExtender}.
9130 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9131 * to apply the extensions to a notification.
9132 * </ol>
9133 *
9134 * <pre class="prettyprint">
9135 * Notification notification = new Notification.Builder(context)
9136 * ...
9137 * .extend(new CarExtender()
9138 * .set*(...))
9139 * .build();
9140 * </pre>
9141 *
9142 * <p>Car extensions can be accessed on an existing notification by using the
9143 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
9144 * to access values.
9145 */
9146 public static final class CarExtender implements Extender {
9147 private static final String TAG = "CarExtender";
9148
9149 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
9150 private static final String EXTRA_LARGE_ICON = "large_icon";
9151 private static final String EXTRA_CONVERSATION = "car_conversation";
9152 private static final String EXTRA_COLOR = "app_color";
9153
9154 private Bitmap mLargeIcon;
9155 private UnreadConversation mUnreadConversation;
9156 private int mColor = Notification.COLOR_DEFAULT;
9157
9158 /**
9159 * Create a {@link CarExtender} with default options.
9160 */
9161 public CarExtender() {
9162 }
9163
9164 /**
9165 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
9166 *
9167 * @param notif The notification from which to copy options.
9168 */
9169 public CarExtender(Notification notif) {
9170 Bundle carBundle = notif.extras == null ?
9171 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
9172 if (carBundle != null) {
9173 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
9174 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
9175
9176 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
9177 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
9178 }
9179 }
9180
9181 /**
9182 * Apply car extensions to a notification that is being built. This is typically called by
9183 * the {@link Notification.Builder#extend(Notification.Extender)}
9184 * method of {@link Notification.Builder}.
9185 */
9186 @Override
9187 public Notification.Builder extend(Notification.Builder builder) {
9188 Bundle carExtensions = new Bundle();
9189
9190 if (mLargeIcon != null) {
9191 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
9192 }
9193 if (mColor != Notification.COLOR_DEFAULT) {
9194 carExtensions.putInt(EXTRA_COLOR, mColor);
9195 }
9196
9197 if (mUnreadConversation != null) {
9198 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
9199 carExtensions.putBundle(EXTRA_CONVERSATION, b);
9200 }
9201
9202 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
9203 return builder;
9204 }
9205
9206 /**
9207 * Sets the accent color to use when Android Auto presents the notification.
9208 *
9209 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
9210 * to accent the displayed notification. However, not all colors are acceptable in an
9211 * automotive setting. This method can be used to override the color provided in the
9212 * notification in such a situation.
9213 */
Tor Norbye80756e32015-03-02 09:39:27 -08009214 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009215 mColor = color;
9216 return this;
9217 }
9218
9219 /**
9220 * Gets the accent color.
9221 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04009222 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009223 */
Tor Norbye80756e32015-03-02 09:39:27 -08009224 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009225 public int getColor() {
9226 return mColor;
9227 }
9228
9229 /**
9230 * Sets the large icon of the car notification.
9231 *
9232 * If no large icon is set in the extender, Android Auto will display the icon
9233 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
9234 *
9235 * @param largeIcon The large icon to use in the car notification.
9236 * @return This object for method chaining.
9237 */
9238 public CarExtender setLargeIcon(Bitmap largeIcon) {
9239 mLargeIcon = largeIcon;
9240 return this;
9241 }
9242
9243 /**
9244 * Gets the large icon used in this car notification, or null if no icon has been set.
9245 *
9246 * @return The large icon for the car notification.
9247 * @see CarExtender#setLargeIcon
9248 */
9249 public Bitmap getLargeIcon() {
9250 return mLargeIcon;
9251 }
9252
9253 /**
9254 * Sets the unread conversation in a message notification.
9255 *
9256 * @param unreadConversation The unread part of the conversation this notification conveys.
9257 * @return This object for method chaining.
9258 */
9259 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
9260 mUnreadConversation = unreadConversation;
9261 return this;
9262 }
9263
9264 /**
9265 * Returns the unread conversation conveyed by this notification.
9266 * @see #setUnreadConversation(UnreadConversation)
9267 */
9268 public UnreadConversation getUnreadConversation() {
9269 return mUnreadConversation;
9270 }
9271
9272 /**
9273 * A class which holds the unread messages from a conversation.
9274 */
9275 public static class UnreadConversation {
9276 private static final String KEY_AUTHOR = "author";
9277 private static final String KEY_TEXT = "text";
9278 private static final String KEY_MESSAGES = "messages";
9279 private static final String KEY_REMOTE_INPUT = "remote_input";
9280 private static final String KEY_ON_REPLY = "on_reply";
9281 private static final String KEY_ON_READ = "on_read";
9282 private static final String KEY_PARTICIPANTS = "participants";
9283 private static final String KEY_TIMESTAMP = "timestamp";
9284
9285 private final String[] mMessages;
9286 private final RemoteInput mRemoteInput;
9287 private final PendingIntent mReplyPendingIntent;
9288 private final PendingIntent mReadPendingIntent;
9289 private final String[] mParticipants;
9290 private final long mLatestTimestamp;
9291
9292 UnreadConversation(String[] messages, RemoteInput remoteInput,
9293 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
9294 String[] participants, long latestTimestamp) {
9295 mMessages = messages;
9296 mRemoteInput = remoteInput;
9297 mReadPendingIntent = readPendingIntent;
9298 mReplyPendingIntent = replyPendingIntent;
9299 mParticipants = participants;
9300 mLatestTimestamp = latestTimestamp;
9301 }
9302
9303 /**
9304 * Gets the list of messages conveyed by this notification.
9305 */
9306 public String[] getMessages() {
9307 return mMessages;
9308 }
9309
9310 /**
9311 * Gets the remote input that will be used to convey the response to a message list, or
9312 * null if no such remote input exists.
9313 */
9314 public RemoteInput getRemoteInput() {
9315 return mRemoteInput;
9316 }
9317
9318 /**
9319 * Gets the pending intent that will be triggered when the user replies to this
9320 * notification.
9321 */
9322 public PendingIntent getReplyPendingIntent() {
9323 return mReplyPendingIntent;
9324 }
9325
9326 /**
9327 * Gets the pending intent that Android Auto will send after it reads aloud all messages
9328 * in this object's message list.
9329 */
9330 public PendingIntent getReadPendingIntent() {
9331 return mReadPendingIntent;
9332 }
9333
9334 /**
9335 * Gets the participants in the conversation.
9336 */
9337 public String[] getParticipants() {
9338 return mParticipants;
9339 }
9340
9341 /**
9342 * Gets the firs participant in the conversation.
9343 */
9344 public String getParticipant() {
9345 return mParticipants.length > 0 ? mParticipants[0] : null;
9346 }
9347
9348 /**
9349 * Gets the timestamp of the conversation.
9350 */
9351 public long getLatestTimestamp() {
9352 return mLatestTimestamp;
9353 }
9354
9355 Bundle getBundleForUnreadConversation() {
9356 Bundle b = new Bundle();
9357 String author = null;
9358 if (mParticipants != null && mParticipants.length > 1) {
9359 author = mParticipants[0];
9360 }
9361 Parcelable[] messages = new Parcelable[mMessages.length];
9362 for (int i = 0; i < messages.length; i++) {
9363 Bundle m = new Bundle();
9364 m.putString(KEY_TEXT, mMessages[i]);
9365 m.putString(KEY_AUTHOR, author);
9366 messages[i] = m;
9367 }
9368 b.putParcelableArray(KEY_MESSAGES, messages);
9369 if (mRemoteInput != null) {
9370 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
9371 }
9372 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
9373 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
9374 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
9375 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
9376 return b;
9377 }
9378
9379 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
9380 if (b == null) {
9381 return null;
9382 }
9383 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
9384 String[] messages = null;
9385 if (parcelableMessages != null) {
9386 String[] tmp = new String[parcelableMessages.length];
9387 boolean success = true;
9388 for (int i = 0; i < tmp.length; i++) {
9389 if (!(parcelableMessages[i] instanceof Bundle)) {
9390 success = false;
9391 break;
9392 }
9393 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
9394 if (tmp[i] == null) {
9395 success = false;
9396 break;
9397 }
9398 }
9399 if (success) {
9400 messages = tmp;
9401 } else {
9402 return null;
9403 }
9404 }
9405
9406 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
9407 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
9408
9409 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
9410
9411 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
9412 if (participants == null || participants.length != 1) {
9413 return null;
9414 }
9415
9416 return new UnreadConversation(messages,
9417 remoteInput,
9418 onReply,
9419 onRead,
9420 participants, b.getLong(KEY_TIMESTAMP));
9421 }
9422 };
9423
9424 /**
9425 * Builder class for {@link CarExtender.UnreadConversation} objects.
9426 */
9427 public static class Builder {
9428 private final List<String> mMessages = new ArrayList<String>();
9429 private final String mParticipant;
9430 private RemoteInput mRemoteInput;
9431 private PendingIntent mReadPendingIntent;
9432 private PendingIntent mReplyPendingIntent;
9433 private long mLatestTimestamp;
9434
9435 /**
9436 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
9437 *
9438 * @param name The name of the other participant in the conversation.
9439 */
9440 public Builder(String name) {
9441 mParticipant = name;
9442 }
9443
9444 /**
9445 * Appends a new unread message to the list of messages for this conversation.
9446 *
9447 * The messages should be added from oldest to newest.
9448 *
9449 * @param message The text of the new unread message.
9450 * @return This object for method chaining.
9451 */
9452 public Builder addMessage(String message) {
9453 mMessages.add(message);
9454 return this;
9455 }
9456
9457 /**
9458 * Sets the pending intent and remote input which will convey the reply to this
9459 * notification.
9460 *
9461 * @param pendingIntent The pending intent which will be triggered on a reply.
9462 * @param remoteInput The remote input parcelable which will carry the reply.
9463 * @return This object for method chaining.
9464 *
9465 * @see CarExtender.UnreadConversation#getRemoteInput
9466 * @see CarExtender.UnreadConversation#getReplyPendingIntent
9467 */
9468 public Builder setReplyAction(
9469 PendingIntent pendingIntent, RemoteInput remoteInput) {
9470 mRemoteInput = remoteInput;
9471 mReplyPendingIntent = pendingIntent;
9472
9473 return this;
9474 }
9475
9476 /**
9477 * Sets the pending intent that will be sent once the messages in this notification
9478 * are read.
9479 *
9480 * @param pendingIntent The pending intent to use.
9481 * @return This object for method chaining.
9482 */
9483 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
9484 mReadPendingIntent = pendingIntent;
9485 return this;
9486 }
9487
9488 /**
9489 * Sets the timestamp of the most recent message in an unread conversation.
9490 *
9491 * If a messaging notification has been posted by your application and has not
9492 * yet been cancelled, posting a later notification with the same id and tag
9493 * but without a newer timestamp may result in Android Auto not displaying a
9494 * heads up notification for the later notification.
9495 *
9496 * @param timestamp The timestamp of the most recent message in the conversation.
9497 * @return This object for method chaining.
9498 */
9499 public Builder setLatestTimestamp(long timestamp) {
9500 mLatestTimestamp = timestamp;
9501 return this;
9502 }
9503
9504 /**
9505 * Builds a new unread conversation object.
9506 *
9507 * @return The new unread conversation object.
9508 */
9509 public UnreadConversation build() {
9510 String[] messages = mMessages.toArray(new String[mMessages.size()]);
9511 String[] participants = { mParticipant };
9512 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
9513 mReadPendingIntent, participants, mLatestTimestamp);
9514 }
9515 }
9516 }
9517
9518 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009519 * <p>Helper class to add Android TV extensions to notifications. To create a notification
9520 * with a TV extension:
9521 *
9522 * <ol>
9523 * <li>Create an {@link Notification.Builder}, setting any desired properties.
9524 * <li>Create a {@link TvExtender}.
9525 * <li>Set TV-specific properties using the {@code set} methods of
9526 * {@link TvExtender}.
9527 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9528 * to apply the extension to a notification.
9529 * </ol>
9530 *
9531 * <pre class="prettyprint">
9532 * Notification notification = new Notification.Builder(context)
9533 * ...
9534 * .extend(new TvExtender()
9535 * .set*(...))
9536 * .build();
9537 * </pre>
9538 *
9539 * <p>TV extensions can be accessed on an existing notification by using the
9540 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
9541 * to access values.
9542 *
9543 * @hide
9544 */
9545 @SystemApi
9546 public static final class TvExtender implements Extender {
9547 private static final String TAG = "TvExtender";
9548
9549 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
9550 private static final String EXTRA_FLAGS = "flags";
9551 private static final String EXTRA_CONTENT_INTENT = "content_intent";
9552 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009553 private static final String EXTRA_CHANNEL_ID = "channel_id";
Rhiannon Malia1a083932018-01-24 15:02:30 -08009554 private static final String EXTRA_SUPPRESS_SHOW_OVER_APPS = "suppressShowOverApps";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009555
9556 // Flags bitwise-ored to mFlags
9557 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
9558
9559 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009560 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009561 private PendingIntent mContentIntent;
9562 private PendingIntent mDeleteIntent;
Rhiannon Malia1a083932018-01-24 15:02:30 -08009563 private boolean mSuppressShowOverApps;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009564
9565 /**
9566 * Create a {@link TvExtender} with default options.
9567 */
9568 public TvExtender() {
9569 mFlags = FLAG_AVAILABLE_ON_TV;
9570 }
9571
9572 /**
9573 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
9574 *
9575 * @param notif The notification from which to copy options.
9576 */
9577 public TvExtender(Notification notif) {
9578 Bundle bundle = notif.extras == null ?
9579 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
9580 if (bundle != null) {
9581 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009582 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Rhiannon Malia1a083932018-01-24 15:02:30 -08009583 mSuppressShowOverApps = bundle.getBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009584 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
9585 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
9586 }
9587 }
9588
9589 /**
9590 * Apply a TV extension to a notification that is being built. This is typically called by
9591 * the {@link Notification.Builder#extend(Notification.Extender)}
9592 * method of {@link Notification.Builder}.
9593 */
9594 @Override
9595 public Notification.Builder extend(Notification.Builder builder) {
9596 Bundle bundle = new Bundle();
9597
9598 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009599 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Rhiannon Malia1a083932018-01-24 15:02:30 -08009600 bundle.putBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS, mSuppressShowOverApps);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009601 if (mContentIntent != null) {
9602 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
9603 }
9604
9605 if (mDeleteIntent != null) {
9606 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
9607 }
9608
9609 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
9610 return builder;
9611 }
9612
9613 /**
9614 * Returns true if this notification should be shown on TV. This method return true
9615 * if the notification was extended with a TvExtender.
9616 */
9617 public boolean isAvailableOnTv() {
9618 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
9619 }
9620
9621 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009622 * Specifies the channel the notification should be delivered on when shown on TV.
9623 * It can be different from the channel that the notification is delivered to when
9624 * posting on a non-TV device.
9625 */
9626 public TvExtender setChannel(String channelId) {
9627 mChannelId = channelId;
9628 return this;
9629 }
9630
9631 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04009632 * Specifies the channel the notification should be delivered on when shown on TV.
9633 * It can be different from the channel that the notification is delivered to when
9634 * posting on a non-TV device.
9635 */
9636 public TvExtender setChannelId(String channelId) {
9637 mChannelId = channelId;
9638 return this;
9639 }
9640
Jeff Sharkey000ce802017-04-29 13:13:27 -06009641 /** @removed */
9642 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009643 public String getChannel() {
9644 return mChannelId;
9645 }
9646
9647 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04009648 * Returns the id of the channel this notification posts to on TV.
9649 */
9650 public String getChannelId() {
9651 return mChannelId;
9652 }
9653
9654 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009655 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
9656 * If provided, it is used instead of the content intent specified
9657 * at the level of Notification.
9658 */
9659 public TvExtender setContentIntent(PendingIntent intent) {
9660 mContentIntent = intent;
9661 return this;
9662 }
9663
9664 /**
9665 * Returns the TV-specific content intent. If this method returns null, the
9666 * main content intent on the notification should be used.
9667 *
9668 * @see {@link Notification#contentIntent}
9669 */
9670 public PendingIntent getContentIntent() {
9671 return mContentIntent;
9672 }
9673
9674 /**
9675 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
9676 * by the user on TV. If provided, it is used instead of the delete intent specified
9677 * at the level of Notification.
9678 */
9679 public TvExtender setDeleteIntent(PendingIntent intent) {
9680 mDeleteIntent = intent;
9681 return this;
9682 }
9683
9684 /**
9685 * Returns the TV-specific delete intent. If this method returns null, the
9686 * main delete intent on the notification should be used.
9687 *
9688 * @see {@link Notification#deleteIntent}
9689 */
9690 public PendingIntent getDeleteIntent() {
9691 return mDeleteIntent;
9692 }
Rhiannon Malia1a083932018-01-24 15:02:30 -08009693
9694 /**
9695 * Specifies whether this notification should suppress showing a message over top of apps
9696 * outside of the launcher.
9697 */
9698 public TvExtender setSuppressShowOverApps(boolean suppress) {
9699 mSuppressShowOverApps = suppress;
9700 return this;
9701 }
9702
9703 /**
9704 * Returns true if this notification should not show messages over top of apps
9705 * outside of the launcher.
9706 */
9707 public boolean getSuppressShowOverApps() {
9708 return mSuppressShowOverApps;
9709 }
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009710 }
9711
9712 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07009713 * Get an array of Notification objects from a parcelable array bundle field.
9714 * Update the bundle to have a typed array so fetches in the future don't need
9715 * to do an array copy.
9716 */
9717 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
9718 Parcelable[] array = bundle.getParcelableArray(key);
9719 if (array instanceof Notification[] || array == null) {
9720 return (Notification[]) array;
9721 }
9722 Notification[] typedArray = Arrays.copyOf(array, array.length,
9723 Notification[].class);
9724 bundle.putParcelableArray(key, typedArray);
9725 return typedArray;
9726 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02009727
9728 private static class BuilderRemoteViews extends RemoteViews {
9729 public BuilderRemoteViews(Parcel parcel) {
9730 super(parcel);
9731 }
9732
Kenny Guy77320062014-08-27 21:37:15 +01009733 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
9734 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02009735 }
9736
9737 @Override
9738 public BuilderRemoteViews clone() {
9739 Parcel p = Parcel.obtain();
9740 writeToParcel(p, 0);
9741 p.setDataPosition(0);
9742 BuilderRemoteViews brv = new BuilderRemoteViews(p);
9743 p.recycle();
9744 return brv;
9745 }
9746 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08009747
Selim Cinek384804b2018-04-18 14:31:07 +08009748 /**
9749 * A result object where information about the template that was created is saved.
9750 */
9751 private static class TemplateBindResult {
Selim Cinek1c72fa02018-04-23 18:00:54 +08009752 int mIconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +08009753
9754 /**
Selim Cinek1c72fa02018-04-23 18:00:54 +08009755 * Get the margin end that needs to be added to any fields that may overlap
Selim Cinek384804b2018-04-18 14:31:07 +08009756 * with the right actions.
9757 */
Selim Cinek1c72fa02018-04-23 18:00:54 +08009758 public int getIconMarginEnd() {
9759 return mIconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +08009760 }
9761
Selim Cinek1c72fa02018-04-23 18:00:54 +08009762 public void setIconMarginEnd(int iconMarginEnd) {
9763 this.mIconMarginEnd = iconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +08009764 }
9765 }
9766
Adrian Roos70d7aa32017-01-11 15:39:06 -08009767 private static class StandardTemplateParams {
9768 boolean hasProgress = true;
9769 boolean ambient = false;
9770 CharSequence title;
9771 CharSequence text;
Selim Cinekafeed292017-12-12 17:32:44 -08009772 CharSequence headerTextSecondary;
Selim Cinekbee4e072018-05-21 22:06:43 -07009773 int maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek88188f22017-09-19 16:46:56 -07009774 boolean hideLargeIcon;
Selim Cinek384804b2018-04-18 14:31:07 +08009775 boolean hideReplyIcon;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009776
9777 final StandardTemplateParams reset() {
9778 hasProgress = true;
9779 ambient = false;
9780 title = null;
9781 text = null;
Selim Cinekafeed292017-12-12 17:32:44 -08009782 headerTextSecondary = null;
Selim Cinekbee4e072018-05-21 22:06:43 -07009783 maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009784 return this;
9785 }
9786
9787 final StandardTemplateParams hasProgress(boolean hasProgress) {
9788 this.hasProgress = hasProgress;
9789 return this;
9790 }
9791
9792 final StandardTemplateParams title(CharSequence title) {
9793 this.title = title;
9794 return this;
9795 }
9796
9797 final StandardTemplateParams text(CharSequence text) {
9798 this.text = text;
9799 return this;
9800 }
9801
Selim Cinekafeed292017-12-12 17:32:44 -08009802 final StandardTemplateParams headerTextSecondary(CharSequence text) {
9803 this.headerTextSecondary = text;
9804 return this;
9805 }
9806
Selim Cinek384804b2018-04-18 14:31:07 +08009807 final StandardTemplateParams hideLargeIcon(boolean hideLargeIcon) {
9808 this.hideLargeIcon = hideLargeIcon;
Selim Cinek88188f22017-09-19 16:46:56 -07009809 return this;
9810 }
9811
Selim Cinek384804b2018-04-18 14:31:07 +08009812 final StandardTemplateParams hideReplyIcon(boolean hideReplyIcon) {
9813 this.hideReplyIcon = hideReplyIcon;
Selim Cinek88188f22017-09-19 16:46:56 -07009814 return this;
9815 }
9816
Adrian Roos70d7aa32017-01-11 15:39:06 -08009817 final StandardTemplateParams ambient(boolean ambient) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08009818 Preconditions.checkState(title == null && text == null, "must set ambient before text");
Adrian Roos70d7aa32017-01-11 15:39:06 -08009819 this.ambient = ambient;
9820 return this;
9821 }
9822
9823 final StandardTemplateParams fillTextsFrom(Builder b) {
9824 Bundle extras = b.mN.extras;
Lucas Dupin06c5e642017-09-13 16:34:58 -07009825 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE), ambient);
9826
9827 // Big text notifications should contain their content when viewed in ambient mode.
9828 CharSequence text = extras.getCharSequence(EXTRA_BIG_TEXT);
9829 if (!ambient || TextUtils.isEmpty(text)) {
9830 text = extras.getCharSequence(EXTRA_TEXT);
9831 }
9832 this.text = b.processLegacyText(text, ambient);
Adrian Roos70d7aa32017-01-11 15:39:06 -08009833 return this;
9834 }
Selim Cinekbee4e072018-05-21 22:06:43 -07009835
9836 /**
9837 * Set the maximum lines of remote input history lines allowed.
9838 * @param maxRemoteInputHistory The number of lines.
9839 * @return The builder for method chaining.
9840 */
9841 public StandardTemplateParams setMaxRemoteInputHistory(int maxRemoteInputHistory) {
9842 this.maxRemoteInputHistory = maxRemoteInputHistory;
9843 return this;
9844 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08009845 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009846}