blob: 450efdf16656985198cb24802bfc154accf979ec [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Lucas Dupina291d192018-06-07 13:59:42 -070019import static com.android.internal.util.ContrastColorUtil.satisfiesTextContrast;
Selim Cinek389edcd2017-05-11 19:16:44 -070020
Tor Norbye80756e32015-03-02 09:39:27 -080021import android.annotation.ColorInt;
Tor Norbye7b9c9122013-05-30 16:48:33 -070022import android.annotation.DrawableRes;
Gus Prevas9cc96602018-10-11 11:24:23 -040023import android.annotation.IdRes;
Tor Norbyed9273d62013-05-30 15:59:53 -070024import android.annotation.IntDef;
Alex Hillsfd590442016-10-07 09:52:44 -040025import android.annotation.NonNull;
Selim Cinek88188f22017-09-19 16:46:56 -070026import android.annotation.Nullable;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060027import android.annotation.RequiresPermission;
Daniel Sandler01df1c62014-06-09 10:54:01 -040028import android.annotation.SdkConstant;
29import android.annotation.SdkConstant.SdkConstantType;
Julia Reynoldse46bb372016-03-17 11:05:58 -040030import android.annotation.SystemApi;
Mathew Inwood61e8ae62018-08-14 14:17:44 +010031import android.annotation.UnsupportedAppUsage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.content.Context;
33import android.content.Intent;
Kenny Guy77320062014-08-27 21:37:15 +010034import android.content.pm.ApplicationInfo;
Dan Sandler732bd6c2016-04-12 14:20:32 -040035import android.content.pm.PackageManager;
Christoph Studer4600f9b2014-07-22 22:44:43 +020036import android.content.pm.PackageManager.NameNotFoundException;
Julia Reynolds13d898c2017-02-02 12:22:05 -050037import android.content.pm.ShortcutInfo;
Jorim Jaggief72a192014-08-26 21:57:46 +020038import android.content.res.ColorStateList;
Anthony Chenad4d1582017-04-10 16:07:58 -070039import android.content.res.Configuration;
40import android.content.res.Resources;
Gus Prevas9cc96602018-10-11 11:24:23 -040041import android.content.res.TypedArray;
Joe Onoratoef1e7762010-09-17 18:38:38 -040042import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010043import android.graphics.Canvas;
Adrian Roosc1a80b02016-04-05 14:54:55 -070044import android.graphics.Color;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010045import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010046import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040047import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040048import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070049import android.media.AudioManager;
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -080050import android.media.PlayerBase;
Jeff Browndba34ba2014-06-24 20:46:03 -070051import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040053import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020054import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050055import android.os.Bundle;
Dianne Hackborn98305522017-05-05 17:53:53 -070056import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.os.Parcel;
58import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040059import android.os.SystemClock;
Selim Cinek6743c0b2017-01-18 18:24:01 -080060import android.os.SystemProperties;
Jeff Sharkey6d515712012-09-20 16:06:08 -070061import android.os.UserHandle;
Adrian Roosc1a80b02016-04-05 14:54:55 -070062import android.text.BidiFormatter;
Selim Cinek60a54252016-02-26 17:03:25 -080063import android.text.SpannableStringBuilder;
64import android.text.Spanned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.text.TextUtils;
Selim Cinek60a54252016-02-26 17:03:25 -080066import android.text.style.AbsoluteSizeSpan;
Selim Cinek89991a22016-03-07 19:51:54 -080067import android.text.style.CharacterStyle;
Selim Cinek981962e2016-07-20 20:41:58 -070068import android.text.style.ForegroundColorSpan;
Selim Cinek60a54252016-02-26 17:03:25 -080069import android.text.style.RelativeSizeSpan;
70import android.text.style.TextAppearanceSpan;
Svet Ganovddb94882016-06-23 19:55:24 -070071import android.util.ArraySet;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040072import android.util.Log;
Tony Mak638430e2018-10-08 19:19:10 +010073import android.util.Pair;
Dan Sandler50128532015-12-08 15:42:41 -050074import android.util.SparseArray;
Yi Jin6b514142017-10-30 14:54:12 -070075import android.util.proto.ProtoOutputStream;
Griff Hazen61a9e862014-05-22 16:05:19 -070076import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080077import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080078import android.view.View;
Selim Cinek954cc232016-05-20 13:29:23 -070079import android.view.ViewGroup;
Jeff Sharkey1c400132011-08-05 14:50:13 -070080import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import android.widget.RemoteViews;
82
Griff Hazen959591e2014-05-15 22:26:18 -070083import com.android.internal.R;
Selim Cinek389edcd2017-05-11 19:16:44 -070084import com.android.internal.annotations.VisibleForTesting;
Svet Ganovddb94882016-06-23 19:55:24 -070085import com.android.internal.util.ArrayUtils;
Lucas Dupina291d192018-06-07 13:59:42 -070086import com.android.internal.util.ContrastColorUtil;
Adrian Roos0bc3f6a2017-03-06 11:54:05 -080087import com.android.internal.util.Preconditions;
Griff Hazen959591e2014-05-15 22:26:18 -070088
Tor Norbyed9273d62013-05-30 15:59:53 -070089import java.lang.annotation.Retention;
90import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020091import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050092import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070093import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070094import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070095import java.util.List;
Julia Reynolds7217dc92018-03-07 12:12:09 -050096import java.util.Objects;
Dan Sandler50128532015-12-08 15:42:41 -050097import java.util.Set;
Jeff Sharkey23b31182018-04-18 21:32:12 -060098import java.util.function.Consumer;
Joe Onorato561d3852010-11-20 18:09:34 -080099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100/**
101 * A class that represents how a persistent notification is to be presented to
102 * the user using the {@link android.app.NotificationManager}.
103 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800104 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
105 * easier to construct Notifications.</p>
106 *
Joe Fernandez558459f2011-10-13 16:47:36 -0700107 * <div class="special reference">
108 * <h3>Developer Guides</h3>
109 * <p>For a guide to creating notifications, read the
110 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
111 * developer guide.</p>
112 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 */
114public class Notification implements Parcelable
115{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -0400116 private static final String TAG = "Notification";
117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -0400119 * An activity that provides a user interface for adjusting notification preferences for its
Julia Reynolds3aedded2017-03-31 14:42:09 -0400120 * containing application.
Daniel Sandler01df1c62014-06-09 10:54:01 -0400121 */
122 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
123 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
124 = "android.intent.category.NOTIFICATION_PREFERENCES";
125
126 /**
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500127 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
128 * contain a {@link NotificationChannel#getId() channel id} that can be used to narrow down
Julia Reynolds3aedded2017-03-31 14:42:09 -0400129 * what settings should be shown in the target app.
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500130 */
131 public static final String EXTRA_CHANNEL_ID = "android.intent.extra.CHANNEL_ID";
132
133 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -0400134 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds005c8b92017-08-24 10:35:53 -0400135 * contain a {@link NotificationChannelGroup#getId() group id} that can be used to narrow down
136 * what settings should be shown in the target app.
137 */
138 public static final String EXTRA_CHANNEL_GROUP_ID = "android.intent.extra.CHANNEL_GROUP_ID";
139
140 /**
141 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds3aedded2017-03-31 14:42:09 -0400142 * contain the tag provided to {@link NotificationManager#notify(String, int, Notification)}
143 * that can be used to narrow down what settings should be shown in the target app.
144 */
145 public static final String EXTRA_NOTIFICATION_TAG = "android.intent.extra.NOTIFICATION_TAG";
146
147 /**
148 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
149 * contain the id provided to {@link NotificationManager#notify(String, int, Notification)}
150 * that can be used to narrow down what settings should be shown in the target app.
151 */
152 public static final String EXTRA_NOTIFICATION_ID = "android.intent.extra.NOTIFICATION_ID";
153
154 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 * Use all default values (where applicable).
156 */
157 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 /**
160 * Use the default notification sound. This will ignore any given
161 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500162 *
Chris Wren47c20a12014-06-18 17:27:29 -0400163 * <p>
164 * A notification that is noisy is more likely to be presented as a heads-up notification.
165 * </p>
166 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500168 */
169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 public static final int DEFAULT_SOUND = 1;
171
172 /**
173 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500174 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700175 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500176 *
Chris Wren47c20a12014-06-18 17:27:29 -0400177 * <p>
178 * A notification that vibrates is more likely to be presented as a heads-up notification.
179 * </p>
180 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500182 */
183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 /**
187 * Use the default notification lights. This will ignore the
188 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
189 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500190 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500192 */
193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200197 * Maximum length of CharSequences accepted by Builder and friends.
198 *
199 * <p>
200 * Avoids spamming the system with overly large strings such as full e-mails.
201 */
202 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
203
204 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800205 * Maximum entries of reply text that are accepted by Builder and friends.
206 */
207 private static final int MAX_REPLY_HISTORY = 5;
208
Tony Mak09db2ea2018-06-27 18:12:48 +0100209 /**
Gustav Sennton1463d832018-11-06 16:12:48 +0000210 * Maximum number of (generic) action buttons in a notification (contextual action buttons are
211 * handled separately).
Tony Mak09db2ea2018-06-27 18:12:48 +0100212 * @hide
213 */
214 public static final int MAX_ACTION_BUTTONS = 3;
Selim Cinekde4de0e2018-01-24 16:21:07 -0800215
216 /**
217 * If the notification contained an unsent draft for a RemoteInput when the user clicked on it,
218 * we're adding the draft as a String extra to the {@link #contentIntent} using this key.
219 *
220 * <p>Apps may use this extra to prepopulate text fields in the app, where the user usually
221 * sends messages.</p>
222 */
223 public static final String EXTRA_REMOTE_INPUT_DRAFT = "android.remoteInputDraft";
224
Adrian Roose458aa82015-12-08 16:17:19 -0800225 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500226 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800227 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500228 * Default value: {@link System#currentTimeMillis() Now}.
229 *
230 * Choose a timestamp that will be most relevant to the user. For most finite events, this
231 * corresponds to the time the event happened (or will happen, in the case of events that have
232 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800233 * timestamped according to when the activity began.
234 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500235 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800236 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500237 * <ul>
238 * <li>Notification of a new chat message should be stamped when the message was received.</li>
239 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
240 * <li>Notification of a completed file download should be stamped when the download finished.</li>
241 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
242 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
243 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800244 * </ul>
245 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700246 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
247 * anymore by default and must be opted into by using
248 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 */
250 public long when;
251
252 /**
Selim Cinekb85f36fd2016-04-20 18:46:36 -0700253 * The creation time of the notification
254 */
255 private long creationTime;
256
257 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400259 *
260 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 */
Dan Sandler86647982015-05-13 23:41:13 -0400262 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700263 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 public int icon;
265
266 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800267 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
268 * leave it at its default value of 0.
269 *
270 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700271 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800272 */
273 public int iconLevel;
274
275 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500276 * The number of events that this notification represents. For example, in a new mail
277 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800278 *
Julia Reynolds30331982017-04-27 10:12:50 -0400279 * The system may or may not use this field to modify the appearance of the notification.
Julia Reynolds13d898c2017-02-02 12:22:05 -0500280 * Starting with {@link android.os.Build.VERSION_CODES#O}, the number may be displayed as a
281 * badge icon in Launchers that support badging.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 */
Julia Reynolds30331982017-04-27 10:12:50 -0400283 public int number = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284
285 /**
286 * The intent to execute when the expanded status entry is clicked. If
287 * this is an activity, it must include the
288 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800289 * that you take care of task management as described in the
290 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800291 * Stack</a> document. In particular, make sure to read the notification section
292 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
293 * Notifications</a> for the correct ways to launch an application from a
294 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 */
296 public PendingIntent contentIntent;
297
298 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500299 * The intent to execute when the notification is explicitly dismissed by the user, either with
300 * the "Clear All" button or by swiping it away individually.
301 *
302 * This probably shouldn't be launching an activity since several of those will be sent
303 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 */
305 public PendingIntent deleteIntent;
306
307 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700308 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800309 *
Chris Wren47c20a12014-06-18 17:27:29 -0400310 * <p>
311 * The system UI may choose to display a heads-up notification, instead of
312 * launching this intent, while the user is using the device.
313 * </p>
314 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800315 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400316 */
317 public PendingIntent fullScreenIntent;
318
319 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400320 * Text that summarizes this notification for accessibility services.
321 *
322 * As of the L release, this text is no longer shown on screen, but it is still useful to
323 * accessibility services (where it serves as an audible announcement of the notification's
324 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400325 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800326 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 */
328 public CharSequence tickerText;
329
330 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400331 * Formerly, a view showing the {@link #tickerText}.
332 *
333 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400334 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400335 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800336 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400337
338 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400339 * The view that will represent this notification in the notification list (which is pulled
340 * down from the status bar).
341 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500342 * As of N, this field may be null. The notification view is determined by the inputs
343 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400344 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400346 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 public RemoteViews contentView;
348
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400349 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400350 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400351 * opportunity to show more detail. The system UI may choose to show this
352 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400353 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500354 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400355 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
356 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400357 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400358 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400359 public RemoteViews bigContentView;
360
Chris Wren8fd39ec2014-02-27 17:43:26 -0500361
362 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400363 * A medium-format version of {@link #contentView}, providing the Notification an
364 * opportunity to add action buttons to contentView. At its discretion, the system UI may
365 * choose to show this as a heads-up notification, which will pop up so the user can see
366 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400367 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500368 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400369 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
370 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500371 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400372 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500373 public RemoteViews headsUpContentView;
374
Julia Reynoldsfc640012018-02-21 12:25:27 -0500375 private boolean mUsesStandardHeader;
376
377 private static final ArraySet<Integer> STANDARD_LAYOUTS = new ArraySet<>();
378 static {
379 STANDARD_LAYOUTS.add(R.layout.notification_template_material_base);
380 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_base);
381 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_picture);
382 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_text);
383 STANDARD_LAYOUTS.add(R.layout.notification_template_material_inbox);
384 STANDARD_LAYOUTS.add(R.layout.notification_template_material_messaging);
385 STANDARD_LAYOUTS.add(R.layout.notification_template_material_media);
386 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_media);
387 STANDARD_LAYOUTS.add(R.layout.notification_template_ambient_header);
388 STANDARD_LAYOUTS.add(R.layout.notification_template_header);
389 STANDARD_LAYOUTS.add(R.layout.notification_template_material_ambient);
390 }
391
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400392 /**
Dan Sandler86647982015-05-13 23:41:13 -0400393 * A large bitmap to be shown in the notification content area.
394 *
395 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 */
Dan Sandler86647982015-05-13 23:41:13 -0400397 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800398 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399
400 /**
401 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500402 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400404 * A notification that is noisy is more likely to be presented as a heads-up notification.
405 * </p>
406 *
407 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500408 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500410 * @deprecated use {@link NotificationChannel#getSound()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500412 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 public Uri sound;
414
415 /**
416 * Use this constant as the value for audioStreamType to request that
417 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700418 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400419 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500420 * @deprecated Use {@link NotificationChannel#getAudioAttributes()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700422 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 public static final int STREAM_DEFAULT = -1;
424
425 /**
426 * The audio stream type to use when playing the sound.
427 * Should be one of the STREAM_ constants from
428 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400429 *
430 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700432 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 public int audioStreamType = STREAM_DEFAULT;
434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400436 * The default value of {@link #audioAttributes}.
437 */
438 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
439 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
440 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
441 .build();
442
443 /**
444 * The {@link AudioAttributes audio attributes} to use when playing the sound.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500445 *
446 * @deprecated use {@link NotificationChannel#getAudioAttributes()} instead.
John Spurlockc0650f022014-07-19 13:22:39 -0400447 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500448 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -0400449 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
450
451 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500452 * The pattern with which to vibrate.
453 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 * <p>
455 * To vibrate the default pattern, see {@link #defaults}.
456 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500457 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 * @see android.os.Vibrator#vibrate(long[],int)
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500459 * @deprecated use {@link NotificationChannel#getVibrationPattern()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500461 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 public long[] vibrate;
463
464 /**
465 * The color of the led. The hardware will do its best approximation.
466 *
467 * @see #FLAG_SHOW_LIGHTS
468 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500469 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 */
Tor Norbye80756e32015-03-02 09:39:27 -0800471 @ColorInt
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500472 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 public int ledARGB;
474
475 /**
476 * The number of milliseconds for the LED to be on while it's flashing.
477 * The hardware will do its best approximation.
478 *
479 * @see #FLAG_SHOW_LIGHTS
480 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500481 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500483 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 public int ledOnMS;
485
486 /**
487 * The number of milliseconds for the LED to be off while it's flashing.
488 * The hardware will do its best approximation.
489 *
490 * @see #FLAG_SHOW_LIGHTS
491 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500492 *
493 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500495 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 public int ledOffMS;
497
498 /**
499 * Specifies which values should be taken from the defaults.
500 * <p>
501 * To set, OR the desired from {@link #DEFAULT_SOUND},
502 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
503 * values, use {@link #DEFAULT_ALL}.
504 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500505 *
506 * @deprecated use {@link NotificationChannel#getSound()} and
507 * {@link NotificationChannel#shouldShowLights()} and
508 * {@link NotificationChannel#shouldVibrate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500510 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 public int defaults;
512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 /**
514 * Bit to be bitwise-ored into the {@link #flags} field that should be
515 * set if you want the LED on for this notification.
516 * <ul>
517 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
518 * or 0 for both ledOnMS and ledOffMS.</li>
519 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
520 * <li>To flash the LED, pass the number of milliseconds that it should
521 * be on and off to ledOnMS and ledOffMS.</li>
522 * </ul>
523 * <p>
524 * Since hardware varies, you are not guaranteed that any of the values
Ricardo Loo Forondaf8f6d0a2018-01-25 08:42:43 -0800525 * you pass are honored exactly. Use the system defaults if possible
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 * because they will be set to values that work on any given hardware.
527 * <p>
528 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500529 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500530 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500532 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
534
535 /**
536 * Bit to be bitwise-ored into the {@link #flags} field that should be
537 * set if this notification is in reference to something that is ongoing,
538 * like a phone call. It should not be set if this notification is in
539 * reference to something that happened at a particular point in time,
540 * like a missed phone call.
541 */
542 public static final int FLAG_ONGOING_EVENT = 0x00000002;
543
544 /**
545 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700546 * the audio will be repeated until the notification is
547 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 */
549 public static final int FLAG_INSISTENT = 0x00000004;
550
551 /**
552 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700553 * set if you would only like the sound, vibrate and ticker to be played
554 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 */
556 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
557
558 /**
559 * Bit to be bitwise-ored into the {@link #flags} field that should be
560 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500561 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 */
563 public static final int FLAG_AUTO_CANCEL = 0x00000010;
564
565 /**
566 * Bit to be bitwise-ored into the {@link #flags} field that should be
567 * set if the notification should not be canceled when the user clicks
568 * the Clear all button.
569 */
570 public static final int FLAG_NO_CLEAR = 0x00000020;
571
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700572 /**
573 * Bit to be bitwise-ored into the {@link #flags} field that should be
574 * set if this notification represents a currently running service. This
575 * will normally be set for you by {@link Service#startForeground}.
576 */
577 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
578
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400579 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500580 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800581 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500582 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400583 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700584 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500585 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400586
Griff Hazendfcb0802014-02-11 12:00:00 -0800587 /**
588 * Bit to be bitswise-ored into the {@link #flags} field that should be
589 * set if this notification is relevant to the current device only
590 * and it is not recommended that it bridge to other devices.
591 */
592 public static final int FLAG_LOCAL_ONLY = 0x00000100;
593
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700594 /**
595 * Bit to be bitswise-ored into the {@link #flags} field that should be
596 * set if this notification is the group summary for a group of notifications.
597 * Grouped notifications may display in a cluster or stack on devices which
598 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
599 */
600 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
601
Julia Reynoldse46bb372016-03-17 11:05:58 -0400602 /**
603 * Bit to be bitswise-ored into the {@link #flags} field that should be
604 * set if this notification is the group summary for an auto-group of notifications.
605 *
606 * @hide
607 */
608 @SystemApi
609 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
610
Julia Reynolds4db59552017-06-30 13:34:01 -0400611 /**
612 * @hide
613 */
614 public static final int FLAG_CAN_COLORIZE = 0x00000800;
615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 public int flags;
617
Tor Norbyed9273d62013-05-30 15:59:53 -0700618 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700619 @IntDef(prefix = { "PRIORITY_" }, value = {
620 PRIORITY_DEFAULT,
621 PRIORITY_LOW,
622 PRIORITY_MIN,
623 PRIORITY_HIGH,
624 PRIORITY_MAX
625 })
Tor Norbyed9273d62013-05-30 15:59:53 -0700626 @Retention(RetentionPolicy.SOURCE)
627 public @interface Priority {}
628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500630 * Default notification {@link #priority}. If your application does not prioritize its own
631 * notifications, use this value for all notifications.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500632 *
633 * @deprecated use {@link NotificationManager#IMPORTANCE_DEFAULT} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500634 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500635 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500636 public static final int PRIORITY_DEFAULT = 0;
637
638 /**
639 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
640 * items smaller, or at a different position in the list, compared with your app's
641 * {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500642 *
643 * @deprecated use {@link NotificationManager#IMPORTANCE_LOW} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500644 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500645 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500646 public static final int PRIORITY_LOW = -1;
647
648 /**
649 * Lowest {@link #priority}; these items might not be shown to the user except under special
650 * circumstances, such as detailed notification logs.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500651 *
652 * @deprecated use {@link NotificationManager#IMPORTANCE_MIN} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500653 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500654 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500655 public static final int PRIORITY_MIN = -2;
656
657 /**
658 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
659 * show these items larger, or at a different position in notification lists, compared with
660 * your app's {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500661 *
662 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500663 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500664 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500665 public static final int PRIORITY_HIGH = 1;
666
667 /**
668 * Highest {@link #priority}, for your application's most important items that require the
669 * user's prompt attention or input.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500670 *
671 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500672 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500673 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500674 public static final int PRIORITY_MAX = 2;
675
676 /**
677 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800678 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500679 * Priority is an indication of how much of the user's valuable attention should be consumed by
680 * this notification. Low-priority notifications may be hidden from the user in certain
681 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500682 * system will make a determination about how to interpret this priority when presenting
683 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400684 *
685 * <p>
686 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
687 * as a heads-up notification.
688 * </p>
689 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500690 * @deprecated use {@link NotificationChannel#getImportance()} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500691 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700692 @Priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500693 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500694 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800695
Dan Sandler26e81cf2014-05-06 10:01:27 -0400696 /**
697 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
698 * to be applied by the standard Style templates when presenting this notification.
699 *
700 * The current template design constructs a colorful header image by overlaying the
701 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
702 * ignored.
703 */
Tor Norbye80756e32015-03-02 09:39:27 -0800704 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400705 public int color = COLOR_DEFAULT;
706
707 /**
708 * Special value of {@link #color} telling the system not to decorate this notification with
709 * any special color but instead use default colors when presenting this notification.
710 */
Tor Norbye80756e32015-03-02 09:39:27 -0800711 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400712 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600713
714 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800715 * Special value of {@link #color} used as a place holder for an invalid color.
Selim Cinek99104832017-01-25 14:47:33 -0800716 * @hide
Adrian Roos4ff3b122016-02-01 12:26:13 -0800717 */
718 @ColorInt
Selim Cinek99104832017-01-25 14:47:33 -0800719 public static final int COLOR_INVALID = 1;
Adrian Roos4ff3b122016-02-01 12:26:13 -0800720
721 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700722 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
723 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600724 * lockscreen).
725 *
726 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
727 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
728 * shown in all situations, but the contents are only available if the device is unlocked for
729 * the appropriate user.
730 *
731 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
732 * can be read even in an "insecure" context (that is, above a secure lockscreen).
733 * To modify the public version of this notification—for example, to redact some portions—see
734 * {@link Builder#setPublicVersion(Notification)}.
735 *
736 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
737 * and ticker until the user has bypassed the lockscreen.
738 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600739 public @Visibility int visibility;
740
741 /** @hide */
742 @IntDef(prefix = { "VISIBILITY_" }, value = {
743 VISIBILITY_PUBLIC,
744 VISIBILITY_PRIVATE,
745 VISIBILITY_SECRET,
746 })
747 @Retention(RetentionPolicy.SOURCE)
748 public @interface Visibility {}
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600749
Griff Hazenfc3922d2014-08-20 11:56:44 -0700750 /**
751 * Notification visibility: Show this notification in its entirety on all lockscreens.
752 *
753 * {@see #visibility}
754 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600755 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700756
757 /**
758 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
759 * private information on secure lockscreens.
760 *
761 * {@see #visibility}
762 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600763 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700764
765 /**
766 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
767 *
768 * {@see #visibility}
769 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600770 public static final int VISIBILITY_SECRET = -1;
771
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500772 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400773 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500774 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400775 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500776
777 /**
Adora Zhangaa90e872018-03-12 14:07:50 -0700778 * Notification category: map turn-by-turn navigation.
779 */
780 public static final String CATEGORY_NAVIGATION = "navigation";
781
782 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400783 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500784 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400785 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500786
787 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400788 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500789 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400790 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500791
792 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400793 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500794 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400795 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500796
797 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400798 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500799 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400800 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500801
802 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400803 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500804 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400805 public static final String CATEGORY_ALARM = "alarm";
806
807 /**
808 * Notification category: progress of a long-running background operation.
809 */
810 public static final String CATEGORY_PROGRESS = "progress";
811
812 /**
813 * Notification category: social network or sharing update.
814 */
815 public static final String CATEGORY_SOCIAL = "social";
816
817 /**
818 * Notification category: error in background operation or authentication status.
819 */
820 public static final String CATEGORY_ERROR = "err";
821
822 /**
823 * Notification category: media transport control for playback.
824 */
825 public static final String CATEGORY_TRANSPORT = "transport";
826
827 /**
828 * Notification category: system or device status update. Reserved for system use.
829 */
830 public static final String CATEGORY_SYSTEM = "sys";
831
832 /**
833 * Notification category: indication of running background service.
834 */
835 public static final String CATEGORY_SERVICE = "service";
836
837 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400838 * Notification category: a specific, timely recommendation for a single thing.
839 * For example, a news app might want to recommend a news story it believes the user will
840 * want to read next.
841 */
842 public static final String CATEGORY_RECOMMENDATION = "recommendation";
843
844 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400845 * Notification category: ongoing information about device or contextual status.
846 */
847 public static final String CATEGORY_STATUS = "status";
848
849 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400850 * Notification category: user-scheduled reminder.
851 */
852 public static final String CATEGORY_REMINDER = "reminder";
853
854 /**
Adora Zhangaa90e872018-03-12 14:07:50 -0700855 * Notification category: extreme car emergencies.
856 * @hide
857 */
858 @SystemApi
859 public static final String CATEGORY_CAR_EMERGENCY = "car_emergency";
860
861 /**
862 * Notification category: car warnings.
863 * @hide
864 */
865 @SystemApi
866 public static final String CATEGORY_CAR_WARNING = "car_warning";
867
868 /**
869 * Notification category: general car system information.
870 * @hide
871 */
872 @SystemApi
873 public static final String CATEGORY_CAR_INFORMATION = "car_information";
874
875 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400876 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
877 * that best describes this Notification. May be used by the system for ranking and filtering.
878 */
879 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500880
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100881 @UnsupportedAppUsage
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700882 private String mGroupKey;
883
884 /**
885 * Get the key used to group this notification into a cluster or stack
886 * with other notifications on devices which support such rendering.
887 */
888 public String getGroup() {
889 return mGroupKey;
890 }
891
892 private String mSortKey;
893
894 /**
895 * Get a sort key that orders this notification among other notifications from the
896 * same package. This can be useful if an external sort was already applied and an app
897 * would like to preserve this. Notifications will be sorted lexicographically using this
898 * value, although providing different priorities in addition to providing sort key may
899 * cause this value to be ignored.
900 *
901 * <p>This sort key can also be used to order members of a notification group. See
902 * {@link Builder#setGroup}.
903 *
904 * @see String#compareTo(String)
905 */
906 public String getSortKey() {
907 return mSortKey;
908 }
909
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500910 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400911 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400912 * <p>
913 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
914 * APIs, and are intended to be used by
915 * {@link android.service.notification.NotificationListenerService} implementations to extract
916 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500917 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400918 public Bundle extras = new Bundle();
919
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400920 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700921 * All pending intents in the notification as the system needs to be able to access them but
922 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700923 * custom parcelable objects.
924 *
925 * @hide
926 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100927 @UnsupportedAppUsage
Felipe Lemedd85da62016-06-28 11:29:54 -0700928 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700929
930 /**
Dianne Hackborn98305522017-05-05 17:53:53 -0700931 * Token identifying the notification that is applying doze/bgcheck whitelisting to the
932 * pending intents inside of it, so only those will get the behavior.
933 *
934 * @hide
935 */
Adrian Roosfb921842017-10-26 14:49:56 +0200936 private IBinder mWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -0700937
938 /**
939 * Must be set by a process to start associating tokens with Notification objects
940 * coming in to it. This is set by NotificationManagerService.
941 *
942 * @hide
943 */
944 static public IBinder processWhitelistToken;
945
946 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400947 * {@link #extras} key: this is the title of the notification,
948 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
949 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500950 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400951
952 /**
953 * {@link #extras} key: this is the title of the notification when shown in expanded form,
954 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
955 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400956 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400957
958 /**
959 * {@link #extras} key: this is the main text payload, as supplied to
960 * {@link Builder#setContentText(CharSequence)}.
961 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500962 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400963
964 /**
965 * {@link #extras} key: this is a third line of text, as supplied to
966 * {@link Builder#setSubText(CharSequence)}.
967 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400968 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400969
970 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800971 * {@link #extras} key: this is the remote input history, as supplied to
972 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700973 *
974 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
975 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
976 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
977 * notifications once the other party has responded).
978 *
979 * The extra with this key is of type CharSequence[] and contains the most recent entry at
980 * the 0 index, the second most recent at the 1 index, etc.
981 *
982 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800983 */
984 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
985
986 /**
Kenny Guya0f6de82018-04-06 16:20:16 +0100987 * {@link #extras} key: boolean as supplied to
988 * {@link Builder#setShowRemoteInputSpinner(boolean)}.
989 *
990 * If set to true, then the view displaying the remote input history from
991 * {@link Builder#setRemoteInputHistory(CharSequence[])} will have a progress spinner.
992 *
993 * @see Builder#setShowRemoteInputSpinner(boolean)
994 * @hide
995 */
996 public static final String EXTRA_SHOW_REMOTE_INPUT_SPINNER = "android.remoteInputSpinner";
997
998 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +0100999 * {@link #extras} key: boolean as supplied to
1000 * {@link Builder#setHideSmartReplies(boolean)}.
1001 *
1002 * If set to true, then any smart reply buttons will be hidden.
1003 *
1004 * @see Builder#setHideSmartReplies(boolean)
1005 * @hide
1006 */
1007 public static final String EXTRA_HIDE_SMART_REPLIES = "android.hideSmartReplies";
1008
1009 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001010 * {@link #extras} key: this is a small piece of additional text as supplied to
1011 * {@link Builder#setContentInfo(CharSequence)}.
1012 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001013 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001014
1015 /**
1016 * {@link #extras} key: this is a line of summary information intended to be shown
1017 * alongside expanded notifications, as supplied to (e.g.)
1018 * {@link BigTextStyle#setSummaryText(CharSequence)}.
1019 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001020 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001021
1022 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02001023 * {@link #extras} key: this is the longer text shown in the big form of a
1024 * {@link BigTextStyle} notification, as supplied to
1025 * {@link BigTextStyle#bigText(CharSequence)}.
1026 */
1027 public static final String EXTRA_BIG_TEXT = "android.bigText";
1028
1029 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001030 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
1031 * supplied to {@link Builder#setSmallIcon(int)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -04001032 *
1033 * @deprecated Use {@link #getSmallIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001034 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04001035 @Deprecated
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001036 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001037
1038 /**
1039 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
1040 * notification payload, as
1041 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -04001042 *
1043 * @deprecated Use {@link #getLargeIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001044 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04001045 @Deprecated
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001046 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001047
1048 /**
1049 * {@link #extras} key: this is a bitmap to be used instead of the one from
1050 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
1051 * shown in its expanded form, as supplied to
1052 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
1053 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001054 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001055
1056 /**
1057 * {@link #extras} key: this is the progress value supplied to
1058 * {@link Builder#setProgress(int, int, boolean)}.
1059 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001060 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001061
1062 /**
1063 * {@link #extras} key: this is the maximum value supplied to
1064 * {@link Builder#setProgress(int, int, boolean)}.
1065 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001066 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001067
1068 /**
1069 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
1070 * {@link Builder#setProgress(int, int, boolean)}.
1071 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001072 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001073
1074 /**
1075 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
1076 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
1077 * {@link Builder#setUsesChronometer(boolean)}.
1078 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001079 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001080
1081 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08001082 * {@link #extras} key: whether the chronometer set on the notification should count down
1083 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -07001084 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -08001085 */
Adrian Roos96b7e202016-05-17 13:50:38 -07001086 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -08001087
1088 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001089 * {@link #extras} key: whether {@link #when} should be shown,
1090 * as supplied to {@link Builder#setShowWhen(boolean)}.
1091 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001092 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001093
1094 /**
1095 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
1096 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
1097 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001098 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001099
1100 /**
1101 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
1102 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
1103 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001104 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -04001105
1106 /**
1107 * {@link #extras} key: A string representing the name of the specific
1108 * {@link android.app.Notification.Style} used to create this notification.
1109 */
Chris Wren91ad5632013-06-05 15:05:57 -04001110 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001111
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001112 /**
Chris Wrene6c48932014-09-29 17:19:27 -04001113 * {@link #extras} key: A String array containing the people that this notification relates to,
1114 * each of which was supplied to {@link Builder#addPerson(String)}.
Selim Cineke7238dd2017-12-14 17:48:32 -08001115 *
1116 * @deprecated the actual objects are now in {@link #EXTRA_PEOPLE_LIST}
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001117 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001118 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001119
1120 /**
Selim Cineke7238dd2017-12-14 17:48:32 -08001121 * {@link #extras} key: An arrayList of {@link Person} objects containing the people that
1122 * this notification relates to.
1123 */
1124 public static final String EXTRA_PEOPLE_LIST = "android.people.list";
1125
1126 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04001127 * Allow certain system-generated notifications to appear before the device is provisioned.
1128 * Only available to notifications coming from the android package.
1129 * @hide
1130 */
Maurice Lam96c10032017-03-29 15:42:38 -07001131 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001132 @RequiresPermission(android.Manifest.permission.NOTIFICATION_DURING_SETUP)
John Spurlockfd7f1e02014-03-18 16:41:57 -04001133 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
1134
1135 /**
Robin Leed107af62018-04-27 13:55:56 +02001136 * {@link #extras} key:
1137 * flat {@link String} representation of a {@link android.content.ContentUris content URI}
1138 * pointing to an image that can be displayed in the background when the notification is
1139 * selected. Used on television platforms. The URI must point to an image stream suitable for
1140 * passing into {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001141 * BitmapFactory.decodeStream}; all other content types will be ignored.
Jose Limae9e3b3b2014-05-18 23:44:50 -07001142 */
1143 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
1144
1145 /**
Dan Sandler842dd772014-05-15 09:36:47 -04001146 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -07001147 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -04001148 * {@link android.app.Notification.MediaStyle} notification.
1149 */
1150 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
1151
1152 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +01001153 * {@link #extras} key: the indices of actions to be shown in the compact view,
1154 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
1155 */
1156 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
1157
Christoph Studer943aa672014-08-03 20:31:16 +02001158 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04001159 * {@link #extras} key: the username to be displayed for all messages sent by the user including
1160 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -07001161 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1162 * {@link CharSequence}
Selim Cinekcb8b9852017-12-15 18:01:52 -08001163 *
1164 * @deprecated use {@link #EXTRA_MESSAGING_PERSON}
Alex Hillsfc737de2016-03-23 17:33:02 -04001165 */
1166 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
1167
1168 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08001169 * {@link #extras} key: the person to be displayed for all messages sent by the user including
1170 * direct replies
1171 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1172 * {@link Person}
1173 */
1174 public static final String EXTRA_MESSAGING_PERSON = "android.messagingUser";
1175
1176 /**
Adrian Roos96b7e202016-05-17 13:50:38 -07001177 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -04001178 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -04001179 */
Alex Hillsd9b04d92016-04-11 16:38:16 -04001180 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -04001181
1182 /**
1183 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
1184 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -07001185 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1186 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -04001187 */
1188 public static final String EXTRA_MESSAGES = "android.messages";
1189
1190 /**
Adrian Roos437cd562017-01-18 15:47:03 -08001191 * {@link #extras} key: an array of
1192 * {@link android.app.Notification.MessagingStyle#addHistoricMessage historic}
1193 * {@link android.app.Notification.MessagingStyle.Message} bundles provided by a
1194 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1195 * array of bundles.
1196 */
1197 public static final String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
1198
1199 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08001200 * {@link #extras} key: whether the {@link android.app.Notification.MessagingStyle} notification
1201 * represents a group conversation.
1202 */
1203 public static final String EXTRA_IS_GROUP_CONVERSATION = "android.isGroupConversation";
1204
1205 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08001206 * {@link #extras} key: whether the notification should be colorized as
1207 * supplied to {@link Builder#setColorized(boolean)}}.
1208 */
1209 public static final String EXTRA_COLORIZED = "android.colorized";
1210
1211 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001212 * @hide
1213 */
1214 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
1215
Selim Cinek247fa012016-02-18 09:50:48 -08001216 /**
1217 * @hide
1218 */
1219 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
1220
Shane Brennan472a3b32016-12-12 15:28:10 -08001221 /**
Selim Cinekd0426622017-07-11 13:19:59 +02001222 * @hide
1223 */
1224 public static final String EXTRA_REDUCED_IMAGES = "android.reduced.images";
1225
1226 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001227 * {@link #extras} key: the audio contents of this notification.
1228 *
1229 * This is for use when rendering the notification on an audio-focused interface;
1230 * the audio contents are a complete sound sample that contains the contents/body of the
1231 * notification. This may be used in substitute of a Text-to-Speech reading of the
1232 * notification. For example if the notification represents a voice message this should point
1233 * to the audio of that message.
1234 *
1235 * The data stored under this key should be a String representation of a Uri that contains the
1236 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1237 *
1238 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1239 * has a field for holding data URI. That field can be used for audio.
1240 * See {@code Message#setData}.
1241 *
1242 * Example usage:
1243 * <pre>
1244 * {@code
1245 * Notification.Builder myBuilder = (build your Notification as normal);
1246 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1247 * }
1248 * </pre>
1249 */
1250 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1251
Dan Sandler80eaa592016-04-14 23:34:54 -04001252 /** @hide */
1253 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001254 @RequiresPermission(android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME)
Dan Sandler732bd6c2016-04-12 14:20:32 -04001255 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1256
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001257 /**
Julia Reynolds3c7de112018-03-28 09:33:13 -04001258 * This is set on the notifications shown by system_server about apps running foreground
1259 * services. It indicates that the notification should be shown
1260 * only if any of the given apps do not already have a properly tagged
1261 * {@link #FLAG_FOREGROUND_SERVICE} notification currently visible to the user.
1262 * This is a string array of all package names of the apps.
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001263 * @hide
1264 */
1265 public static final String EXTRA_FOREGROUND_APPS = "android.foregroundApps";
1266
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001267 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04001268 private Icon mSmallIcon;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001269 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04001270 private Icon mLargeIcon;
1271
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001272 @UnsupportedAppUsage
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001273 private String mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001274 private long mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001275
Julia Reynolds13d898c2017-02-02 12:22:05 -05001276 private String mShortcutId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04001277 private CharSequence mSettingsText;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001278
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04001279 private PendingIntent mAppOverlayIntent;
1280
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001281 /** @hide */
1282 @IntDef(prefix = { "GROUP_ALERT_" }, value = {
1283 GROUP_ALERT_ALL, GROUP_ALERT_CHILDREN, GROUP_ALERT_SUMMARY
1284 })
1285 @Retention(RetentionPolicy.SOURCE)
1286 public @interface GroupAlertBehavior {}
1287
1288 /**
1289 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all notifications in a
1290 * group with sound or vibration ought to make sound or vibrate (respectively), so this
1291 * notification will not be muted when it is in a group.
1292 */
1293 public static final int GROUP_ALERT_ALL = 0;
1294
1295 /**
1296 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all children
1297 * notification in a group should be silenced (no sound or vibration) even if they are posted
1298 * to a {@link NotificationChannel} that has sound and/or vibration. Use this constant to
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001299 * mute this notification if this notification is a group child. This must be applied to all
1300 * children notifications you want to mute.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001301 *
1302 * <p> For example, you might want to use this constant if you post a number of children
1303 * notifications at once (say, after a periodic sync), and only need to notify the user
1304 * audibly once.
1305 */
1306 public static final int GROUP_ALERT_SUMMARY = 1;
1307
1308 /**
1309 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that the summary
1310 * notification in a group should be silenced (no sound or vibration) even if they are
1311 * posted to a {@link NotificationChannel} that has sound and/or vibration. Use this constant
1312 * to mute this notification if this notification is a group summary.
1313 *
1314 * <p>For example, you might want to use this constant if only the children notifications
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001315 * in your group have content and the summary is only used to visually group notifications
1316 * rather than to alert the user that new information is available.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001317 */
1318 public static final int GROUP_ALERT_CHILDREN = 2;
1319
Julia Reynolds2f431e22017-06-07 14:12:09 +00001320 private int mGroupAlertBehavior = GROUP_ALERT_ALL;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001321
Julia Reynolds13d898c2017-02-02 12:22:05 -05001322 /**
1323 * If this notification is being shown as a badge, always show as a number.
1324 */
1325 public static final int BADGE_ICON_NONE = 0;
1326
1327 /**
1328 * If this notification is being shown as a badge, use the {@link #getSmallIcon()} to
1329 * represent this notification.
1330 */
1331 public static final int BADGE_ICON_SMALL = 1;
1332
1333 /**
1334 * If this notification is being shown as a badge, use the {@link #getLargeIcon()} to
1335 * represent this notification.
1336 */
1337 public static final int BADGE_ICON_LARGE = 2;
Julia Reynolds7d5b4da2017-03-20 10:18:49 -04001338 private int mBadgeIcon = BADGE_ICON_NONE;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001339
Chris Wren51c75102013-07-16 20:49:17 -04001340 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001341 * Structure to encapsulate a named action that can be shown as part of this notification.
1342 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1343 * selected by the user.
1344 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001345 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1346 * or {@link Notification.Builder#addAction(Notification.Action)}
1347 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001348 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001349 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001350 /**
1351 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1352 * {@link RemoteInput}s.
1353 *
1354 * This is intended for {@link RemoteInput}s that only accept data, meaning
1355 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
1356 * is null or empty, and {@link RemoteInput#getAllowedDataTypes is non-null and not
1357 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1358 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1359 *
1360 * You can test if a RemoteInput matches these constraints using
1361 * {@link RemoteInput#isDataOnly}.
1362 */
1363 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1364
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001365 /**
1366 * {@link }: No semantic action defined.
1367 */
1368 public static final int SEMANTIC_ACTION_NONE = 0;
1369
1370 /**
1371 * {@code SemanticAction}: Reply to a conversation, chat, group, or wherever replies
1372 * may be appropriate.
1373 */
1374 public static final int SEMANTIC_ACTION_REPLY = 1;
1375
1376 /**
1377 * {@code SemanticAction}: Mark content as read.
1378 */
1379 public static final int SEMANTIC_ACTION_MARK_AS_READ = 2;
1380
1381 /**
1382 * {@code SemanticAction}: Mark content as unread.
1383 */
1384 public static final int SEMANTIC_ACTION_MARK_AS_UNREAD = 3;
1385
1386 /**
1387 * {@code SemanticAction}: Delete the content associated with the notification. This
1388 * could mean deleting an email, message, etc.
1389 */
1390 public static final int SEMANTIC_ACTION_DELETE = 4;
1391
1392 /**
1393 * {@code SemanticAction}: Archive the content associated with the notification. This
1394 * could mean archiving an email, message, etc.
1395 */
1396 public static final int SEMANTIC_ACTION_ARCHIVE = 5;
1397
1398 /**
1399 * {@code SemanticAction}: Mute the content associated with the notification. This could
1400 * mean silencing a conversation or currently playing media.
1401 */
1402 public static final int SEMANTIC_ACTION_MUTE = 6;
1403
1404 /**
1405 * {@code SemanticAction}: Unmute the content associated with the notification. This could
1406 * mean un-silencing a conversation or currently playing media.
1407 */
1408 public static final int SEMANTIC_ACTION_UNMUTE = 7;
1409
1410 /**
1411 * {@code SemanticAction}: Mark content with a thumbs up.
1412 */
1413 public static final int SEMANTIC_ACTION_THUMBS_UP = 8;
1414
1415 /**
1416 * {@code SemanticAction}: Mark content with a thumbs down.
1417 */
1418 public static final int SEMANTIC_ACTION_THUMBS_DOWN = 9;
1419
Kodlee Yincda5b092018-02-15 15:34:53 -08001420 /**
1421 * {@code SemanticAction}: Call a contact, group, etc.
1422 */
1423 public static final int SEMANTIC_ACTION_CALL = 10;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001424
Gustav Sennton1463d832018-11-06 16:12:48 +00001425 /**
1426 * {@code SemanticAction}: Contextual action - dependent on the current notification. E.g.
1427 * open a Map application with an address shown in the notification.
1428 */
1429 public static final int SEMANTIC_ACTION_CONTEXTUAL_SUGGESTION = 11;
1430
Griff Hazen959591e2014-05-15 22:26:18 -07001431 private final Bundle mExtras;
Mathew Inwood8c854f82018-09-14 12:35:36 +01001432 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Dan Sandler86647982015-05-13 23:41:13 -04001433 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001434 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001435 private boolean mAllowGeneratedReplies = true;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001436 private final @SemanticAction int mSemanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001437
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001438 /**
1439 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001440 *
1441 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001442 */
Dan Sandler86647982015-05-13 23:41:13 -04001443 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001444 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001445
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001446 /**
1447 * Title of the action.
1448 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001449 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001450
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001451 /**
1452 * Intent to send when the user invokes this action. May be null, in which case the action
1453 * may be rendered in a disabled presentation by the system UI.
1454 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001455 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001456
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001457 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001458 if (in.readInt() != 0) {
1459 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001460 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1461 icon = mIcon.getResId();
1462 }
Dan Sandler86647982015-05-13 23:41:13 -04001463 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001464 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1465 if (in.readInt() == 1) {
1466 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1467 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001468 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001469 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001470 mAllowGeneratedReplies = in.readInt() == 1;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001471 mSemanticAction = in.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001472 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001473
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001474 /**
Dan Sandler86647982015-05-13 23:41:13 -04001475 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001476 */
Dan Sandler86647982015-05-13 23:41:13 -04001477 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001478 public Action(int icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001479 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true,
1480 SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001481 }
1482
Adrian Roos7af53622016-10-12 13:44:05 -07001483 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001484 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001485 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1486 @SemanticAction int semanticAction) {
Dan Sandler86647982015-05-13 23:41:13 -04001487 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001488 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1489 this.icon = icon.getResId();
1490 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001491 this.title = title;
1492 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001493 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001494 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001495 this.mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001496 this.mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001497 }
1498
1499 /**
Dan Sandler86647982015-05-13 23:41:13 -04001500 * Return an icon representing the action.
1501 */
1502 public Icon getIcon() {
1503 if (mIcon == null && icon != 0) {
1504 // you snuck an icon in here without using the builder; let's try to keep it
1505 mIcon = Icon.createWithResource("", icon);
1506 }
1507 return mIcon;
1508 }
1509
1510 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001511 * Get additional metadata carried around with this Action.
1512 */
1513 public Bundle getExtras() {
1514 return mExtras;
1515 }
1516
1517 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001518 * Return whether the platform should automatically generate possible replies for this
1519 * {@link Action}
1520 */
1521 public boolean getAllowGeneratedReplies() {
1522 return mAllowGeneratedReplies;
1523 }
1524
1525 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001526 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001527 * May return null if no remote inputs were added. Only returns inputs which accept
1528 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001529 */
1530 public RemoteInput[] getRemoteInputs() {
1531 return mRemoteInputs;
1532 }
1533
1534 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001535 * Returns the {@code SemanticAction} associated with this {@link Action}. A
1536 * {@code SemanticAction} denotes what an {@link Action}'s {@link PendingIntent} will do
1537 * (eg. reply, mark as read, delete, etc).
1538 */
1539 public @SemanticAction int getSemanticAction() {
1540 return mSemanticAction;
1541 }
1542
1543 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001544 * Get the list of inputs to be collected from the user that ONLY accept data when this
1545 * action is sent. These remote inputs are guaranteed to return true on a call to
1546 * {@link RemoteInput#isDataOnly}.
1547 *
Shane Brennanf670d6c2017-04-25 13:05:45 -07001548 * Returns null if there are no data-only remote inputs.
Shane Brennan472a3b32016-12-12 15:28:10 -08001549 *
1550 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1551 * of non-textual RemoteInputs do not access these remote inputs.
1552 */
1553 public RemoteInput[] getDataOnlyRemoteInputs() {
1554 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1555 }
1556
1557 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001558 * Builder class for {@link Action} objects.
1559 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001560 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001561 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001562 private final CharSequence mTitle;
1563 private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001564 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001565 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001566 private ArrayList<RemoteInput> mRemoteInputs;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001567 private @SemanticAction int mSemanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001568
1569 /**
1570 * Construct a new builder for {@link Action} object.
1571 * @param icon icon to show for this action
1572 * @param title the title of the action
1573 * @param intent the {@link PendingIntent} to fire when users trigger this action
1574 */
Dan Sandler86647982015-05-13 23:41:13 -04001575 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001576 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001577 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001578 }
1579
1580 /**
1581 * Construct a new builder for {@link Action} object.
1582 * @param icon icon to show for this action
1583 * @param title the title of the action
1584 * @param intent the {@link PendingIntent} to fire when users trigger this action
1585 */
1586 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001587 this(icon, title, intent, new Bundle(), null, true, SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001588 }
1589
1590 /**
1591 * Construct a new builder for {@link Action} object using the fields from an
1592 * {@link Action}.
1593 * @param action the action to read fields from.
1594 */
1595 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001596 this(action.getIcon(), action.title, action.actionIntent,
1597 new Bundle(action.mExtras), action.getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001598 action.getAllowGeneratedReplies(), action.getSemanticAction());
Griff Hazen959591e2014-05-15 22:26:18 -07001599 }
1600
Dan Sandler86647982015-05-13 23:41:13 -04001601 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001602 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1603 @SemanticAction int semanticAction) {
Griff Hazen959591e2014-05-15 22:26:18 -07001604 mIcon = icon;
1605 mTitle = title;
1606 mIntent = intent;
1607 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001608 if (remoteInputs != null) {
1609 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1610 Collections.addAll(mRemoteInputs, remoteInputs);
1611 }
Adrian Roos7af53622016-10-12 13:44:05 -07001612 mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001613 mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001614 }
1615
1616 /**
1617 * Merge additional metadata into this builder.
1618 *
1619 * <p>Values within the Bundle will replace existing extras values in this Builder.
1620 *
1621 * @see Notification.Action#extras
1622 */
1623 public Builder addExtras(Bundle extras) {
1624 if (extras != null) {
1625 mExtras.putAll(extras);
1626 }
1627 return this;
1628 }
1629
1630 /**
1631 * Get the metadata Bundle used by this Builder.
1632 *
1633 * <p>The returned Bundle is shared with this Builder.
1634 */
1635 public Bundle getExtras() {
1636 return mExtras;
1637 }
1638
1639 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001640 * Add an input to be collected from the user when this action is sent.
1641 * Response values can be retrieved from the fired intent by using the
1642 * {@link RemoteInput#getResultsFromIntent} function.
1643 * @param remoteInput a {@link RemoteInput} to add to the action
1644 * @return this object for method chaining
1645 */
1646 public Builder addRemoteInput(RemoteInput remoteInput) {
1647 if (mRemoteInputs == null) {
1648 mRemoteInputs = new ArrayList<RemoteInput>();
1649 }
1650 mRemoteInputs.add(remoteInput);
1651 return this;
1652 }
1653
1654 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001655 * Set whether the platform should automatically generate possible replies to add to
1656 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1657 * {@link RemoteInput}, this has no effect.
1658 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1659 * otherwise
1660 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001661 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001662 */
1663 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1664 mAllowGeneratedReplies = allowGeneratedReplies;
1665 return this;
1666 }
1667
1668 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001669 * Sets the {@code SemanticAction} for this {@link Action}. A
1670 * {@code SemanticAction} denotes what an {@link Action}'s
1671 * {@link PendingIntent} will do (eg. reply, mark as read, delete, etc).
1672 * @param semanticAction a SemanticAction defined within {@link Action} with
1673 * {@code SEMANTIC_ACTION_} prefixes
1674 * @return this object for method chaining
1675 */
1676 public Builder setSemanticAction(@SemanticAction int semanticAction) {
1677 mSemanticAction = semanticAction;
1678 return this;
1679 }
1680
1681 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001682 * Apply an extender to this action builder. Extenders may be used to add
1683 * metadata or change options on this builder.
1684 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001685 public Builder extend(Extender extender) {
1686 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001687 return this;
1688 }
1689
1690 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001691 * Combine all of the options that have been set and return a new {@link Action}
1692 * object.
1693 * @return the built action
1694 */
1695 public Action build() {
Shane Brennan472a3b32016-12-12 15:28:10 -08001696 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1697 RemoteInput[] previousDataInputs =
1698 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001699 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001700 for (RemoteInput input : previousDataInputs) {
1701 dataOnlyInputs.add(input);
1702 }
1703 }
1704 List<RemoteInput> textInputs = new ArrayList<>();
1705 if (mRemoteInputs != null) {
1706 for (RemoteInput input : mRemoteInputs) {
1707 if (input.isDataOnly()) {
1708 dataOnlyInputs.add(input);
1709 } else {
1710 textInputs.add(input);
1711 }
1712 }
1713 }
1714 if (!dataOnlyInputs.isEmpty()) {
1715 RemoteInput[] dataInputsArr =
1716 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1717 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1718 }
1719 RemoteInput[] textInputsArr = textInputs.isEmpty()
1720 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1721 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001722 mAllowGeneratedReplies, mSemanticAction);
Griff Hazen959591e2014-05-15 22:26:18 -07001723 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001724 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001725
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001726 @Override
1727 public Action clone() {
1728 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001729 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001730 title,
1731 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001732 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001733 getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001734 getAllowGeneratedReplies(),
1735 getSemanticAction());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001736 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001737
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001738 @Override
1739 public int describeContents() {
1740 return 0;
1741 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001742
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001743 @Override
1744 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001745 final Icon ic = getIcon();
1746 if (ic != null) {
1747 out.writeInt(1);
1748 ic.writeToParcel(out, 0);
1749 } else {
1750 out.writeInt(0);
1751 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001752 TextUtils.writeToParcel(title, out, flags);
1753 if (actionIntent != null) {
1754 out.writeInt(1);
1755 actionIntent.writeToParcel(out, flags);
1756 } else {
1757 out.writeInt(0);
1758 }
Griff Hazen959591e2014-05-15 22:26:18 -07001759 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001760 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001761 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001762 out.writeInt(mSemanticAction);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001763 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001764
Griff Hazen959591e2014-05-15 22:26:18 -07001765 public static final Parcelable.Creator<Action> CREATOR =
1766 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001767 public Action createFromParcel(Parcel in) {
1768 return new Action(in);
1769 }
1770 public Action[] newArray(int size) {
1771 return new Action[size];
1772 }
1773 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001774
1775 /**
1776 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1777 * metadata or change options on an action builder.
1778 */
1779 public interface Extender {
1780 /**
1781 * Apply this extender to a notification action builder.
1782 * @param builder the builder to be modified.
1783 * @return the build object for chaining.
1784 */
1785 public Builder extend(Builder builder);
1786 }
1787
1788 /**
1789 * Wearable extender for notification actions. To add extensions to an action,
1790 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1791 * the {@code WearableExtender()} constructor and apply it to a
1792 * {@link android.app.Notification.Action.Builder} using
1793 * {@link android.app.Notification.Action.Builder#extend}.
1794 *
1795 * <pre class="prettyprint">
1796 * Notification.Action action = new Notification.Action.Builder(
1797 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001798 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001799 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001800 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001801 */
1802 public static final class WearableExtender implements Extender {
1803 /** Notification action extra which contains wearable extensions */
1804 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1805
Pete Gastaf6781d2014-10-07 15:17:05 -04001806 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001807 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001808 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1809 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1810 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001811
1812 // Flags bitwise-ored to mFlags
1813 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001814 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001815 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001816
1817 // Default value for flags integer
1818 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1819
1820 private int mFlags = DEFAULT_FLAGS;
1821
Pete Gastaf6781d2014-10-07 15:17:05 -04001822 private CharSequence mInProgressLabel;
1823 private CharSequence mConfirmLabel;
1824 private CharSequence mCancelLabel;
1825
Griff Hazen61a9e862014-05-22 16:05:19 -07001826 /**
1827 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1828 * options.
1829 */
1830 public WearableExtender() {
1831 }
1832
1833 /**
1834 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1835 * wearable options present in an existing notification action.
1836 * @param action the notification action to inspect.
1837 */
1838 public WearableExtender(Action action) {
1839 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1840 if (wearableBundle != null) {
1841 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001842 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1843 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1844 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001845 }
1846 }
1847
1848 /**
1849 * Apply wearable extensions to a notification action that is being built. This is
1850 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1851 * method of {@link android.app.Notification.Action.Builder}.
1852 */
1853 @Override
1854 public Action.Builder extend(Action.Builder builder) {
1855 Bundle wearableBundle = new Bundle();
1856
1857 if (mFlags != DEFAULT_FLAGS) {
1858 wearableBundle.putInt(KEY_FLAGS, mFlags);
1859 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001860 if (mInProgressLabel != null) {
1861 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1862 }
1863 if (mConfirmLabel != null) {
1864 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1865 }
1866 if (mCancelLabel != null) {
1867 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1868 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001869
1870 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1871 return builder;
1872 }
1873
1874 @Override
1875 public WearableExtender clone() {
1876 WearableExtender that = new WearableExtender();
1877 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001878 that.mInProgressLabel = this.mInProgressLabel;
1879 that.mConfirmLabel = this.mConfirmLabel;
1880 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001881 return that;
1882 }
1883
1884 /**
1885 * Set whether this action is available when the wearable device is not connected to
1886 * a companion device. The user can still trigger this action when the wearable device is
1887 * offline, but a visual hint will indicate that the action may not be available.
1888 * Defaults to true.
1889 */
1890 public WearableExtender setAvailableOffline(boolean availableOffline) {
1891 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1892 return this;
1893 }
1894
1895 /**
1896 * Get whether this action is available when the wearable device is not connected to
1897 * a companion device. The user can still trigger this action when the wearable device is
1898 * offline, but a visual hint will indicate that the action may not be available.
1899 * Defaults to true.
1900 */
1901 public boolean isAvailableOffline() {
1902 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1903 }
1904
1905 private void setFlag(int mask, boolean value) {
1906 if (value) {
1907 mFlags |= mask;
1908 } else {
1909 mFlags &= ~mask;
1910 }
1911 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001912
1913 /**
1914 * Set a label to display while the wearable is preparing to automatically execute the
1915 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1916 *
1917 * @param label the label to display while the action is being prepared to execute
1918 * @return this object for method chaining
1919 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001920 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001921 public WearableExtender setInProgressLabel(CharSequence label) {
1922 mInProgressLabel = label;
1923 return this;
1924 }
1925
1926 /**
1927 * Get the label to display while the wearable is preparing to automatically execute
1928 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1929 *
1930 * @return the label to display while the action is being prepared to execute
1931 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001932 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001933 public CharSequence getInProgressLabel() {
1934 return mInProgressLabel;
1935 }
1936
1937 /**
1938 * Set a label to display to confirm that the action should be executed.
1939 * This is usually an imperative verb like "Send".
1940 *
1941 * @param label the label to confirm the action should be executed
1942 * @return this object for method chaining
1943 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001944 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001945 public WearableExtender setConfirmLabel(CharSequence label) {
1946 mConfirmLabel = label;
1947 return this;
1948 }
1949
1950 /**
1951 * Get the label to display to confirm that the action should be executed.
1952 * This is usually an imperative verb like "Send".
1953 *
1954 * @return the label to confirm the action should be executed
1955 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001956 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001957 public CharSequence getConfirmLabel() {
1958 return mConfirmLabel;
1959 }
1960
1961 /**
1962 * Set a label to display to cancel the action.
1963 * This is usually an imperative verb, like "Cancel".
1964 *
1965 * @param label the label to display to cancel the action
1966 * @return this object for method chaining
1967 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001968 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001969 public WearableExtender setCancelLabel(CharSequence label) {
1970 mCancelLabel = label;
1971 return this;
1972 }
1973
1974 /**
1975 * Get the label to display to cancel the action.
1976 * This is usually an imperative verb like "Cancel".
1977 *
1978 * @return the label to display to cancel the action
1979 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001980 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001981 public CharSequence getCancelLabel() {
1982 return mCancelLabel;
1983 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001984
1985 /**
1986 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1987 * platform that it can generate the appropriate transitions.
1988 * @param hintLaunchesActivity {@code true} if the content intent will launch
1989 * an activity and transitions should be generated, false otherwise.
1990 * @return this object for method chaining
1991 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001992 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001993 boolean hintLaunchesActivity) {
1994 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1995 return this;
1996 }
1997
1998 /**
1999 * Get a hint that this Action will launch an {@link Activity} directly, telling the
2000 * platform that it can generate the appropriate transitions
2001 * @return {@code true} if the content intent will launch an activity and transitions
2002 * should be generated, false otherwise. The default value is {@code false} if this was
2003 * never set.
2004 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04002005 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04002006 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
2007 }
Alex Hills9f087612016-06-07 09:08:59 -04002008
2009 /**
2010 * Set a hint that this Action should be displayed inline.
2011 *
2012 * @param hintDisplayInline {@code true} if action should be displayed inline, false
2013 * otherwise
2014 * @return this object for method chaining
2015 */
2016 public WearableExtender setHintDisplayActionInline(
2017 boolean hintDisplayInline) {
2018 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
2019 return this;
2020 }
2021
2022 /**
2023 * Get a hint that this Action should be displayed inline.
2024 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08002025 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04002026 * otherwise. The default value is {@code false} if this was never set.
2027 */
2028 public boolean getHintDisplayActionInline() {
2029 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
2030 }
Griff Hazen61a9e862014-05-22 16:05:19 -07002031 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002032
2033 /**
2034 * Provides meaning to an {@link Action} that hints at what the associated
2035 * {@link PendingIntent} will do. For example, an {@link Action} with a
2036 * {@link PendingIntent} that replies to a text message notification may have the
2037 * {@link #SEMANTIC_ACTION_REPLY} {@code SemanticAction} set within it.
2038 *
2039 * @hide
2040 */
2041 @IntDef(prefix = { "SEMANTIC_ACTION_" }, value = {
2042 SEMANTIC_ACTION_NONE,
2043 SEMANTIC_ACTION_REPLY,
2044 SEMANTIC_ACTION_MARK_AS_READ,
2045 SEMANTIC_ACTION_MARK_AS_UNREAD,
2046 SEMANTIC_ACTION_DELETE,
2047 SEMANTIC_ACTION_ARCHIVE,
2048 SEMANTIC_ACTION_MUTE,
2049 SEMANTIC_ACTION_UNMUTE,
2050 SEMANTIC_ACTION_THUMBS_UP,
Kodlee Yincda5b092018-02-15 15:34:53 -08002051 SEMANTIC_ACTION_THUMBS_DOWN,
Gustav Sennton1463d832018-11-06 16:12:48 +00002052 SEMANTIC_ACTION_CALL,
2053 SEMANTIC_ACTION_CONTEXTUAL_SUGGESTION
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002054 })
2055 @Retention(RetentionPolicy.SOURCE)
2056 public @interface SemanticAction {}
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002057 }
2058
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002059 /**
2060 * Array of all {@link Action} structures attached to this notification by
2061 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
2062 * {@link android.service.notification.NotificationListenerService} that provide an alternative
2063 * interface for invoking actions.
2064 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05002065 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002066
2067 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002068 * Replacement version of this notification whose content will be shown
2069 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
2070 * and {@link #VISIBILITY_PUBLIC}.
2071 */
2072 public Notification publicVersion;
2073
2074 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002075 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08002076 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077 */
2078 public Notification()
2079 {
2080 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002081 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002082 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 }
2084
2085 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002086 * @hide
2087 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002088 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002089 public Notification(Context context, int icon, CharSequence tickerText, long when,
2090 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
2091 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04002092 new Builder(context)
2093 .setWhen(when)
2094 .setSmallIcon(icon)
2095 .setTicker(tickerText)
2096 .setContentTitle(contentTitle)
2097 .setContentText(contentText)
2098 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
2099 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002100 }
2101
2102 /**
2103 * Constructs a Notification object with the information needed to
2104 * have a status bar icon without the standard expanded view.
2105 *
2106 * @param icon The resource id of the icon to put in the status bar.
2107 * @param tickerText The text that flows by in the status bar when the notification first
2108 * activates.
2109 * @param when The time to show in the time field. In the System.currentTimeMillis
2110 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08002111 *
2112 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002113 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002114 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002115 public Notification(int icon, CharSequence tickerText, long when)
2116 {
2117 this.icon = icon;
2118 this.tickerText = tickerText;
2119 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002120 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002121 }
2122
2123 /**
2124 * Unflatten the notification from a parcel.
2125 */
Svet Ganovddb94882016-06-23 19:55:24 -07002126 @SuppressWarnings("unchecked")
2127 public Notification(Parcel parcel) {
2128 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
2129 // intents in extras are always written as the last entry.
2130 readFromParcelImpl(parcel);
2131 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002132 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07002133 }
2134
2135 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136 {
2137 int version = parcel.readInt();
2138
Adrian Roosfb921842017-10-26 14:49:56 +02002139 mWhitelistToken = parcel.readStrongBinder();
2140 if (mWhitelistToken == null) {
2141 mWhitelistToken = processWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -07002142 }
2143 // Propagate this token to all pending intents that are unmarshalled from the parcel.
Adrian Roosfb921842017-10-26 14:49:56 +02002144 parcel.setClassCookie(PendingIntent.class, mWhitelistToken);
Dianne Hackborn98305522017-05-05 17:53:53 -07002145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002147 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04002148 if (parcel.readInt() != 0) {
2149 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04002150 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
2151 icon = mSmallIcon.getResId();
2152 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002153 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002154 number = parcel.readInt();
2155 if (parcel.readInt() != 0) {
2156 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2157 }
2158 if (parcel.readInt() != 0) {
2159 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2160 }
2161 if (parcel.readInt() != 0) {
2162 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2163 }
2164 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002165 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002166 }
2167 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002168 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
2169 }
Joe Onorato561d3852010-11-20 18:09:34 -08002170 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002171 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08002172 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002173 defaults = parcel.readInt();
2174 flags = parcel.readInt();
2175 if (parcel.readInt() != 0) {
2176 sound = Uri.CREATOR.createFromParcel(parcel);
2177 }
2178
2179 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04002180 if (parcel.readInt() != 0) {
2181 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
2182 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002183 vibrate = parcel.createLongArray();
2184 ledARGB = parcel.readInt();
2185 ledOnMS = parcel.readInt();
2186 ledOffMS = parcel.readInt();
2187 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002188
2189 if (parcel.readInt() != 0) {
2190 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2191 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002192
2193 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002194
John Spurlockfd7f1e02014-03-18 16:41:57 -04002195 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002196
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002197 mGroupKey = parcel.readString();
2198
2199 mSortKey = parcel.readString();
2200
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06002201 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Robin Leead7e72a2017-12-04 15:45:46 +01002202 fixDuplicateExtras();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002203
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002204 actions = parcel.createTypedArray(Action.CREATOR); // may be null
2205
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002206 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002207 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2208 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002209
Chris Wren8fd39ec2014-02-27 17:43:26 -05002210 if (parcel.readInt() != 0) {
2211 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2212 }
2213
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002214 visibility = parcel.readInt();
2215
2216 if (parcel.readInt() != 0) {
2217 publicVersion = Notification.CREATOR.createFromParcel(parcel);
2218 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002219
2220 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002221
2222 if (parcel.readInt() != 0) {
2223 mChannelId = parcel.readString();
2224 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002225 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05002226
2227 if (parcel.readInt() != 0) {
2228 mShortcutId = parcel.readString();
2229 }
2230
2231 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04002232
2233 if (parcel.readInt() != 0) {
2234 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2235 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002236
2237 mGroupAlertBehavior = parcel.readInt();
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002238 if (parcel.readInt() != 0) {
2239 mAppOverlayIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2240 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 }
2242
Andy Stadler110988c2010-12-03 14:29:16 -08002243 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07002244 public Notification clone() {
2245 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04002246 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002247 return that;
2248 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002249
Daniel Sandler1a497d32013-04-18 14:52:45 -04002250 /**
2251 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
2252 * of this into that.
2253 * @hide
2254 */
2255 public void cloneInto(Notification that, boolean heavy) {
Adrian Roosfb921842017-10-26 14:49:56 +02002256 that.mWhitelistToken = this.mWhitelistToken;
Joe Onorato18e69df2010-05-17 22:26:12 -07002257 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002258 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002259 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07002260 that.number = this.number;
2261
2262 // PendingIntents are global, so there's no reason (or way) to clone them.
2263 that.contentIntent = this.contentIntent;
2264 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002265 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07002266
2267 if (this.tickerText != null) {
2268 that.tickerText = this.tickerText.toString();
2269 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002270 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002271 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04002272 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002273 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07002274 that.contentView = this.contentView.clone();
2275 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002276 if (heavy && this.mLargeIcon != null) {
2277 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08002278 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01002279 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07002280 that.sound = this.sound; // android.net.Uri is immutable
2281 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002282 if (this.audioAttributes != null) {
2283 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
2284 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002285
2286 final long[] vibrate = this.vibrate;
2287 if (vibrate != null) {
2288 final int N = vibrate.length;
2289 final long[] vib = that.vibrate = new long[N];
2290 System.arraycopy(vibrate, 0, vib, 0, N);
2291 }
2292
2293 that.ledARGB = this.ledARGB;
2294 that.ledOnMS = this.ledOnMS;
2295 that.ledOffMS = this.ledOffMS;
2296 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002297
Joe Onorato18e69df2010-05-17 22:26:12 -07002298 that.flags = this.flags;
2299
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002300 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08002301
John Spurlockfd7f1e02014-03-18 16:41:57 -04002302 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002303
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002304 that.mGroupKey = this.mGroupKey;
2305
2306 that.mSortKey = this.mSortKey;
2307
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002308 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002309 try {
2310 that.extras = new Bundle(this.extras);
2311 // will unparcel
2312 that.extras.size();
2313 } catch (BadParcelableException e) {
2314 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
2315 that.extras = null;
2316 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002317 }
2318
Felipe Lemedd85da62016-06-28 11:29:54 -07002319 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2320 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002321 }
2322
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002323 if (this.actions != null) {
2324 that.actions = new Action[this.actions.length];
2325 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002326 if ( this.actions[i] != null) {
2327 that.actions[i] = this.actions[i].clone();
2328 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002329 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002330 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002331
Daniel Sandler1a497d32013-04-18 14:52:45 -04002332 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002333 that.bigContentView = this.bigContentView.clone();
2334 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002335
Chris Wren8fd39ec2014-02-27 17:43:26 -05002336 if (heavy && this.headsUpContentView != null) {
2337 that.headsUpContentView = this.headsUpContentView.clone();
2338 }
2339
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002340 that.visibility = this.visibility;
2341
2342 if (this.publicVersion != null) {
2343 that.publicVersion = new Notification();
2344 this.publicVersion.cloneInto(that.publicVersion, heavy);
2345 }
2346
Dan Sandler26e81cf2014-05-06 10:01:27 -04002347 that.color = this.color;
2348
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002349 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002350 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002351 that.mShortcutId = this.mShortcutId;
2352 that.mBadgeIcon = this.mBadgeIcon;
2353 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002354 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002355 that.mAppOverlayIntent = this.mAppOverlayIntent;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002356
Daniel Sandler1a497d32013-04-18 14:52:45 -04002357 if (!heavy) {
2358 that.lightenPayload(); // will clean out extras
2359 }
2360 }
2361
2362 /**
Jeff Sharkey23b31182018-04-18 21:32:12 -06002363 * Note all {@link Uri} that are referenced internally, with the expectation
2364 * that Uri permission grants will need to be issued to ensure the recipient
2365 * of this object is able to render its contents.
2366 *
2367 * @hide
2368 */
2369 public void visitUris(@NonNull Consumer<Uri> visitor) {
2370 visitor.accept(sound);
2371
2372 if (tickerView != null) tickerView.visitUris(visitor);
2373 if (contentView != null) contentView.visitUris(visitor);
2374 if (bigContentView != null) bigContentView.visitUris(visitor);
2375 if (headsUpContentView != null) headsUpContentView.visitUris(visitor);
2376
2377 if (extras != null) {
2378 visitor.accept(extras.getParcelable(EXTRA_AUDIO_CONTENTS_URI));
Robin Leed107af62018-04-27 13:55:56 +02002379 if (extras.containsKey(EXTRA_BACKGROUND_IMAGE_URI)) {
2380 visitor.accept(Uri.parse(extras.getString(EXTRA_BACKGROUND_IMAGE_URI)));
2381 }
Jeff Sharkey23b31182018-04-18 21:32:12 -06002382 }
2383
2384 if (MessagingStyle.class.equals(getNotificationStyle()) && extras != null) {
2385 final Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
2386 if (!ArrayUtils.isEmpty(messages)) {
2387 for (MessagingStyle.Message message : MessagingStyle.Message
2388 .getMessagesFromBundleArray(messages)) {
2389 visitor.accept(message.getDataUri());
2390 }
2391 }
2392
2393 final Parcelable[] historic = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
2394 if (!ArrayUtils.isEmpty(historic)) {
2395 for (MessagingStyle.Message message : MessagingStyle.Message
2396 .getMessagesFromBundleArray(historic)) {
2397 visitor.accept(message.getDataUri());
2398 }
2399 }
2400 }
2401 }
2402
2403 /**
Daniel Sandler1a497d32013-04-18 14:52:45 -04002404 * Removes heavyweight parts of the Notification object for archival or for sending to
2405 * listeners when the full contents are not necessary.
2406 * @hide
2407 */
2408 public final void lightenPayload() {
2409 tickerView = null;
2410 contentView = null;
2411 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002412 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002413 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002414 if (extras != null && !extras.isEmpty()) {
2415 final Set<String> keyset = extras.keySet();
2416 final int N = keyset.size();
2417 final String[] keys = keyset.toArray(new String[N]);
2418 for (int i=0; i<N; i++) {
2419 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002420 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2421 continue;
2422 }
Dan Sandler50128532015-12-08 15:42:41 -05002423 final Object obj = extras.get(key);
2424 if (obj != null &&
2425 ( obj instanceof Parcelable
2426 || obj instanceof Parcelable[]
2427 || obj instanceof SparseArray
2428 || obj instanceof ArrayList)) {
2429 extras.remove(key);
2430 }
2431 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002432 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002433 }
2434
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002435 /**
2436 * Make sure this CharSequence is safe to put into a bundle, which basically
2437 * means it had better not be some custom Parcelable implementation.
2438 * @hide
2439 */
2440 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002441 if (cs == null) return cs;
2442 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2443 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2444 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002445 if (cs instanceof Parcelable) {
2446 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2447 + " instance is a custom Parcelable and not allowed in Notification");
2448 return cs.toString();
2449 }
Selim Cinek60a54252016-02-26 17:03:25 -08002450 return removeTextSizeSpans(cs);
2451 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002452
Selim Cinek60a54252016-02-26 17:03:25 -08002453 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2454 if (charSequence instanceof Spanned) {
2455 Spanned ss = (Spanned) charSequence;
2456 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2457 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2458 for (Object span : spans) {
2459 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002460 if (resultSpan instanceof CharacterStyle) {
2461 resultSpan = ((CharacterStyle) span).getUnderlying();
2462 }
2463 if (resultSpan instanceof TextAppearanceSpan) {
2464 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002465 resultSpan = new TextAppearanceSpan(
2466 originalSpan.getFamily(),
2467 originalSpan.getTextStyle(),
2468 -1,
2469 originalSpan.getTextColor(),
2470 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002471 } else if (resultSpan instanceof RelativeSizeSpan
2472 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002473 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002474 } else {
2475 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002476 }
2477 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2478 ss.getSpanFlags(span));
2479 }
2480 return builder;
2481 }
2482 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002483 }
2484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002485 public int describeContents() {
2486 return 0;
2487 }
2488
2489 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002490 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002491 */
Svet Ganovddb94882016-06-23 19:55:24 -07002492 public void writeToParcel(Parcel parcel, int flags) {
2493 // We need to mark all pending intents getting into the notification
2494 // system as being put there to later allow the notification ranker
2495 // to launch them and by doing so add the app to the battery saver white
2496 // list for a short period of time. The problem is that the system
2497 // cannot look into the extras as there may be parcelables there that
2498 // the platform does not know how to handle. To go around that we have
2499 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002500 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002501 if (collectPendingIntents) {
2502 PendingIntent.setOnMarshaledListener(
2503 (PendingIntent intent, Parcel out, int outFlags) -> {
2504 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002505 if (allPendingIntents == null) {
2506 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002507 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002508 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002509 }
2510 });
2511 }
2512 try {
2513 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002514 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002515 writeToParcelImpl(parcel, flags);
2516 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002517 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002518 } finally {
2519 if (collectPendingIntents) {
2520 PendingIntent.setOnMarshaledListener(null);
2521 }
2522 }
2523 }
2524
2525 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002526 parcel.writeInt(1);
2527
Adrian Roosfb921842017-10-26 14:49:56 +02002528 parcel.writeStrongBinder(mWhitelistToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002529 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002530 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002531 if (mSmallIcon == null && icon != 0) {
2532 // you snuck an icon in here without using the builder; let's try to keep it
2533 mSmallIcon = Icon.createWithResource("", icon);
2534 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002535 if (mSmallIcon != null) {
2536 parcel.writeInt(1);
2537 mSmallIcon.writeToParcel(parcel, 0);
2538 } else {
2539 parcel.writeInt(0);
2540 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002541 parcel.writeInt(number);
2542 if (contentIntent != null) {
2543 parcel.writeInt(1);
2544 contentIntent.writeToParcel(parcel, 0);
2545 } else {
2546 parcel.writeInt(0);
2547 }
2548 if (deleteIntent != null) {
2549 parcel.writeInt(1);
2550 deleteIntent.writeToParcel(parcel, 0);
2551 } else {
2552 parcel.writeInt(0);
2553 }
2554 if (tickerText != null) {
2555 parcel.writeInt(1);
2556 TextUtils.writeToParcel(tickerText, parcel, flags);
2557 } else {
2558 parcel.writeInt(0);
2559 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002560 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002561 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002562 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002563 } else {
2564 parcel.writeInt(0);
2565 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002566 if (contentView != null) {
2567 parcel.writeInt(1);
2568 contentView.writeToParcel(parcel, 0);
2569 } else {
2570 parcel.writeInt(0);
2571 }
Selim Cinek279fa862016-06-14 10:57:25 -07002572 if (mLargeIcon == null && largeIcon != null) {
2573 // you snuck an icon in here without using the builder; let's try to keep it
2574 mLargeIcon = Icon.createWithBitmap(largeIcon);
2575 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002576 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002577 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002578 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002579 } else {
2580 parcel.writeInt(0);
2581 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002582
2583 parcel.writeInt(defaults);
2584 parcel.writeInt(this.flags);
2585
2586 if (sound != null) {
2587 parcel.writeInt(1);
2588 sound.writeToParcel(parcel, 0);
2589 } else {
2590 parcel.writeInt(0);
2591 }
2592 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002593
2594 if (audioAttributes != null) {
2595 parcel.writeInt(1);
2596 audioAttributes.writeToParcel(parcel, 0);
2597 } else {
2598 parcel.writeInt(0);
2599 }
2600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002601 parcel.writeLongArray(vibrate);
2602 parcel.writeInt(ledARGB);
2603 parcel.writeInt(ledOnMS);
2604 parcel.writeInt(ledOffMS);
2605 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002606
2607 if (fullScreenIntent != null) {
2608 parcel.writeInt(1);
2609 fullScreenIntent.writeToParcel(parcel, 0);
2610 } else {
2611 parcel.writeInt(0);
2612 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002613
2614 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002615
John Spurlockfd7f1e02014-03-18 16:41:57 -04002616 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002617
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002618 parcel.writeString(mGroupKey);
2619
2620 parcel.writeString(mSortKey);
2621
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002622 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002623
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002624 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002625
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002626 if (bigContentView != null) {
2627 parcel.writeInt(1);
2628 bigContentView.writeToParcel(parcel, 0);
2629 } else {
2630 parcel.writeInt(0);
2631 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002632
Chris Wren8fd39ec2014-02-27 17:43:26 -05002633 if (headsUpContentView != null) {
2634 parcel.writeInt(1);
2635 headsUpContentView.writeToParcel(parcel, 0);
2636 } else {
2637 parcel.writeInt(0);
2638 }
2639
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002640 parcel.writeInt(visibility);
2641
2642 if (publicVersion != null) {
2643 parcel.writeInt(1);
2644 publicVersion.writeToParcel(parcel, 0);
2645 } else {
2646 parcel.writeInt(0);
2647 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002648
2649 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002650
2651 if (mChannelId != null) {
2652 parcel.writeInt(1);
2653 parcel.writeString(mChannelId);
2654 } else {
2655 parcel.writeInt(0);
2656 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002657 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002658
2659 if (mShortcutId != null) {
2660 parcel.writeInt(1);
2661 parcel.writeString(mShortcutId);
2662 } else {
2663 parcel.writeInt(0);
2664 }
2665
2666 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002667
2668 if (mSettingsText != null) {
2669 parcel.writeInt(1);
2670 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2671 } else {
2672 parcel.writeInt(0);
2673 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002674
2675 parcel.writeInt(mGroupAlertBehavior);
Julia Reynoldsfc640012018-02-21 12:25:27 -05002676
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002677 if (mAppOverlayIntent != null) {
2678 parcel.writeInt(1);
2679 mAppOverlayIntent.writeToParcel(parcel, 0);
2680 } else {
2681 parcel.writeInt(0);
2682 }
2683
Julia Reynoldsfc640012018-02-21 12:25:27 -05002684 // mUsesStandardHeader is not written because it should be recomputed in listeners
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002685 }
2686
2687 /**
2688 * Parcelable.Creator that instantiates Notification objects
2689 */
2690 public static final Parcelable.Creator<Notification> CREATOR
2691 = new Parcelable.Creator<Notification>()
2692 {
2693 public Notification createFromParcel(Parcel parcel)
2694 {
2695 return new Notification(parcel);
2696 }
2697
2698 public Notification[] newArray(int size)
2699 {
2700 return new Notification[size];
2701 }
2702 };
2703
2704 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05002705 * @hide
2706 */
2707 public static boolean areActionsVisiblyDifferent(Notification first, Notification second) {
2708 Notification.Action[] firstAs = first.actions;
2709 Notification.Action[] secondAs = second.actions;
2710 if (firstAs == null && secondAs != null || firstAs != null && secondAs == null) {
2711 return true;
2712 }
2713 if (firstAs != null && secondAs != null) {
2714 if (firstAs.length != secondAs.length) {
2715 return true;
2716 }
2717 for (int i = 0; i < firstAs.length; i++) {
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -04002718 if (!Objects.equals(String.valueOf(firstAs[i].title),
2719 String.valueOf(secondAs[i].title))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05002720 return true;
2721 }
2722 RemoteInput[] firstRs = firstAs[i].getRemoteInputs();
2723 RemoteInput[] secondRs = secondAs[i].getRemoteInputs();
2724 if (firstRs == null) {
2725 firstRs = new RemoteInput[0];
2726 }
2727 if (secondRs == null) {
2728 secondRs = new RemoteInput[0];
2729 }
2730 if (firstRs.length != secondRs.length) {
2731 return true;
2732 }
2733 for (int j = 0; j < firstRs.length; j++) {
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -04002734 if (!Objects.equals(String.valueOf(firstRs[j].getLabel()),
2735 String.valueOf(secondRs[j].getLabel()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05002736 return true;
2737 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05002738 }
2739 }
2740 }
2741 return false;
2742 }
2743
2744 /**
2745 * @hide
2746 */
2747 public static boolean areStyledNotificationsVisiblyDifferent(Builder first, Builder second) {
2748 if (first.getStyle() == null) {
2749 return second.getStyle() != null;
2750 }
2751 if (second.getStyle() == null) {
2752 return true;
2753 }
2754 return first.getStyle().areNotificationsVisiblyDifferent(second.getStyle());
2755 }
2756
2757 /**
2758 * @hide
2759 */
2760 public static boolean areRemoteViewsChanged(Builder first, Builder second) {
Julia Reynoldse5c60452018-04-30 14:41:36 -04002761 if (!Objects.equals(first.usesStandardHeader(), second.usesStandardHeader())) {
2762 return true;
2763 }
2764
2765 if (areRemoteViewsChanged(first.mN.contentView, second.mN.contentView)) {
2766 return true;
2767 }
2768 if (areRemoteViewsChanged(first.mN.bigContentView, second.mN.bigContentView)) {
2769 return true;
2770 }
2771 if (areRemoteViewsChanged(first.mN.headsUpContentView, second.mN.headsUpContentView)) {
2772 return true;
2773 }
2774
2775 return false;
2776 }
2777
2778 private static boolean areRemoteViewsChanged(RemoteViews first, RemoteViews second) {
2779 if (first == null && second == null) {
2780 return false;
2781 }
2782 if (first == null && second != null || first != null && second == null) {
2783 return true;
2784 }
2785
2786 if (!Objects.equals(first.getLayoutId(), second.getLayoutId())) {
2787 return true;
2788 }
2789
2790 if (!Objects.equals(first.getSequenceNumber(), second.getSequenceNumber())) {
2791 return true;
2792 }
2793
2794 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05002795 }
2796
2797 /**
Robin Leead7e72a2017-12-04 15:45:46 +01002798 * Parcelling creates multiple copies of objects in {@code extras}. Fix them.
2799 * <p>
2800 * For backwards compatibility {@code extras} holds some references to "real" member data such
2801 * as {@link getLargeIcon()} which is mirrored by {@link #EXTRA_LARGE_ICON}. This is mostly
2802 * fine as long as the object stays in one process.
2803 * <p>
2804 * However, once the notification goes into a parcel each reference gets marshalled separately,
2805 * wasting memory. Especially with large images on Auto and TV, this is worth fixing.
2806 */
2807 private void fixDuplicateExtras() {
2808 if (extras != null) {
Robin Leead7e72a2017-12-04 15:45:46 +01002809 fixDuplicateExtra(mLargeIcon, EXTRA_LARGE_ICON);
2810 }
2811 }
2812
2813 /**
2814 * If we find an extra that's exactly the same as one of the "real" fields but refers to a
2815 * separate object, replace it with the field's version to avoid holding duplicate copies.
2816 */
2817 private void fixDuplicateExtra(@Nullable Parcelable original, @NonNull String extraName) {
2818 if (original != null && extras.getParcelable(extraName) != null) {
2819 extras.putParcelable(extraName, original);
2820 }
2821 }
2822
2823 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002824 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2825 * layout.
2826 *
2827 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2828 * in the view.</p>
2829 * @param context The context for your application / activity.
2830 * @param contentTitle The title that goes in the expanded entry.
2831 * @param contentText The text that goes in the expanded entry.
2832 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2833 * If this is an activity, it must include the
2834 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002835 * that you take care of task management as described in the
2836 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2837 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002838 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002839 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002840 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002841 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002842 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002843 public void setLatestEventInfo(Context context,
2844 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002845 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2846 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2847 new Throwable());
2848 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002849
Selim Cinek4ac6f602016-06-13 15:47:03 -07002850 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2851 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2852 }
2853
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002854 // ensure that any information already set directly is preserved
2855 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002856
2857 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002858 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002859 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002860 }
2861 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002862 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002863 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002864 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002865
2866 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002867 }
2868
Julia Reynoldsda303542015-11-23 14:00:20 -05002869 /**
2870 * @hide
2871 */
2872 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002873 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002874 }
2875
2876 /**
2877 * @hide
2878 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002879 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002880 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002881 }
2882
Yi Jin6b514142017-10-30 14:54:12 -07002883 /**
2884 * @hide
2885 */
2886 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2887 long token = proto.start(fieldId);
2888 proto.write(NotificationProto.CHANNEL_ID, getChannelId());
2889 proto.write(NotificationProto.HAS_TICKER_TEXT, this.tickerText != null);
2890 proto.write(NotificationProto.FLAGS, this.flags);
2891 proto.write(NotificationProto.COLOR, this.color);
2892 proto.write(NotificationProto.CATEGORY, this.category);
2893 proto.write(NotificationProto.GROUP_KEY, this.mGroupKey);
2894 proto.write(NotificationProto.SORT_KEY, this.mSortKey);
2895 if (this.actions != null) {
2896 proto.write(NotificationProto.ACTION_LENGTH, this.actions.length);
2897 }
2898 if (this.visibility >= VISIBILITY_SECRET && this.visibility <= VISIBILITY_PUBLIC) {
2899 proto.write(NotificationProto.VISIBILITY, this.visibility);
2900 }
2901 if (publicVersion != null) {
2902 publicVersion.writeToProto(proto, NotificationProto.PUBLIC_VERSION);
2903 }
2904 proto.end(token);
2905 }
2906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002907 @Override
2908 public String toString() {
2909 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002910 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04002911 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002912 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002913 sb.append(priority);
2914 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002915 if (contentView != null) {
2916 sb.append(contentView.getPackage());
2917 sb.append("/0x");
2918 sb.append(Integer.toHexString(contentView.getLayoutId()));
2919 } else {
2920 sb.append("null");
2921 }
2922 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002923 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2924 sb.append("default");
2925 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002926 int N = this.vibrate.length-1;
2927 sb.append("[");
2928 for (int i=0; i<N; i++) {
2929 sb.append(this.vibrate[i]);
2930 sb.append(',');
2931 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002932 if (N != -1) {
2933 sb.append(this.vibrate[N]);
2934 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002935 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002936 } else {
2937 sb.append("null");
2938 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002939 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002940 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002941 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002942 } else if (this.sound != null) {
2943 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002944 } else {
2945 sb.append("null");
2946 }
Chris Wren365b6d32015-07-16 10:39:26 -04002947 if (this.tickerText != null) {
2948 sb.append(" tick");
2949 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002950 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002952 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002953 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002954 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002955 if (this.category != null) {
2956 sb.append(" category=");
2957 sb.append(this.category);
2958 }
2959 if (this.mGroupKey != null) {
2960 sb.append(" groupKey=");
2961 sb.append(this.mGroupKey);
2962 }
2963 if (this.mSortKey != null) {
2964 sb.append(" sortKey=");
2965 sb.append(this.mSortKey);
2966 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002967 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002968 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002969 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002970 }
2971 sb.append(" vis=");
2972 sb.append(visibilityToString(this.visibility));
2973 if (this.publicVersion != null) {
2974 sb.append(" publicVersion=");
2975 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002976 }
2977 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002978 return sb.toString();
2979 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002980
Dan Sandler1b718782014-07-18 12:43:45 -04002981 /**
2982 * {@hide}
2983 */
2984 public static String visibilityToString(int vis) {
2985 switch (vis) {
2986 case VISIBILITY_PRIVATE:
2987 return "PRIVATE";
2988 case VISIBILITY_PUBLIC:
2989 return "PUBLIC";
2990 case VISIBILITY_SECRET:
2991 return "SECRET";
2992 default:
2993 return "UNKNOWN(" + String.valueOf(vis) + ")";
2994 }
2995 }
2996
Joe Onoratocb109a02011-01-18 17:57:41 -08002997 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002998 * {@hide}
2999 */
3000 public static String priorityToString(@Priority int pri) {
3001 switch (pri) {
3002 case PRIORITY_MIN:
3003 return "MIN";
3004 case PRIORITY_LOW:
3005 return "LOW";
3006 case PRIORITY_DEFAULT:
3007 return "DEFAULT";
3008 case PRIORITY_HIGH:
3009 return "HIGH";
3010 case PRIORITY_MAX:
3011 return "MAX";
3012 default:
3013 return "UNKNOWN(" + String.valueOf(pri) + ")";
3014 }
3015 }
3016
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04003017 /**
3018 * @hide
3019 */
3020 public boolean hasCompletedProgress() {
3021 // not a progress notification; can't be complete
3022 if (!extras.containsKey(EXTRA_PROGRESS)
3023 || !extras.containsKey(EXTRA_PROGRESS_MAX)) {
3024 return false;
3025 }
3026 // many apps use max 0 for 'indeterminate'; not complete
3027 if (extras.getInt(EXTRA_PROGRESS_MAX) == 0) {
3028 return false;
3029 }
3030 return extras.getInt(EXTRA_PROGRESS) == extras.getInt(EXTRA_PROGRESS_MAX);
3031 }
3032
Jeff Sharkey000ce802017-04-29 13:13:27 -06003033 /** @removed */
3034 @Deprecated
Julia Reynolds37856052016-11-11 09:20:07 -05003035 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003036 return mChannelId;
3037 }
3038
3039 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003040 * Returns the id of the channel this notification posts to.
3041 */
3042 public String getChannelId() {
3043 return mChannelId;
3044 }
3045
Jeff Sharkey000ce802017-04-29 13:13:27 -06003046 /** @removed */
3047 @Deprecated
Julia Reynolds2a128742016-11-28 14:29:25 -05003048 public long getTimeout() {
3049 return mTimeout;
3050 }
3051
3052 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003053 * Returns the duration from posting after which this notification should be canceled by the
3054 * system, if it's not canceled already.
3055 */
3056 public long getTimeoutAfter() {
3057 return mTimeout;
3058 }
3059
3060 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003061 * Returns what icon should be shown for this notification if it is being displayed in a
3062 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
3063 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
3064 */
3065 public int getBadgeIconType() {
3066 return mBadgeIcon;
3067 }
3068
3069 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003070 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04003071 *
3072 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
3073 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003074 */
3075 public String getShortcutId() {
3076 return mShortcutId;
3077 }
3078
Julia Reynolds3aedded2017-03-31 14:42:09 -04003079
3080 /**
3081 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
3082 */
3083 public CharSequence getSettingsText() {
3084 return mSettingsText;
3085 }
3086
Julia Reynolds13d898c2017-02-02 12:22:05 -05003087 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003088 * Returns which type of notifications in a group are responsible for audibly alerting the
3089 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
3090 * {@link #GROUP_ALERT_SUMMARY}.
3091 */
3092 public @GroupAlertBehavior int getGroupAlertBehavior() {
3093 return mGroupAlertBehavior;
3094 }
3095
3096 /**
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003097 * Returns the intent that will be used to display app content in a floating window over the
3098 * existing foreground activity.
3099 */
3100 public PendingIntent getAppOverlayIntent() {
3101 return mAppOverlayIntent;
3102 }
3103
3104 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003105 * The small icon representing this notification in the status bar and content view.
3106 *
3107 * @return the small icon representing this notification.
3108 *
3109 * @see Builder#getSmallIcon()
3110 * @see Builder#setSmallIcon(Icon)
3111 */
3112 public Icon getSmallIcon() {
3113 return mSmallIcon;
3114 }
3115
3116 /**
3117 * Used when notifying to clean up legacy small icons.
3118 * @hide
3119 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003120 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04003121 public void setSmallIcon(Icon icon) {
3122 mSmallIcon = icon;
3123 }
3124
3125 /**
3126 * The large icon shown in this notification's content view.
3127 * @see Builder#getLargeIcon()
3128 * @see Builder#setLargeIcon(Icon)
3129 */
3130 public Icon getLargeIcon() {
3131 return mLargeIcon;
3132 }
3133
3134 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02003135 * @hide
3136 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003137 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003138 public boolean isGroupSummary() {
3139 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
3140 }
3141
3142 /**
3143 * @hide
3144 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003145 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003146 public boolean isGroupChild() {
3147 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
3148 }
3149
3150 /**
Julia Reynolds30203152017-05-26 13:36:31 -04003151 * @hide
3152 */
3153 public boolean suppressAlertingDueToGrouping() {
3154 if (isGroupSummary()
3155 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
3156 return true;
3157 } else if (isGroupChild()
3158 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
3159 return true;
3160 }
3161 return false;
3162 }
3163
Tony Mak638430e2018-10-08 19:19:10 +01003164
3165 /**
3166 * Finds and returns a remote input and its corresponding action.
3167 *
3168 * @param requiresFreeform requires the remoteinput to allow freeform or not.
3169 * @return the result pair, {@code null} if no result is found.
3170 *
3171 * @hide
3172 */
3173 @Nullable
3174 public Pair<RemoteInput, Action> findRemoteInputActionPair(boolean requiresFreeform) {
3175 if (actions == null) {
3176 return null;
3177 }
3178 for (Notification.Action action : actions) {
3179 if (action.getRemoteInputs() == null) {
3180 continue;
3181 }
3182 RemoteInput resultRemoteInput = null;
3183 for (RemoteInput remoteInput : action.getRemoteInputs()) {
3184 if (remoteInput.getAllowFreeFormInput() || !requiresFreeform) {
3185 resultRemoteInput = remoteInput;
3186 }
3187 }
3188 if (resultRemoteInput != null) {
3189 return Pair.create(resultRemoteInput, action);
3190 }
3191 }
3192 return null;
3193 }
3194
Julia Reynolds30203152017-05-26 13:36:31 -04003195 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003196 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08003197 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003198 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07003199 * content views using the platform's notification layout template. If your app supports
3200 * versions of Android as old as API level 4, you can instead use
3201 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
3202 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
3203 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08003204 *
Scott Main183bf112012-08-13 19:12:13 -07003205 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08003206 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003207 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07003208 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003209 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3210 * .setContentText(subject)
3211 * .setSmallIcon(R.drawable.new_mail)
3212 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003213 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003214 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08003215 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003216 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003217 /**
3218 * @hide
3219 */
3220 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
3221 "android.rebuild.contentViewActionCount";
3222 /**
3223 * @hide
3224 */
3225 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
3226 = "android.rebuild.bigViewActionCount";
3227 /**
3228 * @hide
3229 */
3230 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
3231 = "android.rebuild.hudViewActionCount";
3232
Selim Cinek6743c0b2017-01-18 18:24:01 -08003233 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
3234 SystemProperties.getBoolean("notifications.only_title", true);
3235
Selim Cinek389edcd2017-05-11 19:16:44 -07003236 /**
3237 * The lightness difference that has to be added to the primary text color to obtain the
3238 * secondary text color when the background is light.
3239 */
3240 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
3241
3242 /**
3243 * The lightness difference that has to be added to the primary text color to obtain the
3244 * secondary text color when the background is dark.
3245 * A bit less then the above value, since it looks better on dark backgrounds.
3246 */
3247 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
3248
Joe Onorato46439ce2010-11-19 13:56:21 -08003249 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003250 private Notification mN;
3251 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003252 private Style mStyle;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003253 @UnsupportedAppUsage
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003254 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Selim Cineke7238dd2017-12-14 17:48:32 -08003255 private ArrayList<Person> mPersonList = new ArrayList<>();
Lucas Dupina291d192018-06-07 13:59:42 -07003256 private ContrastColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08003257 private boolean mIsLegacy;
3258 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02003259
3260 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08003261 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
3262 */
3263 private int mCachedContrastColor = COLOR_INVALID;
3264 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08003265 /**
Selim Cinek4717d862018-04-19 09:19:15 +08003266 * Caches a ambient version of {@link #mCachedAmbientColorIsFor}.
Adrian Roos487374f2017-01-11 15:48:14 -08003267 */
3268 private int mCachedAmbientColor = COLOR_INVALID;
3269 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Selim Cinek4717d862018-04-19 09:19:15 +08003270 /**
3271 * A neutral color color that can be used for icons.
3272 */
3273 private int mNeutralColor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08003274
3275 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08003276 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
3277 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
3278 */
3279 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08003280 private int mTextColorsAreForBackground = COLOR_INVALID;
3281 private int mPrimaryTextColor = COLOR_INVALID;
3282 private int mSecondaryTextColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07003283 private int mBackgroundColor = COLOR_INVALID;
3284 private int mForegroundColor = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07003285 /**
3286 * A temporary location where actions are stored. If != null the view originally has action
3287 * but doesn't have any for this inflation.
3288 */
3289 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07003290 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003291
Anthony Chenad4d1582017-04-10 16:07:58 -07003292 private boolean mTintActionButtons;
3293 private boolean mInNightMode;
3294
Adrian Roos70d7aa32017-01-11 15:39:06 -08003295 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003296 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08003297 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003298 * @param context
3299 * A {@link Context} that will be used by the Builder to construct the
3300 * RemoteViews. The Context will not be held past the lifetime of this Builder
3301 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003302 * @param channelId
3303 * The constructed Notification will be posted on this
3304 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
3305 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003306 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003307 public Builder(Context context, String channelId) {
3308 this(context, (Notification) null);
3309 mN.mChannelId = channelId;
3310 }
3311
3312 /**
3313 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
3314 * instead. All posted Notifications must specify a NotificationChannel Id.
3315 */
3316 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003317 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003318 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003319 }
3320
Joe Onoratocb109a02011-01-18 17:57:41 -08003321 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003322 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02003323 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003324 public Builder(Context context, Notification toAdopt) {
3325 mContext = context;
Anthony Chenad4d1582017-04-10 16:07:58 -07003326 Resources res = mContext.getResources();
3327 mTintActionButtons = res.getBoolean(R.bool.config_tintNotificationActionButtons);
3328
3329 if (res.getBoolean(R.bool.config_enableNightMode)) {
3330 Configuration currentConfig = res.getConfiguration();
3331 mInNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
3332 == Configuration.UI_MODE_NIGHT_YES;
3333 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003334
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003335 if (toAdopt == null) {
3336 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003337 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
3338 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
3339 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003340 mN.priority = PRIORITY_DEFAULT;
3341 mN.visibility = VISIBILITY_PRIVATE;
3342 } else {
3343 mN = toAdopt;
3344 if (mN.actions != null) {
3345 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003346 }
3347
Selim Cineke7238dd2017-12-14 17:48:32 -08003348 if (mN.extras.containsKey(EXTRA_PEOPLE_LIST)) {
3349 ArrayList<Person> people = mN.extras.getParcelableArrayList(EXTRA_PEOPLE_LIST);
3350 mPersonList.addAll(people);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003351 }
3352
Selim Cinek4ac6f602016-06-13 15:47:03 -07003353 if (mN.getSmallIcon() == null && mN.icon != 0) {
3354 setSmallIcon(mN.icon);
3355 }
3356
3357 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
3358 setLargeIcon(mN.largeIcon);
3359 }
3360
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003361 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
3362 if (!TextUtils.isEmpty(templateClass)) {
3363 final Class<? extends Style> styleClass
3364 = getNotificationStyleClass(templateClass);
3365 if (styleClass == null) {
3366 Log.d(TAG, "Unknown style class: " + templateClass);
3367 } else {
3368 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003369 final Constructor<? extends Style> ctor =
3370 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003371 ctor.setAccessible(true);
3372 final Style style = ctor.newInstance();
3373 style.restoreFromExtras(mN.extras);
3374
3375 if (style != null) {
3376 setStyle(style);
3377 }
3378 } catch (Throwable t) {
3379 Log.e(TAG, "Could not create Style", t);
3380 }
3381 }
3382 }
3383
3384 }
3385 }
3386
Lucas Dupina291d192018-06-07 13:59:42 -07003387 private ContrastColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08003388 if (mColorUtil == null) {
Lucas Dupina291d192018-06-07 13:59:42 -07003389 mColorUtil = ContrastColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003390 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003391 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003392 }
3393
3394 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003395 * If this notification is duplicative of a Launcher shortcut, sets the
3396 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
3397 * the shortcut.
3398 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04003399 * This field will be ignored by Launchers that don't support badging, don't show
3400 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003401 *
3402 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
3403 * supersedes
3404 */
3405 public Builder setShortcutId(String shortcutId) {
3406 mN.mShortcutId = shortcutId;
3407 return this;
3408 }
3409
3410 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003411 * Sets which icon to display as a badge for this notification.
3412 *
3413 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
3414 * {@link #BADGE_ICON_LARGE}.
3415 *
3416 * Note: This value might be ignored, for launchers that don't support badge icons.
3417 */
Julia Reynolds612beb22017-03-30 10:48:30 -04003418 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04003419 mN.mBadgeIcon = icon;
3420 return this;
3421 }
3422
3423 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003424 * Sets the group alert behavior for this notification. Use this method to mute this
3425 * notification if alerts for this notification's group should be handled by a different
3426 * notification. This is only applicable for notifications that belong to a
Julia Reynolds399d9bf2017-08-11 12:52:14 -04003427 * {@link #setGroup(String) group}. This must be called on all notifications you want to
3428 * mute. For example, if you want only the summary of your group to make noise, all
3429 * children in the group should have the group alert behavior {@link #GROUP_ALERT_SUMMARY}.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003430 *
3431 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
3432 */
3433 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
3434 mN.mGroupAlertBehavior = groupAlertBehavior;
3435 return this;
3436 }
3437
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003438 /**
3439 * Sets the intent that will be used to display app content in a floating window
3440 * over the existing foreground activity.
3441 *
3442 * <p>This intent will be ignored unless this notification is posted to a channel that
3443 * allows {@link NotificationChannel#canOverlayApps() app overlays}.</p>
3444 *
3445 * <p>Notifications with a valid and allowed app overlay intent will be displayed as
3446 * floating windows outside of the notification shade on unlocked devices. When a user
3447 * interacts with one of these windows, this app overlay intent will be invoked and
3448 * displayed.</p>
3449 */
3450 public Builder setAppOverlayIntent(PendingIntent intent) {
3451 mN.mAppOverlayIntent = intent;
3452 return this;
3453 }
3454
Jeff Sharkey000ce802017-04-29 13:13:27 -06003455 /** @removed */
3456 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003457 public Builder setChannel(String channelId) {
3458 mN.mChannelId = channelId;
3459 return this;
3460 }
3461
3462 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003463 * Specifies the channel the notification should be delivered on.
3464 */
3465 public Builder setChannelId(String channelId) {
3466 mN.mChannelId = channelId;
3467 return this;
3468 }
3469
Jeff Sharkey000ce802017-04-29 13:13:27 -06003470 /** @removed */
3471 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05003472 public Builder setTimeout(long durationMs) {
3473 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05003474 return this;
3475 }
3476
3477 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003478 * Specifies a duration in milliseconds after which this notification should be canceled,
3479 * if it is not already canceled.
3480 */
3481 public Builder setTimeoutAfter(long durationMs) {
3482 mN.mTimeout = durationMs;
3483 return this;
3484 }
3485
3486 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003487 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003488 *
3489 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
3490 * shown anymore by default and must be opted into by using
3491 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003492 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003493 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08003494 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003495 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003496 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08003497 return this;
3498 }
3499
Joe Onoratocb109a02011-01-18 17:57:41 -08003500 /**
Griff Hazen50c11652014-05-16 09:46:31 -07003501 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07003502 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003503 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
3504 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07003505 */
3506 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003507 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07003508 return this;
3509 }
3510
3511 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003512 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08003513 *
3514 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003515 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003516 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003517 * Useful when showing an elapsed time (like an ongoing phone call).
3518 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08003519 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07003520 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08003521 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003522 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003523 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07003524 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003525 */
3526 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003527 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003528 return this;
3529 }
3530
3531 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08003532 * Sets the Chronometer to count down instead of counting up.
3533 *
3534 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
3535 * If it isn't set the chronometer will count up.
3536 *
3537 * @see #setUsesChronometer(boolean)
3538 */
Adrian Roos96b7e202016-05-17 13:50:38 -07003539 public Builder setChronometerCountDown(boolean countDown) {
3540 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003541 return this;
3542 }
3543
3544 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003545 * Set the small icon resource, which will be used to represent the notification in the
3546 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003547 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003548
3549 * The platform template for the expanded view will draw this icon in the left, unless a
3550 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3551 * icon will be moved to the right-hand side.
3552 *
3553
3554 * @param icon
3555 * A resource ID in the application's package of the drawable to use.
3556 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003557 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003558 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003559 return setSmallIcon(icon != 0
3560 ? Icon.createWithResource(mContext, icon)
3561 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003562 }
3563
Joe Onoratocb109a02011-01-18 17:57:41 -08003564 /**
3565 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3566 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3567 * LevelListDrawable}.
3568 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003569 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003570 * @param level The level to use for the icon.
3571 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003572 * @see Notification#icon
3573 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003574 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003575 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003576 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003577 return setSmallIcon(icon);
3578 }
3579
3580 /**
3581 * Set the small icon, which will be used to represent the notification in the
koprivadebd4ee2018-09-13 10:59:46 -07003582 * status bar and content view (unless overridden there by a
Dan Sandlerd63f9322015-05-06 15:18:49 -04003583 * {@link #setLargeIcon(Bitmap) large icon}).
3584 *
3585 * @param icon An Icon object to use.
3586 * @see Notification#icon
3587 */
3588 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003589 mN.setSmallIcon(icon);
3590 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3591 mN.icon = icon.getResId();
3592 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003593 return this;
3594 }
3595
Joe Onoratocb109a02011-01-18 17:57:41 -08003596 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003597 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003598 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003599 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003600 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003601 return this;
3602 }
3603
Joe Onoratocb109a02011-01-18 17:57:41 -08003604 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003605 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003606 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003607 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003608 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003609 return this;
3610 }
3611
Joe Onoratocb109a02011-01-18 17:57:41 -08003612 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003613 * This provides some additional information that is displayed in the notification. No
3614 * guarantees are given where exactly it is displayed.
3615 *
3616 * <p>This information should only be provided if it provides an essential
3617 * benefit to the understanding of the notification. The more text you provide the
3618 * less readable it becomes. For example, an email client should only provide the account
3619 * name here if more than one email account has been added.</p>
3620 *
3621 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3622 * notification header area.
3623 *
3624 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3625 * this will be shown in the third line of text in the platform notification template.
3626 * You should not be using {@link #setProgress(int, int, boolean)} at the
3627 * same time on those versions; they occupy the same place.
3628 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003629 */
3630 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003631 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003632 return this;
3633 }
3634
3635 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003636 * Provides text that will appear as a link to your application's settings.
3637 *
3638 * <p>This text does not appear within notification {@link Style templates} but may
3639 * appear when the user uses an affordance to learn more about the notification.
3640 * Additionally, this text will not appear unless you provide a valid link target by
3641 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3642 *
3643 * <p>This text is meant to be concise description about what the user can customize
3644 * when they click on this link. The recommended maximum length is 40 characters.
3645 * @param text
3646 * @return
3647 */
3648 public Builder setSettingsText(CharSequence text) {
3649 mN.mSettingsText = safeCharSequence(text);
3650 return this;
3651 }
3652
3653 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003654 * Set the remote input history.
3655 *
3656 * This should be set to the most recent inputs that have been sent
3657 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3658 * longer relevant (e.g. for chat notifications once the other party has responded).
3659 *
3660 * The most recent input must be stored at the 0 index, the second most recent at the
3661 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3662 * and how much of each individual input is shown.
3663 *
3664 * <p>Note: The reply text will only be shown on notifications that have least one action
3665 * with a {@code RemoteInput}.</p>
3666 */
3667 public Builder setRemoteInputHistory(CharSequence[] text) {
3668 if (text == null) {
3669 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3670 } else {
3671 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3672 CharSequence[] safe = new CharSequence[N];
3673 for (int i = 0; i < N; i++) {
3674 safe[i] = safeCharSequence(text[i]);
3675 }
3676 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3677 }
3678 return this;
3679 }
3680
3681 /**
Kenny Guya0f6de82018-04-06 16:20:16 +01003682 * Sets whether remote history entries view should have a spinner.
3683 * @hide
3684 */
3685 public Builder setShowRemoteInputSpinner(boolean showSpinner) {
3686 mN.extras.putBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER, showSpinner);
3687 return this;
3688 }
3689
3690 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +01003691 * Sets whether smart reply buttons should be hidden.
3692 * @hide
3693 */
3694 public Builder setHideSmartReplies(boolean hideSmartReplies) {
3695 mN.extras.putBoolean(EXTRA_HIDE_SMART_REPLIES, hideSmartReplies);
3696 return this;
3697 }
3698
3699 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003700 * Sets the number of items this notification represents. May be displayed as a badge count
3701 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003702 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08003703 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003704 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003705 return this;
3706 }
3707
Joe Onoratocb109a02011-01-18 17:57:41 -08003708 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003709 * A small piece of additional information pertaining to this notification.
3710 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003711 * The platform template will draw this on the last line of the notification, at the far
3712 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003713 *
3714 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3715 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3716 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003717 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003718 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003719 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003720 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003721 return this;
3722 }
3723
Joe Onoratocb109a02011-01-18 17:57:41 -08003724 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003725 * Set the progress this notification represents.
3726 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003727 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003728 */
3729 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003730 mN.extras.putInt(EXTRA_PROGRESS, progress);
3731 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3732 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003733 return this;
3734 }
3735
3736 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003737 * Supply a custom RemoteViews to use instead of the platform template.
3738 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003739 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003740 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003741 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003742 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003743 return setCustomContentView(views);
3744 }
3745
3746 /**
3747 * Supply custom RemoteViews to use instead of the platform template.
3748 *
3749 * This will override the layout that would otherwise be constructed by this Builder
3750 * object.
3751 */
3752 public Builder setCustomContentView(RemoteViews contentView) {
3753 mN.contentView = contentView;
3754 return this;
3755 }
3756
3757 /**
3758 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3759 *
3760 * This will override the expanded layout that would otherwise be constructed by this
3761 * Builder object.
3762 */
3763 public Builder setCustomBigContentView(RemoteViews contentView) {
3764 mN.bigContentView = contentView;
3765 return this;
3766 }
3767
3768 /**
3769 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3770 *
3771 * This will override the heads-up layout that would otherwise be constructed by this
3772 * Builder object.
3773 */
3774 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3775 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003776 return this;
3777 }
3778
Joe Onoratocb109a02011-01-18 17:57:41 -08003779 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003780 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3781 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003782 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3783 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3784 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003785 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003786 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003787 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003788 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003789 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003790 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003791 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003792 return this;
3793 }
3794
Joe Onoratocb109a02011-01-18 17:57:41 -08003795 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003796 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3797 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003798 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003799 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003800 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003801 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003802 return this;
3803 }
3804
Joe Onoratocb109a02011-01-18 17:57:41 -08003805 /**
3806 * An intent to launch instead of posting the notification to the status bar.
3807 * Only for use with extremely high-priority notifications demanding the user's
3808 * <strong>immediate</strong> attention, such as an incoming phone call or
3809 * alarm clock that the user has explicitly set to a particular time.
3810 * If this facility is used for something else, please give the user an option
3811 * to turn it off and use a normal notification, as this can be extremely
3812 * disruptive.
3813 *
Chris Wren47c20a12014-06-18 17:27:29 -04003814 * <p>
3815 * The system UI may choose to display a heads-up notification, instead of
3816 * launching this intent, while the user is using the device.
3817 * </p>
3818 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003819 * @param intent The pending intent to launch.
3820 * @param highPriority Passing true will cause this notification to be sent
3821 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003822 *
3823 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003824 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003825 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003826 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003827 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3828 return this;
3829 }
3830
Joe Onoratocb109a02011-01-18 17:57:41 -08003831 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003832 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003833 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003834 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003835 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003836 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003837 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003838 return this;
3839 }
3840
Joe Onoratocb109a02011-01-18 17:57:41 -08003841 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003842 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003843 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003844 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003845 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003846 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003847 setTicker(tickerText);
3848 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003849 return this;
3850 }
3851
Joe Onoratocb109a02011-01-18 17:57:41 -08003852 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003853 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003854 *
3855 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04003856 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3857 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04003858 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003859 public Builder setLargeIcon(Bitmap b) {
3860 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3861 }
3862
3863 /**
3864 * Add a large icon to the notification content view.
3865 *
3866 * In the platform template, this image will be shown on the left of the notification view
3867 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3868 * badge atop the large icon).
3869 */
3870 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003871 mN.mLargeIcon = icon;
3872 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08003873 return this;
3874 }
3875
Joe Onoratocb109a02011-01-18 17:57:41 -08003876 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003877 * Set the sound to play.
3878 *
John Spurlockc0650f022014-07-19 13:22:39 -04003879 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
3880 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003881 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003882 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003883 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003884 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08003885 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003886 mN.sound = sound;
3887 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08003888 return this;
3889 }
3890
Joe Onoratocb109a02011-01-18 17:57:41 -08003891 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003892 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08003893 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003894 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
3895 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003896 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003897 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07003898 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003899 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08003900 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003901 mN.sound = sound;
3902 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08003903 return this;
3904 }
3905
Joe Onoratocb109a02011-01-18 17:57:41 -08003906 /**
John Spurlockc0650f022014-07-19 13:22:39 -04003907 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
3908 * use during playback.
3909 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003910 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04003911 * @see Notification#sound
3912 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003913 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04003914 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003915 mN.sound = sound;
3916 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04003917 return this;
3918 }
3919
3920 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003921 * Set the vibration pattern to use.
3922 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003923 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
3924 * <code>pattern</code> parameter.
3925 *
Chris Wren47c20a12014-06-18 17:27:29 -04003926 * <p>
3927 * A notification that vibrates is more likely to be presented as a heads-up notification.
3928 * </p>
3929 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003930 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003931 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08003932 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003933 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003934 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003935 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08003936 return this;
3937 }
3938
Joe Onoratocb109a02011-01-18 17:57:41 -08003939 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003940 * Set the desired color for the indicator LED on the device, as well as the
3941 * blink duty cycle (specified in milliseconds).
3942 *
3943
3944 * Not all devices will honor all (or even any) of these values.
3945 *
Julia Reynolds529e3322017-02-06 08:33:01 -05003946 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003947 * @see Notification#ledARGB
3948 * @see Notification#ledOnMS
3949 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08003950 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003951 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08003952 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003953 mN.ledARGB = argb;
3954 mN.ledOnMS = onMs;
3955 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003956 if (onMs != 0 || offMs != 0) {
3957 mN.flags |= FLAG_SHOW_LIGHTS;
3958 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003959 return this;
3960 }
3961
Joe Onoratocb109a02011-01-18 17:57:41 -08003962 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003963 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08003964 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003965
3966 * Ongoing notifications cannot be dismissed by the user, so your application or service
3967 * must take care of canceling them.
3968 *
3969
3970 * They are typically used to indicate a background task that the user is actively engaged
3971 * with (e.g., playing music) or is pending in some way and therefore occupying the device
3972 * (e.g., a file download, sync operation, active network connection).
3973 *
3974
3975 * @see Notification#FLAG_ONGOING_EVENT
Joe Onoratocb109a02011-01-18 17:57:41 -08003976 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003977 public Builder setOngoing(boolean ongoing) {
3978 setFlag(FLAG_ONGOING_EVENT, ongoing);
3979 return this;
3980 }
3981
Joe Onoratocb109a02011-01-18 17:57:41 -08003982 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08003983 * Set whether this notification should be colorized. When set, the color set with
3984 * {@link #setColor(int)} will be used as the background color of this notification.
3985 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08003986 * This should only be used for high priority ongoing tasks like navigation, an ongoing
3987 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08003988 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07003989 * For most styles, the coloring will only be applied if the notification is for a
3990 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08003991 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07003992 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08003993 *
Aurimas Liutikas7f695332018-05-31 21:07:32 -07003994 * @see #setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08003995 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08003996 */
3997 public Builder setColorized(boolean colorize) {
3998 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
3999 return this;
4000 }
4001
4002 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08004003 * Set this flag if you would only like the sound, vibrate
4004 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004005 *
4006 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08004007 */
Joe Onorato46439ce2010-11-19 13:56:21 -08004008 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
4009 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
4010 return this;
4011 }
4012
Joe Onoratocb109a02011-01-18 17:57:41 -08004013 /**
Julia Reynolds04499532016-09-13 14:04:53 -04004014 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004015 *
4016 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08004017 */
Joe Onorato46439ce2010-11-19 13:56:21 -08004018 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08004019 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08004020 return this;
4021 }
4022
Joe Onoratocb109a02011-01-18 17:57:41 -08004023 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08004024 * Set whether or not this notification should not bridge to other devices.
4025 *
4026 * <p>Some notifications can be bridged to other devices for remote display.
4027 * This hint can be set to recommend this notification not be bridged.
4028 */
4029 public Builder setLocalOnly(boolean localOnly) {
4030 setFlag(FLAG_LOCAL_ONLY, localOnly);
4031 return this;
4032 }
4033
4034 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004035 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08004036 * <p>
4037 * The value should be one or more of the following fields combined with
4038 * bitwise-or:
4039 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
4040 * <p>
4041 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004042 *
4043 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05004044 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004045 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08004046 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004047 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004048 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004049 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08004050 return this;
4051 }
4052
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004053 /**
4054 * Set the priority of this notification.
4055 *
4056 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004057 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004058 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004059 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07004060 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004061 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004062 return this;
4063 }
Joe Malin8d40d042012-11-05 11:36:40 -08004064
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004065 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04004066 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08004067 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04004068 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004069 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04004070 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004071 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004072 return this;
4073 }
4074
4075 /**
Chris Wrendde75302014-03-26 17:24:15 -04004076 * Add a person that is relevant to this notification.
4077 *
Chris Wrene6c48932014-09-29 17:19:27 -04004078 * <P>
4079 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04004080 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4081 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4082 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04004083 * </P>
4084 *
4085 * <P>
4086 * The person should be specified by the {@code String} representation of a
4087 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
4088 * </P>
4089 *
4090 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
4091 * URIs. The path part of these URIs must exist in the contacts database, in the
4092 * appropriate column, or the reference will be discarded as invalid. Telephone schema
4093 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
Selim Cineke7238dd2017-12-14 17:48:32 -08004094 * It is also possible to provide a URI with the schema {@code name:} in order to uniquely
4095 * identify a person without an entry in the contacts database.
Chris Wrene6c48932014-09-29 17:19:27 -04004096 * </P>
4097 *
4098 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04004099 * @see Notification#EXTRA_PEOPLE
Selim Cineke7238dd2017-12-14 17:48:32 -08004100 * @deprecated use {@link #addPerson(Person)}
Chris Wrendde75302014-03-26 17:24:15 -04004101 */
Chris Wrene6c48932014-09-29 17:19:27 -04004102 public Builder addPerson(String uri) {
Selim Cinek9acd6732018-03-23 16:39:02 -07004103 addPerson(new Person.Builder().setUri(uri).build());
Selim Cineke7238dd2017-12-14 17:48:32 -08004104 return this;
4105 }
4106
4107 /**
4108 * Add a person that is relevant to this notification.
4109 *
4110 * <P>
4111 * Depending on user preferences, this annotation may allow the notification to pass
4112 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4113 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4114 * appear more prominently in the user interface.
4115 * </P>
4116 *
4117 * <P>
4118 * A person should usually contain a uri in order to benefit from the ranking boost.
4119 * However, even if no uri is provided, it's beneficial to provide other people in the
4120 * notification, such that listeners and voice only devices can announce and handle them
4121 * properly.
4122 * </P>
4123 *
4124 * @param person the person to add.
4125 * @see Notification#EXTRA_PEOPLE_LIST
4126 */
4127 public Builder addPerson(Person person) {
4128 mPersonList.add(person);
Chris Wrendde75302014-03-26 17:24:15 -04004129 return this;
4130 }
4131
4132 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004133 * Set this notification to be part of a group of notifications sharing the same key.
4134 * Grouped notifications may display in a cluster or stack on devices which
4135 * support such rendering.
4136 *
4137 * <p>To make this notification the summary for its group, also call
4138 * {@link #setGroupSummary}. A sort order can be specified for group members by using
4139 * {@link #setSortKey}.
4140 * @param groupKey The group key of the group.
4141 * @return this object for method chaining
4142 */
4143 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004144 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004145 return this;
4146 }
4147
4148 /**
4149 * Set this notification to be the group summary for a group of notifications.
4150 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04004151 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
4152 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004153 * @param isGroupSummary Whether this notification should be a group summary.
4154 * @return this object for method chaining
4155 */
4156 public Builder setGroupSummary(boolean isGroupSummary) {
4157 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
4158 return this;
4159 }
4160
4161 /**
4162 * Set a sort key that orders this notification among other notifications from the
4163 * same package. This can be useful if an external sort was already applied and an app
4164 * would like to preserve this. Notifications will be sorted lexicographically using this
4165 * value, although providing different priorities in addition to providing sort key may
4166 * cause this value to be ignored.
4167 *
4168 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07004169 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004170 *
4171 * @see String#compareTo(String)
4172 */
4173 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004174 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004175 return this;
4176 }
4177
4178 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004179 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004180 *
Griff Hazen720042b2014-02-24 15:46:56 -08004181 * <p>Values within the Bundle will replace existing extras values in this Builder.
4182 *
4183 * @see Notification#extras
4184 */
Griff Hazen959591e2014-05-15 22:26:18 -07004185 public Builder addExtras(Bundle extras) {
4186 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004187 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08004188 }
4189 return this;
4190 }
4191
4192 /**
4193 * Set metadata for this notification.
4194 *
4195 * <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 -04004196 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004197 * called.
4198 *
Griff Hazen720042b2014-02-24 15:46:56 -08004199 * <p>Replaces any existing extras values with those from the provided Bundle.
4200 * Use {@link #addExtras} to merge in metadata instead.
4201 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004202 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004203 */
Griff Hazen959591e2014-05-15 22:26:18 -07004204 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004205 if (extras != null) {
4206 mUserExtras = extras;
4207 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004208 return this;
4209 }
4210
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004211 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004212 * Get the current metadata Bundle used by this notification Builder.
4213 *
4214 * <p>The returned Bundle is shared with this Builder.
4215 *
4216 * <p>The current contents of this Bundle are copied into the Notification each time
4217 * {@link #build()} is called.
4218 *
4219 * @see Notification#extras
4220 */
4221 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004222 return mUserExtras;
4223 }
4224
4225 private Bundle getAllExtras() {
4226 final Bundle saveExtras = (Bundle) mUserExtras.clone();
4227 saveExtras.putAll(mN.extras);
4228 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08004229 }
4230
4231 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004232 * Add an action to this notification. Actions are typically displayed by
4233 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004234 * <p>
4235 * Every action must have an icon (32dp square and matching the
4236 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4237 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4238 * <p>
4239 * A notification in its expanded form can display up to 3 actions, from left to right in
4240 * the order they were added. Actions will not be displayed when the notification is
4241 * collapsed, however, so be sure that any essential functions may be accessed by the user
4242 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004243 *
4244 * @param icon Resource ID of a drawable that represents the action.
4245 * @param title Text describing the action.
4246 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04004247 *
4248 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004249 */
Dan Sandler86647982015-05-13 23:41:13 -04004250 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004251 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004252 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004253 return this;
4254 }
4255
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004256 /**
Griff Hazen959591e2014-05-15 22:26:18 -07004257 * Add an action to this notification. Actions are typically displayed by
4258 * the system as a button adjacent to the notification content.
4259 * <p>
4260 * Every action must have an icon (32dp square and matching the
4261 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4262 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4263 * <p>
4264 * A notification in its expanded form can display up to 3 actions, from left to right in
4265 * the order they were added. Actions will not be displayed when the notification is
4266 * collapsed, however, so be sure that any essential functions may be accessed by the user
4267 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
4268 *
4269 * @param action The action to add.
4270 */
4271 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08004272 if (action != null) {
4273 mActions.add(action);
4274 }
Griff Hazen959591e2014-05-15 22:26:18 -07004275 return this;
4276 }
4277
4278 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004279 * Alter the complete list of actions attached to this notification.
4280 * @see #addAction(Action).
4281 *
4282 * @param actions
4283 * @return
4284 */
4285 public Builder setActions(Action... actions) {
4286 mActions.clear();
4287 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08004288 if (actions[i] != null) {
4289 mActions.add(actions[i]);
4290 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004291 }
4292 return this;
4293 }
4294
4295 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004296 * Add a rich notification style to be applied at build time.
4297 *
4298 * @param style Object responsible for modifying the notification style.
4299 */
4300 public Builder setStyle(Style style) {
4301 if (mStyle != style) {
4302 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004303 if (mStyle != null) {
4304 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004305 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
4306 } else {
4307 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004308 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004309 }
4310 return this;
4311 }
4312
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004313 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05004314 * Returns the style set by {@link #setStyle(Style)}.
4315 */
4316 public Style getStyle() {
4317 return mStyle;
4318 }
4319
4320 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004321 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07004322 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004323 * @return The same Builder.
4324 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06004325 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004326 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004327 return this;
4328 }
4329
4330 /**
4331 * Supply a replacement Notification whose contents should be shown in insecure contexts
4332 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
4333 * @param n A replacement notification, presumably with some or all info redacted.
4334 * @return The same Builder.
4335 */
4336 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004337 if (n != null) {
4338 mN.publicVersion = new Notification();
4339 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
4340 } else {
4341 mN.publicVersion = null;
4342 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004343 return this;
4344 }
4345
Griff Hazenb720abe2014-05-20 13:15:30 -07004346 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004347 * Apply an extender to this notification builder. Extenders may be used to add
4348 * metadata or change options on this builder.
4349 */
Griff Hazen61a9e862014-05-22 16:05:19 -07004350 public Builder extend(Extender extender) {
4351 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004352 return this;
4353 }
4354
Dan Sandler4e787062015-06-17 15:09:48 -04004355 /**
4356 * @hide
4357 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04004358 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08004359 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004360 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004361 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004362 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004363 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04004364 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08004365 }
4366
Dan Sandler26e81cf2014-05-06 10:01:27 -04004367 /**
4368 * Sets {@link Notification#color}.
4369 *
4370 * @param argb The accent color to use
4371 *
4372 * @return The same Builder.
4373 */
Tor Norbye80756e32015-03-02 09:39:27 -08004374 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004375 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004376 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04004377 return this;
4378 }
4379
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004380 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04004381 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
4382 // This user can never be a badged profile,
4383 // and also includes USER_ALL system notifications.
4384 return null;
4385 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02004386 // Note: This assumes that the current user can read the profile badge of the
4387 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08004388 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05004389 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004390 }
4391
4392 private Bitmap getProfileBadge() {
4393 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01004394 if (badge == null) {
4395 return null;
4396 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004397 final int size = mContext.getResources().getDimensionPixelSize(
4398 R.dimen.notification_badge_size);
4399 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004400 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004401 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004402 badge.draw(canvas);
4403 return bitmap;
4404 }
4405
Selim Cinekc848c3a2016-01-13 15:27:30 -08004406 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01004407 Bitmap profileBadge = getProfileBadge();
4408
Kenny Guy98193ea2014-07-24 19:54:37 +01004409 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004410 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
4411 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004412 if (isColorized()) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004413 contentView.setDrawableTint(R.id.profile_badge, false,
4414 getPrimaryTextColor(), PorterDuff.Mode.SRC_ATOP);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004415 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004416 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004417 }
4418
Julia Reynoldsfc640012018-02-21 12:25:27 -05004419 /**
4420 * @hide
4421 */
4422 public boolean usesStandardHeader() {
4423 if (mN.mUsesStandardHeader) {
4424 return true;
4425 }
4426 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.N) {
4427 if (mN.contentView == null && mN.bigContentView == null) {
4428 return true;
4429 }
4430 }
4431 boolean contentViewUsesHeader = mN.contentView == null
4432 || STANDARD_LAYOUTS.contains(mN.contentView.getLayoutId());
4433 boolean bigContentViewUsesHeader = mN.bigContentView == null
4434 || STANDARD_LAYOUTS.contains(mN.bigContentView.getLayoutId());
4435 return contentViewUsesHeader && bigContentViewUsesHeader;
4436 }
4437
Christoph Studerfe718432014-09-01 18:21:18 +02004438 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004439 resetNotificationHeader(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02004440 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08004441 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004442 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08004443 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004444 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08004445 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08004446 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004447 }
4448
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004449 /**
4450 * Resets the notification header to its original state
4451 */
4452 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07004453 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
4454 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08004455 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004456 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02004457 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004458 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07004459 contentView.setTextViewText(R.id.header_text, null);
Selim Cinekafeed292017-12-12 17:32:44 -08004460 contentView.setViewVisibility(R.id.header_text_secondary, View.GONE);
4461 contentView.setTextViewText(R.id.header_text_secondary, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004462 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinekafeed292017-12-12 17:32:44 -08004463 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004464 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004465 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004466 contentView.setImageViewIcon(R.id.profile_badge, null);
4467 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Gus Prevasa3226492018-10-23 11:10:09 -04004468 contentView.setViewVisibility(R.id.alerted_icon, View.GONE);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004469 mN.mUsesStandardHeader = false;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004470 }
4471
Selim Cinek384804b2018-04-18 14:31:07 +08004472 private RemoteViews applyStandardTemplate(int resId, TemplateBindResult result) {
4473 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this),
4474 result);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004475 }
4476
4477 /**
4478 * @param hasProgress whether the progress bar should be shown and set
Selim Cinek384804b2018-04-18 14:31:07 +08004479 * @param result
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004480 */
Selim Cinek384804b2018-04-18 14:31:07 +08004481 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress,
4482 TemplateBindResult result) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004483 return applyStandardTemplate(resId, mParams.reset().hasProgress(hasProgress)
Selim Cinek384804b2018-04-18 14:31:07 +08004484 .fillTextsFrom(this), result);
Adrian Roosc1a80b02016-04-05 14:54:55 -07004485 }
4486
Selim Cinek384804b2018-04-18 14:31:07 +08004487 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p,
4488 TemplateBindResult result) {
Kenny Guy77320062014-08-27 21:37:15 +01004489 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04004490
Christoph Studerfe718432014-09-01 18:21:18 +02004491 resetStandardTemplate(contentView);
4492
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004493 final Bundle ex = mN.extras;
Selim Cinek7b9605b2017-01-19 17:36:00 -08004494 updateBackgroundColor(contentView);
Selim Cinekafeed292017-12-12 17:32:44 -08004495 bindNotificationHeader(contentView, p.ambient, p.headerTextSecondary);
Selim Cinek384804b2018-04-18 14:31:07 +08004496 bindLargeIconAndReply(contentView, p, result);
Adrian Roos70d7aa32017-01-11 15:39:06 -08004497 boolean showProgress = handleProgressBar(p.hasProgress, contentView, ex);
4498 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08004499 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004500 contentView.setTextViewText(R.id.title, processTextSpans(p.title));
Adrian Roos72171622017-01-27 10:32:06 -08004501 if (!p.ambient) {
4502 setTextViewColorPrimary(contentView, R.id.title);
4503 }
Selim Cinek954cc232016-05-20 13:29:23 -07004504 contentView.setViewLayoutWidth(R.id.title, showProgress
4505 ? ViewGroup.LayoutParams.WRAP_CONTENT
4506 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08004507 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08004508 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08004509 int textId = showProgress ? com.android.internal.R.id.text_line_1
4510 : com.android.internal.R.id.text;
Selim Cinek48f66b72017-08-18 16:17:51 -07004511 contentView.setTextViewText(textId, processTextSpans(p.text));
Adrian Roos72171622017-01-27 10:32:06 -08004512 if (!p.ambient) {
4513 setTextViewColorSecondary(contentView, textId);
4514 }
Selim Cinek41598732016-01-11 16:58:37 -08004515 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08004516 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08004517
Selim Cinek279fa862016-06-14 10:57:25 -07004518 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004519
Selim Cinek29603462015-11-17 19:04:39 -08004520 return contentView;
4521 }
4522
Selim Cinek48f66b72017-08-18 16:17:51 -07004523 private CharSequence processTextSpans(CharSequence text) {
Lucas Dupind3c99322018-09-27 10:22:29 -07004524 if (hasForegroundColor() || mInNightMode) {
Lucas Dupina291d192018-06-07 13:59:42 -07004525 return ContrastColorUtil.clearColorSpans(text);
Selim Cinek48f66b72017-08-18 16:17:51 -07004526 }
4527 return text;
4528 }
4529
Selim Cinek7b9605b2017-01-19 17:36:00 -08004530 private void setTextViewColorPrimary(RemoteViews contentView, int id) {
4531 ensureColors();
4532 contentView.setTextColor(id, mPrimaryTextColor);
4533 }
4534
Selim Cinek48f66b72017-08-18 16:17:51 -07004535 private boolean hasForegroundColor() {
4536 return mForegroundColor != COLOR_INVALID;
4537 }
4538
Selim Cinek389edcd2017-05-11 19:16:44 -07004539 /**
4540 * @return the primary text color
4541 * @hide
4542 */
4543 @VisibleForTesting
4544 public int getPrimaryTextColor() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004545 ensureColors();
4546 return mPrimaryTextColor;
4547 }
4548
Selim Cinek389edcd2017-05-11 19:16:44 -07004549 /**
4550 * @return the secondary text color
4551 * @hide
4552 */
4553 @VisibleForTesting
4554 public int getSecondaryTextColor() {
4555 ensureColors();
4556 return mSecondaryTextColor;
4557 }
4558
Selim Cinek7b9605b2017-01-19 17:36:00 -08004559 private void setTextViewColorSecondary(RemoteViews contentView, int id) {
4560 ensureColors();
4561 contentView.setTextColor(id, mSecondaryTextColor);
4562 }
4563
4564 private void ensureColors() {
4565 int backgroundColor = getBackgroundColor();
4566 if (mPrimaryTextColor == COLOR_INVALID
4567 || mSecondaryTextColor == COLOR_INVALID
Selim Cinek7b9605b2017-01-19 17:36:00 -08004568 || mTextColorsAreForBackground != backgroundColor) {
4569 mTextColorsAreForBackground = backgroundColor;
Selim Cinek48f66b72017-08-18 16:17:51 -07004570 if (!hasForegroundColor() || !isColorized()) {
Lucas Dupina291d192018-06-07 13:59:42 -07004571 mPrimaryTextColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004572 backgroundColor, mInNightMode);
Lucas Dupina291d192018-06-07 13:59:42 -07004573 mSecondaryTextColor = ContrastColorUtil.resolveSecondaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004574 backgroundColor, mInNightMode);
Selim Cinekc7f5a822018-03-20 19:32:06 -07004575 if (backgroundColor != COLOR_DEFAULT && isColorized()) {
Lucas Dupina291d192018-06-07 13:59:42 -07004576 mPrimaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004577 mPrimaryTextColor, backgroundColor, 4.5);
Lucas Dupina291d192018-06-07 13:59:42 -07004578 mSecondaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004579 mSecondaryTextColor, backgroundColor, 4.5);
4580 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07004581 } else {
Lucas Dupina291d192018-06-07 13:59:42 -07004582 double backLum = ContrastColorUtil.calculateLuminance(backgroundColor);
4583 double textLum = ContrastColorUtil.calculateLuminance(mForegroundColor);
4584 double contrast = ContrastColorUtil.calculateContrast(mForegroundColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004585 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07004586 // We only respect the given colors if worst case Black or White still has
4587 // contrast
4588 boolean backgroundLight = backLum > textLum
4589 && satisfiesTextContrast(backgroundColor, Color.BLACK)
4590 || backLum <= textLum
4591 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004592 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07004593 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004594 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004595 mForegroundColor,
4596 backgroundColor,
4597 true /* findFG */,
4598 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004599 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004600 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004601 } else {
4602 mSecondaryTextColor =
Lucas Dupina291d192018-06-07 13:59:42 -07004603 ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004604 mForegroundColor,
4605 backgroundColor,
4606 true /* findFG */,
4607 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004608 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004609 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004610 }
4611 } else {
4612 mPrimaryTextColor = mForegroundColor;
Lucas Dupina291d192018-06-07 13:59:42 -07004613 mSecondaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004614 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4615 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Lucas Dupina291d192018-06-07 13:59:42 -07004616 if (ContrastColorUtil.calculateContrast(mSecondaryTextColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004617 backgroundColor) < 4.5f) {
4618 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07004619 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004620 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004621 mSecondaryTextColor,
4622 backgroundColor,
4623 true /* findFG */,
4624 4.5f);
4625 } else {
4626 mSecondaryTextColor
Lucas Dupina291d192018-06-07 13:59:42 -07004627 = ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004628 mSecondaryTextColor,
4629 backgroundColor,
4630 true /* findFG */,
4631 4.5f);
4632 }
Lucas Dupina291d192018-06-07 13:59:42 -07004633 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004634 mSecondaryTextColor, backgroundLight
4635 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4636 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004637 }
4638 }
4639 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004640 }
4641 }
4642
4643 private void updateBackgroundColor(RemoteViews contentView) {
4644 if (isColorized()) {
4645 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
4646 getBackgroundColor());
4647 } else {
4648 // Clear it!
4649 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4650 0);
4651 }
4652 }
4653
Selim Cinek860b6da2015-12-16 19:02:19 -08004654 /**
4655 * @param remoteView the remote view to update the minheight in
4656 * @param hasMinHeight does it have a mimHeight
4657 * @hide
4658 */
4659 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4660 int minHeight = 0;
4661 if (hasMinHeight) {
4662 // we need to set the minHeight of the notification
4663 minHeight = mContext.getResources().getDimensionPixelSize(
4664 com.android.internal.R.dimen.notification_min_content_height);
4665 }
4666 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4667 }
4668
Selim Cinek29603462015-11-17 19:04:39 -08004669 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004670 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4671 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4672 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4673 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004674 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004675 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004676 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004677 contentView.setProgressBackgroundTintList(
4678 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4679 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08004680 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08004681 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004682 contentView.setProgressTintList(R.id.progress, colorStateList);
4683 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004684 }
Selim Cinek29603462015-11-17 19:04:39 -08004685 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004686 } else {
4687 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004688 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004689 }
Joe Onorato561d3852010-11-20 18:09:34 -08004690 }
4691
Selim Cinek384804b2018-04-18 14:31:07 +08004692 private void bindLargeIconAndReply(RemoteViews contentView, StandardTemplateParams p,
4693 TemplateBindResult result) {
4694 boolean largeIconShown = bindLargeIcon(contentView, p.hideLargeIcon || p.ambient);
Selim Cinek1c72fa02018-04-23 18:00:54 +08004695 boolean replyIconShown = bindReplyIcon(contentView, p.hideReplyIcon || p.ambient);
Selim Cinek384804b2018-04-18 14:31:07 +08004696 contentView.setViewVisibility(R.id.right_icon_container,
Selim Cinek1c72fa02018-04-23 18:00:54 +08004697 largeIconShown || replyIconShown ? View.VISIBLE : View.GONE);
4698 int marginEnd = calculateMarginEnd(largeIconShown, replyIconShown);
4699 contentView.setViewLayoutMarginEnd(R.id.line1, marginEnd);
4700 contentView.setViewLayoutMarginEnd(R.id.text, marginEnd);
4701 contentView.setViewLayoutMarginEnd(R.id.progress, marginEnd);
Selim Cinek384804b2018-04-18 14:31:07 +08004702 if (result != null) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08004703 result.setIconMarginEnd(marginEnd);
Selim Cinek384804b2018-04-18 14:31:07 +08004704 }
4705 }
4706
Selim Cinek1c72fa02018-04-23 18:00:54 +08004707 private int calculateMarginEnd(boolean largeIconShown, boolean replyIconShown) {
4708 int marginEnd = 0;
4709 int contentMargin = mContext.getResources().getDimensionPixelSize(
4710 R.dimen.notification_content_margin_end);
4711 int iconSize = mContext.getResources().getDimensionPixelSize(
4712 R.dimen.notification_right_icon_size);
4713 if (replyIconShown) {
4714 // The size of the reply icon
4715 marginEnd += iconSize;
4716
4717 int replyInset = mContext.getResources().getDimensionPixelSize(
4718 R.dimen.notification_reply_inset);
4719 // We're subtracting the inset of the reply icon to make sure it's
4720 // aligned nicely on the right, and remove it from the following padding
4721 marginEnd -= replyInset * 2;
4722 }
4723 if (largeIconShown) {
4724 // adding size of the right icon
4725 marginEnd += iconSize;
4726
4727 if (replyIconShown) {
4728 // We also add some padding to the reply icon if it's around
4729 marginEnd += contentMargin;
4730 }
4731 }
4732 if (replyIconShown || largeIconShown) {
4733 // The padding to the content
4734 marginEnd += contentMargin;
4735 }
4736 return marginEnd;
4737 }
4738
Selim Cinek384804b2018-04-18 14:31:07 +08004739 /**
4740 * Bind the large icon.
4741 * @return if the largeIcon is visible
4742 */
4743 private boolean bindLargeIcon(RemoteViews contentView, boolean hideLargeIcon) {
Selim Cinek279fa862016-06-14 10:57:25 -07004744 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4745 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4746 }
Selim Cinek88188f22017-09-19 16:46:56 -07004747 boolean showLargeIcon = mN.mLargeIcon != null && !hideLargeIcon;
4748 if (showLargeIcon) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004749 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4750 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
4751 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Selim Cinek88188f22017-09-19 16:46:56 -07004752 }
Selim Cinek384804b2018-04-18 14:31:07 +08004753 return showLargeIcon;
4754 }
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004755
Selim Cinek384804b2018-04-18 14:31:07 +08004756 /**
4757 * Bind the reply icon.
4758 * @return if the reply icon is visible
4759 */
4760 private boolean bindReplyIcon(RemoteViews contentView, boolean hideReplyIcon) {
4761 boolean actionVisible = !hideReplyIcon;
4762 Action action = null;
Selim Cinek88188f22017-09-19 16:46:56 -07004763 if (actionVisible) {
Selim Cinek384804b2018-04-18 14:31:07 +08004764 action = findReplyAction();
4765 actionVisible = action != null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004766 }
Selim Cinek384804b2018-04-18 14:31:07 +08004767 if (actionVisible) {
4768 contentView.setViewVisibility(R.id.reply_icon_action, View.VISIBLE);
4769 contentView.setDrawableTint(R.id.reply_icon_action,
4770 false /* targetBackground */,
Selim Cinek4717d862018-04-19 09:19:15 +08004771 getNeutralColor(),
Selim Cinek384804b2018-04-18 14:31:07 +08004772 PorterDuff.Mode.SRC_ATOP);
4773 contentView.setOnClickPendingIntent(R.id.reply_icon_action, action.actionIntent);
4774 contentView.setRemoteInputs(R.id.reply_icon_action, action.mRemoteInputs);
4775 } else {
4776 contentView.setRemoteInputs(R.id.reply_icon_action, null);
4777 }
4778 contentView.setViewVisibility(R.id.reply_icon_action,
4779 actionVisible ? View.VISIBLE : View.GONE);
4780 return actionVisible;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004781 }
4782
4783 private Action findReplyAction() {
4784 ArrayList<Action> actions = mActions;
4785 if (mOriginalActions != null) {
4786 actions = mOriginalActions;
4787 }
4788 int numActions = actions.size();
4789 for (int i = 0; i < numActions; i++) {
4790 Action action = actions.get(i);
4791 if (hasValidRemoteInput(action)) {
4792 return action;
4793 }
4794 }
4795 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004796 }
4797
Selim Cinekafeed292017-12-12 17:32:44 -08004798 private void bindNotificationHeader(RemoteViews contentView, boolean ambient,
4799 CharSequence secondaryHeaderText) {
Adrian Roos487374f2017-01-11 15:48:14 -08004800 bindSmallIcon(contentView, ambient);
4801 bindHeaderAppName(contentView, ambient);
4802 if (!ambient) {
4803 // Ambient view does not have these
4804 bindHeaderText(contentView);
Selim Cinekafeed292017-12-12 17:32:44 -08004805 bindHeaderTextSecondary(contentView, secondaryHeaderText);
Adrian Roos487374f2017-01-11 15:48:14 -08004806 bindHeaderChronometerAndTime(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08004807 bindProfileBadge(contentView);
4808 }
Julia Reynolds6013e5c2018-04-18 08:43:34 -04004809 bindActivePermissions(contentView, ambient);
Adrian Roosd83e9992017-03-16 15:17:57 -07004810 bindExpandButton(contentView);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004811 mN.mUsesStandardHeader = true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004812 }
4813
Julia Reynolds6013e5c2018-04-18 08:43:34 -04004814 private void bindActivePermissions(RemoteViews contentView, boolean ambient) {
Selim Cinek4717d862018-04-19 09:19:15 +08004815 int color = ambient ? resolveAmbientColor() : getNeutralColor();
Julia Reynoldsb887b232018-04-10 16:38:08 -04004816 contentView.setDrawableTint(R.id.camera, false, color, PorterDuff.Mode.SRC_ATOP);
4817 contentView.setDrawableTint(R.id.mic, false, color, PorterDuff.Mode.SRC_ATOP);
4818 contentView.setDrawableTint(R.id.overlay, false, color, PorterDuff.Mode.SRC_ATOP);
4819 }
4820
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004821 private void bindExpandButton(RemoteViews contentView) {
Selim Cinekc7f5a822018-03-20 19:32:06 -07004822 int color = isColorized() ? getPrimaryTextColor() : getSecondaryTextColor();
Sunny Goyal5b153922017-09-21 21:00:36 -07004823 contentView.setDrawableTint(R.id.expand_button, false, color,
4824 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08004825 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08004826 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004827 }
4828
4829 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
4830 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08004831 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004832 setTextViewColorSecondary(contentView, R.id.time_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004833 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
4834 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
4835 contentView.setLong(R.id.chronometer, "setBase",
4836 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
4837 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07004838 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07004839 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004840 setTextViewColorSecondary(contentView, R.id.chronometer);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004841 } else {
4842 contentView.setViewVisibility(R.id.time, View.VISIBLE);
4843 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004844 setTextViewColorSecondary(contentView, R.id.time);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004845 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004846 } else {
4847 // We still want a time to be set but gone, such that we can show and hide it
4848 // on demand in case it's a child notification without anything in the header
4849 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004850 }
4851 }
4852
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004853 private void bindHeaderText(RemoteViews contentView) {
4854 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4855 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05004856 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004857 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05004858 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004859 if (headerText == null
4860 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4861 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
4862 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
4863 }
4864 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004865 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek48f66b72017-08-18 16:17:51 -07004866 contentView.setTextViewText(R.id.header_text, processTextSpans(
4867 processLegacyText(headerText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004868 setTextViewColorSecondary(contentView, R.id.header_text);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004869 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
4870 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004871 setTextViewColorSecondary(contentView, R.id.header_text_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004872 }
4873 }
4874
Selim Cinekafeed292017-12-12 17:32:44 -08004875 private void bindHeaderTextSecondary(RemoteViews contentView, CharSequence secondaryText) {
4876 if (!TextUtils.isEmpty(secondaryText)) {
4877 contentView.setTextViewText(R.id.header_text_secondary, processTextSpans(
4878 processLegacyText(secondaryText)));
4879 setTextViewColorSecondary(contentView, R.id.header_text_secondary);
4880 contentView.setViewVisibility(R.id.header_text_secondary, View.VISIBLE);
4881 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.VISIBLE);
4882 setTextViewColorSecondary(contentView, R.id.header_text_secondary_divider);
4883 }
4884 }
4885
Adrian Rooseba05822016-04-22 17:09:27 -07004886 /**
4887 * @hide
4888 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01004889 @UnsupportedAppUsage
Adrian Rooseba05822016-04-22 17:09:27 -07004890 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004891 CharSequence name = null;
4892 final PackageManager pm = mContext.getPackageManager();
4893 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
4894 // only system packages which lump together a bunch of unrelated stuff
4895 // may substitute a different name to make the purpose of the
4896 // notification more clear. the correct package label should always
4897 // be accessible via SystemUI.
4898 final String pkg = mContext.getPackageName();
4899 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
4900 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
4901 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
4902 name = subName;
4903 } else {
4904 Log.w(TAG, "warning: pkg "
4905 + pkg + " attempting to substitute app name '" + subName
4906 + "' without holding perm "
4907 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
4908 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004909 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04004910 if (TextUtils.isEmpty(name)) {
4911 name = pm.getApplicationLabel(mContext.getApplicationInfo());
4912 }
4913 if (TextUtils.isEmpty(name)) {
4914 // still nothing?
4915 return null;
4916 }
4917
4918 return String.valueOf(name);
4919 }
Adrian Roos487374f2017-01-11 15:48:14 -08004920 private void bindHeaderAppName(RemoteViews contentView, boolean ambient) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004921 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos72171622017-01-27 10:32:06 -08004922 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004923 setTextViewColorPrimary(contentView, R.id.app_name_text);
4924 } else {
4925 contentView.setTextColor(R.id.app_name_text,
Selim Cinekc7f5a822018-03-20 19:32:06 -07004926 ambient ? resolveAmbientColor() : getSecondaryTextColor());
Selim Cinek7b9605b2017-01-19 17:36:00 -08004927 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004928 }
4929
Adrian Roos487374f2017-01-11 15:48:14 -08004930 private void bindSmallIcon(RemoteViews contentView, boolean ambient) {
Selim Cinek279fa862016-06-14 10:57:25 -07004931 if (mN.mSmallIcon == null && mN.icon != 0) {
4932 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
4933 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004934 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Sunny Goyal5b153922017-09-21 21:00:36 -07004935 contentView.setInt(R.id.icon, "setImageLevel", mN.iconLevel);
Adrian Roos487374f2017-01-11 15:48:14 -08004936 processSmallIconColor(mN.mSmallIcon, contentView, ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004937 }
4938
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004939 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004940 * @return true if the built notification will show the time or the chronometer; false
4941 * otherwise
4942 */
4943 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07004944 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004945 }
4946
Christoph Studerfe718432014-09-01 18:21:18 +02004947 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004948 // actions_container is only reset when there are no actions to avoid focus issues with
4949 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02004950 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004951 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08004952
4953 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
4954 big.setTextViewText(R.id.notification_material_reply_text_1, null);
Kenny Guya0f6de82018-04-06 16:20:16 +01004955 big.setViewVisibility(R.id.notification_material_reply_text_1_container, View.GONE);
4956 big.setViewVisibility(R.id.notification_material_reply_progress, View.GONE);
Adrian Roose458aa82015-12-08 16:17:19 -08004957
4958 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
4959 big.setTextViewText(R.id.notification_material_reply_text_2, null);
4960 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
4961 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07004962
Selim Cineked64a142018-02-06 18:06:01 -08004963 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
4964 R.dimen.notification_content_margin);
Christoph Studerfe718432014-09-01 18:21:18 +02004965 }
4966
Selim Cinek384804b2018-04-18 14:31:07 +08004967 private RemoteViews applyStandardTemplateWithActions(int layoutId,
4968 TemplateBindResult result) {
4969 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this),
4970 result);
Adrian Roos48d746a2016-04-12 14:57:28 -07004971 }
4972
Gustav Sennton1463d832018-11-06 16:12:48 +00004973 private static List<Notification.Action> filterOutContextualActions(
4974 List<Notification.Action> actions) {
4975 List<Notification.Action> nonContextualActions = new ArrayList<>();
4976 for (Notification.Action action : actions) {
4977 if (action.getSemanticAction()
4978 != Action.SEMANTIC_ACTION_CONTEXTUAL_SUGGESTION) {
4979 nonContextualActions.add(action);
4980 }
4981 }
4982 return nonContextualActions;
4983 }
4984
Adrian Roos70d7aa32017-01-11 15:39:06 -08004985 private RemoteViews applyStandardTemplateWithActions(int layoutId,
Selim Cinek384804b2018-04-18 14:31:07 +08004986 StandardTemplateParams p, TemplateBindResult result) {
4987 RemoteViews big = applyStandardTemplate(layoutId, p, result);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004988
Christoph Studerfe718432014-09-01 18:21:18 +02004989 resetStandardTemplateWithActions(big);
4990
Adrian Roose458aa82015-12-08 16:17:19 -08004991 boolean validRemoteInput = false;
4992
Gustav Sennton1463d832018-11-06 16:12:48 +00004993 // In the UI contextual actions appear separately from the standard actions, so we
4994 // filter them out here.
4995 List<Notification.Action> nonContextualActions = filterOutContextualActions(mActions);
4996
4997 int N = nonContextualActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08004998 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004999 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005000 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08005001 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005002 big.setViewVisibility(R.id.actions, View.VISIBLE);
Selim Cineked64a142018-02-06 18:06:01 -08005003 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Daniel Sandler8680bf82012-05-15 16:52:52 -04005004 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005005 for (int i=0; i<N; i++) {
Gustav Sennton1463d832018-11-06 16:12:48 +00005006 Action action = nonContextualActions.get(i);
5007
Selim Cinekffcc7cf2018-02-06 17:43:51 -08005008 boolean actionHasValidInput = hasValidRemoteInput(action);
5009 validRemoteInput |= actionHasValidInput;
Adrian Roose458aa82015-12-08 16:17:19 -08005010
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005011 final RemoteViews button = generateActionButton(action, emphazisedMode,
Selim Cinek396caca2018-04-10 17:46:46 -07005012 p.ambient);
5013 if (actionHasValidInput && !emphazisedMode) {
Selim Cinekffcc7cf2018-02-06 17:43:51 -08005014 // Clear the drawable
5015 button.setInt(R.id.action0, "setBackgroundResource", 0);
5016 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005017 big.addView(R.id.actions, button);
5018 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07005019 } else {
5020 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005021 }
Adrian Roose458aa82015-12-08 16:17:19 -08005022
5023 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08005024 if (!p.ambient && validRemoteInput && replyText != null
Selim Cinekbee4e072018-05-21 22:06:43 -07005025 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])
5026 && p.maxRemoteInputHistory > 0) {
Kenny Guya0f6de82018-04-06 16:20:16 +01005027 boolean showSpinner = mN.extras.getBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER);
Adrian Roose458aa82015-12-08 16:17:19 -08005028 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Kenny Guya0f6de82018-04-06 16:20:16 +01005029 big.setViewVisibility(R.id.notification_material_reply_text_1_container,
5030 View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005031 big.setTextViewText(R.id.notification_material_reply_text_1,
5032 processTextSpans(replyText[0]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005033 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1);
Kenny Guya0f6de82018-04-06 16:20:16 +01005034 big.setViewVisibility(R.id.notification_material_reply_progress,
5035 showSpinner ? View.VISIBLE : View.GONE);
5036 big.setProgressIndeterminateTintList(
5037 R.id.notification_material_reply_progress,
5038 ColorStateList.valueOf(
5039 isColorized() ? getPrimaryTextColor() : resolveContrastColor()));
Adrian Roose458aa82015-12-08 16:17:19 -08005040
Selim Cinekbee4e072018-05-21 22:06:43 -07005041 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])
5042 && p.maxRemoteInputHistory > 1) {
Adrian Roose458aa82015-12-08 16:17:19 -08005043 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005044 big.setTextViewText(R.id.notification_material_reply_text_2,
5045 processTextSpans(replyText[1]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005046 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2);
Adrian Roose458aa82015-12-08 16:17:19 -08005047
Selim Cinekbee4e072018-05-21 22:06:43 -07005048 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])
5049 && p.maxRemoteInputHistory > 2) {
Adrian Roose458aa82015-12-08 16:17:19 -08005050 big.setViewVisibility(
5051 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005052 big.setTextViewText(R.id.notification_material_reply_text_3,
5053 processTextSpans(replyText[2]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005054 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3);
Adrian Roose458aa82015-12-08 16:17:19 -08005055 }
5056 }
5057 }
5058
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005059 return big;
5060 }
5061
Adrian Roose458aa82015-12-08 16:17:19 -08005062 private boolean hasValidRemoteInput(Action action) {
5063 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
5064 // Weird actions
5065 return false;
5066 }
5067
5068 RemoteInput[] remoteInputs = action.getRemoteInputs();
5069 if (remoteInputs == null) {
5070 return false;
5071 }
5072
5073 for (RemoteInput r : remoteInputs) {
5074 CharSequence[] choices = r.getChoices();
5075 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
5076 return true;
5077 }
5078 }
5079 return false;
5080 }
5081
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005082 /**
5083 * Construct a RemoteViews for the final 1U notification layout. In order:
5084 * 1. Custom contentView from the caller
5085 * 2. Style's proposed content view
5086 * 3. Standard template view
5087 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005088 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005089 return createContentView(false /* increasedheight */ );
5090 }
5091
5092 /**
5093 * Construct a RemoteViews for the smaller content view.
5094 *
5095 * @param increasedHeight true if this layout be created with an increased height. Some
5096 * styles may support showing more then just that basic 1U size
5097 * and the system may decide to render important notifications
5098 * slightly bigger even when collapsed.
5099 *
5100 * @hide
5101 */
5102 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005103 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005104 return mN.contentView;
5105 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005106 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005107 if (styleView != null) {
5108 return styleView;
5109 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005110 }
Selim Cinek384804b2018-04-18 14:31:07 +08005111 return applyStandardTemplate(getBaseLayoutResource(), null /* result */);
Joe Onorato46439ce2010-11-19 13:56:21 -08005112 }
5113
Selim Cineka7679b62017-05-10 16:33:25 -07005114 private boolean useExistingRemoteView() {
5115 return mStyle == null || (!mStyle.displayCustomViewInline()
5116 && !mRebuildStyledRemoteViews);
5117 }
5118
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005119 /**
5120 * Construct a RemoteViews for the final big notification layout.
5121 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005122 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05005123 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07005124 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005125 return mN.bigContentView;
5126 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05005127 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08005128 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005129 } else if (mActions.size() != 0) {
Selim Cinek384804b2018-04-18 14:31:07 +08005130 result = applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5131 null /* result */);
Selim Cinek850a8542015-11-11 11:48:36 -05005132 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08005133 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05005134 return result;
5135 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005136
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005137 /**
Selim Cinek414ad332017-02-24 19:06:12 -08005138 * Construct a RemoteViews for the final notification header only. This will not be
5139 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005140 *
Adrian Roos6f6e1592017-05-02 16:22:53 -07005141 * @param ambient if true, generate the header for the ambient display layout.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005142 * @hide
5143 */
Adrian Roos6f6e1592017-05-02 16:22:53 -07005144 public RemoteViews makeNotificationHeader(boolean ambient) {
Selim Cinek414ad332017-02-24 19:06:12 -08005145 Boolean colorized = (Boolean) mN.extras.get(EXTRA_COLORIZED);
5146 mN.extras.putBoolean(EXTRA_COLORIZED, false);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005147 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Adrian Roos6f6e1592017-05-02 16:22:53 -07005148 ambient ? R.layout.notification_template_ambient_header
5149 : R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005150 resetNotificationHeader(header);
Selim Cinekafeed292017-12-12 17:32:44 -08005151 bindNotificationHeader(header, ambient, null);
Selim Cinek414ad332017-02-24 19:06:12 -08005152 if (colorized != null) {
5153 mN.extras.putBoolean(EXTRA_COLORIZED, colorized);
5154 } else {
5155 mN.extras.remove(EXTRA_COLORIZED);
5156 }
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005157 return header;
5158 }
5159
Adrian Roos487374f2017-01-11 15:48:14 -08005160 /**
5161 * Construct a RemoteViews for the ambient version of the notification.
5162 *
5163 * @hide
5164 */
5165 public RemoteViews makeAmbientNotification() {
5166 RemoteViews ambient = applyStandardTemplateWithActions(
5167 R.layout.notification_template_material_ambient,
Selim Cinek384804b2018-04-18 14:31:07 +08005168 mParams.reset().ambient(true).fillTextsFrom(this).hasProgress(false),
5169 null /* result */);
Adrian Roos487374f2017-01-11 15:48:14 -08005170 return ambient;
5171 }
5172
Selim Cinek29603462015-11-17 19:04:39 -08005173 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005174 if (result != null) {
5175 result.setViewVisibility(R.id.text_line_1, View.GONE);
5176 }
Selim Cinek29603462015-11-17 19:04:39 -08005177 }
5178
Selim Cinek6743c0b2017-01-18 18:24:01 -08005179 /**
5180 * Adapt the Notification header if this view is used as an expanded view.
5181 *
5182 * @hide
5183 */
5184 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005185 if (result != null) {
5186 result.setBoolean(R.id.notification_header, "setExpanded", true);
5187 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005188 }
5189
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005190 /**
5191 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005192 *
5193 * @param increasedHeight true if this layout be created with an increased height. Some
5194 * styles may support showing more then just that basic 1U size
5195 * and the system may decide to render important notifications
5196 * slightly bigger even when collapsed.
5197 *
5198 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005199 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005200 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005201 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005202 return mN.headsUpContentView;
5203 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08005204 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
5205 if (styleView != null) {
5206 return styleView;
5207 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05005208 } else if (mActions.size() == 0) {
5209 return null;
5210 }
5211
Selim Cinekbee4e072018-05-21 22:06:43 -07005212 // We only want at most a single remote input history to be shown here, otherwise
5213 // the content would become squished.
5214 StandardTemplateParams p = mParams.reset().fillTextsFrom(this)
5215 .setMaxRemoteInputHistory(1);
5216 return applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5217 p,
5218 null /* result */);
Chris Wren8fd39ec2014-02-27 17:43:26 -05005219 }
5220
Selim Cinek624c02db2015-12-14 21:00:02 -08005221 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08005222 * Construct a RemoteViews for the final heads-up notification layout.
5223 */
5224 public RemoteViews createHeadsUpContentView() {
5225 return createHeadsUpContentView(false /* useIncreasedHeight */);
5226 }
5227
5228 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08005229 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5230 *
5231 * @hide
5232 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005233 @UnsupportedAppUsage
Selim Cinek624c02db2015-12-14 21:00:02 -08005234 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005235 return makePublicView(false /* ambient */);
5236 }
5237
5238 /**
5239 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5240 *
5241 * @hide
5242 */
5243 public RemoteViews makePublicAmbientNotification() {
5244 return makePublicView(true /* ambient */);
5245 }
5246
5247 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08005248 if (mN.publicVersion != null) {
5249 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005250 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08005251 }
5252 Bundle savedBundle = mN.extras;
5253 Style style = mStyle;
5254 mStyle = null;
5255 Icon largeIcon = mN.mLargeIcon;
5256 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07005257 Bitmap largeIconLegacy = mN.largeIcon;
5258 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005259 ArrayList<Action> actions = mActions;
5260 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08005261 Bundle publicExtras = new Bundle();
5262 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
5263 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
5264 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
5265 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07005266 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
5267 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cineked07b962018-04-30 14:39:35 -07005268 String appName = savedBundle.getString(EXTRA_SUBSTITUTE_APP_NAME);
5269 if (appName != null) {
5270 publicExtras.putString(EXTRA_SUBSTITUTE_APP_NAME, appName);
5271 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005272 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07005273 RemoteViews view;
5274 if (ambient) {
5275 publicExtras.putCharSequence(EXTRA_TITLE,
5276 mContext.getString(com.android.internal.R.string.notification_hidden_text));
5277 view = makeAmbientNotification();
5278 } else{
5279 view = makeNotificationHeader(false /* ambient */);
5280 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
5281 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005282 mN.extras = savedBundle;
5283 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07005284 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005285 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08005286 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005287 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08005288 }
5289
Selim Cinek6743c0b2017-01-18 18:24:01 -08005290 /**
5291 * Construct a content view for the display when low - priority
5292 *
5293 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
5294 * a new subtext is created consisting of the content of the
5295 * notification.
5296 * @hide
5297 */
5298 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
5299 int color = mN.color;
5300 mN.color = COLOR_DEFAULT;
5301 CharSequence summary = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
5302 if (!useRegularSubtext || TextUtils.isEmpty(summary)) {
5303 CharSequence newSummary = createSummaryText();
5304 if (!TextUtils.isEmpty(newSummary)) {
5305 mN.extras.putCharSequence(EXTRA_SUB_TEXT, newSummary);
5306 }
5307 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08005308
Adrian Roos6f6e1592017-05-02 16:22:53 -07005309 RemoteViews header = makeNotificationHeader(false /* ambient */);
Selim Cinek1b554392017-02-28 17:22:49 -08005310 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08005311 if (summary != null) {
5312 mN.extras.putCharSequence(EXTRA_SUB_TEXT, summary);
5313 } else {
5314 mN.extras.remove(EXTRA_SUB_TEXT);
5315 }
5316 mN.color = color;
5317 return header;
5318 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005319
Selim Cinek6743c0b2017-01-18 18:24:01 -08005320 private CharSequence createSummaryText() {
5321 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
5322 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
5323 return titleText;
5324 }
5325 SpannableStringBuilder summary = new SpannableStringBuilder();
5326 if (titleText == null) {
5327 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
5328 }
5329 BidiFormatter bidi = BidiFormatter.getInstance();
5330 if (titleText != null) {
5331 summary.append(bidi.unicodeWrap(titleText));
5332 }
5333 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
5334 if (titleText != null && contentText != null) {
5335 summary.append(bidi.unicodeWrap(mContext.getText(
5336 R.string.notification_header_divider_symbol_with_spaces)));
5337 }
5338 if (contentText != null) {
5339 summary.append(bidi.unicodeWrap(contentText));
5340 }
5341 return summary;
5342 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05005343
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005344 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Selim Cinek396caca2018-04-10 17:46:46 -07005345 boolean ambient) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04005346 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07005347 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005348 emphazisedMode ? getEmphasizedActionLayoutResource()
5349 : tombstone ? getActionTombstoneLayoutResource()
5350 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04005351 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04005352 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04005353 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005354 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005355 if (action.mRemoteInputs != null) {
5356 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
5357 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005358 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07005359 // change the background bgColor
Selim Cinek981962e2016-07-20 20:41:58 -07005360 CharSequence title = action.title;
5361 ColorStateList[] outResultColor = null;
Selim Cinek396caca2018-04-10 17:46:46 -07005362 int background = resolveBackgroundColor();
Selim Cinek981962e2016-07-20 20:41:58 -07005363 if (isLegacy()) {
Lucas Dupina291d192018-06-07 13:59:42 -07005364 title = ContrastColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07005365 } else {
5366 outResultColor = new ColorStateList[1];
Selim Cinek396caca2018-04-10 17:46:46 -07005367 title = ensureColorSpanContrast(title, background, outResultColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005368 }
Selim Cinek48f66b72017-08-18 16:17:51 -07005369 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005370 setTextViewColorPrimary(button, R.id.action0);
Selim Cinek396caca2018-04-10 17:46:46 -07005371 int rippleColor;
5372 boolean hasColorOverride = outResultColor != null && outResultColor[0] != null;
5373 if (hasColorOverride) {
5374 // There's a span spanning the full text, let's take it and use it as the
5375 // background color
5376 background = outResultColor[0].getDefaultColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005377 int textColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07005378 background, mInNightMode);
Selim Cinek396caca2018-04-10 17:46:46 -07005379 button.setTextColor(R.id.action0, textColor);
5380 rippleColor = textColor;
Anthony Chenad4d1582017-04-10 16:07:58 -07005381 } else if (mN.color != COLOR_DEFAULT && !isColorized() && mTintActionButtons) {
Selim Cinek396caca2018-04-10 17:46:46 -07005382 rippleColor = resolveContrastColor();
5383 button.setTextColor(R.id.action0, rippleColor);
5384 } else {
5385 rippleColor = getPrimaryTextColor();
Selim Cinek981962e2016-07-20 20:41:58 -07005386 }
Selim Cinek396caca2018-04-10 17:46:46 -07005387 // We only want about 20% alpha for the ripple
5388 rippleColor = (rippleColor & 0x00ffffff) | 0x33000000;
5389 button.setColorStateList(R.id.action0, "setRippleColor",
5390 ColorStateList.valueOf(rippleColor));
5391 button.setColorStateList(R.id.action0, "setButtonBackground",
5392 ColorStateList.valueOf(background));
5393 button.setBoolean(R.id.action0, "setHasStroke", !hasColorOverride);
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005394 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07005395 button.setTextViewText(R.id.action0, processTextSpans(
5396 processLegacyText(action.title)));
Adrian Roos72171622017-01-27 10:32:06 -08005397 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005398 setTextViewColorPrimary(button, R.id.action0);
Anthony Chenad4d1582017-04-10 16:07:58 -07005399 } else if (mN.color != COLOR_DEFAULT && mTintActionButtons) {
Adrian Roos487374f2017-01-11 15:48:14 -08005400 button.setTextColor(R.id.action0,
5401 ambient ? resolveAmbientColor() : resolveContrastColor());
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005402 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005403 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005404 return button;
5405 }
5406
Joe Onoratocb109a02011-01-18 17:57:41 -08005407 /**
Selim Cinek981962e2016-07-20 20:41:58 -07005408 * Ensures contrast on color spans against a background color. also returns the color of the
5409 * text if a span was found that spans over the whole text.
5410 *
5411 * @param charSequence the charSequence on which the spans are
5412 * @param background the background color to ensure the contrast against
5413 * @param outResultColor an array in which a color will be returned as the first element if
5414 * there exists a full length color span.
5415 * @return the contrasted charSequence
5416 */
5417 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
5418 ColorStateList[] outResultColor) {
5419 if (charSequence instanceof Spanned) {
5420 Spanned ss = (Spanned) charSequence;
5421 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
5422 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
5423 for (Object span : spans) {
5424 Object resultSpan = span;
5425 int spanStart = ss.getSpanStart(span);
5426 int spanEnd = ss.getSpanEnd(span);
5427 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
5428 if (resultSpan instanceof CharacterStyle) {
5429 resultSpan = ((CharacterStyle) span).getUnderlying();
5430 }
5431 if (resultSpan instanceof TextAppearanceSpan) {
5432 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
5433 ColorStateList textColor = originalSpan.getTextColor();
5434 if (textColor != null) {
5435 int[] colors = textColor.getColors();
5436 int[] newColors = new int[colors.length];
5437 for (int i = 0; i < newColors.length; i++) {
Lucas Dupina291d192018-06-07 13:59:42 -07005438 newColors[i] = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005439 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005440 }
5441 textColor = new ColorStateList(textColor.getStates().clone(),
5442 newColors);
Selim Cinek396caca2018-04-10 17:46:46 -07005443 if (fullLength) {
5444 outResultColor[0] = textColor;
5445 // Let's drop the color from the span
5446 textColor = null;
5447 }
Selim Cinek981962e2016-07-20 20:41:58 -07005448 resultSpan = new TextAppearanceSpan(
5449 originalSpan.getFamily(),
5450 originalSpan.getTextStyle(),
5451 originalSpan.getTextSize(),
5452 textColor,
5453 originalSpan.getLinkTextColor());
Selim Cinek981962e2016-07-20 20:41:58 -07005454 }
5455 } else if (resultSpan instanceof ForegroundColorSpan) {
5456 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
5457 int foregroundColor = originalSpan.getForegroundColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005458 foregroundColor = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005459 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005460 if (fullLength) {
5461 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
Selim Cinek396caca2018-04-10 17:46:46 -07005462 resultSpan = null;
5463 } else {
5464 resultSpan = new ForegroundColorSpan(foregroundColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005465 }
5466 } else {
5467 resultSpan = span;
5468 }
Selim Cinek396caca2018-04-10 17:46:46 -07005469 if (resultSpan != null) {
5470 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
5471 }
Selim Cinek981962e2016-07-20 20:41:58 -07005472 }
5473 return builder;
5474 }
5475 return charSequence;
5476 }
5477
5478 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005479 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07005480 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005481 */
5482 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005483 if (!mIsLegacyInitialized) {
5484 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
5485 < Build.VERSION_CODES.LOLLIPOP;
5486 mIsLegacyInitialized = true;
5487 }
5488 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005489 }
5490
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005491 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08005492 return processLegacyText(charSequence, false /* ambient */);
5493 }
5494
5495 private CharSequence processLegacyText(CharSequence charSequence, boolean ambient) {
5496 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
5497 boolean wantLightText = ambient;
5498 if (isAlreadyLightText != wantLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005499 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005500 } else {
5501 return charSequence;
5502 }
5503 }
5504
Dan Sandler26e81cf2014-05-06 10:01:27 -04005505 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005506 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04005507 */
Adrian Roos487374f2017-01-11 15:48:14 -08005508 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
5509 boolean ambient) {
Selim Cinekea4bef72015-12-02 15:51:10 -08005510 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005511 int color;
5512 if (ambient) {
5513 color = resolveAmbientColor();
5514 } else if (isColorized()) {
5515 color = getPrimaryTextColor();
5516 } else {
5517 color = resolveContrastColor();
5518 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005519 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07005520 contentView.setDrawableTint(R.id.icon, false, color,
5521 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005522
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005523 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005524 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08005525 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005526 }
5527
Dan Sandler26e81cf2014-05-06 10:01:27 -04005528 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005529 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04005530 * if it's grayscale).
5531 */
5532 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04005533 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
5534 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005535 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005536 // resolve color will fall back to the default when legacy
Sunny Goyal5b153922017-09-21 21:00:36 -07005537 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(),
5538 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01005539 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005540 }
5541
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05005542 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005543 if (mN.color != COLOR_DEFAULT) {
5544 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02005545 }
Jorim Jaggi74419312014-06-10 20:57:21 +02005546 }
5547
Adrian Roos4ff3b122016-02-01 12:26:13 -08005548 int resolveContrastColor() {
5549 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
5550 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005551 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005552
Selim Cinekac5f0272017-05-02 16:05:41 -07005553 int color;
Selim Cinekc7f5a822018-03-20 19:32:06 -07005554 int background = mContext.getColor(
5555 com.android.internal.R.color.notification_material_background_color);
Selim Cinekac5f0272017-05-02 16:05:41 -07005556 if (mN.color == COLOR_DEFAULT) {
5557 ensureColors();
Lucas Dupinf03e7522018-06-25 16:21:13 -07005558 color = ContrastColorUtil.resolveDefaultColor(mContext, background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005559 } else {
Lucas Dupina291d192018-06-07 13:59:42 -07005560 color = ContrastColorUtil.resolveContrastColor(mContext, mN.color,
Anthony Chenad4d1582017-04-10 16:07:58 -07005561 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005562 }
5563 if (Color.alpha(color) < 255) {
5564 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005565 color = ContrastColorUtil.compositeColors(color, background);
Selim Cinekac5f0272017-05-02 16:05:41 -07005566 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005567 mCachedContrastColorIsFor = mN.color;
Selim Cinekac5f0272017-05-02 16:05:41 -07005568 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005569 }
5570
Selim Cinek4717d862018-04-19 09:19:15 +08005571 int resolveNeutralColor() {
5572 if (mNeutralColor != COLOR_INVALID) {
5573 return mNeutralColor;
5574 }
5575 int background = mContext.getColor(
5576 com.android.internal.R.color.notification_material_background_color);
Lucas Dupinf03e7522018-06-25 16:21:13 -07005577 mNeutralColor = ContrastColorUtil.resolveDefaultColor(mContext, background,
5578 mInNightMode);
Selim Cinek4717d862018-04-19 09:19:15 +08005579 if (Color.alpha(mNeutralColor) < 255) {
5580 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005581 mNeutralColor = ContrastColorUtil.compositeColors(mNeutralColor, background);
Selim Cinek4717d862018-04-19 09:19:15 +08005582 }
5583 return mNeutralColor;
5584 }
5585
Adrian Roos487374f2017-01-11 15:48:14 -08005586 int resolveAmbientColor() {
5587 if (mCachedAmbientColorIsFor == mN.color && mCachedAmbientColorIsFor != COLOR_INVALID) {
5588 return mCachedAmbientColor;
5589 }
Lucas Dupina291d192018-06-07 13:59:42 -07005590 final int contrasted = ContrastColorUtil.resolveAmbientColor(mContext, mN.color);
Adrian Roos487374f2017-01-11 15:48:14 -08005591
5592 mCachedAmbientColorIsFor = mN.color;
5593 return mCachedAmbientColor = contrasted;
5594 }
5595
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005596 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005597 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005598 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08005599 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005600 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005601 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005602 mN.actions = new Action[mActions.size()];
5603 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005604 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005605 if (!mPersonList.isEmpty()) {
Selim Cineke7238dd2017-12-14 17:48:32 -08005606 mN.extras.putParcelableArrayList(EXTRA_PEOPLE_LIST, mPersonList);
Dan Sandler0bf2ed82013-12-21 23:33:41 -06005607 }
Selim Cinek247fa012016-02-18 09:50:48 -08005608 if (mN.bigContentView != null || mN.contentView != null
5609 || mN.headsUpContentView != null) {
5610 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
5611 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005612 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08005613 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005614
Julia Reynolds3b848122016-02-26 10:45:32 -05005615 /**
5616 * Creates a Builder from an existing notification so further changes can be made.
5617 * @param context The context for your application / activity.
5618 * @param n The notification to create a Builder from.
5619 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005620 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005621 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005622 ApplicationInfo applicationInfo = n.extras.getParcelable(
5623 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005624 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05005625 if (applicationInfo != null) {
5626 try {
5627 builderContext = context.createApplicationContext(applicationInfo,
5628 Context.CONTEXT_RESTRICTED);
5629 } catch (NameNotFoundException e) {
5630 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
5631 builderContext = context; // try with our context
5632 }
5633 } else {
5634 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02005635 }
5636
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005637 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005638 }
5639
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005640 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005641 * @deprecated Use {@link #build()} instead.
5642 */
5643 @Deprecated
5644 public Notification getNotification() {
5645 return build();
5646 }
5647
5648 /**
5649 * Combine all of the options that have been set and return a new {@link Notification}
5650 * object.
5651 */
5652 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005653 // first, add any extras from the calling code
5654 if (mUserExtras != null) {
5655 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005656 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005657
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005658 mN.creationTime = System.currentTimeMillis();
5659
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005660 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05005661 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02005662
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005663 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005664
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005665 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02005666 mStyle.reduceImageSizes(mContext);
5667 mStyle.purgeResources();
Selim Cinek90343862018-02-01 11:07:11 -08005668 mStyle.validate(mContext);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005669 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005670 }
Selim Cinekd0426622017-07-11 13:19:59 +02005671 mN.reduceImageSizes(mContext);
5672
Adrian Roos5081c0d2016-02-26 16:04:19 -08005673 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07005674 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005675 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005676 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005677 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
5678 mN.contentView.getSequenceNumber());
5679 }
5680 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005681 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005682 if (mN.bigContentView != null) {
5683 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
5684 mN.bigContentView.getSequenceNumber());
5685 }
5686 }
5687 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005688 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005689 if (mN.headsUpContentView != null) {
5690 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
5691 mN.headsUpContentView.getSequenceNumber());
5692 }
5693 }
5694 }
5695
Julia Reynolds4c0c2022016-02-02 15:11:59 -05005696 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
5697 mN.flags |= FLAG_SHOW_LIGHTS;
5698 }
5699
Adrian Roosfb921842017-10-26 14:49:56 +02005700 mN.allPendingIntents = null;
5701
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005702 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005703 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005704
5705 /**
5706 * Apply this Builder to an existing {@link Notification} object.
5707 *
5708 * @hide
5709 */
5710 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005711 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005712 return n;
5713 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005714
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005715 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005716 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005717 * change. Also removes extenders on low ram devices, as
5718 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005719 *
5720 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5721 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005722 * @hide
5723 */
Kristian Monsen05f34792018-04-09 10:27:16 +02005724 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam,
5725 Context context) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005726 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005727
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005728 // Only strip views for known Styles because we won't know how to
5729 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005730 if (!isLowRam
5731 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005732 && getNotificationStyleClass(templateClass) == null) {
5733 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005734 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005735
5736 // Only strip unmodified BuilderRemoteViews.
5737 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005738 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005739 n.contentView.getSequenceNumber();
5740 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005741 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005742 n.bigContentView.getSequenceNumber();
5743 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005744 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005745 n.headsUpContentView.getSequenceNumber();
5746
5747 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005748 if (!isLowRam
5749 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005750 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005751 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005752
5753 Notification clone = n.clone();
5754 if (stripContentView) {
5755 clone.contentView = null;
5756 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5757 }
5758 if (stripBigContentView) {
5759 clone.bigContentView = null;
5760 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5761 }
5762 if (stripHeadsUpContentView) {
5763 clone.headsUpContentView = null;
5764 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5765 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005766 if (isLowRam) {
Kristian Monsen05f34792018-04-09 10:27:16 +02005767 String[] allowedServices = context.getResources().getStringArray(
5768 R.array.config_allowedManagedServicesOnLowRamDevices);
5769 if (allowedServices.length == 0) {
5770 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5771 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5772 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5773 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005774 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005775 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005776 }
5777
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005778 @UnsupportedAppUsage
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005779 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005780 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005781 }
5782
5783 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005784 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005785 }
5786
5787 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005788 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005789 }
5790
5791 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005792 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005793 }
5794
5795 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005796 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005797 }
5798
Adrian Roosc1a80b02016-04-05 14:54:55 -07005799 private int getMessagingLayoutResource() {
5800 return R.layout.notification_template_material_messaging;
5801 }
5802
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005803 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005804 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005805 }
5806
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005807 private int getEmphasizedActionLayoutResource() {
5808 return R.layout.notification_material_action_emphasized;
5809 }
5810
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005811 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005812 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005813 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005814
5815 private int getBackgroundColor() {
5816 if (isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005817 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : mN.color;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005818 } else {
Selim Cinekc7f5a822018-03-20 19:32:06 -07005819 return COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005820 }
5821 }
5822
Selim Cinek396caca2018-04-10 17:46:46 -07005823 /**
Selim Cinek4717d862018-04-19 09:19:15 +08005824 * Gets a neutral color that can be used for icons or similar that should not stand out.
5825 */
5826 private int getNeutralColor() {
5827 if (isColorized()) {
5828 return getSecondaryTextColor();
5829 } else {
5830 return resolveNeutralColor();
5831 }
5832 }
5833
5834 /**
Selim Cinek396caca2018-04-10 17:46:46 -07005835 * Same as getBackgroundColor but also resolved the default color to the background.
5836 */
5837 private int resolveBackgroundColor() {
5838 int backgroundColor = getBackgroundColor();
5839 if (backgroundColor == COLOR_DEFAULT) {
5840 backgroundColor = mContext.getColor(
5841 com.android.internal.R.color.notification_material_background_color);
5842 }
5843 return backgroundColor;
5844 }
5845
Selim Cinek7b9605b2017-01-19 17:36:00 -08005846 private boolean isColorized() {
5847 return mN.isColorized();
5848 }
Selim Cinek99104832017-01-25 14:47:33 -08005849
Anthony Chenad4d1582017-04-10 16:07:58 -07005850 private boolean shouldTintActionButtons() {
5851 return mTintActionButtons;
5852 }
5853
Selim Cinek99104832017-01-25 14:47:33 -08005854 private boolean textColorsNeedInversion() {
5855 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
5856 return false;
5857 }
5858 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
5859 return targetSdkVersion > Build.VERSION_CODES.M
5860 && targetSdkVersion < Build.VERSION_CODES.O;
5861 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005862
5863 /**
5864 * Set a color palette to be used as the background and textColors
5865 *
5866 * @param backgroundColor the color to be used as the background
5867 * @param foregroundColor the color to be used as the foreground
5868 *
5869 * @hide
5870 */
5871 public void setColorPalette(int backgroundColor, int foregroundColor) {
5872 mBackgroundColor = backgroundColor;
5873 mForegroundColor = foregroundColor;
5874 mTextColorsAreForBackground = COLOR_INVALID;
5875 ensureColors();
5876 }
Selim Cinekac5f0272017-05-02 16:05:41 -07005877
5878 /**
Selim Cineka7679b62017-05-10 16:33:25 -07005879 * Forces all styled remoteViews to be built from scratch and not use any cached
5880 * RemoteViews.
5881 * This is needed for legacy apps that are baking in their remoteviews into the
5882 * notification.
5883 *
5884 * @hide
5885 */
5886 public void setRebuildStyledRemoteViews(boolean rebuild) {
5887 mRebuildStyledRemoteViews = rebuild;
5888 }
Selim Cinekaa9db1f2018-02-27 17:35:47 -08005889
5890 /**
5891 * Get the text that should be displayed in the statusBar when heads upped. This is
5892 * usually just the app name, but may be different depending on the style.
5893 *
5894 * @param publicMode If true, return a text that is safe to display in public.
5895 *
5896 * @hide
5897 */
5898 public CharSequence getHeadsUpStatusBarText(boolean publicMode) {
5899 if (mStyle != null && !publicMode) {
5900 CharSequence text = mStyle.getHeadsUpStatusBarText();
5901 if (!TextUtils.isEmpty(text)) {
5902 return text;
5903 }
5904 }
5905 return loadHeaderAppName();
5906 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005907 }
5908
5909 /**
Selim Cinekd0426622017-07-11 13:19:59 +02005910 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
5911 * remote views.
5912 *
5913 * @hide
5914 */
5915 void reduceImageSizes(Context context) {
5916 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
5917 return;
5918 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005919 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02005920 if (mLargeIcon != null || largeIcon != null) {
5921 Resources resources = context.getResources();
5922 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07005923 int maxWidth = resources.getDimensionPixelSize(isLowRam
5924 ? R.dimen.notification_right_icon_size_low_ram
5925 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02005926 int maxHeight = maxWidth;
5927 if (MediaStyle.class.equals(style)
5928 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005929 maxHeight = resources.getDimensionPixelSize(isLowRam
5930 ? R.dimen.notification_media_image_max_height_low_ram
5931 : R.dimen.notification_media_image_max_height);
5932 maxWidth = resources.getDimensionPixelSize(isLowRam
5933 ? R.dimen.notification_media_image_max_width_low_ram
5934 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02005935 }
5936 if (mLargeIcon != null) {
5937 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
5938 }
5939 if (largeIcon != null) {
5940 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
5941 }
5942 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005943 reduceImageSizesForRemoteView(contentView, context, isLowRam);
5944 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
5945 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02005946 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
5947 }
5948
Selim Cineka8cb1262017-08-15 16:53:44 -07005949 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
5950 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02005951 if (remoteView != null) {
5952 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07005953 int maxWidth = resources.getDimensionPixelSize(isLowRam
5954 ? R.dimen.notification_custom_view_max_image_width_low_ram
5955 : R.dimen.notification_custom_view_max_image_width);
5956 int maxHeight = resources.getDimensionPixelSize(isLowRam
5957 ? R.dimen.notification_custom_view_max_image_height_low_ram
5958 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02005959 remoteView.reduceImageSizes(maxWidth, maxHeight);
5960 }
5961 }
5962
5963 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005964 * @return whether this notification is a foreground service notification
Gus Prevasb0c1a462018-11-05 11:06:15 -05005965 * @hide
Selim Cinek7b9605b2017-01-19 17:36:00 -08005966 */
Gus Prevasb0c1a462018-11-05 11:06:15 -05005967 public boolean isForegroundService() {
Selim Cinek22714f12017-04-13 16:23:53 -07005968 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005969 }
5970
5971 /**
Selim Cinek99104832017-01-25 14:47:33 -08005972 * @return whether this notification has a media session attached
5973 * @hide
5974 */
5975 public boolean hasMediaSession() {
5976 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
5977 }
5978
5979 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005980 * @return the style class of this notification
5981 * @hide
5982 */
5983 public Class<? extends Notification.Style> getNotificationStyle() {
5984 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
5985
5986 if (!TextUtils.isEmpty(templateClass)) {
5987 return Notification.getNotificationStyleClass(templateClass);
5988 }
5989 return null;
5990 }
5991
5992 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005993 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08005994 *
5995 * @hide
5996 */
5997 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005998 if (isColorizedMedia()) {
5999 return true;
6000 }
Julia Reynolds4db59552017-06-30 13:34:01 -04006001 return extras.getBoolean(EXTRA_COLORIZED)
6002 && (hasColorizedPermission() || isForegroundService());
6003 }
6004
6005 /**
6006 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
6007 * permission. The permission is checked when a notification is enqueued.
6008 */
6009 private boolean hasColorizedPermission() {
6010 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07006011 }
6012
6013 /**
6014 * @return true if this notification is colorized and it is a media notification
6015 *
6016 * @hide
6017 */
6018 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08006019 Class<? extends Style> style = getNotificationStyle();
6020 if (MediaStyle.class.equals(style)) {
6021 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
6022 if ((colorized == null || colorized) && hasMediaSession()) {
6023 return true;
6024 }
6025 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6026 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
6027 return true;
6028 }
6029 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07006030 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006031 }
6032
Selim Cinek0847acd2017-04-24 19:48:29 -07006033
6034 /**
6035 * @return true if this is a media notification
6036 *
6037 * @hide
6038 */
6039 public boolean isMediaNotification() {
6040 Class<? extends Style> style = getNotificationStyle();
6041 if (MediaStyle.class.equals(style)) {
6042 return true;
6043 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6044 return true;
6045 }
6046 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006047 }
6048
Selim Cinek279fa862016-06-14 10:57:25 -07006049 private boolean hasLargeIcon() {
6050 return mLargeIcon != null || largeIcon != null;
6051 }
6052
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006053 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006054 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006055 * @hide
6056 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07006057 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006058 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
6059 }
6060
6061 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006062 * @return true if the notification will show a chronometer; false otherwise
6063 * @hide
6064 */
6065 public boolean showsChronometer() {
6066 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
6067 }
6068
6069 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04006070 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05006071 */
6072 @SystemApi
6073 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
6074 Class<? extends Style>[] classes = new Class[] {
6075 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
6076 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
6077 MessagingStyle.class };
6078 for (Class<? extends Style> innerClass : classes) {
6079 if (templateClass.equals(innerClass.getName())) {
6080 return innerClass;
6081 }
6082 }
6083 return null;
6084 }
6085
6086 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006087 * An object that can apply a rich notification style to a {@link Notification.Builder}
6088 * object.
6089 */
Griff Hazendfcb0802014-02-11 12:00:00 -08006090 public static abstract class Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07006091
6092 /**
6093 * The number of items allowed simulatanously in the remote input history.
6094 * @hide
6095 */
6096 static final int MAX_REMOTE_INPUT_HISTORY_LINES = 3;
Chris Wrend6297db2012-05-03 16:20:13 -04006097 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006098
6099 /**
6100 * @hide
6101 */
6102 protected CharSequence mSummaryText = null;
6103
6104 /**
6105 * @hide
6106 */
6107 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04006108
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006109 protected Builder mBuilder;
6110
Chris Wrend6297db2012-05-03 16:20:13 -04006111 /**
6112 * Overrides ContentTitle in the big form of the template.
6113 * This defaults to the value passed to setContentTitle().
6114 */
6115 protected void internalSetBigContentTitle(CharSequence title) {
6116 mBigContentTitle = title;
6117 }
6118
6119 /**
6120 * Set the first line of text after the detail section in the big form of the template.
6121 */
6122 protected void internalSetSummaryText(CharSequence cs) {
6123 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04006124 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04006125 }
6126
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006127 public void setBuilder(Builder builder) {
6128 if (mBuilder != builder) {
6129 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07006130 if (mBuilder != null) {
6131 mBuilder.setStyle(this);
6132 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006133 }
6134 }
6135
Chris Wrend6297db2012-05-03 16:20:13 -04006136 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006137 if (mBuilder == null) {
6138 throw new IllegalArgumentException("Style requires a valid Builder object");
6139 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006140 }
Chris Wrend6297db2012-05-03 16:20:13 -04006141
6142 protected RemoteViews getStandardView(int layoutId) {
Selim Cinek384804b2018-04-18 14:31:07 +08006143 return getStandardView(layoutId, null);
6144 }
6145
6146 /**
6147 * Get the standard view for this style.
6148 *
6149 * @param layoutId The layout id to use
6150 * @param result The result where template bind information is saved.
6151 * @return A remoteView for this style.
6152 * @hide
6153 */
6154 protected RemoteViews getStandardView(int layoutId, TemplateBindResult result) {
Chris Wrend6297db2012-05-03 16:20:13 -04006155 checkBuilder();
6156
Christoph Studer4600f9b2014-07-22 22:44:43 +02006157 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006158 CharSequence oldBuilderContentTitle =
6159 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04006160 if (mBigContentTitle != null) {
6161 mBuilder.setContentTitle(mBigContentTitle);
6162 }
6163
Selim Cinek384804b2018-04-18 14:31:07 +08006164 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId, result);
Chris Wrend6297db2012-05-03 16:20:13 -04006165
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006166 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006167
Chris Wrend6297db2012-05-03 16:20:13 -04006168 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
6169 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04006170 } else {
6171 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04006172 }
6173
Chris Wrend6297db2012-05-03 16:20:13 -04006174 return contentView;
6175 }
6176
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006177 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006178 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006179 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08006180 *
6181 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006182 * @hide
6183 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08006184 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006185 return null;
6186 }
6187
6188 /**
6189 * Construct a Style-specific RemoteViews for the final big notification layout.
6190 * @hide
6191 */
6192 public RemoteViews makeBigContentView() {
6193 return null;
6194 }
6195
6196 /**
6197 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08006198 *
6199 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006200 * @hide
6201 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006202 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006203 return null;
6204 }
6205
6206 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006207 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006208 * @hide
6209 */
6210 public void addExtras(Bundle extras) {
6211 if (mSummaryTextSet) {
6212 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
6213 }
6214 if (mBigContentTitle != null) {
6215 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
6216 }
Chris Wren91ad5632013-06-05 15:05:57 -04006217 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006218 }
6219
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006220 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006221 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006222 * @hide
6223 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02006224 protected void restoreFromExtras(Bundle extras) {
6225 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
6226 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
6227 mSummaryTextSet = true;
6228 }
6229 if (extras.containsKey(EXTRA_TITLE_BIG)) {
6230 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
6231 }
6232 }
6233
6234
6235 /**
6236 * @hide
6237 */
6238 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006239 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006240 return wip;
6241 }
6242
Daniel Sandler0ec46202015-06-24 01:27:05 -04006243 /**
6244 * @hide
6245 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006246 public void purgeResources() {}
6247
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006248 /**
6249 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
6250 * attached to.
6251 *
6252 * @return the fully constructed Notification.
6253 */
6254 public Notification build() {
6255 checkBuilder();
6256 return mBuilder.build();
6257 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006258
6259 /**
6260 * @hide
6261 * @return true if the style positions the progress bar on the second line; false if the
6262 * style hides the progress bar
6263 */
6264 protected boolean hasProgress() {
6265 return true;
6266 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006267
6268 /**
6269 * @hide
6270 * @return Whether we should put the summary be put into the notification header
6271 */
6272 public boolean hasSummaryInHeader() {
6273 return true;
6274 }
Selim Cinek593610c2016-02-16 18:42:57 -08006275
6276 /**
6277 * @hide
6278 * @return Whether custom content views are displayed inline in the style
6279 */
6280 public boolean displayCustomViewInline() {
6281 return false;
6282 }
Selim Cinekd0426622017-07-11 13:19:59 +02006283
6284 /**
6285 * Reduces the image sizes contained in this style.
6286 *
6287 * @hide
6288 */
6289 public void reduceImageSizes(Context context) {
6290 }
Selim Cinek90343862018-02-01 11:07:11 -08006291
6292 /**
6293 * Validate that this style was properly composed. This is called at build time.
6294 * @hide
6295 */
6296 public void validate(Context context) {
6297 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006298
6299 /**
6300 * @hide
6301 */
6302 public abstract boolean areNotificationsVisiblyDifferent(Style other);
Selim Cinekc7f5a822018-03-20 19:32:06 -07006303
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006304 /**
koprivaa1a78482018-10-09 10:09:23 -07006305 * @return the text that should be displayed in the statusBar when heads-upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006306 * If {@code null} is returned, the default implementation will be used.
6307 *
6308 * @hide
6309 */
6310 public CharSequence getHeadsUpStatusBarText() {
6311 return null;
6312 }
Joe Onorato46439ce2010-11-19 13:56:21 -08006313 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006314
6315 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006316 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08006317 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006318 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006319 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006320 * Notification notif = new Notification.Builder(mContext)
6321 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
6322 * .setContentText(subject)
6323 * .setSmallIcon(R.drawable.new_post)
6324 * .setLargeIcon(aBitmap)
6325 * .setStyle(new Notification.BigPictureStyle()
6326 * .bigPicture(aBigBitmap))
6327 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006328 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006329 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006330 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006331 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006332 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006333 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006334 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006335 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006336
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006337 public BigPictureStyle() {
6338 }
6339
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006340 /**
6341 * @deprecated use {@code BigPictureStyle()}.
6342 */
6343 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006344 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006345 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006346 }
6347
Chris Wrend6297db2012-05-03 16:20:13 -04006348 /**
6349 * Overrides ContentTitle in the big form of the template.
6350 * This defaults to the value passed to setContentTitle().
6351 */
6352 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006353 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006354 return this;
6355 }
6356
6357 /**
6358 * Set the first line of text after the detail section in the big form of the template.
6359 */
6360 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006361 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006362 return this;
6363 }
6364
Chris Wren0bd664d2012-08-01 13:56:56 -04006365 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05006366 * @hide
6367 */
6368 public Bitmap getBigPicture() {
6369 return mPicture;
6370 }
6371
6372 /**
Chris Wren0bd664d2012-08-01 13:56:56 -04006373 * Provide the bitmap to be used as the payload for the BigPicture notification.
6374 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006375 public BigPictureStyle bigPicture(Bitmap b) {
6376 mPicture = b;
6377 return this;
6378 }
6379
Chris Wren3745a3d2012-05-22 15:11:52 -04006380 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04006381 * Override the large icon when the big notification is shown.
6382 */
6383 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04006384 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
6385 }
6386
6387 /**
6388 * Override the large icon when the big notification is shown.
6389 */
6390 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04006391 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006392 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006393 return this;
6394 }
6395
Riley Andrews0394a0c2015-11-03 23:36:52 -08006396 /** @hide */
6397 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
6398
Daniel Sandler0ec46202015-06-24 01:27:05 -04006399 /**
6400 * @hide
6401 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006402 @Override
6403 public void purgeResources() {
6404 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08006405 if (mPicture != null &&
6406 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08006407 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006408 mPicture = mPicture.createAshmemBitmap();
6409 }
6410 if (mBigLargeIcon != null) {
6411 mBigLargeIcon.convertToAshmem();
6412 }
6413 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01006414
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006415 /**
6416 * @hide
6417 */
Selim Cinekd0426622017-07-11 13:19:59 +02006418 @Override
6419 public void reduceImageSizes(Context context) {
6420 super.reduceImageSizes(context);
6421 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006422 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006423 if (mPicture != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006424 int maxPictureWidth = resources.getDimensionPixelSize(isLowRam
6425 ? R.dimen.notification_big_picture_max_height_low_ram
6426 : R.dimen.notification_big_picture_max_height);
6427 int maxPictureHeight = resources.getDimensionPixelSize(isLowRam
6428 ? R.dimen.notification_big_picture_max_width_low_ram
6429 : R.dimen.notification_big_picture_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006430 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
6431 }
6432 if (mBigLargeIcon != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006433 int rightIconSize = resources.getDimensionPixelSize(isLowRam
6434 ? R.dimen.notification_right_icon_size_low_ram
6435 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006436 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
6437 }
6438 }
6439
6440 /**
6441 * @hide
6442 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006443 public RemoteViews makeBigContentView() {
6444 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01006445 // This covers the following cases:
6446 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006447 // mN.mLargeIcon
6448 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04006449 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07006450 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006451 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006452 oldLargeIcon = mBuilder.mN.mLargeIcon;
6453 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006454 // The legacy largeIcon might not allow us to clear the image, as it's taken in
6455 // replacement if the other one is null. Because we're restoring these legacy icons
6456 // for old listeners, this is in general non-null.
6457 largeIconLegacy = mBuilder.mN.largeIcon;
6458 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006459 }
6460
Selim Cinek384804b2018-04-18 14:31:07 +08006461 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource(),
6462 null /* result */);
Selim Cinek03d0d652015-11-13 13:18:09 -05006463 if (mSummaryTextSet) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006464 contentView.setTextViewText(R.id.text, mBuilder.processTextSpans(
6465 mBuilder.processLegacyText(mSummaryText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006466 mBuilder.setTextViewColorSecondary(contentView, R.id.text);
Selim Cinekc848c3a2016-01-13 15:27:30 -08006467 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05006468 }
Selim Cinek279fa862016-06-14 10:57:25 -07006469 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08006470
Christoph Studer5c510ee2014-12-15 16:32:27 +01006471 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006472 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006473 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006474 }
6475
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006476 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006477 return contentView;
6478 }
6479
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006480 /**
6481 * @hide
6482 */
6483 public void addExtras(Bundle extras) {
6484 super.addExtras(extras);
6485
6486 if (mBigLargeIconSet) {
6487 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
6488 }
6489 extras.putParcelable(EXTRA_PICTURE, mPicture);
6490 }
6491
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006492 /**
6493 * @hide
6494 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006495 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02006496 protected void restoreFromExtras(Bundle extras) {
6497 super.restoreFromExtras(extras);
6498
6499 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01006500 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02006501 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04006502 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006503 mPicture = extras.getParcelable(EXTRA_PICTURE);
6504 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006505
6506 /**
6507 * @hide
6508 */
6509 @Override
6510 public boolean hasSummaryInHeader() {
6511 return false;
6512 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006513
6514 /**
6515 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006516 * Note that we aren't actually comparing the contents of the bitmaps here, so this
6517 * is only doing a cursory inspection. Bitmaps of equal size will appear the same.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006518 */
6519 @Override
6520 public boolean areNotificationsVisiblyDifferent(Style other) {
6521 if (other == null || getClass() != other.getClass()) {
6522 return true;
6523 }
6524 BigPictureStyle otherS = (BigPictureStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006525 return areBitmapsObviouslyDifferent(getBigPicture(), otherS.getBigPicture());
6526 }
6527
6528 private static boolean areBitmapsObviouslyDifferent(Bitmap a, Bitmap b) {
6529 if (a == b) {
6530 return false;
6531 }
6532 if (a == null || b == null) {
6533 return true;
6534 }
6535 return a.getWidth() != b.getWidth()
6536 || a.getHeight() != b.getHeight()
6537 || a.getConfig() != b.getConfig()
6538 || a.getGenerationId() != b.getGenerationId();
Julia Reynolds7217dc92018-03-07 12:12:09 -05006539 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006540 }
6541
6542 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006543 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08006544 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006545 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006546 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006547 * Notification notif = new Notification.Builder(mContext)
6548 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
6549 * .setContentText(subject)
6550 * .setSmallIcon(R.drawable.new_mail)
6551 * .setLargeIcon(aBitmap)
6552 * .setStyle(new Notification.BigTextStyle()
6553 * .bigText(aVeryLongString))
6554 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006555 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006556 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006557 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006558 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006559 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006560
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006561 private CharSequence mBigText;
6562
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006563 public BigTextStyle() {
6564 }
6565
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006566 /**
6567 * @deprecated use {@code BigTextStyle()}.
6568 */
6569 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006570 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006571 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006572 }
6573
Chris Wrend6297db2012-05-03 16:20:13 -04006574 /**
6575 * Overrides ContentTitle in the big form of the template.
6576 * This defaults to the value passed to setContentTitle().
6577 */
6578 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006579 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006580 return this;
6581 }
6582
6583 /**
6584 * Set the first line of text after the detail section in the big form of the template.
6585 */
6586 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006587 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006588 return this;
6589 }
6590
Chris Wren0bd664d2012-08-01 13:56:56 -04006591 /**
6592 * Provide the longer text to be displayed in the big form of the
6593 * template in place of the content text.
6594 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006595 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006596 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006597 return this;
6598 }
6599
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006600 /**
6601 * @hide
6602 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05006603 public CharSequence getBigText() {
6604 return mBigText;
6605 }
6606
6607 /**
6608 * @hide
6609 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006610 public void addExtras(Bundle extras) {
6611 super.addExtras(extras);
6612
Christoph Studer4600f9b2014-07-22 22:44:43 +02006613 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
6614 }
6615
6616 /**
6617 * @hide
6618 */
6619 @Override
6620 protected void restoreFromExtras(Bundle extras) {
6621 super.restoreFromExtras(extras);
6622
6623 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006624 }
6625
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006626 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006627 * @param increasedHeight true if this layout be created with an increased height.
6628 *
6629 * @hide
6630 */
6631 @Override
6632 public RemoteViews makeContentView(boolean increasedHeight) {
6633 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006634 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006635 mBuilder.mActions = new ArrayList<>();
6636 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006637 mBuilder.mActions = mBuilder.mOriginalActions;
6638 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006639 return remoteViews;
6640 }
6641 return super.makeContentView(increasedHeight);
6642 }
6643
6644 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006645 * @hide
6646 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006647 @Override
6648 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
6649 if (increasedHeight && mBuilder.mActions.size() > 0) {
6650 return makeBigContentView();
6651 }
6652 return super.makeHeadsUpContentView(increasedHeight);
6653 }
6654
6655 /**
6656 * @hide
6657 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006658 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02006659
6660 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07006661 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006662 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04006663
Selim Cinek384804b2018-04-18 14:31:07 +08006664 TemplateBindResult result = new TemplateBindResult();
6665 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource(), result);
Selim Cinek1c72fa02018-04-23 18:00:54 +08006666 contentView.setInt(R.id.big_text, "setImageEndMargin", result.getIconMarginEnd());
Joe Malin8d40d042012-11-05 11:36:40 -08006667
Selim Cinek75998782016-04-26 10:39:17 -07006668 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006669
Selim Cinek3a2c4b92015-12-17 17:01:17 -08006670 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07006671 if (TextUtils.isEmpty(bigTextText)) {
6672 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
6673 // experience
6674 bigTextText = mBuilder.processLegacyText(text);
6675 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07006676 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08006677
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006678 return contentView;
6679 }
6680
Julia Reynolds7217dc92018-03-07 12:12:09 -05006681 /**
6682 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006683 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006684 */
6685 @Override
6686 public boolean areNotificationsVisiblyDifferent(Style other) {
6687 if (other == null || getClass() != other.getClass()) {
6688 return true;
6689 }
6690 BigTextStyle newS = (BigTextStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006691 return !Objects.equals(String.valueOf(getBigText()), String.valueOf(newS.getBigText()));
Julia Reynolds7217dc92018-03-07 12:12:09 -05006692 }
6693
Adrian Roosb1f427c2016-05-26 12:27:15 -07006694 static void applyBigTextContentView(Builder builder,
6695 RemoteViews contentView, CharSequence bigTextText) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006696 contentView.setTextViewText(R.id.big_text, builder.processTextSpans(bigTextText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006697 builder.setTextViewColorSecondary(contentView, R.id.big_text);
Adrian Roosb1f427c2016-05-26 12:27:15 -07006698 contentView.setViewVisibility(R.id.big_text,
6699 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek279fa862016-06-14 10:57:25 -07006700 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07006701 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006702 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006703
6704 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006705 * Helper class for generating large-format notifications that include multiple back-and-forth
6706 * messages of varying types between any number of people.
6707 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006708 * <p>
Alex Hillsfc737de2016-03-23 17:33:02 -04006709 * If the platform does not provide large-format notifications, this method has no effect. The
6710 * user will always see the normal notification view.
Selim Cinekce8794f2018-05-23 16:46:05 -07006711 *
6712 * <p>
6713 * If the app is targeting Android P and above, it is required to use the {@link Person}
6714 * class in order to get an optimal rendering of the notification and its avatars. For
6715 * conversations involving multiple people, the app should also make sure that it marks the
6716 * conversation as a group with {@link #setGroupConversation(boolean)}.
6717 *
6718 * <p>
6719 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior.
6720 * Here's an example of how this may be used:
Alex Hillsfc737de2016-03-23 17:33:02 -04006721 * <pre class="prettyprint">
6722 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006723 * Person user = new Person.Builder().setIcon(userIcon).setName(userName).build();
6724 * MessagingStyle style = new MessagingStyle(user)
6725 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getPerson())
6726 * .addMessage(messages[2].getText(), messages[2].getTime(), messages[2].getPerson())
6727 * .setGroupConversation(hasMultiplePeople());
6728 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006729 * Notification noti = new Notification.Builder()
Selim Cinekce8794f2018-05-23 16:46:05 -07006730 * .setContentTitle(&quot;2 new messages with &quot; + sender.toString())
Alex Hillsfc737de2016-03-23 17:33:02 -04006731 * .setContentText(subject)
6732 * .setSmallIcon(R.drawable.new_message)
6733 * .setLargeIcon(aBitmap)
Selim Cinekce8794f2018-05-23 16:46:05 -07006734 * .setStyle(style)
Alex Hillsfc737de2016-03-23 17:33:02 -04006735 * .build();
6736 * </pre>
6737 */
6738 public static class MessagingStyle extends Style {
6739
6740 /**
6741 * The maximum number of messages that will be retained in the Notification itself (the
6742 * number displayed is up to the platform).
6743 */
6744 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
6745
Selim Cinekcb8b9852017-12-15 18:01:52 -08006746 @NonNull Person mUser;
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006747 @Nullable CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04006748 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08006749 List<Message> mHistoricMessages = new ArrayList<>();
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006750 boolean mIsGroupConversation;
Alex Hillsfc737de2016-03-23 17:33:02 -04006751
6752 MessagingStyle() {
6753 }
6754
6755 /**
Alex Hillsfd590442016-10-07 09:52:44 -04006756 * @param userDisplayName Required - the name to be displayed for any replies sent by the
6757 * user before the posting app reposts the notification with those messages after they've
6758 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04006759 * {@link #addMessage(Notification.MessagingStyle.Message)}
Selim Cinekcb8b9852017-12-15 18:01:52 -08006760 *
6761 * @deprecated use {@code MessagingStyle(Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006762 */
Alex Hillsfd590442016-10-07 09:52:44 -04006763 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Selim Cinek9acd6732018-03-23 16:39:02 -07006764 this(new Person.Builder().setName(userDisplayName).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006765 }
6766
6767 /**
6768 * @param user Required - The person displayed for any messages that are sent by the
6769 * user. Any messages added with {@link #addMessage(Notification.MessagingStyle.Message)}
6770 * who don't have a Person associated with it will be displayed as if they were sent
Selim Cinek90343862018-02-01 11:07:11 -08006771 * by this user. The user also needs to have a valid name associated with it, which will
6772 * be enforced starting in Android P.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006773 */
6774 public MessagingStyle(@NonNull Person user) {
6775 mUser = user;
Selim Cinek90343862018-02-01 11:07:11 -08006776 }
6777
6778 /**
6779 * Validate that this style was properly composed. This is called at build time.
6780 * @hide
6781 */
6782 @Override
6783 public void validate(Context context) {
6784 super.validate(context);
6785 if (context.getApplicationInfo().targetSdkVersion
6786 >= Build.VERSION_CODES.P && (mUser == null || mUser.getName() == null)) {
6787 throw new RuntimeException("User must be valid and have a name.");
Selim Cinekcb8b9852017-12-15 18:01:52 -08006788 }
6789 }
6790
6791 /**
koprivaa1a78482018-10-09 10:09:23 -07006792 * @return the text that should be displayed in the statusBar when heads upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006793 * If {@code null} is returned, the default implementation will be used.
6794 *
6795 * @hide
6796 */
6797 @Override
6798 public CharSequence getHeadsUpStatusBarText() {
6799 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
6800 ? super.mBigContentTitle
6801 : mConversationTitle;
6802 if (!TextUtils.isEmpty(conversationTitle) && !hasOnlyWhiteSpaceSenders()) {
6803 return conversationTitle;
6804 }
6805 return null;
6806 }
6807
6808 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08006809 * @return the user to be displayed for any replies sent by the user
6810 */
Selim Cinekeb53c222018-04-06 12:56:09 -07006811 @NonNull
Selim Cinekcb8b9852017-12-15 18:01:52 -08006812 public Person getUser() {
6813 return mUser;
Alex Hillsfc737de2016-03-23 17:33:02 -04006814 }
6815
6816 /**
6817 * Returns the name to be displayed for any replies sent by the user
Selim Cinekcb8b9852017-12-15 18:01:52 -08006818 *
6819 * @deprecated use {@link #getUser()} instead
Alex Hillsfc737de2016-03-23 17:33:02 -04006820 */
6821 public CharSequence getUserDisplayName() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006822 return mUser.getName();
Alex Hillsfc737de2016-03-23 17:33:02 -04006823 }
6824
6825 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006826 * Sets the title to be displayed on this conversation. May be set to {@code null}.
6827 *
Kodlee Yin14656422017-12-22 17:00:46 -08006828 * <p>This API's behavior was changed in SDK version {@link Build.VERSION_CODES#P}. If your
6829 * application's target version is less than {@link Build.VERSION_CODES#P}, setting a
6830 * conversation title to a non-null value will make {@link #isGroupConversation()} return
6831 * {@code true} and passing {@code null} will make it return {@code false}. In
6832 * {@link Build.VERSION_CODES#P} and beyond, use {@link #setGroupConversation(boolean)}
6833 * to set group conversation status.
6834 *
6835 * @param conversationTitle Title displayed for this conversation
6836 * @return this object for method chaining
Alex Hillsfc737de2016-03-23 17:33:02 -04006837 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006838 public MessagingStyle setConversationTitle(@Nullable CharSequence conversationTitle) {
Alex Hillsfc737de2016-03-23 17:33:02 -04006839 mConversationTitle = conversationTitle;
6840 return this;
6841 }
6842
6843 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006844 * Return the title to be displayed on this conversation. May return {@code null}.
Alex Hillsfc737de2016-03-23 17:33:02 -04006845 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006846 @Nullable
Alex Hillsfc737de2016-03-23 17:33:02 -04006847 public CharSequence getConversationTitle() {
6848 return mConversationTitle;
6849 }
6850
6851 /**
6852 * Adds a message for display by this notification. Convenience call for a simple
6853 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6854 * @param text A {@link CharSequence} to be displayed as the message content
6855 * @param timestamp Time at which the message arrived
6856 * @param sender A {@link CharSequence} to be used for displaying the name of the
6857 * sender. Should be <code>null</code> for messages by the current user, in which case
6858 * the platform will insert {@link #getUserDisplayName()}.
6859 * Should be unique amongst all individuals in the conversation, and should be
6860 * consistent during re-posts of the notification.
6861 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07006862 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Alex Hillsfc737de2016-03-23 17:33:02 -04006863 *
6864 * @return this object for method chaining
Selim Cinekcb8b9852017-12-15 18:01:52 -08006865 *
6866 * @deprecated use {@link #addMessage(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006867 */
6868 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006869 return addMessage(text, timestamp,
Selim Cinek9acd6732018-03-23 16:39:02 -07006870 sender == null ? null : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006871 }
6872
6873 /**
6874 * Adds a message for display by this notification. Convenience call for a simple
6875 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6876 * @param text A {@link CharSequence} to be displayed as the message content
6877 * @param timestamp Time at which the message arrived
6878 * @param sender The {@link Person} who sent the message.
6879 * Should be <code>null</code> for messages by the current user, in which case
6880 * the platform will insert the user set in {@code MessagingStyle(Person)}.
6881 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07006882 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Selim Cinekcb8b9852017-12-15 18:01:52 -08006883 *
6884 * @return this object for method chaining
6885 */
Selim Cinekeb53c222018-04-06 12:56:09 -07006886 public MessagingStyle addMessage(@NonNull CharSequence text, long timestamp,
6887 @Nullable Person sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08006888 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04006889 }
6890
6891 /**
6892 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08006893 *
6894 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6895 * the newest last.
6896 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006897 * @param message The {@link Message} to be displayed
6898 * @return this object for method chaining
6899 */
6900 public MessagingStyle addMessage(Message message) {
6901 mMessages.add(message);
6902 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6903 mMessages.remove(0);
6904 }
6905 return this;
6906 }
6907
6908 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006909 * Adds a {@link Message} for historic context in this notification.
6910 *
6911 * <p>Messages should be added as historic if they are not the main subject of the
6912 * notification but may give context to a conversation. The system may choose to present
6913 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
6914 *
6915 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6916 * the newest last.
6917 *
6918 * @param message The historic {@link Message} to be added
6919 * @return this object for method chaining
6920 */
6921 public MessagingStyle addHistoricMessage(Message message) {
6922 mHistoricMessages.add(message);
6923 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6924 mHistoricMessages.remove(0);
6925 }
6926 return this;
6927 }
6928
6929 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006930 * Gets the list of {@code Message} objects that represent the notification
6931 */
6932 public List<Message> getMessages() {
6933 return mMessages;
6934 }
6935
6936 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006937 * Gets the list of historic {@code Message}s in the notification.
6938 */
6939 public List<Message> getHistoricMessages() {
6940 return mHistoricMessages;
6941 }
6942
6943 /**
Selim Cinekce8794f2018-05-23 16:46:05 -07006944 * Sets whether this conversation notification represents a group. If the app is targeting
6945 * Android P, this is required if the app wants to display the largeIcon set with
6946 * {@link Notification.Builder#setLargeIcon(Bitmap)}, otherwise it will be hidden.
Kodlee Yin14656422017-12-22 17:00:46 -08006947 *
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006948 * @param isGroupConversation {@code true} if the conversation represents a group,
6949 * {@code false} otherwise.
6950 * @return this object for method chaining
6951 */
6952 public MessagingStyle setGroupConversation(boolean isGroupConversation) {
6953 mIsGroupConversation = isGroupConversation;
6954 return this;
6955 }
6956
6957 /**
Kodlee Yin14656422017-12-22 17:00:46 -08006958 * Returns {@code true} if this notification represents a group conversation, otherwise
6959 * {@code false}.
6960 *
6961 * <p> If the application that generated this {@link MessagingStyle} targets an SDK version
6962 * less than {@link Build.VERSION_CODES#P}, this method becomes dependent on whether or
6963 * not the conversation title is set; returning {@code true} if the conversation title is
6964 * a non-null value, or {@code false} otherwise. From {@link Build.VERSION_CODES#P} forward,
6965 * this method returns what's set by {@link #setGroupConversation(boolean)} allowing for
6966 * named, non-group conversations.
6967 *
6968 * @see #setConversationTitle(CharSequence)
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006969 */
6970 public boolean isGroupConversation() {
Kodlee Yin14656422017-12-22 17:00:46 -08006971 // When target SDK version is < P, a non-null conversation title dictates if this is
6972 // as group conversation.
6973 if (mBuilder != null
6974 && mBuilder.mContext.getApplicationInfo().targetSdkVersion
6975 < Build.VERSION_CODES.P) {
6976 return mConversationTitle != null;
6977 }
6978
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006979 return mIsGroupConversation;
6980 }
6981
6982 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006983 * @hide
6984 */
6985 @Override
6986 public void addExtras(Bundle extras) {
6987 super.addExtras(extras);
Selim Cinekcb8b9852017-12-15 18:01:52 -08006988 if (mUser != null) {
6989 // For legacy usages
6990 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUser.getName());
6991 extras.putParcelable(EXTRA_MESSAGING_PERSON, mUser);
Alex Hillsfc737de2016-03-23 17:33:02 -04006992 }
6993 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006994 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04006995 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006996 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
6997 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04006998 }
Adrian Roos437cd562017-01-18 15:47:03 -08006999 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
7000 Message.getBundleArrayForMessages(mHistoricMessages));
7001 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007002
7003 fixTitleAndTextExtras(extras);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007004 extras.putBoolean(EXTRA_IS_GROUP_CONVERSATION, mIsGroupConversation);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007005 }
7006
7007 private void fixTitleAndTextExtras(Bundle extras) {
7008 Message m = findLatestIncomingMessage();
7009 CharSequence text = (m == null) ? null : m.mText;
7010 CharSequence sender = m == null ? null
Selim Cinekcb8b9852017-12-15 18:01:52 -08007011 : m.mSender == null || TextUtils.isEmpty(m.mSender.getName())
7012 ? mUser.getName() : m.mSender.getName();
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007013 CharSequence title;
7014 if (!TextUtils.isEmpty(mConversationTitle)) {
7015 if (!TextUtils.isEmpty(sender)) {
7016 BidiFormatter bidi = BidiFormatter.getInstance();
7017 title = mBuilder.mContext.getString(
7018 com.android.internal.R.string.notification_messaging_title_template,
Selim Cinekcb8b9852017-12-15 18:01:52 -08007019 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(sender));
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007020 } else {
7021 title = mConversationTitle;
7022 }
7023 } else {
7024 title = sender;
7025 }
7026
7027 if (title != null) {
7028 extras.putCharSequence(EXTRA_TITLE, title);
7029 }
7030 if (text != null) {
7031 extras.putCharSequence(EXTRA_TEXT, text);
7032 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007033 }
7034
7035 /**
7036 * @hide
7037 */
7038 @Override
7039 protected void restoreFromExtras(Bundle extras) {
7040 super.restoreFromExtras(extras);
7041
Selim Cinekcb8b9852017-12-15 18:01:52 -08007042 mUser = extras.getParcelable(EXTRA_MESSAGING_PERSON);
7043 if (mUser == null) {
7044 CharSequence displayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
Selim Cinek9acd6732018-03-23 16:39:02 -07007045 mUser = new Person.Builder().setName(displayName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007046 }
Adrian Roos96b7e202016-05-17 13:50:38 -07007047 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08007048 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007049 mMessages = Message.getMessagesFromBundleArray(messages);
Adrian Roos437cd562017-01-18 15:47:03 -08007050 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007051 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007052 mIsGroupConversation = extras.getBoolean(EXTRA_IS_GROUP_CONVERSATION);
Alex Hillsfc737de2016-03-23 17:33:02 -04007053 }
7054
7055 /**
7056 * @hide
7057 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07007058 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007059 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07007060 mBuilder.mOriginalActions = mBuilder.mActions;
7061 mBuilder.mActions = new ArrayList<>();
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007062 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007063 false /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007064 mBuilder.mActions = mBuilder.mOriginalActions;
7065 mBuilder.mOriginalActions = null;
7066 return remoteViews;
Adrian Roosc1a80b02016-04-05 14:54:55 -07007067 }
7068
Julia Reynolds7217dc92018-03-07 12:12:09 -05007069 /**
7070 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04007071 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05007072 */
7073 @Override
7074 public boolean areNotificationsVisiblyDifferent(Style other) {
7075 if (other == null || getClass() != other.getClass()) {
7076 return true;
7077 }
7078 MessagingStyle newS = (MessagingStyle) other;
7079 List<MessagingStyle.Message> oldMs = getMessages();
7080 List<MessagingStyle.Message> newMs = newS.getMessages();
7081
Dan Sandler7d67bd42018-05-15 14:06:38 -04007082 if (oldMs == null || newMs == null) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007083 newMs = new ArrayList<>();
7084 }
7085
7086 int n = oldMs.size();
7087 if (n != newMs.size()) {
7088 return true;
7089 }
7090 for (int i = 0; i < n; i++) {
7091 MessagingStyle.Message oldM = oldMs.get(i);
7092 MessagingStyle.Message newM = newMs.get(i);
Dan Sandler7d67bd42018-05-15 14:06:38 -04007093 if (!Objects.equals(
7094 String.valueOf(oldM.getText()),
7095 String.valueOf(newM.getText()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007096 return true;
7097 }
7098 if (!Objects.equals(oldM.getDataUri(), newM.getDataUri())) {
7099 return true;
7100 }
Dan Sandler7d67bd42018-05-15 14:06:38 -04007101 String oldSender = String.valueOf(oldM.getSenderPerson() == null
7102 ? oldM.getSender()
7103 : oldM.getSenderPerson().getName());
7104 String newSender = String.valueOf(newM.getSenderPerson() == null
7105 ? newM.getSender()
7106 : newM.getSenderPerson().getName());
Julia Reynolds7217dc92018-03-07 12:12:09 -05007107 if (!Objects.equals(oldSender, newSender)) {
7108 return true;
7109 }
7110
7111 String oldKey = oldM.getSenderPerson() == null
7112 ? null : oldM.getSenderPerson().getKey();
7113 String newKey = newM.getSenderPerson() == null
7114 ? null : newM.getSenderPerson().getKey();
7115 if (!Objects.equals(oldKey, newKey)) {
7116 return true;
7117 }
7118 // Other fields (like timestamp) intentionally excluded
7119 }
7120 return false;
7121 }
7122
Adrian Roosc1a80b02016-04-05 14:54:55 -07007123 private Message findLatestIncomingMessage() {
Selim Cinek88188f22017-09-19 16:46:56 -07007124 return findLatestIncomingMessage(mMessages);
7125 }
7126
7127 /**
7128 * @hide
7129 */
7130 @Nullable
7131 public static Message findLatestIncomingMessage(
7132 List<Message> messages) {
7133 for (int i = messages.size() - 1; i >= 0; i--) {
7134 Message m = messages.get(i);
Adrian Roosc1a80b02016-04-05 14:54:55 -07007135 // Incoming messages have a non-empty sender.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007136 if (m.mSender != null && !TextUtils.isEmpty(m.mSender.getName())) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07007137 return m;
7138 }
7139 }
Selim Cinek88188f22017-09-19 16:46:56 -07007140 if (!messages.isEmpty()) {
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007141 // No incoming messages, fall back to outgoing message
Selim Cinek88188f22017-09-19 16:46:56 -07007142 return messages.get(messages.size() - 1);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007143 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07007144 return null;
7145 }
7146
7147 /**
7148 * @hide
7149 */
7150 @Override
7151 public RemoteViews makeBigContentView() {
Selim Cinek384804b2018-04-18 14:31:07 +08007152 return makeMessagingView(false /* displayImagesAtEnd */, true /* hideLargeIcon */);
Selim Cinekafeed292017-12-12 17:32:44 -08007153 }
7154
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007155 /**
7156 * Create a messaging layout.
7157 *
7158 * @param displayImagesAtEnd should images be displayed at the end of the content instead
7159 * of inline.
Selim Cinek384804b2018-04-18 14:31:07 +08007160 * @param hideRightIcons Should the reply affordance be shown at the end of the notification
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007161 * @return the created remoteView.
7162 */
Selim Cinekafeed292017-12-12 17:32:44 -08007163 @NonNull
Selim Cinek384804b2018-04-18 14:31:07 +08007164 private RemoteViews makeMessagingView(boolean displayImagesAtEnd, boolean hideRightIcons) {
Selim Cinek88188f22017-09-19 16:46:56 -07007165 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
Adrian Roosc1a80b02016-04-05 14:54:55 -07007166 ? super.mBigContentTitle
7167 : mConversationTitle;
Selim Cinekce8794f2018-05-23 16:46:05 -07007168 boolean atLeastP = mBuilder.mContext.getApplicationInfo().targetSdkVersion
7169 >= Build.VERSION_CODES.P;
7170 boolean isOneToOne;
Selim Cinek2dd3e722018-01-19 11:06:06 -08007171 CharSequence nameReplacement = null;
Selim Cinekce8794f2018-05-23 16:46:05 -07007172 Icon avatarReplacement = null;
7173 if (!atLeastP) {
7174 isOneToOne = TextUtils.isEmpty(conversationTitle);
7175 avatarReplacement = mBuilder.mN.mLargeIcon;
7176 if (hasOnlyWhiteSpaceSenders()) {
7177 isOneToOne = true;
7178 nameReplacement = conversationTitle;
7179 conversationTitle = null;
7180 }
7181 } else {
7182 isOneToOne = !isGroupConversation();
Adrian Roosb1f427c2016-05-26 12:27:15 -07007183 }
Selim Cinek384804b2018-04-18 14:31:07 +08007184 TemplateBindResult bindResult = new TemplateBindResult();
Adrian Roos48d746a2016-04-12 14:57:28 -07007185 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07007186 mBuilder.getMessagingLayoutResource(),
Selim Cinek88188f22017-09-19 16:46:56 -07007187 mBuilder.mParams.reset().hasProgress(false).title(conversationTitle).text(null)
Selim Cinek384804b2018-04-18 14:31:07 +08007188 .hideLargeIcon(hideRightIcons || isOneToOne)
7189 .hideReplyIcon(hideRightIcons)
7190 .headerTextSecondary(conversationTitle),
7191 bindResult);
Selim Cinek88188f22017-09-19 16:46:56 -07007192 addExtras(mBuilder.mN.extras);
Selim Cinekafeed292017-12-12 17:32:44 -08007193 // also update the end margin if there is an image
Selim Cinek1c72fa02018-04-23 18:00:54 +08007194 contentView.setViewLayoutMarginEnd(R.id.notification_messaging,
7195 bindResult.getIconMarginEnd());
Selim Cinek88188f22017-09-19 16:46:56 -07007196 contentView.setInt(R.id.status_bar_latest_event_content, "setLayoutColor",
Kenny Guy14d035c2018-05-02 19:10:36 +01007197 mBuilder.isColorized() ? mBuilder.getPrimaryTextColor()
7198 : mBuilder.resolveContrastColor());
7199 contentView.setInt(R.id.status_bar_latest_event_content, "setSenderTextColor",
7200 mBuilder.getPrimaryTextColor());
7201 contentView.setInt(R.id.status_bar_latest_event_content, "setMessageTextColor",
7202 mBuilder.getSecondaryTextColor());
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007203 contentView.setBoolean(R.id.status_bar_latest_event_content, "setDisplayImagesAtEnd",
7204 displayImagesAtEnd);
Selim Cinekce8794f2018-05-23 16:46:05 -07007205 contentView.setIcon(R.id.status_bar_latest_event_content, "setAvatarReplacement",
7206 avatarReplacement);
Selim Cinek2dd3e722018-01-19 11:06:06 -08007207 contentView.setCharSequence(R.id.status_bar_latest_event_content, "setNameReplacement",
7208 nameReplacement);
Selim Cinek88188f22017-09-19 16:46:56 -07007209 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsOneToOne",
7210 isOneToOne);
7211 contentView.setBundle(R.id.status_bar_latest_event_content, "setData",
7212 mBuilder.mN.extras);
Alex Hillsfc737de2016-03-23 17:33:02 -04007213 return contentView;
7214 }
7215
Selim Cinekf7409db2017-10-24 16:17:14 -07007216 private boolean hasOnlyWhiteSpaceSenders() {
7217 for (int i = 0; i < mMessages.size(); i++) {
7218 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007219 Person sender = m.getSenderPerson();
7220 if (sender != null && !isWhiteSpace(sender.getName())) {
Selim Cinekf7409db2017-10-24 16:17:14 -07007221 return false;
7222 }
7223 }
7224 return true;
7225 }
7226
7227 private boolean isWhiteSpace(CharSequence sender) {
7228 if (TextUtils.isEmpty(sender)) {
7229 return true;
7230 }
7231 if (sender.toString().matches("^\\s*$")) {
7232 return true;
7233 }
7234 // Let's check if we only have 0 whitespace chars. Some apps did this as a workaround
7235 // For the presentation that we had.
7236 for (int i = 0; i < sender.length(); i++) {
7237 char c = sender.charAt(i);
7238 if (c != '\u200B') {
7239 return false;
7240 }
7241 }
7242 return true;
7243 }
7244
Selim Cinek88188f22017-09-19 16:46:56 -07007245 private CharSequence createConversationTitleFromMessages() {
7246 ArraySet<CharSequence> names = new ArraySet<>();
7247 for (int i = 0; i < mMessages.size(); i++) {
7248 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007249 Person sender = m.getSenderPerson();
Selim Cinek88188f22017-09-19 16:46:56 -07007250 if (sender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007251 names.add(sender.getName());
Selim Cinek88188f22017-09-19 16:46:56 -07007252 }
7253 }
7254 SpannableStringBuilder title = new SpannableStringBuilder();
7255 int size = names.size();
7256 for (int i = 0; i < size; i++) {
7257 CharSequence name = names.valueAt(i);
7258 if (!TextUtils.isEmpty(title)) {
7259 title.append(", ");
7260 }
7261 title.append(BidiFormatter.getInstance().unicodeWrap(name));
7262 }
7263 return title;
7264 }
7265
Adrian Roosdedd1df2016-04-26 16:38:47 -07007266 /**
7267 * @hide
7268 */
7269 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007270 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007271 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007272 true /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007273 remoteViews.setInt(R.id.notification_messaging, "setMaxDisplayedLines", 1);
7274 return remoteViews;
Adrian Roosdedd1df2016-04-26 16:38:47 -07007275 }
7276
Adrian Roosc1a80b02016-04-05 14:54:55 -07007277 private static TextAppearanceSpan makeFontColorSpan(int color) {
7278 return new TextAppearanceSpan(null, 0, 0,
7279 ColorStateList.valueOf(color), null);
7280 }
7281
Alex Hillsd9b04d92016-04-11 16:38:16 -04007282 public static final class Message {
Tony Mak09db2ea2018-06-27 18:12:48 +01007283 /** @hide */
7284 public static final String KEY_TEXT = "text";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007285 static final String KEY_TIMESTAMP = "time";
7286 static final String KEY_SENDER = "sender";
Selim Cinekcb8b9852017-12-15 18:01:52 -08007287 static final String KEY_SENDER_PERSON = "sender_person";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007288 static final String KEY_DATA_MIME_TYPE = "type";
7289 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08007290 static final String KEY_EXTRAS_BUNDLE = "extras";
Kenny Guya0f6de82018-04-06 16:20:16 +01007291 static final String KEY_REMOTE_INPUT_HISTORY = "remote_input_history";
Alex Hillsfc737de2016-03-23 17:33:02 -04007292
7293 private final CharSequence mText;
7294 private final long mTimestamp;
Selim Cinekcb8b9852017-12-15 18:01:52 -08007295 @Nullable
7296 private final Person mSender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007297 /** True if this message was generated from the extra
7298 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}
7299 */
7300 private final boolean mRemoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007301
Shane Brennan5a871862017-03-11 13:14:17 -08007302 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007303 private String mDataMimeType;
7304 private Uri mDataUri;
7305
7306 /**
7307 * Constructor
7308 * @param text A {@link CharSequence} to be displayed as the message content
7309 * @param timestamp Time at which the message arrived
7310 * @param sender A {@link CharSequence} to be used for displaying the name of the
7311 * sender. Should be <code>null</code> for messages by the current user, in which case
7312 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
7313 * Should be unique amongst all individuals in the conversation, and should be
7314 * consistent during re-posts of the notification.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007315 *
7316 * @deprecated use {@code Message(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04007317 */
7318 public Message(CharSequence text, long timestamp, CharSequence sender){
Selim Cinek9acd6732018-03-23 16:39:02 -07007319 this(text, timestamp, sender == null ? null
7320 : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08007321 }
7322
7323 /**
7324 * Constructor
7325 * @param text A {@link CharSequence} to be displayed as the message content
7326 * @param timestamp Time at which the message arrived
7327 * @param sender The {@link Person} who sent the message.
7328 * Should be <code>null</code> for messages by the current user, in which case
7329 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7330 * <p>
Selim Cinek9acd6732018-03-23 16:39:02 -07007331 * The person provided should contain an Icon, set with
7332 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7333 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7334 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7335 * to differentiate between the different users.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007336 * </p>
7337 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007338 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender) {
Kenny Guya0f6de82018-04-06 16:20:16 +01007339 this(text, timestamp, sender, false /* remoteHistory */);
7340 }
7341
7342 /**
7343 * Constructor
7344 * @param text A {@link CharSequence} to be displayed as the message content
7345 * @param timestamp Time at which the message arrived
7346 * @param sender The {@link Person} who sent the message.
7347 * Should be <code>null</code> for messages by the current user, in which case
7348 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7349 * @param remoteInputHistory True if the messages was generated from the extra
7350 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7351 * <p>
7352 * The person provided should contain an Icon, set with
7353 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7354 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7355 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7356 * to differentiate between the different users.
7357 * </p>
7358 * @hide
7359 */
7360 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender,
7361 boolean remoteInputHistory) {
Alex Hillsfc737de2016-03-23 17:33:02 -04007362 mText = text;
7363 mTimestamp = timestamp;
7364 mSender = sender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007365 mRemoteInputHistory = remoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007366 }
7367
7368 /**
7369 * Sets a binary blob of data and an associated MIME type for a message. In the case
7370 * where the platform doesn't support the MIME type, the original text provided in the
7371 * constructor will be used.
7372 * @param dataMimeType The MIME type of the content. See
7373 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
7374 * types on Android and Android Wear.
7375 * @param dataUri The uri containing the content whose type is given by the MIME type.
7376 * <p class="note">
7377 * <ol>
7378 * <li>Notification Listeners including the System UI need permission to access the
7379 * data the Uri points to. The recommended ways to do this are:</li>
7380 * <li>Store the data in your own ContentProvider, making sure that other apps have
7381 * the correct permission to access your provider. The preferred mechanism for
7382 * providing access is to use per-URI permissions which are temporary and only
7383 * grant access to the receiving application. An easy way to create a
7384 * ContentProvider like this is to use the FileProvider helper class.</li>
7385 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
7386 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
7387 * also store non-media types (see MediaStore.Files for more info). Files can be
7388 * inserted into the MediaStore using scanFile() after which a content:// style
7389 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
7390 * Note that once added to the system MediaStore the content is accessible to any
7391 * app on the device.</li>
7392 * </ol>
7393 * @return this object for method chaining
7394 */
7395 public Message setData(String dataMimeType, Uri dataUri) {
7396 mDataMimeType = dataMimeType;
7397 mDataUri = dataUri;
7398 return this;
7399 }
7400
Alex Hillsfc737de2016-03-23 17:33:02 -04007401 /**
7402 * Get the text to be used for this message, or the fallback text if a type and content
7403 * Uri have been set
7404 */
7405 public CharSequence getText() {
7406 return mText;
7407 }
7408
7409 /**
7410 * Get the time at which this message arrived
7411 */
7412 public long getTimestamp() {
7413 return mTimestamp;
7414 }
7415
7416 /**
Shane Brennan5a871862017-03-11 13:14:17 -08007417 * Get the extras Bundle for this message.
7418 */
7419 public Bundle getExtras() {
7420 return mExtras;
7421 }
7422
7423 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007424 * Get the text used to display the contact's name in the messaging experience
Selim Cinekcb8b9852017-12-15 18:01:52 -08007425 *
7426 * @deprecated use {@link #getSenderPerson()}
Alex Hillsfc737de2016-03-23 17:33:02 -04007427 */
7428 public CharSequence getSender() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007429 return mSender == null ? null : mSender.getName();
7430 }
7431
7432 /**
7433 * Get the sender associated with this message.
7434 */
7435 @Nullable
7436 public Person getSenderPerson() {
Alex Hillsfc737de2016-03-23 17:33:02 -04007437 return mSender;
7438 }
7439
7440 /**
7441 * Get the MIME type of the data pointed to by the Uri
7442 */
7443 public String getDataMimeType() {
7444 return mDataMimeType;
7445 }
7446
7447 /**
koprivaa1a78482018-10-09 10:09:23 -07007448 * Get the Uri pointing to the content of the message. Can be null, in which case
Alex Hillsfc737de2016-03-23 17:33:02 -04007449 * {@see #getText()} is used.
7450 */
7451 public Uri getDataUri() {
7452 return mDataUri;
7453 }
7454
Kenny Guya0f6de82018-04-06 16:20:16 +01007455 /**
7456 * @return True if the message was generated from
7457 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7458 * @hide
7459 */
7460 public boolean isRemoteInputHistory() {
7461 return mRemoteInputHistory;
7462 }
7463
Alex Hillsd9b04d92016-04-11 16:38:16 -04007464 private Bundle toBundle() {
7465 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007466 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007467 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04007468 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007469 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04007470 if (mSender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007471 // Legacy listeners need this
7472 bundle.putCharSequence(KEY_SENDER, mSender.getName());
7473 bundle.putParcelable(KEY_SENDER_PERSON, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04007474 }
7475 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007476 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04007477 }
7478 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007479 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04007480 }
Shane Brennan5a871862017-03-11 13:14:17 -08007481 if (mExtras != null) {
7482 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
7483 }
Kenny Guya0f6de82018-04-06 16:20:16 +01007484 if (mRemoteInputHistory) {
7485 bundle.putBoolean(KEY_REMOTE_INPUT_HISTORY, mRemoteInputHistory);
7486 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007487 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04007488 }
7489
Alex Hillsd9b04d92016-04-11 16:38:16 -04007490 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
7491 Bundle[] bundles = new Bundle[messages.size()];
7492 final int N = messages.size();
7493 for (int i = 0; i < N; i++) {
7494 bundles[i] = messages.get(i).toBundle();
7495 }
7496 return bundles;
7497 }
7498
Selim Cinek88188f22017-09-19 16:46:56 -07007499 /**
7500 * @return A list of messages read from the bundles.
7501 *
7502 * @hide
7503 */
7504 public static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
7505 if (bundles == null) {
7506 return new ArrayList<>();
7507 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007508 List<Message> messages = new ArrayList<>(bundles.length);
7509 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07007510 if (bundles[i] instanceof Bundle) {
7511 Message message = getMessageFromBundle((Bundle)bundles[i]);
7512 if (message != null) {
7513 messages.add(message);
7514 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007515 }
7516 }
7517 return messages;
7518 }
7519
Selim Cinekb0dc61b2018-05-22 18:49:36 -07007520 /**
7521 * @return The message that is stored in the bundle or null if the message couldn't be
7522 * resolved.
7523 *
7524 * @hide
7525 */
7526 @Nullable
7527 public static Message getMessageFromBundle(Bundle bundle) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007528 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07007529 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007530 return null;
7531 } else {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007532
7533 Person senderPerson = bundle.getParcelable(KEY_SENDER_PERSON);
7534 if (senderPerson == null) {
7535 // Legacy apps that use compat don't actually provide the sender objects
7536 // We need to fix the compat version to provide people / use
7537 // the native api instead
7538 CharSequence senderName = bundle.getCharSequence(KEY_SENDER);
7539 if (senderName != null) {
Selim Cinek9acd6732018-03-23 16:39:02 -07007540 senderPerson = new Person.Builder().setName(senderName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007541 }
7542 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007543 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
Selim Cinekcb8b9852017-12-15 18:01:52 -08007544 bundle.getLong(KEY_TIMESTAMP),
Kenny Guya0f6de82018-04-06 16:20:16 +01007545 senderPerson,
7546 bundle.getBoolean(KEY_REMOTE_INPUT_HISTORY, false));
Alex Hillsd9b04d92016-04-11 16:38:16 -04007547 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
7548 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007549 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
7550 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04007551 }
Shane Brennan5a871862017-03-11 13:14:17 -08007552 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
7553 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
7554 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007555 return message;
7556 }
7557 } catch (ClassCastException e) {
7558 return null;
7559 }
7560 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007561 }
7562 }
7563
7564 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04007565 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08007566 *
Robert Ly91c5ce32014-06-08 15:37:00 -07007567 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04007568 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07007569 * Notification notif = new Notification.Builder(mContext)
7570 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
7571 * .setContentText(subject)
7572 * .setSmallIcon(R.drawable.new_mail)
7573 * .setLargeIcon(aBitmap)
7574 * .setStyle(new Notification.InboxStyle()
7575 * .addLine(str1)
7576 * .addLine(str2)
7577 * .setContentTitle(&quot;&quot;)
7578 * .setSummaryText(&quot;+3 more&quot;))
7579 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04007580 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08007581 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04007582 * @see Notification#bigContentView
7583 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007584 public static class InboxStyle extends Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07007585
7586 /**
7587 * The number of lines of remote input history allowed until we start reducing lines.
7588 */
7589 private static final int NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION = 1;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007590 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
7591
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007592 public InboxStyle() {
7593 }
7594
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007595 /**
7596 * @deprecated use {@code InboxStyle()}.
7597 */
7598 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04007599 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007600 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04007601 }
7602
Chris Wrend6297db2012-05-03 16:20:13 -04007603 /**
7604 * Overrides ContentTitle in the big form of the template.
7605 * This defaults to the value passed to setContentTitle().
7606 */
7607 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007608 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04007609 return this;
7610 }
7611
7612 /**
7613 * Set the first line of text after the detail section in the big form of the template.
7614 */
7615 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007616 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04007617 return this;
7618 }
7619
Chris Wren0bd664d2012-08-01 13:56:56 -04007620 /**
7621 * Append a line to the digest section of the Inbox notification.
7622 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04007623 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007624 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04007625 return this;
7626 }
7627
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007628 /**
7629 * @hide
7630 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05007631 public ArrayList<CharSequence> getLines() {
7632 return mTexts;
7633 }
7634
7635 /**
7636 * @hide
7637 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007638 public void addExtras(Bundle extras) {
7639 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007640
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007641 CharSequence[] a = new CharSequence[mTexts.size()];
7642 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
7643 }
7644
Christoph Studer4600f9b2014-07-22 22:44:43 +02007645 /**
7646 * @hide
7647 */
7648 @Override
7649 protected void restoreFromExtras(Bundle extras) {
7650 super.restoreFromExtras(extras);
7651
7652 mTexts.clear();
7653 if (extras.containsKey(EXTRA_TEXT_LINES)) {
7654 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
7655 }
7656 }
7657
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007658 /**
7659 * @hide
7660 */
7661 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08007662 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02007663 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007664 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
7665 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007666
Selim Cinek384804b2018-04-18 14:31:07 +08007667 TemplateBindResult result = new TemplateBindResult();
7668 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource(), result);
Daniel Sandler619738c2012-06-07 16:33:08 -04007669
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007670 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007671
Chris Wrend6297db2012-05-03 16:20:13 -04007672 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 -04007673 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04007674
Chris Wren4ed80d52012-05-17 09:30:03 -04007675 // Make sure all rows are gone in case we reuse a view.
7676 for (int rowId : rowIds) {
7677 contentView.setViewVisibility(rowId, View.GONE);
7678 }
7679
Daniel Sandler879c5e02012-04-17 16:46:51 -04007680 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07007681 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7682 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08007683 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07007684 int onlyViewId = 0;
7685 int maxRows = rowIds.length;
7686 if (mBuilder.mActions.size() > 0) {
7687 maxRows--;
7688 }
Selim Cinekbee4e072018-05-21 22:06:43 -07007689 CharSequence[] remoteInputHistory = mBuilder.mN.extras.getCharSequenceArray(
7690 EXTRA_REMOTE_INPUT_HISTORY);
7691 if (remoteInputHistory != null
7692 && remoteInputHistory.length > NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION) {
7693 // Let's remove some messages to make room for the remote input history.
7694 // 1 is always able to fit, but let's remove them if they are 2 or 3
7695 int numRemoteInputs = Math.min(remoteInputHistory.length,
7696 MAX_REMOTE_INPUT_HISTORY_LINES);
7697 int totalNumRows = mTexts.size() + numRemoteInputs
7698 - NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION;
7699 if (totalNumRows > maxRows) {
7700 int overflow = totalNumRows - maxRows;
7701 if (mTexts.size() > maxRows) {
7702 // Heuristic: if the Texts don't fit anyway, we'll rather drop the last
7703 // few messages, even with the remote input
7704 maxRows -= overflow;
7705 } else {
7706 // otherwise we drop the first messages
7707 i = overflow;
7708 }
7709 }
7710 }
Selim Cinek07c80172016-04-21 16:40:47 -07007711 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007712 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07007713 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007714 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07007715 contentView.setTextViewText(rowIds[i],
7716 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08007717 mBuilder.setTextViewColorSecondary(contentView, rowIds[i]);
Selim Cinek07c80172016-04-21 16:40:47 -07007718 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek384804b2018-04-18 14:31:07 +08007719 handleInboxImageMargin(contentView, rowIds[i], first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007720 result.getIconMarginEnd());
Selim Cinek07c80172016-04-21 16:40:47 -07007721 if (first) {
7722 onlyViewId = rowIds[i];
7723 } else {
7724 onlyViewId = 0;
7725 }
Selim Cinek247fa012016-02-18 09:50:48 -08007726 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007727 }
7728 i++;
7729 }
Selim Cinek07c80172016-04-21 16:40:47 -07007730 if (onlyViewId != 0) {
7731 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
7732 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7733 R.dimen.notification_text_margin_top);
7734 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
7735 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007736
Daniel Sandler879c5e02012-04-17 16:46:51 -04007737 return contentView;
7738 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007739
Julia Reynolds7217dc92018-03-07 12:12:09 -05007740 /**
7741 * @hide
7742 */
7743 @Override
7744 public boolean areNotificationsVisiblyDifferent(Style other) {
7745 if (other == null || getClass() != other.getClass()) {
7746 return true;
7747 }
7748 InboxStyle newS = (InboxStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04007749
7750 final ArrayList<CharSequence> myLines = getLines();
7751 final ArrayList<CharSequence> newLines = newS.getLines();
7752 final int n = myLines.size();
7753 if (n != newLines.size()) {
7754 return true;
7755 }
7756
7757 for (int i = 0; i < n; i++) {
7758 if (!Objects.equals(
7759 String.valueOf(myLines.get(i)),
7760 String.valueOf(newLines.get(i)))) {
7761 return true;
7762 }
7763 }
7764 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05007765 }
7766
Selim Cinek384804b2018-04-18 14:31:07 +08007767 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007768 int marginEndValue) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08007769 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08007770 if (first) {
7771 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
7772 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
7773 boolean hasProgress = max != 0 || ind;
Selim Cinek384804b2018-04-18 14:31:07 +08007774 if (!hasProgress) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08007775 endMargin = marginEndValue;
Selim Cinek247fa012016-02-18 09:50:48 -08007776 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007777 }
Selim Cinek1c72fa02018-04-23 18:00:54 +08007778 contentView.setViewLayoutMarginEnd(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08007779 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04007780 }
Dan Sandler842dd772014-05-15 09:36:47 -04007781
7782 /**
7783 * Notification style for media playback notifications.
7784 *
7785 * In the expanded form, {@link Notification#bigContentView}, up to 5
7786 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04007787 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04007788 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
7789 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
7790 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08007791 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04007792 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
7793 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01007794 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04007795 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08007796 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007797 * Notifications created with MediaStyle will have their category set to
7798 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
7799 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08007800 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07007801 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
7802 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04007803 * the System UI can identify this as a notification representing an active media session
7804 * and respond accordingly (by showing album artwork in the lockscreen, for example).
7805 *
Selim Cinek99104832017-01-25 14:47:33 -08007806 * <p>
7807 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
7808 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
7809 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
7810 * <p>
7811 *
Dan Sandler842dd772014-05-15 09:36:47 -04007812 * To use this style with your Notification, feed it to
7813 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7814 * <pre class="prettyprint">
7815 * Notification noti = new Notification.Builder()
7816 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01007817 * .setContentTitle(&quot;Track title&quot;)
7818 * .setContentText(&quot;Artist - Album&quot;)
7819 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007820 * .setStyle(<b>new Notification.MediaStyle()</b>
7821 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04007822 * .build();
7823 * </pre>
7824 *
7825 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08007826 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04007827 */
7828 public static class MediaStyle extends Style {
Gus Prevas9cc96602018-10-11 11:24:23 -04007829 // Changing max media buttons requires also changing templates
7830 // (notification_template_material_media and notification_template_material_big_media).
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007831 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04007832 static final int MAX_MEDIA_BUTTONS = 5;
Gus Prevas9cc96602018-10-11 11:24:23 -04007833 @IdRes private static final int[] MEDIA_BUTTON_IDS = {
7834 R.id.action0,
7835 R.id.action1,
7836 R.id.action2,
7837 R.id.action3,
7838 R.id.action4,
7839 };
Dan Sandler842dd772014-05-15 09:36:47 -04007840
7841 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07007842 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04007843
7844 public MediaStyle() {
7845 }
7846
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007847 /**
7848 * @deprecated use {@code MediaStyle()}.
7849 */
7850 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04007851 public MediaStyle(Builder builder) {
7852 setBuilder(builder);
7853 }
7854
7855 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007856 * 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 -04007857 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007858 *
7859 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04007860 */
7861 public MediaStyle setShowActionsInCompactView(int...actions) {
7862 mActionsToShowInCompact = actions;
7863 return this;
7864 }
7865
7866 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07007867 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
7868 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04007869 */
Jeff Browndba34ba2014-06-24 20:46:03 -07007870 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04007871 mToken = token;
7872 return this;
7873 }
7874
Christoph Studer4600f9b2014-07-22 22:44:43 +02007875 /**
7876 * @hide
7877 */
Dan Sandler842dd772014-05-15 09:36:47 -04007878 @Override
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007879 @UnsupportedAppUsage
Dan Sandler842dd772014-05-15 09:36:47 -04007880 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02007881 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007882 if (wip.category == null) {
7883 wip.category = Notification.CATEGORY_TRANSPORT;
7884 }
Dan Sandler842dd772014-05-15 09:36:47 -04007885 return wip;
7886 }
7887
Christoph Studer4600f9b2014-07-22 22:44:43 +02007888 /**
7889 * @hide
7890 */
7891 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007892 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007893 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007894 }
7895
7896 /**
7897 * @hide
7898 */
7899 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007900 public RemoteViews makeBigContentView() {
7901 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007902 }
7903
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007904 /**
7905 * @hide
7906 */
7907 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007908 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007909 RemoteViews expanded = makeMediaBigContentView();
7910 return expanded != null ? expanded : makeMediaContentView();
7911 }
7912
Dan Sandler842dd772014-05-15 09:36:47 -04007913 /** @hide */
7914 @Override
7915 public void addExtras(Bundle extras) {
7916 super.addExtras(extras);
7917
7918 if (mToken != null) {
7919 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
7920 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01007921 if (mActionsToShowInCompact != null) {
7922 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
7923 }
Dan Sandler842dd772014-05-15 09:36:47 -04007924 }
7925
Christoph Studer4600f9b2014-07-22 22:44:43 +02007926 /**
7927 * @hide
7928 */
7929 @Override
7930 protected void restoreFromExtras(Bundle extras) {
7931 super.restoreFromExtras(extras);
7932
7933 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
7934 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
7935 }
7936 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
7937 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
7938 }
7939 }
7940
Julia Reynolds7217dc92018-03-07 12:12:09 -05007941 /**
7942 * @hide
7943 */
7944 @Override
7945 public boolean areNotificationsVisiblyDifferent(Style other) {
7946 if (other == null || getClass() != other.getClass()) {
7947 return true;
7948 }
7949 // All fields to compare are on the Notification object
7950 return false;
7951 }
7952
Gus Prevas9cc96602018-10-11 11:24:23 -04007953 private void bindMediaActionButton(RemoteViews container, @IdRes int buttonId,
7954 Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04007955 final boolean tombstone = (action.actionIntent == null);
Gus Prevas9cc96602018-10-11 11:24:23 -04007956 container.setViewVisibility(buttonId, View.VISIBLE);
7957 container.setImageViewIcon(buttonId, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07007958
7959 // If the action buttons should not be tinted, then just use the default
7960 // notification color. Otherwise, just use the passed-in color.
Gus Prevas9cc96602018-10-11 11:24:23 -04007961 Resources resources = mBuilder.mContext.getResources();
7962 Configuration currentConfig = resources.getConfiguration();
Lucas Dupinf03e7522018-06-25 16:21:13 -07007963 boolean inNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
7964 == Configuration.UI_MODE_NIGHT_YES;
Anthony Chenad4d1582017-04-10 16:07:58 -07007965 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized()
7966 ? color
Lucas Dupina291d192018-06-07 13:59:42 -07007967 : ContrastColorUtil.resolveColor(mBuilder.mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07007968 Notification.COLOR_DEFAULT, inNightMode);
Anthony Chenad4d1582017-04-10 16:07:58 -07007969
Gus Prevas9cc96602018-10-11 11:24:23 -04007970 container.setDrawableTint(buttonId, false, tintColor,
Sunny Goyal5b153922017-09-21 21:00:36 -07007971 PorterDuff.Mode.SRC_ATOP);
Gus Prevas9cc96602018-10-11 11:24:23 -04007972
7973 final TypedArray typedArray = mBuilder.mContext.obtainStyledAttributes(
7974 new int[]{ android.R.attr.colorControlHighlight });
7975 int rippleAlpha = Color.alpha(typedArray.getColor(0, 0));
7976 typedArray.recycle();
7977 int rippleColor = Color.argb(rippleAlpha, Color.red(tintColor), Color.green(tintColor),
7978 Color.blue(tintColor));
7979 container.setRippleDrawableColor(buttonId, ColorStateList.valueOf(rippleColor));
7980
Dan Sandler842dd772014-05-15 09:36:47 -04007981 if (!tombstone) {
Gus Prevas9cc96602018-10-11 11:24:23 -04007982 container.setOnClickPendingIntent(buttonId, action.actionIntent);
Dan Sandler842dd772014-05-15 09:36:47 -04007983 }
Gus Prevas9cc96602018-10-11 11:24:23 -04007984 container.setContentDescription(buttonId, action.title);
Dan Sandler842dd772014-05-15 09:36:47 -04007985 }
7986
7987 private RemoteViews makeMediaContentView() {
7988 RemoteViews view = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08007989 R.layout.notification_template_material_media, false, /* hasProgress */
7990 null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04007991
7992 final int numActions = mBuilder.mActions.size();
Gus Prevas9cc96602018-10-11 11:24:23 -04007993 final int numActionsToShow = mActionsToShowInCompact == null
Dan Sandler842dd772014-05-15 09:36:47 -04007994 ? 0
7995 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Gus Prevas9cc96602018-10-11 11:24:23 -04007996 if (numActionsToShow > numActions) {
7997 throw new IllegalArgumentException(String.format(
7998 "setShowActionsInCompactView: action %d out of bounds (max %d)",
7999 numActions, numActions - 1));
8000 }
8001 for (int i = 0; i < MAX_MEDIA_BUTTONS_IN_COMPACT; i++) {
8002 if (i < numActionsToShow) {
Dan Sandler842dd772014-05-15 09:36:47 -04008003 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Gus Prevas9cc96602018-10-11 11:24:23 -04008004 bindMediaActionButton(view, MEDIA_BUTTON_IDS[i], action, getActionColor());
8005 } else {
8006 view.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04008007 }
8008 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08008009 handleImage(view);
8010 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008011 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07008012 if (mBuilder.mN.hasLargeIcon()) {
Selim Cinek384804b2018-04-18 14:31:07 +08008013 endMargin = R.dimen.notification_media_image_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08008014 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008015 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04008016 return view;
8017 }
8018
Selim Cinekc7f5a822018-03-20 19:32:06 -07008019 private int getActionColor() {
8020 return mBuilder.isColorized() ? mBuilder.getPrimaryTextColor()
8021 : mBuilder.resolveContrastColor();
Selim Cinek99104832017-01-25 14:47:33 -08008022 }
8023
Dan Sandler842dd772014-05-15 09:36:47 -04008024 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008025 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008026 // Dont add an expanded view if there is no more content to be revealed
8027 int actionsInCompact = mActionsToShowInCompact == null
8028 ? 0
8029 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07008030 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008031 return null;
8032 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05008033 RemoteViews big = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08008034 R.layout.notification_template_material_big_media, false, null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04008035
Gus Prevas9cc96602018-10-11 11:24:23 -04008036 for (int i = 0; i < MAX_MEDIA_BUTTONS; i++) {
8037 if (i < actionCount) {
8038 bindMediaActionButton(big, MEDIA_BUTTON_IDS[i], mBuilder.mActions.get(i),
Selim Cinekc7f5a822018-03-20 19:32:06 -07008039 getActionColor());
Gus Prevas9cc96602018-10-11 11:24:23 -04008040 } else {
8041 big.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04008042 }
8043 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05008044 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04008045 return big;
8046 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008047
Selim Cinek5bf069a2015-11-10 19:14:27 -05008048 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07008049 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008050 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
8051 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008052 }
8053 }
8054
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008055 /**
8056 * @hide
8057 */
8058 @Override
8059 protected boolean hasProgress() {
8060 return false;
8061 }
Dan Sandler842dd772014-05-15 09:36:47 -04008062 }
Griff Hazen61a9e862014-05-22 16:05:19 -07008063
Selim Cinek593610c2016-02-16 18:42:57 -08008064 /**
8065 * Notification style for custom views that are decorated by the system
8066 *
8067 * <p>Instead of providing a notification that is completely custom, a developer can set this
8068 * style and still obtain system decorations like the notification header with the expand
8069 * affordance and actions.
8070 *
8071 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8072 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8073 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8074 * corresponding custom views to display.
8075 *
8076 * To use this style with your Notification, feed it to
8077 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8078 * <pre class="prettyprint">
8079 * Notification noti = new Notification.Builder()
8080 * .setSmallIcon(R.drawable.ic_stat_player)
8081 * .setLargeIcon(albumArtBitmap))
8082 * .setCustomContentView(contentView);
8083 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
8084 * .build();
8085 * </pre>
8086 */
8087 public static class DecoratedCustomViewStyle extends Style {
8088
8089 public DecoratedCustomViewStyle() {
8090 }
8091
Selim Cinek593610c2016-02-16 18:42:57 -08008092 /**
8093 * @hide
8094 */
8095 public boolean displayCustomViewInline() {
8096 return true;
8097 }
8098
8099 /**
8100 * @hide
8101 */
8102 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008103 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008104 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
8105 }
8106
8107 /**
8108 * @hide
8109 */
8110 @Override
8111 public RemoteViews makeBigContentView() {
8112 return makeDecoratedBigContentView();
8113 }
8114
8115 /**
8116 * @hide
8117 */
8118 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008119 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008120 return makeDecoratedHeadsUpContentView();
8121 }
8122
Selim Cinek593610c2016-02-16 18:42:57 -08008123 private RemoteViews makeDecoratedHeadsUpContentView() {
8124 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
8125 ? mBuilder.mN.contentView
8126 : mBuilder.mN.headsUpContentView;
8127 if (mBuilder.mActions.size() == 0) {
8128 return makeStandardTemplateWithCustomContent(headsUpContentView);
8129 }
Selim Cinek384804b2018-04-18 14:31:07 +08008130 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008131 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008132 mBuilder.getBigBaseLayoutResource(), result);
8133 buildIntoRemoteViewContent(remoteViews, headsUpContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008134 return remoteViews;
8135 }
8136
Selim Cinek593610c2016-02-16 18:42:57 -08008137 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
Selim Cinek384804b2018-04-18 14:31:07 +08008138 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008139 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08008140 mBuilder.getBaseLayoutResource(), result);
8141 buildIntoRemoteViewContent(remoteViews, customContent, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008142 return remoteViews;
8143 }
8144
Selim Cinek593610c2016-02-16 18:42:57 -08008145 private RemoteViews makeDecoratedBigContentView() {
8146 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
8147 ? mBuilder.mN.contentView
8148 : mBuilder.mN.bigContentView;
8149 if (mBuilder.mActions.size() == 0) {
8150 return makeStandardTemplateWithCustomContent(bigContentView);
8151 }
Selim Cinek384804b2018-04-18 14:31:07 +08008152 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008153 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008154 mBuilder.getBigBaseLayoutResource(), result);
8155 buildIntoRemoteViewContent(remoteViews, bigContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008156 return remoteViews;
8157 }
Selim Cinek247fa012016-02-18 09:50:48 -08008158
8159 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
Selim Cinek384804b2018-04-18 14:31:07 +08008160 RemoteViews customContent, TemplateBindResult result) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08008161 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008162 // Need to clone customContent before adding, because otherwise it can no longer be
8163 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008164 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07008165 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
8166 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Selim Cinekfc8073c2017-08-16 17:50:20 -07008167 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08008168 }
Selim Cinek247fa012016-02-18 09:50:48 -08008169 // also update the end margin if there is an image
Selim Cinek384804b2018-04-18 14:31:07 +08008170 Resources resources = mBuilder.mContext.getResources();
8171 int endMargin = resources.getDimensionPixelSize(
Selim Cinek1c72fa02018-04-23 18:00:54 +08008172 R.dimen.notification_content_margin_end) + result.getIconMarginEnd();
Selim Cinek384804b2018-04-18 14:31:07 +08008173 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08008174 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05008175
8176 /**
8177 * @hide
8178 */
8179 @Override
8180 public boolean areNotificationsVisiblyDifferent(Style other) {
8181 if (other == null || getClass() != other.getClass()) {
8182 return true;
8183 }
8184 // Comparison done for all custom RemoteViews, independent of style
8185 return false;
8186 }
Selim Cinek593610c2016-02-16 18:42:57 -08008187 }
8188
Selim Cinek03eb3b72016-02-18 10:39:45 -08008189 /**
8190 * Notification style for media custom views that are decorated by the system
8191 *
8192 * <p>Instead of providing a media notification that is completely custom, a developer can set
8193 * this style and still obtain system decorations like the notification header with the expand
8194 * affordance and actions.
8195 *
8196 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8197 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8198 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8199 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08008200 * <p>
8201 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
8202 * notification by using {@link Notification.Builder#setColorized(boolean)}.
8203 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08008204 * To use this style with your Notification, feed it to
8205 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8206 * <pre class="prettyprint">
8207 * Notification noti = new Notification.Builder()
8208 * .setSmallIcon(R.drawable.ic_stat_player)
8209 * .setLargeIcon(albumArtBitmap))
8210 * .setCustomContentView(contentView);
8211 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
8212 * .setMediaSession(mySession))
8213 * .build();
8214 * </pre>
8215 *
8216 * @see android.app.Notification.DecoratedCustomViewStyle
8217 * @see android.app.Notification.MediaStyle
8218 */
8219 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
8220
8221 public DecoratedMediaCustomViewStyle() {
8222 }
8223
Selim Cinek03eb3b72016-02-18 10:39:45 -08008224 /**
8225 * @hide
8226 */
8227 public boolean displayCustomViewInline() {
8228 return true;
8229 }
8230
8231 /**
8232 * @hide
8233 */
8234 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008235 public RemoteViews makeContentView(boolean increasedHeight) {
8236 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008237 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8238 mBuilder.mN.contentView);
8239 }
8240
8241 /**
8242 * @hide
8243 */
8244 @Override
8245 public RemoteViews makeBigContentView() {
8246 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
8247 ? mBuilder.mN.bigContentView
8248 : mBuilder.mN.contentView;
8249 return makeBigContentViewWithCustomContent(customRemoteView);
8250 }
8251
8252 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
8253 RemoteViews remoteViews = super.makeBigContentView();
8254 if (remoteViews != null) {
8255 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
8256 customRemoteView);
8257 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08008258 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008259 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8260 customRemoteView);
8261 } else {
8262 return null;
8263 }
8264 }
8265
8266 /**
8267 * @hide
8268 */
8269 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008270 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08008271 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
8272 ? mBuilder.mN.headsUpContentView
8273 : mBuilder.mN.contentView;
8274 return makeBigContentViewWithCustomContent(customRemoteView);
8275 }
8276
Julia Reynolds7217dc92018-03-07 12:12:09 -05008277 /**
8278 * @hide
8279 */
8280 @Override
8281 public boolean areNotificationsVisiblyDifferent(Style other) {
8282 if (other == null || getClass() != other.getClass()) {
8283 return true;
8284 }
8285 // Comparison done for all custom RemoteViews, independent of style
8286 return false;
8287 }
8288
Selim Cinek03eb3b72016-02-18 10:39:45 -08008289 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
8290 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08008291 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008292 // Need to clone customContent before adding, because otherwise it can no longer be
8293 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008294 customContent = customContent.clone();
Selim Cinek87c31532017-08-18 18:53:44 -07008295 customContent.overrideTextColors(mBuilder.getPrimaryTextColor());
Selim Cinekf91017e2016-03-14 12:25:09 -07008296 remoteViews.removeAllViews(id);
8297 remoteViews.addView(id, customContent);
Selim Cinekfc8073c2017-08-16 17:50:20 -07008298 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08008299 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08008300 return remoteViews;
8301 }
8302 }
8303
Christoph Studer4600f9b2014-07-22 22:44:43 +02008304 // When adding a new Style subclass here, don't forget to update
8305 // Builder.getNotificationStyleClass.
8306
Griff Hazen61a9e862014-05-22 16:05:19 -07008307 /**
8308 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
8309 * metadata or change options on a notification builder.
8310 */
8311 public interface Extender {
8312 /**
8313 * Apply this extender to a notification builder.
8314 * @param builder the builder to be modified.
8315 * @return the build object for chaining.
8316 */
8317 public Builder extend(Builder builder);
8318 }
8319
8320 /**
8321 * Helper class to add wearable extensions to notifications.
8322 * <p class="note"> See
8323 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
8324 * for Android Wear</a> for more information on how to use this class.
8325 * <p>
8326 * To create a notification with wearable extensions:
8327 * <ol>
8328 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
8329 * properties.
8330 * <li>Create a {@link android.app.Notification.WearableExtender}.
8331 * <li>Set wearable-specific properties using the
8332 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
8333 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
8334 * notification.
8335 * <li>Post the notification to the notification system with the
8336 * {@code NotificationManager.notify(...)} methods.
8337 * </ol>
8338 *
8339 * <pre class="prettyprint">
8340 * Notification notif = new Notification.Builder(mContext)
8341 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
8342 * .setContentText(subject)
8343 * .setSmallIcon(R.drawable.new_mail)
8344 * .extend(new Notification.WearableExtender()
8345 * .setContentIcon(R.drawable.new_mail))
8346 * .build();
8347 * NotificationManager notificationManger =
8348 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
8349 * notificationManger.notify(0, notif);</pre>
8350 *
8351 * <p>Wearable extensions can be accessed on an existing notification by using the
8352 * {@code WearableExtender(Notification)} constructor,
8353 * and then using the {@code get} methods to access values.
8354 *
8355 * <pre class="prettyprint">
8356 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
8357 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07008358 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008359 */
8360 public static final class WearableExtender implements Extender {
8361 /**
8362 * Sentinel value for an action index that is unset.
8363 */
8364 public static final int UNSET_ACTION_INDEX = -1;
8365
8366 /**
8367 * Size value for use with {@link #setCustomSizePreset} to show this notification with
8368 * default sizing.
8369 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07008370 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07008371 * on their content.
Gus Prevasd7363752018-09-18 14:35:15 -04008372 *
8373 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008374 */
Gus Prevasd7363752018-09-18 14:35:15 -04008375 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008376 public static final int SIZE_DEFAULT = 0;
8377
8378 /**
8379 * Size value for use with {@link #setCustomSizePreset} to show this notification
8380 * with an extra small size.
8381 * <p>This value is only applicable for custom display notifications created using
8382 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008383 *
8384 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008385 */
Gus Prevasd7363752018-09-18 14:35:15 -04008386 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008387 public static final int SIZE_XSMALL = 1;
8388
8389 /**
8390 * Size value for use with {@link #setCustomSizePreset} to show this notification
8391 * with a small size.
8392 * <p>This value is only applicable for custom display notifications created using
8393 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008394 *
8395 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008396 */
Gus Prevasd7363752018-09-18 14:35:15 -04008397 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008398 public static final int SIZE_SMALL = 2;
8399
8400 /**
8401 * Size value for use with {@link #setCustomSizePreset} to show this notification
8402 * with a medium size.
8403 * <p>This value is only applicable for custom display notifications created using
8404 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008405 *
8406 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008407 */
Gus Prevasd7363752018-09-18 14:35:15 -04008408 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008409 public static final int SIZE_MEDIUM = 3;
8410
8411 /**
8412 * Size value for use with {@link #setCustomSizePreset} to show this notification
8413 * with a large size.
8414 * <p>This value is only applicable for custom display notifications created using
8415 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008416 *
8417 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008418 */
Gus Prevasd7363752018-09-18 14:35:15 -04008419 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008420 public static final int SIZE_LARGE = 4;
8421
Griff Hazend5f11f92014-05-27 15:40:09 -07008422 /**
8423 * Size value for use with {@link #setCustomSizePreset} to show this notification
8424 * full screen.
8425 * <p>This value is only applicable for custom display notifications created using
8426 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008427 *
8428 * @deprecated Display intents are no longer supported.
Griff Hazend5f11f92014-05-27 15:40:09 -07008429 */
Gus Prevasd7363752018-09-18 14:35:15 -04008430 @Deprecated
Griff Hazend5f11f92014-05-27 15:40:09 -07008431 public static final int SIZE_FULL_SCREEN = 5;
8432
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008433 /**
8434 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
8435 * short amount of time when this notification is displayed on the screen. This
8436 * is the default value.
Gus Prevasd7363752018-09-18 14:35:15 -04008437 *
8438 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008439 */
Gus Prevasd7363752018-09-18 14:35:15 -04008440 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008441 public static final int SCREEN_TIMEOUT_SHORT = 0;
8442
8443 /**
8444 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
8445 * for a longer amount of time when this notification is displayed on the screen.
Gus Prevasd7363752018-09-18 14:35:15 -04008446 *
8447 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008448 */
Gus Prevasd7363752018-09-18 14:35:15 -04008449 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008450 public static final int SCREEN_TIMEOUT_LONG = -1;
8451
Griff Hazen61a9e862014-05-22 16:05:19 -07008452 /** Notification extra which contains wearable extensions */
8453 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
8454
Pete Gastaf6781d2014-10-07 15:17:05 -04008455 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07008456 private static final String KEY_ACTIONS = "actions";
8457 private static final String KEY_FLAGS = "flags";
8458 private static final String KEY_DISPLAY_INTENT = "displayIntent";
8459 private static final String KEY_PAGES = "pages";
8460 private static final String KEY_BACKGROUND = "background";
8461 private static final String KEY_CONTENT_ICON = "contentIcon";
8462 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
8463 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
8464 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
8465 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
8466 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008467 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04008468 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008469 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07008470
8471 // Flags bitwise-ored to mFlags
8472 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
8473 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
8474 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
8475 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008476 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04008477 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04008478 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07008479
8480 // Default value for flags integer
8481 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
8482
8483 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
8484 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
8485
8486 private ArrayList<Action> mActions = new ArrayList<Action>();
8487 private int mFlags = DEFAULT_FLAGS;
8488 private PendingIntent mDisplayIntent;
8489 private ArrayList<Notification> mPages = new ArrayList<Notification>();
8490 private Bitmap mBackground;
8491 private int mContentIcon;
8492 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
8493 private int mContentActionIndex = UNSET_ACTION_INDEX;
8494 private int mCustomSizePreset = SIZE_DEFAULT;
8495 private int mCustomContentHeight;
8496 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008497 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04008498 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008499 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07008500
8501 /**
8502 * Create a {@link android.app.Notification.WearableExtender} with default
8503 * options.
8504 */
8505 public WearableExtender() {
8506 }
8507
8508 public WearableExtender(Notification notif) {
8509 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
8510 if (wearableBundle != null) {
8511 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
8512 if (actions != null) {
8513 mActions.addAll(actions);
8514 }
8515
8516 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
8517 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
8518
8519 Notification[] pages = getNotificationArrayFromBundle(
8520 wearableBundle, KEY_PAGES);
8521 if (pages != null) {
8522 Collections.addAll(mPages, pages);
8523 }
8524
8525 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
8526 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
8527 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
8528 DEFAULT_CONTENT_ICON_GRAVITY);
8529 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
8530 UNSET_ACTION_INDEX);
8531 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
8532 SIZE_DEFAULT);
8533 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
8534 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008535 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04008536 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008537 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07008538 }
8539 }
8540
8541 /**
8542 * Apply wearable extensions to a notification that is being built. This is typically
8543 * called by the {@link android.app.Notification.Builder#extend} method of
8544 * {@link android.app.Notification.Builder}.
8545 */
8546 @Override
8547 public Notification.Builder extend(Notification.Builder builder) {
8548 Bundle wearableBundle = new Bundle();
8549
8550 if (!mActions.isEmpty()) {
8551 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
8552 }
8553 if (mFlags != DEFAULT_FLAGS) {
8554 wearableBundle.putInt(KEY_FLAGS, mFlags);
8555 }
8556 if (mDisplayIntent != null) {
8557 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
8558 }
8559 if (!mPages.isEmpty()) {
8560 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
8561 new Notification[mPages.size()]));
8562 }
8563 if (mBackground != null) {
8564 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
8565 }
8566 if (mContentIcon != 0) {
8567 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
8568 }
8569 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
8570 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
8571 }
8572 if (mContentActionIndex != UNSET_ACTION_INDEX) {
8573 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
8574 mContentActionIndex);
8575 }
8576 if (mCustomSizePreset != SIZE_DEFAULT) {
8577 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
8578 }
8579 if (mCustomContentHeight != 0) {
8580 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
8581 }
8582 if (mGravity != DEFAULT_GRAVITY) {
8583 wearableBundle.putInt(KEY_GRAVITY, mGravity);
8584 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008585 if (mHintScreenTimeout != 0) {
8586 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
8587 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04008588 if (mDismissalId != null) {
8589 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
8590 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008591 if (mBridgeTag != null) {
8592 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
8593 }
Griff Hazen61a9e862014-05-22 16:05:19 -07008594
8595 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
8596 return builder;
8597 }
8598
8599 @Override
8600 public WearableExtender clone() {
8601 WearableExtender that = new WearableExtender();
8602 that.mActions = new ArrayList<Action>(this.mActions);
8603 that.mFlags = this.mFlags;
8604 that.mDisplayIntent = this.mDisplayIntent;
8605 that.mPages = new ArrayList<Notification>(this.mPages);
8606 that.mBackground = this.mBackground;
8607 that.mContentIcon = this.mContentIcon;
8608 that.mContentIconGravity = this.mContentIconGravity;
8609 that.mContentActionIndex = this.mContentActionIndex;
8610 that.mCustomSizePreset = this.mCustomSizePreset;
8611 that.mCustomContentHeight = this.mCustomContentHeight;
8612 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008613 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04008614 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008615 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07008616 return that;
8617 }
8618
8619 /**
8620 * Add a wearable action to this notification.
8621 *
8622 * <p>When wearable actions are added using this method, the set of actions that
8623 * show on a wearable device splits from devices that only show actions added
8624 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
8625 * of which actions display on different devices.
8626 *
8627 * @param action the action to add to this notification
8628 * @return this object for method chaining
8629 * @see android.app.Notification.Action
8630 */
8631 public WearableExtender addAction(Action action) {
8632 mActions.add(action);
8633 return this;
8634 }
8635
8636 /**
8637 * Adds wearable actions to this notification.
8638 *
8639 * <p>When wearable actions are added using this method, the set of actions that
8640 * show on a wearable device splits from devices that only show actions added
8641 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
8642 * of which actions display on different devices.
8643 *
8644 * @param actions the actions to add to this notification
8645 * @return this object for method chaining
8646 * @see android.app.Notification.Action
8647 */
8648 public WearableExtender addActions(List<Action> actions) {
8649 mActions.addAll(actions);
8650 return this;
8651 }
8652
8653 /**
8654 * Clear all wearable actions present on this builder.
8655 * @return this object for method chaining.
8656 * @see #addAction
8657 */
8658 public WearableExtender clearActions() {
8659 mActions.clear();
8660 return this;
8661 }
8662
8663 /**
8664 * Get the wearable actions present on this notification.
8665 */
8666 public List<Action> getActions() {
8667 return mActions;
8668 }
8669
8670 /**
8671 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07008672 * this notification. The {@link PendingIntent} provided should be for an activity.
8673 *
8674 * <pre class="prettyprint">
8675 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
8676 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
8677 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
8678 * Notification notif = new Notification.Builder(context)
8679 * .extend(new Notification.WearableExtender()
8680 * .setDisplayIntent(displayPendingIntent)
8681 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
8682 * .build();</pre>
8683 *
8684 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07008685 * should have an empty task affinity. It is also recommended to use the device
8686 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07008687 *
8688 * <p>Example AndroidManifest.xml entry:
8689 * <pre class="prettyprint">
8690 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
8691 * android:exported=&quot;true&quot;
8692 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07008693 * android:taskAffinity=&quot;&quot;
8694 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008695 *
8696 * @param intent the {@link PendingIntent} for an activity
8697 * @return this object for method chaining
8698 * @see android.app.Notification.WearableExtender#getDisplayIntent
Gus Prevasd7363752018-09-18 14:35:15 -04008699 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008700 */
Gus Prevasd7363752018-09-18 14:35:15 -04008701 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008702 public WearableExtender setDisplayIntent(PendingIntent intent) {
8703 mDisplayIntent = intent;
8704 return this;
8705 }
8706
8707 /**
8708 * Get the intent to launch inside of an activity view when displaying this
8709 * notification. This {@code PendingIntent} should be for an activity.
Gus Prevasd7363752018-09-18 14:35:15 -04008710 *
8711 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008712 */
Gus Prevasd7363752018-09-18 14:35:15 -04008713 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008714 public PendingIntent getDisplayIntent() {
8715 return mDisplayIntent;
8716 }
8717
8718 /**
8719 * Add an additional page of content to display with this notification. The current
8720 * notification forms the first page, and pages added using this function form
8721 * subsequent pages. This field can be used to separate a notification into multiple
8722 * sections.
8723 *
8724 * @param page the notification to add as another page
8725 * @return this object for method chaining
8726 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04008727 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008728 */
Gus Prevasd7363752018-09-18 14:35:15 -04008729 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008730 public WearableExtender addPage(Notification page) {
8731 mPages.add(page);
8732 return this;
8733 }
8734
8735 /**
8736 * Add additional pages of content to display with this notification. The current
8737 * notification forms the first page, and pages added using this function form
8738 * subsequent pages. This field can be used to separate a notification into multiple
8739 * sections.
8740 *
8741 * @param pages a list of notifications
8742 * @return this object for method chaining
8743 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04008744 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008745 */
Gus Prevasd7363752018-09-18 14:35:15 -04008746 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008747 public WearableExtender addPages(List<Notification> pages) {
8748 mPages.addAll(pages);
8749 return this;
8750 }
8751
8752 /**
8753 * Clear all additional pages present on this builder.
8754 * @return this object for method chaining.
8755 * @see #addPage
Gus Prevasd7363752018-09-18 14:35:15 -04008756 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008757 */
Gus Prevasd7363752018-09-18 14:35:15 -04008758 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008759 public WearableExtender clearPages() {
8760 mPages.clear();
8761 return this;
8762 }
8763
8764 /**
8765 * Get the array of additional pages of content for displaying this notification. The
8766 * current notification forms the first page, and elements within this array form
8767 * subsequent pages. This field can be used to separate a notification into multiple
8768 * sections.
8769 * @return the pages for this notification
Gus Prevasd7363752018-09-18 14:35:15 -04008770 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008771 */
Gus Prevasd7363752018-09-18 14:35:15 -04008772 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008773 public List<Notification> getPages() {
8774 return mPages;
8775 }
8776
8777 /**
8778 * Set a background image to be displayed behind the notification content.
8779 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8780 * will work with any notification style.
8781 *
8782 * @param background the background bitmap
8783 * @return this object for method chaining
8784 * @see android.app.Notification.WearableExtender#getBackground
Gus Prevasd7363752018-09-18 14:35:15 -04008785 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008786 */
Gus Prevasd7363752018-09-18 14:35:15 -04008787 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008788 public WearableExtender setBackground(Bitmap background) {
8789 mBackground = background;
8790 return this;
8791 }
8792
8793 /**
8794 * Get a background image to be displayed behind the notification content.
8795 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8796 * will work with any notification style.
8797 *
8798 * @return the background image
8799 * @see android.app.Notification.WearableExtender#setBackground
Gus Prevasd7363752018-09-18 14:35:15 -04008800 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008801 */
Gus Prevasd7363752018-09-18 14:35:15 -04008802 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008803 public Bitmap getBackground() {
8804 return mBackground;
8805 }
8806
8807 /**
8808 * Set an icon that goes with the content of this notification.
8809 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008810 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008811 public WearableExtender setContentIcon(int icon) {
8812 mContentIcon = icon;
8813 return this;
8814 }
8815
8816 /**
8817 * Get an icon that goes with the content of this notification.
8818 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008819 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008820 public int getContentIcon() {
8821 return mContentIcon;
8822 }
8823
8824 /**
8825 * Set the gravity that the content icon should have within the notification display.
8826 * Supported values include {@link android.view.Gravity#START} and
8827 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8828 * @see #setContentIcon
8829 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008830 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008831 public WearableExtender setContentIconGravity(int contentIconGravity) {
8832 mContentIconGravity = contentIconGravity;
8833 return this;
8834 }
8835
8836 /**
8837 * Get the gravity that the content icon should have within the notification display.
8838 * Supported values include {@link android.view.Gravity#START} and
8839 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8840 * @see #getContentIcon
8841 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008842 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008843 public int getContentIconGravity() {
8844 return mContentIconGravity;
8845 }
8846
8847 /**
Gus Prevasd7363752018-09-18 14:35:15 -04008848 * Set an action from this notification's actions as the primary action. If the action has a
8849 * {@link RemoteInput} associated with it, shortcuts to the options for that input are shown
8850 * directly on the notification.
Griff Hazen14f57992014-05-26 09:07:14 -07008851 *
Gus Prevasd7363752018-09-18 14:35:15 -04008852 * @param actionIndex The index of the primary action.
Griff Hazen14f57992014-05-26 09:07:14 -07008853 * If wearable actions were added to the main notification, this index
8854 * will apply to that list, otherwise it will apply to the regular
8855 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07008856 */
8857 public WearableExtender setContentAction(int actionIndex) {
8858 mContentActionIndex = actionIndex;
8859 return this;
8860 }
8861
8862 /**
Gus Prevasd7363752018-09-18 14:35:15 -04008863 * Get the index of the notification action, if any, that was specified as the primary
8864 * action.
Griff Hazen14f57992014-05-26 09:07:14 -07008865 *
8866 * <p>If wearable specific actions were added to the main notification, this index will
8867 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07008868 *
8869 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07008870 */
8871 public int getContentAction() {
8872 return mContentActionIndex;
8873 }
8874
8875 /**
8876 * Set the gravity that this notification should have within the available viewport space.
8877 * Supported values include {@link android.view.Gravity#TOP},
8878 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8879 * The default value is {@link android.view.Gravity#BOTTOM}.
8880 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008881 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008882 public WearableExtender setGravity(int gravity) {
8883 mGravity = gravity;
8884 return this;
8885 }
8886
8887 /**
8888 * Get the gravity that this notification should have within the available viewport space.
8889 * Supported values include {@link android.view.Gravity#TOP},
8890 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8891 * The default value is {@link android.view.Gravity#BOTTOM}.
8892 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008893 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008894 public int getGravity() {
8895 return mGravity;
8896 }
8897
8898 /**
8899 * Set the custom size preset for the display of this notification out of the available
8900 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
8901 * {@link #SIZE_LARGE}.
8902 * <p>Some custom size presets are only applicable for custom display notifications created
8903 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
8904 * documentation for the preset in question. See also
8905 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
8906 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008907 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008908 public WearableExtender setCustomSizePreset(int sizePreset) {
8909 mCustomSizePreset = sizePreset;
8910 return this;
8911 }
8912
8913 /**
8914 * Get the custom size preset for the display of this notification out of the available
8915 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
8916 * {@link #SIZE_LARGE}.
8917 * <p>Some custom size presets are only applicable for custom display notifications created
8918 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
8919 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
8920 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008921 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008922 public int getCustomSizePreset() {
8923 return mCustomSizePreset;
8924 }
8925
8926 /**
8927 * Set the custom height in pixels for the display of this notification's content.
8928 * <p>This option is only available for custom display notifications created
8929 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
8930 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
8931 * {@link #getCustomContentHeight}.
8932 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008933 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008934 public WearableExtender setCustomContentHeight(int height) {
8935 mCustomContentHeight = height;
8936 return this;
8937 }
8938
8939 /**
8940 * Get the custom height in pixels for the display of this notification's content.
8941 * <p>This option is only available for custom display notifications created
8942 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
8943 * {@link #setCustomContentHeight}.
8944 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008945 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008946 public int getCustomContentHeight() {
8947 return mCustomContentHeight;
8948 }
8949
8950 /**
8951 * Set whether the scrolling position for the contents of this notification should start
8952 * at the bottom of the contents instead of the top when the contents are too long to
8953 * display within the screen. Default is false (start scroll at the top).
8954 */
8955 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
8956 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
8957 return this;
8958 }
8959
8960 /**
8961 * Get whether the scrolling position for the contents of this notification should start
8962 * at the bottom of the contents instead of the top when the contents are too long to
8963 * display within the screen. Default is false (start scroll at the top).
8964 */
8965 public boolean getStartScrollBottom() {
8966 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
8967 }
8968
8969 /**
8970 * Set whether the content intent is available when the wearable device is not connected
8971 * to a companion device. The user can still trigger this intent when the wearable device
8972 * is offline, but a visual hint will indicate that the content intent may not be available.
8973 * Defaults to true.
8974 */
8975 public WearableExtender setContentIntentAvailableOffline(
8976 boolean contentIntentAvailableOffline) {
8977 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
8978 return this;
8979 }
8980
8981 /**
8982 * Get whether the content intent is available when the wearable device is not connected
8983 * to a companion device. The user can still trigger this intent when the wearable device
8984 * is offline, but a visual hint will indicate that the content intent may not be available.
8985 * Defaults to true.
8986 */
8987 public boolean getContentIntentAvailableOffline() {
8988 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
8989 }
8990
8991 /**
8992 * Set a hint that this notification's icon should not be displayed.
8993 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
8994 * @return this object for method chaining
8995 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008996 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008997 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
8998 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
8999 return this;
9000 }
9001
9002 /**
9003 * Get a hint that this notification's icon should not be displayed.
9004 * @return {@code true} if this icon should not be displayed, false otherwise.
9005 * The default value is {@code false} if this was never set.
9006 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009007 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009008 public boolean getHintHideIcon() {
9009 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
9010 }
9011
9012 /**
9013 * Set a visual hint that only the background image of this notification should be
9014 * displayed, and other semantic content should be hidden. This hint is only applicable
9015 * to sub-pages added using {@link #addPage}.
9016 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009017 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009018 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
9019 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
9020 return this;
9021 }
9022
9023 /**
9024 * Get a visual hint that only the background image of this notification should be
9025 * displayed, and other semantic content should be hidden. This hint is only applicable
9026 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
9027 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009028 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009029 public boolean getHintShowBackgroundOnly() {
9030 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
9031 }
9032
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009033 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009034 * Set a hint that this notification's background should not be clipped if possible,
9035 * and should instead be resized to fully display on the screen, retaining the aspect
9036 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009037 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
9038 * @return this object for method chaining
9039 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009040 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009041 public WearableExtender setHintAvoidBackgroundClipping(
9042 boolean hintAvoidBackgroundClipping) {
9043 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
9044 return this;
9045 }
9046
9047 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009048 * Get a hint that this notification's background should not be clipped if possible,
9049 * and should instead be resized to fully display on the screen, retaining the aspect
9050 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009051 * @return {@code true} if it's ok if the background is clipped on the screen, false
9052 * otherwise. The default value is {@code false} if this was never set.
9053 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009054 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009055 public boolean getHintAvoidBackgroundClipping() {
9056 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
9057 }
9058
9059 /**
9060 * Set a hint that the screen should remain on for at least this duration when
9061 * this notification is displayed on the screen.
9062 * @param timeout The requested screen timeout in milliseconds. Can also be either
9063 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9064 * @return this object for method chaining
9065 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009066 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009067 public WearableExtender setHintScreenTimeout(int timeout) {
9068 mHintScreenTimeout = timeout;
9069 return this;
9070 }
9071
9072 /**
9073 * Get the duration, in milliseconds, that the screen should remain on for
9074 * when this notification is displayed.
9075 * @return the duration in milliseconds if > 0, or either one of the sentinel values
9076 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9077 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009078 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009079 public int getHintScreenTimeout() {
9080 return mHintScreenTimeout;
9081 }
9082
Alex Hills9ab3a232016-04-05 14:54:56 -04009083 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04009084 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
9085 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9086 * qr codes, as well as other simple black-and-white tickets.
9087 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
9088 * @return this object for method chaining
Gus Prevasd7363752018-09-18 14:35:15 -04009089 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009090 */
Gus Prevasd7363752018-09-18 14:35:15 -04009091 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009092 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
9093 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
9094 return this;
9095 }
9096
9097 /**
9098 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
9099 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9100 * qr codes, as well as other simple black-and-white tickets.
9101 * @return {@code true} if it should be displayed in ambient, false otherwise
9102 * otherwise. The default value is {@code false} if this was never set.
Gus Prevasd7363752018-09-18 14:35:15 -04009103 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009104 */
Gus Prevasd7363752018-09-18 14:35:15 -04009105 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009106 public boolean getHintAmbientBigPicture() {
9107 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
9108 }
9109
9110 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04009111 * Set a hint that this notification's content intent will launch an {@link Activity}
9112 * directly, telling the platform that it can generate the appropriate transitions.
9113 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
9114 * an activity and transitions should be generated, false otherwise.
9115 * @return this object for method chaining
9116 */
9117 public WearableExtender setHintContentIntentLaunchesActivity(
9118 boolean hintContentIntentLaunchesActivity) {
9119 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
9120 return this;
9121 }
9122
9123 /**
9124 * Get a hint that this notification's content intent will launch an {@link Activity}
9125 * directly, telling the platform that it can generate the appropriate transitions
9126 * @return {@code true} if the content intent will launch an activity and transitions should
9127 * be generated, false otherwise. The default value is {@code false} if this was never set.
9128 */
9129 public boolean getHintContentIntentLaunchesActivity() {
9130 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
9131 }
9132
Nadia Benbernou948627e2016-04-14 14:41:08 -04009133 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009134 * Sets the dismissal id for this notification. If a notification is posted with a
9135 * dismissal id, then when that notification is canceled, notifications on other wearables
9136 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04009137 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009138 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04009139 * @param dismissalId the dismissal id of the notification.
9140 * @return this object for method chaining
9141 */
9142 public WearableExtender setDismissalId(String dismissalId) {
9143 mDismissalId = dismissalId;
9144 return this;
9145 }
9146
9147 /**
9148 * Returns the dismissal id of the notification.
9149 * @return the dismissal id of the notification or null if it has not been set.
9150 */
9151 public String getDismissalId() {
9152 return mDismissalId;
9153 }
9154
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009155 /**
9156 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
9157 * posted from a phone to provide finer-grained control on what notifications are bridged
9158 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
9159 * Features to Notifications</a> for more information.
9160 * @param bridgeTag the bridge tag of the notification.
9161 * @return this object for method chaining
9162 */
9163 public WearableExtender setBridgeTag(String bridgeTag) {
9164 mBridgeTag = bridgeTag;
9165 return this;
9166 }
9167
9168 /**
9169 * Returns the bridge tag of the notification.
9170 * @return the bridge tag or null if not present.
9171 */
9172 public String getBridgeTag() {
9173 return mBridgeTag;
9174 }
9175
Griff Hazen61a9e862014-05-22 16:05:19 -07009176 private void setFlag(int mask, boolean value) {
9177 if (value) {
9178 mFlags |= mask;
9179 } else {
9180 mFlags &= ~mask;
9181 }
9182 }
9183 }
9184
9185 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009186 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
9187 * with car extensions:
9188 *
9189 * <ol>
9190 * <li>Create an {@link Notification.Builder}, setting any desired
9191 * properties.
9192 * <li>Create a {@link CarExtender}.
9193 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
9194 * {@link CarExtender}.
9195 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9196 * to apply the extensions to a notification.
9197 * </ol>
9198 *
9199 * <pre class="prettyprint">
9200 * Notification notification = new Notification.Builder(context)
9201 * ...
9202 * .extend(new CarExtender()
9203 * .set*(...))
9204 * .build();
9205 * </pre>
9206 *
9207 * <p>Car extensions can be accessed on an existing notification by using the
9208 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
9209 * to access values.
9210 */
9211 public static final class CarExtender implements Extender {
9212 private static final String TAG = "CarExtender";
9213
9214 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
9215 private static final String EXTRA_LARGE_ICON = "large_icon";
9216 private static final String EXTRA_CONVERSATION = "car_conversation";
9217 private static final String EXTRA_COLOR = "app_color";
9218
9219 private Bitmap mLargeIcon;
9220 private UnreadConversation mUnreadConversation;
9221 private int mColor = Notification.COLOR_DEFAULT;
9222
9223 /**
9224 * Create a {@link CarExtender} with default options.
9225 */
9226 public CarExtender() {
9227 }
9228
9229 /**
9230 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
9231 *
9232 * @param notif The notification from which to copy options.
9233 */
9234 public CarExtender(Notification notif) {
9235 Bundle carBundle = notif.extras == null ?
9236 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
9237 if (carBundle != null) {
9238 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
9239 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
9240
9241 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
9242 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
9243 }
9244 }
9245
9246 /**
9247 * Apply car extensions to a notification that is being built. This is typically called by
9248 * the {@link Notification.Builder#extend(Notification.Extender)}
9249 * method of {@link Notification.Builder}.
9250 */
9251 @Override
9252 public Notification.Builder extend(Notification.Builder builder) {
9253 Bundle carExtensions = new Bundle();
9254
9255 if (mLargeIcon != null) {
9256 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
9257 }
9258 if (mColor != Notification.COLOR_DEFAULT) {
9259 carExtensions.putInt(EXTRA_COLOR, mColor);
9260 }
9261
9262 if (mUnreadConversation != null) {
9263 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
9264 carExtensions.putBundle(EXTRA_CONVERSATION, b);
9265 }
9266
9267 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
9268 return builder;
9269 }
9270
9271 /**
9272 * Sets the accent color to use when Android Auto presents the notification.
9273 *
9274 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
9275 * to accent the displayed notification. However, not all colors are acceptable in an
9276 * automotive setting. This method can be used to override the color provided in the
9277 * notification in such a situation.
9278 */
Tor Norbye80756e32015-03-02 09:39:27 -08009279 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009280 mColor = color;
9281 return this;
9282 }
9283
9284 /**
9285 * Gets the accent color.
9286 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04009287 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009288 */
Tor Norbye80756e32015-03-02 09:39:27 -08009289 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009290 public int getColor() {
9291 return mColor;
9292 }
9293
9294 /**
9295 * Sets the large icon of the car notification.
9296 *
9297 * If no large icon is set in the extender, Android Auto will display the icon
9298 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
9299 *
9300 * @param largeIcon The large icon to use in the car notification.
9301 * @return This object for method chaining.
9302 */
9303 public CarExtender setLargeIcon(Bitmap largeIcon) {
9304 mLargeIcon = largeIcon;
9305 return this;
9306 }
9307
9308 /**
9309 * Gets the large icon used in this car notification, or null if no icon has been set.
9310 *
9311 * @return The large icon for the car notification.
9312 * @see CarExtender#setLargeIcon
9313 */
9314 public Bitmap getLargeIcon() {
9315 return mLargeIcon;
9316 }
9317
9318 /**
9319 * Sets the unread conversation in a message notification.
9320 *
9321 * @param unreadConversation The unread part of the conversation this notification conveys.
9322 * @return This object for method chaining.
9323 */
9324 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
9325 mUnreadConversation = unreadConversation;
9326 return this;
9327 }
9328
9329 /**
9330 * Returns the unread conversation conveyed by this notification.
9331 * @see #setUnreadConversation(UnreadConversation)
9332 */
9333 public UnreadConversation getUnreadConversation() {
9334 return mUnreadConversation;
9335 }
9336
9337 /**
9338 * A class which holds the unread messages from a conversation.
9339 */
9340 public static class UnreadConversation {
9341 private static final String KEY_AUTHOR = "author";
9342 private static final String KEY_TEXT = "text";
9343 private static final String KEY_MESSAGES = "messages";
9344 private static final String KEY_REMOTE_INPUT = "remote_input";
9345 private static final String KEY_ON_REPLY = "on_reply";
9346 private static final String KEY_ON_READ = "on_read";
9347 private static final String KEY_PARTICIPANTS = "participants";
9348 private static final String KEY_TIMESTAMP = "timestamp";
9349
9350 private final String[] mMessages;
9351 private final RemoteInput mRemoteInput;
9352 private final PendingIntent mReplyPendingIntent;
9353 private final PendingIntent mReadPendingIntent;
9354 private final String[] mParticipants;
9355 private final long mLatestTimestamp;
9356
9357 UnreadConversation(String[] messages, RemoteInput remoteInput,
9358 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
9359 String[] participants, long latestTimestamp) {
9360 mMessages = messages;
9361 mRemoteInput = remoteInput;
9362 mReadPendingIntent = readPendingIntent;
9363 mReplyPendingIntent = replyPendingIntent;
9364 mParticipants = participants;
9365 mLatestTimestamp = latestTimestamp;
9366 }
9367
9368 /**
9369 * Gets the list of messages conveyed by this notification.
9370 */
9371 public String[] getMessages() {
9372 return mMessages;
9373 }
9374
9375 /**
9376 * Gets the remote input that will be used to convey the response to a message list, or
9377 * null if no such remote input exists.
9378 */
9379 public RemoteInput getRemoteInput() {
9380 return mRemoteInput;
9381 }
9382
9383 /**
9384 * Gets the pending intent that will be triggered when the user replies to this
9385 * notification.
9386 */
9387 public PendingIntent getReplyPendingIntent() {
9388 return mReplyPendingIntent;
9389 }
9390
9391 /**
9392 * Gets the pending intent that Android Auto will send after it reads aloud all messages
9393 * in this object's message list.
9394 */
9395 public PendingIntent getReadPendingIntent() {
9396 return mReadPendingIntent;
9397 }
9398
9399 /**
9400 * Gets the participants in the conversation.
9401 */
9402 public String[] getParticipants() {
9403 return mParticipants;
9404 }
9405
9406 /**
9407 * Gets the firs participant in the conversation.
9408 */
9409 public String getParticipant() {
9410 return mParticipants.length > 0 ? mParticipants[0] : null;
9411 }
9412
9413 /**
9414 * Gets the timestamp of the conversation.
9415 */
9416 public long getLatestTimestamp() {
9417 return mLatestTimestamp;
9418 }
9419
9420 Bundle getBundleForUnreadConversation() {
9421 Bundle b = new Bundle();
9422 String author = null;
9423 if (mParticipants != null && mParticipants.length > 1) {
9424 author = mParticipants[0];
9425 }
9426 Parcelable[] messages = new Parcelable[mMessages.length];
9427 for (int i = 0; i < messages.length; i++) {
9428 Bundle m = new Bundle();
9429 m.putString(KEY_TEXT, mMessages[i]);
9430 m.putString(KEY_AUTHOR, author);
9431 messages[i] = m;
9432 }
9433 b.putParcelableArray(KEY_MESSAGES, messages);
9434 if (mRemoteInput != null) {
9435 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
9436 }
9437 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
9438 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
9439 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
9440 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
9441 return b;
9442 }
9443
9444 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
9445 if (b == null) {
9446 return null;
9447 }
9448 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
9449 String[] messages = null;
9450 if (parcelableMessages != null) {
9451 String[] tmp = new String[parcelableMessages.length];
9452 boolean success = true;
9453 for (int i = 0; i < tmp.length; i++) {
9454 if (!(parcelableMessages[i] instanceof Bundle)) {
9455 success = false;
9456 break;
9457 }
9458 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
9459 if (tmp[i] == null) {
9460 success = false;
9461 break;
9462 }
9463 }
9464 if (success) {
9465 messages = tmp;
9466 } else {
9467 return null;
9468 }
9469 }
9470
9471 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
9472 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
9473
9474 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
9475
9476 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
9477 if (participants == null || participants.length != 1) {
9478 return null;
9479 }
9480
9481 return new UnreadConversation(messages,
9482 remoteInput,
9483 onReply,
9484 onRead,
9485 participants, b.getLong(KEY_TIMESTAMP));
9486 }
9487 };
9488
9489 /**
9490 * Builder class for {@link CarExtender.UnreadConversation} objects.
9491 */
9492 public static class Builder {
9493 private final List<String> mMessages = new ArrayList<String>();
9494 private final String mParticipant;
9495 private RemoteInput mRemoteInput;
9496 private PendingIntent mReadPendingIntent;
9497 private PendingIntent mReplyPendingIntent;
9498 private long mLatestTimestamp;
9499
9500 /**
9501 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
9502 *
9503 * @param name The name of the other participant in the conversation.
9504 */
9505 public Builder(String name) {
9506 mParticipant = name;
9507 }
9508
9509 /**
9510 * Appends a new unread message to the list of messages for this conversation.
9511 *
9512 * The messages should be added from oldest to newest.
9513 *
9514 * @param message The text of the new unread message.
9515 * @return This object for method chaining.
9516 */
9517 public Builder addMessage(String message) {
9518 mMessages.add(message);
9519 return this;
9520 }
9521
9522 /**
9523 * Sets the pending intent and remote input which will convey the reply to this
9524 * notification.
9525 *
9526 * @param pendingIntent The pending intent which will be triggered on a reply.
9527 * @param remoteInput The remote input parcelable which will carry the reply.
9528 * @return This object for method chaining.
9529 *
9530 * @see CarExtender.UnreadConversation#getRemoteInput
9531 * @see CarExtender.UnreadConversation#getReplyPendingIntent
9532 */
9533 public Builder setReplyAction(
9534 PendingIntent pendingIntent, RemoteInput remoteInput) {
9535 mRemoteInput = remoteInput;
9536 mReplyPendingIntent = pendingIntent;
9537
9538 return this;
9539 }
9540
9541 /**
9542 * Sets the pending intent that will be sent once the messages in this notification
9543 * are read.
9544 *
9545 * @param pendingIntent The pending intent to use.
9546 * @return This object for method chaining.
9547 */
9548 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
9549 mReadPendingIntent = pendingIntent;
9550 return this;
9551 }
9552
9553 /**
9554 * Sets the timestamp of the most recent message in an unread conversation.
9555 *
9556 * If a messaging notification has been posted by your application and has not
9557 * yet been cancelled, posting a later notification with the same id and tag
9558 * but without a newer timestamp may result in Android Auto not displaying a
9559 * heads up notification for the later notification.
9560 *
9561 * @param timestamp The timestamp of the most recent message in the conversation.
9562 * @return This object for method chaining.
9563 */
9564 public Builder setLatestTimestamp(long timestamp) {
9565 mLatestTimestamp = timestamp;
9566 return this;
9567 }
9568
9569 /**
9570 * Builds a new unread conversation object.
9571 *
9572 * @return The new unread conversation object.
9573 */
9574 public UnreadConversation build() {
9575 String[] messages = mMessages.toArray(new String[mMessages.size()]);
9576 String[] participants = { mParticipant };
9577 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
9578 mReadPendingIntent, participants, mLatestTimestamp);
9579 }
9580 }
9581 }
9582
9583 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009584 * <p>Helper class to add Android TV extensions to notifications. To create a notification
9585 * with a TV extension:
9586 *
9587 * <ol>
9588 * <li>Create an {@link Notification.Builder}, setting any desired properties.
9589 * <li>Create a {@link TvExtender}.
9590 * <li>Set TV-specific properties using the {@code set} methods of
9591 * {@link TvExtender}.
9592 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9593 * to apply the extension to a notification.
9594 * </ol>
9595 *
9596 * <pre class="prettyprint">
9597 * Notification notification = new Notification.Builder(context)
9598 * ...
9599 * .extend(new TvExtender()
9600 * .set*(...))
9601 * .build();
9602 * </pre>
9603 *
9604 * <p>TV extensions can be accessed on an existing notification by using the
9605 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
9606 * to access values.
9607 *
9608 * @hide
9609 */
9610 @SystemApi
9611 public static final class TvExtender implements Extender {
9612 private static final String TAG = "TvExtender";
9613
9614 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
9615 private static final String EXTRA_FLAGS = "flags";
9616 private static final String EXTRA_CONTENT_INTENT = "content_intent";
9617 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009618 private static final String EXTRA_CHANNEL_ID = "channel_id";
Rhiannon Malia1a083932018-01-24 15:02:30 -08009619 private static final String EXTRA_SUPPRESS_SHOW_OVER_APPS = "suppressShowOverApps";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009620
9621 // Flags bitwise-ored to mFlags
9622 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
9623
9624 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009625 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009626 private PendingIntent mContentIntent;
9627 private PendingIntent mDeleteIntent;
Rhiannon Malia1a083932018-01-24 15:02:30 -08009628 private boolean mSuppressShowOverApps;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009629
9630 /**
9631 * Create a {@link TvExtender} with default options.
9632 */
9633 public TvExtender() {
9634 mFlags = FLAG_AVAILABLE_ON_TV;
9635 }
9636
9637 /**
9638 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
9639 *
9640 * @param notif The notification from which to copy options.
9641 */
9642 public TvExtender(Notification notif) {
9643 Bundle bundle = notif.extras == null ?
9644 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
9645 if (bundle != null) {
9646 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009647 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Rhiannon Malia1a083932018-01-24 15:02:30 -08009648 mSuppressShowOverApps = bundle.getBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009649 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
9650 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
9651 }
9652 }
9653
9654 /**
9655 * Apply a TV extension to a notification that is being built. This is typically called by
9656 * the {@link Notification.Builder#extend(Notification.Extender)}
9657 * method of {@link Notification.Builder}.
9658 */
9659 @Override
9660 public Notification.Builder extend(Notification.Builder builder) {
9661 Bundle bundle = new Bundle();
9662
9663 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009664 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Rhiannon Malia1a083932018-01-24 15:02:30 -08009665 bundle.putBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS, mSuppressShowOverApps);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009666 if (mContentIntent != null) {
9667 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
9668 }
9669
9670 if (mDeleteIntent != null) {
9671 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
9672 }
9673
9674 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
9675 return builder;
9676 }
9677
9678 /**
9679 * Returns true if this notification should be shown on TV. This method return true
9680 * if the notification was extended with a TvExtender.
9681 */
9682 public boolean isAvailableOnTv() {
9683 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
9684 }
9685
9686 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009687 * Specifies the channel the notification should be delivered on when shown on TV.
9688 * It can be different from the channel that the notification is delivered to when
9689 * posting on a non-TV device.
9690 */
9691 public TvExtender setChannel(String channelId) {
9692 mChannelId = channelId;
9693 return this;
9694 }
9695
9696 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04009697 * Specifies the channel the notification should be delivered on when shown on TV.
9698 * It can be different from the channel that the notification is delivered to when
9699 * posting on a non-TV device.
9700 */
9701 public TvExtender setChannelId(String channelId) {
9702 mChannelId = channelId;
9703 return this;
9704 }
9705
Jeff Sharkey000ce802017-04-29 13:13:27 -06009706 /** @removed */
9707 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009708 public String getChannel() {
9709 return mChannelId;
9710 }
9711
9712 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04009713 * Returns the id of the channel this notification posts to on TV.
9714 */
9715 public String getChannelId() {
9716 return mChannelId;
9717 }
9718
9719 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009720 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
9721 * If provided, it is used instead of the content intent specified
9722 * at the level of Notification.
9723 */
9724 public TvExtender setContentIntent(PendingIntent intent) {
9725 mContentIntent = intent;
9726 return this;
9727 }
9728
9729 /**
9730 * Returns the TV-specific content intent. If this method returns null, the
9731 * main content intent on the notification should be used.
9732 *
9733 * @see {@link Notification#contentIntent}
9734 */
9735 public PendingIntent getContentIntent() {
9736 return mContentIntent;
9737 }
9738
9739 /**
9740 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
9741 * by the user on TV. If provided, it is used instead of the delete intent specified
9742 * at the level of Notification.
9743 */
9744 public TvExtender setDeleteIntent(PendingIntent intent) {
9745 mDeleteIntent = intent;
9746 return this;
9747 }
9748
9749 /**
9750 * Returns the TV-specific delete intent. If this method returns null, the
9751 * main delete intent on the notification should be used.
9752 *
9753 * @see {@link Notification#deleteIntent}
9754 */
9755 public PendingIntent getDeleteIntent() {
9756 return mDeleteIntent;
9757 }
Rhiannon Malia1a083932018-01-24 15:02:30 -08009758
9759 /**
9760 * Specifies whether this notification should suppress showing a message over top of apps
9761 * outside of the launcher.
9762 */
9763 public TvExtender setSuppressShowOverApps(boolean suppress) {
9764 mSuppressShowOverApps = suppress;
9765 return this;
9766 }
9767
9768 /**
9769 * Returns true if this notification should not show messages over top of apps
9770 * outside of the launcher.
9771 */
9772 public boolean getSuppressShowOverApps() {
9773 return mSuppressShowOverApps;
9774 }
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009775 }
9776
9777 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07009778 * Get an array of Notification objects from a parcelable array bundle field.
9779 * Update the bundle to have a typed array so fetches in the future don't need
9780 * to do an array copy.
9781 */
9782 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
9783 Parcelable[] array = bundle.getParcelableArray(key);
9784 if (array instanceof Notification[] || array == null) {
9785 return (Notification[]) array;
9786 }
9787 Notification[] typedArray = Arrays.copyOf(array, array.length,
9788 Notification[].class);
9789 bundle.putParcelableArray(key, typedArray);
9790 return typedArray;
9791 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02009792
9793 private static class BuilderRemoteViews extends RemoteViews {
9794 public BuilderRemoteViews(Parcel parcel) {
9795 super(parcel);
9796 }
9797
Kenny Guy77320062014-08-27 21:37:15 +01009798 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
9799 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02009800 }
9801
9802 @Override
9803 public BuilderRemoteViews clone() {
9804 Parcel p = Parcel.obtain();
9805 writeToParcel(p, 0);
9806 p.setDataPosition(0);
9807 BuilderRemoteViews brv = new BuilderRemoteViews(p);
9808 p.recycle();
9809 return brv;
9810 }
9811 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08009812
Selim Cinek384804b2018-04-18 14:31:07 +08009813 /**
9814 * A result object where information about the template that was created is saved.
9815 */
9816 private static class TemplateBindResult {
Selim Cinek1c72fa02018-04-23 18:00:54 +08009817 int mIconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +08009818
9819 /**
Selim Cinek1c72fa02018-04-23 18:00:54 +08009820 * Get the margin end that needs to be added to any fields that may overlap
Selim Cinek384804b2018-04-18 14:31:07 +08009821 * with the right actions.
9822 */
Selim Cinek1c72fa02018-04-23 18:00:54 +08009823 public int getIconMarginEnd() {
9824 return mIconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +08009825 }
9826
Selim Cinek1c72fa02018-04-23 18:00:54 +08009827 public void setIconMarginEnd(int iconMarginEnd) {
9828 this.mIconMarginEnd = iconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +08009829 }
9830 }
9831
Adrian Roos70d7aa32017-01-11 15:39:06 -08009832 private static class StandardTemplateParams {
9833 boolean hasProgress = true;
9834 boolean ambient = false;
9835 CharSequence title;
9836 CharSequence text;
Selim Cinekafeed292017-12-12 17:32:44 -08009837 CharSequence headerTextSecondary;
Selim Cinekbee4e072018-05-21 22:06:43 -07009838 int maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek88188f22017-09-19 16:46:56 -07009839 boolean hideLargeIcon;
Selim Cinek384804b2018-04-18 14:31:07 +08009840 boolean hideReplyIcon;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009841
9842 final StandardTemplateParams reset() {
9843 hasProgress = true;
9844 ambient = false;
9845 title = null;
9846 text = null;
Selim Cinekafeed292017-12-12 17:32:44 -08009847 headerTextSecondary = null;
Selim Cinekbee4e072018-05-21 22:06:43 -07009848 maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009849 return this;
9850 }
9851
9852 final StandardTemplateParams hasProgress(boolean hasProgress) {
9853 this.hasProgress = hasProgress;
9854 return this;
9855 }
9856
9857 final StandardTemplateParams title(CharSequence title) {
9858 this.title = title;
9859 return this;
9860 }
9861
9862 final StandardTemplateParams text(CharSequence text) {
9863 this.text = text;
9864 return this;
9865 }
9866
Selim Cinekafeed292017-12-12 17:32:44 -08009867 final StandardTemplateParams headerTextSecondary(CharSequence text) {
9868 this.headerTextSecondary = text;
9869 return this;
9870 }
9871
Selim Cinek384804b2018-04-18 14:31:07 +08009872 final StandardTemplateParams hideLargeIcon(boolean hideLargeIcon) {
9873 this.hideLargeIcon = hideLargeIcon;
Selim Cinek88188f22017-09-19 16:46:56 -07009874 return this;
9875 }
9876
Selim Cinek384804b2018-04-18 14:31:07 +08009877 final StandardTemplateParams hideReplyIcon(boolean hideReplyIcon) {
9878 this.hideReplyIcon = hideReplyIcon;
Selim Cinek88188f22017-09-19 16:46:56 -07009879 return this;
9880 }
9881
Adrian Roos70d7aa32017-01-11 15:39:06 -08009882 final StandardTemplateParams ambient(boolean ambient) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08009883 Preconditions.checkState(title == null && text == null, "must set ambient before text");
Adrian Roos70d7aa32017-01-11 15:39:06 -08009884 this.ambient = ambient;
9885 return this;
9886 }
9887
9888 final StandardTemplateParams fillTextsFrom(Builder b) {
9889 Bundle extras = b.mN.extras;
Lucas Dupin06c5e642017-09-13 16:34:58 -07009890 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE), ambient);
9891
9892 // Big text notifications should contain their content when viewed in ambient mode.
9893 CharSequence text = extras.getCharSequence(EXTRA_BIG_TEXT);
9894 if (!ambient || TextUtils.isEmpty(text)) {
9895 text = extras.getCharSequence(EXTRA_TEXT);
9896 }
9897 this.text = b.processLegacyText(text, ambient);
Adrian Roos70d7aa32017-01-11 15:39:06 -08009898 return this;
9899 }
Selim Cinekbee4e072018-05-21 22:06:43 -07009900
9901 /**
9902 * Set the maximum lines of remote input history lines allowed.
9903 * @param maxRemoteInputHistory The number of lines.
9904 * @return The builder for method chaining.
9905 */
9906 public StandardTemplateParams setMaxRemoteInputHistory(int maxRemoteInputHistory) {
9907 this.maxRemoteInputHistory = maxRemoteInputHistory;
9908 return this;
9909 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08009910 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009911}