blob: 143baef33cbe3b64d1d2d0dc6bb59d4643a61f64 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Lucas Dupina291d192018-06-07 13:59:42 -070019import static com.android.internal.util.ContrastColorUtil.satisfiesTextContrast;
Selim Cinek389edcd2017-05-11 19:16:44 -070020
Tor Norbye80756e32015-03-02 09:39:27 -080021import android.annotation.ColorInt;
Tor Norbye7b9c9122013-05-30 16:48:33 -070022import android.annotation.DrawableRes;
Gus Prevas9cc96602018-10-11 11:24:23 -040023import android.annotation.IdRes;
Tor Norbyed9273d62013-05-30 15:59:53 -070024import android.annotation.IntDef;
Alex Hillsfd590442016-10-07 09:52:44 -040025import android.annotation.NonNull;
Selim Cinek88188f22017-09-19 16:46:56 -070026import android.annotation.Nullable;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060027import android.annotation.RequiresPermission;
Daniel Sandler01df1c62014-06-09 10:54:01 -040028import android.annotation.SdkConstant;
29import android.annotation.SdkConstant.SdkConstantType;
Julia Reynoldse46bb372016-03-17 11:05:58 -040030import android.annotation.SystemApi;
Mathew Inwood61e8ae62018-08-14 14:17:44 +010031import android.annotation.UnsupportedAppUsage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.content.Context;
33import android.content.Intent;
Kenny Guy77320062014-08-27 21:37:15 +010034import android.content.pm.ApplicationInfo;
Dan Sandler732bd6c2016-04-12 14:20:32 -040035import android.content.pm.PackageManager;
Christoph Studer4600f9b2014-07-22 22:44:43 +020036import android.content.pm.PackageManager.NameNotFoundException;
Julia Reynolds13d898c2017-02-02 12:22:05 -050037import android.content.pm.ShortcutInfo;
Jorim Jaggief72a192014-08-26 21:57:46 +020038import android.content.res.ColorStateList;
Anthony Chenad4d1582017-04-10 16:07:58 -070039import android.content.res.Configuration;
40import android.content.res.Resources;
Gus Prevas9cc96602018-10-11 11:24:23 -040041import android.content.res.TypedArray;
Joe Onoratoef1e7762010-09-17 18:38:38 -040042import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010043import android.graphics.Canvas;
Adrian Roosc1a80b02016-04-05 14:54:55 -070044import android.graphics.Color;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010045import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010046import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040047import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040048import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070049import android.media.AudioManager;
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -080050import android.media.PlayerBase;
Jeff Browndba34ba2014-06-24 20:46:03 -070051import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040053import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020054import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050055import android.os.Bundle;
Dianne Hackborn98305522017-05-05 17:53:53 -070056import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.os.Parcel;
58import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040059import android.os.SystemClock;
Selim Cinek6743c0b2017-01-18 18:24:01 -080060import android.os.SystemProperties;
Jeff Sharkey6d515712012-09-20 16:06:08 -070061import android.os.UserHandle;
Adrian Roosc1a80b02016-04-05 14:54:55 -070062import android.text.BidiFormatter;
Selim Cinek60a54252016-02-26 17:03:25 -080063import android.text.SpannableStringBuilder;
64import android.text.Spanned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.text.TextUtils;
Selim Cinek60a54252016-02-26 17:03:25 -080066import android.text.style.AbsoluteSizeSpan;
Selim Cinek89991a22016-03-07 19:51:54 -080067import android.text.style.CharacterStyle;
Selim Cinek981962e2016-07-20 20:41:58 -070068import android.text.style.ForegroundColorSpan;
Selim Cinek60a54252016-02-26 17:03:25 -080069import android.text.style.RelativeSizeSpan;
70import android.text.style.TextAppearanceSpan;
Svet Ganovddb94882016-06-23 19:55:24 -070071import android.util.ArraySet;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040072import android.util.Log;
Tony Mak638430e2018-10-08 19:19:10 +010073import android.util.Pair;
Dan Sandler50128532015-12-08 15:42:41 -050074import android.util.SparseArray;
Yi Jin6b514142017-10-30 14:54:12 -070075import android.util.proto.ProtoOutputStream;
Griff Hazen61a9e862014-05-22 16:05:19 -070076import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080077import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080078import android.view.View;
Selim Cinek954cc232016-05-20 13:29:23 -070079import android.view.ViewGroup;
Jeff Sharkey1c400132011-08-05 14:50:13 -070080import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import android.widget.RemoteViews;
82
Griff Hazen959591e2014-05-15 22:26:18 -070083import com.android.internal.R;
Selim Cinek389edcd2017-05-11 19:16:44 -070084import com.android.internal.annotations.VisibleForTesting;
Svet Ganovddb94882016-06-23 19:55:24 -070085import com.android.internal.util.ArrayUtils;
Lucas Dupina291d192018-06-07 13:59:42 -070086import com.android.internal.util.ContrastColorUtil;
Adrian Roos0bc3f6a2017-03-06 11:54:05 -080087import com.android.internal.util.Preconditions;
Griff Hazen959591e2014-05-15 22:26:18 -070088
Tor Norbyed9273d62013-05-30 15:59:53 -070089import java.lang.annotation.Retention;
90import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020091import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050092import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070093import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070094import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070095import java.util.List;
Julia Reynolds7217dc92018-03-07 12:12:09 -050096import java.util.Objects;
Dan Sandler50128532015-12-08 15:42:41 -050097import java.util.Set;
Jeff Sharkey23b31182018-04-18 21:32:12 -060098import java.util.function.Consumer;
Joe Onorato561d3852010-11-20 18:09:34 -080099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100/**
101 * A class that represents how a persistent notification is to be presented to
102 * the user using the {@link android.app.NotificationManager}.
103 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800104 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
105 * easier to construct Notifications.</p>
106 *
Joe Fernandez558459f2011-10-13 16:47:36 -0700107 * <div class="special reference">
108 * <h3>Developer Guides</h3>
109 * <p>For a guide to creating notifications, read the
110 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
111 * developer guide.</p>
112 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 */
114public class Notification implements Parcelable
115{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -0400116 private static final String TAG = "Notification";
117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -0400119 * An activity that provides a user interface for adjusting notification preferences for its
Julia Reynolds3aedded2017-03-31 14:42:09 -0400120 * containing application.
Daniel Sandler01df1c62014-06-09 10:54:01 -0400121 */
122 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
123 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
124 = "android.intent.category.NOTIFICATION_PREFERENCES";
125
126 /**
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500127 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
128 * contain a {@link NotificationChannel#getId() channel id} that can be used to narrow down
Julia Reynolds3aedded2017-03-31 14:42:09 -0400129 * what settings should be shown in the target app.
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500130 */
131 public static final String EXTRA_CHANNEL_ID = "android.intent.extra.CHANNEL_ID";
132
133 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -0400134 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds005c8b92017-08-24 10:35:53 -0400135 * contain a {@link NotificationChannelGroup#getId() group id} that can be used to narrow down
136 * what settings should be shown in the target app.
137 */
138 public static final String EXTRA_CHANNEL_GROUP_ID = "android.intent.extra.CHANNEL_GROUP_ID";
139
140 /**
141 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds3aedded2017-03-31 14:42:09 -0400142 * contain the tag provided to {@link NotificationManager#notify(String, int, Notification)}
143 * that can be used to narrow down what settings should be shown in the target app.
144 */
145 public static final String EXTRA_NOTIFICATION_TAG = "android.intent.extra.NOTIFICATION_TAG";
146
147 /**
148 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
149 * contain the id provided to {@link NotificationManager#notify(String, int, Notification)}
150 * that can be used to narrow down what settings should be shown in the target app.
151 */
152 public static final String EXTRA_NOTIFICATION_ID = "android.intent.extra.NOTIFICATION_ID";
153
154 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 * Use all default values (where applicable).
156 */
157 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 /**
160 * Use the default notification sound. This will ignore any given
161 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500162 *
Chris Wren47c20a12014-06-18 17:27:29 -0400163 * <p>
164 * A notification that is noisy is more likely to be presented as a heads-up notification.
165 * </p>
166 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500168 */
169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 public static final int DEFAULT_SOUND = 1;
171
172 /**
173 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500174 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700175 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500176 *
Chris Wren47c20a12014-06-18 17:27:29 -0400177 * <p>
178 * A notification that vibrates is more likely to be presented as a heads-up notification.
179 * </p>
180 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500182 */
183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 /**
187 * Use the default notification lights. This will ignore the
188 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
189 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500190 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500192 */
193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200197 * Maximum length of CharSequences accepted by Builder and friends.
198 *
199 * <p>
200 * Avoids spamming the system with overly large strings such as full e-mails.
201 */
202 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
203
204 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800205 * Maximum entries of reply text that are accepted by Builder and friends.
206 */
207 private static final int MAX_REPLY_HISTORY = 5;
208
Tony Mak09db2ea2018-06-27 18:12:48 +0100209 /**
210 * Maximum numbers of action buttons in a notification.
211 * @hide
212 */
213 public static final int MAX_ACTION_BUTTONS = 3;
Selim Cinekde4de0e2018-01-24 16:21:07 -0800214
215 /**
216 * If the notification contained an unsent draft for a RemoteInput when the user clicked on it,
217 * we're adding the draft as a String extra to the {@link #contentIntent} using this key.
218 *
219 * <p>Apps may use this extra to prepopulate text fields in the app, where the user usually
220 * sends messages.</p>
221 */
222 public static final String EXTRA_REMOTE_INPUT_DRAFT = "android.remoteInputDraft";
223
Adrian Roose458aa82015-12-08 16:17:19 -0800224 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500225 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800226 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500227 * Default value: {@link System#currentTimeMillis() Now}.
228 *
229 * Choose a timestamp that will be most relevant to the user. For most finite events, this
230 * corresponds to the time the event happened (or will happen, in the case of events that have
231 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800232 * timestamped according to when the activity began.
233 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500234 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800235 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500236 * <ul>
237 * <li>Notification of a new chat message should be stamped when the message was received.</li>
238 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
239 * <li>Notification of a completed file download should be stamped when the download finished.</li>
240 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
241 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
242 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800243 * </ul>
244 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700245 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
246 * anymore by default and must be opted into by using
247 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 */
249 public long when;
250
251 /**
Selim Cinekb85f36fd2016-04-20 18:46:36 -0700252 * The creation time of the notification
253 */
254 private long creationTime;
255
256 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400258 *
259 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 */
Dan Sandler86647982015-05-13 23:41:13 -0400261 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700262 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 public int icon;
264
265 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800266 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
267 * leave it at its default value of 0.
268 *
269 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700270 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800271 */
272 public int iconLevel;
273
274 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500275 * The number of events that this notification represents. For example, in a new mail
276 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800277 *
Julia Reynolds30331982017-04-27 10:12:50 -0400278 * The system may or may not use this field to modify the appearance of the notification.
Julia Reynolds13d898c2017-02-02 12:22:05 -0500279 * Starting with {@link android.os.Build.VERSION_CODES#O}, the number may be displayed as a
280 * badge icon in Launchers that support badging.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 */
Julia Reynolds30331982017-04-27 10:12:50 -0400282 public int number = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283
284 /**
285 * The intent to execute when the expanded status entry is clicked. If
286 * this is an activity, it must include the
287 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800288 * that you take care of task management as described in the
289 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800290 * Stack</a> document. In particular, make sure to read the notification section
291 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
292 * Notifications</a> for the correct ways to launch an application from a
293 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 */
295 public PendingIntent contentIntent;
296
297 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500298 * The intent to execute when the notification is explicitly dismissed by the user, either with
299 * the "Clear All" button or by swiping it away individually.
300 *
301 * This probably shouldn't be launching an activity since several of those will be sent
302 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 */
304 public PendingIntent deleteIntent;
305
306 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700307 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800308 *
Chris Wren47c20a12014-06-18 17:27:29 -0400309 * <p>
310 * The system UI may choose to display a heads-up notification, instead of
311 * launching this intent, while the user is using the device.
312 * </p>
313 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800314 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400315 */
316 public PendingIntent fullScreenIntent;
317
318 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400319 * Text that summarizes this notification for accessibility services.
320 *
321 * As of the L release, this text is no longer shown on screen, but it is still useful to
322 * accessibility services (where it serves as an audible announcement of the notification's
323 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400324 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800325 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 */
327 public CharSequence tickerText;
328
329 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400330 * Formerly, a view showing the {@link #tickerText}.
331 *
332 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400333 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400334 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800335 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400336
337 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400338 * The view that will represent this notification in the notification list (which is pulled
339 * down from the status bar).
340 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500341 * As of N, this field may be null. The notification view is determined by the inputs
342 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400343 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400345 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 public RemoteViews contentView;
347
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400348 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400349 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400350 * opportunity to show more detail. The system UI may choose to show this
351 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400352 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500353 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400354 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
355 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400356 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400357 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400358 public RemoteViews bigContentView;
359
Chris Wren8fd39ec2014-02-27 17:43:26 -0500360
361 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400362 * A medium-format version of {@link #contentView}, providing the Notification an
363 * opportunity to add action buttons to contentView. At its discretion, the system UI may
364 * choose to show this as a heads-up notification, which will pop up so the user can see
365 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400366 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500367 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400368 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
369 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500370 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400371 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500372 public RemoteViews headsUpContentView;
373
Julia Reynoldsfc640012018-02-21 12:25:27 -0500374 private boolean mUsesStandardHeader;
375
376 private static final ArraySet<Integer> STANDARD_LAYOUTS = new ArraySet<>();
377 static {
378 STANDARD_LAYOUTS.add(R.layout.notification_template_material_base);
379 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_base);
380 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_picture);
381 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_text);
382 STANDARD_LAYOUTS.add(R.layout.notification_template_material_inbox);
383 STANDARD_LAYOUTS.add(R.layout.notification_template_material_messaging);
384 STANDARD_LAYOUTS.add(R.layout.notification_template_material_media);
385 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_media);
386 STANDARD_LAYOUTS.add(R.layout.notification_template_ambient_header);
387 STANDARD_LAYOUTS.add(R.layout.notification_template_header);
388 STANDARD_LAYOUTS.add(R.layout.notification_template_material_ambient);
389 }
390
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400391 /**
Dan Sandler86647982015-05-13 23:41:13 -0400392 * A large bitmap to be shown in the notification content area.
393 *
394 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 */
Dan Sandler86647982015-05-13 23:41:13 -0400396 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800397 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398
399 /**
400 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500401 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400403 * A notification that is noisy is more likely to be presented as a heads-up notification.
404 * </p>
405 *
406 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500407 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500409 * @deprecated use {@link NotificationChannel#getSound()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500411 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 public Uri sound;
413
414 /**
415 * Use this constant as the value for audioStreamType to request that
416 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700417 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400418 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500419 * @deprecated Use {@link NotificationChannel#getAudioAttributes()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700421 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 public static final int STREAM_DEFAULT = -1;
423
424 /**
425 * The audio stream type to use when playing the sound.
426 * Should be one of the STREAM_ constants from
427 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400428 *
429 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700431 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 public int audioStreamType = STREAM_DEFAULT;
433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400435 * The default value of {@link #audioAttributes}.
436 */
437 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
438 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
439 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
440 .build();
441
442 /**
443 * The {@link AudioAttributes audio attributes} to use when playing the sound.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500444 *
445 * @deprecated use {@link NotificationChannel#getAudioAttributes()} instead.
John Spurlockc0650f022014-07-19 13:22:39 -0400446 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500447 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -0400448 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
449
450 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500451 * The pattern with which to vibrate.
452 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 * <p>
454 * To vibrate the default pattern, see {@link #defaults}.
455 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500456 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 * @see android.os.Vibrator#vibrate(long[],int)
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500458 * @deprecated use {@link NotificationChannel#getVibrationPattern()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500460 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 public long[] vibrate;
462
463 /**
464 * The color of the led. The hardware will do its best approximation.
465 *
466 * @see #FLAG_SHOW_LIGHTS
467 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500468 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 */
Tor Norbye80756e32015-03-02 09:39:27 -0800470 @ColorInt
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500471 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 public int ledARGB;
473
474 /**
475 * The number of milliseconds for the LED to be on while it's flashing.
476 * The hardware will do its best approximation.
477 *
478 * @see #FLAG_SHOW_LIGHTS
479 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500480 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500482 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 public int ledOnMS;
484
485 /**
486 * The number of milliseconds for the LED to be off while it's flashing.
487 * The hardware will do its best approximation.
488 *
489 * @see #FLAG_SHOW_LIGHTS
490 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500491 *
492 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500494 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 public int ledOffMS;
496
497 /**
498 * Specifies which values should be taken from the defaults.
499 * <p>
500 * To set, OR the desired from {@link #DEFAULT_SOUND},
501 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
502 * values, use {@link #DEFAULT_ALL}.
503 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500504 *
505 * @deprecated use {@link NotificationChannel#getSound()} and
506 * {@link NotificationChannel#shouldShowLights()} and
507 * {@link NotificationChannel#shouldVibrate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500509 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 public int defaults;
511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 /**
513 * Bit to be bitwise-ored into the {@link #flags} field that should be
514 * set if you want the LED on for this notification.
515 * <ul>
516 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
517 * or 0 for both ledOnMS and ledOffMS.</li>
518 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
519 * <li>To flash the LED, pass the number of milliseconds that it should
520 * be on and off to ledOnMS and ledOffMS.</li>
521 * </ul>
522 * <p>
523 * Since hardware varies, you are not guaranteed that any of the values
Ricardo Loo Forondaf8f6d0a2018-01-25 08:42:43 -0800524 * you pass are honored exactly. Use the system defaults if possible
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 * because they will be set to values that work on any given hardware.
526 * <p>
527 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500528 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500529 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500531 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
533
534 /**
535 * Bit to be bitwise-ored into the {@link #flags} field that should be
536 * set if this notification is in reference to something that is ongoing,
537 * like a phone call. It should not be set if this notification is in
538 * reference to something that happened at a particular point in time,
539 * like a missed phone call.
540 */
541 public static final int FLAG_ONGOING_EVENT = 0x00000002;
542
543 /**
544 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700545 * the audio will be repeated until the notification is
546 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 */
548 public static final int FLAG_INSISTENT = 0x00000004;
549
550 /**
551 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700552 * set if you would only like the sound, vibrate and ticker to be played
553 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 */
555 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
556
557 /**
558 * Bit to be bitwise-ored into the {@link #flags} field that should be
559 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500560 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 */
562 public static final int FLAG_AUTO_CANCEL = 0x00000010;
563
564 /**
565 * Bit to be bitwise-ored into the {@link #flags} field that should be
566 * set if the notification should not be canceled when the user clicks
567 * the Clear all button.
568 */
569 public static final int FLAG_NO_CLEAR = 0x00000020;
570
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700571 /**
572 * Bit to be bitwise-ored into the {@link #flags} field that should be
573 * set if this notification represents a currently running service. This
574 * will normally be set for you by {@link Service#startForeground}.
575 */
576 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
577
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400578 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500579 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800580 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500581 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400582 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700583 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500584 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400585
Griff Hazendfcb0802014-02-11 12:00:00 -0800586 /**
587 * Bit to be bitswise-ored into the {@link #flags} field that should be
588 * set if this notification is relevant to the current device only
589 * and it is not recommended that it bridge to other devices.
590 */
591 public static final int FLAG_LOCAL_ONLY = 0x00000100;
592
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700593 /**
594 * Bit to be bitswise-ored into the {@link #flags} field that should be
595 * set if this notification is the group summary for a group of notifications.
596 * Grouped notifications may display in a cluster or stack on devices which
597 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
598 */
599 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
600
Julia Reynoldse46bb372016-03-17 11:05:58 -0400601 /**
602 * Bit to be bitswise-ored into the {@link #flags} field that should be
603 * set if this notification is the group summary for an auto-group of notifications.
604 *
605 * @hide
606 */
607 @SystemApi
608 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
609
Julia Reynolds4db59552017-06-30 13:34:01 -0400610 /**
611 * @hide
612 */
613 public static final int FLAG_CAN_COLORIZE = 0x00000800;
614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 public int flags;
616
Tor Norbyed9273d62013-05-30 15:59:53 -0700617 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700618 @IntDef(prefix = { "PRIORITY_" }, value = {
619 PRIORITY_DEFAULT,
620 PRIORITY_LOW,
621 PRIORITY_MIN,
622 PRIORITY_HIGH,
623 PRIORITY_MAX
624 })
Tor Norbyed9273d62013-05-30 15:59:53 -0700625 @Retention(RetentionPolicy.SOURCE)
626 public @interface Priority {}
627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500629 * Default notification {@link #priority}. If your application does not prioritize its own
630 * notifications, use this value for all notifications.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500631 *
632 * @deprecated use {@link NotificationManager#IMPORTANCE_DEFAULT} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500633 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500634 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500635 public static final int PRIORITY_DEFAULT = 0;
636
637 /**
638 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
639 * items smaller, or at a different position in the list, compared with your app's
640 * {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500641 *
642 * @deprecated use {@link NotificationManager#IMPORTANCE_LOW} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500643 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500644 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500645 public static final int PRIORITY_LOW = -1;
646
647 /**
648 * Lowest {@link #priority}; these items might not be shown to the user except under special
649 * circumstances, such as detailed notification logs.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500650 *
651 * @deprecated use {@link NotificationManager#IMPORTANCE_MIN} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500652 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500653 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500654 public static final int PRIORITY_MIN = -2;
655
656 /**
657 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
658 * show these items larger, or at a different position in notification lists, compared with
659 * your app's {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500660 *
661 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500662 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500663 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500664 public static final int PRIORITY_HIGH = 1;
665
666 /**
667 * Highest {@link #priority}, for your application's most important items that require the
668 * user's prompt attention or input.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500669 *
670 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500671 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500672 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500673 public static final int PRIORITY_MAX = 2;
674
675 /**
676 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800677 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500678 * Priority is an indication of how much of the user's valuable attention should be consumed by
679 * this notification. Low-priority notifications may be hidden from the user in certain
680 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500681 * system will make a determination about how to interpret this priority when presenting
682 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400683 *
684 * <p>
685 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
686 * as a heads-up notification.
687 * </p>
688 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500689 * @deprecated use {@link NotificationChannel#getImportance()} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500690 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700691 @Priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500692 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500693 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800694
Dan Sandler26e81cf2014-05-06 10:01:27 -0400695 /**
696 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
697 * to be applied by the standard Style templates when presenting this notification.
698 *
699 * The current template design constructs a colorful header image by overlaying the
700 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
701 * ignored.
702 */
Tor Norbye80756e32015-03-02 09:39:27 -0800703 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400704 public int color = COLOR_DEFAULT;
705
706 /**
707 * Special value of {@link #color} telling the system not to decorate this notification with
708 * any special color but instead use default colors when presenting this notification.
709 */
Tor Norbye80756e32015-03-02 09:39:27 -0800710 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400711 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600712
713 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800714 * Special value of {@link #color} used as a place holder for an invalid color.
Selim Cinek99104832017-01-25 14:47:33 -0800715 * @hide
Adrian Roos4ff3b122016-02-01 12:26:13 -0800716 */
717 @ColorInt
Selim Cinek99104832017-01-25 14:47:33 -0800718 public static final int COLOR_INVALID = 1;
Adrian Roos4ff3b122016-02-01 12:26:13 -0800719
720 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700721 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
722 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600723 * lockscreen).
724 *
725 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
726 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
727 * shown in all situations, but the contents are only available if the device is unlocked for
728 * the appropriate user.
729 *
730 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
731 * can be read even in an "insecure" context (that is, above a secure lockscreen).
732 * To modify the public version of this notification—for example, to redact some portions—see
733 * {@link Builder#setPublicVersion(Notification)}.
734 *
735 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
736 * and ticker until the user has bypassed the lockscreen.
737 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600738 public @Visibility int visibility;
739
740 /** @hide */
741 @IntDef(prefix = { "VISIBILITY_" }, value = {
742 VISIBILITY_PUBLIC,
743 VISIBILITY_PRIVATE,
744 VISIBILITY_SECRET,
745 })
746 @Retention(RetentionPolicy.SOURCE)
747 public @interface Visibility {}
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600748
Griff Hazenfc3922d2014-08-20 11:56:44 -0700749 /**
750 * Notification visibility: Show this notification in its entirety on all lockscreens.
751 *
752 * {@see #visibility}
753 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600754 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700755
756 /**
757 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
758 * private information on secure lockscreens.
759 *
760 * {@see #visibility}
761 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600762 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700763
764 /**
765 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
766 *
767 * {@see #visibility}
768 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600769 public static final int VISIBILITY_SECRET = -1;
770
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500771 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400772 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500773 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400774 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500775
776 /**
Adora Zhangaa90e872018-03-12 14:07:50 -0700777 * Notification category: map turn-by-turn navigation.
778 */
779 public static final String CATEGORY_NAVIGATION = "navigation";
780
781 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400782 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500783 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400784 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500785
786 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400787 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500788 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400789 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500790
791 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400792 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500793 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400794 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500795
796 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400797 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500798 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400799 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500800
801 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400802 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500803 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400804 public static final String CATEGORY_ALARM = "alarm";
805
806 /**
807 * Notification category: progress of a long-running background operation.
808 */
809 public static final String CATEGORY_PROGRESS = "progress";
810
811 /**
812 * Notification category: social network or sharing update.
813 */
814 public static final String CATEGORY_SOCIAL = "social";
815
816 /**
817 * Notification category: error in background operation or authentication status.
818 */
819 public static final String CATEGORY_ERROR = "err";
820
821 /**
822 * Notification category: media transport control for playback.
823 */
824 public static final String CATEGORY_TRANSPORT = "transport";
825
826 /**
827 * Notification category: system or device status update. Reserved for system use.
828 */
829 public static final String CATEGORY_SYSTEM = "sys";
830
831 /**
832 * Notification category: indication of running background service.
833 */
834 public static final String CATEGORY_SERVICE = "service";
835
836 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400837 * Notification category: a specific, timely recommendation for a single thing.
838 * For example, a news app might want to recommend a news story it believes the user will
839 * want to read next.
840 */
841 public static final String CATEGORY_RECOMMENDATION = "recommendation";
842
843 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400844 * Notification category: ongoing information about device or contextual status.
845 */
846 public static final String CATEGORY_STATUS = "status";
847
848 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400849 * Notification category: user-scheduled reminder.
850 */
851 public static final String CATEGORY_REMINDER = "reminder";
852
853 /**
Adora Zhangaa90e872018-03-12 14:07:50 -0700854 * Notification category: extreme car emergencies.
855 * @hide
856 */
857 @SystemApi
858 public static final String CATEGORY_CAR_EMERGENCY = "car_emergency";
859
860 /**
861 * Notification category: car warnings.
862 * @hide
863 */
864 @SystemApi
865 public static final String CATEGORY_CAR_WARNING = "car_warning";
866
867 /**
868 * Notification category: general car system information.
869 * @hide
870 */
871 @SystemApi
872 public static final String CATEGORY_CAR_INFORMATION = "car_information";
873
874 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400875 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
876 * that best describes this Notification. May be used by the system for ranking and filtering.
877 */
878 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500879
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100880 @UnsupportedAppUsage
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700881 private String mGroupKey;
882
883 /**
884 * Get the key used to group this notification into a cluster or stack
885 * with other notifications on devices which support such rendering.
886 */
887 public String getGroup() {
888 return mGroupKey;
889 }
890
891 private String mSortKey;
892
893 /**
894 * Get a sort key that orders this notification among other notifications from the
895 * same package. This can be useful if an external sort was already applied and an app
896 * would like to preserve this. Notifications will be sorted lexicographically using this
897 * value, although providing different priorities in addition to providing sort key may
898 * cause this value to be ignored.
899 *
900 * <p>This sort key can also be used to order members of a notification group. See
901 * {@link Builder#setGroup}.
902 *
903 * @see String#compareTo(String)
904 */
905 public String getSortKey() {
906 return mSortKey;
907 }
908
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500909 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400910 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400911 * <p>
912 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
913 * APIs, and are intended to be used by
914 * {@link android.service.notification.NotificationListenerService} implementations to extract
915 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500916 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400917 public Bundle extras = new Bundle();
918
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400919 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700920 * All pending intents in the notification as the system needs to be able to access them but
921 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700922 * custom parcelable objects.
923 *
924 * @hide
925 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100926 @UnsupportedAppUsage
Felipe Lemedd85da62016-06-28 11:29:54 -0700927 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700928
929 /**
Dianne Hackborn98305522017-05-05 17:53:53 -0700930 * Token identifying the notification that is applying doze/bgcheck whitelisting to the
931 * pending intents inside of it, so only those will get the behavior.
932 *
933 * @hide
934 */
Adrian Roosfb921842017-10-26 14:49:56 +0200935 private IBinder mWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -0700936
937 /**
938 * Must be set by a process to start associating tokens with Notification objects
939 * coming in to it. This is set by NotificationManagerService.
940 *
941 * @hide
942 */
943 static public IBinder processWhitelistToken;
944
945 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400946 * {@link #extras} key: this is the title of the notification,
947 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
948 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500949 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400950
951 /**
952 * {@link #extras} key: this is the title of the notification when shown in expanded form,
953 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
954 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400955 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400956
957 /**
958 * {@link #extras} key: this is the main text payload, as supplied to
959 * {@link Builder#setContentText(CharSequence)}.
960 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500961 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400962
963 /**
964 * {@link #extras} key: this is a third line of text, as supplied to
965 * {@link Builder#setSubText(CharSequence)}.
966 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400967 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400968
969 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800970 * {@link #extras} key: this is the remote input history, as supplied to
971 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700972 *
973 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
974 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
975 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
976 * notifications once the other party has responded).
977 *
978 * The extra with this key is of type CharSequence[] and contains the most recent entry at
979 * the 0 index, the second most recent at the 1 index, etc.
980 *
981 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800982 */
983 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
984
985 /**
Kenny Guya0f6de82018-04-06 16:20:16 +0100986 * {@link #extras} key: boolean as supplied to
987 * {@link Builder#setShowRemoteInputSpinner(boolean)}.
988 *
989 * If set to true, then the view displaying the remote input history from
990 * {@link Builder#setRemoteInputHistory(CharSequence[])} will have a progress spinner.
991 *
992 * @see Builder#setShowRemoteInputSpinner(boolean)
993 * @hide
994 */
995 public static final String EXTRA_SHOW_REMOTE_INPUT_SPINNER = "android.remoteInputSpinner";
996
997 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +0100998 * {@link #extras} key: boolean as supplied to
999 * {@link Builder#setHideSmartReplies(boolean)}.
1000 *
1001 * If set to true, then any smart reply buttons will be hidden.
1002 *
1003 * @see Builder#setHideSmartReplies(boolean)
1004 * @hide
1005 */
1006 public static final String EXTRA_HIDE_SMART_REPLIES = "android.hideSmartReplies";
1007
1008 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001009 * {@link #extras} key: this is a small piece of additional text as supplied to
1010 * {@link Builder#setContentInfo(CharSequence)}.
1011 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001012 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001013
1014 /**
1015 * {@link #extras} key: this is a line of summary information intended to be shown
1016 * alongside expanded notifications, as supplied to (e.g.)
1017 * {@link BigTextStyle#setSummaryText(CharSequence)}.
1018 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001019 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001020
1021 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02001022 * {@link #extras} key: this is the longer text shown in the big form of a
1023 * {@link BigTextStyle} notification, as supplied to
1024 * {@link BigTextStyle#bigText(CharSequence)}.
1025 */
1026 public static final String EXTRA_BIG_TEXT = "android.bigText";
1027
1028 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001029 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
1030 * supplied to {@link Builder#setSmallIcon(int)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -04001031 *
1032 * @deprecated Use {@link #getSmallIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001033 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04001034 @Deprecated
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001035 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001036
1037 /**
1038 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
1039 * notification payload, as
1040 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -04001041 *
1042 * @deprecated Use {@link #getLargeIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001043 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04001044 @Deprecated
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001045 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001046
1047 /**
1048 * {@link #extras} key: this is a bitmap to be used instead of the one from
1049 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
1050 * shown in its expanded form, as supplied to
1051 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
1052 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001053 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001054
1055 /**
1056 * {@link #extras} key: this is the progress value supplied to
1057 * {@link Builder#setProgress(int, int, boolean)}.
1058 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001059 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001060
1061 /**
1062 * {@link #extras} key: this is the maximum value supplied to
1063 * {@link Builder#setProgress(int, int, boolean)}.
1064 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001065 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001066
1067 /**
1068 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
1069 * {@link Builder#setProgress(int, int, boolean)}.
1070 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001071 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001072
1073 /**
1074 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
1075 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
1076 * {@link Builder#setUsesChronometer(boolean)}.
1077 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001078 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001079
1080 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08001081 * {@link #extras} key: whether the chronometer set on the notification should count down
1082 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -07001083 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -08001084 */
Adrian Roos96b7e202016-05-17 13:50:38 -07001085 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -08001086
1087 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001088 * {@link #extras} key: whether {@link #when} should be shown,
1089 * as supplied to {@link Builder#setShowWhen(boolean)}.
1090 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001091 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001092
1093 /**
1094 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
1095 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
1096 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001097 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001098
1099 /**
1100 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
1101 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
1102 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001103 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -04001104
1105 /**
1106 * {@link #extras} key: A string representing the name of the specific
1107 * {@link android.app.Notification.Style} used to create this notification.
1108 */
Chris Wren91ad5632013-06-05 15:05:57 -04001109 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001110
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001111 /**
Chris Wrene6c48932014-09-29 17:19:27 -04001112 * {@link #extras} key: A String array containing the people that this notification relates to,
1113 * each of which was supplied to {@link Builder#addPerson(String)}.
Selim Cineke7238dd2017-12-14 17:48:32 -08001114 *
1115 * @deprecated the actual objects are now in {@link #EXTRA_PEOPLE_LIST}
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001116 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001117 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001118
1119 /**
Selim Cineke7238dd2017-12-14 17:48:32 -08001120 * {@link #extras} key: An arrayList of {@link Person} objects containing the people that
1121 * this notification relates to.
1122 */
1123 public static final String EXTRA_PEOPLE_LIST = "android.people.list";
1124
1125 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04001126 * Allow certain system-generated notifications to appear before the device is provisioned.
1127 * Only available to notifications coming from the android package.
1128 * @hide
1129 */
Maurice Lam96c10032017-03-29 15:42:38 -07001130 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001131 @RequiresPermission(android.Manifest.permission.NOTIFICATION_DURING_SETUP)
John Spurlockfd7f1e02014-03-18 16:41:57 -04001132 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
1133
1134 /**
Robin Leed107af62018-04-27 13:55:56 +02001135 * {@link #extras} key:
1136 * flat {@link String} representation of a {@link android.content.ContentUris content URI}
1137 * pointing to an image that can be displayed in the background when the notification is
1138 * selected. Used on television platforms. The URI must point to an image stream suitable for
1139 * passing into {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001140 * BitmapFactory.decodeStream}; all other content types will be ignored.
Jose Limae9e3b3b2014-05-18 23:44:50 -07001141 */
1142 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
1143
1144 /**
Dan Sandler842dd772014-05-15 09:36:47 -04001145 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -07001146 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -04001147 * {@link android.app.Notification.MediaStyle} notification.
1148 */
1149 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
1150
1151 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +01001152 * {@link #extras} key: the indices of actions to be shown in the compact view,
1153 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
1154 */
1155 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
1156
Christoph Studer943aa672014-08-03 20:31:16 +02001157 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04001158 * {@link #extras} key: the username to be displayed for all messages sent by the user including
1159 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -07001160 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1161 * {@link CharSequence}
Selim Cinekcb8b9852017-12-15 18:01:52 -08001162 *
1163 * @deprecated use {@link #EXTRA_MESSAGING_PERSON}
Alex Hillsfc737de2016-03-23 17:33:02 -04001164 */
1165 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
1166
1167 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08001168 * {@link #extras} key: the person to be displayed for all messages sent by the user including
1169 * direct replies
1170 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1171 * {@link Person}
1172 */
1173 public static final String EXTRA_MESSAGING_PERSON = "android.messagingUser";
1174
1175 /**
Adrian Roos96b7e202016-05-17 13:50:38 -07001176 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -04001177 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -04001178 */
Alex Hillsd9b04d92016-04-11 16:38:16 -04001179 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -04001180
1181 /**
1182 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
1183 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -07001184 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1185 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -04001186 */
1187 public static final String EXTRA_MESSAGES = "android.messages";
1188
1189 /**
Adrian Roos437cd562017-01-18 15:47:03 -08001190 * {@link #extras} key: an array of
1191 * {@link android.app.Notification.MessagingStyle#addHistoricMessage historic}
1192 * {@link android.app.Notification.MessagingStyle.Message} bundles provided by a
1193 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1194 * array of bundles.
1195 */
1196 public static final String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
1197
1198 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08001199 * {@link #extras} key: whether the {@link android.app.Notification.MessagingStyle} notification
1200 * represents a group conversation.
1201 */
1202 public static final String EXTRA_IS_GROUP_CONVERSATION = "android.isGroupConversation";
1203
1204 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08001205 * {@link #extras} key: whether the notification should be colorized as
1206 * supplied to {@link Builder#setColorized(boolean)}}.
1207 */
1208 public static final String EXTRA_COLORIZED = "android.colorized";
1209
1210 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001211 * @hide
1212 */
1213 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
1214
Selim Cinek247fa012016-02-18 09:50:48 -08001215 /**
1216 * @hide
1217 */
1218 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
1219
Shane Brennan472a3b32016-12-12 15:28:10 -08001220 /**
Selim Cinekd0426622017-07-11 13:19:59 +02001221 * @hide
1222 */
1223 public static final String EXTRA_REDUCED_IMAGES = "android.reduced.images";
1224
1225 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001226 * {@link #extras} key: the audio contents of this notification.
1227 *
1228 * This is for use when rendering the notification on an audio-focused interface;
1229 * the audio contents are a complete sound sample that contains the contents/body of the
1230 * notification. This may be used in substitute of a Text-to-Speech reading of the
1231 * notification. For example if the notification represents a voice message this should point
1232 * to the audio of that message.
1233 *
1234 * The data stored under this key should be a String representation of a Uri that contains the
1235 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1236 *
1237 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1238 * has a field for holding data URI. That field can be used for audio.
1239 * See {@code Message#setData}.
1240 *
1241 * Example usage:
1242 * <pre>
1243 * {@code
1244 * Notification.Builder myBuilder = (build your Notification as normal);
1245 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1246 * }
1247 * </pre>
1248 */
1249 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1250
Dan Sandler80eaa592016-04-14 23:34:54 -04001251 /** @hide */
1252 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001253 @RequiresPermission(android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME)
Dan Sandler732bd6c2016-04-12 14:20:32 -04001254 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1255
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001256 /**
Julia Reynolds3c7de112018-03-28 09:33:13 -04001257 * This is set on the notifications shown by system_server about apps running foreground
1258 * services. It indicates that the notification should be shown
1259 * only if any of the given apps do not already have a properly tagged
1260 * {@link #FLAG_FOREGROUND_SERVICE} notification currently visible to the user.
1261 * This is a string array of all package names of the apps.
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001262 * @hide
1263 */
1264 public static final String EXTRA_FOREGROUND_APPS = "android.foregroundApps";
1265
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001266 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04001267 private Icon mSmallIcon;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001268 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04001269 private Icon mLargeIcon;
1270
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001271 @UnsupportedAppUsage
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001272 private String mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001273 private long mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001274
Julia Reynolds13d898c2017-02-02 12:22:05 -05001275 private String mShortcutId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04001276 private CharSequence mSettingsText;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001277
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04001278 private PendingIntent mAppOverlayIntent;
1279
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001280 /** @hide */
1281 @IntDef(prefix = { "GROUP_ALERT_" }, value = {
1282 GROUP_ALERT_ALL, GROUP_ALERT_CHILDREN, GROUP_ALERT_SUMMARY
1283 })
1284 @Retention(RetentionPolicy.SOURCE)
1285 public @interface GroupAlertBehavior {}
1286
1287 /**
1288 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all notifications in a
1289 * group with sound or vibration ought to make sound or vibrate (respectively), so this
1290 * notification will not be muted when it is in a group.
1291 */
1292 public static final int GROUP_ALERT_ALL = 0;
1293
1294 /**
1295 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all children
1296 * notification in a group should be silenced (no sound or vibration) even if they are posted
1297 * to a {@link NotificationChannel} that has sound and/or vibration. Use this constant to
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001298 * mute this notification if this notification is a group child. This must be applied to all
1299 * children notifications you want to mute.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001300 *
1301 * <p> For example, you might want to use this constant if you post a number of children
1302 * notifications at once (say, after a periodic sync), and only need to notify the user
1303 * audibly once.
1304 */
1305 public static final int GROUP_ALERT_SUMMARY = 1;
1306
1307 /**
1308 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that the summary
1309 * notification in a group should be silenced (no sound or vibration) even if they are
1310 * posted to a {@link NotificationChannel} that has sound and/or vibration. Use this constant
1311 * to mute this notification if this notification is a group summary.
1312 *
1313 * <p>For example, you might want to use this constant if only the children notifications
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001314 * in your group have content and the summary is only used to visually group notifications
1315 * rather than to alert the user that new information is available.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001316 */
1317 public static final int GROUP_ALERT_CHILDREN = 2;
1318
Julia Reynolds2f431e22017-06-07 14:12:09 +00001319 private int mGroupAlertBehavior = GROUP_ALERT_ALL;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001320
Julia Reynolds13d898c2017-02-02 12:22:05 -05001321 /**
1322 * If this notification is being shown as a badge, always show as a number.
1323 */
1324 public static final int BADGE_ICON_NONE = 0;
1325
1326 /**
1327 * If this notification is being shown as a badge, use the {@link #getSmallIcon()} to
1328 * represent this notification.
1329 */
1330 public static final int BADGE_ICON_SMALL = 1;
1331
1332 /**
1333 * If this notification is being shown as a badge, use the {@link #getLargeIcon()} to
1334 * represent this notification.
1335 */
1336 public static final int BADGE_ICON_LARGE = 2;
Julia Reynolds7d5b4da2017-03-20 10:18:49 -04001337 private int mBadgeIcon = BADGE_ICON_NONE;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001338
Chris Wren51c75102013-07-16 20:49:17 -04001339 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001340 * Structure to encapsulate a named action that can be shown as part of this notification.
1341 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1342 * selected by the user.
1343 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001344 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1345 * or {@link Notification.Builder#addAction(Notification.Action)}
1346 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001347 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001348 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001349 /**
1350 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1351 * {@link RemoteInput}s.
1352 *
1353 * This is intended for {@link RemoteInput}s that only accept data, meaning
1354 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
1355 * is null or empty, and {@link RemoteInput#getAllowedDataTypes is non-null and not
1356 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1357 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1358 *
1359 * You can test if a RemoteInput matches these constraints using
1360 * {@link RemoteInput#isDataOnly}.
1361 */
1362 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1363
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001364 /**
1365 * {@link }: No semantic action defined.
1366 */
1367 public static final int SEMANTIC_ACTION_NONE = 0;
1368
1369 /**
1370 * {@code SemanticAction}: Reply to a conversation, chat, group, or wherever replies
1371 * may be appropriate.
1372 */
1373 public static final int SEMANTIC_ACTION_REPLY = 1;
1374
1375 /**
1376 * {@code SemanticAction}: Mark content as read.
1377 */
1378 public static final int SEMANTIC_ACTION_MARK_AS_READ = 2;
1379
1380 /**
1381 * {@code SemanticAction}: Mark content as unread.
1382 */
1383 public static final int SEMANTIC_ACTION_MARK_AS_UNREAD = 3;
1384
1385 /**
1386 * {@code SemanticAction}: Delete the content associated with the notification. This
1387 * could mean deleting an email, message, etc.
1388 */
1389 public static final int SEMANTIC_ACTION_DELETE = 4;
1390
1391 /**
1392 * {@code SemanticAction}: Archive the content associated with the notification. This
1393 * could mean archiving an email, message, etc.
1394 */
1395 public static final int SEMANTIC_ACTION_ARCHIVE = 5;
1396
1397 /**
1398 * {@code SemanticAction}: Mute the content associated with the notification. This could
1399 * mean silencing a conversation or currently playing media.
1400 */
1401 public static final int SEMANTIC_ACTION_MUTE = 6;
1402
1403 /**
1404 * {@code SemanticAction}: Unmute the content associated with the notification. This could
1405 * mean un-silencing a conversation or currently playing media.
1406 */
1407 public static final int SEMANTIC_ACTION_UNMUTE = 7;
1408
1409 /**
1410 * {@code SemanticAction}: Mark content with a thumbs up.
1411 */
1412 public static final int SEMANTIC_ACTION_THUMBS_UP = 8;
1413
1414 /**
1415 * {@code SemanticAction}: Mark content with a thumbs down.
1416 */
1417 public static final int SEMANTIC_ACTION_THUMBS_DOWN = 9;
1418
Kodlee Yincda5b092018-02-15 15:34:53 -08001419 /**
1420 * {@code SemanticAction}: Call a contact, group, etc.
1421 */
1422 public static final int SEMANTIC_ACTION_CALL = 10;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001423
Griff Hazen959591e2014-05-15 22:26:18 -07001424 private final Bundle mExtras;
Mathew Inwood8c854f82018-09-14 12:35:36 +01001425 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Dan Sandler86647982015-05-13 23:41:13 -04001426 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001427 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001428 private boolean mAllowGeneratedReplies = true;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001429 private final @SemanticAction int mSemanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001430
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001431 /**
1432 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001433 *
1434 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001435 */
Dan Sandler86647982015-05-13 23:41:13 -04001436 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001437 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001438
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001439 /**
1440 * Title of the action.
1441 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001442 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001443
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001444 /**
1445 * Intent to send when the user invokes this action. May be null, in which case the action
1446 * may be rendered in a disabled presentation by the system UI.
1447 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001448 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001449
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001450 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001451 if (in.readInt() != 0) {
1452 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001453 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1454 icon = mIcon.getResId();
1455 }
Dan Sandler86647982015-05-13 23:41:13 -04001456 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001457 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1458 if (in.readInt() == 1) {
1459 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1460 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001461 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001462 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001463 mAllowGeneratedReplies = in.readInt() == 1;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001464 mSemanticAction = in.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001465 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001466
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001467 /**
Dan Sandler86647982015-05-13 23:41:13 -04001468 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001469 */
Dan Sandler86647982015-05-13 23:41:13 -04001470 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001471 public Action(int icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001472 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true,
1473 SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001474 }
1475
Adrian Roos7af53622016-10-12 13:44:05 -07001476 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001477 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001478 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1479 @SemanticAction int semanticAction) {
Dan Sandler86647982015-05-13 23:41:13 -04001480 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001481 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1482 this.icon = icon.getResId();
1483 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001484 this.title = title;
1485 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001486 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001487 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001488 this.mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001489 this.mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001490 }
1491
1492 /**
Dan Sandler86647982015-05-13 23:41:13 -04001493 * Return an icon representing the action.
1494 */
1495 public Icon getIcon() {
1496 if (mIcon == null && icon != 0) {
1497 // you snuck an icon in here without using the builder; let's try to keep it
1498 mIcon = Icon.createWithResource("", icon);
1499 }
1500 return mIcon;
1501 }
1502
1503 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001504 * Get additional metadata carried around with this Action.
1505 */
1506 public Bundle getExtras() {
1507 return mExtras;
1508 }
1509
1510 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001511 * Return whether the platform should automatically generate possible replies for this
1512 * {@link Action}
1513 */
1514 public boolean getAllowGeneratedReplies() {
1515 return mAllowGeneratedReplies;
1516 }
1517
1518 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001519 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001520 * May return null if no remote inputs were added. Only returns inputs which accept
1521 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001522 */
1523 public RemoteInput[] getRemoteInputs() {
1524 return mRemoteInputs;
1525 }
1526
1527 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001528 * Returns the {@code SemanticAction} associated with this {@link Action}. A
1529 * {@code SemanticAction} denotes what an {@link Action}'s {@link PendingIntent} will do
1530 * (eg. reply, mark as read, delete, etc).
1531 */
1532 public @SemanticAction int getSemanticAction() {
1533 return mSemanticAction;
1534 }
1535
1536 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001537 * Get the list of inputs to be collected from the user that ONLY accept data when this
1538 * action is sent. These remote inputs are guaranteed to return true on a call to
1539 * {@link RemoteInput#isDataOnly}.
1540 *
Shane Brennanf670d6c2017-04-25 13:05:45 -07001541 * Returns null if there are no data-only remote inputs.
Shane Brennan472a3b32016-12-12 15:28:10 -08001542 *
1543 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1544 * of non-textual RemoteInputs do not access these remote inputs.
1545 */
1546 public RemoteInput[] getDataOnlyRemoteInputs() {
1547 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1548 }
1549
1550 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001551 * Builder class for {@link Action} objects.
1552 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001553 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001554 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001555 private final CharSequence mTitle;
1556 private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001557 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001558 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001559 private ArrayList<RemoteInput> mRemoteInputs;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001560 private @SemanticAction int mSemanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001561
1562 /**
1563 * Construct a new builder for {@link Action} object.
1564 * @param icon icon to show for this action
1565 * @param title the title of the action
1566 * @param intent the {@link PendingIntent} to fire when users trigger this action
1567 */
Dan Sandler86647982015-05-13 23:41:13 -04001568 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001569 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001570 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001571 }
1572
1573 /**
1574 * Construct a new builder for {@link Action} object.
1575 * @param icon icon to show for this action
1576 * @param title the title of the action
1577 * @param intent the {@link PendingIntent} to fire when users trigger this action
1578 */
1579 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001580 this(icon, title, intent, new Bundle(), null, true, SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001581 }
1582
1583 /**
1584 * Construct a new builder for {@link Action} object using the fields from an
1585 * {@link Action}.
1586 * @param action the action to read fields from.
1587 */
1588 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001589 this(action.getIcon(), action.title, action.actionIntent,
1590 new Bundle(action.mExtras), action.getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001591 action.getAllowGeneratedReplies(), action.getSemanticAction());
Griff Hazen959591e2014-05-15 22:26:18 -07001592 }
1593
Dan Sandler86647982015-05-13 23:41:13 -04001594 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001595 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1596 @SemanticAction int semanticAction) {
Griff Hazen959591e2014-05-15 22:26:18 -07001597 mIcon = icon;
1598 mTitle = title;
1599 mIntent = intent;
1600 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001601 if (remoteInputs != null) {
1602 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1603 Collections.addAll(mRemoteInputs, remoteInputs);
1604 }
Adrian Roos7af53622016-10-12 13:44:05 -07001605 mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001606 mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001607 }
1608
1609 /**
1610 * Merge additional metadata into this builder.
1611 *
1612 * <p>Values within the Bundle will replace existing extras values in this Builder.
1613 *
1614 * @see Notification.Action#extras
1615 */
1616 public Builder addExtras(Bundle extras) {
1617 if (extras != null) {
1618 mExtras.putAll(extras);
1619 }
1620 return this;
1621 }
1622
1623 /**
1624 * Get the metadata Bundle used by this Builder.
1625 *
1626 * <p>The returned Bundle is shared with this Builder.
1627 */
1628 public Bundle getExtras() {
1629 return mExtras;
1630 }
1631
1632 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001633 * Add an input to be collected from the user when this action is sent.
1634 * Response values can be retrieved from the fired intent by using the
1635 * {@link RemoteInput#getResultsFromIntent} function.
1636 * @param remoteInput a {@link RemoteInput} to add to the action
1637 * @return this object for method chaining
1638 */
1639 public Builder addRemoteInput(RemoteInput remoteInput) {
1640 if (mRemoteInputs == null) {
1641 mRemoteInputs = new ArrayList<RemoteInput>();
1642 }
1643 mRemoteInputs.add(remoteInput);
1644 return this;
1645 }
1646
1647 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001648 * Set whether the platform should automatically generate possible replies to add to
1649 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1650 * {@link RemoteInput}, this has no effect.
1651 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1652 * otherwise
1653 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001654 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001655 */
1656 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1657 mAllowGeneratedReplies = allowGeneratedReplies;
1658 return this;
1659 }
1660
1661 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001662 * Sets the {@code SemanticAction} for this {@link Action}. A
1663 * {@code SemanticAction} denotes what an {@link Action}'s
1664 * {@link PendingIntent} will do (eg. reply, mark as read, delete, etc).
1665 * @param semanticAction a SemanticAction defined within {@link Action} with
1666 * {@code SEMANTIC_ACTION_} prefixes
1667 * @return this object for method chaining
1668 */
1669 public Builder setSemanticAction(@SemanticAction int semanticAction) {
1670 mSemanticAction = semanticAction;
1671 return this;
1672 }
1673
1674 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001675 * Apply an extender to this action builder. Extenders may be used to add
1676 * metadata or change options on this builder.
1677 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001678 public Builder extend(Extender extender) {
1679 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001680 return this;
1681 }
1682
1683 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001684 * Combine all of the options that have been set and return a new {@link Action}
1685 * object.
1686 * @return the built action
1687 */
1688 public Action build() {
Shane Brennan472a3b32016-12-12 15:28:10 -08001689 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1690 RemoteInput[] previousDataInputs =
1691 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001692 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001693 for (RemoteInput input : previousDataInputs) {
1694 dataOnlyInputs.add(input);
1695 }
1696 }
1697 List<RemoteInput> textInputs = new ArrayList<>();
1698 if (mRemoteInputs != null) {
1699 for (RemoteInput input : mRemoteInputs) {
1700 if (input.isDataOnly()) {
1701 dataOnlyInputs.add(input);
1702 } else {
1703 textInputs.add(input);
1704 }
1705 }
1706 }
1707 if (!dataOnlyInputs.isEmpty()) {
1708 RemoteInput[] dataInputsArr =
1709 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1710 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1711 }
1712 RemoteInput[] textInputsArr = textInputs.isEmpty()
1713 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1714 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001715 mAllowGeneratedReplies, mSemanticAction);
Griff Hazen959591e2014-05-15 22:26:18 -07001716 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001717 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001718
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001719 @Override
1720 public Action clone() {
1721 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001722 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001723 title,
1724 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001725 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001726 getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001727 getAllowGeneratedReplies(),
1728 getSemanticAction());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001729 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001730
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001731 @Override
1732 public int describeContents() {
1733 return 0;
1734 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001735
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001736 @Override
1737 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001738 final Icon ic = getIcon();
1739 if (ic != null) {
1740 out.writeInt(1);
1741 ic.writeToParcel(out, 0);
1742 } else {
1743 out.writeInt(0);
1744 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001745 TextUtils.writeToParcel(title, out, flags);
1746 if (actionIntent != null) {
1747 out.writeInt(1);
1748 actionIntent.writeToParcel(out, flags);
1749 } else {
1750 out.writeInt(0);
1751 }
Griff Hazen959591e2014-05-15 22:26:18 -07001752 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001753 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001754 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001755 out.writeInt(mSemanticAction);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001756 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001757
Griff Hazen959591e2014-05-15 22:26:18 -07001758 public static final Parcelable.Creator<Action> CREATOR =
1759 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001760 public Action createFromParcel(Parcel in) {
1761 return new Action(in);
1762 }
1763 public Action[] newArray(int size) {
1764 return new Action[size];
1765 }
1766 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001767
1768 /**
1769 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1770 * metadata or change options on an action builder.
1771 */
1772 public interface Extender {
1773 /**
1774 * Apply this extender to a notification action builder.
1775 * @param builder the builder to be modified.
1776 * @return the build object for chaining.
1777 */
1778 public Builder extend(Builder builder);
1779 }
1780
1781 /**
1782 * Wearable extender for notification actions. To add extensions to an action,
1783 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1784 * the {@code WearableExtender()} constructor and apply it to a
1785 * {@link android.app.Notification.Action.Builder} using
1786 * {@link android.app.Notification.Action.Builder#extend}.
1787 *
1788 * <pre class="prettyprint">
1789 * Notification.Action action = new Notification.Action.Builder(
1790 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001791 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001792 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001793 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001794 */
1795 public static final class WearableExtender implements Extender {
1796 /** Notification action extra which contains wearable extensions */
1797 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1798
Pete Gastaf6781d2014-10-07 15:17:05 -04001799 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001800 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001801 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1802 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1803 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001804
1805 // Flags bitwise-ored to mFlags
1806 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001807 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001808 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001809
1810 // Default value for flags integer
1811 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1812
1813 private int mFlags = DEFAULT_FLAGS;
1814
Pete Gastaf6781d2014-10-07 15:17:05 -04001815 private CharSequence mInProgressLabel;
1816 private CharSequence mConfirmLabel;
1817 private CharSequence mCancelLabel;
1818
Griff Hazen61a9e862014-05-22 16:05:19 -07001819 /**
1820 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1821 * options.
1822 */
1823 public WearableExtender() {
1824 }
1825
1826 /**
1827 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1828 * wearable options present in an existing notification action.
1829 * @param action the notification action to inspect.
1830 */
1831 public WearableExtender(Action action) {
1832 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1833 if (wearableBundle != null) {
1834 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001835 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1836 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1837 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001838 }
1839 }
1840
1841 /**
1842 * Apply wearable extensions to a notification action that is being built. This is
1843 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1844 * method of {@link android.app.Notification.Action.Builder}.
1845 */
1846 @Override
1847 public Action.Builder extend(Action.Builder builder) {
1848 Bundle wearableBundle = new Bundle();
1849
1850 if (mFlags != DEFAULT_FLAGS) {
1851 wearableBundle.putInt(KEY_FLAGS, mFlags);
1852 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001853 if (mInProgressLabel != null) {
1854 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1855 }
1856 if (mConfirmLabel != null) {
1857 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1858 }
1859 if (mCancelLabel != null) {
1860 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1861 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001862
1863 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1864 return builder;
1865 }
1866
1867 @Override
1868 public WearableExtender clone() {
1869 WearableExtender that = new WearableExtender();
1870 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001871 that.mInProgressLabel = this.mInProgressLabel;
1872 that.mConfirmLabel = this.mConfirmLabel;
1873 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001874 return that;
1875 }
1876
1877 /**
1878 * Set whether this action is available when the wearable device is not connected to
1879 * a companion device. The user can still trigger this action when the wearable device is
1880 * offline, but a visual hint will indicate that the action may not be available.
1881 * Defaults to true.
1882 */
1883 public WearableExtender setAvailableOffline(boolean availableOffline) {
1884 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1885 return this;
1886 }
1887
1888 /**
1889 * Get whether this action is available when the wearable device is not connected to
1890 * a companion device. The user can still trigger this action when the wearable device is
1891 * offline, but a visual hint will indicate that the action may not be available.
1892 * Defaults to true.
1893 */
1894 public boolean isAvailableOffline() {
1895 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1896 }
1897
1898 private void setFlag(int mask, boolean value) {
1899 if (value) {
1900 mFlags |= mask;
1901 } else {
1902 mFlags &= ~mask;
1903 }
1904 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001905
1906 /**
1907 * Set a label to display while the wearable is preparing to automatically execute the
1908 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1909 *
1910 * @param label the label to display while the action is being prepared to execute
1911 * @return this object for method chaining
1912 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001913 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001914 public WearableExtender setInProgressLabel(CharSequence label) {
1915 mInProgressLabel = label;
1916 return this;
1917 }
1918
1919 /**
1920 * Get the label to display while the wearable is preparing to automatically execute
1921 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1922 *
1923 * @return the label to display while the action is being prepared to execute
1924 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001925 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001926 public CharSequence getInProgressLabel() {
1927 return mInProgressLabel;
1928 }
1929
1930 /**
1931 * Set a label to display to confirm that the action should be executed.
1932 * This is usually an imperative verb like "Send".
1933 *
1934 * @param label the label to confirm the action should be executed
1935 * @return this object for method chaining
1936 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001937 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001938 public WearableExtender setConfirmLabel(CharSequence label) {
1939 mConfirmLabel = label;
1940 return this;
1941 }
1942
1943 /**
1944 * Get the label to display to confirm that the action should be executed.
1945 * This is usually an imperative verb like "Send".
1946 *
1947 * @return the label to confirm the action should be executed
1948 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001949 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001950 public CharSequence getConfirmLabel() {
1951 return mConfirmLabel;
1952 }
1953
1954 /**
1955 * Set a label to display to cancel the action.
1956 * This is usually an imperative verb, like "Cancel".
1957 *
1958 * @param label the label to display to cancel the action
1959 * @return this object for method chaining
1960 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001961 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001962 public WearableExtender setCancelLabel(CharSequence label) {
1963 mCancelLabel = label;
1964 return this;
1965 }
1966
1967 /**
1968 * Get the label to display to cancel the action.
1969 * This is usually an imperative verb like "Cancel".
1970 *
1971 * @return the label to display to cancel the action
1972 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001973 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001974 public CharSequence getCancelLabel() {
1975 return mCancelLabel;
1976 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001977
1978 /**
1979 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1980 * platform that it can generate the appropriate transitions.
1981 * @param hintLaunchesActivity {@code true} if the content intent will launch
1982 * an activity and transitions should be generated, false otherwise.
1983 * @return this object for method chaining
1984 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001985 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001986 boolean hintLaunchesActivity) {
1987 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1988 return this;
1989 }
1990
1991 /**
1992 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1993 * platform that it can generate the appropriate transitions
1994 * @return {@code true} if the content intent will launch an activity and transitions
1995 * should be generated, false otherwise. The default value is {@code false} if this was
1996 * never set.
1997 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001998 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001999 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
2000 }
Alex Hills9f087612016-06-07 09:08:59 -04002001
2002 /**
2003 * Set a hint that this Action should be displayed inline.
2004 *
2005 * @param hintDisplayInline {@code true} if action should be displayed inline, false
2006 * otherwise
2007 * @return this object for method chaining
2008 */
2009 public WearableExtender setHintDisplayActionInline(
2010 boolean hintDisplayInline) {
2011 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
2012 return this;
2013 }
2014
2015 /**
2016 * Get a hint that this Action should be displayed inline.
2017 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08002018 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04002019 * otherwise. The default value is {@code false} if this was never set.
2020 */
2021 public boolean getHintDisplayActionInline() {
2022 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
2023 }
Griff Hazen61a9e862014-05-22 16:05:19 -07002024 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002025
2026 /**
2027 * Provides meaning to an {@link Action} that hints at what the associated
2028 * {@link PendingIntent} will do. For example, an {@link Action} with a
2029 * {@link PendingIntent} that replies to a text message notification may have the
2030 * {@link #SEMANTIC_ACTION_REPLY} {@code SemanticAction} set within it.
2031 *
2032 * @hide
2033 */
2034 @IntDef(prefix = { "SEMANTIC_ACTION_" }, value = {
2035 SEMANTIC_ACTION_NONE,
2036 SEMANTIC_ACTION_REPLY,
2037 SEMANTIC_ACTION_MARK_AS_READ,
2038 SEMANTIC_ACTION_MARK_AS_UNREAD,
2039 SEMANTIC_ACTION_DELETE,
2040 SEMANTIC_ACTION_ARCHIVE,
2041 SEMANTIC_ACTION_MUTE,
2042 SEMANTIC_ACTION_UNMUTE,
2043 SEMANTIC_ACTION_THUMBS_UP,
Kodlee Yincda5b092018-02-15 15:34:53 -08002044 SEMANTIC_ACTION_THUMBS_DOWN,
2045 SEMANTIC_ACTION_CALL
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002046 })
2047 @Retention(RetentionPolicy.SOURCE)
2048 public @interface SemanticAction {}
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002049 }
2050
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002051 /**
2052 * Array of all {@link Action} structures attached to this notification by
2053 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
2054 * {@link android.service.notification.NotificationListenerService} that provide an alternative
2055 * interface for invoking actions.
2056 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05002057 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002058
2059 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002060 * Replacement version of this notification whose content will be shown
2061 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
2062 * and {@link #VISIBILITY_PUBLIC}.
2063 */
2064 public Notification publicVersion;
2065
2066 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002067 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08002068 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002069 */
2070 public Notification()
2071 {
2072 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002073 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002074 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075 }
2076
2077 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002078 * @hide
2079 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002080 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002081 public Notification(Context context, int icon, CharSequence tickerText, long when,
2082 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
2083 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04002084 new Builder(context)
2085 .setWhen(when)
2086 .setSmallIcon(icon)
2087 .setTicker(tickerText)
2088 .setContentTitle(contentTitle)
2089 .setContentText(contentText)
2090 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
2091 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002092 }
2093
2094 /**
2095 * Constructs a Notification object with the information needed to
2096 * have a status bar icon without the standard expanded view.
2097 *
2098 * @param icon The resource id of the icon to put in the status bar.
2099 * @param tickerText The text that flows by in the status bar when the notification first
2100 * activates.
2101 * @param when The time to show in the time field. In the System.currentTimeMillis
2102 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08002103 *
2104 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002105 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002106 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 public Notification(int icon, CharSequence tickerText, long when)
2108 {
2109 this.icon = icon;
2110 this.tickerText = tickerText;
2111 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002112 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002113 }
2114
2115 /**
2116 * Unflatten the notification from a parcel.
2117 */
Svet Ganovddb94882016-06-23 19:55:24 -07002118 @SuppressWarnings("unchecked")
2119 public Notification(Parcel parcel) {
2120 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
2121 // intents in extras are always written as the last entry.
2122 readFromParcelImpl(parcel);
2123 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002124 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07002125 }
2126
2127 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128 {
2129 int version = parcel.readInt();
2130
Adrian Roosfb921842017-10-26 14:49:56 +02002131 mWhitelistToken = parcel.readStrongBinder();
2132 if (mWhitelistToken == null) {
2133 mWhitelistToken = processWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -07002134 }
2135 // Propagate this token to all pending intents that are unmarshalled from the parcel.
Adrian Roosfb921842017-10-26 14:49:56 +02002136 parcel.setClassCookie(PendingIntent.class, mWhitelistToken);
Dianne Hackborn98305522017-05-05 17:53:53 -07002137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002138 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002139 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04002140 if (parcel.readInt() != 0) {
2141 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04002142 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
2143 icon = mSmallIcon.getResId();
2144 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002145 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146 number = parcel.readInt();
2147 if (parcel.readInt() != 0) {
2148 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2149 }
2150 if (parcel.readInt() != 0) {
2151 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2152 }
2153 if (parcel.readInt() != 0) {
2154 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2155 }
2156 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002157 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002158 }
2159 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002160 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
2161 }
Joe Onorato561d3852010-11-20 18:09:34 -08002162 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002163 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08002164 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002165 defaults = parcel.readInt();
2166 flags = parcel.readInt();
2167 if (parcel.readInt() != 0) {
2168 sound = Uri.CREATOR.createFromParcel(parcel);
2169 }
2170
2171 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04002172 if (parcel.readInt() != 0) {
2173 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
2174 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002175 vibrate = parcel.createLongArray();
2176 ledARGB = parcel.readInt();
2177 ledOnMS = parcel.readInt();
2178 ledOffMS = parcel.readInt();
2179 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002180
2181 if (parcel.readInt() != 0) {
2182 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2183 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002184
2185 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002186
John Spurlockfd7f1e02014-03-18 16:41:57 -04002187 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002188
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002189 mGroupKey = parcel.readString();
2190
2191 mSortKey = parcel.readString();
2192
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06002193 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Robin Leead7e72a2017-12-04 15:45:46 +01002194 fixDuplicateExtras();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002195
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002196 actions = parcel.createTypedArray(Action.CREATOR); // may be null
2197
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002198 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002199 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2200 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002201
Chris Wren8fd39ec2014-02-27 17:43:26 -05002202 if (parcel.readInt() != 0) {
2203 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2204 }
2205
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002206 visibility = parcel.readInt();
2207
2208 if (parcel.readInt() != 0) {
2209 publicVersion = Notification.CREATOR.createFromParcel(parcel);
2210 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002211
2212 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002213
2214 if (parcel.readInt() != 0) {
2215 mChannelId = parcel.readString();
2216 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002217 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05002218
2219 if (parcel.readInt() != 0) {
2220 mShortcutId = parcel.readString();
2221 }
2222
2223 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04002224
2225 if (parcel.readInt() != 0) {
2226 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2227 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002228
2229 mGroupAlertBehavior = parcel.readInt();
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002230 if (parcel.readInt() != 0) {
2231 mAppOverlayIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2232 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002233 }
2234
Andy Stadler110988c2010-12-03 14:29:16 -08002235 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07002236 public Notification clone() {
2237 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04002238 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002239 return that;
2240 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002241
Daniel Sandler1a497d32013-04-18 14:52:45 -04002242 /**
2243 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
2244 * of this into that.
2245 * @hide
2246 */
2247 public void cloneInto(Notification that, boolean heavy) {
Adrian Roosfb921842017-10-26 14:49:56 +02002248 that.mWhitelistToken = this.mWhitelistToken;
Joe Onorato18e69df2010-05-17 22:26:12 -07002249 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002250 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002251 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07002252 that.number = this.number;
2253
2254 // PendingIntents are global, so there's no reason (or way) to clone them.
2255 that.contentIntent = this.contentIntent;
2256 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002257 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07002258
2259 if (this.tickerText != null) {
2260 that.tickerText = this.tickerText.toString();
2261 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002262 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002263 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04002264 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002265 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07002266 that.contentView = this.contentView.clone();
2267 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002268 if (heavy && this.mLargeIcon != null) {
2269 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08002270 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01002271 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07002272 that.sound = this.sound; // android.net.Uri is immutable
2273 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002274 if (this.audioAttributes != null) {
2275 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
2276 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002277
2278 final long[] vibrate = this.vibrate;
2279 if (vibrate != null) {
2280 final int N = vibrate.length;
2281 final long[] vib = that.vibrate = new long[N];
2282 System.arraycopy(vibrate, 0, vib, 0, N);
2283 }
2284
2285 that.ledARGB = this.ledARGB;
2286 that.ledOnMS = this.ledOnMS;
2287 that.ledOffMS = this.ledOffMS;
2288 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002289
Joe Onorato18e69df2010-05-17 22:26:12 -07002290 that.flags = this.flags;
2291
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002292 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08002293
John Spurlockfd7f1e02014-03-18 16:41:57 -04002294 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002295
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002296 that.mGroupKey = this.mGroupKey;
2297
2298 that.mSortKey = this.mSortKey;
2299
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002300 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002301 try {
2302 that.extras = new Bundle(this.extras);
2303 // will unparcel
2304 that.extras.size();
2305 } catch (BadParcelableException e) {
2306 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
2307 that.extras = null;
2308 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002309 }
2310
Felipe Lemedd85da62016-06-28 11:29:54 -07002311 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2312 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002313 }
2314
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002315 if (this.actions != null) {
2316 that.actions = new Action[this.actions.length];
2317 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002318 if ( this.actions[i] != null) {
2319 that.actions[i] = this.actions[i].clone();
2320 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002321 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002322 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002323
Daniel Sandler1a497d32013-04-18 14:52:45 -04002324 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002325 that.bigContentView = this.bigContentView.clone();
2326 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002327
Chris Wren8fd39ec2014-02-27 17:43:26 -05002328 if (heavy && this.headsUpContentView != null) {
2329 that.headsUpContentView = this.headsUpContentView.clone();
2330 }
2331
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002332 that.visibility = this.visibility;
2333
2334 if (this.publicVersion != null) {
2335 that.publicVersion = new Notification();
2336 this.publicVersion.cloneInto(that.publicVersion, heavy);
2337 }
2338
Dan Sandler26e81cf2014-05-06 10:01:27 -04002339 that.color = this.color;
2340
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002341 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002342 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002343 that.mShortcutId = this.mShortcutId;
2344 that.mBadgeIcon = this.mBadgeIcon;
2345 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002346 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002347 that.mAppOverlayIntent = this.mAppOverlayIntent;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002348
Daniel Sandler1a497d32013-04-18 14:52:45 -04002349 if (!heavy) {
2350 that.lightenPayload(); // will clean out extras
2351 }
2352 }
2353
2354 /**
Jeff Sharkey23b31182018-04-18 21:32:12 -06002355 * Note all {@link Uri} that are referenced internally, with the expectation
2356 * that Uri permission grants will need to be issued to ensure the recipient
2357 * of this object is able to render its contents.
2358 *
2359 * @hide
2360 */
2361 public void visitUris(@NonNull Consumer<Uri> visitor) {
2362 visitor.accept(sound);
2363
2364 if (tickerView != null) tickerView.visitUris(visitor);
2365 if (contentView != null) contentView.visitUris(visitor);
2366 if (bigContentView != null) bigContentView.visitUris(visitor);
2367 if (headsUpContentView != null) headsUpContentView.visitUris(visitor);
2368
2369 if (extras != null) {
2370 visitor.accept(extras.getParcelable(EXTRA_AUDIO_CONTENTS_URI));
Robin Leed107af62018-04-27 13:55:56 +02002371 if (extras.containsKey(EXTRA_BACKGROUND_IMAGE_URI)) {
2372 visitor.accept(Uri.parse(extras.getString(EXTRA_BACKGROUND_IMAGE_URI)));
2373 }
Jeff Sharkey23b31182018-04-18 21:32:12 -06002374 }
2375
2376 if (MessagingStyle.class.equals(getNotificationStyle()) && extras != null) {
2377 final Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
2378 if (!ArrayUtils.isEmpty(messages)) {
2379 for (MessagingStyle.Message message : MessagingStyle.Message
2380 .getMessagesFromBundleArray(messages)) {
2381 visitor.accept(message.getDataUri());
2382 }
2383 }
2384
2385 final Parcelable[] historic = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
2386 if (!ArrayUtils.isEmpty(historic)) {
2387 for (MessagingStyle.Message message : MessagingStyle.Message
2388 .getMessagesFromBundleArray(historic)) {
2389 visitor.accept(message.getDataUri());
2390 }
2391 }
2392 }
2393 }
2394
2395 /**
Daniel Sandler1a497d32013-04-18 14:52:45 -04002396 * Removes heavyweight parts of the Notification object for archival or for sending to
2397 * listeners when the full contents are not necessary.
2398 * @hide
2399 */
2400 public final void lightenPayload() {
2401 tickerView = null;
2402 contentView = null;
2403 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002404 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002405 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002406 if (extras != null && !extras.isEmpty()) {
2407 final Set<String> keyset = extras.keySet();
2408 final int N = keyset.size();
2409 final String[] keys = keyset.toArray(new String[N]);
2410 for (int i=0; i<N; i++) {
2411 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002412 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2413 continue;
2414 }
Dan Sandler50128532015-12-08 15:42:41 -05002415 final Object obj = extras.get(key);
2416 if (obj != null &&
2417 ( obj instanceof Parcelable
2418 || obj instanceof Parcelable[]
2419 || obj instanceof SparseArray
2420 || obj instanceof ArrayList)) {
2421 extras.remove(key);
2422 }
2423 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002424 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002425 }
2426
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002427 /**
2428 * Make sure this CharSequence is safe to put into a bundle, which basically
2429 * means it had better not be some custom Parcelable implementation.
2430 * @hide
2431 */
2432 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002433 if (cs == null) return cs;
2434 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2435 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2436 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002437 if (cs instanceof Parcelable) {
2438 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2439 + " instance is a custom Parcelable and not allowed in Notification");
2440 return cs.toString();
2441 }
Selim Cinek60a54252016-02-26 17:03:25 -08002442 return removeTextSizeSpans(cs);
2443 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002444
Selim Cinek60a54252016-02-26 17:03:25 -08002445 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2446 if (charSequence instanceof Spanned) {
2447 Spanned ss = (Spanned) charSequence;
2448 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2449 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2450 for (Object span : spans) {
2451 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002452 if (resultSpan instanceof CharacterStyle) {
2453 resultSpan = ((CharacterStyle) span).getUnderlying();
2454 }
2455 if (resultSpan instanceof TextAppearanceSpan) {
2456 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002457 resultSpan = new TextAppearanceSpan(
2458 originalSpan.getFamily(),
2459 originalSpan.getTextStyle(),
2460 -1,
2461 originalSpan.getTextColor(),
2462 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002463 } else if (resultSpan instanceof RelativeSizeSpan
2464 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002465 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002466 } else {
2467 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002468 }
2469 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2470 ss.getSpanFlags(span));
2471 }
2472 return builder;
2473 }
2474 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002475 }
2476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002477 public int describeContents() {
2478 return 0;
2479 }
2480
2481 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002482 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002483 */
Svet Ganovddb94882016-06-23 19:55:24 -07002484 public void writeToParcel(Parcel parcel, int flags) {
2485 // We need to mark all pending intents getting into the notification
2486 // system as being put there to later allow the notification ranker
2487 // to launch them and by doing so add the app to the battery saver white
2488 // list for a short period of time. The problem is that the system
2489 // cannot look into the extras as there may be parcelables there that
2490 // the platform does not know how to handle. To go around that we have
2491 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002492 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002493 if (collectPendingIntents) {
2494 PendingIntent.setOnMarshaledListener(
2495 (PendingIntent intent, Parcel out, int outFlags) -> {
2496 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002497 if (allPendingIntents == null) {
2498 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002499 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002500 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002501 }
2502 });
2503 }
2504 try {
2505 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002506 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002507 writeToParcelImpl(parcel, flags);
2508 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002509 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002510 } finally {
2511 if (collectPendingIntents) {
2512 PendingIntent.setOnMarshaledListener(null);
2513 }
2514 }
2515 }
2516
2517 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002518 parcel.writeInt(1);
2519
Adrian Roosfb921842017-10-26 14:49:56 +02002520 parcel.writeStrongBinder(mWhitelistToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002521 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002522 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002523 if (mSmallIcon == null && icon != 0) {
2524 // you snuck an icon in here without using the builder; let's try to keep it
2525 mSmallIcon = Icon.createWithResource("", icon);
2526 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002527 if (mSmallIcon != null) {
2528 parcel.writeInt(1);
2529 mSmallIcon.writeToParcel(parcel, 0);
2530 } else {
2531 parcel.writeInt(0);
2532 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002533 parcel.writeInt(number);
2534 if (contentIntent != null) {
2535 parcel.writeInt(1);
2536 contentIntent.writeToParcel(parcel, 0);
2537 } else {
2538 parcel.writeInt(0);
2539 }
2540 if (deleteIntent != null) {
2541 parcel.writeInt(1);
2542 deleteIntent.writeToParcel(parcel, 0);
2543 } else {
2544 parcel.writeInt(0);
2545 }
2546 if (tickerText != null) {
2547 parcel.writeInt(1);
2548 TextUtils.writeToParcel(tickerText, parcel, flags);
2549 } else {
2550 parcel.writeInt(0);
2551 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002552 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002553 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002554 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002555 } else {
2556 parcel.writeInt(0);
2557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002558 if (contentView != null) {
2559 parcel.writeInt(1);
2560 contentView.writeToParcel(parcel, 0);
2561 } else {
2562 parcel.writeInt(0);
2563 }
Selim Cinek279fa862016-06-14 10:57:25 -07002564 if (mLargeIcon == null && largeIcon != null) {
2565 // you snuck an icon in here without using the builder; let's try to keep it
2566 mLargeIcon = Icon.createWithBitmap(largeIcon);
2567 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002568 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002569 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002570 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002571 } else {
2572 parcel.writeInt(0);
2573 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002574
2575 parcel.writeInt(defaults);
2576 parcel.writeInt(this.flags);
2577
2578 if (sound != null) {
2579 parcel.writeInt(1);
2580 sound.writeToParcel(parcel, 0);
2581 } else {
2582 parcel.writeInt(0);
2583 }
2584 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002585
2586 if (audioAttributes != null) {
2587 parcel.writeInt(1);
2588 audioAttributes.writeToParcel(parcel, 0);
2589 } else {
2590 parcel.writeInt(0);
2591 }
2592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002593 parcel.writeLongArray(vibrate);
2594 parcel.writeInt(ledARGB);
2595 parcel.writeInt(ledOnMS);
2596 parcel.writeInt(ledOffMS);
2597 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002598
2599 if (fullScreenIntent != null) {
2600 parcel.writeInt(1);
2601 fullScreenIntent.writeToParcel(parcel, 0);
2602 } else {
2603 parcel.writeInt(0);
2604 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002605
2606 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002607
John Spurlockfd7f1e02014-03-18 16:41:57 -04002608 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002609
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002610 parcel.writeString(mGroupKey);
2611
2612 parcel.writeString(mSortKey);
2613
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002614 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002615
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002616 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002617
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002618 if (bigContentView != null) {
2619 parcel.writeInt(1);
2620 bigContentView.writeToParcel(parcel, 0);
2621 } else {
2622 parcel.writeInt(0);
2623 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002624
Chris Wren8fd39ec2014-02-27 17:43:26 -05002625 if (headsUpContentView != null) {
2626 parcel.writeInt(1);
2627 headsUpContentView.writeToParcel(parcel, 0);
2628 } else {
2629 parcel.writeInt(0);
2630 }
2631
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002632 parcel.writeInt(visibility);
2633
2634 if (publicVersion != null) {
2635 parcel.writeInt(1);
2636 publicVersion.writeToParcel(parcel, 0);
2637 } else {
2638 parcel.writeInt(0);
2639 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002640
2641 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002642
2643 if (mChannelId != null) {
2644 parcel.writeInt(1);
2645 parcel.writeString(mChannelId);
2646 } else {
2647 parcel.writeInt(0);
2648 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002649 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002650
2651 if (mShortcutId != null) {
2652 parcel.writeInt(1);
2653 parcel.writeString(mShortcutId);
2654 } else {
2655 parcel.writeInt(0);
2656 }
2657
2658 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002659
2660 if (mSettingsText != null) {
2661 parcel.writeInt(1);
2662 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2663 } else {
2664 parcel.writeInt(0);
2665 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002666
2667 parcel.writeInt(mGroupAlertBehavior);
Julia Reynoldsfc640012018-02-21 12:25:27 -05002668
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002669 if (mAppOverlayIntent != null) {
2670 parcel.writeInt(1);
2671 mAppOverlayIntent.writeToParcel(parcel, 0);
2672 } else {
2673 parcel.writeInt(0);
2674 }
2675
Julia Reynoldsfc640012018-02-21 12:25:27 -05002676 // mUsesStandardHeader is not written because it should be recomputed in listeners
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002677 }
2678
2679 /**
2680 * Parcelable.Creator that instantiates Notification objects
2681 */
2682 public static final Parcelable.Creator<Notification> CREATOR
2683 = new Parcelable.Creator<Notification>()
2684 {
2685 public Notification createFromParcel(Parcel parcel)
2686 {
2687 return new Notification(parcel);
2688 }
2689
2690 public Notification[] newArray(int size)
2691 {
2692 return new Notification[size];
2693 }
2694 };
2695
2696 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05002697 * @hide
2698 */
2699 public static boolean areActionsVisiblyDifferent(Notification first, Notification second) {
2700 Notification.Action[] firstAs = first.actions;
2701 Notification.Action[] secondAs = second.actions;
2702 if (firstAs == null && secondAs != null || firstAs != null && secondAs == null) {
2703 return true;
2704 }
2705 if (firstAs != null && secondAs != null) {
2706 if (firstAs.length != secondAs.length) {
2707 return true;
2708 }
2709 for (int i = 0; i < firstAs.length; i++) {
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -04002710 if (!Objects.equals(String.valueOf(firstAs[i].title),
2711 String.valueOf(secondAs[i].title))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05002712 return true;
2713 }
2714 RemoteInput[] firstRs = firstAs[i].getRemoteInputs();
2715 RemoteInput[] secondRs = secondAs[i].getRemoteInputs();
2716 if (firstRs == null) {
2717 firstRs = new RemoteInput[0];
2718 }
2719 if (secondRs == null) {
2720 secondRs = new RemoteInput[0];
2721 }
2722 if (firstRs.length != secondRs.length) {
2723 return true;
2724 }
2725 for (int j = 0; j < firstRs.length; j++) {
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -04002726 if (!Objects.equals(String.valueOf(firstRs[j].getLabel()),
2727 String.valueOf(secondRs[j].getLabel()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05002728 return true;
2729 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05002730 }
2731 }
2732 }
2733 return false;
2734 }
2735
2736 /**
2737 * @hide
2738 */
2739 public static boolean areStyledNotificationsVisiblyDifferent(Builder first, Builder second) {
2740 if (first.getStyle() == null) {
2741 return second.getStyle() != null;
2742 }
2743 if (second.getStyle() == null) {
2744 return true;
2745 }
2746 return first.getStyle().areNotificationsVisiblyDifferent(second.getStyle());
2747 }
2748
2749 /**
2750 * @hide
2751 */
2752 public static boolean areRemoteViewsChanged(Builder first, Builder second) {
Julia Reynoldse5c60452018-04-30 14:41:36 -04002753 if (!Objects.equals(first.usesStandardHeader(), second.usesStandardHeader())) {
2754 return true;
2755 }
2756
2757 if (areRemoteViewsChanged(first.mN.contentView, second.mN.contentView)) {
2758 return true;
2759 }
2760 if (areRemoteViewsChanged(first.mN.bigContentView, second.mN.bigContentView)) {
2761 return true;
2762 }
2763 if (areRemoteViewsChanged(first.mN.headsUpContentView, second.mN.headsUpContentView)) {
2764 return true;
2765 }
2766
2767 return false;
2768 }
2769
2770 private static boolean areRemoteViewsChanged(RemoteViews first, RemoteViews second) {
2771 if (first == null && second == null) {
2772 return false;
2773 }
2774 if (first == null && second != null || first != null && second == null) {
2775 return true;
2776 }
2777
2778 if (!Objects.equals(first.getLayoutId(), second.getLayoutId())) {
2779 return true;
2780 }
2781
2782 if (!Objects.equals(first.getSequenceNumber(), second.getSequenceNumber())) {
2783 return true;
2784 }
2785
2786 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05002787 }
2788
2789 /**
Robin Leead7e72a2017-12-04 15:45:46 +01002790 * Parcelling creates multiple copies of objects in {@code extras}. Fix them.
2791 * <p>
2792 * For backwards compatibility {@code extras} holds some references to "real" member data such
2793 * as {@link getLargeIcon()} which is mirrored by {@link #EXTRA_LARGE_ICON}. This is mostly
2794 * fine as long as the object stays in one process.
2795 * <p>
2796 * However, once the notification goes into a parcel each reference gets marshalled separately,
2797 * wasting memory. Especially with large images on Auto and TV, this is worth fixing.
2798 */
2799 private void fixDuplicateExtras() {
2800 if (extras != null) {
Robin Leead7e72a2017-12-04 15:45:46 +01002801 fixDuplicateExtra(mLargeIcon, EXTRA_LARGE_ICON);
2802 }
2803 }
2804
2805 /**
2806 * If we find an extra that's exactly the same as one of the "real" fields but refers to a
2807 * separate object, replace it with the field's version to avoid holding duplicate copies.
2808 */
2809 private void fixDuplicateExtra(@Nullable Parcelable original, @NonNull String extraName) {
2810 if (original != null && extras.getParcelable(extraName) != null) {
2811 extras.putParcelable(extraName, original);
2812 }
2813 }
2814
2815 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002816 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2817 * layout.
2818 *
2819 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2820 * in the view.</p>
2821 * @param context The context for your application / activity.
2822 * @param contentTitle The title that goes in the expanded entry.
2823 * @param contentText The text that goes in the expanded entry.
2824 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2825 * If this is an activity, it must include the
2826 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002827 * that you take care of task management as described in the
2828 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2829 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002830 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002831 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002832 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002833 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002834 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002835 public void setLatestEventInfo(Context context,
2836 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002837 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2838 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2839 new Throwable());
2840 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002841
Selim Cinek4ac6f602016-06-13 15:47:03 -07002842 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2843 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2844 }
2845
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002846 // ensure that any information already set directly is preserved
2847 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002848
2849 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002850 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002851 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002852 }
2853 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002854 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002856 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002857
2858 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002859 }
2860
Julia Reynoldsda303542015-11-23 14:00:20 -05002861 /**
2862 * @hide
2863 */
2864 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002865 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002866 }
2867
2868 /**
2869 * @hide
2870 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002871 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002872 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002873 }
2874
Yi Jin6b514142017-10-30 14:54:12 -07002875 /**
2876 * @hide
2877 */
2878 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2879 long token = proto.start(fieldId);
2880 proto.write(NotificationProto.CHANNEL_ID, getChannelId());
2881 proto.write(NotificationProto.HAS_TICKER_TEXT, this.tickerText != null);
2882 proto.write(NotificationProto.FLAGS, this.flags);
2883 proto.write(NotificationProto.COLOR, this.color);
2884 proto.write(NotificationProto.CATEGORY, this.category);
2885 proto.write(NotificationProto.GROUP_KEY, this.mGroupKey);
2886 proto.write(NotificationProto.SORT_KEY, this.mSortKey);
2887 if (this.actions != null) {
2888 proto.write(NotificationProto.ACTION_LENGTH, this.actions.length);
2889 }
2890 if (this.visibility >= VISIBILITY_SECRET && this.visibility <= VISIBILITY_PUBLIC) {
2891 proto.write(NotificationProto.VISIBILITY, this.visibility);
2892 }
2893 if (publicVersion != null) {
2894 publicVersion.writeToProto(proto, NotificationProto.PUBLIC_VERSION);
2895 }
2896 proto.end(token);
2897 }
2898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002899 @Override
2900 public String toString() {
2901 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002902 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04002903 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002904 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002905 sb.append(priority);
2906 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002907 if (contentView != null) {
2908 sb.append(contentView.getPackage());
2909 sb.append("/0x");
2910 sb.append(Integer.toHexString(contentView.getLayoutId()));
2911 } else {
2912 sb.append("null");
2913 }
2914 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002915 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2916 sb.append("default");
2917 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002918 int N = this.vibrate.length-1;
2919 sb.append("[");
2920 for (int i=0; i<N; i++) {
2921 sb.append(this.vibrate[i]);
2922 sb.append(',');
2923 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002924 if (N != -1) {
2925 sb.append(this.vibrate[N]);
2926 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002927 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002928 } else {
2929 sb.append("null");
2930 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002931 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002932 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002933 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002934 } else if (this.sound != null) {
2935 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002936 } else {
2937 sb.append("null");
2938 }
Chris Wren365b6d32015-07-16 10:39:26 -04002939 if (this.tickerText != null) {
2940 sb.append(" tick");
2941 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002942 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002943 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002944 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002945 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002946 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002947 if (this.category != null) {
2948 sb.append(" category=");
2949 sb.append(this.category);
2950 }
2951 if (this.mGroupKey != null) {
2952 sb.append(" groupKey=");
2953 sb.append(this.mGroupKey);
2954 }
2955 if (this.mSortKey != null) {
2956 sb.append(" sortKey=");
2957 sb.append(this.mSortKey);
2958 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002959 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002960 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002961 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002962 }
2963 sb.append(" vis=");
2964 sb.append(visibilityToString(this.visibility));
2965 if (this.publicVersion != null) {
2966 sb.append(" publicVersion=");
2967 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002968 }
2969 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002970 return sb.toString();
2971 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002972
Dan Sandler1b718782014-07-18 12:43:45 -04002973 /**
2974 * {@hide}
2975 */
2976 public static String visibilityToString(int vis) {
2977 switch (vis) {
2978 case VISIBILITY_PRIVATE:
2979 return "PRIVATE";
2980 case VISIBILITY_PUBLIC:
2981 return "PUBLIC";
2982 case VISIBILITY_SECRET:
2983 return "SECRET";
2984 default:
2985 return "UNKNOWN(" + String.valueOf(vis) + ")";
2986 }
2987 }
2988
Joe Onoratocb109a02011-01-18 17:57:41 -08002989 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002990 * {@hide}
2991 */
2992 public static String priorityToString(@Priority int pri) {
2993 switch (pri) {
2994 case PRIORITY_MIN:
2995 return "MIN";
2996 case PRIORITY_LOW:
2997 return "LOW";
2998 case PRIORITY_DEFAULT:
2999 return "DEFAULT";
3000 case PRIORITY_HIGH:
3001 return "HIGH";
3002 case PRIORITY_MAX:
3003 return "MAX";
3004 default:
3005 return "UNKNOWN(" + String.valueOf(pri) + ")";
3006 }
3007 }
3008
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04003009 /**
3010 * @hide
3011 */
3012 public boolean hasCompletedProgress() {
3013 // not a progress notification; can't be complete
3014 if (!extras.containsKey(EXTRA_PROGRESS)
3015 || !extras.containsKey(EXTRA_PROGRESS_MAX)) {
3016 return false;
3017 }
3018 // many apps use max 0 for 'indeterminate'; not complete
3019 if (extras.getInt(EXTRA_PROGRESS_MAX) == 0) {
3020 return false;
3021 }
3022 return extras.getInt(EXTRA_PROGRESS) == extras.getInt(EXTRA_PROGRESS_MAX);
3023 }
3024
Jeff Sharkey000ce802017-04-29 13:13:27 -06003025 /** @removed */
3026 @Deprecated
Julia Reynolds37856052016-11-11 09:20:07 -05003027 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003028 return mChannelId;
3029 }
3030
3031 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003032 * Returns the id of the channel this notification posts to.
3033 */
3034 public String getChannelId() {
3035 return mChannelId;
3036 }
3037
Jeff Sharkey000ce802017-04-29 13:13:27 -06003038 /** @removed */
3039 @Deprecated
Julia Reynolds2a128742016-11-28 14:29:25 -05003040 public long getTimeout() {
3041 return mTimeout;
3042 }
3043
3044 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003045 * Returns the duration from posting after which this notification should be canceled by the
3046 * system, if it's not canceled already.
3047 */
3048 public long getTimeoutAfter() {
3049 return mTimeout;
3050 }
3051
3052 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003053 * Returns what icon should be shown for this notification if it is being displayed in a
3054 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
3055 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
3056 */
3057 public int getBadgeIconType() {
3058 return mBadgeIcon;
3059 }
3060
3061 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003062 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04003063 *
3064 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
3065 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003066 */
3067 public String getShortcutId() {
3068 return mShortcutId;
3069 }
3070
Julia Reynolds3aedded2017-03-31 14:42:09 -04003071
3072 /**
3073 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
3074 */
3075 public CharSequence getSettingsText() {
3076 return mSettingsText;
3077 }
3078
Julia Reynolds13d898c2017-02-02 12:22:05 -05003079 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003080 * Returns which type of notifications in a group are responsible for audibly alerting the
3081 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
3082 * {@link #GROUP_ALERT_SUMMARY}.
3083 */
3084 public @GroupAlertBehavior int getGroupAlertBehavior() {
3085 return mGroupAlertBehavior;
3086 }
3087
3088 /**
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003089 * Returns the intent that will be used to display app content in a floating window over the
3090 * existing foreground activity.
3091 */
3092 public PendingIntent getAppOverlayIntent() {
3093 return mAppOverlayIntent;
3094 }
3095
3096 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003097 * The small icon representing this notification in the status bar and content view.
3098 *
3099 * @return the small icon representing this notification.
3100 *
3101 * @see Builder#getSmallIcon()
3102 * @see Builder#setSmallIcon(Icon)
3103 */
3104 public Icon getSmallIcon() {
3105 return mSmallIcon;
3106 }
3107
3108 /**
3109 * Used when notifying to clean up legacy small icons.
3110 * @hide
3111 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003112 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04003113 public void setSmallIcon(Icon icon) {
3114 mSmallIcon = icon;
3115 }
3116
3117 /**
3118 * The large icon shown in this notification's content view.
3119 * @see Builder#getLargeIcon()
3120 * @see Builder#setLargeIcon(Icon)
3121 */
3122 public Icon getLargeIcon() {
3123 return mLargeIcon;
3124 }
3125
3126 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02003127 * @hide
3128 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003129 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003130 public boolean isGroupSummary() {
3131 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
3132 }
3133
3134 /**
3135 * @hide
3136 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003137 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003138 public boolean isGroupChild() {
3139 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
3140 }
3141
3142 /**
Julia Reynolds30203152017-05-26 13:36:31 -04003143 * @hide
3144 */
3145 public boolean suppressAlertingDueToGrouping() {
3146 if (isGroupSummary()
3147 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
3148 return true;
3149 } else if (isGroupChild()
3150 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
3151 return true;
3152 }
3153 return false;
3154 }
3155
Tony Mak638430e2018-10-08 19:19:10 +01003156
3157 /**
3158 * Finds and returns a remote input and its corresponding action.
3159 *
3160 * @param requiresFreeform requires the remoteinput to allow freeform or not.
3161 * @return the result pair, {@code null} if no result is found.
3162 *
3163 * @hide
3164 */
3165 @Nullable
3166 public Pair<RemoteInput, Action> findRemoteInputActionPair(boolean requiresFreeform) {
3167 if (actions == null) {
3168 return null;
3169 }
3170 for (Notification.Action action : actions) {
3171 if (action.getRemoteInputs() == null) {
3172 continue;
3173 }
3174 RemoteInput resultRemoteInput = null;
3175 for (RemoteInput remoteInput : action.getRemoteInputs()) {
3176 if (remoteInput.getAllowFreeFormInput() || !requiresFreeform) {
3177 resultRemoteInput = remoteInput;
3178 }
3179 }
3180 if (resultRemoteInput != null) {
3181 return Pair.create(resultRemoteInput, action);
3182 }
3183 }
3184 return null;
3185 }
3186
Julia Reynolds30203152017-05-26 13:36:31 -04003187 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003188 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08003189 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003190 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07003191 * content views using the platform's notification layout template. If your app supports
3192 * versions of Android as old as API level 4, you can instead use
3193 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
3194 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
3195 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08003196 *
Scott Main183bf112012-08-13 19:12:13 -07003197 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08003198 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003199 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07003200 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003201 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3202 * .setContentText(subject)
3203 * .setSmallIcon(R.drawable.new_mail)
3204 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003205 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003206 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08003207 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003208 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003209 /**
3210 * @hide
3211 */
3212 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
3213 "android.rebuild.contentViewActionCount";
3214 /**
3215 * @hide
3216 */
3217 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
3218 = "android.rebuild.bigViewActionCount";
3219 /**
3220 * @hide
3221 */
3222 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
3223 = "android.rebuild.hudViewActionCount";
3224
Selim Cinek6743c0b2017-01-18 18:24:01 -08003225 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
3226 SystemProperties.getBoolean("notifications.only_title", true);
3227
Selim Cinek389edcd2017-05-11 19:16:44 -07003228 /**
3229 * The lightness difference that has to be added to the primary text color to obtain the
3230 * secondary text color when the background is light.
3231 */
3232 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
3233
3234 /**
3235 * The lightness difference that has to be added to the primary text color to obtain the
3236 * secondary text color when the background is dark.
3237 * A bit less then the above value, since it looks better on dark backgrounds.
3238 */
3239 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
3240
Joe Onorato46439ce2010-11-19 13:56:21 -08003241 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003242 private Notification mN;
3243 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003244 private Style mStyle;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003245 @UnsupportedAppUsage
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003246 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Selim Cineke7238dd2017-12-14 17:48:32 -08003247 private ArrayList<Person> mPersonList = new ArrayList<>();
Lucas Dupina291d192018-06-07 13:59:42 -07003248 private ContrastColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08003249 private boolean mIsLegacy;
3250 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02003251
3252 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08003253 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
3254 */
3255 private int mCachedContrastColor = COLOR_INVALID;
3256 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08003257 /**
Selim Cinek4717d862018-04-19 09:19:15 +08003258 * Caches a ambient version of {@link #mCachedAmbientColorIsFor}.
Adrian Roos487374f2017-01-11 15:48:14 -08003259 */
3260 private int mCachedAmbientColor = COLOR_INVALID;
3261 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Selim Cinek4717d862018-04-19 09:19:15 +08003262 /**
3263 * A neutral color color that can be used for icons.
3264 */
3265 private int mNeutralColor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08003266
3267 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08003268 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
3269 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
3270 */
3271 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08003272 private int mTextColorsAreForBackground = COLOR_INVALID;
3273 private int mPrimaryTextColor = COLOR_INVALID;
3274 private int mSecondaryTextColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07003275 private int mBackgroundColor = COLOR_INVALID;
3276 private int mForegroundColor = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07003277 /**
3278 * A temporary location where actions are stored. If != null the view originally has action
3279 * but doesn't have any for this inflation.
3280 */
3281 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07003282 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003283
Anthony Chenad4d1582017-04-10 16:07:58 -07003284 private boolean mTintActionButtons;
3285 private boolean mInNightMode;
3286
Adrian Roos70d7aa32017-01-11 15:39:06 -08003287 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003288 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08003289 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003290 * @param context
3291 * A {@link Context} that will be used by the Builder to construct the
3292 * RemoteViews. The Context will not be held past the lifetime of this Builder
3293 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003294 * @param channelId
3295 * The constructed Notification will be posted on this
3296 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
3297 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003298 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003299 public Builder(Context context, String channelId) {
3300 this(context, (Notification) null);
3301 mN.mChannelId = channelId;
3302 }
3303
3304 /**
3305 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
3306 * instead. All posted Notifications must specify a NotificationChannel Id.
3307 */
3308 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003309 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003310 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003311 }
3312
Joe Onoratocb109a02011-01-18 17:57:41 -08003313 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003314 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02003315 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003316 public Builder(Context context, Notification toAdopt) {
3317 mContext = context;
Anthony Chenad4d1582017-04-10 16:07:58 -07003318 Resources res = mContext.getResources();
3319 mTintActionButtons = res.getBoolean(R.bool.config_tintNotificationActionButtons);
3320
3321 if (res.getBoolean(R.bool.config_enableNightMode)) {
3322 Configuration currentConfig = res.getConfiguration();
3323 mInNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
3324 == Configuration.UI_MODE_NIGHT_YES;
3325 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003326
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003327 if (toAdopt == null) {
3328 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003329 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
3330 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
3331 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003332 mN.priority = PRIORITY_DEFAULT;
3333 mN.visibility = VISIBILITY_PRIVATE;
3334 } else {
3335 mN = toAdopt;
3336 if (mN.actions != null) {
3337 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003338 }
3339
Selim Cineke7238dd2017-12-14 17:48:32 -08003340 if (mN.extras.containsKey(EXTRA_PEOPLE_LIST)) {
3341 ArrayList<Person> people = mN.extras.getParcelableArrayList(EXTRA_PEOPLE_LIST);
3342 mPersonList.addAll(people);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003343 }
3344
Selim Cinek4ac6f602016-06-13 15:47:03 -07003345 if (mN.getSmallIcon() == null && mN.icon != 0) {
3346 setSmallIcon(mN.icon);
3347 }
3348
3349 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
3350 setLargeIcon(mN.largeIcon);
3351 }
3352
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003353 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
3354 if (!TextUtils.isEmpty(templateClass)) {
3355 final Class<? extends Style> styleClass
3356 = getNotificationStyleClass(templateClass);
3357 if (styleClass == null) {
3358 Log.d(TAG, "Unknown style class: " + templateClass);
3359 } else {
3360 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003361 final Constructor<? extends Style> ctor =
3362 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003363 ctor.setAccessible(true);
3364 final Style style = ctor.newInstance();
3365 style.restoreFromExtras(mN.extras);
3366
3367 if (style != null) {
3368 setStyle(style);
3369 }
3370 } catch (Throwable t) {
3371 Log.e(TAG, "Could not create Style", t);
3372 }
3373 }
3374 }
3375
3376 }
3377 }
3378
Lucas Dupina291d192018-06-07 13:59:42 -07003379 private ContrastColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08003380 if (mColorUtil == null) {
Lucas Dupina291d192018-06-07 13:59:42 -07003381 mColorUtil = ContrastColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003382 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003383 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003384 }
3385
3386 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003387 * If this notification is duplicative of a Launcher shortcut, sets the
3388 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
3389 * the shortcut.
3390 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04003391 * This field will be ignored by Launchers that don't support badging, don't show
3392 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003393 *
3394 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
3395 * supersedes
3396 */
3397 public Builder setShortcutId(String shortcutId) {
3398 mN.mShortcutId = shortcutId;
3399 return this;
3400 }
3401
3402 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003403 * Sets which icon to display as a badge for this notification.
3404 *
3405 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
3406 * {@link #BADGE_ICON_LARGE}.
3407 *
3408 * Note: This value might be ignored, for launchers that don't support badge icons.
3409 */
Julia Reynolds612beb22017-03-30 10:48:30 -04003410 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04003411 mN.mBadgeIcon = icon;
3412 return this;
3413 }
3414
3415 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003416 * Sets the group alert behavior for this notification. Use this method to mute this
3417 * notification if alerts for this notification's group should be handled by a different
3418 * notification. This is only applicable for notifications that belong to a
Julia Reynolds399d9bf2017-08-11 12:52:14 -04003419 * {@link #setGroup(String) group}. This must be called on all notifications you want to
3420 * mute. For example, if you want only the summary of your group to make noise, all
3421 * children in the group should have the group alert behavior {@link #GROUP_ALERT_SUMMARY}.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003422 *
3423 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
3424 */
3425 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
3426 mN.mGroupAlertBehavior = groupAlertBehavior;
3427 return this;
3428 }
3429
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003430 /**
3431 * Sets the intent that will be used to display app content in a floating window
3432 * over the existing foreground activity.
3433 *
3434 * <p>This intent will be ignored unless this notification is posted to a channel that
3435 * allows {@link NotificationChannel#canOverlayApps() app overlays}.</p>
3436 *
3437 * <p>Notifications with a valid and allowed app overlay intent will be displayed as
3438 * floating windows outside of the notification shade on unlocked devices. When a user
3439 * interacts with one of these windows, this app overlay intent will be invoked and
3440 * displayed.</p>
3441 */
3442 public Builder setAppOverlayIntent(PendingIntent intent) {
3443 mN.mAppOverlayIntent = intent;
3444 return this;
3445 }
3446
Jeff Sharkey000ce802017-04-29 13:13:27 -06003447 /** @removed */
3448 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003449 public Builder setChannel(String channelId) {
3450 mN.mChannelId = channelId;
3451 return this;
3452 }
3453
3454 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003455 * Specifies the channel the notification should be delivered on.
3456 */
3457 public Builder setChannelId(String channelId) {
3458 mN.mChannelId = channelId;
3459 return this;
3460 }
3461
Jeff Sharkey000ce802017-04-29 13:13:27 -06003462 /** @removed */
3463 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05003464 public Builder setTimeout(long durationMs) {
3465 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05003466 return this;
3467 }
3468
3469 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003470 * Specifies a duration in milliseconds after which this notification should be canceled,
3471 * if it is not already canceled.
3472 */
3473 public Builder setTimeoutAfter(long durationMs) {
3474 mN.mTimeout = durationMs;
3475 return this;
3476 }
3477
3478 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003479 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003480 *
3481 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
3482 * shown anymore by default and must be opted into by using
3483 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003484 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003485 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08003486 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003487 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003488 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08003489 return this;
3490 }
3491
Joe Onoratocb109a02011-01-18 17:57:41 -08003492 /**
Griff Hazen50c11652014-05-16 09:46:31 -07003493 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07003494 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003495 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
3496 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07003497 */
3498 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003499 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07003500 return this;
3501 }
3502
3503 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003504 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08003505 *
3506 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003507 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003508 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003509 * Useful when showing an elapsed time (like an ongoing phone call).
3510 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08003511 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07003512 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08003513 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003514 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003515 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07003516 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003517 */
3518 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003519 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003520 return this;
3521 }
3522
3523 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08003524 * Sets the Chronometer to count down instead of counting up.
3525 *
3526 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
3527 * If it isn't set the chronometer will count up.
3528 *
3529 * @see #setUsesChronometer(boolean)
3530 */
Adrian Roos96b7e202016-05-17 13:50:38 -07003531 public Builder setChronometerCountDown(boolean countDown) {
3532 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003533 return this;
3534 }
3535
3536 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003537 * Set the small icon resource, which will be used to represent the notification in the
3538 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003539 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003540
3541 * The platform template for the expanded view will draw this icon in the left, unless a
3542 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3543 * icon will be moved to the right-hand side.
3544 *
3545
3546 * @param icon
3547 * A resource ID in the application's package of the drawable to use.
3548 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003549 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003550 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003551 return setSmallIcon(icon != 0
3552 ? Icon.createWithResource(mContext, icon)
3553 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003554 }
3555
Joe Onoratocb109a02011-01-18 17:57:41 -08003556 /**
3557 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3558 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3559 * LevelListDrawable}.
3560 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003561 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003562 * @param level The level to use for the icon.
3563 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003564 * @see Notification#icon
3565 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003566 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003567 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003568 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003569 return setSmallIcon(icon);
3570 }
3571
3572 /**
3573 * Set the small icon, which will be used to represent the notification in the
koprivadebd4ee2018-09-13 10:59:46 -07003574 * status bar and content view (unless overridden there by a
Dan Sandlerd63f9322015-05-06 15:18:49 -04003575 * {@link #setLargeIcon(Bitmap) large icon}).
3576 *
3577 * @param icon An Icon object to use.
3578 * @see Notification#icon
3579 */
3580 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003581 mN.setSmallIcon(icon);
3582 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3583 mN.icon = icon.getResId();
3584 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003585 return this;
3586 }
3587
Joe Onoratocb109a02011-01-18 17:57:41 -08003588 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003589 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003590 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003591 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003592 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
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 second 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 setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003600 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003601 return this;
3602 }
3603
Joe Onoratocb109a02011-01-18 17:57:41 -08003604 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003605 * This provides some additional information that is displayed in the notification. No
3606 * guarantees are given where exactly it is displayed.
3607 *
3608 * <p>This information should only be provided if it provides an essential
3609 * benefit to the understanding of the notification. The more text you provide the
3610 * less readable it becomes. For example, an email client should only provide the account
3611 * name here if more than one email account has been added.</p>
3612 *
3613 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3614 * notification header area.
3615 *
3616 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3617 * this will be shown in the third line of text in the platform notification template.
3618 * You should not be using {@link #setProgress(int, int, boolean)} at the
3619 * same time on those versions; they occupy the same place.
3620 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003621 */
3622 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003623 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003624 return this;
3625 }
3626
3627 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003628 * Provides text that will appear as a link to your application's settings.
3629 *
3630 * <p>This text does not appear within notification {@link Style templates} but may
3631 * appear when the user uses an affordance to learn more about the notification.
3632 * Additionally, this text will not appear unless you provide a valid link target by
3633 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3634 *
3635 * <p>This text is meant to be concise description about what the user can customize
3636 * when they click on this link. The recommended maximum length is 40 characters.
3637 * @param text
3638 * @return
3639 */
3640 public Builder setSettingsText(CharSequence text) {
3641 mN.mSettingsText = safeCharSequence(text);
3642 return this;
3643 }
3644
3645 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003646 * Set the remote input history.
3647 *
3648 * This should be set to the most recent inputs that have been sent
3649 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3650 * longer relevant (e.g. for chat notifications once the other party has responded).
3651 *
3652 * The most recent input must be stored at the 0 index, the second most recent at the
3653 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3654 * and how much of each individual input is shown.
3655 *
3656 * <p>Note: The reply text will only be shown on notifications that have least one action
3657 * with a {@code RemoteInput}.</p>
3658 */
3659 public Builder setRemoteInputHistory(CharSequence[] text) {
3660 if (text == null) {
3661 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3662 } else {
3663 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3664 CharSequence[] safe = new CharSequence[N];
3665 for (int i = 0; i < N; i++) {
3666 safe[i] = safeCharSequence(text[i]);
3667 }
3668 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3669 }
3670 return this;
3671 }
3672
3673 /**
Kenny Guya0f6de82018-04-06 16:20:16 +01003674 * Sets whether remote history entries view should have a spinner.
3675 * @hide
3676 */
3677 public Builder setShowRemoteInputSpinner(boolean showSpinner) {
3678 mN.extras.putBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER, showSpinner);
3679 return this;
3680 }
3681
3682 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +01003683 * Sets whether smart reply buttons should be hidden.
3684 * @hide
3685 */
3686 public Builder setHideSmartReplies(boolean hideSmartReplies) {
3687 mN.extras.putBoolean(EXTRA_HIDE_SMART_REPLIES, hideSmartReplies);
3688 return this;
3689 }
3690
3691 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003692 * Sets the number of items this notification represents. May be displayed as a badge count
3693 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003694 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08003695 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003696 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003697 return this;
3698 }
3699
Joe Onoratocb109a02011-01-18 17:57:41 -08003700 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003701 * A small piece of additional information pertaining to this notification.
3702 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003703 * The platform template will draw this on the last line of the notification, at the far
3704 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003705 *
3706 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3707 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3708 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003709 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003710 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003711 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003712 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003713 return this;
3714 }
3715
Joe Onoratocb109a02011-01-18 17:57:41 -08003716 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003717 * Set the progress this notification represents.
3718 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003719 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003720 */
3721 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003722 mN.extras.putInt(EXTRA_PROGRESS, progress);
3723 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3724 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003725 return this;
3726 }
3727
3728 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003729 * Supply a custom RemoteViews to use instead of the platform template.
3730 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003731 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003732 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003733 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003734 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003735 return setCustomContentView(views);
3736 }
3737
3738 /**
3739 * Supply custom RemoteViews to use instead of the platform template.
3740 *
3741 * This will override the layout that would otherwise be constructed by this Builder
3742 * object.
3743 */
3744 public Builder setCustomContentView(RemoteViews contentView) {
3745 mN.contentView = contentView;
3746 return this;
3747 }
3748
3749 /**
3750 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3751 *
3752 * This will override the expanded layout that would otherwise be constructed by this
3753 * Builder object.
3754 */
3755 public Builder setCustomBigContentView(RemoteViews contentView) {
3756 mN.bigContentView = contentView;
3757 return this;
3758 }
3759
3760 /**
3761 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3762 *
3763 * This will override the heads-up layout that would otherwise be constructed by this
3764 * Builder object.
3765 */
3766 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3767 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003768 return this;
3769 }
3770
Joe Onoratocb109a02011-01-18 17:57:41 -08003771 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003772 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3773 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003774 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3775 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3776 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003777 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003778 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003779 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003780 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003781 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003782 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003783 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003784 return this;
3785 }
3786
Joe Onoratocb109a02011-01-18 17:57:41 -08003787 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003788 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3789 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003790 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003791 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003792 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003793 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003794 return this;
3795 }
3796
Joe Onoratocb109a02011-01-18 17:57:41 -08003797 /**
3798 * An intent to launch instead of posting the notification to the status bar.
3799 * Only for use with extremely high-priority notifications demanding the user's
3800 * <strong>immediate</strong> attention, such as an incoming phone call or
3801 * alarm clock that the user has explicitly set to a particular time.
3802 * If this facility is used for something else, please give the user an option
3803 * to turn it off and use a normal notification, as this can be extremely
3804 * disruptive.
3805 *
Chris Wren47c20a12014-06-18 17:27:29 -04003806 * <p>
3807 * The system UI may choose to display a heads-up notification, instead of
3808 * launching this intent, while the user is using the device.
3809 * </p>
3810 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003811 * @param intent The pending intent to launch.
3812 * @param highPriority Passing true will cause this notification to be sent
3813 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003814 *
3815 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003816 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003817 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003818 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003819 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3820 return this;
3821 }
3822
Joe Onoratocb109a02011-01-18 17:57:41 -08003823 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003824 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003825 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003826 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003827 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003828 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003829 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003830 return this;
3831 }
3832
Joe Onoratocb109a02011-01-18 17:57:41 -08003833 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003834 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003835 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003836 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003837 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003838 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003839 setTicker(tickerText);
3840 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003841 return this;
3842 }
3843
Joe Onoratocb109a02011-01-18 17:57:41 -08003844 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003845 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003846 *
3847 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04003848 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3849 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04003850 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003851 public Builder setLargeIcon(Bitmap b) {
3852 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3853 }
3854
3855 /**
3856 * Add a large icon to the notification content view.
3857 *
3858 * In the platform template, this image will be shown on the left of the notification view
3859 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3860 * badge atop the large icon).
3861 */
3862 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003863 mN.mLargeIcon = icon;
3864 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08003865 return this;
3866 }
3867
Joe Onoratocb109a02011-01-18 17:57:41 -08003868 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003869 * Set the sound to play.
3870 *
John Spurlockc0650f022014-07-19 13:22:39 -04003871 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
3872 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003873 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003874 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003875 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003876 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08003877 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003878 mN.sound = sound;
3879 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08003880 return this;
3881 }
3882
Joe Onoratocb109a02011-01-18 17:57:41 -08003883 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003884 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08003885 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003886 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
3887 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003888 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003889 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07003890 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003891 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08003892 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003893 mN.sound = sound;
3894 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08003895 return this;
3896 }
3897
Joe Onoratocb109a02011-01-18 17:57:41 -08003898 /**
John Spurlockc0650f022014-07-19 13:22:39 -04003899 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
3900 * use during playback.
3901 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003902 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04003903 * @see Notification#sound
3904 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003905 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04003906 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003907 mN.sound = sound;
3908 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04003909 return this;
3910 }
3911
3912 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003913 * Set the vibration pattern to use.
3914 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003915 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
3916 * <code>pattern</code> parameter.
3917 *
Chris Wren47c20a12014-06-18 17:27:29 -04003918 * <p>
3919 * A notification that vibrates is more likely to be presented as a heads-up notification.
3920 * </p>
3921 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003922 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003923 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08003924 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003925 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003926 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003927 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08003928 return this;
3929 }
3930
Joe Onoratocb109a02011-01-18 17:57:41 -08003931 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003932 * Set the desired color for the indicator LED on the device, as well as the
3933 * blink duty cycle (specified in milliseconds).
3934 *
3935
3936 * Not all devices will honor all (or even any) of these values.
3937 *
Julia Reynolds529e3322017-02-06 08:33:01 -05003938 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003939 * @see Notification#ledARGB
3940 * @see Notification#ledOnMS
3941 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08003942 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003943 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08003944 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003945 mN.ledARGB = argb;
3946 mN.ledOnMS = onMs;
3947 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003948 if (onMs != 0 || offMs != 0) {
3949 mN.flags |= FLAG_SHOW_LIGHTS;
3950 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003951 return this;
3952 }
3953
Joe Onoratocb109a02011-01-18 17:57:41 -08003954 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003955 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08003956 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003957
3958 * Ongoing notifications cannot be dismissed by the user, so your application or service
3959 * must take care of canceling them.
3960 *
3961
3962 * They are typically used to indicate a background task that the user is actively engaged
3963 * with (e.g., playing music) or is pending in some way and therefore occupying the device
3964 * (e.g., a file download, sync operation, active network connection).
3965 *
3966
3967 * @see Notification#FLAG_ONGOING_EVENT
Joe Onoratocb109a02011-01-18 17:57:41 -08003968 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003969 public Builder setOngoing(boolean ongoing) {
3970 setFlag(FLAG_ONGOING_EVENT, ongoing);
3971 return this;
3972 }
3973
Joe Onoratocb109a02011-01-18 17:57:41 -08003974 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08003975 * Set whether this notification should be colorized. When set, the color set with
3976 * {@link #setColor(int)} will be used as the background color of this notification.
3977 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08003978 * This should only be used for high priority ongoing tasks like navigation, an ongoing
3979 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08003980 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07003981 * For most styles, the coloring will only be applied if the notification is for a
3982 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08003983 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07003984 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08003985 *
Aurimas Liutikas7f695332018-05-31 21:07:32 -07003986 * @see #setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08003987 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08003988 */
3989 public Builder setColorized(boolean colorize) {
3990 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
3991 return this;
3992 }
3993
3994 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003995 * Set this flag if you would only like the sound, vibrate
3996 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003997 *
3998 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08003999 */
Joe Onorato46439ce2010-11-19 13:56:21 -08004000 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
4001 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
4002 return this;
4003 }
4004
Joe Onoratocb109a02011-01-18 17:57:41 -08004005 /**
Julia Reynolds04499532016-09-13 14:04:53 -04004006 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004007 *
4008 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08004009 */
Joe Onorato46439ce2010-11-19 13:56:21 -08004010 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08004011 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08004012 return this;
4013 }
4014
Joe Onoratocb109a02011-01-18 17:57:41 -08004015 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08004016 * Set whether or not this notification should not bridge to other devices.
4017 *
4018 * <p>Some notifications can be bridged to other devices for remote display.
4019 * This hint can be set to recommend this notification not be bridged.
4020 */
4021 public Builder setLocalOnly(boolean localOnly) {
4022 setFlag(FLAG_LOCAL_ONLY, localOnly);
4023 return this;
4024 }
4025
4026 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004027 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08004028 * <p>
4029 * The value should be one or more of the following fields combined with
4030 * bitwise-or:
4031 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
4032 * <p>
4033 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004034 *
4035 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05004036 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004037 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08004038 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004039 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004040 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004041 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08004042 return this;
4043 }
4044
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004045 /**
4046 * Set the priority of this notification.
4047 *
4048 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004049 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004050 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004051 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07004052 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004053 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004054 return this;
4055 }
Joe Malin8d40d042012-11-05 11:36:40 -08004056
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004057 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04004058 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08004059 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04004060 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004061 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04004062 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004063 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004064 return this;
4065 }
4066
4067 /**
Chris Wrendde75302014-03-26 17:24:15 -04004068 * Add a person that is relevant to this notification.
4069 *
Chris Wrene6c48932014-09-29 17:19:27 -04004070 * <P>
4071 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04004072 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4073 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4074 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04004075 * </P>
4076 *
4077 * <P>
4078 * The person should be specified by the {@code String} representation of a
4079 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
4080 * </P>
4081 *
4082 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
4083 * URIs. The path part of these URIs must exist in the contacts database, in the
4084 * appropriate column, or the reference will be discarded as invalid. Telephone schema
4085 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
Selim Cineke7238dd2017-12-14 17:48:32 -08004086 * It is also possible to provide a URI with the schema {@code name:} in order to uniquely
4087 * identify a person without an entry in the contacts database.
Chris Wrene6c48932014-09-29 17:19:27 -04004088 * </P>
4089 *
4090 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04004091 * @see Notification#EXTRA_PEOPLE
Selim Cineke7238dd2017-12-14 17:48:32 -08004092 * @deprecated use {@link #addPerson(Person)}
Chris Wrendde75302014-03-26 17:24:15 -04004093 */
Chris Wrene6c48932014-09-29 17:19:27 -04004094 public Builder addPerson(String uri) {
Selim Cinek9acd6732018-03-23 16:39:02 -07004095 addPerson(new Person.Builder().setUri(uri).build());
Selim Cineke7238dd2017-12-14 17:48:32 -08004096 return this;
4097 }
4098
4099 /**
4100 * Add a person that is relevant to this notification.
4101 *
4102 * <P>
4103 * Depending on user preferences, this annotation may allow the notification to pass
4104 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4105 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4106 * appear more prominently in the user interface.
4107 * </P>
4108 *
4109 * <P>
4110 * A person should usually contain a uri in order to benefit from the ranking boost.
4111 * However, even if no uri is provided, it's beneficial to provide other people in the
4112 * notification, such that listeners and voice only devices can announce and handle them
4113 * properly.
4114 * </P>
4115 *
4116 * @param person the person to add.
4117 * @see Notification#EXTRA_PEOPLE_LIST
4118 */
4119 public Builder addPerson(Person person) {
4120 mPersonList.add(person);
Chris Wrendde75302014-03-26 17:24:15 -04004121 return this;
4122 }
4123
4124 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004125 * Set this notification to be part of a group of notifications sharing the same key.
4126 * Grouped notifications may display in a cluster or stack on devices which
4127 * support such rendering.
4128 *
4129 * <p>To make this notification the summary for its group, also call
4130 * {@link #setGroupSummary}. A sort order can be specified for group members by using
4131 * {@link #setSortKey}.
4132 * @param groupKey The group key of the group.
4133 * @return this object for method chaining
4134 */
4135 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004136 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004137 return this;
4138 }
4139
4140 /**
4141 * Set this notification to be the group summary for a group of notifications.
4142 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04004143 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
4144 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004145 * @param isGroupSummary Whether this notification should be a group summary.
4146 * @return this object for method chaining
4147 */
4148 public Builder setGroupSummary(boolean isGroupSummary) {
4149 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
4150 return this;
4151 }
4152
4153 /**
4154 * Set a sort key that orders this notification among other notifications from the
4155 * same package. This can be useful if an external sort was already applied and an app
4156 * would like to preserve this. Notifications will be sorted lexicographically using this
4157 * value, although providing different priorities in addition to providing sort key may
4158 * cause this value to be ignored.
4159 *
4160 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07004161 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004162 *
4163 * @see String#compareTo(String)
4164 */
4165 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004166 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004167 return this;
4168 }
4169
4170 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004171 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004172 *
Griff Hazen720042b2014-02-24 15:46:56 -08004173 * <p>Values within the Bundle will replace existing extras values in this Builder.
4174 *
4175 * @see Notification#extras
4176 */
Griff Hazen959591e2014-05-15 22:26:18 -07004177 public Builder addExtras(Bundle extras) {
4178 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004179 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08004180 }
4181 return this;
4182 }
4183
4184 /**
4185 * Set metadata for this notification.
4186 *
4187 * <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 -04004188 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004189 * called.
4190 *
Griff Hazen720042b2014-02-24 15:46:56 -08004191 * <p>Replaces any existing extras values with those from the provided Bundle.
4192 * Use {@link #addExtras} to merge in metadata instead.
4193 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004194 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004195 */
Griff Hazen959591e2014-05-15 22:26:18 -07004196 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004197 if (extras != null) {
4198 mUserExtras = extras;
4199 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004200 return this;
4201 }
4202
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004203 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004204 * Get the current metadata Bundle used by this notification Builder.
4205 *
4206 * <p>The returned Bundle is shared with this Builder.
4207 *
4208 * <p>The current contents of this Bundle are copied into the Notification each time
4209 * {@link #build()} is called.
4210 *
4211 * @see Notification#extras
4212 */
4213 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004214 return mUserExtras;
4215 }
4216
4217 private Bundle getAllExtras() {
4218 final Bundle saveExtras = (Bundle) mUserExtras.clone();
4219 saveExtras.putAll(mN.extras);
4220 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08004221 }
4222
4223 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004224 * Add an action to this notification. Actions are typically displayed by
4225 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004226 * <p>
4227 * Every action must have an icon (32dp square and matching the
4228 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4229 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4230 * <p>
4231 * A notification in its expanded form can display up to 3 actions, from left to right in
4232 * the order they were added. Actions will not be displayed when the notification is
4233 * collapsed, however, so be sure that any essential functions may be accessed by the user
4234 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004235 *
4236 * @param icon Resource ID of a drawable that represents the action.
4237 * @param title Text describing the action.
4238 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04004239 *
4240 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004241 */
Dan Sandler86647982015-05-13 23:41:13 -04004242 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004243 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004244 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004245 return this;
4246 }
4247
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004248 /**
Griff Hazen959591e2014-05-15 22:26:18 -07004249 * Add an action to this notification. Actions are typically displayed by
4250 * the system as a button adjacent to the notification content.
4251 * <p>
4252 * Every action must have an icon (32dp square and matching the
4253 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4254 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4255 * <p>
4256 * A notification in its expanded form can display up to 3 actions, from left to right in
4257 * the order they were added. Actions will not be displayed when the notification is
4258 * collapsed, however, so be sure that any essential functions may be accessed by the user
4259 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
4260 *
4261 * @param action The action to add.
4262 */
4263 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08004264 if (action != null) {
4265 mActions.add(action);
4266 }
Griff Hazen959591e2014-05-15 22:26:18 -07004267 return this;
4268 }
4269
4270 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004271 * Alter the complete list of actions attached to this notification.
4272 * @see #addAction(Action).
4273 *
4274 * @param actions
4275 * @return
4276 */
4277 public Builder setActions(Action... actions) {
4278 mActions.clear();
4279 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08004280 if (actions[i] != null) {
4281 mActions.add(actions[i]);
4282 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004283 }
4284 return this;
4285 }
4286
4287 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004288 * Add a rich notification style to be applied at build time.
4289 *
4290 * @param style Object responsible for modifying the notification style.
4291 */
4292 public Builder setStyle(Style style) {
4293 if (mStyle != style) {
4294 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004295 if (mStyle != null) {
4296 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004297 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
4298 } else {
4299 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004300 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004301 }
4302 return this;
4303 }
4304
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004305 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05004306 * Returns the style set by {@link #setStyle(Style)}.
4307 */
4308 public Style getStyle() {
4309 return mStyle;
4310 }
4311
4312 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004313 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07004314 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004315 * @return The same Builder.
4316 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06004317 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004318 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004319 return this;
4320 }
4321
4322 /**
4323 * Supply a replacement Notification whose contents should be shown in insecure contexts
4324 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
4325 * @param n A replacement notification, presumably with some or all info redacted.
4326 * @return The same Builder.
4327 */
4328 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004329 if (n != null) {
4330 mN.publicVersion = new Notification();
4331 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
4332 } else {
4333 mN.publicVersion = null;
4334 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004335 return this;
4336 }
4337
Griff Hazenb720abe2014-05-20 13:15:30 -07004338 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004339 * Apply an extender to this notification builder. Extenders may be used to add
4340 * metadata or change options on this builder.
4341 */
Griff Hazen61a9e862014-05-22 16:05:19 -07004342 public Builder extend(Extender extender) {
4343 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004344 return this;
4345 }
4346
Dan Sandler4e787062015-06-17 15:09:48 -04004347 /**
4348 * @hide
4349 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04004350 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08004351 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004352 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004353 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004354 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004355 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04004356 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08004357 }
4358
Dan Sandler26e81cf2014-05-06 10:01:27 -04004359 /**
4360 * Sets {@link Notification#color}.
4361 *
4362 * @param argb The accent color to use
4363 *
4364 * @return The same Builder.
4365 */
Tor Norbye80756e32015-03-02 09:39:27 -08004366 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004367 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004368 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04004369 return this;
4370 }
4371
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004372 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04004373 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
4374 // This user can never be a badged profile,
4375 // and also includes USER_ALL system notifications.
4376 return null;
4377 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02004378 // Note: This assumes that the current user can read the profile badge of the
4379 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08004380 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05004381 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004382 }
4383
4384 private Bitmap getProfileBadge() {
4385 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01004386 if (badge == null) {
4387 return null;
4388 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004389 final int size = mContext.getResources().getDimensionPixelSize(
4390 R.dimen.notification_badge_size);
4391 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004392 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004393 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004394 badge.draw(canvas);
4395 return bitmap;
4396 }
4397
Selim Cinekc848c3a2016-01-13 15:27:30 -08004398 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01004399 Bitmap profileBadge = getProfileBadge();
4400
Kenny Guy98193ea2014-07-24 19:54:37 +01004401 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004402 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
4403 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004404 if (isColorized()) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004405 contentView.setDrawableTint(R.id.profile_badge, false,
4406 getPrimaryTextColor(), PorterDuff.Mode.SRC_ATOP);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004407 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004408 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004409 }
4410
Julia Reynoldsfc640012018-02-21 12:25:27 -05004411 /**
4412 * @hide
4413 */
4414 public boolean usesStandardHeader() {
4415 if (mN.mUsesStandardHeader) {
4416 return true;
4417 }
4418 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.N) {
4419 if (mN.contentView == null && mN.bigContentView == null) {
4420 return true;
4421 }
4422 }
4423 boolean contentViewUsesHeader = mN.contentView == null
4424 || STANDARD_LAYOUTS.contains(mN.contentView.getLayoutId());
4425 boolean bigContentViewUsesHeader = mN.bigContentView == null
4426 || STANDARD_LAYOUTS.contains(mN.bigContentView.getLayoutId());
4427 return contentViewUsesHeader && bigContentViewUsesHeader;
4428 }
4429
Christoph Studerfe718432014-09-01 18:21:18 +02004430 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004431 resetNotificationHeader(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02004432 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08004433 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004434 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08004435 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004436 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08004437 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08004438 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004439 }
4440
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004441 /**
4442 * Resets the notification header to its original state
4443 */
4444 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07004445 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
4446 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08004447 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004448 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02004449 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004450 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07004451 contentView.setTextViewText(R.id.header_text, null);
Selim Cinekafeed292017-12-12 17:32:44 -08004452 contentView.setViewVisibility(R.id.header_text_secondary, View.GONE);
4453 contentView.setTextViewText(R.id.header_text_secondary, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004454 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinekafeed292017-12-12 17:32:44 -08004455 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004456 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004457 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004458 contentView.setImageViewIcon(R.id.profile_badge, null);
4459 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Gus Prevasa3226492018-10-23 11:10:09 -04004460 contentView.setViewVisibility(R.id.alerted_icon, View.GONE);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004461 mN.mUsesStandardHeader = false;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004462 }
4463
Selim Cinek384804b2018-04-18 14:31:07 +08004464 private RemoteViews applyStandardTemplate(int resId, TemplateBindResult result) {
4465 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this),
4466 result);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004467 }
4468
4469 /**
4470 * @param hasProgress whether the progress bar should be shown and set
Selim Cinek384804b2018-04-18 14:31:07 +08004471 * @param result
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004472 */
Selim Cinek384804b2018-04-18 14:31:07 +08004473 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress,
4474 TemplateBindResult result) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004475 return applyStandardTemplate(resId, mParams.reset().hasProgress(hasProgress)
Selim Cinek384804b2018-04-18 14:31:07 +08004476 .fillTextsFrom(this), result);
Adrian Roosc1a80b02016-04-05 14:54:55 -07004477 }
4478
Selim Cinek384804b2018-04-18 14:31:07 +08004479 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p,
4480 TemplateBindResult result) {
Kenny Guy77320062014-08-27 21:37:15 +01004481 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04004482
Christoph Studerfe718432014-09-01 18:21:18 +02004483 resetStandardTemplate(contentView);
4484
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004485 final Bundle ex = mN.extras;
Selim Cinek7b9605b2017-01-19 17:36:00 -08004486 updateBackgroundColor(contentView);
Selim Cinekafeed292017-12-12 17:32:44 -08004487 bindNotificationHeader(contentView, p.ambient, p.headerTextSecondary);
Selim Cinek384804b2018-04-18 14:31:07 +08004488 bindLargeIconAndReply(contentView, p, result);
Adrian Roos70d7aa32017-01-11 15:39:06 -08004489 boolean showProgress = handleProgressBar(p.hasProgress, contentView, ex);
4490 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08004491 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004492 contentView.setTextViewText(R.id.title, processTextSpans(p.title));
Adrian Roos72171622017-01-27 10:32:06 -08004493 if (!p.ambient) {
4494 setTextViewColorPrimary(contentView, R.id.title);
4495 }
Selim Cinek954cc232016-05-20 13:29:23 -07004496 contentView.setViewLayoutWidth(R.id.title, showProgress
4497 ? ViewGroup.LayoutParams.WRAP_CONTENT
4498 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08004499 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08004500 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08004501 int textId = showProgress ? com.android.internal.R.id.text_line_1
4502 : com.android.internal.R.id.text;
Selim Cinek48f66b72017-08-18 16:17:51 -07004503 contentView.setTextViewText(textId, processTextSpans(p.text));
Adrian Roos72171622017-01-27 10:32:06 -08004504 if (!p.ambient) {
4505 setTextViewColorSecondary(contentView, textId);
4506 }
Selim Cinek41598732016-01-11 16:58:37 -08004507 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08004508 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08004509
Selim Cinek279fa862016-06-14 10:57:25 -07004510 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004511
Selim Cinek29603462015-11-17 19:04:39 -08004512 return contentView;
4513 }
4514
Selim Cinek48f66b72017-08-18 16:17:51 -07004515 private CharSequence processTextSpans(CharSequence text) {
Lucas Dupind3c99322018-09-27 10:22:29 -07004516 if (hasForegroundColor() || mInNightMode) {
Lucas Dupina291d192018-06-07 13:59:42 -07004517 return ContrastColorUtil.clearColorSpans(text);
Selim Cinek48f66b72017-08-18 16:17:51 -07004518 }
4519 return text;
4520 }
4521
Selim Cinek7b9605b2017-01-19 17:36:00 -08004522 private void setTextViewColorPrimary(RemoteViews contentView, int id) {
4523 ensureColors();
4524 contentView.setTextColor(id, mPrimaryTextColor);
4525 }
4526
Selim Cinek48f66b72017-08-18 16:17:51 -07004527 private boolean hasForegroundColor() {
4528 return mForegroundColor != COLOR_INVALID;
4529 }
4530
Selim Cinek389edcd2017-05-11 19:16:44 -07004531 /**
4532 * @return the primary text color
4533 * @hide
4534 */
4535 @VisibleForTesting
4536 public int getPrimaryTextColor() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004537 ensureColors();
4538 return mPrimaryTextColor;
4539 }
4540
Selim Cinek389edcd2017-05-11 19:16:44 -07004541 /**
4542 * @return the secondary text color
4543 * @hide
4544 */
4545 @VisibleForTesting
4546 public int getSecondaryTextColor() {
4547 ensureColors();
4548 return mSecondaryTextColor;
4549 }
4550
Selim Cinek7b9605b2017-01-19 17:36:00 -08004551 private void setTextViewColorSecondary(RemoteViews contentView, int id) {
4552 ensureColors();
4553 contentView.setTextColor(id, mSecondaryTextColor);
4554 }
4555
4556 private void ensureColors() {
4557 int backgroundColor = getBackgroundColor();
4558 if (mPrimaryTextColor == COLOR_INVALID
4559 || mSecondaryTextColor == COLOR_INVALID
Selim Cinek7b9605b2017-01-19 17:36:00 -08004560 || mTextColorsAreForBackground != backgroundColor) {
4561 mTextColorsAreForBackground = backgroundColor;
Selim Cinek48f66b72017-08-18 16:17:51 -07004562 if (!hasForegroundColor() || !isColorized()) {
Lucas Dupina291d192018-06-07 13:59:42 -07004563 mPrimaryTextColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004564 backgroundColor, mInNightMode);
Lucas Dupina291d192018-06-07 13:59:42 -07004565 mSecondaryTextColor = ContrastColorUtil.resolveSecondaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004566 backgroundColor, mInNightMode);
Selim Cinekc7f5a822018-03-20 19:32:06 -07004567 if (backgroundColor != COLOR_DEFAULT && isColorized()) {
Lucas Dupina291d192018-06-07 13:59:42 -07004568 mPrimaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004569 mPrimaryTextColor, backgroundColor, 4.5);
Lucas Dupina291d192018-06-07 13:59:42 -07004570 mSecondaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004571 mSecondaryTextColor, backgroundColor, 4.5);
4572 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07004573 } else {
Lucas Dupina291d192018-06-07 13:59:42 -07004574 double backLum = ContrastColorUtil.calculateLuminance(backgroundColor);
4575 double textLum = ContrastColorUtil.calculateLuminance(mForegroundColor);
4576 double contrast = ContrastColorUtil.calculateContrast(mForegroundColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004577 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07004578 // We only respect the given colors if worst case Black or White still has
4579 // contrast
4580 boolean backgroundLight = backLum > textLum
4581 && satisfiesTextContrast(backgroundColor, Color.BLACK)
4582 || backLum <= textLum
4583 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004584 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07004585 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004586 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004587 mForegroundColor,
4588 backgroundColor,
4589 true /* findFG */,
4590 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004591 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004592 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004593 } else {
4594 mSecondaryTextColor =
Lucas Dupina291d192018-06-07 13:59:42 -07004595 ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004596 mForegroundColor,
4597 backgroundColor,
4598 true /* findFG */,
4599 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004600 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004601 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004602 }
4603 } else {
4604 mPrimaryTextColor = mForegroundColor;
Lucas Dupina291d192018-06-07 13:59:42 -07004605 mSecondaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004606 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4607 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Lucas Dupina291d192018-06-07 13:59:42 -07004608 if (ContrastColorUtil.calculateContrast(mSecondaryTextColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004609 backgroundColor) < 4.5f) {
4610 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07004611 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004612 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004613 mSecondaryTextColor,
4614 backgroundColor,
4615 true /* findFG */,
4616 4.5f);
4617 } else {
4618 mSecondaryTextColor
Lucas Dupina291d192018-06-07 13:59:42 -07004619 = ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004620 mSecondaryTextColor,
4621 backgroundColor,
4622 true /* findFG */,
4623 4.5f);
4624 }
Lucas Dupina291d192018-06-07 13:59:42 -07004625 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004626 mSecondaryTextColor, backgroundLight
4627 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4628 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004629 }
4630 }
4631 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004632 }
4633 }
4634
4635 private void updateBackgroundColor(RemoteViews contentView) {
4636 if (isColorized()) {
4637 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
4638 getBackgroundColor());
4639 } else {
4640 // Clear it!
4641 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4642 0);
4643 }
4644 }
4645
Selim Cinek860b6da2015-12-16 19:02:19 -08004646 /**
4647 * @param remoteView the remote view to update the minheight in
4648 * @param hasMinHeight does it have a mimHeight
4649 * @hide
4650 */
4651 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4652 int minHeight = 0;
4653 if (hasMinHeight) {
4654 // we need to set the minHeight of the notification
4655 minHeight = mContext.getResources().getDimensionPixelSize(
4656 com.android.internal.R.dimen.notification_min_content_height);
4657 }
4658 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4659 }
4660
Selim Cinek29603462015-11-17 19:04:39 -08004661 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004662 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4663 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4664 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4665 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004666 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004667 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004668 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004669 contentView.setProgressBackgroundTintList(
4670 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4671 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08004672 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08004673 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004674 contentView.setProgressTintList(R.id.progress, colorStateList);
4675 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004676 }
Selim Cinek29603462015-11-17 19:04:39 -08004677 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004678 } else {
4679 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004680 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004681 }
Joe Onorato561d3852010-11-20 18:09:34 -08004682 }
4683
Selim Cinek384804b2018-04-18 14:31:07 +08004684 private void bindLargeIconAndReply(RemoteViews contentView, StandardTemplateParams p,
4685 TemplateBindResult result) {
4686 boolean largeIconShown = bindLargeIcon(contentView, p.hideLargeIcon || p.ambient);
Selim Cinek1c72fa02018-04-23 18:00:54 +08004687 boolean replyIconShown = bindReplyIcon(contentView, p.hideReplyIcon || p.ambient);
Selim Cinek384804b2018-04-18 14:31:07 +08004688 contentView.setViewVisibility(R.id.right_icon_container,
Selim Cinek1c72fa02018-04-23 18:00:54 +08004689 largeIconShown || replyIconShown ? View.VISIBLE : View.GONE);
4690 int marginEnd = calculateMarginEnd(largeIconShown, replyIconShown);
4691 contentView.setViewLayoutMarginEnd(R.id.line1, marginEnd);
4692 contentView.setViewLayoutMarginEnd(R.id.text, marginEnd);
4693 contentView.setViewLayoutMarginEnd(R.id.progress, marginEnd);
Selim Cinek384804b2018-04-18 14:31:07 +08004694 if (result != null) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08004695 result.setIconMarginEnd(marginEnd);
Selim Cinek384804b2018-04-18 14:31:07 +08004696 }
4697 }
4698
Selim Cinek1c72fa02018-04-23 18:00:54 +08004699 private int calculateMarginEnd(boolean largeIconShown, boolean replyIconShown) {
4700 int marginEnd = 0;
4701 int contentMargin = mContext.getResources().getDimensionPixelSize(
4702 R.dimen.notification_content_margin_end);
4703 int iconSize = mContext.getResources().getDimensionPixelSize(
4704 R.dimen.notification_right_icon_size);
4705 if (replyIconShown) {
4706 // The size of the reply icon
4707 marginEnd += iconSize;
4708
4709 int replyInset = mContext.getResources().getDimensionPixelSize(
4710 R.dimen.notification_reply_inset);
4711 // We're subtracting the inset of the reply icon to make sure it's
4712 // aligned nicely on the right, and remove it from the following padding
4713 marginEnd -= replyInset * 2;
4714 }
4715 if (largeIconShown) {
4716 // adding size of the right icon
4717 marginEnd += iconSize;
4718
4719 if (replyIconShown) {
4720 // We also add some padding to the reply icon if it's around
4721 marginEnd += contentMargin;
4722 }
4723 }
4724 if (replyIconShown || largeIconShown) {
4725 // The padding to the content
4726 marginEnd += contentMargin;
4727 }
4728 return marginEnd;
4729 }
4730
Selim Cinek384804b2018-04-18 14:31:07 +08004731 /**
4732 * Bind the large icon.
4733 * @return if the largeIcon is visible
4734 */
4735 private boolean bindLargeIcon(RemoteViews contentView, boolean hideLargeIcon) {
Selim Cinek279fa862016-06-14 10:57:25 -07004736 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4737 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4738 }
Selim Cinek88188f22017-09-19 16:46:56 -07004739 boolean showLargeIcon = mN.mLargeIcon != null && !hideLargeIcon;
4740 if (showLargeIcon) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004741 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4742 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
4743 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Selim Cinek88188f22017-09-19 16:46:56 -07004744 }
Selim Cinek384804b2018-04-18 14:31:07 +08004745 return showLargeIcon;
4746 }
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004747
Selim Cinek384804b2018-04-18 14:31:07 +08004748 /**
4749 * Bind the reply icon.
4750 * @return if the reply icon is visible
4751 */
4752 private boolean bindReplyIcon(RemoteViews contentView, boolean hideReplyIcon) {
4753 boolean actionVisible = !hideReplyIcon;
4754 Action action = null;
Selim Cinek88188f22017-09-19 16:46:56 -07004755 if (actionVisible) {
Selim Cinek384804b2018-04-18 14:31:07 +08004756 action = findReplyAction();
4757 actionVisible = action != null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004758 }
Selim Cinek384804b2018-04-18 14:31:07 +08004759 if (actionVisible) {
4760 contentView.setViewVisibility(R.id.reply_icon_action, View.VISIBLE);
4761 contentView.setDrawableTint(R.id.reply_icon_action,
4762 false /* targetBackground */,
Selim Cinek4717d862018-04-19 09:19:15 +08004763 getNeutralColor(),
Selim Cinek384804b2018-04-18 14:31:07 +08004764 PorterDuff.Mode.SRC_ATOP);
4765 contentView.setOnClickPendingIntent(R.id.reply_icon_action, action.actionIntent);
4766 contentView.setRemoteInputs(R.id.reply_icon_action, action.mRemoteInputs);
4767 } else {
4768 contentView.setRemoteInputs(R.id.reply_icon_action, null);
4769 }
4770 contentView.setViewVisibility(R.id.reply_icon_action,
4771 actionVisible ? View.VISIBLE : View.GONE);
4772 return actionVisible;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004773 }
4774
4775 private Action findReplyAction() {
4776 ArrayList<Action> actions = mActions;
4777 if (mOriginalActions != null) {
4778 actions = mOriginalActions;
4779 }
4780 int numActions = actions.size();
4781 for (int i = 0; i < numActions; i++) {
4782 Action action = actions.get(i);
4783 if (hasValidRemoteInput(action)) {
4784 return action;
4785 }
4786 }
4787 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004788 }
4789
Selim Cinekafeed292017-12-12 17:32:44 -08004790 private void bindNotificationHeader(RemoteViews contentView, boolean ambient,
4791 CharSequence secondaryHeaderText) {
Adrian Roos487374f2017-01-11 15:48:14 -08004792 bindSmallIcon(contentView, ambient);
4793 bindHeaderAppName(contentView, ambient);
4794 if (!ambient) {
4795 // Ambient view does not have these
4796 bindHeaderText(contentView);
Selim Cinekafeed292017-12-12 17:32:44 -08004797 bindHeaderTextSecondary(contentView, secondaryHeaderText);
Adrian Roos487374f2017-01-11 15:48:14 -08004798 bindHeaderChronometerAndTime(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08004799 bindProfileBadge(contentView);
4800 }
Julia Reynolds6013e5c2018-04-18 08:43:34 -04004801 bindActivePermissions(contentView, ambient);
Adrian Roosd83e9992017-03-16 15:17:57 -07004802 bindExpandButton(contentView);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004803 mN.mUsesStandardHeader = true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004804 }
4805
Julia Reynolds6013e5c2018-04-18 08:43:34 -04004806 private void bindActivePermissions(RemoteViews contentView, boolean ambient) {
Selim Cinek4717d862018-04-19 09:19:15 +08004807 int color = ambient ? resolveAmbientColor() : getNeutralColor();
Julia Reynoldsb887b232018-04-10 16:38:08 -04004808 contentView.setDrawableTint(R.id.camera, false, color, PorterDuff.Mode.SRC_ATOP);
4809 contentView.setDrawableTint(R.id.mic, false, color, PorterDuff.Mode.SRC_ATOP);
4810 contentView.setDrawableTint(R.id.overlay, false, color, PorterDuff.Mode.SRC_ATOP);
4811 }
4812
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004813 private void bindExpandButton(RemoteViews contentView) {
Selim Cinekc7f5a822018-03-20 19:32:06 -07004814 int color = isColorized() ? getPrimaryTextColor() : getSecondaryTextColor();
Sunny Goyal5b153922017-09-21 21:00:36 -07004815 contentView.setDrawableTint(R.id.expand_button, false, color,
4816 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08004817 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08004818 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004819 }
4820
4821 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
4822 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08004823 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004824 setTextViewColorSecondary(contentView, R.id.time_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004825 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
4826 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
4827 contentView.setLong(R.id.chronometer, "setBase",
4828 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
4829 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07004830 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07004831 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004832 setTextViewColorSecondary(contentView, R.id.chronometer);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004833 } else {
4834 contentView.setViewVisibility(R.id.time, View.VISIBLE);
4835 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004836 setTextViewColorSecondary(contentView, R.id.time);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004837 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004838 } else {
4839 // We still want a time to be set but gone, such that we can show and hide it
4840 // on demand in case it's a child notification without anything in the header
4841 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004842 }
4843 }
4844
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004845 private void bindHeaderText(RemoteViews contentView) {
4846 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4847 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05004848 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004849 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05004850 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004851 if (headerText == null
4852 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4853 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
4854 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
4855 }
4856 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004857 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek48f66b72017-08-18 16:17:51 -07004858 contentView.setTextViewText(R.id.header_text, processTextSpans(
4859 processLegacyText(headerText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004860 setTextViewColorSecondary(contentView, R.id.header_text);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004861 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
4862 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004863 setTextViewColorSecondary(contentView, R.id.header_text_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004864 }
4865 }
4866
Selim Cinekafeed292017-12-12 17:32:44 -08004867 private void bindHeaderTextSecondary(RemoteViews contentView, CharSequence secondaryText) {
4868 if (!TextUtils.isEmpty(secondaryText)) {
4869 contentView.setTextViewText(R.id.header_text_secondary, processTextSpans(
4870 processLegacyText(secondaryText)));
4871 setTextViewColorSecondary(contentView, R.id.header_text_secondary);
4872 contentView.setViewVisibility(R.id.header_text_secondary, View.VISIBLE);
4873 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.VISIBLE);
4874 setTextViewColorSecondary(contentView, R.id.header_text_secondary_divider);
4875 }
4876 }
4877
Adrian Rooseba05822016-04-22 17:09:27 -07004878 /**
4879 * @hide
4880 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01004881 @UnsupportedAppUsage
Adrian Rooseba05822016-04-22 17:09:27 -07004882 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004883 CharSequence name = null;
4884 final PackageManager pm = mContext.getPackageManager();
4885 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
4886 // only system packages which lump together a bunch of unrelated stuff
4887 // may substitute a different name to make the purpose of the
4888 // notification more clear. the correct package label should always
4889 // be accessible via SystemUI.
4890 final String pkg = mContext.getPackageName();
4891 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
4892 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
4893 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
4894 name = subName;
4895 } else {
4896 Log.w(TAG, "warning: pkg "
4897 + pkg + " attempting to substitute app name '" + subName
4898 + "' without holding perm "
4899 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
4900 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004901 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04004902 if (TextUtils.isEmpty(name)) {
4903 name = pm.getApplicationLabel(mContext.getApplicationInfo());
4904 }
4905 if (TextUtils.isEmpty(name)) {
4906 // still nothing?
4907 return null;
4908 }
4909
4910 return String.valueOf(name);
4911 }
Adrian Roos487374f2017-01-11 15:48:14 -08004912 private void bindHeaderAppName(RemoteViews contentView, boolean ambient) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004913 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos72171622017-01-27 10:32:06 -08004914 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004915 setTextViewColorPrimary(contentView, R.id.app_name_text);
4916 } else {
4917 contentView.setTextColor(R.id.app_name_text,
Selim Cinekc7f5a822018-03-20 19:32:06 -07004918 ambient ? resolveAmbientColor() : getSecondaryTextColor());
Selim Cinek7b9605b2017-01-19 17:36:00 -08004919 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004920 }
4921
Adrian Roos487374f2017-01-11 15:48:14 -08004922 private void bindSmallIcon(RemoteViews contentView, boolean ambient) {
Selim Cinek279fa862016-06-14 10:57:25 -07004923 if (mN.mSmallIcon == null && mN.icon != 0) {
4924 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
4925 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004926 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Sunny Goyal5b153922017-09-21 21:00:36 -07004927 contentView.setInt(R.id.icon, "setImageLevel", mN.iconLevel);
Adrian Roos487374f2017-01-11 15:48:14 -08004928 processSmallIconColor(mN.mSmallIcon, contentView, ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004929 }
4930
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004931 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004932 * @return true if the built notification will show the time or the chronometer; false
4933 * otherwise
4934 */
4935 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07004936 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004937 }
4938
Christoph Studerfe718432014-09-01 18:21:18 +02004939 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004940 // actions_container is only reset when there are no actions to avoid focus issues with
4941 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02004942 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004943 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08004944
4945 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
4946 big.setTextViewText(R.id.notification_material_reply_text_1, null);
Kenny Guya0f6de82018-04-06 16:20:16 +01004947 big.setViewVisibility(R.id.notification_material_reply_text_1_container, View.GONE);
4948 big.setViewVisibility(R.id.notification_material_reply_progress, View.GONE);
Adrian Roose458aa82015-12-08 16:17:19 -08004949
4950 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
4951 big.setTextViewText(R.id.notification_material_reply_text_2, null);
4952 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
4953 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07004954
Selim Cineked64a142018-02-06 18:06:01 -08004955 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
4956 R.dimen.notification_content_margin);
Christoph Studerfe718432014-09-01 18:21:18 +02004957 }
4958
Selim Cinek384804b2018-04-18 14:31:07 +08004959 private RemoteViews applyStandardTemplateWithActions(int layoutId,
4960 TemplateBindResult result) {
4961 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this),
4962 result);
Adrian Roos48d746a2016-04-12 14:57:28 -07004963 }
4964
Adrian Roos70d7aa32017-01-11 15:39:06 -08004965 private RemoteViews applyStandardTemplateWithActions(int layoutId,
Selim Cinek384804b2018-04-18 14:31:07 +08004966 StandardTemplateParams p, TemplateBindResult result) {
4967 RemoteViews big = applyStandardTemplate(layoutId, p, result);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004968
Christoph Studerfe718432014-09-01 18:21:18 +02004969 resetStandardTemplateWithActions(big);
4970
Adrian Roose458aa82015-12-08 16:17:19 -08004971 boolean validRemoteInput = false;
4972
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004973 int N = mActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08004974 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004975 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004976 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08004977 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004978 big.setViewVisibility(R.id.actions, View.VISIBLE);
Selim Cineked64a142018-02-06 18:06:01 -08004979 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Daniel Sandler8680bf82012-05-15 16:52:52 -04004980 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004981 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08004982 Action action = mActions.get(i);
Selim Cinekffcc7cf2018-02-06 17:43:51 -08004983 boolean actionHasValidInput = hasValidRemoteInput(action);
4984 validRemoteInput |= actionHasValidInput;
Adrian Roose458aa82015-12-08 16:17:19 -08004985
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004986 final RemoteViews button = generateActionButton(action, emphazisedMode,
Selim Cinek396caca2018-04-10 17:46:46 -07004987 p.ambient);
4988 if (actionHasValidInput && !emphazisedMode) {
Selim Cinekffcc7cf2018-02-06 17:43:51 -08004989 // Clear the drawable
4990 button.setInt(R.id.action0, "setBackgroundResource", 0);
4991 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004992 big.addView(R.id.actions, button);
4993 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004994 } else {
4995 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004996 }
Adrian Roose458aa82015-12-08 16:17:19 -08004997
4998 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08004999 if (!p.ambient && validRemoteInput && replyText != null
Selim Cinekbee4e072018-05-21 22:06:43 -07005000 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])
5001 && p.maxRemoteInputHistory > 0) {
Kenny Guya0f6de82018-04-06 16:20:16 +01005002 boolean showSpinner = mN.extras.getBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER);
Adrian Roose458aa82015-12-08 16:17:19 -08005003 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Kenny Guya0f6de82018-04-06 16:20:16 +01005004 big.setViewVisibility(R.id.notification_material_reply_text_1_container,
5005 View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005006 big.setTextViewText(R.id.notification_material_reply_text_1,
5007 processTextSpans(replyText[0]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005008 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1);
Kenny Guya0f6de82018-04-06 16:20:16 +01005009 big.setViewVisibility(R.id.notification_material_reply_progress,
5010 showSpinner ? View.VISIBLE : View.GONE);
5011 big.setProgressIndeterminateTintList(
5012 R.id.notification_material_reply_progress,
5013 ColorStateList.valueOf(
5014 isColorized() ? getPrimaryTextColor() : resolveContrastColor()));
Adrian Roose458aa82015-12-08 16:17:19 -08005015
Selim Cinekbee4e072018-05-21 22:06:43 -07005016 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])
5017 && p.maxRemoteInputHistory > 1) {
Adrian Roose458aa82015-12-08 16:17:19 -08005018 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005019 big.setTextViewText(R.id.notification_material_reply_text_2,
5020 processTextSpans(replyText[1]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005021 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2);
Adrian Roose458aa82015-12-08 16:17:19 -08005022
Selim Cinekbee4e072018-05-21 22:06:43 -07005023 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])
5024 && p.maxRemoteInputHistory > 2) {
Adrian Roose458aa82015-12-08 16:17:19 -08005025 big.setViewVisibility(
5026 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005027 big.setTextViewText(R.id.notification_material_reply_text_3,
5028 processTextSpans(replyText[2]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005029 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3);
Adrian Roose458aa82015-12-08 16:17:19 -08005030 }
5031 }
5032 }
5033
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005034 return big;
5035 }
5036
Adrian Roose458aa82015-12-08 16:17:19 -08005037 private boolean hasValidRemoteInput(Action action) {
5038 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
5039 // Weird actions
5040 return false;
5041 }
5042
5043 RemoteInput[] remoteInputs = action.getRemoteInputs();
5044 if (remoteInputs == null) {
5045 return false;
5046 }
5047
5048 for (RemoteInput r : remoteInputs) {
5049 CharSequence[] choices = r.getChoices();
5050 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
5051 return true;
5052 }
5053 }
5054 return false;
5055 }
5056
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005057 /**
5058 * Construct a RemoteViews for the final 1U notification layout. In order:
5059 * 1. Custom contentView from the caller
5060 * 2. Style's proposed content view
5061 * 3. Standard template view
5062 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005063 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005064 return createContentView(false /* increasedheight */ );
5065 }
5066
5067 /**
5068 * Construct a RemoteViews for the smaller content view.
5069 *
5070 * @param increasedHeight true if this layout be created with an increased height. Some
5071 * styles may support showing more then just that basic 1U size
5072 * and the system may decide to render important notifications
5073 * slightly bigger even when collapsed.
5074 *
5075 * @hide
5076 */
5077 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005078 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005079 return mN.contentView;
5080 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005081 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005082 if (styleView != null) {
5083 return styleView;
5084 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005085 }
Selim Cinek384804b2018-04-18 14:31:07 +08005086 return applyStandardTemplate(getBaseLayoutResource(), null /* result */);
Joe Onorato46439ce2010-11-19 13:56:21 -08005087 }
5088
Selim Cineka7679b62017-05-10 16:33:25 -07005089 private boolean useExistingRemoteView() {
5090 return mStyle == null || (!mStyle.displayCustomViewInline()
5091 && !mRebuildStyledRemoteViews);
5092 }
5093
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005094 /**
5095 * Construct a RemoteViews for the final big notification layout.
5096 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005097 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05005098 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07005099 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005100 return mN.bigContentView;
5101 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05005102 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08005103 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005104 } else if (mActions.size() != 0) {
Selim Cinek384804b2018-04-18 14:31:07 +08005105 result = applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5106 null /* result */);
Selim Cinek850a8542015-11-11 11:48:36 -05005107 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08005108 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05005109 return result;
5110 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005111
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005112 /**
Selim Cinek414ad332017-02-24 19:06:12 -08005113 * Construct a RemoteViews for the final notification header only. This will not be
5114 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005115 *
Adrian Roos6f6e1592017-05-02 16:22:53 -07005116 * @param ambient if true, generate the header for the ambient display layout.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005117 * @hide
5118 */
Adrian Roos6f6e1592017-05-02 16:22:53 -07005119 public RemoteViews makeNotificationHeader(boolean ambient) {
Selim Cinek414ad332017-02-24 19:06:12 -08005120 Boolean colorized = (Boolean) mN.extras.get(EXTRA_COLORIZED);
5121 mN.extras.putBoolean(EXTRA_COLORIZED, false);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005122 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Adrian Roos6f6e1592017-05-02 16:22:53 -07005123 ambient ? R.layout.notification_template_ambient_header
5124 : R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005125 resetNotificationHeader(header);
Selim Cinekafeed292017-12-12 17:32:44 -08005126 bindNotificationHeader(header, ambient, null);
Selim Cinek414ad332017-02-24 19:06:12 -08005127 if (colorized != null) {
5128 mN.extras.putBoolean(EXTRA_COLORIZED, colorized);
5129 } else {
5130 mN.extras.remove(EXTRA_COLORIZED);
5131 }
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005132 return header;
5133 }
5134
Adrian Roos487374f2017-01-11 15:48:14 -08005135 /**
5136 * Construct a RemoteViews for the ambient version of the notification.
5137 *
5138 * @hide
5139 */
5140 public RemoteViews makeAmbientNotification() {
5141 RemoteViews ambient = applyStandardTemplateWithActions(
5142 R.layout.notification_template_material_ambient,
Selim Cinek384804b2018-04-18 14:31:07 +08005143 mParams.reset().ambient(true).fillTextsFrom(this).hasProgress(false),
5144 null /* result */);
Adrian Roos487374f2017-01-11 15:48:14 -08005145 return ambient;
5146 }
5147
Selim Cinek29603462015-11-17 19:04:39 -08005148 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005149 if (result != null) {
5150 result.setViewVisibility(R.id.text_line_1, View.GONE);
5151 }
Selim Cinek29603462015-11-17 19:04:39 -08005152 }
5153
Selim Cinek6743c0b2017-01-18 18:24:01 -08005154 /**
5155 * Adapt the Notification header if this view is used as an expanded view.
5156 *
5157 * @hide
5158 */
5159 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005160 if (result != null) {
5161 result.setBoolean(R.id.notification_header, "setExpanded", true);
5162 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005163 }
5164
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005165 /**
5166 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005167 *
5168 * @param increasedHeight true if this layout be created with an increased height. Some
5169 * styles may support showing more then just that basic 1U size
5170 * and the system may decide to render important notifications
5171 * slightly bigger even when collapsed.
5172 *
5173 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005174 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005175 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005176 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005177 return mN.headsUpContentView;
5178 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08005179 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
5180 if (styleView != null) {
5181 return styleView;
5182 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05005183 } else if (mActions.size() == 0) {
5184 return null;
5185 }
5186
Selim Cinekbee4e072018-05-21 22:06:43 -07005187 // We only want at most a single remote input history to be shown here, otherwise
5188 // the content would become squished.
5189 StandardTemplateParams p = mParams.reset().fillTextsFrom(this)
5190 .setMaxRemoteInputHistory(1);
5191 return applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5192 p,
5193 null /* result */);
Chris Wren8fd39ec2014-02-27 17:43:26 -05005194 }
5195
Selim Cinek624c02db2015-12-14 21:00:02 -08005196 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08005197 * Construct a RemoteViews for the final heads-up notification layout.
5198 */
5199 public RemoteViews createHeadsUpContentView() {
5200 return createHeadsUpContentView(false /* useIncreasedHeight */);
5201 }
5202
5203 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08005204 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5205 *
5206 * @hide
5207 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005208 @UnsupportedAppUsage
Selim Cinek624c02db2015-12-14 21:00:02 -08005209 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005210 return makePublicView(false /* ambient */);
5211 }
5212
5213 /**
5214 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5215 *
5216 * @hide
5217 */
5218 public RemoteViews makePublicAmbientNotification() {
5219 return makePublicView(true /* ambient */);
5220 }
5221
5222 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08005223 if (mN.publicVersion != null) {
5224 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005225 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08005226 }
5227 Bundle savedBundle = mN.extras;
5228 Style style = mStyle;
5229 mStyle = null;
5230 Icon largeIcon = mN.mLargeIcon;
5231 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07005232 Bitmap largeIconLegacy = mN.largeIcon;
5233 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005234 ArrayList<Action> actions = mActions;
5235 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08005236 Bundle publicExtras = new Bundle();
5237 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
5238 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
5239 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
5240 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07005241 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
5242 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cineked07b962018-04-30 14:39:35 -07005243 String appName = savedBundle.getString(EXTRA_SUBSTITUTE_APP_NAME);
5244 if (appName != null) {
5245 publicExtras.putString(EXTRA_SUBSTITUTE_APP_NAME, appName);
5246 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005247 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07005248 RemoteViews view;
5249 if (ambient) {
5250 publicExtras.putCharSequence(EXTRA_TITLE,
5251 mContext.getString(com.android.internal.R.string.notification_hidden_text));
5252 view = makeAmbientNotification();
5253 } else{
5254 view = makeNotificationHeader(false /* ambient */);
5255 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
5256 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005257 mN.extras = savedBundle;
5258 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07005259 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005260 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08005261 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005262 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08005263 }
5264
Selim Cinek6743c0b2017-01-18 18:24:01 -08005265 /**
5266 * Construct a content view for the display when low - priority
5267 *
5268 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
5269 * a new subtext is created consisting of the content of the
5270 * notification.
5271 * @hide
5272 */
5273 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
5274 int color = mN.color;
5275 mN.color = COLOR_DEFAULT;
5276 CharSequence summary = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
5277 if (!useRegularSubtext || TextUtils.isEmpty(summary)) {
5278 CharSequence newSummary = createSummaryText();
5279 if (!TextUtils.isEmpty(newSummary)) {
5280 mN.extras.putCharSequence(EXTRA_SUB_TEXT, newSummary);
5281 }
5282 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08005283
Adrian Roos6f6e1592017-05-02 16:22:53 -07005284 RemoteViews header = makeNotificationHeader(false /* ambient */);
Selim Cinek1b554392017-02-28 17:22:49 -08005285 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08005286 if (summary != null) {
5287 mN.extras.putCharSequence(EXTRA_SUB_TEXT, summary);
5288 } else {
5289 mN.extras.remove(EXTRA_SUB_TEXT);
5290 }
5291 mN.color = color;
5292 return header;
5293 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005294
Selim Cinek6743c0b2017-01-18 18:24:01 -08005295 private CharSequence createSummaryText() {
5296 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
5297 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
5298 return titleText;
5299 }
5300 SpannableStringBuilder summary = new SpannableStringBuilder();
5301 if (titleText == null) {
5302 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
5303 }
5304 BidiFormatter bidi = BidiFormatter.getInstance();
5305 if (titleText != null) {
5306 summary.append(bidi.unicodeWrap(titleText));
5307 }
5308 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
5309 if (titleText != null && contentText != null) {
5310 summary.append(bidi.unicodeWrap(mContext.getText(
5311 R.string.notification_header_divider_symbol_with_spaces)));
5312 }
5313 if (contentText != null) {
5314 summary.append(bidi.unicodeWrap(contentText));
5315 }
5316 return summary;
5317 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05005318
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005319 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Selim Cinek396caca2018-04-10 17:46:46 -07005320 boolean ambient) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04005321 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07005322 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005323 emphazisedMode ? getEmphasizedActionLayoutResource()
5324 : tombstone ? getActionTombstoneLayoutResource()
5325 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04005326 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04005327 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04005328 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005329 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005330 if (action.mRemoteInputs != null) {
5331 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
5332 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005333 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07005334 // change the background bgColor
Selim Cinek981962e2016-07-20 20:41:58 -07005335 CharSequence title = action.title;
5336 ColorStateList[] outResultColor = null;
Selim Cinek396caca2018-04-10 17:46:46 -07005337 int background = resolveBackgroundColor();
Selim Cinek981962e2016-07-20 20:41:58 -07005338 if (isLegacy()) {
Lucas Dupina291d192018-06-07 13:59:42 -07005339 title = ContrastColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07005340 } else {
5341 outResultColor = new ColorStateList[1];
Selim Cinek396caca2018-04-10 17:46:46 -07005342 title = ensureColorSpanContrast(title, background, outResultColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005343 }
Selim Cinek48f66b72017-08-18 16:17:51 -07005344 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005345 setTextViewColorPrimary(button, R.id.action0);
Selim Cinek396caca2018-04-10 17:46:46 -07005346 int rippleColor;
5347 boolean hasColorOverride = outResultColor != null && outResultColor[0] != null;
5348 if (hasColorOverride) {
5349 // There's a span spanning the full text, let's take it and use it as the
5350 // background color
5351 background = outResultColor[0].getDefaultColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005352 int textColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07005353 background, mInNightMode);
Selim Cinek396caca2018-04-10 17:46:46 -07005354 button.setTextColor(R.id.action0, textColor);
5355 rippleColor = textColor;
Anthony Chenad4d1582017-04-10 16:07:58 -07005356 } else if (mN.color != COLOR_DEFAULT && !isColorized() && mTintActionButtons) {
Selim Cinek396caca2018-04-10 17:46:46 -07005357 rippleColor = resolveContrastColor();
5358 button.setTextColor(R.id.action0, rippleColor);
5359 } else {
5360 rippleColor = getPrimaryTextColor();
Selim Cinek981962e2016-07-20 20:41:58 -07005361 }
Selim Cinek396caca2018-04-10 17:46:46 -07005362 // We only want about 20% alpha for the ripple
5363 rippleColor = (rippleColor & 0x00ffffff) | 0x33000000;
5364 button.setColorStateList(R.id.action0, "setRippleColor",
5365 ColorStateList.valueOf(rippleColor));
5366 button.setColorStateList(R.id.action0, "setButtonBackground",
5367 ColorStateList.valueOf(background));
5368 button.setBoolean(R.id.action0, "setHasStroke", !hasColorOverride);
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005369 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07005370 button.setTextViewText(R.id.action0, processTextSpans(
5371 processLegacyText(action.title)));
Adrian Roos72171622017-01-27 10:32:06 -08005372 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005373 setTextViewColorPrimary(button, R.id.action0);
Anthony Chenad4d1582017-04-10 16:07:58 -07005374 } else if (mN.color != COLOR_DEFAULT && mTintActionButtons) {
Adrian Roos487374f2017-01-11 15:48:14 -08005375 button.setTextColor(R.id.action0,
5376 ambient ? resolveAmbientColor() : resolveContrastColor());
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005377 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005378 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005379 return button;
5380 }
5381
Joe Onoratocb109a02011-01-18 17:57:41 -08005382 /**
Selim Cinek981962e2016-07-20 20:41:58 -07005383 * Ensures contrast on color spans against a background color. also returns the color of the
5384 * text if a span was found that spans over the whole text.
5385 *
5386 * @param charSequence the charSequence on which the spans are
5387 * @param background the background color to ensure the contrast against
5388 * @param outResultColor an array in which a color will be returned as the first element if
5389 * there exists a full length color span.
5390 * @return the contrasted charSequence
5391 */
5392 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
5393 ColorStateList[] outResultColor) {
5394 if (charSequence instanceof Spanned) {
5395 Spanned ss = (Spanned) charSequence;
5396 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
5397 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
5398 for (Object span : spans) {
5399 Object resultSpan = span;
5400 int spanStart = ss.getSpanStart(span);
5401 int spanEnd = ss.getSpanEnd(span);
5402 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
5403 if (resultSpan instanceof CharacterStyle) {
5404 resultSpan = ((CharacterStyle) span).getUnderlying();
5405 }
5406 if (resultSpan instanceof TextAppearanceSpan) {
5407 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
5408 ColorStateList textColor = originalSpan.getTextColor();
5409 if (textColor != null) {
5410 int[] colors = textColor.getColors();
5411 int[] newColors = new int[colors.length];
5412 for (int i = 0; i < newColors.length; i++) {
Lucas Dupina291d192018-06-07 13:59:42 -07005413 newColors[i] = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005414 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005415 }
5416 textColor = new ColorStateList(textColor.getStates().clone(),
5417 newColors);
Selim Cinek396caca2018-04-10 17:46:46 -07005418 if (fullLength) {
5419 outResultColor[0] = textColor;
5420 // Let's drop the color from the span
5421 textColor = null;
5422 }
Selim Cinek981962e2016-07-20 20:41:58 -07005423 resultSpan = new TextAppearanceSpan(
5424 originalSpan.getFamily(),
5425 originalSpan.getTextStyle(),
5426 originalSpan.getTextSize(),
5427 textColor,
5428 originalSpan.getLinkTextColor());
Selim Cinek981962e2016-07-20 20:41:58 -07005429 }
5430 } else if (resultSpan instanceof ForegroundColorSpan) {
5431 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
5432 int foregroundColor = originalSpan.getForegroundColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005433 foregroundColor = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005434 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005435 if (fullLength) {
5436 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
Selim Cinek396caca2018-04-10 17:46:46 -07005437 resultSpan = null;
5438 } else {
5439 resultSpan = new ForegroundColorSpan(foregroundColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005440 }
5441 } else {
5442 resultSpan = span;
5443 }
Selim Cinek396caca2018-04-10 17:46:46 -07005444 if (resultSpan != null) {
5445 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
5446 }
Selim Cinek981962e2016-07-20 20:41:58 -07005447 }
5448 return builder;
5449 }
5450 return charSequence;
5451 }
5452
5453 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005454 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07005455 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005456 */
5457 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005458 if (!mIsLegacyInitialized) {
5459 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
5460 < Build.VERSION_CODES.LOLLIPOP;
5461 mIsLegacyInitialized = true;
5462 }
5463 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005464 }
5465
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005466 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08005467 return processLegacyText(charSequence, false /* ambient */);
5468 }
5469
5470 private CharSequence processLegacyText(CharSequence charSequence, boolean ambient) {
5471 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
5472 boolean wantLightText = ambient;
5473 if (isAlreadyLightText != wantLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005474 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005475 } else {
5476 return charSequence;
5477 }
5478 }
5479
Dan Sandler26e81cf2014-05-06 10:01:27 -04005480 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005481 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04005482 */
Adrian Roos487374f2017-01-11 15:48:14 -08005483 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
5484 boolean ambient) {
Selim Cinekea4bef72015-12-02 15:51:10 -08005485 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005486 int color;
5487 if (ambient) {
5488 color = resolveAmbientColor();
5489 } else if (isColorized()) {
5490 color = getPrimaryTextColor();
5491 } else {
5492 color = resolveContrastColor();
5493 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005494 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07005495 contentView.setDrawableTint(R.id.icon, false, color,
5496 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005497
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005498 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005499 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08005500 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005501 }
5502
Dan Sandler26e81cf2014-05-06 10:01:27 -04005503 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005504 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04005505 * if it's grayscale).
5506 */
5507 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04005508 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
5509 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005510 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005511 // resolve color will fall back to the default when legacy
Sunny Goyal5b153922017-09-21 21:00:36 -07005512 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(),
5513 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01005514 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005515 }
5516
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05005517 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005518 if (mN.color != COLOR_DEFAULT) {
5519 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02005520 }
Jorim Jaggi74419312014-06-10 20:57:21 +02005521 }
5522
Adrian Roos4ff3b122016-02-01 12:26:13 -08005523 int resolveContrastColor() {
5524 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
5525 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005526 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005527
Selim Cinekac5f0272017-05-02 16:05:41 -07005528 int color;
Selim Cinekc7f5a822018-03-20 19:32:06 -07005529 int background = mContext.getColor(
5530 com.android.internal.R.color.notification_material_background_color);
Selim Cinekac5f0272017-05-02 16:05:41 -07005531 if (mN.color == COLOR_DEFAULT) {
5532 ensureColors();
Lucas Dupinf03e7522018-06-25 16:21:13 -07005533 color = ContrastColorUtil.resolveDefaultColor(mContext, background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005534 } else {
Lucas Dupina291d192018-06-07 13:59:42 -07005535 color = ContrastColorUtil.resolveContrastColor(mContext, mN.color,
Anthony Chenad4d1582017-04-10 16:07:58 -07005536 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005537 }
5538 if (Color.alpha(color) < 255) {
5539 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005540 color = ContrastColorUtil.compositeColors(color, background);
Selim Cinekac5f0272017-05-02 16:05:41 -07005541 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005542 mCachedContrastColorIsFor = mN.color;
Selim Cinekac5f0272017-05-02 16:05:41 -07005543 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005544 }
5545
Selim Cinek4717d862018-04-19 09:19:15 +08005546 int resolveNeutralColor() {
5547 if (mNeutralColor != COLOR_INVALID) {
5548 return mNeutralColor;
5549 }
5550 int background = mContext.getColor(
5551 com.android.internal.R.color.notification_material_background_color);
Lucas Dupinf03e7522018-06-25 16:21:13 -07005552 mNeutralColor = ContrastColorUtil.resolveDefaultColor(mContext, background,
5553 mInNightMode);
Selim Cinek4717d862018-04-19 09:19:15 +08005554 if (Color.alpha(mNeutralColor) < 255) {
5555 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005556 mNeutralColor = ContrastColorUtil.compositeColors(mNeutralColor, background);
Selim Cinek4717d862018-04-19 09:19:15 +08005557 }
5558 return mNeutralColor;
5559 }
5560
Adrian Roos487374f2017-01-11 15:48:14 -08005561 int resolveAmbientColor() {
5562 if (mCachedAmbientColorIsFor == mN.color && mCachedAmbientColorIsFor != COLOR_INVALID) {
5563 return mCachedAmbientColor;
5564 }
Lucas Dupina291d192018-06-07 13:59:42 -07005565 final int contrasted = ContrastColorUtil.resolveAmbientColor(mContext, mN.color);
Adrian Roos487374f2017-01-11 15:48:14 -08005566
5567 mCachedAmbientColorIsFor = mN.color;
5568 return mCachedAmbientColor = contrasted;
5569 }
5570
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005571 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005572 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005573 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08005574 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005575 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005576 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005577 mN.actions = new Action[mActions.size()];
5578 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005579 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005580 if (!mPersonList.isEmpty()) {
Selim Cineke7238dd2017-12-14 17:48:32 -08005581 mN.extras.putParcelableArrayList(EXTRA_PEOPLE_LIST, mPersonList);
Dan Sandler0bf2ed82013-12-21 23:33:41 -06005582 }
Selim Cinek247fa012016-02-18 09:50:48 -08005583 if (mN.bigContentView != null || mN.contentView != null
5584 || mN.headsUpContentView != null) {
5585 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
5586 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005587 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08005588 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005589
Julia Reynolds3b848122016-02-26 10:45:32 -05005590 /**
5591 * Creates a Builder from an existing notification so further changes can be made.
5592 * @param context The context for your application / activity.
5593 * @param n The notification to create a Builder from.
5594 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005595 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005596 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005597 ApplicationInfo applicationInfo = n.extras.getParcelable(
5598 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005599 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05005600 if (applicationInfo != null) {
5601 try {
5602 builderContext = context.createApplicationContext(applicationInfo,
5603 Context.CONTEXT_RESTRICTED);
5604 } catch (NameNotFoundException e) {
5605 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
5606 builderContext = context; // try with our context
5607 }
5608 } else {
5609 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02005610 }
5611
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005612 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005613 }
5614
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005615 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005616 * @deprecated Use {@link #build()} instead.
5617 */
5618 @Deprecated
5619 public Notification getNotification() {
5620 return build();
5621 }
5622
5623 /**
5624 * Combine all of the options that have been set and return a new {@link Notification}
5625 * object.
5626 */
5627 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005628 // first, add any extras from the calling code
5629 if (mUserExtras != null) {
5630 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005631 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005632
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005633 mN.creationTime = System.currentTimeMillis();
5634
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005635 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05005636 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02005637
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005638 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005639
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005640 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02005641 mStyle.reduceImageSizes(mContext);
5642 mStyle.purgeResources();
Selim Cinek90343862018-02-01 11:07:11 -08005643 mStyle.validate(mContext);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005644 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005645 }
Selim Cinekd0426622017-07-11 13:19:59 +02005646 mN.reduceImageSizes(mContext);
5647
Adrian Roos5081c0d2016-02-26 16:04:19 -08005648 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07005649 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005650 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005651 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005652 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
5653 mN.contentView.getSequenceNumber());
5654 }
5655 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005656 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005657 if (mN.bigContentView != null) {
5658 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
5659 mN.bigContentView.getSequenceNumber());
5660 }
5661 }
5662 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005663 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005664 if (mN.headsUpContentView != null) {
5665 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
5666 mN.headsUpContentView.getSequenceNumber());
5667 }
5668 }
5669 }
5670
Julia Reynolds4c0c2022016-02-02 15:11:59 -05005671 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
5672 mN.flags |= FLAG_SHOW_LIGHTS;
5673 }
5674
Adrian Roosfb921842017-10-26 14:49:56 +02005675 mN.allPendingIntents = null;
5676
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005677 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005678 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005679
5680 /**
5681 * Apply this Builder to an existing {@link Notification} object.
5682 *
5683 * @hide
5684 */
5685 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005686 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005687 return n;
5688 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005689
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005690 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005691 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005692 * change. Also removes extenders on low ram devices, as
5693 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005694 *
5695 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5696 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005697 * @hide
5698 */
Kristian Monsen05f34792018-04-09 10:27:16 +02005699 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam,
5700 Context context) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005701 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005702
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005703 // Only strip views for known Styles because we won't know how to
5704 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005705 if (!isLowRam
5706 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005707 && getNotificationStyleClass(templateClass) == null) {
5708 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005709 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005710
5711 // Only strip unmodified BuilderRemoteViews.
5712 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005713 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005714 n.contentView.getSequenceNumber();
5715 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005716 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005717 n.bigContentView.getSequenceNumber();
5718 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005719 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005720 n.headsUpContentView.getSequenceNumber();
5721
5722 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005723 if (!isLowRam
5724 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005725 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005726 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005727
5728 Notification clone = n.clone();
5729 if (stripContentView) {
5730 clone.contentView = null;
5731 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5732 }
5733 if (stripBigContentView) {
5734 clone.bigContentView = null;
5735 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5736 }
5737 if (stripHeadsUpContentView) {
5738 clone.headsUpContentView = null;
5739 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5740 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005741 if (isLowRam) {
Kristian Monsen05f34792018-04-09 10:27:16 +02005742 String[] allowedServices = context.getResources().getStringArray(
5743 R.array.config_allowedManagedServicesOnLowRamDevices);
5744 if (allowedServices.length == 0) {
5745 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5746 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5747 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5748 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005749 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005750 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005751 }
5752
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005753 @UnsupportedAppUsage
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005754 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005755 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005756 }
5757
5758 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005759 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005760 }
5761
5762 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005763 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005764 }
5765
5766 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005767 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005768 }
5769
5770 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005771 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005772 }
5773
Adrian Roosc1a80b02016-04-05 14:54:55 -07005774 private int getMessagingLayoutResource() {
5775 return R.layout.notification_template_material_messaging;
5776 }
5777
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005778 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005779 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005780 }
5781
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005782 private int getEmphasizedActionLayoutResource() {
5783 return R.layout.notification_material_action_emphasized;
5784 }
5785
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005786 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005787 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005788 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005789
5790 private int getBackgroundColor() {
5791 if (isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005792 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : mN.color;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005793 } else {
Selim Cinekc7f5a822018-03-20 19:32:06 -07005794 return COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005795 }
5796 }
5797
Selim Cinek396caca2018-04-10 17:46:46 -07005798 /**
Selim Cinek4717d862018-04-19 09:19:15 +08005799 * Gets a neutral color that can be used for icons or similar that should not stand out.
5800 */
5801 private int getNeutralColor() {
5802 if (isColorized()) {
5803 return getSecondaryTextColor();
5804 } else {
5805 return resolveNeutralColor();
5806 }
5807 }
5808
5809 /**
Selim Cinek396caca2018-04-10 17:46:46 -07005810 * Same as getBackgroundColor but also resolved the default color to the background.
5811 */
5812 private int resolveBackgroundColor() {
5813 int backgroundColor = getBackgroundColor();
5814 if (backgroundColor == COLOR_DEFAULT) {
5815 backgroundColor = mContext.getColor(
5816 com.android.internal.R.color.notification_material_background_color);
5817 }
5818 return backgroundColor;
5819 }
5820
Selim Cinek7b9605b2017-01-19 17:36:00 -08005821 private boolean isColorized() {
5822 return mN.isColorized();
5823 }
Selim Cinek99104832017-01-25 14:47:33 -08005824
Anthony Chenad4d1582017-04-10 16:07:58 -07005825 private boolean shouldTintActionButtons() {
5826 return mTintActionButtons;
5827 }
5828
Selim Cinek99104832017-01-25 14:47:33 -08005829 private boolean textColorsNeedInversion() {
5830 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
5831 return false;
5832 }
5833 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
5834 return targetSdkVersion > Build.VERSION_CODES.M
5835 && targetSdkVersion < Build.VERSION_CODES.O;
5836 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005837
5838 /**
5839 * Set a color palette to be used as the background and textColors
5840 *
5841 * @param backgroundColor the color to be used as the background
5842 * @param foregroundColor the color to be used as the foreground
5843 *
5844 * @hide
5845 */
5846 public void setColorPalette(int backgroundColor, int foregroundColor) {
5847 mBackgroundColor = backgroundColor;
5848 mForegroundColor = foregroundColor;
5849 mTextColorsAreForBackground = COLOR_INVALID;
5850 ensureColors();
5851 }
Selim Cinekac5f0272017-05-02 16:05:41 -07005852
5853 /**
Selim Cineka7679b62017-05-10 16:33:25 -07005854 * Forces all styled remoteViews to be built from scratch and not use any cached
5855 * RemoteViews.
5856 * This is needed for legacy apps that are baking in their remoteviews into the
5857 * notification.
5858 *
5859 * @hide
5860 */
5861 public void setRebuildStyledRemoteViews(boolean rebuild) {
5862 mRebuildStyledRemoteViews = rebuild;
5863 }
Selim Cinekaa9db1f2018-02-27 17:35:47 -08005864
5865 /**
5866 * Get the text that should be displayed in the statusBar when heads upped. This is
5867 * usually just the app name, but may be different depending on the style.
5868 *
5869 * @param publicMode If true, return a text that is safe to display in public.
5870 *
5871 * @hide
5872 */
5873 public CharSequence getHeadsUpStatusBarText(boolean publicMode) {
5874 if (mStyle != null && !publicMode) {
5875 CharSequence text = mStyle.getHeadsUpStatusBarText();
5876 if (!TextUtils.isEmpty(text)) {
5877 return text;
5878 }
5879 }
5880 return loadHeaderAppName();
5881 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005882 }
5883
5884 /**
Selim Cinekd0426622017-07-11 13:19:59 +02005885 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
5886 * remote views.
5887 *
5888 * @hide
5889 */
5890 void reduceImageSizes(Context context) {
5891 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
5892 return;
5893 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005894 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02005895 if (mLargeIcon != null || largeIcon != null) {
5896 Resources resources = context.getResources();
5897 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07005898 int maxWidth = resources.getDimensionPixelSize(isLowRam
5899 ? R.dimen.notification_right_icon_size_low_ram
5900 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02005901 int maxHeight = maxWidth;
5902 if (MediaStyle.class.equals(style)
5903 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005904 maxHeight = resources.getDimensionPixelSize(isLowRam
5905 ? R.dimen.notification_media_image_max_height_low_ram
5906 : R.dimen.notification_media_image_max_height);
5907 maxWidth = resources.getDimensionPixelSize(isLowRam
5908 ? R.dimen.notification_media_image_max_width_low_ram
5909 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02005910 }
5911 if (mLargeIcon != null) {
5912 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
5913 }
5914 if (largeIcon != null) {
5915 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
5916 }
5917 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005918 reduceImageSizesForRemoteView(contentView, context, isLowRam);
5919 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
5920 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02005921 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
5922 }
5923
Selim Cineka8cb1262017-08-15 16:53:44 -07005924 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
5925 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02005926 if (remoteView != null) {
5927 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07005928 int maxWidth = resources.getDimensionPixelSize(isLowRam
5929 ? R.dimen.notification_custom_view_max_image_width_low_ram
5930 : R.dimen.notification_custom_view_max_image_width);
5931 int maxHeight = resources.getDimensionPixelSize(isLowRam
5932 ? R.dimen.notification_custom_view_max_image_height_low_ram
5933 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02005934 remoteView.reduceImageSizes(maxWidth, maxHeight);
5935 }
5936 }
5937
5938 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005939 * @return whether this notification is a foreground service notification
Gus Prevasb0c1a462018-11-05 11:06:15 -05005940 * @hide
Selim Cinek7b9605b2017-01-19 17:36:00 -08005941 */
Gus Prevasb0c1a462018-11-05 11:06:15 -05005942 public boolean isForegroundService() {
Selim Cinek22714f12017-04-13 16:23:53 -07005943 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005944 }
5945
5946 /**
Selim Cinek99104832017-01-25 14:47:33 -08005947 * @return whether this notification has a media session attached
5948 * @hide
5949 */
5950 public boolean hasMediaSession() {
5951 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
5952 }
5953
5954 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005955 * @return the style class of this notification
5956 * @hide
5957 */
5958 public Class<? extends Notification.Style> getNotificationStyle() {
5959 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
5960
5961 if (!TextUtils.isEmpty(templateClass)) {
5962 return Notification.getNotificationStyleClass(templateClass);
5963 }
5964 return null;
5965 }
5966
5967 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005968 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08005969 *
5970 * @hide
5971 */
5972 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005973 if (isColorizedMedia()) {
5974 return true;
5975 }
Julia Reynolds4db59552017-06-30 13:34:01 -04005976 return extras.getBoolean(EXTRA_COLORIZED)
5977 && (hasColorizedPermission() || isForegroundService());
5978 }
5979
5980 /**
5981 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
5982 * permission. The permission is checked when a notification is enqueued.
5983 */
5984 private boolean hasColorizedPermission() {
5985 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07005986 }
5987
5988 /**
5989 * @return true if this notification is colorized and it is a media notification
5990 *
5991 * @hide
5992 */
5993 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08005994 Class<? extends Style> style = getNotificationStyle();
5995 if (MediaStyle.class.equals(style)) {
5996 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
5997 if ((colorized == null || colorized) && hasMediaSession()) {
5998 return true;
5999 }
6000 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6001 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
6002 return true;
6003 }
6004 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07006005 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006006 }
6007
Selim Cinek0847acd2017-04-24 19:48:29 -07006008
6009 /**
6010 * @return true if this is a media notification
6011 *
6012 * @hide
6013 */
6014 public boolean isMediaNotification() {
6015 Class<? extends Style> style = getNotificationStyle();
6016 if (MediaStyle.class.equals(style)) {
6017 return true;
6018 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6019 return true;
6020 }
6021 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006022 }
6023
Selim Cinek279fa862016-06-14 10:57:25 -07006024 private boolean hasLargeIcon() {
6025 return mLargeIcon != null || largeIcon != null;
6026 }
6027
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006028 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006029 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006030 * @hide
6031 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07006032 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006033 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
6034 }
6035
6036 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006037 * @return true if the notification will show a chronometer; false otherwise
6038 * @hide
6039 */
6040 public boolean showsChronometer() {
6041 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
6042 }
6043
6044 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04006045 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05006046 */
6047 @SystemApi
6048 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
6049 Class<? extends Style>[] classes = new Class[] {
6050 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
6051 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
6052 MessagingStyle.class };
6053 for (Class<? extends Style> innerClass : classes) {
6054 if (templateClass.equals(innerClass.getName())) {
6055 return innerClass;
6056 }
6057 }
6058 return null;
6059 }
6060
6061 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006062 * An object that can apply a rich notification style to a {@link Notification.Builder}
6063 * object.
6064 */
Griff Hazendfcb0802014-02-11 12:00:00 -08006065 public static abstract class Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07006066
6067 /**
6068 * The number of items allowed simulatanously in the remote input history.
6069 * @hide
6070 */
6071 static final int MAX_REMOTE_INPUT_HISTORY_LINES = 3;
Chris Wrend6297db2012-05-03 16:20:13 -04006072 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006073
6074 /**
6075 * @hide
6076 */
6077 protected CharSequence mSummaryText = null;
6078
6079 /**
6080 * @hide
6081 */
6082 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04006083
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006084 protected Builder mBuilder;
6085
Chris Wrend6297db2012-05-03 16:20:13 -04006086 /**
6087 * Overrides ContentTitle in the big form of the template.
6088 * This defaults to the value passed to setContentTitle().
6089 */
6090 protected void internalSetBigContentTitle(CharSequence title) {
6091 mBigContentTitle = title;
6092 }
6093
6094 /**
6095 * Set the first line of text after the detail section in the big form of the template.
6096 */
6097 protected void internalSetSummaryText(CharSequence cs) {
6098 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04006099 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04006100 }
6101
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006102 public void setBuilder(Builder builder) {
6103 if (mBuilder != builder) {
6104 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07006105 if (mBuilder != null) {
6106 mBuilder.setStyle(this);
6107 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006108 }
6109 }
6110
Chris Wrend6297db2012-05-03 16:20:13 -04006111 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006112 if (mBuilder == null) {
6113 throw new IllegalArgumentException("Style requires a valid Builder object");
6114 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006115 }
Chris Wrend6297db2012-05-03 16:20:13 -04006116
6117 protected RemoteViews getStandardView(int layoutId) {
Selim Cinek384804b2018-04-18 14:31:07 +08006118 return getStandardView(layoutId, null);
6119 }
6120
6121 /**
6122 * Get the standard view for this style.
6123 *
6124 * @param layoutId The layout id to use
6125 * @param result The result where template bind information is saved.
6126 * @return A remoteView for this style.
6127 * @hide
6128 */
6129 protected RemoteViews getStandardView(int layoutId, TemplateBindResult result) {
Chris Wrend6297db2012-05-03 16:20:13 -04006130 checkBuilder();
6131
Christoph Studer4600f9b2014-07-22 22:44:43 +02006132 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006133 CharSequence oldBuilderContentTitle =
6134 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04006135 if (mBigContentTitle != null) {
6136 mBuilder.setContentTitle(mBigContentTitle);
6137 }
6138
Selim Cinek384804b2018-04-18 14:31:07 +08006139 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId, result);
Chris Wrend6297db2012-05-03 16:20:13 -04006140
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006141 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006142
Chris Wrend6297db2012-05-03 16:20:13 -04006143 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
6144 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04006145 } else {
6146 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04006147 }
6148
Chris Wrend6297db2012-05-03 16:20:13 -04006149 return contentView;
6150 }
6151
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006152 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006153 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006154 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08006155 *
6156 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006157 * @hide
6158 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08006159 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006160 return null;
6161 }
6162
6163 /**
6164 * Construct a Style-specific RemoteViews for the final big notification layout.
6165 * @hide
6166 */
6167 public RemoteViews makeBigContentView() {
6168 return null;
6169 }
6170
6171 /**
6172 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08006173 *
6174 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006175 * @hide
6176 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006177 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006178 return null;
6179 }
6180
6181 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006182 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006183 * @hide
6184 */
6185 public void addExtras(Bundle extras) {
6186 if (mSummaryTextSet) {
6187 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
6188 }
6189 if (mBigContentTitle != null) {
6190 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
6191 }
Chris Wren91ad5632013-06-05 15:05:57 -04006192 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006193 }
6194
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006195 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006196 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006197 * @hide
6198 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02006199 protected void restoreFromExtras(Bundle extras) {
6200 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
6201 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
6202 mSummaryTextSet = true;
6203 }
6204 if (extras.containsKey(EXTRA_TITLE_BIG)) {
6205 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
6206 }
6207 }
6208
6209
6210 /**
6211 * @hide
6212 */
6213 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006214 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006215 return wip;
6216 }
6217
Daniel Sandler0ec46202015-06-24 01:27:05 -04006218 /**
6219 * @hide
6220 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006221 public void purgeResources() {}
6222
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006223 /**
6224 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
6225 * attached to.
6226 *
6227 * @return the fully constructed Notification.
6228 */
6229 public Notification build() {
6230 checkBuilder();
6231 return mBuilder.build();
6232 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006233
6234 /**
6235 * @hide
6236 * @return true if the style positions the progress bar on the second line; false if the
6237 * style hides the progress bar
6238 */
6239 protected boolean hasProgress() {
6240 return true;
6241 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006242
6243 /**
6244 * @hide
6245 * @return Whether we should put the summary be put into the notification header
6246 */
6247 public boolean hasSummaryInHeader() {
6248 return true;
6249 }
Selim Cinek593610c2016-02-16 18:42:57 -08006250
6251 /**
6252 * @hide
6253 * @return Whether custom content views are displayed inline in the style
6254 */
6255 public boolean displayCustomViewInline() {
6256 return false;
6257 }
Selim Cinekd0426622017-07-11 13:19:59 +02006258
6259 /**
6260 * Reduces the image sizes contained in this style.
6261 *
6262 * @hide
6263 */
6264 public void reduceImageSizes(Context context) {
6265 }
Selim Cinek90343862018-02-01 11:07:11 -08006266
6267 /**
6268 * Validate that this style was properly composed. This is called at build time.
6269 * @hide
6270 */
6271 public void validate(Context context) {
6272 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006273
6274 /**
6275 * @hide
6276 */
6277 public abstract boolean areNotificationsVisiblyDifferent(Style other);
Selim Cinekc7f5a822018-03-20 19:32:06 -07006278
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006279 /**
koprivaa1a78482018-10-09 10:09:23 -07006280 * @return the text that should be displayed in the statusBar when heads-upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006281 * If {@code null} is returned, the default implementation will be used.
6282 *
6283 * @hide
6284 */
6285 public CharSequence getHeadsUpStatusBarText() {
6286 return null;
6287 }
Joe Onorato46439ce2010-11-19 13:56:21 -08006288 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006289
6290 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006291 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08006292 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006293 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006294 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006295 * Notification notif = new Notification.Builder(mContext)
6296 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
6297 * .setContentText(subject)
6298 * .setSmallIcon(R.drawable.new_post)
6299 * .setLargeIcon(aBitmap)
6300 * .setStyle(new Notification.BigPictureStyle()
6301 * .bigPicture(aBigBitmap))
6302 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006303 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006304 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006305 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006306 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006307 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006308 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006309 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006310 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006311
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006312 public BigPictureStyle() {
6313 }
6314
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006315 /**
6316 * @deprecated use {@code BigPictureStyle()}.
6317 */
6318 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006319 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006320 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006321 }
6322
Chris Wrend6297db2012-05-03 16:20:13 -04006323 /**
6324 * Overrides ContentTitle in the big form of the template.
6325 * This defaults to the value passed to setContentTitle().
6326 */
6327 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006328 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006329 return this;
6330 }
6331
6332 /**
6333 * Set the first line of text after the detail section in the big form of the template.
6334 */
6335 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006336 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006337 return this;
6338 }
6339
Chris Wren0bd664d2012-08-01 13:56:56 -04006340 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05006341 * @hide
6342 */
6343 public Bitmap getBigPicture() {
6344 return mPicture;
6345 }
6346
6347 /**
Chris Wren0bd664d2012-08-01 13:56:56 -04006348 * Provide the bitmap to be used as the payload for the BigPicture notification.
6349 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006350 public BigPictureStyle bigPicture(Bitmap b) {
6351 mPicture = b;
6352 return this;
6353 }
6354
Chris Wren3745a3d2012-05-22 15:11:52 -04006355 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04006356 * Override the large icon when the big notification is shown.
6357 */
6358 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04006359 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
6360 }
6361
6362 /**
6363 * Override the large icon when the big notification is shown.
6364 */
6365 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04006366 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006367 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006368 return this;
6369 }
6370
Riley Andrews0394a0c2015-11-03 23:36:52 -08006371 /** @hide */
6372 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
6373
Daniel Sandler0ec46202015-06-24 01:27:05 -04006374 /**
6375 * @hide
6376 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006377 @Override
6378 public void purgeResources() {
6379 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08006380 if (mPicture != null &&
6381 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08006382 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006383 mPicture = mPicture.createAshmemBitmap();
6384 }
6385 if (mBigLargeIcon != null) {
6386 mBigLargeIcon.convertToAshmem();
6387 }
6388 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01006389
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006390 /**
6391 * @hide
6392 */
Selim Cinekd0426622017-07-11 13:19:59 +02006393 @Override
6394 public void reduceImageSizes(Context context) {
6395 super.reduceImageSizes(context);
6396 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006397 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006398 if (mPicture != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006399 int maxPictureWidth = resources.getDimensionPixelSize(isLowRam
6400 ? R.dimen.notification_big_picture_max_height_low_ram
6401 : R.dimen.notification_big_picture_max_height);
6402 int maxPictureHeight = resources.getDimensionPixelSize(isLowRam
6403 ? R.dimen.notification_big_picture_max_width_low_ram
6404 : R.dimen.notification_big_picture_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006405 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
6406 }
6407 if (mBigLargeIcon != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006408 int rightIconSize = resources.getDimensionPixelSize(isLowRam
6409 ? R.dimen.notification_right_icon_size_low_ram
6410 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006411 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
6412 }
6413 }
6414
6415 /**
6416 * @hide
6417 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006418 public RemoteViews makeBigContentView() {
6419 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01006420 // This covers the following cases:
6421 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006422 // mN.mLargeIcon
6423 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04006424 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07006425 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006426 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006427 oldLargeIcon = mBuilder.mN.mLargeIcon;
6428 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006429 // The legacy largeIcon might not allow us to clear the image, as it's taken in
6430 // replacement if the other one is null. Because we're restoring these legacy icons
6431 // for old listeners, this is in general non-null.
6432 largeIconLegacy = mBuilder.mN.largeIcon;
6433 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006434 }
6435
Selim Cinek384804b2018-04-18 14:31:07 +08006436 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource(),
6437 null /* result */);
Selim Cinek03d0d652015-11-13 13:18:09 -05006438 if (mSummaryTextSet) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006439 contentView.setTextViewText(R.id.text, mBuilder.processTextSpans(
6440 mBuilder.processLegacyText(mSummaryText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006441 mBuilder.setTextViewColorSecondary(contentView, R.id.text);
Selim Cinekc848c3a2016-01-13 15:27:30 -08006442 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05006443 }
Selim Cinek279fa862016-06-14 10:57:25 -07006444 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08006445
Christoph Studer5c510ee2014-12-15 16:32:27 +01006446 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006447 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006448 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006449 }
6450
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006451 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006452 return contentView;
6453 }
6454
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006455 /**
6456 * @hide
6457 */
6458 public void addExtras(Bundle extras) {
6459 super.addExtras(extras);
6460
6461 if (mBigLargeIconSet) {
6462 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
6463 }
6464 extras.putParcelable(EXTRA_PICTURE, mPicture);
6465 }
6466
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006467 /**
6468 * @hide
6469 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006470 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02006471 protected void restoreFromExtras(Bundle extras) {
6472 super.restoreFromExtras(extras);
6473
6474 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01006475 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02006476 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04006477 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006478 mPicture = extras.getParcelable(EXTRA_PICTURE);
6479 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006480
6481 /**
6482 * @hide
6483 */
6484 @Override
6485 public boolean hasSummaryInHeader() {
6486 return false;
6487 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006488
6489 /**
6490 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006491 * Note that we aren't actually comparing the contents of the bitmaps here, so this
6492 * is only doing a cursory inspection. Bitmaps of equal size will appear the same.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006493 */
6494 @Override
6495 public boolean areNotificationsVisiblyDifferent(Style other) {
6496 if (other == null || getClass() != other.getClass()) {
6497 return true;
6498 }
6499 BigPictureStyle otherS = (BigPictureStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006500 return areBitmapsObviouslyDifferent(getBigPicture(), otherS.getBigPicture());
6501 }
6502
6503 private static boolean areBitmapsObviouslyDifferent(Bitmap a, Bitmap b) {
6504 if (a == b) {
6505 return false;
6506 }
6507 if (a == null || b == null) {
6508 return true;
6509 }
6510 return a.getWidth() != b.getWidth()
6511 || a.getHeight() != b.getHeight()
6512 || a.getConfig() != b.getConfig()
6513 || a.getGenerationId() != b.getGenerationId();
Julia Reynolds7217dc92018-03-07 12:12:09 -05006514 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006515 }
6516
6517 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006518 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08006519 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006520 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006521 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006522 * Notification notif = new Notification.Builder(mContext)
6523 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
6524 * .setContentText(subject)
6525 * .setSmallIcon(R.drawable.new_mail)
6526 * .setLargeIcon(aBitmap)
6527 * .setStyle(new Notification.BigTextStyle()
6528 * .bigText(aVeryLongString))
6529 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006530 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006531 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006532 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006533 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006534 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006535
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006536 private CharSequence mBigText;
6537
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006538 public BigTextStyle() {
6539 }
6540
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006541 /**
6542 * @deprecated use {@code BigTextStyle()}.
6543 */
6544 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006545 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006546 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006547 }
6548
Chris Wrend6297db2012-05-03 16:20:13 -04006549 /**
6550 * Overrides ContentTitle in the big form of the template.
6551 * This defaults to the value passed to setContentTitle().
6552 */
6553 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006554 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006555 return this;
6556 }
6557
6558 /**
6559 * Set the first line of text after the detail section in the big form of the template.
6560 */
6561 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006562 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006563 return this;
6564 }
6565
Chris Wren0bd664d2012-08-01 13:56:56 -04006566 /**
6567 * Provide the longer text to be displayed in the big form of the
6568 * template in place of the content text.
6569 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006570 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006571 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006572 return this;
6573 }
6574
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006575 /**
6576 * @hide
6577 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05006578 public CharSequence getBigText() {
6579 return mBigText;
6580 }
6581
6582 /**
6583 * @hide
6584 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006585 public void addExtras(Bundle extras) {
6586 super.addExtras(extras);
6587
Christoph Studer4600f9b2014-07-22 22:44:43 +02006588 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
6589 }
6590
6591 /**
6592 * @hide
6593 */
6594 @Override
6595 protected void restoreFromExtras(Bundle extras) {
6596 super.restoreFromExtras(extras);
6597
6598 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006599 }
6600
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006601 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006602 * @param increasedHeight true if this layout be created with an increased height.
6603 *
6604 * @hide
6605 */
6606 @Override
6607 public RemoteViews makeContentView(boolean increasedHeight) {
6608 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006609 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006610 mBuilder.mActions = new ArrayList<>();
6611 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006612 mBuilder.mActions = mBuilder.mOriginalActions;
6613 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006614 return remoteViews;
6615 }
6616 return super.makeContentView(increasedHeight);
6617 }
6618
6619 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006620 * @hide
6621 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006622 @Override
6623 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
6624 if (increasedHeight && mBuilder.mActions.size() > 0) {
6625 return makeBigContentView();
6626 }
6627 return super.makeHeadsUpContentView(increasedHeight);
6628 }
6629
6630 /**
6631 * @hide
6632 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006633 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02006634
6635 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07006636 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006637 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04006638
Selim Cinek384804b2018-04-18 14:31:07 +08006639 TemplateBindResult result = new TemplateBindResult();
6640 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource(), result);
Selim Cinek1c72fa02018-04-23 18:00:54 +08006641 contentView.setInt(R.id.big_text, "setImageEndMargin", result.getIconMarginEnd());
Joe Malin8d40d042012-11-05 11:36:40 -08006642
Selim Cinek75998782016-04-26 10:39:17 -07006643 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006644
Selim Cinek3a2c4b92015-12-17 17:01:17 -08006645 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07006646 if (TextUtils.isEmpty(bigTextText)) {
6647 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
6648 // experience
6649 bigTextText = mBuilder.processLegacyText(text);
6650 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07006651 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08006652
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006653 return contentView;
6654 }
6655
Julia Reynolds7217dc92018-03-07 12:12:09 -05006656 /**
6657 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006658 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006659 */
6660 @Override
6661 public boolean areNotificationsVisiblyDifferent(Style other) {
6662 if (other == null || getClass() != other.getClass()) {
6663 return true;
6664 }
6665 BigTextStyle newS = (BigTextStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006666 return !Objects.equals(String.valueOf(getBigText()), String.valueOf(newS.getBigText()));
Julia Reynolds7217dc92018-03-07 12:12:09 -05006667 }
6668
Adrian Roosb1f427c2016-05-26 12:27:15 -07006669 static void applyBigTextContentView(Builder builder,
6670 RemoteViews contentView, CharSequence bigTextText) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006671 contentView.setTextViewText(R.id.big_text, builder.processTextSpans(bigTextText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006672 builder.setTextViewColorSecondary(contentView, R.id.big_text);
Adrian Roosb1f427c2016-05-26 12:27:15 -07006673 contentView.setViewVisibility(R.id.big_text,
6674 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek279fa862016-06-14 10:57:25 -07006675 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07006676 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006677 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006678
6679 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006680 * Helper class for generating large-format notifications that include multiple back-and-forth
6681 * messages of varying types between any number of people.
6682 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006683 * <p>
Alex Hillsfc737de2016-03-23 17:33:02 -04006684 * If the platform does not provide large-format notifications, this method has no effect. The
6685 * user will always see the normal notification view.
Selim Cinekce8794f2018-05-23 16:46:05 -07006686 *
6687 * <p>
6688 * If the app is targeting Android P and above, it is required to use the {@link Person}
6689 * class in order to get an optimal rendering of the notification and its avatars. For
6690 * conversations involving multiple people, the app should also make sure that it marks the
6691 * conversation as a group with {@link #setGroupConversation(boolean)}.
6692 *
6693 * <p>
6694 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior.
6695 * Here's an example of how this may be used:
Alex Hillsfc737de2016-03-23 17:33:02 -04006696 * <pre class="prettyprint">
6697 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006698 * Person user = new Person.Builder().setIcon(userIcon).setName(userName).build();
6699 * MessagingStyle style = new MessagingStyle(user)
6700 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getPerson())
6701 * .addMessage(messages[2].getText(), messages[2].getTime(), messages[2].getPerson())
6702 * .setGroupConversation(hasMultiplePeople());
6703 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006704 * Notification noti = new Notification.Builder()
Selim Cinekce8794f2018-05-23 16:46:05 -07006705 * .setContentTitle(&quot;2 new messages with &quot; + sender.toString())
Alex Hillsfc737de2016-03-23 17:33:02 -04006706 * .setContentText(subject)
6707 * .setSmallIcon(R.drawable.new_message)
6708 * .setLargeIcon(aBitmap)
Selim Cinekce8794f2018-05-23 16:46:05 -07006709 * .setStyle(style)
Alex Hillsfc737de2016-03-23 17:33:02 -04006710 * .build();
6711 * </pre>
6712 */
6713 public static class MessagingStyle extends Style {
6714
6715 /**
6716 * The maximum number of messages that will be retained in the Notification itself (the
6717 * number displayed is up to the platform).
6718 */
6719 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
6720
Selim Cinekcb8b9852017-12-15 18:01:52 -08006721 @NonNull Person mUser;
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006722 @Nullable CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04006723 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08006724 List<Message> mHistoricMessages = new ArrayList<>();
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006725 boolean mIsGroupConversation;
Alex Hillsfc737de2016-03-23 17:33:02 -04006726
6727 MessagingStyle() {
6728 }
6729
6730 /**
Alex Hillsfd590442016-10-07 09:52:44 -04006731 * @param userDisplayName Required - the name to be displayed for any replies sent by the
6732 * user before the posting app reposts the notification with those messages after they've
6733 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04006734 * {@link #addMessage(Notification.MessagingStyle.Message)}
Selim Cinekcb8b9852017-12-15 18:01:52 -08006735 *
6736 * @deprecated use {@code MessagingStyle(Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006737 */
Alex Hillsfd590442016-10-07 09:52:44 -04006738 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Selim Cinek9acd6732018-03-23 16:39:02 -07006739 this(new Person.Builder().setName(userDisplayName).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006740 }
6741
6742 /**
6743 * @param user Required - The person displayed for any messages that are sent by the
6744 * user. Any messages added with {@link #addMessage(Notification.MessagingStyle.Message)}
6745 * who don't have a Person associated with it will be displayed as if they were sent
Selim Cinek90343862018-02-01 11:07:11 -08006746 * by this user. The user also needs to have a valid name associated with it, which will
6747 * be enforced starting in Android P.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006748 */
6749 public MessagingStyle(@NonNull Person user) {
6750 mUser = user;
Selim Cinek90343862018-02-01 11:07:11 -08006751 }
6752
6753 /**
6754 * Validate that this style was properly composed. This is called at build time.
6755 * @hide
6756 */
6757 @Override
6758 public void validate(Context context) {
6759 super.validate(context);
6760 if (context.getApplicationInfo().targetSdkVersion
6761 >= Build.VERSION_CODES.P && (mUser == null || mUser.getName() == null)) {
6762 throw new RuntimeException("User must be valid and have a name.");
Selim Cinekcb8b9852017-12-15 18:01:52 -08006763 }
6764 }
6765
6766 /**
koprivaa1a78482018-10-09 10:09:23 -07006767 * @return the text that should be displayed in the statusBar when heads upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006768 * If {@code null} is returned, the default implementation will be used.
6769 *
6770 * @hide
6771 */
6772 @Override
6773 public CharSequence getHeadsUpStatusBarText() {
6774 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
6775 ? super.mBigContentTitle
6776 : mConversationTitle;
6777 if (!TextUtils.isEmpty(conversationTitle) && !hasOnlyWhiteSpaceSenders()) {
6778 return conversationTitle;
6779 }
6780 return null;
6781 }
6782
6783 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08006784 * @return the user to be displayed for any replies sent by the user
6785 */
Selim Cinekeb53c222018-04-06 12:56:09 -07006786 @NonNull
Selim Cinekcb8b9852017-12-15 18:01:52 -08006787 public Person getUser() {
6788 return mUser;
Alex Hillsfc737de2016-03-23 17:33:02 -04006789 }
6790
6791 /**
6792 * Returns the name to be displayed for any replies sent by the user
Selim Cinekcb8b9852017-12-15 18:01:52 -08006793 *
6794 * @deprecated use {@link #getUser()} instead
Alex Hillsfc737de2016-03-23 17:33:02 -04006795 */
6796 public CharSequence getUserDisplayName() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006797 return mUser.getName();
Alex Hillsfc737de2016-03-23 17:33:02 -04006798 }
6799
6800 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006801 * Sets the title to be displayed on this conversation. May be set to {@code null}.
6802 *
Kodlee Yin14656422017-12-22 17:00:46 -08006803 * <p>This API's behavior was changed in SDK version {@link Build.VERSION_CODES#P}. If your
6804 * application's target version is less than {@link Build.VERSION_CODES#P}, setting a
6805 * conversation title to a non-null value will make {@link #isGroupConversation()} return
6806 * {@code true} and passing {@code null} will make it return {@code false}. In
6807 * {@link Build.VERSION_CODES#P} and beyond, use {@link #setGroupConversation(boolean)}
6808 * to set group conversation status.
6809 *
6810 * @param conversationTitle Title displayed for this conversation
6811 * @return this object for method chaining
Alex Hillsfc737de2016-03-23 17:33:02 -04006812 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006813 public MessagingStyle setConversationTitle(@Nullable CharSequence conversationTitle) {
Alex Hillsfc737de2016-03-23 17:33:02 -04006814 mConversationTitle = conversationTitle;
6815 return this;
6816 }
6817
6818 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006819 * Return the title to be displayed on this conversation. May return {@code null}.
Alex Hillsfc737de2016-03-23 17:33:02 -04006820 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006821 @Nullable
Alex Hillsfc737de2016-03-23 17:33:02 -04006822 public CharSequence getConversationTitle() {
6823 return mConversationTitle;
6824 }
6825
6826 /**
6827 * Adds a message for display by this notification. Convenience call for a simple
6828 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6829 * @param text A {@link CharSequence} to be displayed as the message content
6830 * @param timestamp Time at which the message arrived
6831 * @param sender A {@link CharSequence} to be used for displaying the name of the
6832 * sender. Should be <code>null</code> for messages by the current user, in which case
6833 * the platform will insert {@link #getUserDisplayName()}.
6834 * Should be unique amongst all individuals in the conversation, and should be
6835 * consistent during re-posts of the notification.
6836 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07006837 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Alex Hillsfc737de2016-03-23 17:33:02 -04006838 *
6839 * @return this object for method chaining
Selim Cinekcb8b9852017-12-15 18:01:52 -08006840 *
6841 * @deprecated use {@link #addMessage(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006842 */
6843 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006844 return addMessage(text, timestamp,
Selim Cinek9acd6732018-03-23 16:39:02 -07006845 sender == null ? null : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006846 }
6847
6848 /**
6849 * Adds a message for display by this notification. Convenience call for a simple
6850 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6851 * @param text A {@link CharSequence} to be displayed as the message content
6852 * @param timestamp Time at which the message arrived
6853 * @param sender The {@link Person} who sent the message.
6854 * Should be <code>null</code> for messages by the current user, in which case
6855 * the platform will insert the user set in {@code MessagingStyle(Person)}.
6856 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07006857 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Selim Cinekcb8b9852017-12-15 18:01:52 -08006858 *
6859 * @return this object for method chaining
6860 */
Selim Cinekeb53c222018-04-06 12:56:09 -07006861 public MessagingStyle addMessage(@NonNull CharSequence text, long timestamp,
6862 @Nullable Person sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08006863 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04006864 }
6865
6866 /**
6867 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08006868 *
6869 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6870 * the newest last.
6871 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006872 * @param message The {@link Message} to be displayed
6873 * @return this object for method chaining
6874 */
6875 public MessagingStyle addMessage(Message message) {
6876 mMessages.add(message);
6877 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6878 mMessages.remove(0);
6879 }
6880 return this;
6881 }
6882
6883 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006884 * Adds a {@link Message} for historic context in this notification.
6885 *
6886 * <p>Messages should be added as historic if they are not the main subject of the
6887 * notification but may give context to a conversation. The system may choose to present
6888 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
6889 *
6890 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6891 * the newest last.
6892 *
6893 * @param message The historic {@link Message} to be added
6894 * @return this object for method chaining
6895 */
6896 public MessagingStyle addHistoricMessage(Message message) {
6897 mHistoricMessages.add(message);
6898 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6899 mHistoricMessages.remove(0);
6900 }
6901 return this;
6902 }
6903
6904 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006905 * Gets the list of {@code Message} objects that represent the notification
6906 */
6907 public List<Message> getMessages() {
6908 return mMessages;
6909 }
6910
6911 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006912 * Gets the list of historic {@code Message}s in the notification.
6913 */
6914 public List<Message> getHistoricMessages() {
6915 return mHistoricMessages;
6916 }
6917
6918 /**
Selim Cinekce8794f2018-05-23 16:46:05 -07006919 * Sets whether this conversation notification represents a group. If the app is targeting
6920 * Android P, this is required if the app wants to display the largeIcon set with
6921 * {@link Notification.Builder#setLargeIcon(Bitmap)}, otherwise it will be hidden.
Kodlee Yin14656422017-12-22 17:00:46 -08006922 *
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006923 * @param isGroupConversation {@code true} if the conversation represents a group,
6924 * {@code false} otherwise.
6925 * @return this object for method chaining
6926 */
6927 public MessagingStyle setGroupConversation(boolean isGroupConversation) {
6928 mIsGroupConversation = isGroupConversation;
6929 return this;
6930 }
6931
6932 /**
Kodlee Yin14656422017-12-22 17:00:46 -08006933 * Returns {@code true} if this notification represents a group conversation, otherwise
6934 * {@code false}.
6935 *
6936 * <p> If the application that generated this {@link MessagingStyle} targets an SDK version
6937 * less than {@link Build.VERSION_CODES#P}, this method becomes dependent on whether or
6938 * not the conversation title is set; returning {@code true} if the conversation title is
6939 * a non-null value, or {@code false} otherwise. From {@link Build.VERSION_CODES#P} forward,
6940 * this method returns what's set by {@link #setGroupConversation(boolean)} allowing for
6941 * named, non-group conversations.
6942 *
6943 * @see #setConversationTitle(CharSequence)
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006944 */
6945 public boolean isGroupConversation() {
Kodlee Yin14656422017-12-22 17:00:46 -08006946 // When target SDK version is < P, a non-null conversation title dictates if this is
6947 // as group conversation.
6948 if (mBuilder != null
6949 && mBuilder.mContext.getApplicationInfo().targetSdkVersion
6950 < Build.VERSION_CODES.P) {
6951 return mConversationTitle != null;
6952 }
6953
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006954 return mIsGroupConversation;
6955 }
6956
6957 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006958 * @hide
6959 */
6960 @Override
6961 public void addExtras(Bundle extras) {
6962 super.addExtras(extras);
Selim Cinekcb8b9852017-12-15 18:01:52 -08006963 if (mUser != null) {
6964 // For legacy usages
6965 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUser.getName());
6966 extras.putParcelable(EXTRA_MESSAGING_PERSON, mUser);
Alex Hillsfc737de2016-03-23 17:33:02 -04006967 }
6968 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006969 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04006970 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006971 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
6972 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04006973 }
Adrian Roos437cd562017-01-18 15:47:03 -08006974 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
6975 Message.getBundleArrayForMessages(mHistoricMessages));
6976 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006977
6978 fixTitleAndTextExtras(extras);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006979 extras.putBoolean(EXTRA_IS_GROUP_CONVERSATION, mIsGroupConversation);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006980 }
6981
6982 private void fixTitleAndTextExtras(Bundle extras) {
6983 Message m = findLatestIncomingMessage();
6984 CharSequence text = (m == null) ? null : m.mText;
6985 CharSequence sender = m == null ? null
Selim Cinekcb8b9852017-12-15 18:01:52 -08006986 : m.mSender == null || TextUtils.isEmpty(m.mSender.getName())
6987 ? mUser.getName() : m.mSender.getName();
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006988 CharSequence title;
6989 if (!TextUtils.isEmpty(mConversationTitle)) {
6990 if (!TextUtils.isEmpty(sender)) {
6991 BidiFormatter bidi = BidiFormatter.getInstance();
6992 title = mBuilder.mContext.getString(
6993 com.android.internal.R.string.notification_messaging_title_template,
Selim Cinekcb8b9852017-12-15 18:01:52 -08006994 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(sender));
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006995 } else {
6996 title = mConversationTitle;
6997 }
6998 } else {
6999 title = sender;
7000 }
7001
7002 if (title != null) {
7003 extras.putCharSequence(EXTRA_TITLE, title);
7004 }
7005 if (text != null) {
7006 extras.putCharSequence(EXTRA_TEXT, text);
7007 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007008 }
7009
7010 /**
7011 * @hide
7012 */
7013 @Override
7014 protected void restoreFromExtras(Bundle extras) {
7015 super.restoreFromExtras(extras);
7016
Selim Cinekcb8b9852017-12-15 18:01:52 -08007017 mUser = extras.getParcelable(EXTRA_MESSAGING_PERSON);
7018 if (mUser == null) {
7019 CharSequence displayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
Selim Cinek9acd6732018-03-23 16:39:02 -07007020 mUser = new Person.Builder().setName(displayName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007021 }
Adrian Roos96b7e202016-05-17 13:50:38 -07007022 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08007023 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007024 mMessages = Message.getMessagesFromBundleArray(messages);
Adrian Roos437cd562017-01-18 15:47:03 -08007025 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007026 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007027 mIsGroupConversation = extras.getBoolean(EXTRA_IS_GROUP_CONVERSATION);
Alex Hillsfc737de2016-03-23 17:33:02 -04007028 }
7029
7030 /**
7031 * @hide
7032 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07007033 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007034 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07007035 mBuilder.mOriginalActions = mBuilder.mActions;
7036 mBuilder.mActions = new ArrayList<>();
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007037 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007038 false /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007039 mBuilder.mActions = mBuilder.mOriginalActions;
7040 mBuilder.mOriginalActions = null;
7041 return remoteViews;
Adrian Roosc1a80b02016-04-05 14:54:55 -07007042 }
7043
Julia Reynolds7217dc92018-03-07 12:12:09 -05007044 /**
7045 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04007046 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05007047 */
7048 @Override
7049 public boolean areNotificationsVisiblyDifferent(Style other) {
7050 if (other == null || getClass() != other.getClass()) {
7051 return true;
7052 }
7053 MessagingStyle newS = (MessagingStyle) other;
7054 List<MessagingStyle.Message> oldMs = getMessages();
7055 List<MessagingStyle.Message> newMs = newS.getMessages();
7056
Dan Sandler7d67bd42018-05-15 14:06:38 -04007057 if (oldMs == null || newMs == null) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007058 newMs = new ArrayList<>();
7059 }
7060
7061 int n = oldMs.size();
7062 if (n != newMs.size()) {
7063 return true;
7064 }
7065 for (int i = 0; i < n; i++) {
7066 MessagingStyle.Message oldM = oldMs.get(i);
7067 MessagingStyle.Message newM = newMs.get(i);
Dan Sandler7d67bd42018-05-15 14:06:38 -04007068 if (!Objects.equals(
7069 String.valueOf(oldM.getText()),
7070 String.valueOf(newM.getText()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007071 return true;
7072 }
7073 if (!Objects.equals(oldM.getDataUri(), newM.getDataUri())) {
7074 return true;
7075 }
Dan Sandler7d67bd42018-05-15 14:06:38 -04007076 String oldSender = String.valueOf(oldM.getSenderPerson() == null
7077 ? oldM.getSender()
7078 : oldM.getSenderPerson().getName());
7079 String newSender = String.valueOf(newM.getSenderPerson() == null
7080 ? newM.getSender()
7081 : newM.getSenderPerson().getName());
Julia Reynolds7217dc92018-03-07 12:12:09 -05007082 if (!Objects.equals(oldSender, newSender)) {
7083 return true;
7084 }
7085
7086 String oldKey = oldM.getSenderPerson() == null
7087 ? null : oldM.getSenderPerson().getKey();
7088 String newKey = newM.getSenderPerson() == null
7089 ? null : newM.getSenderPerson().getKey();
7090 if (!Objects.equals(oldKey, newKey)) {
7091 return true;
7092 }
7093 // Other fields (like timestamp) intentionally excluded
7094 }
7095 return false;
7096 }
7097
Adrian Roosc1a80b02016-04-05 14:54:55 -07007098 private Message findLatestIncomingMessage() {
Selim Cinek88188f22017-09-19 16:46:56 -07007099 return findLatestIncomingMessage(mMessages);
7100 }
7101
7102 /**
7103 * @hide
7104 */
7105 @Nullable
7106 public static Message findLatestIncomingMessage(
7107 List<Message> messages) {
7108 for (int i = messages.size() - 1; i >= 0; i--) {
7109 Message m = messages.get(i);
Adrian Roosc1a80b02016-04-05 14:54:55 -07007110 // Incoming messages have a non-empty sender.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007111 if (m.mSender != null && !TextUtils.isEmpty(m.mSender.getName())) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07007112 return m;
7113 }
7114 }
Selim Cinek88188f22017-09-19 16:46:56 -07007115 if (!messages.isEmpty()) {
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007116 // No incoming messages, fall back to outgoing message
Selim Cinek88188f22017-09-19 16:46:56 -07007117 return messages.get(messages.size() - 1);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007118 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07007119 return null;
7120 }
7121
7122 /**
7123 * @hide
7124 */
7125 @Override
7126 public RemoteViews makeBigContentView() {
Selim Cinek384804b2018-04-18 14:31:07 +08007127 return makeMessagingView(false /* displayImagesAtEnd */, true /* hideLargeIcon */);
Selim Cinekafeed292017-12-12 17:32:44 -08007128 }
7129
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007130 /**
7131 * Create a messaging layout.
7132 *
7133 * @param displayImagesAtEnd should images be displayed at the end of the content instead
7134 * of inline.
Selim Cinek384804b2018-04-18 14:31:07 +08007135 * @param hideRightIcons Should the reply affordance be shown at the end of the notification
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007136 * @return the created remoteView.
7137 */
Selim Cinekafeed292017-12-12 17:32:44 -08007138 @NonNull
Selim Cinek384804b2018-04-18 14:31:07 +08007139 private RemoteViews makeMessagingView(boolean displayImagesAtEnd, boolean hideRightIcons) {
Selim Cinek88188f22017-09-19 16:46:56 -07007140 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
Adrian Roosc1a80b02016-04-05 14:54:55 -07007141 ? super.mBigContentTitle
7142 : mConversationTitle;
Selim Cinekce8794f2018-05-23 16:46:05 -07007143 boolean atLeastP = mBuilder.mContext.getApplicationInfo().targetSdkVersion
7144 >= Build.VERSION_CODES.P;
7145 boolean isOneToOne;
Selim Cinek2dd3e722018-01-19 11:06:06 -08007146 CharSequence nameReplacement = null;
Selim Cinekce8794f2018-05-23 16:46:05 -07007147 Icon avatarReplacement = null;
7148 if (!atLeastP) {
7149 isOneToOne = TextUtils.isEmpty(conversationTitle);
7150 avatarReplacement = mBuilder.mN.mLargeIcon;
7151 if (hasOnlyWhiteSpaceSenders()) {
7152 isOneToOne = true;
7153 nameReplacement = conversationTitle;
7154 conversationTitle = null;
7155 }
7156 } else {
7157 isOneToOne = !isGroupConversation();
Adrian Roosb1f427c2016-05-26 12:27:15 -07007158 }
Selim Cinek384804b2018-04-18 14:31:07 +08007159 TemplateBindResult bindResult = new TemplateBindResult();
Adrian Roos48d746a2016-04-12 14:57:28 -07007160 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07007161 mBuilder.getMessagingLayoutResource(),
Selim Cinek88188f22017-09-19 16:46:56 -07007162 mBuilder.mParams.reset().hasProgress(false).title(conversationTitle).text(null)
Selim Cinek384804b2018-04-18 14:31:07 +08007163 .hideLargeIcon(hideRightIcons || isOneToOne)
7164 .hideReplyIcon(hideRightIcons)
7165 .headerTextSecondary(conversationTitle),
7166 bindResult);
Selim Cinek88188f22017-09-19 16:46:56 -07007167 addExtras(mBuilder.mN.extras);
Selim Cinekafeed292017-12-12 17:32:44 -08007168 // also update the end margin if there is an image
Selim Cinek1c72fa02018-04-23 18:00:54 +08007169 contentView.setViewLayoutMarginEnd(R.id.notification_messaging,
7170 bindResult.getIconMarginEnd());
Selim Cinek88188f22017-09-19 16:46:56 -07007171 contentView.setInt(R.id.status_bar_latest_event_content, "setLayoutColor",
Kenny Guy14d035c2018-05-02 19:10:36 +01007172 mBuilder.isColorized() ? mBuilder.getPrimaryTextColor()
7173 : mBuilder.resolveContrastColor());
7174 contentView.setInt(R.id.status_bar_latest_event_content, "setSenderTextColor",
7175 mBuilder.getPrimaryTextColor());
7176 contentView.setInt(R.id.status_bar_latest_event_content, "setMessageTextColor",
7177 mBuilder.getSecondaryTextColor());
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007178 contentView.setBoolean(R.id.status_bar_latest_event_content, "setDisplayImagesAtEnd",
7179 displayImagesAtEnd);
Selim Cinekce8794f2018-05-23 16:46:05 -07007180 contentView.setIcon(R.id.status_bar_latest_event_content, "setAvatarReplacement",
7181 avatarReplacement);
Selim Cinek2dd3e722018-01-19 11:06:06 -08007182 contentView.setCharSequence(R.id.status_bar_latest_event_content, "setNameReplacement",
7183 nameReplacement);
Selim Cinek88188f22017-09-19 16:46:56 -07007184 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsOneToOne",
7185 isOneToOne);
7186 contentView.setBundle(R.id.status_bar_latest_event_content, "setData",
7187 mBuilder.mN.extras);
Alex Hillsfc737de2016-03-23 17:33:02 -04007188 return contentView;
7189 }
7190
Selim Cinekf7409db2017-10-24 16:17:14 -07007191 private boolean hasOnlyWhiteSpaceSenders() {
7192 for (int i = 0; i < mMessages.size(); i++) {
7193 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007194 Person sender = m.getSenderPerson();
7195 if (sender != null && !isWhiteSpace(sender.getName())) {
Selim Cinekf7409db2017-10-24 16:17:14 -07007196 return false;
7197 }
7198 }
7199 return true;
7200 }
7201
7202 private boolean isWhiteSpace(CharSequence sender) {
7203 if (TextUtils.isEmpty(sender)) {
7204 return true;
7205 }
7206 if (sender.toString().matches("^\\s*$")) {
7207 return true;
7208 }
7209 // Let's check if we only have 0 whitespace chars. Some apps did this as a workaround
7210 // For the presentation that we had.
7211 for (int i = 0; i < sender.length(); i++) {
7212 char c = sender.charAt(i);
7213 if (c != '\u200B') {
7214 return false;
7215 }
7216 }
7217 return true;
7218 }
7219
Selim Cinek88188f22017-09-19 16:46:56 -07007220 private CharSequence createConversationTitleFromMessages() {
7221 ArraySet<CharSequence> names = new ArraySet<>();
7222 for (int i = 0; i < mMessages.size(); i++) {
7223 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007224 Person sender = m.getSenderPerson();
Selim Cinek88188f22017-09-19 16:46:56 -07007225 if (sender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007226 names.add(sender.getName());
Selim Cinek88188f22017-09-19 16:46:56 -07007227 }
7228 }
7229 SpannableStringBuilder title = new SpannableStringBuilder();
7230 int size = names.size();
7231 for (int i = 0; i < size; i++) {
7232 CharSequence name = names.valueAt(i);
7233 if (!TextUtils.isEmpty(title)) {
7234 title.append(", ");
7235 }
7236 title.append(BidiFormatter.getInstance().unicodeWrap(name));
7237 }
7238 return title;
7239 }
7240
Adrian Roosdedd1df2016-04-26 16:38:47 -07007241 /**
7242 * @hide
7243 */
7244 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007245 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007246 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007247 true /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007248 remoteViews.setInt(R.id.notification_messaging, "setMaxDisplayedLines", 1);
7249 return remoteViews;
Adrian Roosdedd1df2016-04-26 16:38:47 -07007250 }
7251
Adrian Roosc1a80b02016-04-05 14:54:55 -07007252 private static TextAppearanceSpan makeFontColorSpan(int color) {
7253 return new TextAppearanceSpan(null, 0, 0,
7254 ColorStateList.valueOf(color), null);
7255 }
7256
Alex Hillsd9b04d92016-04-11 16:38:16 -04007257 public static final class Message {
Tony Mak09db2ea2018-06-27 18:12:48 +01007258 /** @hide */
7259 public static final String KEY_TEXT = "text";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007260 static final String KEY_TIMESTAMP = "time";
7261 static final String KEY_SENDER = "sender";
Selim Cinekcb8b9852017-12-15 18:01:52 -08007262 static final String KEY_SENDER_PERSON = "sender_person";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007263 static final String KEY_DATA_MIME_TYPE = "type";
7264 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08007265 static final String KEY_EXTRAS_BUNDLE = "extras";
Kenny Guya0f6de82018-04-06 16:20:16 +01007266 static final String KEY_REMOTE_INPUT_HISTORY = "remote_input_history";
Alex Hillsfc737de2016-03-23 17:33:02 -04007267
7268 private final CharSequence mText;
7269 private final long mTimestamp;
Selim Cinekcb8b9852017-12-15 18:01:52 -08007270 @Nullable
7271 private final Person mSender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007272 /** True if this message was generated from the extra
7273 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}
7274 */
7275 private final boolean mRemoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007276
Shane Brennan5a871862017-03-11 13:14:17 -08007277 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007278 private String mDataMimeType;
7279 private Uri mDataUri;
7280
7281 /**
7282 * Constructor
7283 * @param text A {@link CharSequence} to be displayed as the message content
7284 * @param timestamp Time at which the message arrived
7285 * @param sender A {@link CharSequence} to be used for displaying the name of the
7286 * sender. Should be <code>null</code> for messages by the current user, in which case
7287 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
7288 * Should be unique amongst all individuals in the conversation, and should be
7289 * consistent during re-posts of the notification.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007290 *
7291 * @deprecated use {@code Message(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04007292 */
7293 public Message(CharSequence text, long timestamp, CharSequence sender){
Selim Cinek9acd6732018-03-23 16:39:02 -07007294 this(text, timestamp, sender == null ? null
7295 : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08007296 }
7297
7298 /**
7299 * Constructor
7300 * @param text A {@link CharSequence} to be displayed as the message content
7301 * @param timestamp Time at which the message arrived
7302 * @param sender The {@link Person} who sent the message.
7303 * Should be <code>null</code> for messages by the current user, in which case
7304 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7305 * <p>
Selim Cinek9acd6732018-03-23 16:39:02 -07007306 * The person provided should contain an Icon, set with
7307 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7308 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7309 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7310 * to differentiate between the different users.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007311 * </p>
7312 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007313 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender) {
Kenny Guya0f6de82018-04-06 16:20:16 +01007314 this(text, timestamp, sender, false /* remoteHistory */);
7315 }
7316
7317 /**
7318 * Constructor
7319 * @param text A {@link CharSequence} to be displayed as the message content
7320 * @param timestamp Time at which the message arrived
7321 * @param sender The {@link Person} who sent the message.
7322 * Should be <code>null</code> for messages by the current user, in which case
7323 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7324 * @param remoteInputHistory True if the messages was generated from the extra
7325 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7326 * <p>
7327 * The person provided should contain an Icon, set with
7328 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7329 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7330 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7331 * to differentiate between the different users.
7332 * </p>
7333 * @hide
7334 */
7335 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender,
7336 boolean remoteInputHistory) {
Alex Hillsfc737de2016-03-23 17:33:02 -04007337 mText = text;
7338 mTimestamp = timestamp;
7339 mSender = sender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007340 mRemoteInputHistory = remoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007341 }
7342
7343 /**
7344 * Sets a binary blob of data and an associated MIME type for a message. In the case
7345 * where the platform doesn't support the MIME type, the original text provided in the
7346 * constructor will be used.
7347 * @param dataMimeType The MIME type of the content. See
7348 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
7349 * types on Android and Android Wear.
7350 * @param dataUri The uri containing the content whose type is given by the MIME type.
7351 * <p class="note">
7352 * <ol>
7353 * <li>Notification Listeners including the System UI need permission to access the
7354 * data the Uri points to. The recommended ways to do this are:</li>
7355 * <li>Store the data in your own ContentProvider, making sure that other apps have
7356 * the correct permission to access your provider. The preferred mechanism for
7357 * providing access is to use per-URI permissions which are temporary and only
7358 * grant access to the receiving application. An easy way to create a
7359 * ContentProvider like this is to use the FileProvider helper class.</li>
7360 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
7361 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
7362 * also store non-media types (see MediaStore.Files for more info). Files can be
7363 * inserted into the MediaStore using scanFile() after which a content:// style
7364 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
7365 * Note that once added to the system MediaStore the content is accessible to any
7366 * app on the device.</li>
7367 * </ol>
7368 * @return this object for method chaining
7369 */
7370 public Message setData(String dataMimeType, Uri dataUri) {
7371 mDataMimeType = dataMimeType;
7372 mDataUri = dataUri;
7373 return this;
7374 }
7375
Alex Hillsfc737de2016-03-23 17:33:02 -04007376 /**
7377 * Get the text to be used for this message, or the fallback text if a type and content
7378 * Uri have been set
7379 */
7380 public CharSequence getText() {
7381 return mText;
7382 }
7383
7384 /**
7385 * Get the time at which this message arrived
7386 */
7387 public long getTimestamp() {
7388 return mTimestamp;
7389 }
7390
7391 /**
Shane Brennan5a871862017-03-11 13:14:17 -08007392 * Get the extras Bundle for this message.
7393 */
7394 public Bundle getExtras() {
7395 return mExtras;
7396 }
7397
7398 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007399 * Get the text used to display the contact's name in the messaging experience
Selim Cinekcb8b9852017-12-15 18:01:52 -08007400 *
7401 * @deprecated use {@link #getSenderPerson()}
Alex Hillsfc737de2016-03-23 17:33:02 -04007402 */
7403 public CharSequence getSender() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007404 return mSender == null ? null : mSender.getName();
7405 }
7406
7407 /**
7408 * Get the sender associated with this message.
7409 */
7410 @Nullable
7411 public Person getSenderPerson() {
Alex Hillsfc737de2016-03-23 17:33:02 -04007412 return mSender;
7413 }
7414
7415 /**
7416 * Get the MIME type of the data pointed to by the Uri
7417 */
7418 public String getDataMimeType() {
7419 return mDataMimeType;
7420 }
7421
7422 /**
koprivaa1a78482018-10-09 10:09:23 -07007423 * Get the Uri pointing to the content of the message. Can be null, in which case
Alex Hillsfc737de2016-03-23 17:33:02 -04007424 * {@see #getText()} is used.
7425 */
7426 public Uri getDataUri() {
7427 return mDataUri;
7428 }
7429
Kenny Guya0f6de82018-04-06 16:20:16 +01007430 /**
7431 * @return True if the message was generated from
7432 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7433 * @hide
7434 */
7435 public boolean isRemoteInputHistory() {
7436 return mRemoteInputHistory;
7437 }
7438
Beverlye98937a2018-11-15 10:18:57 -05007439 /**
7440 * @hide
7441 */
7442 @VisibleForTesting
7443 public Bundle toBundle() {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007444 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007445 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007446 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04007447 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007448 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04007449 if (mSender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007450 // Legacy listeners need this
7451 bundle.putCharSequence(KEY_SENDER, mSender.getName());
7452 bundle.putParcelable(KEY_SENDER_PERSON, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04007453 }
7454 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007455 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04007456 }
7457 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007458 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04007459 }
Shane Brennan5a871862017-03-11 13:14:17 -08007460 if (mExtras != null) {
7461 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
7462 }
Kenny Guya0f6de82018-04-06 16:20:16 +01007463 if (mRemoteInputHistory) {
7464 bundle.putBoolean(KEY_REMOTE_INPUT_HISTORY, mRemoteInputHistory);
7465 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007466 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04007467 }
7468
Alex Hillsd9b04d92016-04-11 16:38:16 -04007469 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
7470 Bundle[] bundles = new Bundle[messages.size()];
7471 final int N = messages.size();
7472 for (int i = 0; i < N; i++) {
7473 bundles[i] = messages.get(i).toBundle();
7474 }
7475 return bundles;
7476 }
7477
Selim Cinek88188f22017-09-19 16:46:56 -07007478 /**
7479 * @return A list of messages read from the bundles.
7480 *
7481 * @hide
7482 */
7483 public static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
7484 if (bundles == null) {
7485 return new ArrayList<>();
7486 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007487 List<Message> messages = new ArrayList<>(bundles.length);
7488 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07007489 if (bundles[i] instanceof Bundle) {
7490 Message message = getMessageFromBundle((Bundle)bundles[i]);
7491 if (message != null) {
7492 messages.add(message);
7493 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007494 }
7495 }
7496 return messages;
7497 }
7498
Selim Cinekb0dc61b2018-05-22 18:49:36 -07007499 /**
7500 * @return The message that is stored in the bundle or null if the message couldn't be
7501 * resolved.
7502 *
7503 * @hide
7504 */
7505 @Nullable
7506 public static Message getMessageFromBundle(Bundle bundle) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007507 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07007508 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007509 return null;
7510 } else {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007511
7512 Person senderPerson = bundle.getParcelable(KEY_SENDER_PERSON);
7513 if (senderPerson == null) {
7514 // Legacy apps that use compat don't actually provide the sender objects
7515 // We need to fix the compat version to provide people / use
7516 // the native api instead
7517 CharSequence senderName = bundle.getCharSequence(KEY_SENDER);
7518 if (senderName != null) {
Selim Cinek9acd6732018-03-23 16:39:02 -07007519 senderPerson = new Person.Builder().setName(senderName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007520 }
7521 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007522 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
Selim Cinekcb8b9852017-12-15 18:01:52 -08007523 bundle.getLong(KEY_TIMESTAMP),
Kenny Guya0f6de82018-04-06 16:20:16 +01007524 senderPerson,
7525 bundle.getBoolean(KEY_REMOTE_INPUT_HISTORY, false));
Alex Hillsd9b04d92016-04-11 16:38:16 -04007526 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
7527 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007528 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
7529 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04007530 }
Shane Brennan5a871862017-03-11 13:14:17 -08007531 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
7532 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
7533 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007534 return message;
7535 }
7536 } catch (ClassCastException e) {
7537 return null;
7538 }
7539 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007540 }
7541 }
7542
7543 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04007544 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08007545 *
Robert Ly91c5ce32014-06-08 15:37:00 -07007546 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04007547 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07007548 * Notification notif = new Notification.Builder(mContext)
7549 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
7550 * .setContentText(subject)
7551 * .setSmallIcon(R.drawable.new_mail)
7552 * .setLargeIcon(aBitmap)
7553 * .setStyle(new Notification.InboxStyle()
7554 * .addLine(str1)
7555 * .addLine(str2)
7556 * .setContentTitle(&quot;&quot;)
7557 * .setSummaryText(&quot;+3 more&quot;))
7558 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04007559 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08007560 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04007561 * @see Notification#bigContentView
7562 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007563 public static class InboxStyle extends Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07007564
7565 /**
7566 * The number of lines of remote input history allowed until we start reducing lines.
7567 */
7568 private static final int NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION = 1;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007569 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
7570
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007571 public InboxStyle() {
7572 }
7573
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007574 /**
7575 * @deprecated use {@code InboxStyle()}.
7576 */
7577 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04007578 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007579 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04007580 }
7581
Chris Wrend6297db2012-05-03 16:20:13 -04007582 /**
7583 * Overrides ContentTitle in the big form of the template.
7584 * This defaults to the value passed to setContentTitle().
7585 */
7586 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007587 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04007588 return this;
7589 }
7590
7591 /**
7592 * Set the first line of text after the detail section in the big form of the template.
7593 */
7594 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007595 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04007596 return this;
7597 }
7598
Chris Wren0bd664d2012-08-01 13:56:56 -04007599 /**
7600 * Append a line to the digest section of the Inbox notification.
7601 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04007602 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007603 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04007604 return this;
7605 }
7606
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007607 /**
7608 * @hide
7609 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05007610 public ArrayList<CharSequence> getLines() {
7611 return mTexts;
7612 }
7613
7614 /**
7615 * @hide
7616 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007617 public void addExtras(Bundle extras) {
7618 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007619
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007620 CharSequence[] a = new CharSequence[mTexts.size()];
7621 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
7622 }
7623
Christoph Studer4600f9b2014-07-22 22:44:43 +02007624 /**
7625 * @hide
7626 */
7627 @Override
7628 protected void restoreFromExtras(Bundle extras) {
7629 super.restoreFromExtras(extras);
7630
7631 mTexts.clear();
7632 if (extras.containsKey(EXTRA_TEXT_LINES)) {
7633 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
7634 }
7635 }
7636
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007637 /**
7638 * @hide
7639 */
7640 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08007641 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02007642 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007643 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
7644 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007645
Selim Cinek384804b2018-04-18 14:31:07 +08007646 TemplateBindResult result = new TemplateBindResult();
7647 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource(), result);
Daniel Sandler619738c2012-06-07 16:33:08 -04007648
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007649 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007650
Chris Wrend6297db2012-05-03 16:20:13 -04007651 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 -04007652 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04007653
Chris Wren4ed80d52012-05-17 09:30:03 -04007654 // Make sure all rows are gone in case we reuse a view.
7655 for (int rowId : rowIds) {
7656 contentView.setViewVisibility(rowId, View.GONE);
7657 }
7658
Daniel Sandler879c5e02012-04-17 16:46:51 -04007659 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07007660 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7661 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08007662 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07007663 int onlyViewId = 0;
7664 int maxRows = rowIds.length;
7665 if (mBuilder.mActions.size() > 0) {
7666 maxRows--;
7667 }
Selim Cinekbee4e072018-05-21 22:06:43 -07007668 CharSequence[] remoteInputHistory = mBuilder.mN.extras.getCharSequenceArray(
7669 EXTRA_REMOTE_INPUT_HISTORY);
7670 if (remoteInputHistory != null
7671 && remoteInputHistory.length > NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION) {
7672 // Let's remove some messages to make room for the remote input history.
7673 // 1 is always able to fit, but let's remove them if they are 2 or 3
7674 int numRemoteInputs = Math.min(remoteInputHistory.length,
7675 MAX_REMOTE_INPUT_HISTORY_LINES);
7676 int totalNumRows = mTexts.size() + numRemoteInputs
7677 - NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION;
7678 if (totalNumRows > maxRows) {
7679 int overflow = totalNumRows - maxRows;
7680 if (mTexts.size() > maxRows) {
7681 // Heuristic: if the Texts don't fit anyway, we'll rather drop the last
7682 // few messages, even with the remote input
7683 maxRows -= overflow;
7684 } else {
7685 // otherwise we drop the first messages
7686 i = overflow;
7687 }
7688 }
7689 }
Selim Cinek07c80172016-04-21 16:40:47 -07007690 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007691 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07007692 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007693 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07007694 contentView.setTextViewText(rowIds[i],
7695 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08007696 mBuilder.setTextViewColorSecondary(contentView, rowIds[i]);
Selim Cinek07c80172016-04-21 16:40:47 -07007697 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek384804b2018-04-18 14:31:07 +08007698 handleInboxImageMargin(contentView, rowIds[i], first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007699 result.getIconMarginEnd());
Selim Cinek07c80172016-04-21 16:40:47 -07007700 if (first) {
7701 onlyViewId = rowIds[i];
7702 } else {
7703 onlyViewId = 0;
7704 }
Selim Cinek247fa012016-02-18 09:50:48 -08007705 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007706 }
7707 i++;
7708 }
Selim Cinek07c80172016-04-21 16:40:47 -07007709 if (onlyViewId != 0) {
7710 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
7711 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7712 R.dimen.notification_text_margin_top);
7713 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
7714 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007715
Daniel Sandler879c5e02012-04-17 16:46:51 -04007716 return contentView;
7717 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007718
Julia Reynolds7217dc92018-03-07 12:12:09 -05007719 /**
7720 * @hide
7721 */
7722 @Override
7723 public boolean areNotificationsVisiblyDifferent(Style other) {
7724 if (other == null || getClass() != other.getClass()) {
7725 return true;
7726 }
7727 InboxStyle newS = (InboxStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04007728
7729 final ArrayList<CharSequence> myLines = getLines();
7730 final ArrayList<CharSequence> newLines = newS.getLines();
7731 final int n = myLines.size();
7732 if (n != newLines.size()) {
7733 return true;
7734 }
7735
7736 for (int i = 0; i < n; i++) {
7737 if (!Objects.equals(
7738 String.valueOf(myLines.get(i)),
7739 String.valueOf(newLines.get(i)))) {
7740 return true;
7741 }
7742 }
7743 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05007744 }
7745
Selim Cinek384804b2018-04-18 14:31:07 +08007746 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007747 int marginEndValue) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08007748 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08007749 if (first) {
7750 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
7751 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
7752 boolean hasProgress = max != 0 || ind;
Selim Cinek384804b2018-04-18 14:31:07 +08007753 if (!hasProgress) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08007754 endMargin = marginEndValue;
Selim Cinek247fa012016-02-18 09:50:48 -08007755 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007756 }
Selim Cinek1c72fa02018-04-23 18:00:54 +08007757 contentView.setViewLayoutMarginEnd(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08007758 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04007759 }
Dan Sandler842dd772014-05-15 09:36:47 -04007760
7761 /**
7762 * Notification style for media playback notifications.
7763 *
7764 * In the expanded form, {@link Notification#bigContentView}, up to 5
7765 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04007766 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04007767 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
7768 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
7769 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08007770 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04007771 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
7772 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01007773 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04007774 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08007775 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007776 * Notifications created with MediaStyle will have their category set to
7777 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
7778 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08007779 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07007780 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
7781 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04007782 * the System UI can identify this as a notification representing an active media session
7783 * and respond accordingly (by showing album artwork in the lockscreen, for example).
7784 *
Selim Cinek99104832017-01-25 14:47:33 -08007785 * <p>
7786 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
7787 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
7788 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
7789 * <p>
7790 *
Dan Sandler842dd772014-05-15 09:36:47 -04007791 * To use this style with your Notification, feed it to
7792 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7793 * <pre class="prettyprint">
7794 * Notification noti = new Notification.Builder()
7795 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01007796 * .setContentTitle(&quot;Track title&quot;)
7797 * .setContentText(&quot;Artist - Album&quot;)
7798 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007799 * .setStyle(<b>new Notification.MediaStyle()</b>
7800 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04007801 * .build();
7802 * </pre>
7803 *
7804 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08007805 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04007806 */
7807 public static class MediaStyle extends Style {
Gus Prevas9cc96602018-10-11 11:24:23 -04007808 // Changing max media buttons requires also changing templates
7809 // (notification_template_material_media and notification_template_material_big_media).
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007810 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04007811 static final int MAX_MEDIA_BUTTONS = 5;
Gus Prevas9cc96602018-10-11 11:24:23 -04007812 @IdRes private static final int[] MEDIA_BUTTON_IDS = {
7813 R.id.action0,
7814 R.id.action1,
7815 R.id.action2,
7816 R.id.action3,
7817 R.id.action4,
7818 };
Dan Sandler842dd772014-05-15 09:36:47 -04007819
7820 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07007821 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04007822
7823 public MediaStyle() {
7824 }
7825
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007826 /**
7827 * @deprecated use {@code MediaStyle()}.
7828 */
7829 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04007830 public MediaStyle(Builder builder) {
7831 setBuilder(builder);
7832 }
7833
7834 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007835 * 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 -04007836 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007837 *
7838 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04007839 */
7840 public MediaStyle setShowActionsInCompactView(int...actions) {
7841 mActionsToShowInCompact = actions;
7842 return this;
7843 }
7844
7845 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07007846 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
7847 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04007848 */
Jeff Browndba34ba2014-06-24 20:46:03 -07007849 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04007850 mToken = token;
7851 return this;
7852 }
7853
Christoph Studer4600f9b2014-07-22 22:44:43 +02007854 /**
7855 * @hide
7856 */
Dan Sandler842dd772014-05-15 09:36:47 -04007857 @Override
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007858 @UnsupportedAppUsage
Dan Sandler842dd772014-05-15 09:36:47 -04007859 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02007860 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007861 if (wip.category == null) {
7862 wip.category = Notification.CATEGORY_TRANSPORT;
7863 }
Dan Sandler842dd772014-05-15 09:36:47 -04007864 return wip;
7865 }
7866
Christoph Studer4600f9b2014-07-22 22:44:43 +02007867 /**
7868 * @hide
7869 */
7870 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007871 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007872 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007873 }
7874
7875 /**
7876 * @hide
7877 */
7878 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007879 public RemoteViews makeBigContentView() {
7880 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007881 }
7882
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007883 /**
7884 * @hide
7885 */
7886 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007887 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007888 RemoteViews expanded = makeMediaBigContentView();
7889 return expanded != null ? expanded : makeMediaContentView();
7890 }
7891
Dan Sandler842dd772014-05-15 09:36:47 -04007892 /** @hide */
7893 @Override
7894 public void addExtras(Bundle extras) {
7895 super.addExtras(extras);
7896
7897 if (mToken != null) {
7898 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
7899 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01007900 if (mActionsToShowInCompact != null) {
7901 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
7902 }
Dan Sandler842dd772014-05-15 09:36:47 -04007903 }
7904
Christoph Studer4600f9b2014-07-22 22:44:43 +02007905 /**
7906 * @hide
7907 */
7908 @Override
7909 protected void restoreFromExtras(Bundle extras) {
7910 super.restoreFromExtras(extras);
7911
7912 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
7913 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
7914 }
7915 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
7916 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
7917 }
7918 }
7919
Julia Reynolds7217dc92018-03-07 12:12:09 -05007920 /**
7921 * @hide
7922 */
7923 @Override
7924 public boolean areNotificationsVisiblyDifferent(Style other) {
7925 if (other == null || getClass() != other.getClass()) {
7926 return true;
7927 }
7928 // All fields to compare are on the Notification object
7929 return false;
7930 }
7931
Gus Prevas9cc96602018-10-11 11:24:23 -04007932 private void bindMediaActionButton(RemoteViews container, @IdRes int buttonId,
7933 Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04007934 final boolean tombstone = (action.actionIntent == null);
Gus Prevas9cc96602018-10-11 11:24:23 -04007935 container.setViewVisibility(buttonId, View.VISIBLE);
7936 container.setImageViewIcon(buttonId, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07007937
7938 // If the action buttons should not be tinted, then just use the default
7939 // notification color. Otherwise, just use the passed-in color.
Gus Prevas9cc96602018-10-11 11:24:23 -04007940 Resources resources = mBuilder.mContext.getResources();
7941 Configuration currentConfig = resources.getConfiguration();
Lucas Dupinf03e7522018-06-25 16:21:13 -07007942 boolean inNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
7943 == Configuration.UI_MODE_NIGHT_YES;
Anthony Chenad4d1582017-04-10 16:07:58 -07007944 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized()
7945 ? color
Lucas Dupina291d192018-06-07 13:59:42 -07007946 : ContrastColorUtil.resolveColor(mBuilder.mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07007947 Notification.COLOR_DEFAULT, inNightMode);
Anthony Chenad4d1582017-04-10 16:07:58 -07007948
Gus Prevas9cc96602018-10-11 11:24:23 -04007949 container.setDrawableTint(buttonId, false, tintColor,
Sunny Goyal5b153922017-09-21 21:00:36 -07007950 PorterDuff.Mode.SRC_ATOP);
Gus Prevas9cc96602018-10-11 11:24:23 -04007951
7952 final TypedArray typedArray = mBuilder.mContext.obtainStyledAttributes(
7953 new int[]{ android.R.attr.colorControlHighlight });
7954 int rippleAlpha = Color.alpha(typedArray.getColor(0, 0));
7955 typedArray.recycle();
7956 int rippleColor = Color.argb(rippleAlpha, Color.red(tintColor), Color.green(tintColor),
7957 Color.blue(tintColor));
7958 container.setRippleDrawableColor(buttonId, ColorStateList.valueOf(rippleColor));
7959
Dan Sandler842dd772014-05-15 09:36:47 -04007960 if (!tombstone) {
Gus Prevas9cc96602018-10-11 11:24:23 -04007961 container.setOnClickPendingIntent(buttonId, action.actionIntent);
Dan Sandler842dd772014-05-15 09:36:47 -04007962 }
Gus Prevas9cc96602018-10-11 11:24:23 -04007963 container.setContentDescription(buttonId, action.title);
Dan Sandler842dd772014-05-15 09:36:47 -04007964 }
7965
7966 private RemoteViews makeMediaContentView() {
7967 RemoteViews view = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08007968 R.layout.notification_template_material_media, false, /* hasProgress */
7969 null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04007970
7971 final int numActions = mBuilder.mActions.size();
Gus Prevas9cc96602018-10-11 11:24:23 -04007972 final int numActionsToShow = mActionsToShowInCompact == null
Dan Sandler842dd772014-05-15 09:36:47 -04007973 ? 0
7974 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Gus Prevas9cc96602018-10-11 11:24:23 -04007975 if (numActionsToShow > numActions) {
7976 throw new IllegalArgumentException(String.format(
7977 "setShowActionsInCompactView: action %d out of bounds (max %d)",
7978 numActions, numActions - 1));
7979 }
7980 for (int i = 0; i < MAX_MEDIA_BUTTONS_IN_COMPACT; i++) {
7981 if (i < numActionsToShow) {
Dan Sandler842dd772014-05-15 09:36:47 -04007982 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Gus Prevas9cc96602018-10-11 11:24:23 -04007983 bindMediaActionButton(view, MEDIA_BUTTON_IDS[i], action, getActionColor());
7984 } else {
7985 view.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04007986 }
7987 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08007988 handleImage(view);
7989 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007990 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07007991 if (mBuilder.mN.hasLargeIcon()) {
Selim Cinek384804b2018-04-18 14:31:07 +08007992 endMargin = R.dimen.notification_media_image_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08007993 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007994 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04007995 return view;
7996 }
7997
Selim Cinekc7f5a822018-03-20 19:32:06 -07007998 private int getActionColor() {
7999 return mBuilder.isColorized() ? mBuilder.getPrimaryTextColor()
8000 : mBuilder.resolveContrastColor();
Selim Cinek99104832017-01-25 14:47:33 -08008001 }
8002
Dan Sandler842dd772014-05-15 09:36:47 -04008003 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008004 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008005 // Dont add an expanded view if there is no more content to be revealed
8006 int actionsInCompact = mActionsToShowInCompact == null
8007 ? 0
8008 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07008009 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008010 return null;
8011 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05008012 RemoteViews big = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08008013 R.layout.notification_template_material_big_media, false, null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04008014
Gus Prevas9cc96602018-10-11 11:24:23 -04008015 for (int i = 0; i < MAX_MEDIA_BUTTONS; i++) {
8016 if (i < actionCount) {
8017 bindMediaActionButton(big, MEDIA_BUTTON_IDS[i], mBuilder.mActions.get(i),
Selim Cinekc7f5a822018-03-20 19:32:06 -07008018 getActionColor());
Gus Prevas9cc96602018-10-11 11:24:23 -04008019 } else {
8020 big.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04008021 }
8022 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05008023 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04008024 return big;
8025 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008026
Selim Cinek5bf069a2015-11-10 19:14:27 -05008027 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07008028 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008029 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
8030 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008031 }
8032 }
8033
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008034 /**
8035 * @hide
8036 */
8037 @Override
8038 protected boolean hasProgress() {
8039 return false;
8040 }
Dan Sandler842dd772014-05-15 09:36:47 -04008041 }
Griff Hazen61a9e862014-05-22 16:05:19 -07008042
Selim Cinek593610c2016-02-16 18:42:57 -08008043 /**
8044 * Notification style for custom views that are decorated by the system
8045 *
8046 * <p>Instead of providing a notification that is completely custom, a developer can set this
8047 * style and still obtain system decorations like the notification header with the expand
8048 * affordance and actions.
8049 *
8050 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8051 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8052 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8053 * corresponding custom views to display.
8054 *
8055 * To use this style with your Notification, feed it to
8056 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8057 * <pre class="prettyprint">
8058 * Notification noti = new Notification.Builder()
8059 * .setSmallIcon(R.drawable.ic_stat_player)
8060 * .setLargeIcon(albumArtBitmap))
8061 * .setCustomContentView(contentView);
8062 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
8063 * .build();
8064 * </pre>
8065 */
8066 public static class DecoratedCustomViewStyle extends Style {
8067
8068 public DecoratedCustomViewStyle() {
8069 }
8070
Selim Cinek593610c2016-02-16 18:42:57 -08008071 /**
8072 * @hide
8073 */
8074 public boolean displayCustomViewInline() {
8075 return true;
8076 }
8077
8078 /**
8079 * @hide
8080 */
8081 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008082 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008083 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
8084 }
8085
8086 /**
8087 * @hide
8088 */
8089 @Override
8090 public RemoteViews makeBigContentView() {
8091 return makeDecoratedBigContentView();
8092 }
8093
8094 /**
8095 * @hide
8096 */
8097 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008098 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008099 return makeDecoratedHeadsUpContentView();
8100 }
8101
Selim Cinek593610c2016-02-16 18:42:57 -08008102 private RemoteViews makeDecoratedHeadsUpContentView() {
8103 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
8104 ? mBuilder.mN.contentView
8105 : mBuilder.mN.headsUpContentView;
8106 if (mBuilder.mActions.size() == 0) {
8107 return makeStandardTemplateWithCustomContent(headsUpContentView);
8108 }
Selim Cinek384804b2018-04-18 14:31:07 +08008109 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008110 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008111 mBuilder.getBigBaseLayoutResource(), result);
8112 buildIntoRemoteViewContent(remoteViews, headsUpContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008113 return remoteViews;
8114 }
8115
Selim Cinek593610c2016-02-16 18:42:57 -08008116 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
Selim Cinek384804b2018-04-18 14:31:07 +08008117 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008118 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08008119 mBuilder.getBaseLayoutResource(), result);
8120 buildIntoRemoteViewContent(remoteViews, customContent, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008121 return remoteViews;
8122 }
8123
Selim Cinek593610c2016-02-16 18:42:57 -08008124 private RemoteViews makeDecoratedBigContentView() {
8125 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
8126 ? mBuilder.mN.contentView
8127 : mBuilder.mN.bigContentView;
8128 if (mBuilder.mActions.size() == 0) {
8129 return makeStandardTemplateWithCustomContent(bigContentView);
8130 }
Selim Cinek384804b2018-04-18 14:31:07 +08008131 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008132 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008133 mBuilder.getBigBaseLayoutResource(), result);
8134 buildIntoRemoteViewContent(remoteViews, bigContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008135 return remoteViews;
8136 }
Selim Cinek247fa012016-02-18 09:50:48 -08008137
8138 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
Selim Cinek384804b2018-04-18 14:31:07 +08008139 RemoteViews customContent, TemplateBindResult result) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08008140 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008141 // Need to clone customContent before adding, because otherwise it can no longer be
8142 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008143 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07008144 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
8145 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Selim Cinekfc8073c2017-08-16 17:50:20 -07008146 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08008147 }
Selim Cinek247fa012016-02-18 09:50:48 -08008148 // also update the end margin if there is an image
Selim Cinek384804b2018-04-18 14:31:07 +08008149 Resources resources = mBuilder.mContext.getResources();
8150 int endMargin = resources.getDimensionPixelSize(
Selim Cinek1c72fa02018-04-23 18:00:54 +08008151 R.dimen.notification_content_margin_end) + result.getIconMarginEnd();
Selim Cinek384804b2018-04-18 14:31:07 +08008152 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08008153 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05008154
8155 /**
8156 * @hide
8157 */
8158 @Override
8159 public boolean areNotificationsVisiblyDifferent(Style other) {
8160 if (other == null || getClass() != other.getClass()) {
8161 return true;
8162 }
8163 // Comparison done for all custom RemoteViews, independent of style
8164 return false;
8165 }
Selim Cinek593610c2016-02-16 18:42:57 -08008166 }
8167
Selim Cinek03eb3b72016-02-18 10:39:45 -08008168 /**
8169 * Notification style for media custom views that are decorated by the system
8170 *
8171 * <p>Instead of providing a media notification that is completely custom, a developer can set
8172 * this style and still obtain system decorations like the notification header with the expand
8173 * affordance and actions.
8174 *
8175 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8176 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8177 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8178 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08008179 * <p>
8180 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
8181 * notification by using {@link Notification.Builder#setColorized(boolean)}.
8182 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08008183 * To use this style with your Notification, feed it to
8184 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8185 * <pre class="prettyprint">
8186 * Notification noti = new Notification.Builder()
8187 * .setSmallIcon(R.drawable.ic_stat_player)
8188 * .setLargeIcon(albumArtBitmap))
8189 * .setCustomContentView(contentView);
8190 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
8191 * .setMediaSession(mySession))
8192 * .build();
8193 * </pre>
8194 *
8195 * @see android.app.Notification.DecoratedCustomViewStyle
8196 * @see android.app.Notification.MediaStyle
8197 */
8198 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
8199
8200 public DecoratedMediaCustomViewStyle() {
8201 }
8202
Selim Cinek03eb3b72016-02-18 10:39:45 -08008203 /**
8204 * @hide
8205 */
8206 public boolean displayCustomViewInline() {
8207 return true;
8208 }
8209
8210 /**
8211 * @hide
8212 */
8213 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008214 public RemoteViews makeContentView(boolean increasedHeight) {
8215 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008216 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8217 mBuilder.mN.contentView);
8218 }
8219
8220 /**
8221 * @hide
8222 */
8223 @Override
8224 public RemoteViews makeBigContentView() {
8225 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
8226 ? mBuilder.mN.bigContentView
8227 : mBuilder.mN.contentView;
8228 return makeBigContentViewWithCustomContent(customRemoteView);
8229 }
8230
8231 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
8232 RemoteViews remoteViews = super.makeBigContentView();
8233 if (remoteViews != null) {
8234 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
8235 customRemoteView);
8236 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08008237 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008238 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8239 customRemoteView);
8240 } else {
8241 return null;
8242 }
8243 }
8244
8245 /**
8246 * @hide
8247 */
8248 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008249 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08008250 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
8251 ? mBuilder.mN.headsUpContentView
8252 : mBuilder.mN.contentView;
8253 return makeBigContentViewWithCustomContent(customRemoteView);
8254 }
8255
Julia Reynolds7217dc92018-03-07 12:12:09 -05008256 /**
8257 * @hide
8258 */
8259 @Override
8260 public boolean areNotificationsVisiblyDifferent(Style other) {
8261 if (other == null || getClass() != other.getClass()) {
8262 return true;
8263 }
8264 // Comparison done for all custom RemoteViews, independent of style
8265 return false;
8266 }
8267
Selim Cinek03eb3b72016-02-18 10:39:45 -08008268 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
8269 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08008270 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008271 // Need to clone customContent before adding, because otherwise it can no longer be
8272 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008273 customContent = customContent.clone();
Selim Cinek87c31532017-08-18 18:53:44 -07008274 customContent.overrideTextColors(mBuilder.getPrimaryTextColor());
Selim Cinekf91017e2016-03-14 12:25:09 -07008275 remoteViews.removeAllViews(id);
8276 remoteViews.addView(id, customContent);
Selim Cinekfc8073c2017-08-16 17:50:20 -07008277 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08008278 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08008279 return remoteViews;
8280 }
8281 }
8282
Christoph Studer4600f9b2014-07-22 22:44:43 +02008283 // When adding a new Style subclass here, don't forget to update
8284 // Builder.getNotificationStyleClass.
8285
Griff Hazen61a9e862014-05-22 16:05:19 -07008286 /**
8287 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
8288 * metadata or change options on a notification builder.
8289 */
8290 public interface Extender {
8291 /**
8292 * Apply this extender to a notification builder.
8293 * @param builder the builder to be modified.
8294 * @return the build object for chaining.
8295 */
8296 public Builder extend(Builder builder);
8297 }
8298
8299 /**
8300 * Helper class to add wearable extensions to notifications.
8301 * <p class="note"> See
8302 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
8303 * for Android Wear</a> for more information on how to use this class.
8304 * <p>
8305 * To create a notification with wearable extensions:
8306 * <ol>
8307 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
8308 * properties.
8309 * <li>Create a {@link android.app.Notification.WearableExtender}.
8310 * <li>Set wearable-specific properties using the
8311 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
8312 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
8313 * notification.
8314 * <li>Post the notification to the notification system with the
8315 * {@code NotificationManager.notify(...)} methods.
8316 * </ol>
8317 *
8318 * <pre class="prettyprint">
8319 * Notification notif = new Notification.Builder(mContext)
8320 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
8321 * .setContentText(subject)
8322 * .setSmallIcon(R.drawable.new_mail)
8323 * .extend(new Notification.WearableExtender()
8324 * .setContentIcon(R.drawable.new_mail))
8325 * .build();
8326 * NotificationManager notificationManger =
8327 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
8328 * notificationManger.notify(0, notif);</pre>
8329 *
8330 * <p>Wearable extensions can be accessed on an existing notification by using the
8331 * {@code WearableExtender(Notification)} constructor,
8332 * and then using the {@code get} methods to access values.
8333 *
8334 * <pre class="prettyprint">
8335 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
8336 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07008337 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008338 */
8339 public static final class WearableExtender implements Extender {
8340 /**
8341 * Sentinel value for an action index that is unset.
8342 */
8343 public static final int UNSET_ACTION_INDEX = -1;
8344
8345 /**
8346 * Size value for use with {@link #setCustomSizePreset} to show this notification with
8347 * default sizing.
8348 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07008349 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07008350 * on their content.
Gus Prevasd7363752018-09-18 14:35:15 -04008351 *
8352 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008353 */
Gus Prevasd7363752018-09-18 14:35:15 -04008354 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008355 public static final int SIZE_DEFAULT = 0;
8356
8357 /**
8358 * Size value for use with {@link #setCustomSizePreset} to show this notification
8359 * with an extra small size.
8360 * <p>This value is only applicable for custom display notifications created using
8361 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008362 *
8363 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008364 */
Gus Prevasd7363752018-09-18 14:35:15 -04008365 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008366 public static final int SIZE_XSMALL = 1;
8367
8368 /**
8369 * Size value for use with {@link #setCustomSizePreset} to show this notification
8370 * with a small size.
8371 * <p>This value is only applicable for custom display notifications created using
8372 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008373 *
8374 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008375 */
Gus Prevasd7363752018-09-18 14:35:15 -04008376 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008377 public static final int SIZE_SMALL = 2;
8378
8379 /**
8380 * Size value for use with {@link #setCustomSizePreset} to show this notification
8381 * with a medium size.
8382 * <p>This value is only applicable for custom display notifications created using
8383 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008384 *
8385 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008386 */
Gus Prevasd7363752018-09-18 14:35:15 -04008387 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008388 public static final int SIZE_MEDIUM = 3;
8389
8390 /**
8391 * Size value for use with {@link #setCustomSizePreset} to show this notification
8392 * with a large size.
8393 * <p>This value is only applicable for custom display notifications created using
8394 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008395 *
8396 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008397 */
Gus Prevasd7363752018-09-18 14:35:15 -04008398 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008399 public static final int SIZE_LARGE = 4;
8400
Griff Hazend5f11f92014-05-27 15:40:09 -07008401 /**
8402 * Size value for use with {@link #setCustomSizePreset} to show this notification
8403 * full screen.
8404 * <p>This value is only applicable for custom display notifications created using
8405 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008406 *
8407 * @deprecated Display intents are no longer supported.
Griff Hazend5f11f92014-05-27 15:40:09 -07008408 */
Gus Prevasd7363752018-09-18 14:35:15 -04008409 @Deprecated
Griff Hazend5f11f92014-05-27 15:40:09 -07008410 public static final int SIZE_FULL_SCREEN = 5;
8411
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008412 /**
8413 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
8414 * short amount of time when this notification is displayed on the screen. This
8415 * is the default value.
Gus Prevasd7363752018-09-18 14:35:15 -04008416 *
8417 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008418 */
Gus Prevasd7363752018-09-18 14:35:15 -04008419 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008420 public static final int SCREEN_TIMEOUT_SHORT = 0;
8421
8422 /**
8423 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
8424 * for a longer amount of time when this notification is displayed on the screen.
Gus Prevasd7363752018-09-18 14:35:15 -04008425 *
8426 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008427 */
Gus Prevasd7363752018-09-18 14:35:15 -04008428 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008429 public static final int SCREEN_TIMEOUT_LONG = -1;
8430
Griff Hazen61a9e862014-05-22 16:05:19 -07008431 /** Notification extra which contains wearable extensions */
8432 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
8433
Pete Gastaf6781d2014-10-07 15:17:05 -04008434 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07008435 private static final String KEY_ACTIONS = "actions";
8436 private static final String KEY_FLAGS = "flags";
8437 private static final String KEY_DISPLAY_INTENT = "displayIntent";
8438 private static final String KEY_PAGES = "pages";
8439 private static final String KEY_BACKGROUND = "background";
8440 private static final String KEY_CONTENT_ICON = "contentIcon";
8441 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
8442 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
8443 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
8444 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
8445 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008446 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04008447 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008448 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07008449
8450 // Flags bitwise-ored to mFlags
8451 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
8452 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
8453 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
8454 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008455 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04008456 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04008457 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07008458
8459 // Default value for flags integer
8460 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
8461
8462 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
8463 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
8464
8465 private ArrayList<Action> mActions = new ArrayList<Action>();
8466 private int mFlags = DEFAULT_FLAGS;
8467 private PendingIntent mDisplayIntent;
8468 private ArrayList<Notification> mPages = new ArrayList<Notification>();
8469 private Bitmap mBackground;
8470 private int mContentIcon;
8471 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
8472 private int mContentActionIndex = UNSET_ACTION_INDEX;
8473 private int mCustomSizePreset = SIZE_DEFAULT;
8474 private int mCustomContentHeight;
8475 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008476 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04008477 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008478 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07008479
8480 /**
8481 * Create a {@link android.app.Notification.WearableExtender} with default
8482 * options.
8483 */
8484 public WearableExtender() {
8485 }
8486
8487 public WearableExtender(Notification notif) {
8488 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
8489 if (wearableBundle != null) {
8490 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
8491 if (actions != null) {
8492 mActions.addAll(actions);
8493 }
8494
8495 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
8496 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
8497
8498 Notification[] pages = getNotificationArrayFromBundle(
8499 wearableBundle, KEY_PAGES);
8500 if (pages != null) {
8501 Collections.addAll(mPages, pages);
8502 }
8503
8504 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
8505 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
8506 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
8507 DEFAULT_CONTENT_ICON_GRAVITY);
8508 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
8509 UNSET_ACTION_INDEX);
8510 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
8511 SIZE_DEFAULT);
8512 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
8513 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008514 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04008515 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008516 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07008517 }
8518 }
8519
8520 /**
8521 * Apply wearable extensions to a notification that is being built. This is typically
8522 * called by the {@link android.app.Notification.Builder#extend} method of
8523 * {@link android.app.Notification.Builder}.
8524 */
8525 @Override
8526 public Notification.Builder extend(Notification.Builder builder) {
8527 Bundle wearableBundle = new Bundle();
8528
8529 if (!mActions.isEmpty()) {
8530 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
8531 }
8532 if (mFlags != DEFAULT_FLAGS) {
8533 wearableBundle.putInt(KEY_FLAGS, mFlags);
8534 }
8535 if (mDisplayIntent != null) {
8536 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
8537 }
8538 if (!mPages.isEmpty()) {
8539 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
8540 new Notification[mPages.size()]));
8541 }
8542 if (mBackground != null) {
8543 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
8544 }
8545 if (mContentIcon != 0) {
8546 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
8547 }
8548 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
8549 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
8550 }
8551 if (mContentActionIndex != UNSET_ACTION_INDEX) {
8552 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
8553 mContentActionIndex);
8554 }
8555 if (mCustomSizePreset != SIZE_DEFAULT) {
8556 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
8557 }
8558 if (mCustomContentHeight != 0) {
8559 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
8560 }
8561 if (mGravity != DEFAULT_GRAVITY) {
8562 wearableBundle.putInt(KEY_GRAVITY, mGravity);
8563 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008564 if (mHintScreenTimeout != 0) {
8565 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
8566 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04008567 if (mDismissalId != null) {
8568 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
8569 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008570 if (mBridgeTag != null) {
8571 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
8572 }
Griff Hazen61a9e862014-05-22 16:05:19 -07008573
8574 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
8575 return builder;
8576 }
8577
8578 @Override
8579 public WearableExtender clone() {
8580 WearableExtender that = new WearableExtender();
8581 that.mActions = new ArrayList<Action>(this.mActions);
8582 that.mFlags = this.mFlags;
8583 that.mDisplayIntent = this.mDisplayIntent;
8584 that.mPages = new ArrayList<Notification>(this.mPages);
8585 that.mBackground = this.mBackground;
8586 that.mContentIcon = this.mContentIcon;
8587 that.mContentIconGravity = this.mContentIconGravity;
8588 that.mContentActionIndex = this.mContentActionIndex;
8589 that.mCustomSizePreset = this.mCustomSizePreset;
8590 that.mCustomContentHeight = this.mCustomContentHeight;
8591 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008592 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04008593 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008594 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07008595 return that;
8596 }
8597
8598 /**
8599 * Add a wearable action to this notification.
8600 *
8601 * <p>When wearable actions are added using this method, the set of actions that
8602 * show on a wearable device splits from devices that only show actions added
8603 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
8604 * of which actions display on different devices.
8605 *
8606 * @param action the action to add to this notification
8607 * @return this object for method chaining
8608 * @see android.app.Notification.Action
8609 */
8610 public WearableExtender addAction(Action action) {
8611 mActions.add(action);
8612 return this;
8613 }
8614
8615 /**
8616 * Adds wearable actions to this notification.
8617 *
8618 * <p>When wearable actions are added using this method, the set of actions that
8619 * show on a wearable device splits from devices that only show actions added
8620 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
8621 * of which actions display on different devices.
8622 *
8623 * @param actions the actions to add to this notification
8624 * @return this object for method chaining
8625 * @see android.app.Notification.Action
8626 */
8627 public WearableExtender addActions(List<Action> actions) {
8628 mActions.addAll(actions);
8629 return this;
8630 }
8631
8632 /**
8633 * Clear all wearable actions present on this builder.
8634 * @return this object for method chaining.
8635 * @see #addAction
8636 */
8637 public WearableExtender clearActions() {
8638 mActions.clear();
8639 return this;
8640 }
8641
8642 /**
8643 * Get the wearable actions present on this notification.
8644 */
8645 public List<Action> getActions() {
8646 return mActions;
8647 }
8648
8649 /**
8650 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07008651 * this notification. The {@link PendingIntent} provided should be for an activity.
8652 *
8653 * <pre class="prettyprint">
8654 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
8655 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
8656 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
8657 * Notification notif = new Notification.Builder(context)
8658 * .extend(new Notification.WearableExtender()
8659 * .setDisplayIntent(displayPendingIntent)
8660 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
8661 * .build();</pre>
8662 *
8663 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07008664 * should have an empty task affinity. It is also recommended to use the device
8665 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07008666 *
8667 * <p>Example AndroidManifest.xml entry:
8668 * <pre class="prettyprint">
8669 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
8670 * android:exported=&quot;true&quot;
8671 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07008672 * android:taskAffinity=&quot;&quot;
8673 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008674 *
8675 * @param intent the {@link PendingIntent} for an activity
8676 * @return this object for method chaining
8677 * @see android.app.Notification.WearableExtender#getDisplayIntent
Gus Prevasd7363752018-09-18 14:35:15 -04008678 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008679 */
Gus Prevasd7363752018-09-18 14:35:15 -04008680 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008681 public WearableExtender setDisplayIntent(PendingIntent intent) {
8682 mDisplayIntent = intent;
8683 return this;
8684 }
8685
8686 /**
8687 * Get the intent to launch inside of an activity view when displaying this
8688 * notification. This {@code PendingIntent} should be for an activity.
Gus Prevasd7363752018-09-18 14:35:15 -04008689 *
8690 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008691 */
Gus Prevasd7363752018-09-18 14:35:15 -04008692 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008693 public PendingIntent getDisplayIntent() {
8694 return mDisplayIntent;
8695 }
8696
8697 /**
8698 * Add an additional page of content to display with this notification. The current
8699 * notification forms the first page, and pages added using this function form
8700 * subsequent pages. This field can be used to separate a notification into multiple
8701 * sections.
8702 *
8703 * @param page the notification to add as another page
8704 * @return this object for method chaining
8705 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04008706 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008707 */
Gus Prevasd7363752018-09-18 14:35:15 -04008708 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008709 public WearableExtender addPage(Notification page) {
8710 mPages.add(page);
8711 return this;
8712 }
8713
8714 /**
8715 * Add additional pages of content to display with this notification. The current
8716 * notification forms the first page, and pages added using this function form
8717 * subsequent pages. This field can be used to separate a notification into multiple
8718 * sections.
8719 *
8720 * @param pages a list of notifications
8721 * @return this object for method chaining
8722 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04008723 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008724 */
Gus Prevasd7363752018-09-18 14:35:15 -04008725 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008726 public WearableExtender addPages(List<Notification> pages) {
8727 mPages.addAll(pages);
8728 return this;
8729 }
8730
8731 /**
8732 * Clear all additional pages present on this builder.
8733 * @return this object for method chaining.
8734 * @see #addPage
Gus Prevasd7363752018-09-18 14:35:15 -04008735 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008736 */
Gus Prevasd7363752018-09-18 14:35:15 -04008737 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008738 public WearableExtender clearPages() {
8739 mPages.clear();
8740 return this;
8741 }
8742
8743 /**
8744 * Get the array of additional pages of content for displaying this notification. The
8745 * current notification forms the first page, and elements within this array form
8746 * subsequent pages. This field can be used to separate a notification into multiple
8747 * sections.
8748 * @return the pages for this notification
Gus Prevasd7363752018-09-18 14:35:15 -04008749 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008750 */
Gus Prevasd7363752018-09-18 14:35:15 -04008751 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008752 public List<Notification> getPages() {
8753 return mPages;
8754 }
8755
8756 /**
8757 * Set a background image to be displayed behind the notification content.
8758 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8759 * will work with any notification style.
8760 *
8761 * @param background the background bitmap
8762 * @return this object for method chaining
8763 * @see android.app.Notification.WearableExtender#getBackground
Gus Prevasd7363752018-09-18 14:35:15 -04008764 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008765 */
Gus Prevasd7363752018-09-18 14:35:15 -04008766 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008767 public WearableExtender setBackground(Bitmap background) {
8768 mBackground = background;
8769 return this;
8770 }
8771
8772 /**
8773 * Get a background image to be displayed behind the notification content.
8774 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8775 * will work with any notification style.
8776 *
8777 * @return the background image
8778 * @see android.app.Notification.WearableExtender#setBackground
Gus Prevasd7363752018-09-18 14:35:15 -04008779 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008780 */
Gus Prevasd7363752018-09-18 14:35:15 -04008781 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008782 public Bitmap getBackground() {
8783 return mBackground;
8784 }
8785
8786 /**
8787 * Set an icon that goes with the content of this notification.
8788 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008789 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008790 public WearableExtender setContentIcon(int icon) {
8791 mContentIcon = icon;
8792 return this;
8793 }
8794
8795 /**
8796 * Get an icon that goes with the content of this notification.
8797 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008798 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008799 public int getContentIcon() {
8800 return mContentIcon;
8801 }
8802
8803 /**
8804 * Set the gravity that the content icon should have within the notification display.
8805 * Supported values include {@link android.view.Gravity#START} and
8806 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8807 * @see #setContentIcon
8808 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008809 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008810 public WearableExtender setContentIconGravity(int contentIconGravity) {
8811 mContentIconGravity = contentIconGravity;
8812 return this;
8813 }
8814
8815 /**
8816 * Get the gravity that the content icon should have within the notification display.
8817 * Supported values include {@link android.view.Gravity#START} and
8818 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8819 * @see #getContentIcon
8820 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008821 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008822 public int getContentIconGravity() {
8823 return mContentIconGravity;
8824 }
8825
8826 /**
Gus Prevasd7363752018-09-18 14:35:15 -04008827 * Set an action from this notification's actions as the primary action. If the action has a
8828 * {@link RemoteInput} associated with it, shortcuts to the options for that input are shown
8829 * directly on the notification.
Griff Hazen14f57992014-05-26 09:07:14 -07008830 *
Gus Prevasd7363752018-09-18 14:35:15 -04008831 * @param actionIndex The index of the primary action.
Griff Hazen14f57992014-05-26 09:07:14 -07008832 * If wearable actions were added to the main notification, this index
8833 * will apply to that list, otherwise it will apply to the regular
8834 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07008835 */
8836 public WearableExtender setContentAction(int actionIndex) {
8837 mContentActionIndex = actionIndex;
8838 return this;
8839 }
8840
8841 /**
Gus Prevasd7363752018-09-18 14:35:15 -04008842 * Get the index of the notification action, if any, that was specified as the primary
8843 * action.
Griff Hazen14f57992014-05-26 09:07:14 -07008844 *
8845 * <p>If wearable specific actions were added to the main notification, this index will
8846 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07008847 *
8848 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07008849 */
8850 public int getContentAction() {
8851 return mContentActionIndex;
8852 }
8853
8854 /**
8855 * Set the gravity that this notification should have within the available viewport space.
8856 * Supported values include {@link android.view.Gravity#TOP},
8857 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8858 * The default value is {@link android.view.Gravity#BOTTOM}.
8859 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008860 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008861 public WearableExtender setGravity(int gravity) {
8862 mGravity = gravity;
8863 return this;
8864 }
8865
8866 /**
8867 * Get the gravity that this notification should have within the available viewport space.
8868 * Supported values include {@link android.view.Gravity#TOP},
8869 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8870 * The default value is {@link android.view.Gravity#BOTTOM}.
8871 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008872 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008873 public int getGravity() {
8874 return mGravity;
8875 }
8876
8877 /**
8878 * Set the custom size preset for the display of this notification out of the available
8879 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
8880 * {@link #SIZE_LARGE}.
8881 * <p>Some custom size presets are only applicable for custom display notifications created
8882 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
8883 * documentation for the preset in question. See also
8884 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
8885 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008886 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008887 public WearableExtender setCustomSizePreset(int sizePreset) {
8888 mCustomSizePreset = sizePreset;
8889 return this;
8890 }
8891
8892 /**
8893 * Get the custom size preset for the display of this notification out of the available
8894 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
8895 * {@link #SIZE_LARGE}.
8896 * <p>Some custom size presets are only applicable for custom display notifications created
8897 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
8898 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
8899 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008900 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008901 public int getCustomSizePreset() {
8902 return mCustomSizePreset;
8903 }
8904
8905 /**
8906 * Set the custom height in pixels for the display of this notification's content.
8907 * <p>This option is only available for custom display notifications created
8908 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
8909 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
8910 * {@link #getCustomContentHeight}.
8911 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008912 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008913 public WearableExtender setCustomContentHeight(int height) {
8914 mCustomContentHeight = height;
8915 return this;
8916 }
8917
8918 /**
8919 * Get the custom height in pixels for the display of this notification's content.
8920 * <p>This option is only available for custom display notifications created
8921 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
8922 * {@link #setCustomContentHeight}.
8923 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008924 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008925 public int getCustomContentHeight() {
8926 return mCustomContentHeight;
8927 }
8928
8929 /**
8930 * Set whether the scrolling position for the contents of this notification should start
8931 * at the bottom of the contents instead of the top when the contents are too long to
8932 * display within the screen. Default is false (start scroll at the top).
8933 */
8934 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
8935 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
8936 return this;
8937 }
8938
8939 /**
8940 * Get whether the scrolling position for the contents of this notification should start
8941 * at the bottom of the contents instead of the top when the contents are too long to
8942 * display within the screen. Default is false (start scroll at the top).
8943 */
8944 public boolean getStartScrollBottom() {
8945 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
8946 }
8947
8948 /**
8949 * Set whether the content intent is available when the wearable device is not connected
8950 * to a companion device. The user can still trigger this intent when the wearable device
8951 * is offline, but a visual hint will indicate that the content intent may not be available.
8952 * Defaults to true.
8953 */
8954 public WearableExtender setContentIntentAvailableOffline(
8955 boolean contentIntentAvailableOffline) {
8956 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
8957 return this;
8958 }
8959
8960 /**
8961 * Get whether the content intent is available when the wearable device is not connected
8962 * to a companion device. The user can still trigger this intent when the wearable device
8963 * is offline, but a visual hint will indicate that the content intent may not be available.
8964 * Defaults to true.
8965 */
8966 public boolean getContentIntentAvailableOffline() {
8967 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
8968 }
8969
8970 /**
8971 * Set a hint that this notification's icon should not be displayed.
8972 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
8973 * @return this object for method chaining
8974 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008975 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008976 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
8977 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
8978 return this;
8979 }
8980
8981 /**
8982 * Get a hint that this notification's icon should not be displayed.
8983 * @return {@code true} if this icon should not be displayed, false otherwise.
8984 * The default value is {@code false} if this was never set.
8985 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008986 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008987 public boolean getHintHideIcon() {
8988 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
8989 }
8990
8991 /**
8992 * Set a visual hint that only the background image of this notification should be
8993 * displayed, and other semantic content should be hidden. This hint is only applicable
8994 * to sub-pages added using {@link #addPage}.
8995 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008996 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008997 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
8998 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
8999 return this;
9000 }
9001
9002 /**
9003 * Get a visual hint that only the background image of this notification should be
9004 * displayed, and other semantic content should be hidden. This hint is only applicable
9005 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
9006 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009007 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009008 public boolean getHintShowBackgroundOnly() {
9009 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
9010 }
9011
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009012 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009013 * Set a hint that this notification's background should not be clipped if possible,
9014 * and should instead be resized to fully display on the screen, retaining the aspect
9015 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009016 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
9017 * @return this object for method chaining
9018 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009019 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009020 public WearableExtender setHintAvoidBackgroundClipping(
9021 boolean hintAvoidBackgroundClipping) {
9022 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
9023 return this;
9024 }
9025
9026 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009027 * Get a hint that this notification's background should not be clipped if possible,
9028 * and should instead be resized to fully display on the screen, retaining the aspect
9029 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009030 * @return {@code true} if it's ok if the background is clipped on the screen, false
9031 * otherwise. The default value is {@code false} if this was never set.
9032 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009033 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009034 public boolean getHintAvoidBackgroundClipping() {
9035 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
9036 }
9037
9038 /**
9039 * Set a hint that the screen should remain on for at least this duration when
9040 * this notification is displayed on the screen.
9041 * @param timeout The requested screen timeout in milliseconds. Can also be either
9042 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9043 * @return this object for method chaining
9044 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009045 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009046 public WearableExtender setHintScreenTimeout(int timeout) {
9047 mHintScreenTimeout = timeout;
9048 return this;
9049 }
9050
9051 /**
9052 * Get the duration, in milliseconds, that the screen should remain on for
9053 * when this notification is displayed.
9054 * @return the duration in milliseconds if > 0, or either one of the sentinel values
9055 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9056 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009057 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009058 public int getHintScreenTimeout() {
9059 return mHintScreenTimeout;
9060 }
9061
Alex Hills9ab3a232016-04-05 14:54:56 -04009062 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04009063 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
9064 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9065 * qr codes, as well as other simple black-and-white tickets.
9066 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
9067 * @return this object for method chaining
Gus Prevasd7363752018-09-18 14:35:15 -04009068 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009069 */
Gus Prevasd7363752018-09-18 14:35:15 -04009070 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009071 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
9072 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
9073 return this;
9074 }
9075
9076 /**
9077 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
9078 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9079 * qr codes, as well as other simple black-and-white tickets.
9080 * @return {@code true} if it should be displayed in ambient, false otherwise
9081 * otherwise. The default value is {@code false} if this was never set.
Gus Prevasd7363752018-09-18 14:35:15 -04009082 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009083 */
Gus Prevasd7363752018-09-18 14:35:15 -04009084 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009085 public boolean getHintAmbientBigPicture() {
9086 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
9087 }
9088
9089 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04009090 * Set a hint that this notification's content intent will launch an {@link Activity}
9091 * directly, telling the platform that it can generate the appropriate transitions.
9092 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
9093 * an activity and transitions should be generated, false otherwise.
9094 * @return this object for method chaining
9095 */
9096 public WearableExtender setHintContentIntentLaunchesActivity(
9097 boolean hintContentIntentLaunchesActivity) {
9098 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
9099 return this;
9100 }
9101
9102 /**
9103 * Get a hint that this notification's content intent will launch an {@link Activity}
9104 * directly, telling the platform that it can generate the appropriate transitions
9105 * @return {@code true} if the content intent will launch an activity and transitions should
9106 * be generated, false otherwise. The default value is {@code false} if this was never set.
9107 */
9108 public boolean getHintContentIntentLaunchesActivity() {
9109 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
9110 }
9111
Nadia Benbernou948627e2016-04-14 14:41:08 -04009112 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009113 * Sets the dismissal id for this notification. If a notification is posted with a
9114 * dismissal id, then when that notification is canceled, notifications on other wearables
9115 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04009116 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009117 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04009118 * @param dismissalId the dismissal id of the notification.
9119 * @return this object for method chaining
9120 */
9121 public WearableExtender setDismissalId(String dismissalId) {
9122 mDismissalId = dismissalId;
9123 return this;
9124 }
9125
9126 /**
9127 * Returns the dismissal id of the notification.
9128 * @return the dismissal id of the notification or null if it has not been set.
9129 */
9130 public String getDismissalId() {
9131 return mDismissalId;
9132 }
9133
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009134 /**
9135 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
9136 * posted from a phone to provide finer-grained control on what notifications are bridged
9137 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
9138 * Features to Notifications</a> for more information.
9139 * @param bridgeTag the bridge tag of the notification.
9140 * @return this object for method chaining
9141 */
9142 public WearableExtender setBridgeTag(String bridgeTag) {
9143 mBridgeTag = bridgeTag;
9144 return this;
9145 }
9146
9147 /**
9148 * Returns the bridge tag of the notification.
9149 * @return the bridge tag or null if not present.
9150 */
9151 public String getBridgeTag() {
9152 return mBridgeTag;
9153 }
9154
Griff Hazen61a9e862014-05-22 16:05:19 -07009155 private void setFlag(int mask, boolean value) {
9156 if (value) {
9157 mFlags |= mask;
9158 } else {
9159 mFlags &= ~mask;
9160 }
9161 }
9162 }
9163
9164 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009165 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
9166 * with car extensions:
9167 *
9168 * <ol>
9169 * <li>Create an {@link Notification.Builder}, setting any desired
9170 * properties.
9171 * <li>Create a {@link CarExtender}.
9172 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
9173 * {@link CarExtender}.
9174 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9175 * to apply the extensions to a notification.
9176 * </ol>
9177 *
9178 * <pre class="prettyprint">
9179 * Notification notification = new Notification.Builder(context)
9180 * ...
9181 * .extend(new CarExtender()
9182 * .set*(...))
9183 * .build();
9184 * </pre>
9185 *
9186 * <p>Car extensions can be accessed on an existing notification by using the
9187 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
9188 * to access values.
9189 */
9190 public static final class CarExtender implements Extender {
9191 private static final String TAG = "CarExtender";
9192
9193 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
9194 private static final String EXTRA_LARGE_ICON = "large_icon";
9195 private static final String EXTRA_CONVERSATION = "car_conversation";
9196 private static final String EXTRA_COLOR = "app_color";
9197
9198 private Bitmap mLargeIcon;
9199 private UnreadConversation mUnreadConversation;
9200 private int mColor = Notification.COLOR_DEFAULT;
9201
9202 /**
9203 * Create a {@link CarExtender} with default options.
9204 */
9205 public CarExtender() {
9206 }
9207
9208 /**
9209 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
9210 *
9211 * @param notif The notification from which to copy options.
9212 */
9213 public CarExtender(Notification notif) {
9214 Bundle carBundle = notif.extras == null ?
9215 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
9216 if (carBundle != null) {
9217 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
9218 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
9219
9220 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
9221 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
9222 }
9223 }
9224
9225 /**
9226 * Apply car extensions to a notification that is being built. This is typically called by
9227 * the {@link Notification.Builder#extend(Notification.Extender)}
9228 * method of {@link Notification.Builder}.
9229 */
9230 @Override
9231 public Notification.Builder extend(Notification.Builder builder) {
9232 Bundle carExtensions = new Bundle();
9233
9234 if (mLargeIcon != null) {
9235 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
9236 }
9237 if (mColor != Notification.COLOR_DEFAULT) {
9238 carExtensions.putInt(EXTRA_COLOR, mColor);
9239 }
9240
9241 if (mUnreadConversation != null) {
9242 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
9243 carExtensions.putBundle(EXTRA_CONVERSATION, b);
9244 }
9245
9246 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
9247 return builder;
9248 }
9249
9250 /**
9251 * Sets the accent color to use when Android Auto presents the notification.
9252 *
9253 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
9254 * to accent the displayed notification. However, not all colors are acceptable in an
9255 * automotive setting. This method can be used to override the color provided in the
9256 * notification in such a situation.
9257 */
Tor Norbye80756e32015-03-02 09:39:27 -08009258 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009259 mColor = color;
9260 return this;
9261 }
9262
9263 /**
9264 * Gets the accent color.
9265 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04009266 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009267 */
Tor Norbye80756e32015-03-02 09:39:27 -08009268 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009269 public int getColor() {
9270 return mColor;
9271 }
9272
9273 /**
9274 * Sets the large icon of the car notification.
9275 *
9276 * If no large icon is set in the extender, Android Auto will display the icon
9277 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
9278 *
9279 * @param largeIcon The large icon to use in the car notification.
9280 * @return This object for method chaining.
9281 */
9282 public CarExtender setLargeIcon(Bitmap largeIcon) {
9283 mLargeIcon = largeIcon;
9284 return this;
9285 }
9286
9287 /**
9288 * Gets the large icon used in this car notification, or null if no icon has been set.
9289 *
9290 * @return The large icon for the car notification.
9291 * @see CarExtender#setLargeIcon
9292 */
9293 public Bitmap getLargeIcon() {
9294 return mLargeIcon;
9295 }
9296
9297 /**
9298 * Sets the unread conversation in a message notification.
9299 *
9300 * @param unreadConversation The unread part of the conversation this notification conveys.
9301 * @return This object for method chaining.
9302 */
9303 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
9304 mUnreadConversation = unreadConversation;
9305 return this;
9306 }
9307
9308 /**
9309 * Returns the unread conversation conveyed by this notification.
9310 * @see #setUnreadConversation(UnreadConversation)
9311 */
9312 public UnreadConversation getUnreadConversation() {
9313 return mUnreadConversation;
9314 }
9315
9316 /**
9317 * A class which holds the unread messages from a conversation.
9318 */
9319 public static class UnreadConversation {
9320 private static final String KEY_AUTHOR = "author";
9321 private static final String KEY_TEXT = "text";
9322 private static final String KEY_MESSAGES = "messages";
9323 private static final String KEY_REMOTE_INPUT = "remote_input";
9324 private static final String KEY_ON_REPLY = "on_reply";
9325 private static final String KEY_ON_READ = "on_read";
9326 private static final String KEY_PARTICIPANTS = "participants";
9327 private static final String KEY_TIMESTAMP = "timestamp";
9328
9329 private final String[] mMessages;
9330 private final RemoteInput mRemoteInput;
9331 private final PendingIntent mReplyPendingIntent;
9332 private final PendingIntent mReadPendingIntent;
9333 private final String[] mParticipants;
9334 private final long mLatestTimestamp;
9335
9336 UnreadConversation(String[] messages, RemoteInput remoteInput,
9337 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
9338 String[] participants, long latestTimestamp) {
9339 mMessages = messages;
9340 mRemoteInput = remoteInput;
9341 mReadPendingIntent = readPendingIntent;
9342 mReplyPendingIntent = replyPendingIntent;
9343 mParticipants = participants;
9344 mLatestTimestamp = latestTimestamp;
9345 }
9346
9347 /**
9348 * Gets the list of messages conveyed by this notification.
9349 */
9350 public String[] getMessages() {
9351 return mMessages;
9352 }
9353
9354 /**
9355 * Gets the remote input that will be used to convey the response to a message list, or
9356 * null if no such remote input exists.
9357 */
9358 public RemoteInput getRemoteInput() {
9359 return mRemoteInput;
9360 }
9361
9362 /**
9363 * Gets the pending intent that will be triggered when the user replies to this
9364 * notification.
9365 */
9366 public PendingIntent getReplyPendingIntent() {
9367 return mReplyPendingIntent;
9368 }
9369
9370 /**
9371 * Gets the pending intent that Android Auto will send after it reads aloud all messages
9372 * in this object's message list.
9373 */
9374 public PendingIntent getReadPendingIntent() {
9375 return mReadPendingIntent;
9376 }
9377
9378 /**
9379 * Gets the participants in the conversation.
9380 */
9381 public String[] getParticipants() {
9382 return mParticipants;
9383 }
9384
9385 /**
9386 * Gets the firs participant in the conversation.
9387 */
9388 public String getParticipant() {
9389 return mParticipants.length > 0 ? mParticipants[0] : null;
9390 }
9391
9392 /**
9393 * Gets the timestamp of the conversation.
9394 */
9395 public long getLatestTimestamp() {
9396 return mLatestTimestamp;
9397 }
9398
9399 Bundle getBundleForUnreadConversation() {
9400 Bundle b = new Bundle();
9401 String author = null;
9402 if (mParticipants != null && mParticipants.length > 1) {
9403 author = mParticipants[0];
9404 }
9405 Parcelable[] messages = new Parcelable[mMessages.length];
9406 for (int i = 0; i < messages.length; i++) {
9407 Bundle m = new Bundle();
9408 m.putString(KEY_TEXT, mMessages[i]);
9409 m.putString(KEY_AUTHOR, author);
9410 messages[i] = m;
9411 }
9412 b.putParcelableArray(KEY_MESSAGES, messages);
9413 if (mRemoteInput != null) {
9414 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
9415 }
9416 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
9417 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
9418 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
9419 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
9420 return b;
9421 }
9422
9423 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
9424 if (b == null) {
9425 return null;
9426 }
9427 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
9428 String[] messages = null;
9429 if (parcelableMessages != null) {
9430 String[] tmp = new String[parcelableMessages.length];
9431 boolean success = true;
9432 for (int i = 0; i < tmp.length; i++) {
9433 if (!(parcelableMessages[i] instanceof Bundle)) {
9434 success = false;
9435 break;
9436 }
9437 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
9438 if (tmp[i] == null) {
9439 success = false;
9440 break;
9441 }
9442 }
9443 if (success) {
9444 messages = tmp;
9445 } else {
9446 return null;
9447 }
9448 }
9449
9450 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
9451 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
9452
9453 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
9454
9455 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
9456 if (participants == null || participants.length != 1) {
9457 return null;
9458 }
9459
9460 return new UnreadConversation(messages,
9461 remoteInput,
9462 onReply,
9463 onRead,
9464 participants, b.getLong(KEY_TIMESTAMP));
9465 }
9466 };
9467
9468 /**
9469 * Builder class for {@link CarExtender.UnreadConversation} objects.
9470 */
9471 public static class Builder {
9472 private final List<String> mMessages = new ArrayList<String>();
9473 private final String mParticipant;
9474 private RemoteInput mRemoteInput;
9475 private PendingIntent mReadPendingIntent;
9476 private PendingIntent mReplyPendingIntent;
9477 private long mLatestTimestamp;
9478
9479 /**
9480 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
9481 *
9482 * @param name The name of the other participant in the conversation.
9483 */
9484 public Builder(String name) {
9485 mParticipant = name;
9486 }
9487
9488 /**
9489 * Appends a new unread message to the list of messages for this conversation.
9490 *
9491 * The messages should be added from oldest to newest.
9492 *
9493 * @param message The text of the new unread message.
9494 * @return This object for method chaining.
9495 */
9496 public Builder addMessage(String message) {
9497 mMessages.add(message);
9498 return this;
9499 }
9500
9501 /**
9502 * Sets the pending intent and remote input which will convey the reply to this
9503 * notification.
9504 *
9505 * @param pendingIntent The pending intent which will be triggered on a reply.
9506 * @param remoteInput The remote input parcelable which will carry the reply.
9507 * @return This object for method chaining.
9508 *
9509 * @see CarExtender.UnreadConversation#getRemoteInput
9510 * @see CarExtender.UnreadConversation#getReplyPendingIntent
9511 */
9512 public Builder setReplyAction(
9513 PendingIntent pendingIntent, RemoteInput remoteInput) {
9514 mRemoteInput = remoteInput;
9515 mReplyPendingIntent = pendingIntent;
9516
9517 return this;
9518 }
9519
9520 /**
9521 * Sets the pending intent that will be sent once the messages in this notification
9522 * are read.
9523 *
9524 * @param pendingIntent The pending intent to use.
9525 * @return This object for method chaining.
9526 */
9527 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
9528 mReadPendingIntent = pendingIntent;
9529 return this;
9530 }
9531
9532 /**
9533 * Sets the timestamp of the most recent message in an unread conversation.
9534 *
9535 * If a messaging notification has been posted by your application and has not
9536 * yet been cancelled, posting a later notification with the same id and tag
9537 * but without a newer timestamp may result in Android Auto not displaying a
9538 * heads up notification for the later notification.
9539 *
9540 * @param timestamp The timestamp of the most recent message in the conversation.
9541 * @return This object for method chaining.
9542 */
9543 public Builder setLatestTimestamp(long timestamp) {
9544 mLatestTimestamp = timestamp;
9545 return this;
9546 }
9547
9548 /**
9549 * Builds a new unread conversation object.
9550 *
9551 * @return The new unread conversation object.
9552 */
9553 public UnreadConversation build() {
9554 String[] messages = mMessages.toArray(new String[mMessages.size()]);
9555 String[] participants = { mParticipant };
9556 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
9557 mReadPendingIntent, participants, mLatestTimestamp);
9558 }
9559 }
9560 }
9561
9562 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009563 * <p>Helper class to add Android TV extensions to notifications. To create a notification
9564 * with a TV extension:
9565 *
9566 * <ol>
9567 * <li>Create an {@link Notification.Builder}, setting any desired properties.
9568 * <li>Create a {@link TvExtender}.
9569 * <li>Set TV-specific properties using the {@code set} methods of
9570 * {@link TvExtender}.
9571 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9572 * to apply the extension to a notification.
9573 * </ol>
9574 *
9575 * <pre class="prettyprint">
9576 * Notification notification = new Notification.Builder(context)
9577 * ...
9578 * .extend(new TvExtender()
9579 * .set*(...))
9580 * .build();
9581 * </pre>
9582 *
9583 * <p>TV extensions can be accessed on an existing notification by using the
9584 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
9585 * to access values.
9586 *
9587 * @hide
9588 */
9589 @SystemApi
9590 public static final class TvExtender implements Extender {
9591 private static final String TAG = "TvExtender";
9592
9593 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
9594 private static final String EXTRA_FLAGS = "flags";
9595 private static final String EXTRA_CONTENT_INTENT = "content_intent";
9596 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009597 private static final String EXTRA_CHANNEL_ID = "channel_id";
Rhiannon Malia1a083932018-01-24 15:02:30 -08009598 private static final String EXTRA_SUPPRESS_SHOW_OVER_APPS = "suppressShowOverApps";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009599
9600 // Flags bitwise-ored to mFlags
9601 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
9602
9603 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009604 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009605 private PendingIntent mContentIntent;
9606 private PendingIntent mDeleteIntent;
Rhiannon Malia1a083932018-01-24 15:02:30 -08009607 private boolean mSuppressShowOverApps;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009608
9609 /**
9610 * Create a {@link TvExtender} with default options.
9611 */
9612 public TvExtender() {
9613 mFlags = FLAG_AVAILABLE_ON_TV;
9614 }
9615
9616 /**
9617 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
9618 *
9619 * @param notif The notification from which to copy options.
9620 */
9621 public TvExtender(Notification notif) {
9622 Bundle bundle = notif.extras == null ?
9623 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
9624 if (bundle != null) {
9625 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009626 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Rhiannon Malia1a083932018-01-24 15:02:30 -08009627 mSuppressShowOverApps = bundle.getBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009628 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
9629 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
9630 }
9631 }
9632
9633 /**
9634 * Apply a TV extension to a notification that is being built. This is typically called by
9635 * the {@link Notification.Builder#extend(Notification.Extender)}
9636 * method of {@link Notification.Builder}.
9637 */
9638 @Override
9639 public Notification.Builder extend(Notification.Builder builder) {
9640 Bundle bundle = new Bundle();
9641
9642 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009643 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Rhiannon Malia1a083932018-01-24 15:02:30 -08009644 bundle.putBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS, mSuppressShowOverApps);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009645 if (mContentIntent != null) {
9646 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
9647 }
9648
9649 if (mDeleteIntent != null) {
9650 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
9651 }
9652
9653 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
9654 return builder;
9655 }
9656
9657 /**
9658 * Returns true if this notification should be shown on TV. This method return true
9659 * if the notification was extended with a TvExtender.
9660 */
9661 public boolean isAvailableOnTv() {
9662 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
9663 }
9664
9665 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009666 * Specifies the channel the notification should be delivered on when shown on TV.
9667 * It can be different from the channel that the notification is delivered to when
9668 * posting on a non-TV device.
9669 */
9670 public TvExtender setChannel(String channelId) {
9671 mChannelId = channelId;
9672 return this;
9673 }
9674
9675 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04009676 * Specifies the channel the notification should be delivered on when shown on TV.
9677 * It can be different from the channel that the notification is delivered to when
9678 * posting on a non-TV device.
9679 */
9680 public TvExtender setChannelId(String channelId) {
9681 mChannelId = channelId;
9682 return this;
9683 }
9684
Jeff Sharkey000ce802017-04-29 13:13:27 -06009685 /** @removed */
9686 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009687 public String getChannel() {
9688 return mChannelId;
9689 }
9690
9691 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04009692 * Returns the id of the channel this notification posts to on TV.
9693 */
9694 public String getChannelId() {
9695 return mChannelId;
9696 }
9697
9698 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009699 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
9700 * If provided, it is used instead of the content intent specified
9701 * at the level of Notification.
9702 */
9703 public TvExtender setContentIntent(PendingIntent intent) {
9704 mContentIntent = intent;
9705 return this;
9706 }
9707
9708 /**
9709 * Returns the TV-specific content intent. If this method returns null, the
9710 * main content intent on the notification should be used.
9711 *
9712 * @see {@link Notification#contentIntent}
9713 */
9714 public PendingIntent getContentIntent() {
9715 return mContentIntent;
9716 }
9717
9718 /**
9719 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
9720 * by the user on TV. If provided, it is used instead of the delete intent specified
9721 * at the level of Notification.
9722 */
9723 public TvExtender setDeleteIntent(PendingIntent intent) {
9724 mDeleteIntent = intent;
9725 return this;
9726 }
9727
9728 /**
9729 * Returns the TV-specific delete intent. If this method returns null, the
9730 * main delete intent on the notification should be used.
9731 *
9732 * @see {@link Notification#deleteIntent}
9733 */
9734 public PendingIntent getDeleteIntent() {
9735 return mDeleteIntent;
9736 }
Rhiannon Malia1a083932018-01-24 15:02:30 -08009737
9738 /**
9739 * Specifies whether this notification should suppress showing a message over top of apps
9740 * outside of the launcher.
9741 */
9742 public TvExtender setSuppressShowOverApps(boolean suppress) {
9743 mSuppressShowOverApps = suppress;
9744 return this;
9745 }
9746
9747 /**
9748 * Returns true if this notification should not show messages over top of apps
9749 * outside of the launcher.
9750 */
9751 public boolean getSuppressShowOverApps() {
9752 return mSuppressShowOverApps;
9753 }
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009754 }
9755
9756 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07009757 * Get an array of Notification objects from a parcelable array bundle field.
9758 * Update the bundle to have a typed array so fetches in the future don't need
9759 * to do an array copy.
9760 */
9761 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
9762 Parcelable[] array = bundle.getParcelableArray(key);
9763 if (array instanceof Notification[] || array == null) {
9764 return (Notification[]) array;
9765 }
9766 Notification[] typedArray = Arrays.copyOf(array, array.length,
9767 Notification[].class);
9768 bundle.putParcelableArray(key, typedArray);
9769 return typedArray;
9770 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02009771
9772 private static class BuilderRemoteViews extends RemoteViews {
9773 public BuilderRemoteViews(Parcel parcel) {
9774 super(parcel);
9775 }
9776
Kenny Guy77320062014-08-27 21:37:15 +01009777 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
9778 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02009779 }
9780
9781 @Override
9782 public BuilderRemoteViews clone() {
9783 Parcel p = Parcel.obtain();
9784 writeToParcel(p, 0);
9785 p.setDataPosition(0);
9786 BuilderRemoteViews brv = new BuilderRemoteViews(p);
9787 p.recycle();
9788 return brv;
9789 }
9790 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08009791
Selim Cinek384804b2018-04-18 14:31:07 +08009792 /**
9793 * A result object where information about the template that was created is saved.
9794 */
9795 private static class TemplateBindResult {
Selim Cinek1c72fa02018-04-23 18:00:54 +08009796 int mIconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +08009797
9798 /**
Selim Cinek1c72fa02018-04-23 18:00:54 +08009799 * Get the margin end that needs to be added to any fields that may overlap
Selim Cinek384804b2018-04-18 14:31:07 +08009800 * with the right actions.
9801 */
Selim Cinek1c72fa02018-04-23 18:00:54 +08009802 public int getIconMarginEnd() {
9803 return mIconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +08009804 }
9805
Selim Cinek1c72fa02018-04-23 18:00:54 +08009806 public void setIconMarginEnd(int iconMarginEnd) {
9807 this.mIconMarginEnd = iconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +08009808 }
9809 }
9810
Adrian Roos70d7aa32017-01-11 15:39:06 -08009811 private static class StandardTemplateParams {
9812 boolean hasProgress = true;
9813 boolean ambient = false;
9814 CharSequence title;
9815 CharSequence text;
Selim Cinekafeed292017-12-12 17:32:44 -08009816 CharSequence headerTextSecondary;
Selim Cinekbee4e072018-05-21 22:06:43 -07009817 int maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek88188f22017-09-19 16:46:56 -07009818 boolean hideLargeIcon;
Selim Cinek384804b2018-04-18 14:31:07 +08009819 boolean hideReplyIcon;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009820
9821 final StandardTemplateParams reset() {
9822 hasProgress = true;
9823 ambient = false;
9824 title = null;
9825 text = null;
Selim Cinekafeed292017-12-12 17:32:44 -08009826 headerTextSecondary = null;
Selim Cinekbee4e072018-05-21 22:06:43 -07009827 maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009828 return this;
9829 }
9830
9831 final StandardTemplateParams hasProgress(boolean hasProgress) {
9832 this.hasProgress = hasProgress;
9833 return this;
9834 }
9835
9836 final StandardTemplateParams title(CharSequence title) {
9837 this.title = title;
9838 return this;
9839 }
9840
9841 final StandardTemplateParams text(CharSequence text) {
9842 this.text = text;
9843 return this;
9844 }
9845
Selim Cinekafeed292017-12-12 17:32:44 -08009846 final StandardTemplateParams headerTextSecondary(CharSequence text) {
9847 this.headerTextSecondary = text;
9848 return this;
9849 }
9850
Selim Cinek384804b2018-04-18 14:31:07 +08009851 final StandardTemplateParams hideLargeIcon(boolean hideLargeIcon) {
9852 this.hideLargeIcon = hideLargeIcon;
Selim Cinek88188f22017-09-19 16:46:56 -07009853 return this;
9854 }
9855
Selim Cinek384804b2018-04-18 14:31:07 +08009856 final StandardTemplateParams hideReplyIcon(boolean hideReplyIcon) {
9857 this.hideReplyIcon = hideReplyIcon;
Selim Cinek88188f22017-09-19 16:46:56 -07009858 return this;
9859 }
9860
Adrian Roos70d7aa32017-01-11 15:39:06 -08009861 final StandardTemplateParams ambient(boolean ambient) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08009862 Preconditions.checkState(title == null && text == null, "must set ambient before text");
Adrian Roos70d7aa32017-01-11 15:39:06 -08009863 this.ambient = ambient;
9864 return this;
9865 }
9866
9867 final StandardTemplateParams fillTextsFrom(Builder b) {
9868 Bundle extras = b.mN.extras;
Lucas Dupin06c5e642017-09-13 16:34:58 -07009869 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE), ambient);
9870
9871 // Big text notifications should contain their content when viewed in ambient mode.
9872 CharSequence text = extras.getCharSequence(EXTRA_BIG_TEXT);
9873 if (!ambient || TextUtils.isEmpty(text)) {
9874 text = extras.getCharSequence(EXTRA_TEXT);
9875 }
9876 this.text = b.processLegacyText(text, ambient);
Adrian Roos70d7aa32017-01-11 15:39:06 -08009877 return this;
9878 }
Selim Cinekbee4e072018-05-21 22:06:43 -07009879
9880 /**
9881 * Set the maximum lines of remote input history lines allowed.
9882 * @param maxRemoteInputHistory The number of lines.
9883 * @return The builder for method chaining.
9884 */
9885 public StandardTemplateParams setMaxRemoteInputHistory(int maxRemoteInputHistory) {
9886 this.maxRemoteInputHistory = maxRemoteInputHistory;
9887 return this;
9888 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08009889 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009890}