blob: 6d464fb190c179d412f084fa3150f266e3636ac4 [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);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004460 mN.mUsesStandardHeader = false;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004461 }
4462
Selim Cinek384804b2018-04-18 14:31:07 +08004463 private RemoteViews applyStandardTemplate(int resId, TemplateBindResult result) {
4464 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this),
4465 result);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004466 }
4467
4468 /**
4469 * @param hasProgress whether the progress bar should be shown and set
Selim Cinek384804b2018-04-18 14:31:07 +08004470 * @param result
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004471 */
Selim Cinek384804b2018-04-18 14:31:07 +08004472 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress,
4473 TemplateBindResult result) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004474 return applyStandardTemplate(resId, mParams.reset().hasProgress(hasProgress)
Selim Cinek384804b2018-04-18 14:31:07 +08004475 .fillTextsFrom(this), result);
Adrian Roosc1a80b02016-04-05 14:54:55 -07004476 }
4477
Selim Cinek384804b2018-04-18 14:31:07 +08004478 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p,
4479 TemplateBindResult result) {
Kenny Guy77320062014-08-27 21:37:15 +01004480 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04004481
Christoph Studerfe718432014-09-01 18:21:18 +02004482 resetStandardTemplate(contentView);
4483
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004484 final Bundle ex = mN.extras;
Selim Cinek7b9605b2017-01-19 17:36:00 -08004485 updateBackgroundColor(contentView);
Selim Cinekafeed292017-12-12 17:32:44 -08004486 bindNotificationHeader(contentView, p.ambient, p.headerTextSecondary);
Selim Cinek384804b2018-04-18 14:31:07 +08004487 bindLargeIconAndReply(contentView, p, result);
Adrian Roos70d7aa32017-01-11 15:39:06 -08004488 boolean showProgress = handleProgressBar(p.hasProgress, contentView, ex);
4489 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08004490 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004491 contentView.setTextViewText(R.id.title, processTextSpans(p.title));
Adrian Roos72171622017-01-27 10:32:06 -08004492 if (!p.ambient) {
4493 setTextViewColorPrimary(contentView, R.id.title);
4494 }
Selim Cinek954cc232016-05-20 13:29:23 -07004495 contentView.setViewLayoutWidth(R.id.title, showProgress
4496 ? ViewGroup.LayoutParams.WRAP_CONTENT
4497 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08004498 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08004499 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08004500 int textId = showProgress ? com.android.internal.R.id.text_line_1
4501 : com.android.internal.R.id.text;
Selim Cinek48f66b72017-08-18 16:17:51 -07004502 contentView.setTextViewText(textId, processTextSpans(p.text));
Adrian Roos72171622017-01-27 10:32:06 -08004503 if (!p.ambient) {
4504 setTextViewColorSecondary(contentView, textId);
4505 }
Selim Cinek41598732016-01-11 16:58:37 -08004506 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08004507 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08004508
Selim Cinek279fa862016-06-14 10:57:25 -07004509 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004510
Selim Cinek29603462015-11-17 19:04:39 -08004511 return contentView;
4512 }
4513
Selim Cinek48f66b72017-08-18 16:17:51 -07004514 private CharSequence processTextSpans(CharSequence text) {
Lucas Dupind3c99322018-09-27 10:22:29 -07004515 if (hasForegroundColor() || mInNightMode) {
Lucas Dupina291d192018-06-07 13:59:42 -07004516 return ContrastColorUtil.clearColorSpans(text);
Selim Cinek48f66b72017-08-18 16:17:51 -07004517 }
4518 return text;
4519 }
4520
Selim Cinek7b9605b2017-01-19 17:36:00 -08004521 private void setTextViewColorPrimary(RemoteViews contentView, int id) {
4522 ensureColors();
4523 contentView.setTextColor(id, mPrimaryTextColor);
4524 }
4525
Selim Cinek48f66b72017-08-18 16:17:51 -07004526 private boolean hasForegroundColor() {
4527 return mForegroundColor != COLOR_INVALID;
4528 }
4529
Selim Cinek389edcd2017-05-11 19:16:44 -07004530 /**
4531 * @return the primary text color
4532 * @hide
4533 */
4534 @VisibleForTesting
4535 public int getPrimaryTextColor() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004536 ensureColors();
4537 return mPrimaryTextColor;
4538 }
4539
Selim Cinek389edcd2017-05-11 19:16:44 -07004540 /**
4541 * @return the secondary text color
4542 * @hide
4543 */
4544 @VisibleForTesting
4545 public int getSecondaryTextColor() {
4546 ensureColors();
4547 return mSecondaryTextColor;
4548 }
4549
Selim Cinek7b9605b2017-01-19 17:36:00 -08004550 private void setTextViewColorSecondary(RemoteViews contentView, int id) {
4551 ensureColors();
4552 contentView.setTextColor(id, mSecondaryTextColor);
4553 }
4554
4555 private void ensureColors() {
4556 int backgroundColor = getBackgroundColor();
4557 if (mPrimaryTextColor == COLOR_INVALID
4558 || mSecondaryTextColor == COLOR_INVALID
Selim Cinek7b9605b2017-01-19 17:36:00 -08004559 || mTextColorsAreForBackground != backgroundColor) {
4560 mTextColorsAreForBackground = backgroundColor;
Selim Cinek48f66b72017-08-18 16:17:51 -07004561 if (!hasForegroundColor() || !isColorized()) {
Lucas Dupina291d192018-06-07 13:59:42 -07004562 mPrimaryTextColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004563 backgroundColor, mInNightMode);
Lucas Dupina291d192018-06-07 13:59:42 -07004564 mSecondaryTextColor = ContrastColorUtil.resolveSecondaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004565 backgroundColor, mInNightMode);
Selim Cinekc7f5a822018-03-20 19:32:06 -07004566 if (backgroundColor != COLOR_DEFAULT && isColorized()) {
Lucas Dupina291d192018-06-07 13:59:42 -07004567 mPrimaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004568 mPrimaryTextColor, backgroundColor, 4.5);
Lucas Dupina291d192018-06-07 13:59:42 -07004569 mSecondaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004570 mSecondaryTextColor, backgroundColor, 4.5);
4571 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07004572 } else {
Lucas Dupina291d192018-06-07 13:59:42 -07004573 double backLum = ContrastColorUtil.calculateLuminance(backgroundColor);
4574 double textLum = ContrastColorUtil.calculateLuminance(mForegroundColor);
4575 double contrast = ContrastColorUtil.calculateContrast(mForegroundColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004576 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07004577 // We only respect the given colors if worst case Black or White still has
4578 // contrast
4579 boolean backgroundLight = backLum > textLum
4580 && satisfiesTextContrast(backgroundColor, Color.BLACK)
4581 || backLum <= textLum
4582 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004583 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07004584 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004585 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004586 mForegroundColor,
4587 backgroundColor,
4588 true /* findFG */,
4589 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004590 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004591 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004592 } else {
4593 mSecondaryTextColor =
Lucas Dupina291d192018-06-07 13:59:42 -07004594 ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004595 mForegroundColor,
4596 backgroundColor,
4597 true /* findFG */,
4598 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004599 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004600 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004601 }
4602 } else {
4603 mPrimaryTextColor = mForegroundColor;
Lucas Dupina291d192018-06-07 13:59:42 -07004604 mSecondaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004605 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4606 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Lucas Dupina291d192018-06-07 13:59:42 -07004607 if (ContrastColorUtil.calculateContrast(mSecondaryTextColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004608 backgroundColor) < 4.5f) {
4609 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07004610 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004611 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004612 mSecondaryTextColor,
4613 backgroundColor,
4614 true /* findFG */,
4615 4.5f);
4616 } else {
4617 mSecondaryTextColor
Lucas Dupina291d192018-06-07 13:59:42 -07004618 = ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004619 mSecondaryTextColor,
4620 backgroundColor,
4621 true /* findFG */,
4622 4.5f);
4623 }
Lucas Dupina291d192018-06-07 13:59:42 -07004624 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004625 mSecondaryTextColor, backgroundLight
4626 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4627 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004628 }
4629 }
4630 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004631 }
4632 }
4633
4634 private void updateBackgroundColor(RemoteViews contentView) {
4635 if (isColorized()) {
4636 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
4637 getBackgroundColor());
4638 } else {
4639 // Clear it!
4640 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4641 0);
4642 }
4643 }
4644
Selim Cinek860b6da2015-12-16 19:02:19 -08004645 /**
4646 * @param remoteView the remote view to update the minheight in
4647 * @param hasMinHeight does it have a mimHeight
4648 * @hide
4649 */
4650 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4651 int minHeight = 0;
4652 if (hasMinHeight) {
4653 // we need to set the minHeight of the notification
4654 minHeight = mContext.getResources().getDimensionPixelSize(
4655 com.android.internal.R.dimen.notification_min_content_height);
4656 }
4657 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4658 }
4659
Selim Cinek29603462015-11-17 19:04:39 -08004660 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004661 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4662 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4663 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4664 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004665 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004666 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004667 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004668 contentView.setProgressBackgroundTintList(
4669 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4670 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08004671 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08004672 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004673 contentView.setProgressTintList(R.id.progress, colorStateList);
4674 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004675 }
Selim Cinek29603462015-11-17 19:04:39 -08004676 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004677 } else {
4678 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004679 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004680 }
Joe Onorato561d3852010-11-20 18:09:34 -08004681 }
4682
Selim Cinek384804b2018-04-18 14:31:07 +08004683 private void bindLargeIconAndReply(RemoteViews contentView, StandardTemplateParams p,
4684 TemplateBindResult result) {
4685 boolean largeIconShown = bindLargeIcon(contentView, p.hideLargeIcon || p.ambient);
Selim Cinek1c72fa02018-04-23 18:00:54 +08004686 boolean replyIconShown = bindReplyIcon(contentView, p.hideReplyIcon || p.ambient);
Selim Cinek384804b2018-04-18 14:31:07 +08004687 contentView.setViewVisibility(R.id.right_icon_container,
Selim Cinek1c72fa02018-04-23 18:00:54 +08004688 largeIconShown || replyIconShown ? View.VISIBLE : View.GONE);
4689 int marginEnd = calculateMarginEnd(largeIconShown, replyIconShown);
4690 contentView.setViewLayoutMarginEnd(R.id.line1, marginEnd);
4691 contentView.setViewLayoutMarginEnd(R.id.text, marginEnd);
4692 contentView.setViewLayoutMarginEnd(R.id.progress, marginEnd);
Selim Cinek384804b2018-04-18 14:31:07 +08004693 if (result != null) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08004694 result.setIconMarginEnd(marginEnd);
Selim Cinek384804b2018-04-18 14:31:07 +08004695 }
4696 }
4697
Selim Cinek1c72fa02018-04-23 18:00:54 +08004698 private int calculateMarginEnd(boolean largeIconShown, boolean replyIconShown) {
4699 int marginEnd = 0;
4700 int contentMargin = mContext.getResources().getDimensionPixelSize(
4701 R.dimen.notification_content_margin_end);
4702 int iconSize = mContext.getResources().getDimensionPixelSize(
4703 R.dimen.notification_right_icon_size);
4704 if (replyIconShown) {
4705 // The size of the reply icon
4706 marginEnd += iconSize;
4707
4708 int replyInset = mContext.getResources().getDimensionPixelSize(
4709 R.dimen.notification_reply_inset);
4710 // We're subtracting the inset of the reply icon to make sure it's
4711 // aligned nicely on the right, and remove it from the following padding
4712 marginEnd -= replyInset * 2;
4713 }
4714 if (largeIconShown) {
4715 // adding size of the right icon
4716 marginEnd += iconSize;
4717
4718 if (replyIconShown) {
4719 // We also add some padding to the reply icon if it's around
4720 marginEnd += contentMargin;
4721 }
4722 }
4723 if (replyIconShown || largeIconShown) {
4724 // The padding to the content
4725 marginEnd += contentMargin;
4726 }
4727 return marginEnd;
4728 }
4729
Selim Cinek384804b2018-04-18 14:31:07 +08004730 /**
4731 * Bind the large icon.
4732 * @return if the largeIcon is visible
4733 */
4734 private boolean bindLargeIcon(RemoteViews contentView, boolean hideLargeIcon) {
Selim Cinek279fa862016-06-14 10:57:25 -07004735 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4736 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4737 }
Selim Cinek88188f22017-09-19 16:46:56 -07004738 boolean showLargeIcon = mN.mLargeIcon != null && !hideLargeIcon;
4739 if (showLargeIcon) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004740 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4741 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
4742 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Selim Cinek88188f22017-09-19 16:46:56 -07004743 }
Selim Cinek384804b2018-04-18 14:31:07 +08004744 return showLargeIcon;
4745 }
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004746
Selim Cinek384804b2018-04-18 14:31:07 +08004747 /**
4748 * Bind the reply icon.
4749 * @return if the reply icon is visible
4750 */
4751 private boolean bindReplyIcon(RemoteViews contentView, boolean hideReplyIcon) {
4752 boolean actionVisible = !hideReplyIcon;
4753 Action action = null;
Selim Cinek88188f22017-09-19 16:46:56 -07004754 if (actionVisible) {
Selim Cinek384804b2018-04-18 14:31:07 +08004755 action = findReplyAction();
4756 actionVisible = action != null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004757 }
Selim Cinek384804b2018-04-18 14:31:07 +08004758 if (actionVisible) {
4759 contentView.setViewVisibility(R.id.reply_icon_action, View.VISIBLE);
4760 contentView.setDrawableTint(R.id.reply_icon_action,
4761 false /* targetBackground */,
Selim Cinek4717d862018-04-19 09:19:15 +08004762 getNeutralColor(),
Selim Cinek384804b2018-04-18 14:31:07 +08004763 PorterDuff.Mode.SRC_ATOP);
4764 contentView.setOnClickPendingIntent(R.id.reply_icon_action, action.actionIntent);
4765 contentView.setRemoteInputs(R.id.reply_icon_action, action.mRemoteInputs);
4766 } else {
4767 contentView.setRemoteInputs(R.id.reply_icon_action, null);
4768 }
4769 contentView.setViewVisibility(R.id.reply_icon_action,
4770 actionVisible ? View.VISIBLE : View.GONE);
4771 return actionVisible;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004772 }
4773
4774 private Action findReplyAction() {
4775 ArrayList<Action> actions = mActions;
4776 if (mOriginalActions != null) {
4777 actions = mOriginalActions;
4778 }
4779 int numActions = actions.size();
4780 for (int i = 0; i < numActions; i++) {
4781 Action action = actions.get(i);
4782 if (hasValidRemoteInput(action)) {
4783 return action;
4784 }
4785 }
4786 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004787 }
4788
Selim Cinekafeed292017-12-12 17:32:44 -08004789 private void bindNotificationHeader(RemoteViews contentView, boolean ambient,
4790 CharSequence secondaryHeaderText) {
Adrian Roos487374f2017-01-11 15:48:14 -08004791 bindSmallIcon(contentView, ambient);
4792 bindHeaderAppName(contentView, ambient);
4793 if (!ambient) {
4794 // Ambient view does not have these
4795 bindHeaderText(contentView);
Selim Cinekafeed292017-12-12 17:32:44 -08004796 bindHeaderTextSecondary(contentView, secondaryHeaderText);
Adrian Roos487374f2017-01-11 15:48:14 -08004797 bindHeaderChronometerAndTime(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08004798 bindProfileBadge(contentView);
4799 }
Julia Reynolds6013e5c2018-04-18 08:43:34 -04004800 bindActivePermissions(contentView, ambient);
Adrian Roosd83e9992017-03-16 15:17:57 -07004801 bindExpandButton(contentView);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004802 mN.mUsesStandardHeader = true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004803 }
4804
Julia Reynolds6013e5c2018-04-18 08:43:34 -04004805 private void bindActivePermissions(RemoteViews contentView, boolean ambient) {
Selim Cinek4717d862018-04-19 09:19:15 +08004806 int color = ambient ? resolveAmbientColor() : getNeutralColor();
Julia Reynoldsb887b232018-04-10 16:38:08 -04004807 contentView.setDrawableTint(R.id.camera, false, color, PorterDuff.Mode.SRC_ATOP);
4808 contentView.setDrawableTint(R.id.mic, false, color, PorterDuff.Mode.SRC_ATOP);
4809 contentView.setDrawableTint(R.id.overlay, false, color, PorterDuff.Mode.SRC_ATOP);
4810 }
4811
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004812 private void bindExpandButton(RemoteViews contentView) {
Selim Cinekc7f5a822018-03-20 19:32:06 -07004813 int color = isColorized() ? getPrimaryTextColor() : getSecondaryTextColor();
Sunny Goyal5b153922017-09-21 21:00:36 -07004814 contentView.setDrawableTint(R.id.expand_button, false, color,
4815 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08004816 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08004817 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004818 }
4819
4820 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
4821 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08004822 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004823 setTextViewColorSecondary(contentView, R.id.time_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004824 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
4825 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
4826 contentView.setLong(R.id.chronometer, "setBase",
4827 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
4828 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07004829 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07004830 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004831 setTextViewColorSecondary(contentView, R.id.chronometer);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004832 } else {
4833 contentView.setViewVisibility(R.id.time, View.VISIBLE);
4834 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004835 setTextViewColorSecondary(contentView, R.id.time);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004836 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004837 } else {
4838 // We still want a time to be set but gone, such that we can show and hide it
4839 // on demand in case it's a child notification without anything in the header
4840 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004841 }
4842 }
4843
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004844 private void bindHeaderText(RemoteViews contentView) {
4845 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4846 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05004847 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004848 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05004849 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004850 if (headerText == null
4851 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4852 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
4853 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
4854 }
4855 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004856 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek48f66b72017-08-18 16:17:51 -07004857 contentView.setTextViewText(R.id.header_text, processTextSpans(
4858 processLegacyText(headerText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004859 setTextViewColorSecondary(contentView, R.id.header_text);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004860 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
4861 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004862 setTextViewColorSecondary(contentView, R.id.header_text_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004863 }
4864 }
4865
Selim Cinekafeed292017-12-12 17:32:44 -08004866 private void bindHeaderTextSecondary(RemoteViews contentView, CharSequence secondaryText) {
4867 if (!TextUtils.isEmpty(secondaryText)) {
4868 contentView.setTextViewText(R.id.header_text_secondary, processTextSpans(
4869 processLegacyText(secondaryText)));
4870 setTextViewColorSecondary(contentView, R.id.header_text_secondary);
4871 contentView.setViewVisibility(R.id.header_text_secondary, View.VISIBLE);
4872 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.VISIBLE);
4873 setTextViewColorSecondary(contentView, R.id.header_text_secondary_divider);
4874 }
4875 }
4876
Adrian Rooseba05822016-04-22 17:09:27 -07004877 /**
4878 * @hide
4879 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01004880 @UnsupportedAppUsage
Adrian Rooseba05822016-04-22 17:09:27 -07004881 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004882 CharSequence name = null;
4883 final PackageManager pm = mContext.getPackageManager();
4884 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
4885 // only system packages which lump together a bunch of unrelated stuff
4886 // may substitute a different name to make the purpose of the
4887 // notification more clear. the correct package label should always
4888 // be accessible via SystemUI.
4889 final String pkg = mContext.getPackageName();
4890 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
4891 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
4892 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
4893 name = subName;
4894 } else {
4895 Log.w(TAG, "warning: pkg "
4896 + pkg + " attempting to substitute app name '" + subName
4897 + "' without holding perm "
4898 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
4899 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004900 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04004901 if (TextUtils.isEmpty(name)) {
4902 name = pm.getApplicationLabel(mContext.getApplicationInfo());
4903 }
4904 if (TextUtils.isEmpty(name)) {
4905 // still nothing?
4906 return null;
4907 }
4908
4909 return String.valueOf(name);
4910 }
Adrian Roos487374f2017-01-11 15:48:14 -08004911 private void bindHeaderAppName(RemoteViews contentView, boolean ambient) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004912 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos72171622017-01-27 10:32:06 -08004913 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004914 setTextViewColorPrimary(contentView, R.id.app_name_text);
4915 } else {
4916 contentView.setTextColor(R.id.app_name_text,
Selim Cinekc7f5a822018-03-20 19:32:06 -07004917 ambient ? resolveAmbientColor() : getSecondaryTextColor());
Selim Cinek7b9605b2017-01-19 17:36:00 -08004918 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004919 }
4920
Adrian Roos487374f2017-01-11 15:48:14 -08004921 private void bindSmallIcon(RemoteViews contentView, boolean ambient) {
Selim Cinek279fa862016-06-14 10:57:25 -07004922 if (mN.mSmallIcon == null && mN.icon != 0) {
4923 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
4924 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004925 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Sunny Goyal5b153922017-09-21 21:00:36 -07004926 contentView.setInt(R.id.icon, "setImageLevel", mN.iconLevel);
Adrian Roos487374f2017-01-11 15:48:14 -08004927 processSmallIconColor(mN.mSmallIcon, contentView, ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004928 }
4929
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004930 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004931 * @return true if the built notification will show the time or the chronometer; false
4932 * otherwise
4933 */
4934 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07004935 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004936 }
4937
Christoph Studerfe718432014-09-01 18:21:18 +02004938 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004939 // actions_container is only reset when there are no actions to avoid focus issues with
4940 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02004941 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004942 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08004943
4944 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
4945 big.setTextViewText(R.id.notification_material_reply_text_1, null);
Kenny Guya0f6de82018-04-06 16:20:16 +01004946 big.setViewVisibility(R.id.notification_material_reply_text_1_container, View.GONE);
4947 big.setViewVisibility(R.id.notification_material_reply_progress, View.GONE);
Adrian Roose458aa82015-12-08 16:17:19 -08004948
4949 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
4950 big.setTextViewText(R.id.notification_material_reply_text_2, null);
4951 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
4952 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07004953
Selim Cineked64a142018-02-06 18:06:01 -08004954 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
4955 R.dimen.notification_content_margin);
Christoph Studerfe718432014-09-01 18:21:18 +02004956 }
4957
Selim Cinek384804b2018-04-18 14:31:07 +08004958 private RemoteViews applyStandardTemplateWithActions(int layoutId,
4959 TemplateBindResult result) {
4960 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this),
4961 result);
Adrian Roos48d746a2016-04-12 14:57:28 -07004962 }
4963
Adrian Roos70d7aa32017-01-11 15:39:06 -08004964 private RemoteViews applyStandardTemplateWithActions(int layoutId,
Selim Cinek384804b2018-04-18 14:31:07 +08004965 StandardTemplateParams p, TemplateBindResult result) {
4966 RemoteViews big = applyStandardTemplate(layoutId, p, result);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004967
Christoph Studerfe718432014-09-01 18:21:18 +02004968 resetStandardTemplateWithActions(big);
4969
Adrian Roose458aa82015-12-08 16:17:19 -08004970 boolean validRemoteInput = false;
4971
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004972 int N = mActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08004973 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004974 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004975 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08004976 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004977 big.setViewVisibility(R.id.actions, View.VISIBLE);
Selim Cineked64a142018-02-06 18:06:01 -08004978 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Daniel Sandler8680bf82012-05-15 16:52:52 -04004979 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004980 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08004981 Action action = mActions.get(i);
Selim Cinekffcc7cf2018-02-06 17:43:51 -08004982 boolean actionHasValidInput = hasValidRemoteInput(action);
4983 validRemoteInput |= actionHasValidInput;
Adrian Roose458aa82015-12-08 16:17:19 -08004984
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004985 final RemoteViews button = generateActionButton(action, emphazisedMode,
Selim Cinek396caca2018-04-10 17:46:46 -07004986 p.ambient);
4987 if (actionHasValidInput && !emphazisedMode) {
Selim Cinekffcc7cf2018-02-06 17:43:51 -08004988 // Clear the drawable
4989 button.setInt(R.id.action0, "setBackgroundResource", 0);
4990 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004991 big.addView(R.id.actions, button);
4992 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004993 } else {
4994 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004995 }
Adrian Roose458aa82015-12-08 16:17:19 -08004996
4997 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08004998 if (!p.ambient && validRemoteInput && replyText != null
Selim Cinekbee4e072018-05-21 22:06:43 -07004999 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])
5000 && p.maxRemoteInputHistory > 0) {
Kenny Guya0f6de82018-04-06 16:20:16 +01005001 boolean showSpinner = mN.extras.getBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER);
Adrian Roose458aa82015-12-08 16:17:19 -08005002 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Kenny Guya0f6de82018-04-06 16:20:16 +01005003 big.setViewVisibility(R.id.notification_material_reply_text_1_container,
5004 View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005005 big.setTextViewText(R.id.notification_material_reply_text_1,
5006 processTextSpans(replyText[0]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005007 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1);
Kenny Guya0f6de82018-04-06 16:20:16 +01005008 big.setViewVisibility(R.id.notification_material_reply_progress,
5009 showSpinner ? View.VISIBLE : View.GONE);
5010 big.setProgressIndeterminateTintList(
5011 R.id.notification_material_reply_progress,
5012 ColorStateList.valueOf(
5013 isColorized() ? getPrimaryTextColor() : resolveContrastColor()));
Adrian Roose458aa82015-12-08 16:17:19 -08005014
Selim Cinekbee4e072018-05-21 22:06:43 -07005015 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])
5016 && p.maxRemoteInputHistory > 1) {
Adrian Roose458aa82015-12-08 16:17:19 -08005017 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005018 big.setTextViewText(R.id.notification_material_reply_text_2,
5019 processTextSpans(replyText[1]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005020 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2);
Adrian Roose458aa82015-12-08 16:17:19 -08005021
Selim Cinekbee4e072018-05-21 22:06:43 -07005022 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])
5023 && p.maxRemoteInputHistory > 2) {
Adrian Roose458aa82015-12-08 16:17:19 -08005024 big.setViewVisibility(
5025 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005026 big.setTextViewText(R.id.notification_material_reply_text_3,
5027 processTextSpans(replyText[2]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005028 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3);
Adrian Roose458aa82015-12-08 16:17:19 -08005029 }
5030 }
5031 }
5032
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005033 return big;
5034 }
5035
Adrian Roose458aa82015-12-08 16:17:19 -08005036 private boolean hasValidRemoteInput(Action action) {
5037 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
5038 // Weird actions
5039 return false;
5040 }
5041
5042 RemoteInput[] remoteInputs = action.getRemoteInputs();
5043 if (remoteInputs == null) {
5044 return false;
5045 }
5046
5047 for (RemoteInput r : remoteInputs) {
5048 CharSequence[] choices = r.getChoices();
5049 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
5050 return true;
5051 }
5052 }
5053 return false;
5054 }
5055
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005056 /**
5057 * Construct a RemoteViews for the final 1U notification layout. In order:
5058 * 1. Custom contentView from the caller
5059 * 2. Style's proposed content view
5060 * 3. Standard template view
5061 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005062 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005063 return createContentView(false /* increasedheight */ );
5064 }
5065
5066 /**
5067 * Construct a RemoteViews for the smaller content view.
5068 *
5069 * @param increasedHeight true if this layout be created with an increased height. Some
5070 * styles may support showing more then just that basic 1U size
5071 * and the system may decide to render important notifications
5072 * slightly bigger even when collapsed.
5073 *
5074 * @hide
5075 */
5076 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005077 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005078 return mN.contentView;
5079 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005080 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005081 if (styleView != null) {
5082 return styleView;
5083 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005084 }
Selim Cinek384804b2018-04-18 14:31:07 +08005085 return applyStandardTemplate(getBaseLayoutResource(), null /* result */);
Joe Onorato46439ce2010-11-19 13:56:21 -08005086 }
5087
Selim Cineka7679b62017-05-10 16:33:25 -07005088 private boolean useExistingRemoteView() {
5089 return mStyle == null || (!mStyle.displayCustomViewInline()
5090 && !mRebuildStyledRemoteViews);
5091 }
5092
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005093 /**
5094 * Construct a RemoteViews for the final big notification layout.
5095 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005096 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05005097 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07005098 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005099 return mN.bigContentView;
5100 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05005101 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08005102 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005103 } else if (mActions.size() != 0) {
Selim Cinek384804b2018-04-18 14:31:07 +08005104 result = applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5105 null /* result */);
Selim Cinek850a8542015-11-11 11:48:36 -05005106 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08005107 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05005108 return result;
5109 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005110
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005111 /**
Selim Cinek414ad332017-02-24 19:06:12 -08005112 * Construct a RemoteViews for the final notification header only. This will not be
5113 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005114 *
Adrian Roos6f6e1592017-05-02 16:22:53 -07005115 * @param ambient if true, generate the header for the ambient display layout.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005116 * @hide
5117 */
Adrian Roos6f6e1592017-05-02 16:22:53 -07005118 public RemoteViews makeNotificationHeader(boolean ambient) {
Selim Cinek414ad332017-02-24 19:06:12 -08005119 Boolean colorized = (Boolean) mN.extras.get(EXTRA_COLORIZED);
5120 mN.extras.putBoolean(EXTRA_COLORIZED, false);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005121 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Adrian Roos6f6e1592017-05-02 16:22:53 -07005122 ambient ? R.layout.notification_template_ambient_header
5123 : R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005124 resetNotificationHeader(header);
Selim Cinekafeed292017-12-12 17:32:44 -08005125 bindNotificationHeader(header, ambient, null);
Selim Cinek414ad332017-02-24 19:06:12 -08005126 if (colorized != null) {
5127 mN.extras.putBoolean(EXTRA_COLORIZED, colorized);
5128 } else {
5129 mN.extras.remove(EXTRA_COLORIZED);
5130 }
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005131 return header;
5132 }
5133
Adrian Roos487374f2017-01-11 15:48:14 -08005134 /**
5135 * Construct a RemoteViews for the ambient version of the notification.
5136 *
5137 * @hide
5138 */
5139 public RemoteViews makeAmbientNotification() {
5140 RemoteViews ambient = applyStandardTemplateWithActions(
5141 R.layout.notification_template_material_ambient,
Selim Cinek384804b2018-04-18 14:31:07 +08005142 mParams.reset().ambient(true).fillTextsFrom(this).hasProgress(false),
5143 null /* result */);
Adrian Roos487374f2017-01-11 15:48:14 -08005144 return ambient;
5145 }
5146
Selim Cinek29603462015-11-17 19:04:39 -08005147 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005148 if (result != null) {
5149 result.setViewVisibility(R.id.text_line_1, View.GONE);
5150 }
Selim Cinek29603462015-11-17 19:04:39 -08005151 }
5152
Selim Cinek6743c0b2017-01-18 18:24:01 -08005153 /**
5154 * Adapt the Notification header if this view is used as an expanded view.
5155 *
5156 * @hide
5157 */
5158 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005159 if (result != null) {
5160 result.setBoolean(R.id.notification_header, "setExpanded", true);
5161 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005162 }
5163
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005164 /**
5165 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005166 *
5167 * @param increasedHeight true if this layout be created with an increased height. Some
5168 * styles may support showing more then just that basic 1U size
5169 * and the system may decide to render important notifications
5170 * slightly bigger even when collapsed.
5171 *
5172 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005173 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005174 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005175 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005176 return mN.headsUpContentView;
5177 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08005178 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
5179 if (styleView != null) {
5180 return styleView;
5181 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05005182 } else if (mActions.size() == 0) {
5183 return null;
5184 }
5185
Selim Cinekbee4e072018-05-21 22:06:43 -07005186 // We only want at most a single remote input history to be shown here, otherwise
5187 // the content would become squished.
5188 StandardTemplateParams p = mParams.reset().fillTextsFrom(this)
5189 .setMaxRemoteInputHistory(1);
5190 return applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5191 p,
5192 null /* result */);
Chris Wren8fd39ec2014-02-27 17:43:26 -05005193 }
5194
Selim Cinek624c02db2015-12-14 21:00:02 -08005195 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08005196 * Construct a RemoteViews for the final heads-up notification layout.
5197 */
5198 public RemoteViews createHeadsUpContentView() {
5199 return createHeadsUpContentView(false /* useIncreasedHeight */);
5200 }
5201
5202 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08005203 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5204 *
5205 * @hide
5206 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005207 @UnsupportedAppUsage
Selim Cinek624c02db2015-12-14 21:00:02 -08005208 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005209 return makePublicView(false /* ambient */);
5210 }
5211
5212 /**
5213 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5214 *
5215 * @hide
5216 */
5217 public RemoteViews makePublicAmbientNotification() {
5218 return makePublicView(true /* ambient */);
5219 }
5220
5221 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08005222 if (mN.publicVersion != null) {
5223 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005224 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08005225 }
5226 Bundle savedBundle = mN.extras;
5227 Style style = mStyle;
5228 mStyle = null;
5229 Icon largeIcon = mN.mLargeIcon;
5230 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07005231 Bitmap largeIconLegacy = mN.largeIcon;
5232 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005233 ArrayList<Action> actions = mActions;
5234 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08005235 Bundle publicExtras = new Bundle();
5236 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
5237 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
5238 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
5239 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07005240 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
5241 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cineked07b962018-04-30 14:39:35 -07005242 String appName = savedBundle.getString(EXTRA_SUBSTITUTE_APP_NAME);
5243 if (appName != null) {
5244 publicExtras.putString(EXTRA_SUBSTITUTE_APP_NAME, appName);
5245 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005246 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07005247 RemoteViews view;
5248 if (ambient) {
5249 publicExtras.putCharSequence(EXTRA_TITLE,
5250 mContext.getString(com.android.internal.R.string.notification_hidden_text));
5251 view = makeAmbientNotification();
5252 } else{
5253 view = makeNotificationHeader(false /* ambient */);
5254 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
5255 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005256 mN.extras = savedBundle;
5257 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07005258 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005259 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08005260 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005261 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08005262 }
5263
Selim Cinek6743c0b2017-01-18 18:24:01 -08005264 /**
5265 * Construct a content view for the display when low - priority
5266 *
5267 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
5268 * a new subtext is created consisting of the content of the
5269 * notification.
5270 * @hide
5271 */
5272 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
5273 int color = mN.color;
5274 mN.color = COLOR_DEFAULT;
5275 CharSequence summary = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
5276 if (!useRegularSubtext || TextUtils.isEmpty(summary)) {
5277 CharSequence newSummary = createSummaryText();
5278 if (!TextUtils.isEmpty(newSummary)) {
5279 mN.extras.putCharSequence(EXTRA_SUB_TEXT, newSummary);
5280 }
5281 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08005282
Adrian Roos6f6e1592017-05-02 16:22:53 -07005283 RemoteViews header = makeNotificationHeader(false /* ambient */);
Selim Cinek1b554392017-02-28 17:22:49 -08005284 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08005285 if (summary != null) {
5286 mN.extras.putCharSequence(EXTRA_SUB_TEXT, summary);
5287 } else {
5288 mN.extras.remove(EXTRA_SUB_TEXT);
5289 }
5290 mN.color = color;
5291 return header;
5292 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005293
Selim Cinek6743c0b2017-01-18 18:24:01 -08005294 private CharSequence createSummaryText() {
5295 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
5296 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
5297 return titleText;
5298 }
5299 SpannableStringBuilder summary = new SpannableStringBuilder();
5300 if (titleText == null) {
5301 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
5302 }
5303 BidiFormatter bidi = BidiFormatter.getInstance();
5304 if (titleText != null) {
5305 summary.append(bidi.unicodeWrap(titleText));
5306 }
5307 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
5308 if (titleText != null && contentText != null) {
5309 summary.append(bidi.unicodeWrap(mContext.getText(
5310 R.string.notification_header_divider_symbol_with_spaces)));
5311 }
5312 if (contentText != null) {
5313 summary.append(bidi.unicodeWrap(contentText));
5314 }
5315 return summary;
5316 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05005317
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005318 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Selim Cinek396caca2018-04-10 17:46:46 -07005319 boolean ambient) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04005320 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07005321 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005322 emphazisedMode ? getEmphasizedActionLayoutResource()
5323 : tombstone ? getActionTombstoneLayoutResource()
5324 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04005325 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04005326 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04005327 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005328 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005329 if (action.mRemoteInputs != null) {
5330 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
5331 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005332 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07005333 // change the background bgColor
Selim Cinek981962e2016-07-20 20:41:58 -07005334 CharSequence title = action.title;
5335 ColorStateList[] outResultColor = null;
Selim Cinek396caca2018-04-10 17:46:46 -07005336 int background = resolveBackgroundColor();
Selim Cinek981962e2016-07-20 20:41:58 -07005337 if (isLegacy()) {
Lucas Dupina291d192018-06-07 13:59:42 -07005338 title = ContrastColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07005339 } else {
5340 outResultColor = new ColorStateList[1];
Selim Cinek396caca2018-04-10 17:46:46 -07005341 title = ensureColorSpanContrast(title, background, outResultColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005342 }
Selim Cinek48f66b72017-08-18 16:17:51 -07005343 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005344 setTextViewColorPrimary(button, R.id.action0);
Selim Cinek396caca2018-04-10 17:46:46 -07005345 int rippleColor;
5346 boolean hasColorOverride = outResultColor != null && outResultColor[0] != null;
5347 if (hasColorOverride) {
5348 // There's a span spanning the full text, let's take it and use it as the
5349 // background color
5350 background = outResultColor[0].getDefaultColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005351 int textColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07005352 background, mInNightMode);
Selim Cinek396caca2018-04-10 17:46:46 -07005353 button.setTextColor(R.id.action0, textColor);
5354 rippleColor = textColor;
Anthony Chenad4d1582017-04-10 16:07:58 -07005355 } else if (mN.color != COLOR_DEFAULT && !isColorized() && mTintActionButtons) {
Selim Cinek396caca2018-04-10 17:46:46 -07005356 rippleColor = resolveContrastColor();
5357 button.setTextColor(R.id.action0, rippleColor);
5358 } else {
5359 rippleColor = getPrimaryTextColor();
Selim Cinek981962e2016-07-20 20:41:58 -07005360 }
Selim Cinek396caca2018-04-10 17:46:46 -07005361 // We only want about 20% alpha for the ripple
5362 rippleColor = (rippleColor & 0x00ffffff) | 0x33000000;
5363 button.setColorStateList(R.id.action0, "setRippleColor",
5364 ColorStateList.valueOf(rippleColor));
5365 button.setColorStateList(R.id.action0, "setButtonBackground",
5366 ColorStateList.valueOf(background));
5367 button.setBoolean(R.id.action0, "setHasStroke", !hasColorOverride);
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005368 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07005369 button.setTextViewText(R.id.action0, processTextSpans(
5370 processLegacyText(action.title)));
Adrian Roos72171622017-01-27 10:32:06 -08005371 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005372 setTextViewColorPrimary(button, R.id.action0);
Anthony Chenad4d1582017-04-10 16:07:58 -07005373 } else if (mN.color != COLOR_DEFAULT && mTintActionButtons) {
Adrian Roos487374f2017-01-11 15:48:14 -08005374 button.setTextColor(R.id.action0,
5375 ambient ? resolveAmbientColor() : resolveContrastColor());
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005376 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005377 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005378 return button;
5379 }
5380
Joe Onoratocb109a02011-01-18 17:57:41 -08005381 /**
Selim Cinek981962e2016-07-20 20:41:58 -07005382 * Ensures contrast on color spans against a background color. also returns the color of the
5383 * text if a span was found that spans over the whole text.
5384 *
5385 * @param charSequence the charSequence on which the spans are
5386 * @param background the background color to ensure the contrast against
5387 * @param outResultColor an array in which a color will be returned as the first element if
5388 * there exists a full length color span.
5389 * @return the contrasted charSequence
5390 */
5391 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
5392 ColorStateList[] outResultColor) {
5393 if (charSequence instanceof Spanned) {
5394 Spanned ss = (Spanned) charSequence;
5395 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
5396 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
5397 for (Object span : spans) {
5398 Object resultSpan = span;
5399 int spanStart = ss.getSpanStart(span);
5400 int spanEnd = ss.getSpanEnd(span);
5401 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
5402 if (resultSpan instanceof CharacterStyle) {
5403 resultSpan = ((CharacterStyle) span).getUnderlying();
5404 }
5405 if (resultSpan instanceof TextAppearanceSpan) {
5406 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
5407 ColorStateList textColor = originalSpan.getTextColor();
5408 if (textColor != null) {
5409 int[] colors = textColor.getColors();
5410 int[] newColors = new int[colors.length];
5411 for (int i = 0; i < newColors.length; i++) {
Lucas Dupina291d192018-06-07 13:59:42 -07005412 newColors[i] = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005413 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005414 }
5415 textColor = new ColorStateList(textColor.getStates().clone(),
5416 newColors);
Selim Cinek396caca2018-04-10 17:46:46 -07005417 if (fullLength) {
5418 outResultColor[0] = textColor;
5419 // Let's drop the color from the span
5420 textColor = null;
5421 }
Selim Cinek981962e2016-07-20 20:41:58 -07005422 resultSpan = new TextAppearanceSpan(
5423 originalSpan.getFamily(),
5424 originalSpan.getTextStyle(),
5425 originalSpan.getTextSize(),
5426 textColor,
5427 originalSpan.getLinkTextColor());
Selim Cinek981962e2016-07-20 20:41:58 -07005428 }
5429 } else if (resultSpan instanceof ForegroundColorSpan) {
5430 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
5431 int foregroundColor = originalSpan.getForegroundColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005432 foregroundColor = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005433 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005434 if (fullLength) {
5435 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
Selim Cinek396caca2018-04-10 17:46:46 -07005436 resultSpan = null;
5437 } else {
5438 resultSpan = new ForegroundColorSpan(foregroundColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005439 }
5440 } else {
5441 resultSpan = span;
5442 }
Selim Cinek396caca2018-04-10 17:46:46 -07005443 if (resultSpan != null) {
5444 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
5445 }
Selim Cinek981962e2016-07-20 20:41:58 -07005446 }
5447 return builder;
5448 }
5449 return charSequence;
5450 }
5451
5452 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005453 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07005454 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005455 */
5456 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005457 if (!mIsLegacyInitialized) {
5458 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
5459 < Build.VERSION_CODES.LOLLIPOP;
5460 mIsLegacyInitialized = true;
5461 }
5462 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005463 }
5464
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005465 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08005466 return processLegacyText(charSequence, false /* ambient */);
5467 }
5468
5469 private CharSequence processLegacyText(CharSequence charSequence, boolean ambient) {
5470 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
5471 boolean wantLightText = ambient;
5472 if (isAlreadyLightText != wantLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005473 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005474 } else {
5475 return charSequence;
5476 }
5477 }
5478
Dan Sandler26e81cf2014-05-06 10:01:27 -04005479 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005480 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04005481 */
Adrian Roos487374f2017-01-11 15:48:14 -08005482 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
5483 boolean ambient) {
Selim Cinekea4bef72015-12-02 15:51:10 -08005484 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005485 int color;
5486 if (ambient) {
5487 color = resolveAmbientColor();
5488 } else if (isColorized()) {
5489 color = getPrimaryTextColor();
5490 } else {
5491 color = resolveContrastColor();
5492 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005493 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07005494 contentView.setDrawableTint(R.id.icon, false, color,
5495 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005496
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005497 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005498 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08005499 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005500 }
5501
Dan Sandler26e81cf2014-05-06 10:01:27 -04005502 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005503 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04005504 * if it's grayscale).
5505 */
5506 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04005507 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
5508 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005509 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005510 // resolve color will fall back to the default when legacy
Sunny Goyal5b153922017-09-21 21:00:36 -07005511 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(),
5512 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01005513 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005514 }
5515
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05005516 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005517 if (mN.color != COLOR_DEFAULT) {
5518 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02005519 }
Jorim Jaggi74419312014-06-10 20:57:21 +02005520 }
5521
Adrian Roos4ff3b122016-02-01 12:26:13 -08005522 int resolveContrastColor() {
5523 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
5524 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005525 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005526
Selim Cinekac5f0272017-05-02 16:05:41 -07005527 int color;
Selim Cinekc7f5a822018-03-20 19:32:06 -07005528 int background = mContext.getColor(
5529 com.android.internal.R.color.notification_material_background_color);
Selim Cinekac5f0272017-05-02 16:05:41 -07005530 if (mN.color == COLOR_DEFAULT) {
5531 ensureColors();
Lucas Dupinf03e7522018-06-25 16:21:13 -07005532 color = ContrastColorUtil.resolveDefaultColor(mContext, background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005533 } else {
Lucas Dupina291d192018-06-07 13:59:42 -07005534 color = ContrastColorUtil.resolveContrastColor(mContext, mN.color,
Anthony Chenad4d1582017-04-10 16:07:58 -07005535 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005536 }
5537 if (Color.alpha(color) < 255) {
5538 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005539 color = ContrastColorUtil.compositeColors(color, background);
Selim Cinekac5f0272017-05-02 16:05:41 -07005540 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005541 mCachedContrastColorIsFor = mN.color;
Selim Cinekac5f0272017-05-02 16:05:41 -07005542 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005543 }
5544
Selim Cinek4717d862018-04-19 09:19:15 +08005545 int resolveNeutralColor() {
5546 if (mNeutralColor != COLOR_INVALID) {
5547 return mNeutralColor;
5548 }
5549 int background = mContext.getColor(
5550 com.android.internal.R.color.notification_material_background_color);
Lucas Dupinf03e7522018-06-25 16:21:13 -07005551 mNeutralColor = ContrastColorUtil.resolveDefaultColor(mContext, background,
5552 mInNightMode);
Selim Cinek4717d862018-04-19 09:19:15 +08005553 if (Color.alpha(mNeutralColor) < 255) {
5554 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005555 mNeutralColor = ContrastColorUtil.compositeColors(mNeutralColor, background);
Selim Cinek4717d862018-04-19 09:19:15 +08005556 }
5557 return mNeutralColor;
5558 }
5559
Adrian Roos487374f2017-01-11 15:48:14 -08005560 int resolveAmbientColor() {
5561 if (mCachedAmbientColorIsFor == mN.color && mCachedAmbientColorIsFor != COLOR_INVALID) {
5562 return mCachedAmbientColor;
5563 }
Lucas Dupina291d192018-06-07 13:59:42 -07005564 final int contrasted = ContrastColorUtil.resolveAmbientColor(mContext, mN.color);
Adrian Roos487374f2017-01-11 15:48:14 -08005565
5566 mCachedAmbientColorIsFor = mN.color;
5567 return mCachedAmbientColor = contrasted;
5568 }
5569
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005570 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005571 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005572 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08005573 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005574 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005575 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005576 mN.actions = new Action[mActions.size()];
5577 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005578 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005579 if (!mPersonList.isEmpty()) {
Selim Cineke7238dd2017-12-14 17:48:32 -08005580 mN.extras.putParcelableArrayList(EXTRA_PEOPLE_LIST, mPersonList);
Dan Sandler0bf2ed82013-12-21 23:33:41 -06005581 }
Selim Cinek247fa012016-02-18 09:50:48 -08005582 if (mN.bigContentView != null || mN.contentView != null
5583 || mN.headsUpContentView != null) {
5584 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
5585 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005586 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08005587 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005588
Julia Reynolds3b848122016-02-26 10:45:32 -05005589 /**
5590 * Creates a Builder from an existing notification so further changes can be made.
5591 * @param context The context for your application / activity.
5592 * @param n The notification to create a Builder from.
5593 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005594 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005595 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005596 ApplicationInfo applicationInfo = n.extras.getParcelable(
5597 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005598 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05005599 if (applicationInfo != null) {
5600 try {
5601 builderContext = context.createApplicationContext(applicationInfo,
5602 Context.CONTEXT_RESTRICTED);
5603 } catch (NameNotFoundException e) {
5604 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
5605 builderContext = context; // try with our context
5606 }
5607 } else {
5608 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02005609 }
5610
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005611 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005612 }
5613
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005614 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005615 * @deprecated Use {@link #build()} instead.
5616 */
5617 @Deprecated
5618 public Notification getNotification() {
5619 return build();
5620 }
5621
5622 /**
5623 * Combine all of the options that have been set and return a new {@link Notification}
5624 * object.
5625 */
5626 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005627 // first, add any extras from the calling code
5628 if (mUserExtras != null) {
5629 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005630 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005631
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005632 mN.creationTime = System.currentTimeMillis();
5633
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005634 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05005635 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02005636
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005637 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005638
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005639 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02005640 mStyle.reduceImageSizes(mContext);
5641 mStyle.purgeResources();
Selim Cinek90343862018-02-01 11:07:11 -08005642 mStyle.validate(mContext);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005643 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005644 }
Selim Cinekd0426622017-07-11 13:19:59 +02005645 mN.reduceImageSizes(mContext);
5646
Adrian Roos5081c0d2016-02-26 16:04:19 -08005647 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07005648 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005649 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005650 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005651 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
5652 mN.contentView.getSequenceNumber());
5653 }
5654 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005655 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005656 if (mN.bigContentView != null) {
5657 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
5658 mN.bigContentView.getSequenceNumber());
5659 }
5660 }
5661 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005662 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005663 if (mN.headsUpContentView != null) {
5664 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
5665 mN.headsUpContentView.getSequenceNumber());
5666 }
5667 }
5668 }
5669
Julia Reynolds4c0c2022016-02-02 15:11:59 -05005670 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
5671 mN.flags |= FLAG_SHOW_LIGHTS;
5672 }
5673
Adrian Roosfb921842017-10-26 14:49:56 +02005674 mN.allPendingIntents = null;
5675
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005676 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005677 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005678
5679 /**
5680 * Apply this Builder to an existing {@link Notification} object.
5681 *
5682 * @hide
5683 */
5684 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005685 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005686 return n;
5687 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005688
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005689 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005690 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005691 * change. Also removes extenders on low ram devices, as
5692 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005693 *
5694 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5695 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005696 * @hide
5697 */
Kristian Monsen05f34792018-04-09 10:27:16 +02005698 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam,
5699 Context context) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005700 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005701
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005702 // Only strip views for known Styles because we won't know how to
5703 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005704 if (!isLowRam
5705 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005706 && getNotificationStyleClass(templateClass) == null) {
5707 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005708 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005709
5710 // Only strip unmodified BuilderRemoteViews.
5711 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005712 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005713 n.contentView.getSequenceNumber();
5714 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005715 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005716 n.bigContentView.getSequenceNumber();
5717 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005718 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005719 n.headsUpContentView.getSequenceNumber();
5720
5721 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005722 if (!isLowRam
5723 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005724 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005725 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005726
5727 Notification clone = n.clone();
5728 if (stripContentView) {
5729 clone.contentView = null;
5730 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5731 }
5732 if (stripBigContentView) {
5733 clone.bigContentView = null;
5734 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5735 }
5736 if (stripHeadsUpContentView) {
5737 clone.headsUpContentView = null;
5738 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5739 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005740 if (isLowRam) {
Kristian Monsen05f34792018-04-09 10:27:16 +02005741 String[] allowedServices = context.getResources().getStringArray(
5742 R.array.config_allowedManagedServicesOnLowRamDevices);
5743 if (allowedServices.length == 0) {
5744 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5745 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5746 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5747 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005748 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005749 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005750 }
5751
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005752 @UnsupportedAppUsage
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005753 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005754 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005755 }
5756
5757 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005758 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005759 }
5760
5761 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005762 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005763 }
5764
5765 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005766 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005767 }
5768
5769 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005770 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005771 }
5772
Adrian Roosc1a80b02016-04-05 14:54:55 -07005773 private int getMessagingLayoutResource() {
5774 return R.layout.notification_template_material_messaging;
5775 }
5776
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005777 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005778 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005779 }
5780
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005781 private int getEmphasizedActionLayoutResource() {
5782 return R.layout.notification_material_action_emphasized;
5783 }
5784
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005785 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005786 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005787 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005788
5789 private int getBackgroundColor() {
5790 if (isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005791 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : mN.color;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005792 } else {
Selim Cinekc7f5a822018-03-20 19:32:06 -07005793 return COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005794 }
5795 }
5796
Selim Cinek396caca2018-04-10 17:46:46 -07005797 /**
Selim Cinek4717d862018-04-19 09:19:15 +08005798 * Gets a neutral color that can be used for icons or similar that should not stand out.
5799 */
5800 private int getNeutralColor() {
5801 if (isColorized()) {
5802 return getSecondaryTextColor();
5803 } else {
5804 return resolveNeutralColor();
5805 }
5806 }
5807
5808 /**
Selim Cinek396caca2018-04-10 17:46:46 -07005809 * Same as getBackgroundColor but also resolved the default color to the background.
5810 */
5811 private int resolveBackgroundColor() {
5812 int backgroundColor = getBackgroundColor();
5813 if (backgroundColor == COLOR_DEFAULT) {
5814 backgroundColor = mContext.getColor(
5815 com.android.internal.R.color.notification_material_background_color);
5816 }
5817 return backgroundColor;
5818 }
5819
Selim Cinek7b9605b2017-01-19 17:36:00 -08005820 private boolean isColorized() {
5821 return mN.isColorized();
5822 }
Selim Cinek99104832017-01-25 14:47:33 -08005823
Anthony Chenad4d1582017-04-10 16:07:58 -07005824 private boolean shouldTintActionButtons() {
5825 return mTintActionButtons;
5826 }
5827
Selim Cinek99104832017-01-25 14:47:33 -08005828 private boolean textColorsNeedInversion() {
5829 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
5830 return false;
5831 }
5832 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
5833 return targetSdkVersion > Build.VERSION_CODES.M
5834 && targetSdkVersion < Build.VERSION_CODES.O;
5835 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005836
5837 /**
5838 * Set a color palette to be used as the background and textColors
5839 *
5840 * @param backgroundColor the color to be used as the background
5841 * @param foregroundColor the color to be used as the foreground
5842 *
5843 * @hide
5844 */
5845 public void setColorPalette(int backgroundColor, int foregroundColor) {
5846 mBackgroundColor = backgroundColor;
5847 mForegroundColor = foregroundColor;
5848 mTextColorsAreForBackground = COLOR_INVALID;
5849 ensureColors();
5850 }
Selim Cinekac5f0272017-05-02 16:05:41 -07005851
5852 /**
Selim Cineka7679b62017-05-10 16:33:25 -07005853 * Forces all styled remoteViews to be built from scratch and not use any cached
5854 * RemoteViews.
5855 * This is needed for legacy apps that are baking in their remoteviews into the
5856 * notification.
5857 *
5858 * @hide
5859 */
5860 public void setRebuildStyledRemoteViews(boolean rebuild) {
5861 mRebuildStyledRemoteViews = rebuild;
5862 }
Selim Cinekaa9db1f2018-02-27 17:35:47 -08005863
5864 /**
5865 * Get the text that should be displayed in the statusBar when heads upped. This is
5866 * usually just the app name, but may be different depending on the style.
5867 *
5868 * @param publicMode If true, return a text that is safe to display in public.
5869 *
5870 * @hide
5871 */
5872 public CharSequence getHeadsUpStatusBarText(boolean publicMode) {
5873 if (mStyle != null && !publicMode) {
5874 CharSequence text = mStyle.getHeadsUpStatusBarText();
5875 if (!TextUtils.isEmpty(text)) {
5876 return text;
5877 }
5878 }
5879 return loadHeaderAppName();
5880 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005881 }
5882
5883 /**
Selim Cinekd0426622017-07-11 13:19:59 +02005884 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
5885 * remote views.
5886 *
5887 * @hide
5888 */
5889 void reduceImageSizes(Context context) {
5890 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
5891 return;
5892 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005893 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02005894 if (mLargeIcon != null || largeIcon != null) {
5895 Resources resources = context.getResources();
5896 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07005897 int maxWidth = resources.getDimensionPixelSize(isLowRam
5898 ? R.dimen.notification_right_icon_size_low_ram
5899 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02005900 int maxHeight = maxWidth;
5901 if (MediaStyle.class.equals(style)
5902 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005903 maxHeight = resources.getDimensionPixelSize(isLowRam
5904 ? R.dimen.notification_media_image_max_height_low_ram
5905 : R.dimen.notification_media_image_max_height);
5906 maxWidth = resources.getDimensionPixelSize(isLowRam
5907 ? R.dimen.notification_media_image_max_width_low_ram
5908 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02005909 }
5910 if (mLargeIcon != null) {
5911 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
5912 }
5913 if (largeIcon != null) {
5914 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
5915 }
5916 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005917 reduceImageSizesForRemoteView(contentView, context, isLowRam);
5918 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
5919 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02005920 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
5921 }
5922
Selim Cineka8cb1262017-08-15 16:53:44 -07005923 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
5924 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02005925 if (remoteView != null) {
5926 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07005927 int maxWidth = resources.getDimensionPixelSize(isLowRam
5928 ? R.dimen.notification_custom_view_max_image_width_low_ram
5929 : R.dimen.notification_custom_view_max_image_width);
5930 int maxHeight = resources.getDimensionPixelSize(isLowRam
5931 ? R.dimen.notification_custom_view_max_image_height_low_ram
5932 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02005933 remoteView.reduceImageSizes(maxWidth, maxHeight);
5934 }
5935 }
5936
5937 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005938 * @return whether this notification is a foreground service notification
Selim Cinek7b9605b2017-01-19 17:36:00 -08005939 */
Selim Cinek22714f12017-04-13 16:23:53 -07005940 private boolean isForegroundService() {
5941 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005942 }
5943
5944 /**
Selim Cinek99104832017-01-25 14:47:33 -08005945 * @return whether this notification has a media session attached
5946 * @hide
5947 */
5948 public boolean hasMediaSession() {
5949 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
5950 }
5951
5952 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005953 * @return the style class of this notification
5954 * @hide
5955 */
5956 public Class<? extends Notification.Style> getNotificationStyle() {
5957 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
5958
5959 if (!TextUtils.isEmpty(templateClass)) {
5960 return Notification.getNotificationStyleClass(templateClass);
5961 }
5962 return null;
5963 }
5964
5965 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005966 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08005967 *
5968 * @hide
5969 */
5970 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005971 if (isColorizedMedia()) {
5972 return true;
5973 }
Julia Reynolds4db59552017-06-30 13:34:01 -04005974 return extras.getBoolean(EXTRA_COLORIZED)
5975 && (hasColorizedPermission() || isForegroundService());
5976 }
5977
5978 /**
5979 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
5980 * permission. The permission is checked when a notification is enqueued.
5981 */
5982 private boolean hasColorizedPermission() {
5983 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07005984 }
5985
5986 /**
5987 * @return true if this notification is colorized and it is a media notification
5988 *
5989 * @hide
5990 */
5991 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08005992 Class<? extends Style> style = getNotificationStyle();
5993 if (MediaStyle.class.equals(style)) {
5994 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
5995 if ((colorized == null || colorized) && hasMediaSession()) {
5996 return true;
5997 }
5998 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5999 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
6000 return true;
6001 }
6002 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07006003 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006004 }
6005
Selim Cinek0847acd2017-04-24 19:48:29 -07006006
6007 /**
6008 * @return true if this is a media notification
6009 *
6010 * @hide
6011 */
6012 public boolean isMediaNotification() {
6013 Class<? extends Style> style = getNotificationStyle();
6014 if (MediaStyle.class.equals(style)) {
6015 return true;
6016 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6017 return true;
6018 }
6019 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006020 }
6021
Selim Cinek279fa862016-06-14 10:57:25 -07006022 private boolean hasLargeIcon() {
6023 return mLargeIcon != null || largeIcon != null;
6024 }
6025
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006026 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006027 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006028 * @hide
6029 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07006030 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006031 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
6032 }
6033
6034 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006035 * @return true if the notification will show a chronometer; false otherwise
6036 * @hide
6037 */
6038 public boolean showsChronometer() {
6039 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
6040 }
6041
6042 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04006043 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05006044 */
6045 @SystemApi
6046 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
6047 Class<? extends Style>[] classes = new Class[] {
6048 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
6049 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
6050 MessagingStyle.class };
6051 for (Class<? extends Style> innerClass : classes) {
6052 if (templateClass.equals(innerClass.getName())) {
6053 return innerClass;
6054 }
6055 }
6056 return null;
6057 }
6058
6059 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006060 * An object that can apply a rich notification style to a {@link Notification.Builder}
6061 * object.
6062 */
Griff Hazendfcb0802014-02-11 12:00:00 -08006063 public static abstract class Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07006064
6065 /**
6066 * The number of items allowed simulatanously in the remote input history.
6067 * @hide
6068 */
6069 static final int MAX_REMOTE_INPUT_HISTORY_LINES = 3;
Chris Wrend6297db2012-05-03 16:20:13 -04006070 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006071
6072 /**
6073 * @hide
6074 */
6075 protected CharSequence mSummaryText = null;
6076
6077 /**
6078 * @hide
6079 */
6080 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04006081
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006082 protected Builder mBuilder;
6083
Chris Wrend6297db2012-05-03 16:20:13 -04006084 /**
6085 * Overrides ContentTitle in the big form of the template.
6086 * This defaults to the value passed to setContentTitle().
6087 */
6088 protected void internalSetBigContentTitle(CharSequence title) {
6089 mBigContentTitle = title;
6090 }
6091
6092 /**
6093 * Set the first line of text after the detail section in the big form of the template.
6094 */
6095 protected void internalSetSummaryText(CharSequence cs) {
6096 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04006097 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04006098 }
6099
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006100 public void setBuilder(Builder builder) {
6101 if (mBuilder != builder) {
6102 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07006103 if (mBuilder != null) {
6104 mBuilder.setStyle(this);
6105 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006106 }
6107 }
6108
Chris Wrend6297db2012-05-03 16:20:13 -04006109 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006110 if (mBuilder == null) {
6111 throw new IllegalArgumentException("Style requires a valid Builder object");
6112 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006113 }
Chris Wrend6297db2012-05-03 16:20:13 -04006114
6115 protected RemoteViews getStandardView(int layoutId) {
Selim Cinek384804b2018-04-18 14:31:07 +08006116 return getStandardView(layoutId, null);
6117 }
6118
6119 /**
6120 * Get the standard view for this style.
6121 *
6122 * @param layoutId The layout id to use
6123 * @param result The result where template bind information is saved.
6124 * @return A remoteView for this style.
6125 * @hide
6126 */
6127 protected RemoteViews getStandardView(int layoutId, TemplateBindResult result) {
Chris Wrend6297db2012-05-03 16:20:13 -04006128 checkBuilder();
6129
Christoph Studer4600f9b2014-07-22 22:44:43 +02006130 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006131 CharSequence oldBuilderContentTitle =
6132 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04006133 if (mBigContentTitle != null) {
6134 mBuilder.setContentTitle(mBigContentTitle);
6135 }
6136
Selim Cinek384804b2018-04-18 14:31:07 +08006137 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId, result);
Chris Wrend6297db2012-05-03 16:20:13 -04006138
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006139 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006140
Chris Wrend6297db2012-05-03 16:20:13 -04006141 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
6142 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04006143 } else {
6144 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04006145 }
6146
Chris Wrend6297db2012-05-03 16:20:13 -04006147 return contentView;
6148 }
6149
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006150 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006151 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006152 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08006153 *
6154 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006155 * @hide
6156 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08006157 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006158 return null;
6159 }
6160
6161 /**
6162 * Construct a Style-specific RemoteViews for the final big notification layout.
6163 * @hide
6164 */
6165 public RemoteViews makeBigContentView() {
6166 return null;
6167 }
6168
6169 /**
6170 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08006171 *
6172 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006173 * @hide
6174 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006175 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006176 return null;
6177 }
6178
6179 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006180 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006181 * @hide
6182 */
6183 public void addExtras(Bundle extras) {
6184 if (mSummaryTextSet) {
6185 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
6186 }
6187 if (mBigContentTitle != null) {
6188 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
6189 }
Chris Wren91ad5632013-06-05 15:05:57 -04006190 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006191 }
6192
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006193 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006194 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006195 * @hide
6196 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02006197 protected void restoreFromExtras(Bundle extras) {
6198 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
6199 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
6200 mSummaryTextSet = true;
6201 }
6202 if (extras.containsKey(EXTRA_TITLE_BIG)) {
6203 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
6204 }
6205 }
6206
6207
6208 /**
6209 * @hide
6210 */
6211 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006212 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006213 return wip;
6214 }
6215
Daniel Sandler0ec46202015-06-24 01:27:05 -04006216 /**
6217 * @hide
6218 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006219 public void purgeResources() {}
6220
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006221 /**
6222 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
6223 * attached to.
6224 *
6225 * @return the fully constructed Notification.
6226 */
6227 public Notification build() {
6228 checkBuilder();
6229 return mBuilder.build();
6230 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006231
6232 /**
6233 * @hide
6234 * @return true if the style positions the progress bar on the second line; false if the
6235 * style hides the progress bar
6236 */
6237 protected boolean hasProgress() {
6238 return true;
6239 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006240
6241 /**
6242 * @hide
6243 * @return Whether we should put the summary be put into the notification header
6244 */
6245 public boolean hasSummaryInHeader() {
6246 return true;
6247 }
Selim Cinek593610c2016-02-16 18:42:57 -08006248
6249 /**
6250 * @hide
6251 * @return Whether custom content views are displayed inline in the style
6252 */
6253 public boolean displayCustomViewInline() {
6254 return false;
6255 }
Selim Cinekd0426622017-07-11 13:19:59 +02006256
6257 /**
6258 * Reduces the image sizes contained in this style.
6259 *
6260 * @hide
6261 */
6262 public void reduceImageSizes(Context context) {
6263 }
Selim Cinek90343862018-02-01 11:07:11 -08006264
6265 /**
6266 * Validate that this style was properly composed. This is called at build time.
6267 * @hide
6268 */
6269 public void validate(Context context) {
6270 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006271
6272 /**
6273 * @hide
6274 */
6275 public abstract boolean areNotificationsVisiblyDifferent(Style other);
Selim Cinekc7f5a822018-03-20 19:32:06 -07006276
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006277 /**
koprivaa1a78482018-10-09 10:09:23 -07006278 * @return the text that should be displayed in the statusBar when heads-upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006279 * If {@code null} is returned, the default implementation will be used.
6280 *
6281 * @hide
6282 */
6283 public CharSequence getHeadsUpStatusBarText() {
6284 return null;
6285 }
Joe Onorato46439ce2010-11-19 13:56:21 -08006286 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006287
6288 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006289 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08006290 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006291 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006292 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006293 * Notification notif = new Notification.Builder(mContext)
6294 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
6295 * .setContentText(subject)
6296 * .setSmallIcon(R.drawable.new_post)
6297 * .setLargeIcon(aBitmap)
6298 * .setStyle(new Notification.BigPictureStyle()
6299 * .bigPicture(aBigBitmap))
6300 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006301 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006302 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006303 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006304 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006305 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006306 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006307 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006308 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006309
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006310 public BigPictureStyle() {
6311 }
6312
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006313 /**
6314 * @deprecated use {@code BigPictureStyle()}.
6315 */
6316 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006317 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006318 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006319 }
6320
Chris Wrend6297db2012-05-03 16:20:13 -04006321 /**
6322 * Overrides ContentTitle in the big form of the template.
6323 * This defaults to the value passed to setContentTitle().
6324 */
6325 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006326 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006327 return this;
6328 }
6329
6330 /**
6331 * Set the first line of text after the detail section in the big form of the template.
6332 */
6333 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006334 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006335 return this;
6336 }
6337
Chris Wren0bd664d2012-08-01 13:56:56 -04006338 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05006339 * @hide
6340 */
6341 public Bitmap getBigPicture() {
6342 return mPicture;
6343 }
6344
6345 /**
Chris Wren0bd664d2012-08-01 13:56:56 -04006346 * Provide the bitmap to be used as the payload for the BigPicture notification.
6347 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006348 public BigPictureStyle bigPicture(Bitmap b) {
6349 mPicture = b;
6350 return this;
6351 }
6352
Chris Wren3745a3d2012-05-22 15:11:52 -04006353 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04006354 * Override the large icon when the big notification is shown.
6355 */
6356 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04006357 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
6358 }
6359
6360 /**
6361 * Override the large icon when the big notification is shown.
6362 */
6363 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04006364 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006365 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006366 return this;
6367 }
6368
Riley Andrews0394a0c2015-11-03 23:36:52 -08006369 /** @hide */
6370 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
6371
Daniel Sandler0ec46202015-06-24 01:27:05 -04006372 /**
6373 * @hide
6374 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006375 @Override
6376 public void purgeResources() {
6377 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08006378 if (mPicture != null &&
6379 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08006380 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006381 mPicture = mPicture.createAshmemBitmap();
6382 }
6383 if (mBigLargeIcon != null) {
6384 mBigLargeIcon.convertToAshmem();
6385 }
6386 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01006387
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006388 /**
6389 * @hide
6390 */
Selim Cinekd0426622017-07-11 13:19:59 +02006391 @Override
6392 public void reduceImageSizes(Context context) {
6393 super.reduceImageSizes(context);
6394 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006395 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006396 if (mPicture != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006397 int maxPictureWidth = resources.getDimensionPixelSize(isLowRam
6398 ? R.dimen.notification_big_picture_max_height_low_ram
6399 : R.dimen.notification_big_picture_max_height);
6400 int maxPictureHeight = resources.getDimensionPixelSize(isLowRam
6401 ? R.dimen.notification_big_picture_max_width_low_ram
6402 : R.dimen.notification_big_picture_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006403 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
6404 }
6405 if (mBigLargeIcon != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006406 int rightIconSize = resources.getDimensionPixelSize(isLowRam
6407 ? R.dimen.notification_right_icon_size_low_ram
6408 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006409 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
6410 }
6411 }
6412
6413 /**
6414 * @hide
6415 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006416 public RemoteViews makeBigContentView() {
6417 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01006418 // This covers the following cases:
6419 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006420 // mN.mLargeIcon
6421 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04006422 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07006423 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006424 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006425 oldLargeIcon = mBuilder.mN.mLargeIcon;
6426 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006427 // The legacy largeIcon might not allow us to clear the image, as it's taken in
6428 // replacement if the other one is null. Because we're restoring these legacy icons
6429 // for old listeners, this is in general non-null.
6430 largeIconLegacy = mBuilder.mN.largeIcon;
6431 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006432 }
6433
Selim Cinek384804b2018-04-18 14:31:07 +08006434 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource(),
6435 null /* result */);
Selim Cinek03d0d652015-11-13 13:18:09 -05006436 if (mSummaryTextSet) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006437 contentView.setTextViewText(R.id.text, mBuilder.processTextSpans(
6438 mBuilder.processLegacyText(mSummaryText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006439 mBuilder.setTextViewColorSecondary(contentView, R.id.text);
Selim Cinekc848c3a2016-01-13 15:27:30 -08006440 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05006441 }
Selim Cinek279fa862016-06-14 10:57:25 -07006442 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08006443
Christoph Studer5c510ee2014-12-15 16:32:27 +01006444 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006445 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006446 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006447 }
6448
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006449 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006450 return contentView;
6451 }
6452
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006453 /**
6454 * @hide
6455 */
6456 public void addExtras(Bundle extras) {
6457 super.addExtras(extras);
6458
6459 if (mBigLargeIconSet) {
6460 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
6461 }
6462 extras.putParcelable(EXTRA_PICTURE, mPicture);
6463 }
6464
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006465 /**
6466 * @hide
6467 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006468 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02006469 protected void restoreFromExtras(Bundle extras) {
6470 super.restoreFromExtras(extras);
6471
6472 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01006473 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02006474 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04006475 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006476 mPicture = extras.getParcelable(EXTRA_PICTURE);
6477 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006478
6479 /**
6480 * @hide
6481 */
6482 @Override
6483 public boolean hasSummaryInHeader() {
6484 return false;
6485 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006486
6487 /**
6488 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006489 * Note that we aren't actually comparing the contents of the bitmaps here, so this
6490 * is only doing a cursory inspection. Bitmaps of equal size will appear the same.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006491 */
6492 @Override
6493 public boolean areNotificationsVisiblyDifferent(Style other) {
6494 if (other == null || getClass() != other.getClass()) {
6495 return true;
6496 }
6497 BigPictureStyle otherS = (BigPictureStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006498 return areBitmapsObviouslyDifferent(getBigPicture(), otherS.getBigPicture());
6499 }
6500
6501 private static boolean areBitmapsObviouslyDifferent(Bitmap a, Bitmap b) {
6502 if (a == b) {
6503 return false;
6504 }
6505 if (a == null || b == null) {
6506 return true;
6507 }
6508 return a.getWidth() != b.getWidth()
6509 || a.getHeight() != b.getHeight()
6510 || a.getConfig() != b.getConfig()
6511 || a.getGenerationId() != b.getGenerationId();
Julia Reynolds7217dc92018-03-07 12:12:09 -05006512 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006513 }
6514
6515 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006516 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08006517 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006518 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006519 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006520 * Notification notif = new Notification.Builder(mContext)
6521 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
6522 * .setContentText(subject)
6523 * .setSmallIcon(R.drawable.new_mail)
6524 * .setLargeIcon(aBitmap)
6525 * .setStyle(new Notification.BigTextStyle()
6526 * .bigText(aVeryLongString))
6527 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006528 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006529 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006530 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006531 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006532 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006533
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006534 private CharSequence mBigText;
6535
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006536 public BigTextStyle() {
6537 }
6538
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006539 /**
6540 * @deprecated use {@code BigTextStyle()}.
6541 */
6542 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006543 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006544 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006545 }
6546
Chris Wrend6297db2012-05-03 16:20:13 -04006547 /**
6548 * Overrides ContentTitle in the big form of the template.
6549 * This defaults to the value passed to setContentTitle().
6550 */
6551 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006552 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006553 return this;
6554 }
6555
6556 /**
6557 * Set the first line of text after the detail section in the big form of the template.
6558 */
6559 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006560 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006561 return this;
6562 }
6563
Chris Wren0bd664d2012-08-01 13:56:56 -04006564 /**
6565 * Provide the longer text to be displayed in the big form of the
6566 * template in place of the content text.
6567 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006568 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006569 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006570 return this;
6571 }
6572
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006573 /**
6574 * @hide
6575 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05006576 public CharSequence getBigText() {
6577 return mBigText;
6578 }
6579
6580 /**
6581 * @hide
6582 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006583 public void addExtras(Bundle extras) {
6584 super.addExtras(extras);
6585
Christoph Studer4600f9b2014-07-22 22:44:43 +02006586 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
6587 }
6588
6589 /**
6590 * @hide
6591 */
6592 @Override
6593 protected void restoreFromExtras(Bundle extras) {
6594 super.restoreFromExtras(extras);
6595
6596 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006597 }
6598
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006599 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006600 * @param increasedHeight true if this layout be created with an increased height.
6601 *
6602 * @hide
6603 */
6604 @Override
6605 public RemoteViews makeContentView(boolean increasedHeight) {
6606 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006607 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006608 mBuilder.mActions = new ArrayList<>();
6609 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006610 mBuilder.mActions = mBuilder.mOriginalActions;
6611 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006612 return remoteViews;
6613 }
6614 return super.makeContentView(increasedHeight);
6615 }
6616
6617 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006618 * @hide
6619 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006620 @Override
6621 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
6622 if (increasedHeight && mBuilder.mActions.size() > 0) {
6623 return makeBigContentView();
6624 }
6625 return super.makeHeadsUpContentView(increasedHeight);
6626 }
6627
6628 /**
6629 * @hide
6630 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006631 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02006632
6633 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07006634 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006635 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04006636
Selim Cinek384804b2018-04-18 14:31:07 +08006637 TemplateBindResult result = new TemplateBindResult();
6638 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource(), result);
Selim Cinek1c72fa02018-04-23 18:00:54 +08006639 contentView.setInt(R.id.big_text, "setImageEndMargin", result.getIconMarginEnd());
Joe Malin8d40d042012-11-05 11:36:40 -08006640
Selim Cinek75998782016-04-26 10:39:17 -07006641 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006642
Selim Cinek3a2c4b92015-12-17 17:01:17 -08006643 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07006644 if (TextUtils.isEmpty(bigTextText)) {
6645 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
6646 // experience
6647 bigTextText = mBuilder.processLegacyText(text);
6648 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07006649 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08006650
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006651 return contentView;
6652 }
6653
Julia Reynolds7217dc92018-03-07 12:12:09 -05006654 /**
6655 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006656 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006657 */
6658 @Override
6659 public boolean areNotificationsVisiblyDifferent(Style other) {
6660 if (other == null || getClass() != other.getClass()) {
6661 return true;
6662 }
6663 BigTextStyle newS = (BigTextStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006664 return !Objects.equals(String.valueOf(getBigText()), String.valueOf(newS.getBigText()));
Julia Reynolds7217dc92018-03-07 12:12:09 -05006665 }
6666
Adrian Roosb1f427c2016-05-26 12:27:15 -07006667 static void applyBigTextContentView(Builder builder,
6668 RemoteViews contentView, CharSequence bigTextText) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006669 contentView.setTextViewText(R.id.big_text, builder.processTextSpans(bigTextText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006670 builder.setTextViewColorSecondary(contentView, R.id.big_text);
Adrian Roosb1f427c2016-05-26 12:27:15 -07006671 contentView.setViewVisibility(R.id.big_text,
6672 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek279fa862016-06-14 10:57:25 -07006673 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07006674 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006675 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006676
6677 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006678 * Helper class for generating large-format notifications that include multiple back-and-forth
6679 * messages of varying types between any number of people.
6680 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006681 * <p>
Alex Hillsfc737de2016-03-23 17:33:02 -04006682 * If the platform does not provide large-format notifications, this method has no effect. The
6683 * user will always see the normal notification view.
Selim Cinekce8794f2018-05-23 16:46:05 -07006684 *
6685 * <p>
6686 * If the app is targeting Android P and above, it is required to use the {@link Person}
6687 * class in order to get an optimal rendering of the notification and its avatars. For
6688 * conversations involving multiple people, the app should also make sure that it marks the
6689 * conversation as a group with {@link #setGroupConversation(boolean)}.
6690 *
6691 * <p>
6692 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior.
6693 * Here's an example of how this may be used:
Alex Hillsfc737de2016-03-23 17:33:02 -04006694 * <pre class="prettyprint">
6695 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006696 * Person user = new Person.Builder().setIcon(userIcon).setName(userName).build();
6697 * MessagingStyle style = new MessagingStyle(user)
6698 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getPerson())
6699 * .addMessage(messages[2].getText(), messages[2].getTime(), messages[2].getPerson())
6700 * .setGroupConversation(hasMultiplePeople());
6701 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006702 * Notification noti = new Notification.Builder()
Selim Cinekce8794f2018-05-23 16:46:05 -07006703 * .setContentTitle(&quot;2 new messages with &quot; + sender.toString())
Alex Hillsfc737de2016-03-23 17:33:02 -04006704 * .setContentText(subject)
6705 * .setSmallIcon(R.drawable.new_message)
6706 * .setLargeIcon(aBitmap)
Selim Cinekce8794f2018-05-23 16:46:05 -07006707 * .setStyle(style)
Alex Hillsfc737de2016-03-23 17:33:02 -04006708 * .build();
6709 * </pre>
6710 */
6711 public static class MessagingStyle extends Style {
6712
6713 /**
6714 * The maximum number of messages that will be retained in the Notification itself (the
6715 * number displayed is up to the platform).
6716 */
6717 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
6718
Selim Cinekcb8b9852017-12-15 18:01:52 -08006719 @NonNull Person mUser;
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006720 @Nullable CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04006721 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08006722 List<Message> mHistoricMessages = new ArrayList<>();
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006723 boolean mIsGroupConversation;
Alex Hillsfc737de2016-03-23 17:33:02 -04006724
6725 MessagingStyle() {
6726 }
6727
6728 /**
Alex Hillsfd590442016-10-07 09:52:44 -04006729 * @param userDisplayName Required - the name to be displayed for any replies sent by the
6730 * user before the posting app reposts the notification with those messages after they've
6731 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04006732 * {@link #addMessage(Notification.MessagingStyle.Message)}
Selim Cinekcb8b9852017-12-15 18:01:52 -08006733 *
6734 * @deprecated use {@code MessagingStyle(Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006735 */
Alex Hillsfd590442016-10-07 09:52:44 -04006736 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Selim Cinek9acd6732018-03-23 16:39:02 -07006737 this(new Person.Builder().setName(userDisplayName).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006738 }
6739
6740 /**
6741 * @param user Required - The person displayed for any messages that are sent by the
6742 * user. Any messages added with {@link #addMessage(Notification.MessagingStyle.Message)}
6743 * who don't have a Person associated with it will be displayed as if they were sent
Selim Cinek90343862018-02-01 11:07:11 -08006744 * by this user. The user also needs to have a valid name associated with it, which will
6745 * be enforced starting in Android P.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006746 */
6747 public MessagingStyle(@NonNull Person user) {
6748 mUser = user;
Selim Cinek90343862018-02-01 11:07:11 -08006749 }
6750
6751 /**
6752 * Validate that this style was properly composed. This is called at build time.
6753 * @hide
6754 */
6755 @Override
6756 public void validate(Context context) {
6757 super.validate(context);
6758 if (context.getApplicationInfo().targetSdkVersion
6759 >= Build.VERSION_CODES.P && (mUser == null || mUser.getName() == null)) {
6760 throw new RuntimeException("User must be valid and have a name.");
Selim Cinekcb8b9852017-12-15 18:01:52 -08006761 }
6762 }
6763
6764 /**
koprivaa1a78482018-10-09 10:09:23 -07006765 * @return the text that should be displayed in the statusBar when heads upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006766 * If {@code null} is returned, the default implementation will be used.
6767 *
6768 * @hide
6769 */
6770 @Override
6771 public CharSequence getHeadsUpStatusBarText() {
6772 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
6773 ? super.mBigContentTitle
6774 : mConversationTitle;
6775 if (!TextUtils.isEmpty(conversationTitle) && !hasOnlyWhiteSpaceSenders()) {
6776 return conversationTitle;
6777 }
6778 return null;
6779 }
6780
6781 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08006782 * @return the user to be displayed for any replies sent by the user
6783 */
Selim Cinekeb53c222018-04-06 12:56:09 -07006784 @NonNull
Selim Cinekcb8b9852017-12-15 18:01:52 -08006785 public Person getUser() {
6786 return mUser;
Alex Hillsfc737de2016-03-23 17:33:02 -04006787 }
6788
6789 /**
6790 * Returns the name to be displayed for any replies sent by the user
Selim Cinekcb8b9852017-12-15 18:01:52 -08006791 *
6792 * @deprecated use {@link #getUser()} instead
Alex Hillsfc737de2016-03-23 17:33:02 -04006793 */
6794 public CharSequence getUserDisplayName() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006795 return mUser.getName();
Alex Hillsfc737de2016-03-23 17:33:02 -04006796 }
6797
6798 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006799 * Sets the title to be displayed on this conversation. May be set to {@code null}.
6800 *
Kodlee Yin14656422017-12-22 17:00:46 -08006801 * <p>This API's behavior was changed in SDK version {@link Build.VERSION_CODES#P}. If your
6802 * application's target version is less than {@link Build.VERSION_CODES#P}, setting a
6803 * conversation title to a non-null value will make {@link #isGroupConversation()} return
6804 * {@code true} and passing {@code null} will make it return {@code false}. In
6805 * {@link Build.VERSION_CODES#P} and beyond, use {@link #setGroupConversation(boolean)}
6806 * to set group conversation status.
6807 *
6808 * @param conversationTitle Title displayed for this conversation
6809 * @return this object for method chaining
Alex Hillsfc737de2016-03-23 17:33:02 -04006810 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006811 public MessagingStyle setConversationTitle(@Nullable CharSequence conversationTitle) {
Alex Hillsfc737de2016-03-23 17:33:02 -04006812 mConversationTitle = conversationTitle;
6813 return this;
6814 }
6815
6816 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006817 * Return the title to be displayed on this conversation. May return {@code null}.
Alex Hillsfc737de2016-03-23 17:33:02 -04006818 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006819 @Nullable
Alex Hillsfc737de2016-03-23 17:33:02 -04006820 public CharSequence getConversationTitle() {
6821 return mConversationTitle;
6822 }
6823
6824 /**
6825 * Adds a message for display by this notification. Convenience call for a simple
6826 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6827 * @param text A {@link CharSequence} to be displayed as the message content
6828 * @param timestamp Time at which the message arrived
6829 * @param sender A {@link CharSequence} to be used for displaying the name of the
6830 * sender. Should be <code>null</code> for messages by the current user, in which case
6831 * the platform will insert {@link #getUserDisplayName()}.
6832 * Should be unique amongst all individuals in the conversation, and should be
6833 * consistent during re-posts of the notification.
6834 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07006835 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Alex Hillsfc737de2016-03-23 17:33:02 -04006836 *
6837 * @return this object for method chaining
Selim Cinekcb8b9852017-12-15 18:01:52 -08006838 *
6839 * @deprecated use {@link #addMessage(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006840 */
6841 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006842 return addMessage(text, timestamp,
Selim Cinek9acd6732018-03-23 16:39:02 -07006843 sender == null ? null : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006844 }
6845
6846 /**
6847 * Adds a message for display by this notification. Convenience call for a simple
6848 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6849 * @param text A {@link CharSequence} to be displayed as the message content
6850 * @param timestamp Time at which the message arrived
6851 * @param sender The {@link Person} who sent the message.
6852 * Should be <code>null</code> for messages by the current user, in which case
6853 * the platform will insert the user set in {@code MessagingStyle(Person)}.
6854 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07006855 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Selim Cinekcb8b9852017-12-15 18:01:52 -08006856 *
6857 * @return this object for method chaining
6858 */
Selim Cinekeb53c222018-04-06 12:56:09 -07006859 public MessagingStyle addMessage(@NonNull CharSequence text, long timestamp,
6860 @Nullable Person sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08006861 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04006862 }
6863
6864 /**
6865 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08006866 *
6867 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6868 * the newest last.
6869 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006870 * @param message The {@link Message} to be displayed
6871 * @return this object for method chaining
6872 */
6873 public MessagingStyle addMessage(Message message) {
6874 mMessages.add(message);
6875 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6876 mMessages.remove(0);
6877 }
6878 return this;
6879 }
6880
6881 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006882 * Adds a {@link Message} for historic context in this notification.
6883 *
6884 * <p>Messages should be added as historic if they are not the main subject of the
6885 * notification but may give context to a conversation. The system may choose to present
6886 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
6887 *
6888 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6889 * the newest last.
6890 *
6891 * @param message The historic {@link Message} to be added
6892 * @return this object for method chaining
6893 */
6894 public MessagingStyle addHistoricMessage(Message message) {
6895 mHistoricMessages.add(message);
6896 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6897 mHistoricMessages.remove(0);
6898 }
6899 return this;
6900 }
6901
6902 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006903 * Gets the list of {@code Message} objects that represent the notification
6904 */
6905 public List<Message> getMessages() {
6906 return mMessages;
6907 }
6908
6909 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006910 * Gets the list of historic {@code Message}s in the notification.
6911 */
6912 public List<Message> getHistoricMessages() {
6913 return mHistoricMessages;
6914 }
6915
6916 /**
Selim Cinekce8794f2018-05-23 16:46:05 -07006917 * Sets whether this conversation notification represents a group. If the app is targeting
6918 * Android P, this is required if the app wants to display the largeIcon set with
6919 * {@link Notification.Builder#setLargeIcon(Bitmap)}, otherwise it will be hidden.
Kodlee Yin14656422017-12-22 17:00:46 -08006920 *
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006921 * @param isGroupConversation {@code true} if the conversation represents a group,
6922 * {@code false} otherwise.
6923 * @return this object for method chaining
6924 */
6925 public MessagingStyle setGroupConversation(boolean isGroupConversation) {
6926 mIsGroupConversation = isGroupConversation;
6927 return this;
6928 }
6929
6930 /**
Kodlee Yin14656422017-12-22 17:00:46 -08006931 * Returns {@code true} if this notification represents a group conversation, otherwise
6932 * {@code false}.
6933 *
6934 * <p> If the application that generated this {@link MessagingStyle} targets an SDK version
6935 * less than {@link Build.VERSION_CODES#P}, this method becomes dependent on whether or
6936 * not the conversation title is set; returning {@code true} if the conversation title is
6937 * a non-null value, or {@code false} otherwise. From {@link Build.VERSION_CODES#P} forward,
6938 * this method returns what's set by {@link #setGroupConversation(boolean)} allowing for
6939 * named, non-group conversations.
6940 *
6941 * @see #setConversationTitle(CharSequence)
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006942 */
6943 public boolean isGroupConversation() {
Kodlee Yin14656422017-12-22 17:00:46 -08006944 // When target SDK version is < P, a non-null conversation title dictates if this is
6945 // as group conversation.
6946 if (mBuilder != null
6947 && mBuilder.mContext.getApplicationInfo().targetSdkVersion
6948 < Build.VERSION_CODES.P) {
6949 return mConversationTitle != null;
6950 }
6951
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006952 return mIsGroupConversation;
6953 }
6954
6955 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006956 * @hide
6957 */
6958 @Override
6959 public void addExtras(Bundle extras) {
6960 super.addExtras(extras);
Selim Cinekcb8b9852017-12-15 18:01:52 -08006961 if (mUser != null) {
6962 // For legacy usages
6963 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUser.getName());
6964 extras.putParcelable(EXTRA_MESSAGING_PERSON, mUser);
Alex Hillsfc737de2016-03-23 17:33:02 -04006965 }
6966 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006967 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04006968 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006969 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
6970 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04006971 }
Adrian Roos437cd562017-01-18 15:47:03 -08006972 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
6973 Message.getBundleArrayForMessages(mHistoricMessages));
6974 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006975
6976 fixTitleAndTextExtras(extras);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006977 extras.putBoolean(EXTRA_IS_GROUP_CONVERSATION, mIsGroupConversation);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006978 }
6979
6980 private void fixTitleAndTextExtras(Bundle extras) {
6981 Message m = findLatestIncomingMessage();
6982 CharSequence text = (m == null) ? null : m.mText;
6983 CharSequence sender = m == null ? null
Selim Cinekcb8b9852017-12-15 18:01:52 -08006984 : m.mSender == null || TextUtils.isEmpty(m.mSender.getName())
6985 ? mUser.getName() : m.mSender.getName();
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006986 CharSequence title;
6987 if (!TextUtils.isEmpty(mConversationTitle)) {
6988 if (!TextUtils.isEmpty(sender)) {
6989 BidiFormatter bidi = BidiFormatter.getInstance();
6990 title = mBuilder.mContext.getString(
6991 com.android.internal.R.string.notification_messaging_title_template,
Selim Cinekcb8b9852017-12-15 18:01:52 -08006992 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(sender));
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006993 } else {
6994 title = mConversationTitle;
6995 }
6996 } else {
6997 title = sender;
6998 }
6999
7000 if (title != null) {
7001 extras.putCharSequence(EXTRA_TITLE, title);
7002 }
7003 if (text != null) {
7004 extras.putCharSequence(EXTRA_TEXT, text);
7005 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007006 }
7007
7008 /**
7009 * @hide
7010 */
7011 @Override
7012 protected void restoreFromExtras(Bundle extras) {
7013 super.restoreFromExtras(extras);
7014
Selim Cinekcb8b9852017-12-15 18:01:52 -08007015 mUser = extras.getParcelable(EXTRA_MESSAGING_PERSON);
7016 if (mUser == null) {
7017 CharSequence displayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
Selim Cinek9acd6732018-03-23 16:39:02 -07007018 mUser = new Person.Builder().setName(displayName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007019 }
Adrian Roos96b7e202016-05-17 13:50:38 -07007020 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08007021 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007022 mMessages = Message.getMessagesFromBundleArray(messages);
Adrian Roos437cd562017-01-18 15:47:03 -08007023 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007024 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007025 mIsGroupConversation = extras.getBoolean(EXTRA_IS_GROUP_CONVERSATION);
Alex Hillsfc737de2016-03-23 17:33:02 -04007026 }
7027
7028 /**
7029 * @hide
7030 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07007031 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007032 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07007033 mBuilder.mOriginalActions = mBuilder.mActions;
7034 mBuilder.mActions = new ArrayList<>();
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007035 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007036 false /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007037 mBuilder.mActions = mBuilder.mOriginalActions;
7038 mBuilder.mOriginalActions = null;
7039 return remoteViews;
Adrian Roosc1a80b02016-04-05 14:54:55 -07007040 }
7041
Julia Reynolds7217dc92018-03-07 12:12:09 -05007042 /**
7043 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04007044 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05007045 */
7046 @Override
7047 public boolean areNotificationsVisiblyDifferent(Style other) {
7048 if (other == null || getClass() != other.getClass()) {
7049 return true;
7050 }
7051 MessagingStyle newS = (MessagingStyle) other;
7052 List<MessagingStyle.Message> oldMs = getMessages();
7053 List<MessagingStyle.Message> newMs = newS.getMessages();
7054
Dan Sandler7d67bd42018-05-15 14:06:38 -04007055 if (oldMs == null || newMs == null) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007056 newMs = new ArrayList<>();
7057 }
7058
7059 int n = oldMs.size();
7060 if (n != newMs.size()) {
7061 return true;
7062 }
7063 for (int i = 0; i < n; i++) {
7064 MessagingStyle.Message oldM = oldMs.get(i);
7065 MessagingStyle.Message newM = newMs.get(i);
Dan Sandler7d67bd42018-05-15 14:06:38 -04007066 if (!Objects.equals(
7067 String.valueOf(oldM.getText()),
7068 String.valueOf(newM.getText()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007069 return true;
7070 }
7071 if (!Objects.equals(oldM.getDataUri(), newM.getDataUri())) {
7072 return true;
7073 }
Dan Sandler7d67bd42018-05-15 14:06:38 -04007074 String oldSender = String.valueOf(oldM.getSenderPerson() == null
7075 ? oldM.getSender()
7076 : oldM.getSenderPerson().getName());
7077 String newSender = String.valueOf(newM.getSenderPerson() == null
7078 ? newM.getSender()
7079 : newM.getSenderPerson().getName());
Julia Reynolds7217dc92018-03-07 12:12:09 -05007080 if (!Objects.equals(oldSender, newSender)) {
7081 return true;
7082 }
7083
7084 String oldKey = oldM.getSenderPerson() == null
7085 ? null : oldM.getSenderPerson().getKey();
7086 String newKey = newM.getSenderPerson() == null
7087 ? null : newM.getSenderPerson().getKey();
7088 if (!Objects.equals(oldKey, newKey)) {
7089 return true;
7090 }
7091 // Other fields (like timestamp) intentionally excluded
7092 }
7093 return false;
7094 }
7095
Adrian Roosc1a80b02016-04-05 14:54:55 -07007096 private Message findLatestIncomingMessage() {
Selim Cinek88188f22017-09-19 16:46:56 -07007097 return findLatestIncomingMessage(mMessages);
7098 }
7099
7100 /**
7101 * @hide
7102 */
7103 @Nullable
7104 public static Message findLatestIncomingMessage(
7105 List<Message> messages) {
7106 for (int i = messages.size() - 1; i >= 0; i--) {
7107 Message m = messages.get(i);
Adrian Roosc1a80b02016-04-05 14:54:55 -07007108 // Incoming messages have a non-empty sender.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007109 if (m.mSender != null && !TextUtils.isEmpty(m.mSender.getName())) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07007110 return m;
7111 }
7112 }
Selim Cinek88188f22017-09-19 16:46:56 -07007113 if (!messages.isEmpty()) {
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007114 // No incoming messages, fall back to outgoing message
Selim Cinek88188f22017-09-19 16:46:56 -07007115 return messages.get(messages.size() - 1);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007116 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07007117 return null;
7118 }
7119
7120 /**
7121 * @hide
7122 */
7123 @Override
7124 public RemoteViews makeBigContentView() {
Selim Cinek384804b2018-04-18 14:31:07 +08007125 return makeMessagingView(false /* displayImagesAtEnd */, true /* hideLargeIcon */);
Selim Cinekafeed292017-12-12 17:32:44 -08007126 }
7127
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007128 /**
7129 * Create a messaging layout.
7130 *
7131 * @param displayImagesAtEnd should images be displayed at the end of the content instead
7132 * of inline.
Selim Cinek384804b2018-04-18 14:31:07 +08007133 * @param hideRightIcons Should the reply affordance be shown at the end of the notification
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007134 * @return the created remoteView.
7135 */
Selim Cinekafeed292017-12-12 17:32:44 -08007136 @NonNull
Selim Cinek384804b2018-04-18 14:31:07 +08007137 private RemoteViews makeMessagingView(boolean displayImagesAtEnd, boolean hideRightIcons) {
Selim Cinek88188f22017-09-19 16:46:56 -07007138 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
Adrian Roosc1a80b02016-04-05 14:54:55 -07007139 ? super.mBigContentTitle
7140 : mConversationTitle;
Selim Cinekce8794f2018-05-23 16:46:05 -07007141 boolean atLeastP = mBuilder.mContext.getApplicationInfo().targetSdkVersion
7142 >= Build.VERSION_CODES.P;
7143 boolean isOneToOne;
Selim Cinek2dd3e722018-01-19 11:06:06 -08007144 CharSequence nameReplacement = null;
Selim Cinekce8794f2018-05-23 16:46:05 -07007145 Icon avatarReplacement = null;
7146 if (!atLeastP) {
7147 isOneToOne = TextUtils.isEmpty(conversationTitle);
7148 avatarReplacement = mBuilder.mN.mLargeIcon;
7149 if (hasOnlyWhiteSpaceSenders()) {
7150 isOneToOne = true;
7151 nameReplacement = conversationTitle;
7152 conversationTitle = null;
7153 }
7154 } else {
7155 isOneToOne = !isGroupConversation();
Adrian Roosb1f427c2016-05-26 12:27:15 -07007156 }
Selim Cinek384804b2018-04-18 14:31:07 +08007157 TemplateBindResult bindResult = new TemplateBindResult();
Adrian Roos48d746a2016-04-12 14:57:28 -07007158 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07007159 mBuilder.getMessagingLayoutResource(),
Selim Cinek88188f22017-09-19 16:46:56 -07007160 mBuilder.mParams.reset().hasProgress(false).title(conversationTitle).text(null)
Selim Cinek384804b2018-04-18 14:31:07 +08007161 .hideLargeIcon(hideRightIcons || isOneToOne)
7162 .hideReplyIcon(hideRightIcons)
7163 .headerTextSecondary(conversationTitle),
7164 bindResult);
Selim Cinek88188f22017-09-19 16:46:56 -07007165 addExtras(mBuilder.mN.extras);
Selim Cinekafeed292017-12-12 17:32:44 -08007166 // also update the end margin if there is an image
Selim Cinek1c72fa02018-04-23 18:00:54 +08007167 contentView.setViewLayoutMarginEnd(R.id.notification_messaging,
7168 bindResult.getIconMarginEnd());
Selim Cinek88188f22017-09-19 16:46:56 -07007169 contentView.setInt(R.id.status_bar_latest_event_content, "setLayoutColor",
Kenny Guy14d035c2018-05-02 19:10:36 +01007170 mBuilder.isColorized() ? mBuilder.getPrimaryTextColor()
7171 : mBuilder.resolveContrastColor());
7172 contentView.setInt(R.id.status_bar_latest_event_content, "setSenderTextColor",
7173 mBuilder.getPrimaryTextColor());
7174 contentView.setInt(R.id.status_bar_latest_event_content, "setMessageTextColor",
7175 mBuilder.getSecondaryTextColor());
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007176 contentView.setBoolean(R.id.status_bar_latest_event_content, "setDisplayImagesAtEnd",
7177 displayImagesAtEnd);
Selim Cinekce8794f2018-05-23 16:46:05 -07007178 contentView.setIcon(R.id.status_bar_latest_event_content, "setAvatarReplacement",
7179 avatarReplacement);
Selim Cinek2dd3e722018-01-19 11:06:06 -08007180 contentView.setCharSequence(R.id.status_bar_latest_event_content, "setNameReplacement",
7181 nameReplacement);
Selim Cinek88188f22017-09-19 16:46:56 -07007182 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsOneToOne",
7183 isOneToOne);
7184 contentView.setBundle(R.id.status_bar_latest_event_content, "setData",
7185 mBuilder.mN.extras);
Alex Hillsfc737de2016-03-23 17:33:02 -04007186 return contentView;
7187 }
7188
Selim Cinekf7409db2017-10-24 16:17:14 -07007189 private boolean hasOnlyWhiteSpaceSenders() {
7190 for (int i = 0; i < mMessages.size(); i++) {
7191 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007192 Person sender = m.getSenderPerson();
7193 if (sender != null && !isWhiteSpace(sender.getName())) {
Selim Cinekf7409db2017-10-24 16:17:14 -07007194 return false;
7195 }
7196 }
7197 return true;
7198 }
7199
7200 private boolean isWhiteSpace(CharSequence sender) {
7201 if (TextUtils.isEmpty(sender)) {
7202 return true;
7203 }
7204 if (sender.toString().matches("^\\s*$")) {
7205 return true;
7206 }
7207 // Let's check if we only have 0 whitespace chars. Some apps did this as a workaround
7208 // For the presentation that we had.
7209 for (int i = 0; i < sender.length(); i++) {
7210 char c = sender.charAt(i);
7211 if (c != '\u200B') {
7212 return false;
7213 }
7214 }
7215 return true;
7216 }
7217
Selim Cinek88188f22017-09-19 16:46:56 -07007218 private CharSequence createConversationTitleFromMessages() {
7219 ArraySet<CharSequence> names = new ArraySet<>();
7220 for (int i = 0; i < mMessages.size(); i++) {
7221 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007222 Person sender = m.getSenderPerson();
Selim Cinek88188f22017-09-19 16:46:56 -07007223 if (sender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007224 names.add(sender.getName());
Selim Cinek88188f22017-09-19 16:46:56 -07007225 }
7226 }
7227 SpannableStringBuilder title = new SpannableStringBuilder();
7228 int size = names.size();
7229 for (int i = 0; i < size; i++) {
7230 CharSequence name = names.valueAt(i);
7231 if (!TextUtils.isEmpty(title)) {
7232 title.append(", ");
7233 }
7234 title.append(BidiFormatter.getInstance().unicodeWrap(name));
7235 }
7236 return title;
7237 }
7238
Adrian Roosdedd1df2016-04-26 16:38:47 -07007239 /**
7240 * @hide
7241 */
7242 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007243 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007244 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007245 true /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007246 remoteViews.setInt(R.id.notification_messaging, "setMaxDisplayedLines", 1);
7247 return remoteViews;
Adrian Roosdedd1df2016-04-26 16:38:47 -07007248 }
7249
Adrian Roosc1a80b02016-04-05 14:54:55 -07007250 private static TextAppearanceSpan makeFontColorSpan(int color) {
7251 return new TextAppearanceSpan(null, 0, 0,
7252 ColorStateList.valueOf(color), null);
7253 }
7254
Alex Hillsd9b04d92016-04-11 16:38:16 -04007255 public static final class Message {
Tony Mak09db2ea2018-06-27 18:12:48 +01007256 /** @hide */
7257 public static final String KEY_TEXT = "text";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007258 static final String KEY_TIMESTAMP = "time";
7259 static final String KEY_SENDER = "sender";
Selim Cinekcb8b9852017-12-15 18:01:52 -08007260 static final String KEY_SENDER_PERSON = "sender_person";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007261 static final String KEY_DATA_MIME_TYPE = "type";
7262 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08007263 static final String KEY_EXTRAS_BUNDLE = "extras";
Kenny Guya0f6de82018-04-06 16:20:16 +01007264 static final String KEY_REMOTE_INPUT_HISTORY = "remote_input_history";
Alex Hillsfc737de2016-03-23 17:33:02 -04007265
7266 private final CharSequence mText;
7267 private final long mTimestamp;
Selim Cinekcb8b9852017-12-15 18:01:52 -08007268 @Nullable
7269 private final Person mSender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007270 /** True if this message was generated from the extra
7271 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}
7272 */
7273 private final boolean mRemoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007274
Shane Brennan5a871862017-03-11 13:14:17 -08007275 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007276 private String mDataMimeType;
7277 private Uri mDataUri;
7278
7279 /**
7280 * Constructor
7281 * @param text A {@link CharSequence} to be displayed as the message content
7282 * @param timestamp Time at which the message arrived
7283 * @param sender A {@link CharSequence} to be used for displaying the name of the
7284 * sender. Should be <code>null</code> for messages by the current user, in which case
7285 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
7286 * Should be unique amongst all individuals in the conversation, and should be
7287 * consistent during re-posts of the notification.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007288 *
7289 * @deprecated use {@code Message(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04007290 */
7291 public Message(CharSequence text, long timestamp, CharSequence sender){
Selim Cinek9acd6732018-03-23 16:39:02 -07007292 this(text, timestamp, sender == null ? null
7293 : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08007294 }
7295
7296 /**
7297 * Constructor
7298 * @param text A {@link CharSequence} to be displayed as the message content
7299 * @param timestamp Time at which the message arrived
7300 * @param sender The {@link Person} who sent the message.
7301 * Should be <code>null</code> for messages by the current user, in which case
7302 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7303 * <p>
Selim Cinek9acd6732018-03-23 16:39:02 -07007304 * The person provided should contain an Icon, set with
7305 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7306 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7307 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7308 * to differentiate between the different users.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007309 * </p>
7310 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007311 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender) {
Kenny Guya0f6de82018-04-06 16:20:16 +01007312 this(text, timestamp, sender, false /* remoteHistory */);
7313 }
7314
7315 /**
7316 * Constructor
7317 * @param text A {@link CharSequence} to be displayed as the message content
7318 * @param timestamp Time at which the message arrived
7319 * @param sender The {@link Person} who sent the message.
7320 * Should be <code>null</code> for messages by the current user, in which case
7321 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7322 * @param remoteInputHistory True if the messages was generated from the extra
7323 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7324 * <p>
7325 * The person provided should contain an Icon, set with
7326 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7327 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7328 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7329 * to differentiate between the different users.
7330 * </p>
7331 * @hide
7332 */
7333 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender,
7334 boolean remoteInputHistory) {
Alex Hillsfc737de2016-03-23 17:33:02 -04007335 mText = text;
7336 mTimestamp = timestamp;
7337 mSender = sender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007338 mRemoteInputHistory = remoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007339 }
7340
7341 /**
7342 * Sets a binary blob of data and an associated MIME type for a message. In the case
7343 * where the platform doesn't support the MIME type, the original text provided in the
7344 * constructor will be used.
7345 * @param dataMimeType The MIME type of the content. See
7346 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
7347 * types on Android and Android Wear.
7348 * @param dataUri The uri containing the content whose type is given by the MIME type.
7349 * <p class="note">
7350 * <ol>
7351 * <li>Notification Listeners including the System UI need permission to access the
7352 * data the Uri points to. The recommended ways to do this are:</li>
7353 * <li>Store the data in your own ContentProvider, making sure that other apps have
7354 * the correct permission to access your provider. The preferred mechanism for
7355 * providing access is to use per-URI permissions which are temporary and only
7356 * grant access to the receiving application. An easy way to create a
7357 * ContentProvider like this is to use the FileProvider helper class.</li>
7358 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
7359 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
7360 * also store non-media types (see MediaStore.Files for more info). Files can be
7361 * inserted into the MediaStore using scanFile() after which a content:// style
7362 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
7363 * Note that once added to the system MediaStore the content is accessible to any
7364 * app on the device.</li>
7365 * </ol>
7366 * @return this object for method chaining
7367 */
7368 public Message setData(String dataMimeType, Uri dataUri) {
7369 mDataMimeType = dataMimeType;
7370 mDataUri = dataUri;
7371 return this;
7372 }
7373
Alex Hillsfc737de2016-03-23 17:33:02 -04007374 /**
7375 * Get the text to be used for this message, or the fallback text if a type and content
7376 * Uri have been set
7377 */
7378 public CharSequence getText() {
7379 return mText;
7380 }
7381
7382 /**
7383 * Get the time at which this message arrived
7384 */
7385 public long getTimestamp() {
7386 return mTimestamp;
7387 }
7388
7389 /**
Shane Brennan5a871862017-03-11 13:14:17 -08007390 * Get the extras Bundle for this message.
7391 */
7392 public Bundle getExtras() {
7393 return mExtras;
7394 }
7395
7396 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007397 * Get the text used to display the contact's name in the messaging experience
Selim Cinekcb8b9852017-12-15 18:01:52 -08007398 *
7399 * @deprecated use {@link #getSenderPerson()}
Alex Hillsfc737de2016-03-23 17:33:02 -04007400 */
7401 public CharSequence getSender() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007402 return mSender == null ? null : mSender.getName();
7403 }
7404
7405 /**
7406 * Get the sender associated with this message.
7407 */
7408 @Nullable
7409 public Person getSenderPerson() {
Alex Hillsfc737de2016-03-23 17:33:02 -04007410 return mSender;
7411 }
7412
7413 /**
7414 * Get the MIME type of the data pointed to by the Uri
7415 */
7416 public String getDataMimeType() {
7417 return mDataMimeType;
7418 }
7419
7420 /**
koprivaa1a78482018-10-09 10:09:23 -07007421 * Get the Uri pointing to the content of the message. Can be null, in which case
Alex Hillsfc737de2016-03-23 17:33:02 -04007422 * {@see #getText()} is used.
7423 */
7424 public Uri getDataUri() {
7425 return mDataUri;
7426 }
7427
Kenny Guya0f6de82018-04-06 16:20:16 +01007428 /**
7429 * @return True if the message was generated from
7430 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7431 * @hide
7432 */
7433 public boolean isRemoteInputHistory() {
7434 return mRemoteInputHistory;
7435 }
7436
Alex Hillsd9b04d92016-04-11 16:38:16 -04007437 private Bundle toBundle() {
7438 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007439 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007440 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04007441 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007442 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04007443 if (mSender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007444 // Legacy listeners need this
7445 bundle.putCharSequence(KEY_SENDER, mSender.getName());
7446 bundle.putParcelable(KEY_SENDER_PERSON, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04007447 }
7448 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007449 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04007450 }
7451 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007452 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04007453 }
Shane Brennan5a871862017-03-11 13:14:17 -08007454 if (mExtras != null) {
7455 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
7456 }
Kenny Guya0f6de82018-04-06 16:20:16 +01007457 if (mRemoteInputHistory) {
7458 bundle.putBoolean(KEY_REMOTE_INPUT_HISTORY, mRemoteInputHistory);
7459 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007460 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04007461 }
7462
Alex Hillsd9b04d92016-04-11 16:38:16 -04007463 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
7464 Bundle[] bundles = new Bundle[messages.size()];
7465 final int N = messages.size();
7466 for (int i = 0; i < N; i++) {
7467 bundles[i] = messages.get(i).toBundle();
7468 }
7469 return bundles;
7470 }
7471
Selim Cinek88188f22017-09-19 16:46:56 -07007472 /**
7473 * @return A list of messages read from the bundles.
7474 *
7475 * @hide
7476 */
7477 public static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
7478 if (bundles == null) {
7479 return new ArrayList<>();
7480 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007481 List<Message> messages = new ArrayList<>(bundles.length);
7482 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07007483 if (bundles[i] instanceof Bundle) {
7484 Message message = getMessageFromBundle((Bundle)bundles[i]);
7485 if (message != null) {
7486 messages.add(message);
7487 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007488 }
7489 }
7490 return messages;
7491 }
7492
Selim Cinekb0dc61b2018-05-22 18:49:36 -07007493 /**
7494 * @return The message that is stored in the bundle or null if the message couldn't be
7495 * resolved.
7496 *
7497 * @hide
7498 */
7499 @Nullable
7500 public static Message getMessageFromBundle(Bundle bundle) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007501 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07007502 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007503 return null;
7504 } else {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007505
7506 Person senderPerson = bundle.getParcelable(KEY_SENDER_PERSON);
7507 if (senderPerson == null) {
7508 // Legacy apps that use compat don't actually provide the sender objects
7509 // We need to fix the compat version to provide people / use
7510 // the native api instead
7511 CharSequence senderName = bundle.getCharSequence(KEY_SENDER);
7512 if (senderName != null) {
Selim Cinek9acd6732018-03-23 16:39:02 -07007513 senderPerson = new Person.Builder().setName(senderName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007514 }
7515 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007516 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
Selim Cinekcb8b9852017-12-15 18:01:52 -08007517 bundle.getLong(KEY_TIMESTAMP),
Kenny Guya0f6de82018-04-06 16:20:16 +01007518 senderPerson,
7519 bundle.getBoolean(KEY_REMOTE_INPUT_HISTORY, false));
Alex Hillsd9b04d92016-04-11 16:38:16 -04007520 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
7521 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007522 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
7523 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04007524 }
Shane Brennan5a871862017-03-11 13:14:17 -08007525 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
7526 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
7527 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007528 return message;
7529 }
7530 } catch (ClassCastException e) {
7531 return null;
7532 }
7533 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007534 }
7535 }
7536
7537 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04007538 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08007539 *
Robert Ly91c5ce32014-06-08 15:37:00 -07007540 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04007541 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07007542 * Notification notif = new Notification.Builder(mContext)
7543 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
7544 * .setContentText(subject)
7545 * .setSmallIcon(R.drawable.new_mail)
7546 * .setLargeIcon(aBitmap)
7547 * .setStyle(new Notification.InboxStyle()
7548 * .addLine(str1)
7549 * .addLine(str2)
7550 * .setContentTitle(&quot;&quot;)
7551 * .setSummaryText(&quot;+3 more&quot;))
7552 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04007553 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08007554 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04007555 * @see Notification#bigContentView
7556 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007557 public static class InboxStyle extends Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07007558
7559 /**
7560 * The number of lines of remote input history allowed until we start reducing lines.
7561 */
7562 private static final int NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION = 1;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007563 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
7564
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007565 public InboxStyle() {
7566 }
7567
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007568 /**
7569 * @deprecated use {@code InboxStyle()}.
7570 */
7571 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04007572 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007573 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04007574 }
7575
Chris Wrend6297db2012-05-03 16:20:13 -04007576 /**
7577 * Overrides ContentTitle in the big form of the template.
7578 * This defaults to the value passed to setContentTitle().
7579 */
7580 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007581 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04007582 return this;
7583 }
7584
7585 /**
7586 * Set the first line of text after the detail section in the big form of the template.
7587 */
7588 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007589 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04007590 return this;
7591 }
7592
Chris Wren0bd664d2012-08-01 13:56:56 -04007593 /**
7594 * Append a line to the digest section of the Inbox notification.
7595 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04007596 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007597 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04007598 return this;
7599 }
7600
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007601 /**
7602 * @hide
7603 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05007604 public ArrayList<CharSequence> getLines() {
7605 return mTexts;
7606 }
7607
7608 /**
7609 * @hide
7610 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007611 public void addExtras(Bundle extras) {
7612 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007613
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007614 CharSequence[] a = new CharSequence[mTexts.size()];
7615 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
7616 }
7617
Christoph Studer4600f9b2014-07-22 22:44:43 +02007618 /**
7619 * @hide
7620 */
7621 @Override
7622 protected void restoreFromExtras(Bundle extras) {
7623 super.restoreFromExtras(extras);
7624
7625 mTexts.clear();
7626 if (extras.containsKey(EXTRA_TEXT_LINES)) {
7627 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
7628 }
7629 }
7630
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007631 /**
7632 * @hide
7633 */
7634 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08007635 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02007636 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007637 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
7638 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007639
Selim Cinek384804b2018-04-18 14:31:07 +08007640 TemplateBindResult result = new TemplateBindResult();
7641 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource(), result);
Daniel Sandler619738c2012-06-07 16:33:08 -04007642
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007643 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007644
Chris Wrend6297db2012-05-03 16:20:13 -04007645 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 -04007646 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04007647
Chris Wren4ed80d52012-05-17 09:30:03 -04007648 // Make sure all rows are gone in case we reuse a view.
7649 for (int rowId : rowIds) {
7650 contentView.setViewVisibility(rowId, View.GONE);
7651 }
7652
Daniel Sandler879c5e02012-04-17 16:46:51 -04007653 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07007654 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7655 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08007656 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07007657 int onlyViewId = 0;
7658 int maxRows = rowIds.length;
7659 if (mBuilder.mActions.size() > 0) {
7660 maxRows--;
7661 }
Selim Cinekbee4e072018-05-21 22:06:43 -07007662 CharSequence[] remoteInputHistory = mBuilder.mN.extras.getCharSequenceArray(
7663 EXTRA_REMOTE_INPUT_HISTORY);
7664 if (remoteInputHistory != null
7665 && remoteInputHistory.length > NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION) {
7666 // Let's remove some messages to make room for the remote input history.
7667 // 1 is always able to fit, but let's remove them if they are 2 or 3
7668 int numRemoteInputs = Math.min(remoteInputHistory.length,
7669 MAX_REMOTE_INPUT_HISTORY_LINES);
7670 int totalNumRows = mTexts.size() + numRemoteInputs
7671 - NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION;
7672 if (totalNumRows > maxRows) {
7673 int overflow = totalNumRows - maxRows;
7674 if (mTexts.size() > maxRows) {
7675 // Heuristic: if the Texts don't fit anyway, we'll rather drop the last
7676 // few messages, even with the remote input
7677 maxRows -= overflow;
7678 } else {
7679 // otherwise we drop the first messages
7680 i = overflow;
7681 }
7682 }
7683 }
Selim Cinek07c80172016-04-21 16:40:47 -07007684 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007685 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07007686 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007687 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07007688 contentView.setTextViewText(rowIds[i],
7689 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08007690 mBuilder.setTextViewColorSecondary(contentView, rowIds[i]);
Selim Cinek07c80172016-04-21 16:40:47 -07007691 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek384804b2018-04-18 14:31:07 +08007692 handleInboxImageMargin(contentView, rowIds[i], first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007693 result.getIconMarginEnd());
Selim Cinek07c80172016-04-21 16:40:47 -07007694 if (first) {
7695 onlyViewId = rowIds[i];
7696 } else {
7697 onlyViewId = 0;
7698 }
Selim Cinek247fa012016-02-18 09:50:48 -08007699 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007700 }
7701 i++;
7702 }
Selim Cinek07c80172016-04-21 16:40:47 -07007703 if (onlyViewId != 0) {
7704 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
7705 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7706 R.dimen.notification_text_margin_top);
7707 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
7708 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007709
Daniel Sandler879c5e02012-04-17 16:46:51 -04007710 return contentView;
7711 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007712
Julia Reynolds7217dc92018-03-07 12:12:09 -05007713 /**
7714 * @hide
7715 */
7716 @Override
7717 public boolean areNotificationsVisiblyDifferent(Style other) {
7718 if (other == null || getClass() != other.getClass()) {
7719 return true;
7720 }
7721 InboxStyle newS = (InboxStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04007722
7723 final ArrayList<CharSequence> myLines = getLines();
7724 final ArrayList<CharSequence> newLines = newS.getLines();
7725 final int n = myLines.size();
7726 if (n != newLines.size()) {
7727 return true;
7728 }
7729
7730 for (int i = 0; i < n; i++) {
7731 if (!Objects.equals(
7732 String.valueOf(myLines.get(i)),
7733 String.valueOf(newLines.get(i)))) {
7734 return true;
7735 }
7736 }
7737 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05007738 }
7739
Selim Cinek384804b2018-04-18 14:31:07 +08007740 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007741 int marginEndValue) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08007742 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08007743 if (first) {
7744 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
7745 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
7746 boolean hasProgress = max != 0 || ind;
Selim Cinek384804b2018-04-18 14:31:07 +08007747 if (!hasProgress) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08007748 endMargin = marginEndValue;
Selim Cinek247fa012016-02-18 09:50:48 -08007749 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007750 }
Selim Cinek1c72fa02018-04-23 18:00:54 +08007751 contentView.setViewLayoutMarginEnd(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08007752 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04007753 }
Dan Sandler842dd772014-05-15 09:36:47 -04007754
7755 /**
7756 * Notification style for media playback notifications.
7757 *
7758 * In the expanded form, {@link Notification#bigContentView}, up to 5
7759 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04007760 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04007761 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
7762 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
7763 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08007764 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04007765 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
7766 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01007767 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04007768 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08007769 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007770 * Notifications created with MediaStyle will have their category set to
7771 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
7772 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08007773 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07007774 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
7775 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04007776 * the System UI can identify this as a notification representing an active media session
7777 * and respond accordingly (by showing album artwork in the lockscreen, for example).
7778 *
Selim Cinek99104832017-01-25 14:47:33 -08007779 * <p>
7780 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
7781 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
7782 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
7783 * <p>
7784 *
Dan Sandler842dd772014-05-15 09:36:47 -04007785 * To use this style with your Notification, feed it to
7786 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7787 * <pre class="prettyprint">
7788 * Notification noti = new Notification.Builder()
7789 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01007790 * .setContentTitle(&quot;Track title&quot;)
7791 * .setContentText(&quot;Artist - Album&quot;)
7792 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007793 * .setStyle(<b>new Notification.MediaStyle()</b>
7794 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04007795 * .build();
7796 * </pre>
7797 *
7798 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08007799 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04007800 */
7801 public static class MediaStyle extends Style {
Gus Prevas9cc96602018-10-11 11:24:23 -04007802 // Changing max media buttons requires also changing templates
7803 // (notification_template_material_media and notification_template_material_big_media).
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007804 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04007805 static final int MAX_MEDIA_BUTTONS = 5;
Gus Prevas9cc96602018-10-11 11:24:23 -04007806 @IdRes private static final int[] MEDIA_BUTTON_IDS = {
7807 R.id.action0,
7808 R.id.action1,
7809 R.id.action2,
7810 R.id.action3,
7811 R.id.action4,
7812 };
Dan Sandler842dd772014-05-15 09:36:47 -04007813
7814 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07007815 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04007816
7817 public MediaStyle() {
7818 }
7819
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007820 /**
7821 * @deprecated use {@code MediaStyle()}.
7822 */
7823 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04007824 public MediaStyle(Builder builder) {
7825 setBuilder(builder);
7826 }
7827
7828 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007829 * 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 -04007830 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007831 *
7832 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04007833 */
7834 public MediaStyle setShowActionsInCompactView(int...actions) {
7835 mActionsToShowInCompact = actions;
7836 return this;
7837 }
7838
7839 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07007840 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
7841 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04007842 */
Jeff Browndba34ba2014-06-24 20:46:03 -07007843 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04007844 mToken = token;
7845 return this;
7846 }
7847
Christoph Studer4600f9b2014-07-22 22:44:43 +02007848 /**
7849 * @hide
7850 */
Dan Sandler842dd772014-05-15 09:36:47 -04007851 @Override
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007852 @UnsupportedAppUsage
Dan Sandler842dd772014-05-15 09:36:47 -04007853 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02007854 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007855 if (wip.category == null) {
7856 wip.category = Notification.CATEGORY_TRANSPORT;
7857 }
Dan Sandler842dd772014-05-15 09:36:47 -04007858 return wip;
7859 }
7860
Christoph Studer4600f9b2014-07-22 22:44:43 +02007861 /**
7862 * @hide
7863 */
7864 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007865 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007866 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007867 }
7868
7869 /**
7870 * @hide
7871 */
7872 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007873 public RemoteViews makeBigContentView() {
7874 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007875 }
7876
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007877 /**
7878 * @hide
7879 */
7880 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007881 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007882 RemoteViews expanded = makeMediaBigContentView();
7883 return expanded != null ? expanded : makeMediaContentView();
7884 }
7885
Dan Sandler842dd772014-05-15 09:36:47 -04007886 /** @hide */
7887 @Override
7888 public void addExtras(Bundle extras) {
7889 super.addExtras(extras);
7890
7891 if (mToken != null) {
7892 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
7893 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01007894 if (mActionsToShowInCompact != null) {
7895 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
7896 }
Dan Sandler842dd772014-05-15 09:36:47 -04007897 }
7898
Christoph Studer4600f9b2014-07-22 22:44:43 +02007899 /**
7900 * @hide
7901 */
7902 @Override
7903 protected void restoreFromExtras(Bundle extras) {
7904 super.restoreFromExtras(extras);
7905
7906 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
7907 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
7908 }
7909 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
7910 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
7911 }
7912 }
7913
Julia Reynolds7217dc92018-03-07 12:12:09 -05007914 /**
7915 * @hide
7916 */
7917 @Override
7918 public boolean areNotificationsVisiblyDifferent(Style other) {
7919 if (other == null || getClass() != other.getClass()) {
7920 return true;
7921 }
7922 // All fields to compare are on the Notification object
7923 return false;
7924 }
7925
Gus Prevas9cc96602018-10-11 11:24:23 -04007926 private void bindMediaActionButton(RemoteViews container, @IdRes int buttonId,
7927 Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04007928 final boolean tombstone = (action.actionIntent == null);
Gus Prevas9cc96602018-10-11 11:24:23 -04007929 container.setViewVisibility(buttonId, View.VISIBLE);
7930 container.setImageViewIcon(buttonId, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07007931
7932 // If the action buttons should not be tinted, then just use the default
7933 // notification color. Otherwise, just use the passed-in color.
Gus Prevas9cc96602018-10-11 11:24:23 -04007934 Resources resources = mBuilder.mContext.getResources();
7935 Configuration currentConfig = resources.getConfiguration();
Lucas Dupinf03e7522018-06-25 16:21:13 -07007936 boolean inNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
7937 == Configuration.UI_MODE_NIGHT_YES;
Anthony Chenad4d1582017-04-10 16:07:58 -07007938 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized()
7939 ? color
Lucas Dupina291d192018-06-07 13:59:42 -07007940 : ContrastColorUtil.resolveColor(mBuilder.mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07007941 Notification.COLOR_DEFAULT, inNightMode);
Anthony Chenad4d1582017-04-10 16:07:58 -07007942
Gus Prevas9cc96602018-10-11 11:24:23 -04007943 container.setDrawableTint(buttonId, false, tintColor,
Sunny Goyal5b153922017-09-21 21:00:36 -07007944 PorterDuff.Mode.SRC_ATOP);
Gus Prevas9cc96602018-10-11 11:24:23 -04007945
7946 final TypedArray typedArray = mBuilder.mContext.obtainStyledAttributes(
7947 new int[]{ android.R.attr.colorControlHighlight });
7948 int rippleAlpha = Color.alpha(typedArray.getColor(0, 0));
7949 typedArray.recycle();
7950 int rippleColor = Color.argb(rippleAlpha, Color.red(tintColor), Color.green(tintColor),
7951 Color.blue(tintColor));
7952 container.setRippleDrawableColor(buttonId, ColorStateList.valueOf(rippleColor));
7953
Dan Sandler842dd772014-05-15 09:36:47 -04007954 if (!tombstone) {
Gus Prevas9cc96602018-10-11 11:24:23 -04007955 container.setOnClickPendingIntent(buttonId, action.actionIntent);
Dan Sandler842dd772014-05-15 09:36:47 -04007956 }
Gus Prevas9cc96602018-10-11 11:24:23 -04007957 container.setContentDescription(buttonId, action.title);
Dan Sandler842dd772014-05-15 09:36:47 -04007958 }
7959
7960 private RemoteViews makeMediaContentView() {
7961 RemoteViews view = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08007962 R.layout.notification_template_material_media, false, /* hasProgress */
7963 null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04007964
7965 final int numActions = mBuilder.mActions.size();
Gus Prevas9cc96602018-10-11 11:24:23 -04007966 final int numActionsToShow = mActionsToShowInCompact == null
Dan Sandler842dd772014-05-15 09:36:47 -04007967 ? 0
7968 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Gus Prevas9cc96602018-10-11 11:24:23 -04007969 if (numActionsToShow > numActions) {
7970 throw new IllegalArgumentException(String.format(
7971 "setShowActionsInCompactView: action %d out of bounds (max %d)",
7972 numActions, numActions - 1));
7973 }
7974 for (int i = 0; i < MAX_MEDIA_BUTTONS_IN_COMPACT; i++) {
7975 if (i < numActionsToShow) {
Dan Sandler842dd772014-05-15 09:36:47 -04007976 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Gus Prevas9cc96602018-10-11 11:24:23 -04007977 bindMediaActionButton(view, MEDIA_BUTTON_IDS[i], action, getActionColor());
7978 } else {
7979 view.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04007980 }
7981 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08007982 handleImage(view);
7983 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007984 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07007985 if (mBuilder.mN.hasLargeIcon()) {
Selim Cinek384804b2018-04-18 14:31:07 +08007986 endMargin = R.dimen.notification_media_image_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08007987 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007988 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04007989 return view;
7990 }
7991
Selim Cinekc7f5a822018-03-20 19:32:06 -07007992 private int getActionColor() {
7993 return mBuilder.isColorized() ? mBuilder.getPrimaryTextColor()
7994 : mBuilder.resolveContrastColor();
Selim Cinek99104832017-01-25 14:47:33 -08007995 }
7996
Dan Sandler842dd772014-05-15 09:36:47 -04007997 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007998 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007999 // Dont add an expanded view if there is no more content to be revealed
8000 int actionsInCompact = mActionsToShowInCompact == null
8001 ? 0
8002 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07008003 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008004 return null;
8005 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05008006 RemoteViews big = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08008007 R.layout.notification_template_material_big_media, false, null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04008008
Gus Prevas9cc96602018-10-11 11:24:23 -04008009 for (int i = 0; i < MAX_MEDIA_BUTTONS; i++) {
8010 if (i < actionCount) {
8011 bindMediaActionButton(big, MEDIA_BUTTON_IDS[i], mBuilder.mActions.get(i),
Selim Cinekc7f5a822018-03-20 19:32:06 -07008012 getActionColor());
Gus Prevas9cc96602018-10-11 11:24:23 -04008013 } else {
8014 big.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04008015 }
8016 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05008017 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04008018 return big;
8019 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008020
Selim Cinek5bf069a2015-11-10 19:14:27 -05008021 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07008022 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008023 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
8024 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008025 }
8026 }
8027
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008028 /**
8029 * @hide
8030 */
8031 @Override
8032 protected boolean hasProgress() {
8033 return false;
8034 }
Dan Sandler842dd772014-05-15 09:36:47 -04008035 }
Griff Hazen61a9e862014-05-22 16:05:19 -07008036
Selim Cinek593610c2016-02-16 18:42:57 -08008037 /**
8038 * Notification style for custom views that are decorated by the system
8039 *
8040 * <p>Instead of providing a notification that is completely custom, a developer can set this
8041 * style and still obtain system decorations like the notification header with the expand
8042 * affordance and actions.
8043 *
8044 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8045 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8046 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8047 * corresponding custom views to display.
8048 *
8049 * To use this style with your Notification, feed it to
8050 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8051 * <pre class="prettyprint">
8052 * Notification noti = new Notification.Builder()
8053 * .setSmallIcon(R.drawable.ic_stat_player)
8054 * .setLargeIcon(albumArtBitmap))
8055 * .setCustomContentView(contentView);
8056 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
8057 * .build();
8058 * </pre>
8059 */
8060 public static class DecoratedCustomViewStyle extends Style {
8061
8062 public DecoratedCustomViewStyle() {
8063 }
8064
Selim Cinek593610c2016-02-16 18:42:57 -08008065 /**
8066 * @hide
8067 */
8068 public boolean displayCustomViewInline() {
8069 return true;
8070 }
8071
8072 /**
8073 * @hide
8074 */
8075 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008076 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008077 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
8078 }
8079
8080 /**
8081 * @hide
8082 */
8083 @Override
8084 public RemoteViews makeBigContentView() {
8085 return makeDecoratedBigContentView();
8086 }
8087
8088 /**
8089 * @hide
8090 */
8091 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008092 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008093 return makeDecoratedHeadsUpContentView();
8094 }
8095
Selim Cinek593610c2016-02-16 18:42:57 -08008096 private RemoteViews makeDecoratedHeadsUpContentView() {
8097 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
8098 ? mBuilder.mN.contentView
8099 : mBuilder.mN.headsUpContentView;
8100 if (mBuilder.mActions.size() == 0) {
8101 return makeStandardTemplateWithCustomContent(headsUpContentView);
8102 }
Selim Cinek384804b2018-04-18 14:31:07 +08008103 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008104 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008105 mBuilder.getBigBaseLayoutResource(), result);
8106 buildIntoRemoteViewContent(remoteViews, headsUpContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008107 return remoteViews;
8108 }
8109
Selim Cinek593610c2016-02-16 18:42:57 -08008110 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
Selim Cinek384804b2018-04-18 14:31:07 +08008111 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008112 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08008113 mBuilder.getBaseLayoutResource(), result);
8114 buildIntoRemoteViewContent(remoteViews, customContent, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008115 return remoteViews;
8116 }
8117
Selim Cinek593610c2016-02-16 18:42:57 -08008118 private RemoteViews makeDecoratedBigContentView() {
8119 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
8120 ? mBuilder.mN.contentView
8121 : mBuilder.mN.bigContentView;
8122 if (mBuilder.mActions.size() == 0) {
8123 return makeStandardTemplateWithCustomContent(bigContentView);
8124 }
Selim Cinek384804b2018-04-18 14:31:07 +08008125 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008126 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008127 mBuilder.getBigBaseLayoutResource(), result);
8128 buildIntoRemoteViewContent(remoteViews, bigContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008129 return remoteViews;
8130 }
Selim Cinek247fa012016-02-18 09:50:48 -08008131
8132 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
Selim Cinek384804b2018-04-18 14:31:07 +08008133 RemoteViews customContent, TemplateBindResult result) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08008134 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008135 // Need to clone customContent before adding, because otherwise it can no longer be
8136 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008137 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07008138 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
8139 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Selim Cinekfc8073c2017-08-16 17:50:20 -07008140 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08008141 }
Selim Cinek247fa012016-02-18 09:50:48 -08008142 // also update the end margin if there is an image
Selim Cinek384804b2018-04-18 14:31:07 +08008143 Resources resources = mBuilder.mContext.getResources();
8144 int endMargin = resources.getDimensionPixelSize(
Selim Cinek1c72fa02018-04-23 18:00:54 +08008145 R.dimen.notification_content_margin_end) + result.getIconMarginEnd();
Selim Cinek384804b2018-04-18 14:31:07 +08008146 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08008147 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05008148
8149 /**
8150 * @hide
8151 */
8152 @Override
8153 public boolean areNotificationsVisiblyDifferent(Style other) {
8154 if (other == null || getClass() != other.getClass()) {
8155 return true;
8156 }
8157 // Comparison done for all custom RemoteViews, independent of style
8158 return false;
8159 }
Selim Cinek593610c2016-02-16 18:42:57 -08008160 }
8161
Selim Cinek03eb3b72016-02-18 10:39:45 -08008162 /**
8163 * Notification style for media custom views that are decorated by the system
8164 *
8165 * <p>Instead of providing a media notification that is completely custom, a developer can set
8166 * this style and still obtain system decorations like the notification header with the expand
8167 * affordance and actions.
8168 *
8169 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8170 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8171 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8172 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08008173 * <p>
8174 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
8175 * notification by using {@link Notification.Builder#setColorized(boolean)}.
8176 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08008177 * To use this style with your Notification, feed it to
8178 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8179 * <pre class="prettyprint">
8180 * Notification noti = new Notification.Builder()
8181 * .setSmallIcon(R.drawable.ic_stat_player)
8182 * .setLargeIcon(albumArtBitmap))
8183 * .setCustomContentView(contentView);
8184 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
8185 * .setMediaSession(mySession))
8186 * .build();
8187 * </pre>
8188 *
8189 * @see android.app.Notification.DecoratedCustomViewStyle
8190 * @see android.app.Notification.MediaStyle
8191 */
8192 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
8193
8194 public DecoratedMediaCustomViewStyle() {
8195 }
8196
Selim Cinek03eb3b72016-02-18 10:39:45 -08008197 /**
8198 * @hide
8199 */
8200 public boolean displayCustomViewInline() {
8201 return true;
8202 }
8203
8204 /**
8205 * @hide
8206 */
8207 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008208 public RemoteViews makeContentView(boolean increasedHeight) {
8209 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008210 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8211 mBuilder.mN.contentView);
8212 }
8213
8214 /**
8215 * @hide
8216 */
8217 @Override
8218 public RemoteViews makeBigContentView() {
8219 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
8220 ? mBuilder.mN.bigContentView
8221 : mBuilder.mN.contentView;
8222 return makeBigContentViewWithCustomContent(customRemoteView);
8223 }
8224
8225 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
8226 RemoteViews remoteViews = super.makeBigContentView();
8227 if (remoteViews != null) {
8228 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
8229 customRemoteView);
8230 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08008231 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008232 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8233 customRemoteView);
8234 } else {
8235 return null;
8236 }
8237 }
8238
8239 /**
8240 * @hide
8241 */
8242 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008243 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08008244 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
8245 ? mBuilder.mN.headsUpContentView
8246 : mBuilder.mN.contentView;
8247 return makeBigContentViewWithCustomContent(customRemoteView);
8248 }
8249
Julia Reynolds7217dc92018-03-07 12:12:09 -05008250 /**
8251 * @hide
8252 */
8253 @Override
8254 public boolean areNotificationsVisiblyDifferent(Style other) {
8255 if (other == null || getClass() != other.getClass()) {
8256 return true;
8257 }
8258 // Comparison done for all custom RemoteViews, independent of style
8259 return false;
8260 }
8261
Selim Cinek03eb3b72016-02-18 10:39:45 -08008262 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
8263 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08008264 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008265 // Need to clone customContent before adding, because otherwise it can no longer be
8266 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008267 customContent = customContent.clone();
Selim Cinek87c31532017-08-18 18:53:44 -07008268 customContent.overrideTextColors(mBuilder.getPrimaryTextColor());
Selim Cinekf91017e2016-03-14 12:25:09 -07008269 remoteViews.removeAllViews(id);
8270 remoteViews.addView(id, customContent);
Selim Cinekfc8073c2017-08-16 17:50:20 -07008271 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08008272 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08008273 return remoteViews;
8274 }
8275 }
8276
Christoph Studer4600f9b2014-07-22 22:44:43 +02008277 // When adding a new Style subclass here, don't forget to update
8278 // Builder.getNotificationStyleClass.
8279
Griff Hazen61a9e862014-05-22 16:05:19 -07008280 /**
8281 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
8282 * metadata or change options on a notification builder.
8283 */
8284 public interface Extender {
8285 /**
8286 * Apply this extender to a notification builder.
8287 * @param builder the builder to be modified.
8288 * @return the build object for chaining.
8289 */
8290 public Builder extend(Builder builder);
8291 }
8292
8293 /**
8294 * Helper class to add wearable extensions to notifications.
8295 * <p class="note"> See
8296 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
8297 * for Android Wear</a> for more information on how to use this class.
8298 * <p>
8299 * To create a notification with wearable extensions:
8300 * <ol>
8301 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
8302 * properties.
8303 * <li>Create a {@link android.app.Notification.WearableExtender}.
8304 * <li>Set wearable-specific properties using the
8305 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
8306 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
8307 * notification.
8308 * <li>Post the notification to the notification system with the
8309 * {@code NotificationManager.notify(...)} methods.
8310 * </ol>
8311 *
8312 * <pre class="prettyprint">
8313 * Notification notif = new Notification.Builder(mContext)
8314 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
8315 * .setContentText(subject)
8316 * .setSmallIcon(R.drawable.new_mail)
8317 * .extend(new Notification.WearableExtender()
8318 * .setContentIcon(R.drawable.new_mail))
8319 * .build();
8320 * NotificationManager notificationManger =
8321 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
8322 * notificationManger.notify(0, notif);</pre>
8323 *
8324 * <p>Wearable extensions can be accessed on an existing notification by using the
8325 * {@code WearableExtender(Notification)} constructor,
8326 * and then using the {@code get} methods to access values.
8327 *
8328 * <pre class="prettyprint">
8329 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
8330 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07008331 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008332 */
8333 public static final class WearableExtender implements Extender {
8334 /**
8335 * Sentinel value for an action index that is unset.
8336 */
8337 public static final int UNSET_ACTION_INDEX = -1;
8338
8339 /**
8340 * Size value for use with {@link #setCustomSizePreset} to show this notification with
8341 * default sizing.
8342 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07008343 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07008344 * on their content.
Gus Prevasd7363752018-09-18 14:35:15 -04008345 *
8346 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008347 */
Gus Prevasd7363752018-09-18 14:35:15 -04008348 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008349 public static final int SIZE_DEFAULT = 0;
8350
8351 /**
8352 * Size value for use with {@link #setCustomSizePreset} to show this notification
8353 * with an extra small size.
8354 * <p>This value is only applicable for custom display notifications created using
8355 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008356 *
8357 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008358 */
Gus Prevasd7363752018-09-18 14:35:15 -04008359 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008360 public static final int SIZE_XSMALL = 1;
8361
8362 /**
8363 * Size value for use with {@link #setCustomSizePreset} to show this notification
8364 * with a small size.
8365 * <p>This value is only applicable for custom display notifications created using
8366 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008367 *
8368 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008369 */
Gus Prevasd7363752018-09-18 14:35:15 -04008370 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008371 public static final int SIZE_SMALL = 2;
8372
8373 /**
8374 * Size value for use with {@link #setCustomSizePreset} to show this notification
8375 * with a medium size.
8376 * <p>This value is only applicable for custom display notifications created using
8377 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008378 *
8379 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008380 */
Gus Prevasd7363752018-09-18 14:35:15 -04008381 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008382 public static final int SIZE_MEDIUM = 3;
8383
8384 /**
8385 * Size value for use with {@link #setCustomSizePreset} to show this notification
8386 * with a large size.
8387 * <p>This value is only applicable for custom display notifications created using
8388 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008389 *
8390 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008391 */
Gus Prevasd7363752018-09-18 14:35:15 -04008392 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008393 public static final int SIZE_LARGE = 4;
8394
Griff Hazend5f11f92014-05-27 15:40:09 -07008395 /**
8396 * Size value for use with {@link #setCustomSizePreset} to show this notification
8397 * full screen.
8398 * <p>This value is only applicable for custom display notifications created using
8399 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008400 *
8401 * @deprecated Display intents are no longer supported.
Griff Hazend5f11f92014-05-27 15:40:09 -07008402 */
Gus Prevasd7363752018-09-18 14:35:15 -04008403 @Deprecated
Griff Hazend5f11f92014-05-27 15:40:09 -07008404 public static final int SIZE_FULL_SCREEN = 5;
8405
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008406 /**
8407 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
8408 * short amount of time when this notification is displayed on the screen. This
8409 * is the default value.
Gus Prevasd7363752018-09-18 14:35:15 -04008410 *
8411 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008412 */
Gus Prevasd7363752018-09-18 14:35:15 -04008413 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008414 public static final int SCREEN_TIMEOUT_SHORT = 0;
8415
8416 /**
8417 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
8418 * for a longer amount of time when this notification is displayed on the screen.
Gus Prevasd7363752018-09-18 14:35:15 -04008419 *
8420 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008421 */
Gus Prevasd7363752018-09-18 14:35:15 -04008422 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008423 public static final int SCREEN_TIMEOUT_LONG = -1;
8424
Griff Hazen61a9e862014-05-22 16:05:19 -07008425 /** Notification extra which contains wearable extensions */
8426 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
8427
Pete Gastaf6781d2014-10-07 15:17:05 -04008428 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07008429 private static final String KEY_ACTIONS = "actions";
8430 private static final String KEY_FLAGS = "flags";
8431 private static final String KEY_DISPLAY_INTENT = "displayIntent";
8432 private static final String KEY_PAGES = "pages";
8433 private static final String KEY_BACKGROUND = "background";
8434 private static final String KEY_CONTENT_ICON = "contentIcon";
8435 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
8436 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
8437 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
8438 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
8439 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008440 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04008441 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008442 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07008443
8444 // Flags bitwise-ored to mFlags
8445 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
8446 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
8447 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
8448 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008449 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04008450 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04008451 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07008452
8453 // Default value for flags integer
8454 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
8455
8456 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
8457 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
8458
8459 private ArrayList<Action> mActions = new ArrayList<Action>();
8460 private int mFlags = DEFAULT_FLAGS;
8461 private PendingIntent mDisplayIntent;
8462 private ArrayList<Notification> mPages = new ArrayList<Notification>();
8463 private Bitmap mBackground;
8464 private int mContentIcon;
8465 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
8466 private int mContentActionIndex = UNSET_ACTION_INDEX;
8467 private int mCustomSizePreset = SIZE_DEFAULT;
8468 private int mCustomContentHeight;
8469 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008470 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04008471 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008472 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07008473
8474 /**
8475 * Create a {@link android.app.Notification.WearableExtender} with default
8476 * options.
8477 */
8478 public WearableExtender() {
8479 }
8480
8481 public WearableExtender(Notification notif) {
8482 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
8483 if (wearableBundle != null) {
8484 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
8485 if (actions != null) {
8486 mActions.addAll(actions);
8487 }
8488
8489 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
8490 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
8491
8492 Notification[] pages = getNotificationArrayFromBundle(
8493 wearableBundle, KEY_PAGES);
8494 if (pages != null) {
8495 Collections.addAll(mPages, pages);
8496 }
8497
8498 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
8499 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
8500 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
8501 DEFAULT_CONTENT_ICON_GRAVITY);
8502 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
8503 UNSET_ACTION_INDEX);
8504 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
8505 SIZE_DEFAULT);
8506 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
8507 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008508 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04008509 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008510 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07008511 }
8512 }
8513
8514 /**
8515 * Apply wearable extensions to a notification that is being built. This is typically
8516 * called by the {@link android.app.Notification.Builder#extend} method of
8517 * {@link android.app.Notification.Builder}.
8518 */
8519 @Override
8520 public Notification.Builder extend(Notification.Builder builder) {
8521 Bundle wearableBundle = new Bundle();
8522
8523 if (!mActions.isEmpty()) {
8524 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
8525 }
8526 if (mFlags != DEFAULT_FLAGS) {
8527 wearableBundle.putInt(KEY_FLAGS, mFlags);
8528 }
8529 if (mDisplayIntent != null) {
8530 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
8531 }
8532 if (!mPages.isEmpty()) {
8533 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
8534 new Notification[mPages.size()]));
8535 }
8536 if (mBackground != null) {
8537 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
8538 }
8539 if (mContentIcon != 0) {
8540 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
8541 }
8542 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
8543 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
8544 }
8545 if (mContentActionIndex != UNSET_ACTION_INDEX) {
8546 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
8547 mContentActionIndex);
8548 }
8549 if (mCustomSizePreset != SIZE_DEFAULT) {
8550 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
8551 }
8552 if (mCustomContentHeight != 0) {
8553 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
8554 }
8555 if (mGravity != DEFAULT_GRAVITY) {
8556 wearableBundle.putInt(KEY_GRAVITY, mGravity);
8557 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008558 if (mHintScreenTimeout != 0) {
8559 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
8560 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04008561 if (mDismissalId != null) {
8562 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
8563 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008564 if (mBridgeTag != null) {
8565 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
8566 }
Griff Hazen61a9e862014-05-22 16:05:19 -07008567
8568 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
8569 return builder;
8570 }
8571
8572 @Override
8573 public WearableExtender clone() {
8574 WearableExtender that = new WearableExtender();
8575 that.mActions = new ArrayList<Action>(this.mActions);
8576 that.mFlags = this.mFlags;
8577 that.mDisplayIntent = this.mDisplayIntent;
8578 that.mPages = new ArrayList<Notification>(this.mPages);
8579 that.mBackground = this.mBackground;
8580 that.mContentIcon = this.mContentIcon;
8581 that.mContentIconGravity = this.mContentIconGravity;
8582 that.mContentActionIndex = this.mContentActionIndex;
8583 that.mCustomSizePreset = this.mCustomSizePreset;
8584 that.mCustomContentHeight = this.mCustomContentHeight;
8585 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008586 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04008587 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008588 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07008589 return that;
8590 }
8591
8592 /**
8593 * Add a wearable action to this notification.
8594 *
8595 * <p>When wearable actions are added using this method, the set of actions that
8596 * show on a wearable device splits from devices that only show actions added
8597 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
8598 * of which actions display on different devices.
8599 *
8600 * @param action the action to add to this notification
8601 * @return this object for method chaining
8602 * @see android.app.Notification.Action
8603 */
8604 public WearableExtender addAction(Action action) {
8605 mActions.add(action);
8606 return this;
8607 }
8608
8609 /**
8610 * Adds wearable actions to this notification.
8611 *
8612 * <p>When wearable actions are added using this method, the set of actions that
8613 * show on a wearable device splits from devices that only show actions added
8614 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
8615 * of which actions display on different devices.
8616 *
8617 * @param actions the actions to add to this notification
8618 * @return this object for method chaining
8619 * @see android.app.Notification.Action
8620 */
8621 public WearableExtender addActions(List<Action> actions) {
8622 mActions.addAll(actions);
8623 return this;
8624 }
8625
8626 /**
8627 * Clear all wearable actions present on this builder.
8628 * @return this object for method chaining.
8629 * @see #addAction
8630 */
8631 public WearableExtender clearActions() {
8632 mActions.clear();
8633 return this;
8634 }
8635
8636 /**
8637 * Get the wearable actions present on this notification.
8638 */
8639 public List<Action> getActions() {
8640 return mActions;
8641 }
8642
8643 /**
8644 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07008645 * this notification. The {@link PendingIntent} provided should be for an activity.
8646 *
8647 * <pre class="prettyprint">
8648 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
8649 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
8650 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
8651 * Notification notif = new Notification.Builder(context)
8652 * .extend(new Notification.WearableExtender()
8653 * .setDisplayIntent(displayPendingIntent)
8654 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
8655 * .build();</pre>
8656 *
8657 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07008658 * should have an empty task affinity. It is also recommended to use the device
8659 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07008660 *
8661 * <p>Example AndroidManifest.xml entry:
8662 * <pre class="prettyprint">
8663 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
8664 * android:exported=&quot;true&quot;
8665 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07008666 * android:taskAffinity=&quot;&quot;
8667 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008668 *
8669 * @param intent the {@link PendingIntent} for an activity
8670 * @return this object for method chaining
8671 * @see android.app.Notification.WearableExtender#getDisplayIntent
Gus Prevasd7363752018-09-18 14:35:15 -04008672 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008673 */
Gus Prevasd7363752018-09-18 14:35:15 -04008674 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008675 public WearableExtender setDisplayIntent(PendingIntent intent) {
8676 mDisplayIntent = intent;
8677 return this;
8678 }
8679
8680 /**
8681 * Get the intent to launch inside of an activity view when displaying this
8682 * notification. This {@code PendingIntent} should be for an activity.
Gus Prevasd7363752018-09-18 14:35:15 -04008683 *
8684 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008685 */
Gus Prevasd7363752018-09-18 14:35:15 -04008686 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008687 public PendingIntent getDisplayIntent() {
8688 return mDisplayIntent;
8689 }
8690
8691 /**
8692 * Add an additional page of content to display with this notification. The current
8693 * notification forms the first page, and pages added using this function form
8694 * subsequent pages. This field can be used to separate a notification into multiple
8695 * sections.
8696 *
8697 * @param page the notification to add as another page
8698 * @return this object for method chaining
8699 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04008700 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008701 */
Gus Prevasd7363752018-09-18 14:35:15 -04008702 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008703 public WearableExtender addPage(Notification page) {
8704 mPages.add(page);
8705 return this;
8706 }
8707
8708 /**
8709 * Add additional pages of content to display with this notification. The current
8710 * notification forms the first page, and pages added using this function form
8711 * subsequent pages. This field can be used to separate a notification into multiple
8712 * sections.
8713 *
8714 * @param pages a list of notifications
8715 * @return this object for method chaining
8716 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04008717 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008718 */
Gus Prevasd7363752018-09-18 14:35:15 -04008719 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008720 public WearableExtender addPages(List<Notification> pages) {
8721 mPages.addAll(pages);
8722 return this;
8723 }
8724
8725 /**
8726 * Clear all additional pages present on this builder.
8727 * @return this object for method chaining.
8728 * @see #addPage
Gus Prevasd7363752018-09-18 14:35:15 -04008729 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008730 */
Gus Prevasd7363752018-09-18 14:35:15 -04008731 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008732 public WearableExtender clearPages() {
8733 mPages.clear();
8734 return this;
8735 }
8736
8737 /**
8738 * Get the array of additional pages of content for displaying this notification. The
8739 * current notification forms the first page, and elements within this array form
8740 * subsequent pages. This field can be used to separate a notification into multiple
8741 * sections.
8742 * @return the pages for this notification
Gus Prevasd7363752018-09-18 14:35:15 -04008743 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008744 */
Gus Prevasd7363752018-09-18 14:35:15 -04008745 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008746 public List<Notification> getPages() {
8747 return mPages;
8748 }
8749
8750 /**
8751 * Set a background image to be displayed behind the notification content.
8752 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8753 * will work with any notification style.
8754 *
8755 * @param background the background bitmap
8756 * @return this object for method chaining
8757 * @see android.app.Notification.WearableExtender#getBackground
Gus Prevasd7363752018-09-18 14:35:15 -04008758 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008759 */
Gus Prevasd7363752018-09-18 14:35:15 -04008760 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008761 public WearableExtender setBackground(Bitmap background) {
8762 mBackground = background;
8763 return this;
8764 }
8765
8766 /**
8767 * Get a background image to be displayed behind the notification content.
8768 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8769 * will work with any notification style.
8770 *
8771 * @return the background image
8772 * @see android.app.Notification.WearableExtender#setBackground
Gus Prevasd7363752018-09-18 14:35:15 -04008773 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008774 */
Gus Prevasd7363752018-09-18 14:35:15 -04008775 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008776 public Bitmap getBackground() {
8777 return mBackground;
8778 }
8779
8780 /**
8781 * Set an icon that goes with the content of this notification.
8782 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008783 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008784 public WearableExtender setContentIcon(int icon) {
8785 mContentIcon = icon;
8786 return this;
8787 }
8788
8789 /**
8790 * Get an icon that goes with the content of this notification.
8791 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008792 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008793 public int getContentIcon() {
8794 return mContentIcon;
8795 }
8796
8797 /**
8798 * Set the gravity that the content icon should have within the notification display.
8799 * Supported values include {@link android.view.Gravity#START} and
8800 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8801 * @see #setContentIcon
8802 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008803 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008804 public WearableExtender setContentIconGravity(int contentIconGravity) {
8805 mContentIconGravity = contentIconGravity;
8806 return this;
8807 }
8808
8809 /**
8810 * Get the gravity that the content icon should have within the notification display.
8811 * Supported values include {@link android.view.Gravity#START} and
8812 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8813 * @see #getContentIcon
8814 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008815 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008816 public int getContentIconGravity() {
8817 return mContentIconGravity;
8818 }
8819
8820 /**
Gus Prevasd7363752018-09-18 14:35:15 -04008821 * Set an action from this notification's actions as the primary action. If the action has a
8822 * {@link RemoteInput} associated with it, shortcuts to the options for that input are shown
8823 * directly on the notification.
Griff Hazen14f57992014-05-26 09:07:14 -07008824 *
Gus Prevasd7363752018-09-18 14:35:15 -04008825 * @param actionIndex The index of the primary action.
Griff Hazen14f57992014-05-26 09:07:14 -07008826 * If wearable actions were added to the main notification, this index
8827 * will apply to that list, otherwise it will apply to the regular
8828 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07008829 */
8830 public WearableExtender setContentAction(int actionIndex) {
8831 mContentActionIndex = actionIndex;
8832 return this;
8833 }
8834
8835 /**
Gus Prevasd7363752018-09-18 14:35:15 -04008836 * Get the index of the notification action, if any, that was specified as the primary
8837 * action.
Griff Hazen14f57992014-05-26 09:07:14 -07008838 *
8839 * <p>If wearable specific actions were added to the main notification, this index will
8840 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07008841 *
8842 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07008843 */
8844 public int getContentAction() {
8845 return mContentActionIndex;
8846 }
8847
8848 /**
8849 * Set the gravity that this notification should have within the available viewport space.
8850 * Supported values include {@link android.view.Gravity#TOP},
8851 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8852 * The default value is {@link android.view.Gravity#BOTTOM}.
8853 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008854 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008855 public WearableExtender setGravity(int gravity) {
8856 mGravity = gravity;
8857 return this;
8858 }
8859
8860 /**
8861 * Get the gravity that this notification should have within the available viewport space.
8862 * Supported values include {@link android.view.Gravity#TOP},
8863 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8864 * The default value is {@link android.view.Gravity#BOTTOM}.
8865 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008866 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008867 public int getGravity() {
8868 return mGravity;
8869 }
8870
8871 /**
8872 * Set the custom size preset for the display of this notification out of the available
8873 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
8874 * {@link #SIZE_LARGE}.
8875 * <p>Some custom size presets are only applicable for custom display notifications created
8876 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
8877 * documentation for the preset in question. See also
8878 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
8879 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008880 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008881 public WearableExtender setCustomSizePreset(int sizePreset) {
8882 mCustomSizePreset = sizePreset;
8883 return this;
8884 }
8885
8886 /**
8887 * Get the custom size preset for the display of this notification out of the available
8888 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
8889 * {@link #SIZE_LARGE}.
8890 * <p>Some custom size presets are only applicable for custom display notifications created
8891 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
8892 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
8893 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008894 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008895 public int getCustomSizePreset() {
8896 return mCustomSizePreset;
8897 }
8898
8899 /**
8900 * Set the custom height in pixels for the display of this notification's content.
8901 * <p>This option is only available for custom display notifications created
8902 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
8903 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
8904 * {@link #getCustomContentHeight}.
8905 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008906 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008907 public WearableExtender setCustomContentHeight(int height) {
8908 mCustomContentHeight = height;
8909 return this;
8910 }
8911
8912 /**
8913 * Get the custom height in pixels for the display of this notification's content.
8914 * <p>This option is only available for custom display notifications created
8915 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
8916 * {@link #setCustomContentHeight}.
8917 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008918 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008919 public int getCustomContentHeight() {
8920 return mCustomContentHeight;
8921 }
8922
8923 /**
8924 * Set whether the scrolling position for the contents of this notification should start
8925 * at the bottom of the contents instead of the top when the contents are too long to
8926 * display within the screen. Default is false (start scroll at the top).
8927 */
8928 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
8929 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
8930 return this;
8931 }
8932
8933 /**
8934 * Get whether the scrolling position for the contents of this notification should start
8935 * at the bottom of the contents instead of the top when the contents are too long to
8936 * display within the screen. Default is false (start scroll at the top).
8937 */
8938 public boolean getStartScrollBottom() {
8939 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
8940 }
8941
8942 /**
8943 * Set whether the content intent is available when the wearable device is not connected
8944 * to a companion device. The user can still trigger this intent when the wearable device
8945 * is offline, but a visual hint will indicate that the content intent may not be available.
8946 * Defaults to true.
8947 */
8948 public WearableExtender setContentIntentAvailableOffline(
8949 boolean contentIntentAvailableOffline) {
8950 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
8951 return this;
8952 }
8953
8954 /**
8955 * Get whether the content intent is available when the wearable device is not connected
8956 * to a companion device. The user can still trigger this intent when the wearable device
8957 * is offline, but a visual hint will indicate that the content intent may not be available.
8958 * Defaults to true.
8959 */
8960 public boolean getContentIntentAvailableOffline() {
8961 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
8962 }
8963
8964 /**
8965 * Set a hint that this notification's icon should not be displayed.
8966 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
8967 * @return this object for method chaining
8968 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008969 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008970 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
8971 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
8972 return this;
8973 }
8974
8975 /**
8976 * Get a hint that this notification's icon should not be displayed.
8977 * @return {@code true} if this icon should not be displayed, false otherwise.
8978 * The default value is {@code false} if this was never set.
8979 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008980 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008981 public boolean getHintHideIcon() {
8982 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
8983 }
8984
8985 /**
8986 * Set a visual hint that only the background image of this notification should be
8987 * displayed, and other semantic content should be hidden. This hint is only applicable
8988 * to sub-pages added using {@link #addPage}.
8989 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008990 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008991 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
8992 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
8993 return this;
8994 }
8995
8996 /**
8997 * Get a visual hint that only the background image of this notification should be
8998 * displayed, and other semantic content should be hidden. This hint is only applicable
8999 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
9000 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009001 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009002 public boolean getHintShowBackgroundOnly() {
9003 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
9004 }
9005
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009006 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009007 * Set a hint that this notification's background should not be clipped if possible,
9008 * and should instead be resized to fully display on the screen, retaining the aspect
9009 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009010 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
9011 * @return this object for method chaining
9012 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009013 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009014 public WearableExtender setHintAvoidBackgroundClipping(
9015 boolean hintAvoidBackgroundClipping) {
9016 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
9017 return this;
9018 }
9019
9020 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009021 * Get a hint that this notification's background should not be clipped if possible,
9022 * and should instead be resized to fully display on the screen, retaining the aspect
9023 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009024 * @return {@code true} if it's ok if the background is clipped on the screen, false
9025 * otherwise. The default value is {@code false} if this was never set.
9026 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009027 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009028 public boolean getHintAvoidBackgroundClipping() {
9029 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
9030 }
9031
9032 /**
9033 * Set a hint that the screen should remain on for at least this duration when
9034 * this notification is displayed on the screen.
9035 * @param timeout The requested screen timeout in milliseconds. Can also be either
9036 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9037 * @return this object for method chaining
9038 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009039 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009040 public WearableExtender setHintScreenTimeout(int timeout) {
9041 mHintScreenTimeout = timeout;
9042 return this;
9043 }
9044
9045 /**
9046 * Get the duration, in milliseconds, that the screen should remain on for
9047 * when this notification is displayed.
9048 * @return the duration in milliseconds if > 0, or either one of the sentinel values
9049 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9050 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009051 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009052 public int getHintScreenTimeout() {
9053 return mHintScreenTimeout;
9054 }
9055
Alex Hills9ab3a232016-04-05 14:54:56 -04009056 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04009057 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
9058 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9059 * qr codes, as well as other simple black-and-white tickets.
9060 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
9061 * @return this object for method chaining
Gus Prevasd7363752018-09-18 14:35:15 -04009062 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009063 */
Gus Prevasd7363752018-09-18 14:35:15 -04009064 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009065 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
9066 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
9067 return this;
9068 }
9069
9070 /**
9071 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
9072 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9073 * qr codes, as well as other simple black-and-white tickets.
9074 * @return {@code true} if it should be displayed in ambient, false otherwise
9075 * otherwise. The default value is {@code false} if this was never set.
Gus Prevasd7363752018-09-18 14:35:15 -04009076 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009077 */
Gus Prevasd7363752018-09-18 14:35:15 -04009078 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009079 public boolean getHintAmbientBigPicture() {
9080 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
9081 }
9082
9083 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04009084 * Set a hint that this notification's content intent will launch an {@link Activity}
9085 * directly, telling the platform that it can generate the appropriate transitions.
9086 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
9087 * an activity and transitions should be generated, false otherwise.
9088 * @return this object for method chaining
9089 */
9090 public WearableExtender setHintContentIntentLaunchesActivity(
9091 boolean hintContentIntentLaunchesActivity) {
9092 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
9093 return this;
9094 }
9095
9096 /**
9097 * Get a hint that this notification's content intent will launch an {@link Activity}
9098 * directly, telling the platform that it can generate the appropriate transitions
9099 * @return {@code true} if the content intent will launch an activity and transitions should
9100 * be generated, false otherwise. The default value is {@code false} if this was never set.
9101 */
9102 public boolean getHintContentIntentLaunchesActivity() {
9103 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
9104 }
9105
Nadia Benbernou948627e2016-04-14 14:41:08 -04009106 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009107 * Sets the dismissal id for this notification. If a notification is posted with a
9108 * dismissal id, then when that notification is canceled, notifications on other wearables
9109 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04009110 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009111 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04009112 * @param dismissalId the dismissal id of the notification.
9113 * @return this object for method chaining
9114 */
9115 public WearableExtender setDismissalId(String dismissalId) {
9116 mDismissalId = dismissalId;
9117 return this;
9118 }
9119
9120 /**
9121 * Returns the dismissal id of the notification.
9122 * @return the dismissal id of the notification or null if it has not been set.
9123 */
9124 public String getDismissalId() {
9125 return mDismissalId;
9126 }
9127
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009128 /**
9129 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
9130 * posted from a phone to provide finer-grained control on what notifications are bridged
9131 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
9132 * Features to Notifications</a> for more information.
9133 * @param bridgeTag the bridge tag of the notification.
9134 * @return this object for method chaining
9135 */
9136 public WearableExtender setBridgeTag(String bridgeTag) {
9137 mBridgeTag = bridgeTag;
9138 return this;
9139 }
9140
9141 /**
9142 * Returns the bridge tag of the notification.
9143 * @return the bridge tag or null if not present.
9144 */
9145 public String getBridgeTag() {
9146 return mBridgeTag;
9147 }
9148
Griff Hazen61a9e862014-05-22 16:05:19 -07009149 private void setFlag(int mask, boolean value) {
9150 if (value) {
9151 mFlags |= mask;
9152 } else {
9153 mFlags &= ~mask;
9154 }
9155 }
9156 }
9157
9158 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009159 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
9160 * with car extensions:
9161 *
9162 * <ol>
9163 * <li>Create an {@link Notification.Builder}, setting any desired
9164 * properties.
9165 * <li>Create a {@link CarExtender}.
9166 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
9167 * {@link CarExtender}.
9168 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9169 * to apply the extensions to a notification.
9170 * </ol>
9171 *
9172 * <pre class="prettyprint">
9173 * Notification notification = new Notification.Builder(context)
9174 * ...
9175 * .extend(new CarExtender()
9176 * .set*(...))
9177 * .build();
9178 * </pre>
9179 *
9180 * <p>Car extensions can be accessed on an existing notification by using the
9181 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
9182 * to access values.
9183 */
9184 public static final class CarExtender implements Extender {
9185 private static final String TAG = "CarExtender";
9186
9187 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
9188 private static final String EXTRA_LARGE_ICON = "large_icon";
9189 private static final String EXTRA_CONVERSATION = "car_conversation";
9190 private static final String EXTRA_COLOR = "app_color";
9191
9192 private Bitmap mLargeIcon;
9193 private UnreadConversation mUnreadConversation;
9194 private int mColor = Notification.COLOR_DEFAULT;
9195
9196 /**
9197 * Create a {@link CarExtender} with default options.
9198 */
9199 public CarExtender() {
9200 }
9201
9202 /**
9203 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
9204 *
9205 * @param notif The notification from which to copy options.
9206 */
9207 public CarExtender(Notification notif) {
9208 Bundle carBundle = notif.extras == null ?
9209 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
9210 if (carBundle != null) {
9211 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
9212 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
9213
9214 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
9215 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
9216 }
9217 }
9218
9219 /**
9220 * Apply car extensions to a notification that is being built. This is typically called by
9221 * the {@link Notification.Builder#extend(Notification.Extender)}
9222 * method of {@link Notification.Builder}.
9223 */
9224 @Override
9225 public Notification.Builder extend(Notification.Builder builder) {
9226 Bundle carExtensions = new Bundle();
9227
9228 if (mLargeIcon != null) {
9229 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
9230 }
9231 if (mColor != Notification.COLOR_DEFAULT) {
9232 carExtensions.putInt(EXTRA_COLOR, mColor);
9233 }
9234
9235 if (mUnreadConversation != null) {
9236 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
9237 carExtensions.putBundle(EXTRA_CONVERSATION, b);
9238 }
9239
9240 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
9241 return builder;
9242 }
9243
9244 /**
9245 * Sets the accent color to use when Android Auto presents the notification.
9246 *
9247 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
9248 * to accent the displayed notification. However, not all colors are acceptable in an
9249 * automotive setting. This method can be used to override the color provided in the
9250 * notification in such a situation.
9251 */
Tor Norbye80756e32015-03-02 09:39:27 -08009252 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009253 mColor = color;
9254 return this;
9255 }
9256
9257 /**
9258 * Gets the accent color.
9259 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04009260 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009261 */
Tor Norbye80756e32015-03-02 09:39:27 -08009262 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009263 public int getColor() {
9264 return mColor;
9265 }
9266
9267 /**
9268 * Sets the large icon of the car notification.
9269 *
9270 * If no large icon is set in the extender, Android Auto will display the icon
9271 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
9272 *
9273 * @param largeIcon The large icon to use in the car notification.
9274 * @return This object for method chaining.
9275 */
9276 public CarExtender setLargeIcon(Bitmap largeIcon) {
9277 mLargeIcon = largeIcon;
9278 return this;
9279 }
9280
9281 /**
9282 * Gets the large icon used in this car notification, or null if no icon has been set.
9283 *
9284 * @return The large icon for the car notification.
9285 * @see CarExtender#setLargeIcon
9286 */
9287 public Bitmap getLargeIcon() {
9288 return mLargeIcon;
9289 }
9290
9291 /**
9292 * Sets the unread conversation in a message notification.
9293 *
9294 * @param unreadConversation The unread part of the conversation this notification conveys.
9295 * @return This object for method chaining.
9296 */
9297 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
9298 mUnreadConversation = unreadConversation;
9299 return this;
9300 }
9301
9302 /**
9303 * Returns the unread conversation conveyed by this notification.
9304 * @see #setUnreadConversation(UnreadConversation)
9305 */
9306 public UnreadConversation getUnreadConversation() {
9307 return mUnreadConversation;
9308 }
9309
9310 /**
9311 * A class which holds the unread messages from a conversation.
9312 */
9313 public static class UnreadConversation {
9314 private static final String KEY_AUTHOR = "author";
9315 private static final String KEY_TEXT = "text";
9316 private static final String KEY_MESSAGES = "messages";
9317 private static final String KEY_REMOTE_INPUT = "remote_input";
9318 private static final String KEY_ON_REPLY = "on_reply";
9319 private static final String KEY_ON_READ = "on_read";
9320 private static final String KEY_PARTICIPANTS = "participants";
9321 private static final String KEY_TIMESTAMP = "timestamp";
9322
9323 private final String[] mMessages;
9324 private final RemoteInput mRemoteInput;
9325 private final PendingIntent mReplyPendingIntent;
9326 private final PendingIntent mReadPendingIntent;
9327 private final String[] mParticipants;
9328 private final long mLatestTimestamp;
9329
9330 UnreadConversation(String[] messages, RemoteInput remoteInput,
9331 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
9332 String[] participants, long latestTimestamp) {
9333 mMessages = messages;
9334 mRemoteInput = remoteInput;
9335 mReadPendingIntent = readPendingIntent;
9336 mReplyPendingIntent = replyPendingIntent;
9337 mParticipants = participants;
9338 mLatestTimestamp = latestTimestamp;
9339 }
9340
9341 /**
9342 * Gets the list of messages conveyed by this notification.
9343 */
9344 public String[] getMessages() {
9345 return mMessages;
9346 }
9347
9348 /**
9349 * Gets the remote input that will be used to convey the response to a message list, or
9350 * null if no such remote input exists.
9351 */
9352 public RemoteInput getRemoteInput() {
9353 return mRemoteInput;
9354 }
9355
9356 /**
9357 * Gets the pending intent that will be triggered when the user replies to this
9358 * notification.
9359 */
9360 public PendingIntent getReplyPendingIntent() {
9361 return mReplyPendingIntent;
9362 }
9363
9364 /**
9365 * Gets the pending intent that Android Auto will send after it reads aloud all messages
9366 * in this object's message list.
9367 */
9368 public PendingIntent getReadPendingIntent() {
9369 return mReadPendingIntent;
9370 }
9371
9372 /**
9373 * Gets the participants in the conversation.
9374 */
9375 public String[] getParticipants() {
9376 return mParticipants;
9377 }
9378
9379 /**
9380 * Gets the firs participant in the conversation.
9381 */
9382 public String getParticipant() {
9383 return mParticipants.length > 0 ? mParticipants[0] : null;
9384 }
9385
9386 /**
9387 * Gets the timestamp of the conversation.
9388 */
9389 public long getLatestTimestamp() {
9390 return mLatestTimestamp;
9391 }
9392
9393 Bundle getBundleForUnreadConversation() {
9394 Bundle b = new Bundle();
9395 String author = null;
9396 if (mParticipants != null && mParticipants.length > 1) {
9397 author = mParticipants[0];
9398 }
9399 Parcelable[] messages = new Parcelable[mMessages.length];
9400 for (int i = 0; i < messages.length; i++) {
9401 Bundle m = new Bundle();
9402 m.putString(KEY_TEXT, mMessages[i]);
9403 m.putString(KEY_AUTHOR, author);
9404 messages[i] = m;
9405 }
9406 b.putParcelableArray(KEY_MESSAGES, messages);
9407 if (mRemoteInput != null) {
9408 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
9409 }
9410 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
9411 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
9412 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
9413 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
9414 return b;
9415 }
9416
9417 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
9418 if (b == null) {
9419 return null;
9420 }
9421 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
9422 String[] messages = null;
9423 if (parcelableMessages != null) {
9424 String[] tmp = new String[parcelableMessages.length];
9425 boolean success = true;
9426 for (int i = 0; i < tmp.length; i++) {
9427 if (!(parcelableMessages[i] instanceof Bundle)) {
9428 success = false;
9429 break;
9430 }
9431 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
9432 if (tmp[i] == null) {
9433 success = false;
9434 break;
9435 }
9436 }
9437 if (success) {
9438 messages = tmp;
9439 } else {
9440 return null;
9441 }
9442 }
9443
9444 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
9445 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
9446
9447 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
9448
9449 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
9450 if (participants == null || participants.length != 1) {
9451 return null;
9452 }
9453
9454 return new UnreadConversation(messages,
9455 remoteInput,
9456 onReply,
9457 onRead,
9458 participants, b.getLong(KEY_TIMESTAMP));
9459 }
9460 };
9461
9462 /**
9463 * Builder class for {@link CarExtender.UnreadConversation} objects.
9464 */
9465 public static class Builder {
9466 private final List<String> mMessages = new ArrayList<String>();
9467 private final String mParticipant;
9468 private RemoteInput mRemoteInput;
9469 private PendingIntent mReadPendingIntent;
9470 private PendingIntent mReplyPendingIntent;
9471 private long mLatestTimestamp;
9472
9473 /**
9474 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
9475 *
9476 * @param name The name of the other participant in the conversation.
9477 */
9478 public Builder(String name) {
9479 mParticipant = name;
9480 }
9481
9482 /**
9483 * Appends a new unread message to the list of messages for this conversation.
9484 *
9485 * The messages should be added from oldest to newest.
9486 *
9487 * @param message The text of the new unread message.
9488 * @return This object for method chaining.
9489 */
9490 public Builder addMessage(String message) {
9491 mMessages.add(message);
9492 return this;
9493 }
9494
9495 /**
9496 * Sets the pending intent and remote input which will convey the reply to this
9497 * notification.
9498 *
9499 * @param pendingIntent The pending intent which will be triggered on a reply.
9500 * @param remoteInput The remote input parcelable which will carry the reply.
9501 * @return This object for method chaining.
9502 *
9503 * @see CarExtender.UnreadConversation#getRemoteInput
9504 * @see CarExtender.UnreadConversation#getReplyPendingIntent
9505 */
9506 public Builder setReplyAction(
9507 PendingIntent pendingIntent, RemoteInput remoteInput) {
9508 mRemoteInput = remoteInput;
9509 mReplyPendingIntent = pendingIntent;
9510
9511 return this;
9512 }
9513
9514 /**
9515 * Sets the pending intent that will be sent once the messages in this notification
9516 * are read.
9517 *
9518 * @param pendingIntent The pending intent to use.
9519 * @return This object for method chaining.
9520 */
9521 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
9522 mReadPendingIntent = pendingIntent;
9523 return this;
9524 }
9525
9526 /**
9527 * Sets the timestamp of the most recent message in an unread conversation.
9528 *
9529 * If a messaging notification has been posted by your application and has not
9530 * yet been cancelled, posting a later notification with the same id and tag
9531 * but without a newer timestamp may result in Android Auto not displaying a
9532 * heads up notification for the later notification.
9533 *
9534 * @param timestamp The timestamp of the most recent message in the conversation.
9535 * @return This object for method chaining.
9536 */
9537 public Builder setLatestTimestamp(long timestamp) {
9538 mLatestTimestamp = timestamp;
9539 return this;
9540 }
9541
9542 /**
9543 * Builds a new unread conversation object.
9544 *
9545 * @return The new unread conversation object.
9546 */
9547 public UnreadConversation build() {
9548 String[] messages = mMessages.toArray(new String[mMessages.size()]);
9549 String[] participants = { mParticipant };
9550 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
9551 mReadPendingIntent, participants, mLatestTimestamp);
9552 }
9553 }
9554 }
9555
9556 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009557 * <p>Helper class to add Android TV extensions to notifications. To create a notification
9558 * with a TV extension:
9559 *
9560 * <ol>
9561 * <li>Create an {@link Notification.Builder}, setting any desired properties.
9562 * <li>Create a {@link TvExtender}.
9563 * <li>Set TV-specific properties using the {@code set} methods of
9564 * {@link TvExtender}.
9565 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9566 * to apply the extension to a notification.
9567 * </ol>
9568 *
9569 * <pre class="prettyprint">
9570 * Notification notification = new Notification.Builder(context)
9571 * ...
9572 * .extend(new TvExtender()
9573 * .set*(...))
9574 * .build();
9575 * </pre>
9576 *
9577 * <p>TV extensions can be accessed on an existing notification by using the
9578 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
9579 * to access values.
9580 *
9581 * @hide
9582 */
9583 @SystemApi
9584 public static final class TvExtender implements Extender {
9585 private static final String TAG = "TvExtender";
9586
9587 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
9588 private static final String EXTRA_FLAGS = "flags";
9589 private static final String EXTRA_CONTENT_INTENT = "content_intent";
9590 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009591 private static final String EXTRA_CHANNEL_ID = "channel_id";
Rhiannon Malia1a083932018-01-24 15:02:30 -08009592 private static final String EXTRA_SUPPRESS_SHOW_OVER_APPS = "suppressShowOverApps";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009593
9594 // Flags bitwise-ored to mFlags
9595 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
9596
9597 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009598 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009599 private PendingIntent mContentIntent;
9600 private PendingIntent mDeleteIntent;
Rhiannon Malia1a083932018-01-24 15:02:30 -08009601 private boolean mSuppressShowOverApps;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009602
9603 /**
9604 * Create a {@link TvExtender} with default options.
9605 */
9606 public TvExtender() {
9607 mFlags = FLAG_AVAILABLE_ON_TV;
9608 }
9609
9610 /**
9611 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
9612 *
9613 * @param notif The notification from which to copy options.
9614 */
9615 public TvExtender(Notification notif) {
9616 Bundle bundle = notif.extras == null ?
9617 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
9618 if (bundle != null) {
9619 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009620 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Rhiannon Malia1a083932018-01-24 15:02:30 -08009621 mSuppressShowOverApps = bundle.getBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009622 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
9623 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
9624 }
9625 }
9626
9627 /**
9628 * Apply a TV extension to a notification that is being built. This is typically called by
9629 * the {@link Notification.Builder#extend(Notification.Extender)}
9630 * method of {@link Notification.Builder}.
9631 */
9632 @Override
9633 public Notification.Builder extend(Notification.Builder builder) {
9634 Bundle bundle = new Bundle();
9635
9636 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009637 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Rhiannon Malia1a083932018-01-24 15:02:30 -08009638 bundle.putBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS, mSuppressShowOverApps);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009639 if (mContentIntent != null) {
9640 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
9641 }
9642
9643 if (mDeleteIntent != null) {
9644 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
9645 }
9646
9647 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
9648 return builder;
9649 }
9650
9651 /**
9652 * Returns true if this notification should be shown on TV. This method return true
9653 * if the notification was extended with a TvExtender.
9654 */
9655 public boolean isAvailableOnTv() {
9656 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
9657 }
9658
9659 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009660 * Specifies the channel the notification should be delivered on when shown on TV.
9661 * It can be different from the channel that the notification is delivered to when
9662 * posting on a non-TV device.
9663 */
9664 public TvExtender setChannel(String channelId) {
9665 mChannelId = channelId;
9666 return this;
9667 }
9668
9669 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04009670 * Specifies the channel the notification should be delivered on when shown on TV.
9671 * It can be different from the channel that the notification is delivered to when
9672 * posting on a non-TV device.
9673 */
9674 public TvExtender setChannelId(String channelId) {
9675 mChannelId = channelId;
9676 return this;
9677 }
9678
Jeff Sharkey000ce802017-04-29 13:13:27 -06009679 /** @removed */
9680 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009681 public String getChannel() {
9682 return mChannelId;
9683 }
9684
9685 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04009686 * Returns the id of the channel this notification posts to on TV.
9687 */
9688 public String getChannelId() {
9689 return mChannelId;
9690 }
9691
9692 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009693 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
9694 * If provided, it is used instead of the content intent specified
9695 * at the level of Notification.
9696 */
9697 public TvExtender setContentIntent(PendingIntent intent) {
9698 mContentIntent = intent;
9699 return this;
9700 }
9701
9702 /**
9703 * Returns the TV-specific content intent. If this method returns null, the
9704 * main content intent on the notification should be used.
9705 *
9706 * @see {@link Notification#contentIntent}
9707 */
9708 public PendingIntent getContentIntent() {
9709 return mContentIntent;
9710 }
9711
9712 /**
9713 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
9714 * by the user on TV. If provided, it is used instead of the delete intent specified
9715 * at the level of Notification.
9716 */
9717 public TvExtender setDeleteIntent(PendingIntent intent) {
9718 mDeleteIntent = intent;
9719 return this;
9720 }
9721
9722 /**
9723 * Returns the TV-specific delete intent. If this method returns null, the
9724 * main delete intent on the notification should be used.
9725 *
9726 * @see {@link Notification#deleteIntent}
9727 */
9728 public PendingIntent getDeleteIntent() {
9729 return mDeleteIntent;
9730 }
Rhiannon Malia1a083932018-01-24 15:02:30 -08009731
9732 /**
9733 * Specifies whether this notification should suppress showing a message over top of apps
9734 * outside of the launcher.
9735 */
9736 public TvExtender setSuppressShowOverApps(boolean suppress) {
9737 mSuppressShowOverApps = suppress;
9738 return this;
9739 }
9740
9741 /**
9742 * Returns true if this notification should not show messages over top of apps
9743 * outside of the launcher.
9744 */
9745 public boolean getSuppressShowOverApps() {
9746 return mSuppressShowOverApps;
9747 }
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009748 }
9749
9750 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07009751 * Get an array of Notification objects from a parcelable array bundle field.
9752 * Update the bundle to have a typed array so fetches in the future don't need
9753 * to do an array copy.
9754 */
9755 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
9756 Parcelable[] array = bundle.getParcelableArray(key);
9757 if (array instanceof Notification[] || array == null) {
9758 return (Notification[]) array;
9759 }
9760 Notification[] typedArray = Arrays.copyOf(array, array.length,
9761 Notification[].class);
9762 bundle.putParcelableArray(key, typedArray);
9763 return typedArray;
9764 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02009765
9766 private static class BuilderRemoteViews extends RemoteViews {
9767 public BuilderRemoteViews(Parcel parcel) {
9768 super(parcel);
9769 }
9770
Kenny Guy77320062014-08-27 21:37:15 +01009771 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
9772 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02009773 }
9774
9775 @Override
9776 public BuilderRemoteViews clone() {
9777 Parcel p = Parcel.obtain();
9778 writeToParcel(p, 0);
9779 p.setDataPosition(0);
9780 BuilderRemoteViews brv = new BuilderRemoteViews(p);
9781 p.recycle();
9782 return brv;
9783 }
9784 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08009785
Selim Cinek384804b2018-04-18 14:31:07 +08009786 /**
9787 * A result object where information about the template that was created is saved.
9788 */
9789 private static class TemplateBindResult {
Selim Cinek1c72fa02018-04-23 18:00:54 +08009790 int mIconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +08009791
9792 /**
Selim Cinek1c72fa02018-04-23 18:00:54 +08009793 * Get the margin end that needs to be added to any fields that may overlap
Selim Cinek384804b2018-04-18 14:31:07 +08009794 * with the right actions.
9795 */
Selim Cinek1c72fa02018-04-23 18:00:54 +08009796 public int getIconMarginEnd() {
9797 return mIconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +08009798 }
9799
Selim Cinek1c72fa02018-04-23 18:00:54 +08009800 public void setIconMarginEnd(int iconMarginEnd) {
9801 this.mIconMarginEnd = iconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +08009802 }
9803 }
9804
Adrian Roos70d7aa32017-01-11 15:39:06 -08009805 private static class StandardTemplateParams {
9806 boolean hasProgress = true;
9807 boolean ambient = false;
9808 CharSequence title;
9809 CharSequence text;
Selim Cinekafeed292017-12-12 17:32:44 -08009810 CharSequence headerTextSecondary;
Selim Cinekbee4e072018-05-21 22:06:43 -07009811 int maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek88188f22017-09-19 16:46:56 -07009812 boolean hideLargeIcon;
Selim Cinek384804b2018-04-18 14:31:07 +08009813 boolean hideReplyIcon;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009814
9815 final StandardTemplateParams reset() {
9816 hasProgress = true;
9817 ambient = false;
9818 title = null;
9819 text = null;
Selim Cinekafeed292017-12-12 17:32:44 -08009820 headerTextSecondary = null;
Selim Cinekbee4e072018-05-21 22:06:43 -07009821 maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009822 return this;
9823 }
9824
9825 final StandardTemplateParams hasProgress(boolean hasProgress) {
9826 this.hasProgress = hasProgress;
9827 return this;
9828 }
9829
9830 final StandardTemplateParams title(CharSequence title) {
9831 this.title = title;
9832 return this;
9833 }
9834
9835 final StandardTemplateParams text(CharSequence text) {
9836 this.text = text;
9837 return this;
9838 }
9839
Selim Cinekafeed292017-12-12 17:32:44 -08009840 final StandardTemplateParams headerTextSecondary(CharSequence text) {
9841 this.headerTextSecondary = text;
9842 return this;
9843 }
9844
Selim Cinek384804b2018-04-18 14:31:07 +08009845 final StandardTemplateParams hideLargeIcon(boolean hideLargeIcon) {
9846 this.hideLargeIcon = hideLargeIcon;
Selim Cinek88188f22017-09-19 16:46:56 -07009847 return this;
9848 }
9849
Selim Cinek384804b2018-04-18 14:31:07 +08009850 final StandardTemplateParams hideReplyIcon(boolean hideReplyIcon) {
9851 this.hideReplyIcon = hideReplyIcon;
Selim Cinek88188f22017-09-19 16:46:56 -07009852 return this;
9853 }
9854
Adrian Roos70d7aa32017-01-11 15:39:06 -08009855 final StandardTemplateParams ambient(boolean ambient) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08009856 Preconditions.checkState(title == null && text == null, "must set ambient before text");
Adrian Roos70d7aa32017-01-11 15:39:06 -08009857 this.ambient = ambient;
9858 return this;
9859 }
9860
9861 final StandardTemplateParams fillTextsFrom(Builder b) {
9862 Bundle extras = b.mN.extras;
Lucas Dupin06c5e642017-09-13 16:34:58 -07009863 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE), ambient);
9864
9865 // Big text notifications should contain their content when viewed in ambient mode.
9866 CharSequence text = extras.getCharSequence(EXTRA_BIG_TEXT);
9867 if (!ambient || TextUtils.isEmpty(text)) {
9868 text = extras.getCharSequence(EXTRA_TEXT);
9869 }
9870 this.text = b.processLegacyText(text, ambient);
Adrian Roos70d7aa32017-01-11 15:39:06 -08009871 return this;
9872 }
Selim Cinekbee4e072018-05-21 22:06:43 -07009873
9874 /**
9875 * Set the maximum lines of remote input history lines allowed.
9876 * @param maxRemoteInputHistory The number of lines.
9877 * @return The builder for method chaining.
9878 */
9879 public StandardTemplateParams setMaxRemoteInputHistory(int maxRemoteInputHistory) {
9880 this.maxRemoteInputHistory = maxRemoteInputHistory;
9881 return this;
9882 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08009883 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009884}